add arraytype def
This commit is contained in:
@ -91,6 +91,8 @@ int Type::getSize() const {
|
|||||||
return 8;
|
return 8;
|
||||||
case kVoid:
|
case kVoid:
|
||||||
return 0;
|
return 0;
|
||||||
|
case kArray:
|
||||||
|
return asArrayType()->getArraySize();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
49
src/IR.h
49
src/IR.h
@ -42,6 +42,7 @@ public:
|
|||||||
kLabel,
|
kLabel,
|
||||||
kPointer,
|
kPointer,
|
||||||
kFunction,
|
kFunction,
|
||||||
|
kArray,
|
||||||
};
|
};
|
||||||
Kind kind;
|
Kind kind;
|
||||||
|
|
||||||
@ -57,6 +58,9 @@ public:
|
|||||||
static Type *getPointerType(Type *baseType);
|
static Type *getPointerType(Type *baseType);
|
||||||
static Type *getFunctionType(Type *returnType,
|
static Type *getFunctionType(Type *returnType,
|
||||||
const std::vector<Type *> ¶mTypes = {});
|
const std::vector<Type *> ¶mTypes = {});
|
||||||
|
static Type *getArrayType(Type *elementType, const std::vector<int> &dims = {}) {
|
||||||
|
return ArrayType::get(elementType, dims);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Kind getKind() const { return kind; }
|
Kind getKind() const { return kind; }
|
||||||
@ -66,8 +70,12 @@ public:
|
|||||||
bool isLabel() const { return kind == kLabel; }
|
bool isLabel() const { return kind == kLabel; }
|
||||||
bool isPointer() const { return kind == kPointer; }
|
bool isPointer() const { return kind == kPointer; }
|
||||||
bool isFunction() const { return kind == kFunction; }
|
bool isFunction() const { return kind == kFunction; }
|
||||||
|
bool isArray() const { return kind == kArray; }
|
||||||
bool isIntOrFloat() const { return kind == kInt or kind == kFloat; }
|
bool isIntOrFloat() const { return kind == kInt or kind == kFloat; }
|
||||||
int getSize() const;
|
int getSize() const;
|
||||||
|
ArrayType *asArrayType() const {
|
||||||
|
return isArray() ? static_cast<ArrayType*>(const_cast<Type*>(this)) : nullptr;
|
||||||
|
}
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::enable_if_t<std::is_base_of_v<Type, T>, T *> as() const {
|
std::enable_if_t<std::is_base_of_v<Type, T>, T *> as() const {
|
||||||
return dynamic_cast<T *>(const_cast<Type *>(this));
|
return dynamic_cast<T *>(const_cast<Type *>(this));
|
||||||
@ -110,6 +118,47 @@ public:
|
|||||||
int getNumParams() const { return paramTypes.size(); }
|
int getNumParams() const { return paramTypes.size(); }
|
||||||
}; // class FunctionType
|
}; // class FunctionType
|
||||||
|
|
||||||
|
class ArrayType : public Type {
|
||||||
|
private:
|
||||||
|
Type *elementType; // 数组元素类型
|
||||||
|
std::vector<int> dimensions; // 维度信息(空向量表示未知大小)
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ArrayType(Type *elemType, const std::vector<int> &dims = {})
|
||||||
|
: Type(kArray), elementType(elemType), dimensions(dims) {
|
||||||
|
// 确保元素类型有效
|
||||||
|
assert(elemType && "Array element type cannot be null");
|
||||||
|
assert(!elemType->isVoid() && "Cannot have array of void");
|
||||||
|
assert(!elemType->isLabel() && "Cannot have array of labels");
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
// 获取数组类型(带缓存机制)
|
||||||
|
static ArrayType *get(Type *elemType, const std::vector<int> &dims = {}) {
|
||||||
|
// 实现类型缓存池(避免重复创建)
|
||||||
|
static std::map<std::pair<Type*, std::vector<int>>, ArrayType*> cache;
|
||||||
|
|
||||||
|
auto key = std::make_pair(elemType, dims);
|
||||||
|
if (cache.find(key) == cache.end()) {
|
||||||
|
cache[key] = new ArrayType(elemType, dims);
|
||||||
|
}
|
||||||
|
return cache[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
Type *getElementType() const { return elementType; }
|
||||||
|
const std::vector<int>& getDimensions() const { return dimensions; }
|
||||||
|
size_t getNumDimensions() const { return dimensions.size(); }
|
||||||
|
|
||||||
|
int getArraySize() const {
|
||||||
|
int size = elementType->getSize();
|
||||||
|
for (int dim : dimensions) {
|
||||||
|
size *= dim;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
};//class ArrayType
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user