1 #ifndef __TAN_SRC_AST_TYPE_H__
2 #define __TAN_SRC_AST_TYPE_H__
27 [[nodiscard]]
static PrimitiveType *GetIntegerType(
size_t bit_size,
bool is_unsigned);
28 [[nodiscard]]
static PrimitiveType *GetFloatType(
size_t bit_size);
30 [[nodiscard]]
static StringType *GetStringType();
32 [[nodiscard]]
static ArrayType *GetArrayType(
Type *element_type,
int size);
34 [[nodiscard]]
static FunctionType *GetFunctionType(
Type *ret_type,
const vector<Type *> &arg_types);
36 [[nodiscard]]
static TypeRef *GetTypeRef(
const str &name);
38 static inline vector<str> ALL_TYPE_NAMES{
"bool",
"int",
"float",
"f32",
"str",
"char",
"f64",
"i8",
39 "u8",
"i16",
"u16",
"i32",
"u32",
"i64",
"u64",
"void"};
49 virtual ~
Type() =
default;
51 [[nodiscard]]
virtual bool is_primitive()
const;
52 [[nodiscard]]
virtual bool is_pointer()
const;
53 [[nodiscard]]
virtual bool is_array()
const;
54 [[nodiscard]]
virtual bool is_string()
const;
55 [[nodiscard]]
virtual bool is_struct()
const;
56 [[nodiscard]]
virtual bool is_function()
const;
57 [[nodiscard]]
virtual bool is_ref()
const;
58 [[nodiscard]]
virtual bool is_float()
const;
59 [[nodiscard]]
virtual bool is_int()
const;
60 [[nodiscard]]
virtual bool is_num()
const;
61 [[nodiscard]]
virtual bool is_unsigned()
const;
62 [[nodiscard]]
virtual bool is_bool()
const;
63 [[nodiscard]]
virtual bool is_void()
const;
64 [[nodiscard]]
virtual bool is_char()
const;
66 virtual int get_align_bits();
67 virtual int get_size_bits();
69 [[nodiscard]]
virtual vector<Type *> children()
const;
70 [[nodiscard]]
bool is_canonical()
const;
71 [[nodiscard]]
const str &get_typename()
const {
return _type_name; }
81 static inline umap<Type *, PointerType *> POINTER_TYPE_CACHE{};
83 static inline umap<str, Type *> NAMED_TYPE_CACHE{};
103 static inline umap<Kind, int> SIZE_BITS{
117 static inline umap<Kind, str> TYPE_NAMES{
131 static const inline umap<str, Kind> TYPENAME_TO_KIND = {
152 [[nodiscard]]
bool is_primitive()
const override {
return true; }
153 [[nodiscard]]
bool is_float()
const override {
return _kind == F32 || _kind == F64; }
154 [[nodiscard]]
bool is_int()
const override {
return _kind >= I8 && _kind <= U64; }
155 [[nodiscard]]
bool is_num()
const override {
return _kind >= I8 && _kind <= F64; }
156 [[nodiscard]]
bool is_unsigned()
const override {
return _kind >= CHAR && _kind <= U64; };
157 [[nodiscard]]
bool is_bool()
const override {
return _kind == BOOL; }
158 [[nodiscard]]
bool is_void()
const override {
return _kind == VOID; }
159 [[nodiscard]]
bool is_char()
const override {
return _kind == CHAR; }
160 [[nodiscard]] vector<Type *> children()
const override {
return {}; }
162 int get_align_bits()
override;
163 int get_size_bits()
override;
169 static inline umap<PrimitiveType::Kind, PrimitiveType *> CACHE{};
176 [[nodiscard]]
bool is_pointer()
const override {
return true; }
177 Type *get_pointee() {
return _pointee_type; }
178 int get_align_bits()
override;
179 int get_size_bits()
override;
180 [[nodiscard]] vector<Type *> children()
const override;
188 Type *_pointee_type =
nullptr;
193 Type *get_element_type() {
return _element_type; }
194 int array_size() {
return _size; }
195 [[nodiscard]]
bool is_array()
const override {
return true; }
196 int get_align_bits()
override;
197 int get_size_bits()
override;
198 [[nodiscard]] vector<Type *> children()
const override;
206 Type *_element_type =
nullptr;
212 [[nodiscard]]
bool is_string()
const override {
return true; }
213 int get_align_bits()
override;
214 int get_size_bits()
override;
215 [[nodiscard]] vector<Type *> children()
const override {
return {}; }
225 [[nodiscard]]
bool is_struct()
const override {
return true; }
226 [[nodiscard]] vector<Type *> get_member_types()
const;
227 int get_align_bits()
override;
228 int get_size_bits()
override;
229 [[nodiscard]] vector<Type *> children()
const override;
232 void append_member_type(
Type *t);
233 Type *&operator[](
size_t index);
234 Type *operator[](
size_t index)
const;
242 vector<Type *> _member_types{};
248 [[nodiscard]]
bool is_function()
const override {
return true; }
249 [[nodiscard]]
Type *get_return_type()
const;
250 void set_return_type(
Type *t);
251 [[nodiscard]] vector<Type *> get_arg_types()
const;
252 void set_arg_types(
const vector<Type *> &arg_types);
253 [[nodiscard]] vector<Type *> children()
const override;
261 Type *_ret_type =
nullptr;
262 vector<Type *> _arg_types{};
271 [[nodiscard]]
bool is_ref()
const override {
return true; }
272 [[nodiscard]] vector<Type *> children()
const override {
return {}; }
275 explicit TypeRef(
const str &name);
Placeholder during parsing.
Type is immutable once created. The exception is StructType. Its information is updated in multiple s...
static bool IsCanonical(const Type &type)
A composite type is canonical only if its subtype(s) are also canonical. A non-composite type is cano...