00001
00002
00003 #ifndef type_rep_h
00004 #define type_rep_h
00005
00006 #include <string>
00007 #include <vector>
00008
00009
00010
00011
00012
00013
00014 class Ptree;
00015 class Abstract_scope;
00016 class Type_symbol;
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 enum Storage_class_specifier {
00049 s_None,
00050 s_Auto,
00051 s_Parameter,
00052 s_Register,
00053 s_Static,
00054 s_Extern,
00055 s_Mutable,
00056 s_Member
00057 };
00058
00059 enum Function_specifier {
00060 f_None = 0,
00061 f_Inline = 1,
00062 f_Virtual = 2,
00063 f_Explicit = 4,
00064 f_Abstract = 8
00065 };
00066 typedef int Function_specifier_set;
00067
00075 class Type {
00076 std::string code;
00077 int qualifiers;
00078 friend class Function_type_maker;
00079 public:
00080 enum Qualifier {
00081 q_Const,
00082 q_Volatile,
00083 q_Restrict
00084 };
00085 enum Kind {
00086 k_Fundamental,
00087 k_Pointer = 'P',
00088 k_Reference = 'R',
00089 k_Function = 'F',
00090 k_Array = 'A',
00091 k_Member = 'M',
00092 k_Userdef = 'Q',
00093 k_Ellipsis = 'E',
00094 k_Template = 'T',
00095 k_Nothing = 'O'
00096 };
00097 static const int ignore_qual = 0;
00098
00099 bool is_qualified(Qualifier q) const { return qualifiers & (1 << q); }
00100 bool is_qualified() const { return qualifiers != 0; }
00101 void add_qualifier(Qualifier q) { qualifiers |= (1 << q); }
00102 void remove_qualifier(Qualifier q) { qualifiers &= ~(1 << q); }
00103 bool is_valid() const { return code.length() != 0; }
00104
00105 void copy_qualifiers(const Type& other) { qualifiers |= other.qualifiers; }
00106
00107 const std::string& get_encoded_type() const { return code; }
00108
00109 Type get_unqualified_type() const { return Type(code); }
00110
00111 bool operator==(const Type& rhs) const { return rhs.code == code && ((rhs.qualifiers ^ qualifiers) & ~ignore_qual) == 0; }
00112 bool operator!=(const Type& rhs) const { return rhs.code != code || ((rhs.qualifiers ^ qualifiers) & ~ignore_qual) != 0; }
00113
00114 bool is_more_qualified_than(const Type& other) const { return (qualifiers & ~other.qualifiers) != 0; }
00115 bool is_same_qualified_as(const Type& other) const { return ((qualifiers ^ other.qualifiers) & ~ignore_qual) == 0; }
00116 bool is_same_unqualified_type(const Type& other) const { return code == other.code; }
00117
00118 public:
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00131 Type();
00132
00136 Type(std::string encoded);
00137
00139 static Type get_named_type(std::string name);
00140
00142 Type::Kind get_kind() const;
00143
00145 Type make_pointer_type() const;
00146
00148 Type make_reference_type() const;
00149
00151 Type make_array_type(Ptree* dimension) const;
00152
00157 Type make_member_type(Type membertype) const;
00158
00161 Type get_basis_type() const;
00162
00165 Type sans_reference() const;
00166
00167 Type sans_array() const;
00168
00170 Type get_class_type() const;
00171
00173 Type get_member_type() const;
00174
00176 Type get_return_type() const;
00177
00180 std::string get_function_signature() const;
00181
00183 Type get_function_arg(int n) const;
00184
00186 int get_num_function_args() const;
00187
00189 int get_template_argindex() const;
00190
00192 std::string get_human_readable_type() const;
00193
00194
00195
00197 bool is_void() const;
00198
00200 bool is_float() const;
00201
00203 bool is_int() const;
00204
00206 bool is_complete() const;
00207
00210 bool is_object_type() const;
00211
00214 bool is_arithmetic_type() const;
00215
00219 bool is_scalar_type() const;
00220
00222 bool is_pod() const;
00223
00224 bool is_aggregate() const;
00225
00231 bool is_compound_type() const;
00232
00233 bool is_enum_type() const;
00234
00235 bool is_class_type() const;
00236
00238 Type_symbol* get_type_symbol() const;
00239
00241 Type with_qualifier(Type::Qualifier q) const;
00242
00246 Type with_first_arg(Type t) const;
00247
00249 Type with_qualifiers(const Type& other) const;
00250
00254 Type get_promoted_integer() const;
00255
00257 bool is_qualification_convertible_to(Type t) const;
00258
00259 private:
00260 Type(std::string code, int qualifiers);
00261
00264 std::string encode_qualifiers(std::string prefix) const;
00265
00267 static std::string eat_type(std::string s);
00268 };
00269
00270 class Function_type_maker {
00271 std::string types;
00272
00273 public:
00274
00275
00280 Function_type_maker();
00281
00283 void add_parameter(Type t);
00284
00286 void add_ellipsis();
00287
00289 Type make_function_type(Type return_type) const;
00290 };
00291
00292
00293 extern Type int_type, uint_type, short_type, ushort_type, long_type,
00294 ulong_type, llong_type, ullong_type, char_type, schar_type,
00295 uchar_type, float_type, double_type, ldouble_type, bool_type,
00296 wchar_type, void_type, ctor_type;
00297
00298
00299 extern Type ptrdiff_type, size_type;
00300
00301 typedef std::vector<Type> Type_vector;
00302
00305 bool parse_qualifier(Ptree* tree, Type& type);
00306
00325 Type parse_type(Ptree* tree, Abstract_scope* scope, Ptree* name_for_anon, bool is_type_declaration);
00326
00327
00328
00335 bool parse_specifier(Ptree* tree, Storage_class_specifier* scs, Function_specifier_set* fs);
00336
00342 void parse_specifiers(Ptree* tree, Storage_class_specifier* scs, Function_specifier_set* fs);
00343
00345 const char* get_storage_specifier_name(Storage_class_specifier x);
00346
00348 std::string get_function_specifier_name(Function_specifier_set s);
00349
00352 Type make_unary_function_type(Type arg, Type ret);
00353
00354 Type make_binary_function_type(Type arg, Type brg, Type ret);
00355
00356
00357
00358
00359
00360
00361
00363 inline Type
00364 Type::make_pointer_type() const
00365 {
00366 return Type(encode_qualifiers("P"));
00367 }
00368
00369
00371 inline Type
00372 Type::make_reference_type() const
00373 {
00374 return Type(encode_qualifiers("R"));
00375 }
00376
00377
00378
00379
00381 inline bool
00382 Type::is_void() const
00383 {
00384 return code.length() && code[0] == 'v';
00385 }
00386
00387
00389 inline bool
00390 Type::is_float() const
00391 {
00392 return code.length() && (code[0] == 'd' || code[0] == 'e' || code[0] == 'f');
00393 }
00394
00395
00401 inline bool
00402 Type::is_compound_type() const
00403 {
00404 return get_kind() != k_Fundamental;
00405 }
00406
00407
00409 inline Type
00410 Type::with_qualifier(Type::Qualifier q) const
00411 {
00412 return Type(code, qualifiers | (1 << q));
00413 }
00414
00415
00419 inline Type
00420 Type::with_first_arg(Type t) const
00421 {
00422 assert(get_kind() == k_Function);
00423 Type t2 = *this;
00424 t2.code.insert(1, t.encode_qualifiers(""));
00425 return t2;
00426 }
00427
00428
00430 inline Type
00431 Type::with_qualifiers(const Type& other) const
00432 {
00433 return Type(code, qualifiers | other.qualifiers);
00434 }
00435
00436 #endif // type_rep_h