00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00213 #ifndef CTRUMP_PARSER_TYPE_H
00214 #define CTRUMP_PARSER_TYPE_H
00215
00216 #include "ctrump/common/symbol.h"
00217 #include "ctrump/common/mempool.h"
00218 #include "ctrump/ast/location.h"
00219 #include "ctrump/ast/source-string.h"
00220 #include "ctrump/common/dll.h"
00221
00222 #ifdef __cplusplus
00223 extern "C" {
00224 #endif
00225
00226 struct ctrump_texpr;
00227
00228 enum ctrump_builtin_type_code {
00229 CTRUMP_BOOL,
00230 CTRUMP_CHAR,
00231 CTRUMP_SCHAR,
00232 CTRUMP_UCHAR,
00233 CTRUMP_SSHORT,
00234 CTRUMP_USHORT,
00235 CTRUMP_SINT,
00236 CTRUMP_UINT,
00237 CTRUMP_SLONG,
00238 CTRUMP_ULONG,
00239 CTRUMP_SLONGLONG,
00240 CTRUMP_ULONGLONG,
00241 CTRUMP_FLOAT,
00242 CTRUMP_DOUBLE,
00243 CTRUMP_LONG_DOUBLE,
00244 CTRUMP_VOID,
00245
00246
00247 CTRUMP_VEC_SCHAR,
00248 CTRUMP_VEC_UCHAR,
00249 CTRUMP_VEC_SSHORT,
00250 CTRUMP_VEC_USHORT,
00251 CTRUMP_VEC_SINT,
00252 CTRUMP_VEC_UINT,
00253 CTRUMP_VEC_SLONG,
00254 CTRUMP_VEC_ULONG,
00255 CTRUMP_VEC_SLONGLONG,
00256 CTRUMP_VEC_ULONGLONG,
00257 CTRUMP_VEC_FLOAT,
00258 CTRUMP_VEC_DOUBLE,
00259 };
00260
00264 CTRUMP_EXTDEF extern const int ctrump_builtin_type_is_float_table[];
00265
00269 CTRUMP_EXTDEF extern const int ctrump_builtin_type_is_scalar_table[];
00270
00271 CTRUMP_EXTDEF int ctrump_type_is_float(const struct ctrump_texpr *type);
00272 CTRUMP_EXTDEF int ctrump_type_is_scalar(const struct ctrump_texpr *type);
00273
00274 enum ctrump_texpr_code {
00275 CTRUMP_TYPE_BUILTIN,
00276 CTRUMP_TYPE_UNION,
00277 CTRUMP_TYPE_STRUCT,
00278 CTRUMP_TYPE_ENUM,
00279 CTRUMP_TYPE_FUNC,
00280 CTRUMP_TYPE_POINTER,
00281 CTRUMP_TYPE_ARRAY,
00282 CTRUMP_TYPE_INCOMPLETE_ARRAY,
00283 CTRUMP_TYPE_VARLEN_ARRAY,
00284 CTRUMP_TYPE_TYPEDEF_NAME,
00285 CTRUMP_TYPE_QUALIFIED
00286 };
00287
00288 #define CTRUMP_ALIGN_UNSPECIFIED (-1)
00289
00290
00291 #define CTRUMP_QUAL_ATTR_PACKED (1<<0)
00292 #define CTRUMP_QUAL_ATTR_MAY_ALIAS (1<<1)
00293 #define CTRUMP_TYPE_ATTR_PACKED (1<<2)
00294
00296 struct ctrump_gccext_attribute {
00297 int aligned;
00298 int flags;
00299 };
00300
00301 #define CTRUMP_BITFIELD_UNSPECIFIED (-1)
00302
00306 struct ctrump_record_type_field{
00307 const struct ctrump_symbol *name;
00308 struct ctrump_texpr *type;
00309 int bitfield_width;
00310 struct ctrump_gccext_attribute attr;
00311 };
00312
00316 struct ctrump_record_type {
00317 const struct ctrump_symbol *name;
00318 int is_completed;
00319 int nfield;
00320 struct ctrump_record_type_field *fields;
00321 struct ctrump_gccext_attribute attr;
00322 };
00323
00327 struct ctrump_enum_type_field {
00328 const struct ctrump_symbol *name;
00329 long long value;
00330 };
00331
00335 struct ctrump_enum_type {
00336 const struct ctrump_symbol *name;
00337 int is_completed;
00338 int nfield;
00340 struct ctrump_enum_type_field *fields;
00341 };
00342
00355 struct ctrump_function_type {
00356 int arg_num;
00357 int has_unspecified_arg;
00358 struct ctrump_texpr *ret;
00359 struct ctrump_texpr **args;
00360 };
00361
00362
00363
00364
00365
00366
00367 #define CTRUMP_QUAL_CONST (1<<0)
00368 #define CTRUMP_QUAL_RESTRICT (1<<1)
00369 #define CTRUMP_QUAL_VOLATILE (1<<2)
00370
00371
00372
00373
00374 #define CTRUMP_QUAL_STATIC (1<<3)
00375
00379 struct ctrump_typedef_name {
00380 const struct ctrump_symbol *def_name;
00381 struct ctrump_texpr *defined_to;
00382 };
00383
00387 struct ctrump_qualified_type {
00388 struct ctrump_texpr *unqualified_type;
00389 struct ctrump_gccext_attribute attr;
00390 int qual_flags;
00391 };
00392
00396 struct ctrump_builtin_type {
00397 enum ctrump_builtin_type_code code;
00398 const char *name;
00399 };
00400
00404 struct ctrump_array_type {
00405 struct ctrump_texpr *array_of;
00406 int size;
00407 };
00408
00412 struct ctrump_array_type_chain {
00413 int size;
00414 struct ctrump_texpr *t;
00415 struct ctrump_array_type_chain *chain;
00416 };
00417
00421 struct ctrump_qualified_type_chain {
00422 struct ctrump_texpr *type;
00423 struct ctrump_qualified_type_chain *chain;
00424 struct ctrump_gccext_attribute attr;
00425 int qual_flags;
00426 };
00427
00431 struct ctrump_func_type_node {
00432 struct ctrump_texpr *arg_type;
00433 struct ctrump_texpr *func_type;
00434 struct ctrump_func_type_node *brother;
00435 struct ctrump_func_type_node *child;
00436 };
00437
00441 struct ctrump_texpr {
00442 int id;
00443 enum ctrump_texpr_code code;
00445 struct ctrump_texpr *pointer_type;
00446 struct ctrump_texpr *incomplete_array_type;
00447 struct ctrump_array_type_chain *array_types;
00448 struct ctrump_func_type_node *func_types;
00449 struct ctrump_qualified_type_chain *qualified_types;
00451 union {
00452 struct ctrump_builtin_type builtin;
00453 struct ctrump_texpr *pointer_to;
00454 struct ctrump_texpr *incomplete_array_of;
00455 struct ctrump_texpr *varlen_array_of;
00456 struct ctrump_array_type array;
00457 struct ctrump_record_type struct_, union_;
00458 struct ctrump_function_type func;
00459 struct ctrump_typedef_name typedef_name;
00460 struct ctrump_qualified_type qualified;
00461 struct ctrump_enum_type enum_;
00462 }u;
00463 };
00464
00465 #define DEFINE_BUILTIN_TYPE(str_name,code,sym) \
00466 CTRUMP_EXTDEF extern struct ctrump_texpr ctrump_builtin_##str_name;
00467 #include "ctrump/ast/builtin-types.def"
00468 #undef DEFINE_BUILTIN_TYPE
00469
00473 struct ctrump_type_env {
00474 int id;
00475 struct ctrump_mempool type_allocator;
00476 struct ctrump_texpr *builtin_char_type;
00477 };
00478
00479
00486 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_typedef_name(struct ctrump_type_env *env,
00487 struct ctrump_texpr *orig_type,
00488 const struct ctrump_symbol *name);
00489
00497 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_qualified_attr_type(struct ctrump_type_env *env,
00498 struct ctrump_texpr *texpr,
00499 int qual_flags,
00500 const struct ctrump_gccext_attribute *attr);
00501
00508 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_qualified_type(struct ctrump_type_env *env,
00509 struct ctrump_texpr *texpr,
00510 int qual_flags);
00511
00516 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_unqualified_type(struct ctrump_texpr *texpr);
00517
00523 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_pointer_type(struct ctrump_type_env *env,
00524 struct ctrump_texpr *texpr);
00525
00532 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_array_type(struct ctrump_type_env *env,
00533 struct ctrump_texpr *texpr,
00534 int size);
00535
00541 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_incomplete_array_type(struct ctrump_type_env *env,
00542 struct ctrump_texpr *texpr);
00543
00544
00549 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_varlen_array_type(struct ctrump_type_env *env,
00550 struct ctrump_texpr *texpr);
00551
00557 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_incomplete_struct_type(struct ctrump_type_env *env,
00558 enum ctrump_texpr_code code,
00559 const struct ctrump_symbol *name);
00560
00566 CTRUMP_EXTDEF void ctrump_complete_struct_type(struct ctrump_record_type *record_type,
00567 int nfield,
00568 struct ctrump_record_type_field *fields,
00569 struct ctrump_gccext_attribute *attr);
00570
00571 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_get_incomplete_enum_type(struct ctrump_type_env *env,
00572 const struct ctrump_symbol *name
00573 );
00574
00575 CTRUMP_EXTDEF void ctrump_complete_enum_type(struct ctrump_enum_type *enum_type,
00576 int nfield,
00577 struct ctrump_enum_type_field *fields);
00578
00582 struct ctrump_get_functype_iterator
00583 {
00584 struct ctrump_func_type_node *cur;
00585 int cur_arg;
00586 struct ctrump_texpr *ret;
00587 struct ctrump_texpr **args;
00588 };
00589
00590
00621 CTRUMP_EXTDEF void ctrump_get_functype_begin(struct ctrump_get_functype_iterator *iter,
00622 struct ctrump_type_env *env,
00623 struct ctrump_texpr *ret_type,
00624 int num_args);
00625
00632 CTRUMP_EXTDEF void ctrump_get_functype_next(struct ctrump_type_env *env,
00633 struct ctrump_get_functype_iterator *iter,
00634 struct ctrump_texpr *arg);
00635
00641 CTRUMP_EXTDEF void ctrump_get_functype_cancel(struct ctrump_get_functype_iterator *iter);
00642
00649 CTRUMP_EXTDEF struct ctrump_texpr * ctrump_get_functype_end(struct ctrump_type_env *env,
00650 struct ctrump_get_functype_iterator *iter,
00651 int has_unspec_arg);
00652
00658 CTRUMP_EXTDEF void ctrump_type_env_init(struct ctrump_type_env *env,
00659 int is_char_signed);
00660
00665 CTRUMP_EXTDEF void ctrump_type_env_destroy(struct ctrump_type_env *env);
00666
00670 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_alloc_texpr(struct ctrump_type_env *env, enum ctrump_texpr_code code);
00671
00672
00676 CTRUMP_EXTDEF int ctrump_texpr_is_pointer(struct ctrump_texpr *type);
00677
00683 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_texpr_apply_unary_pointer_conversion(struct ctrump_type_env *env,
00684 struct ctrump_texpr *type);
00685
00691 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_retrieve_pointer_type(struct ctrump_texpr *type);
00692
00698 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_retrieve_address_type(struct ctrump_texpr *type);
00699
00700
00704 CTRUMP_EXTDEF struct ctrump_texpr *ctrump_retrieve_typedef_name(struct ctrump_texpr *texpr);
00705
00706
00710 CTRUMP_EXTDEF struct ctrump_gccext_attribute
00711 ctrump_merge_attr(const struct ctrump_gccext_attribute *decl_attr,
00712 const struct ctrump_gccext_attribute *var_attr);
00713
00717 CTRUMP_EXTDEF int ctrump_is_scalar_type(const struct ctrump_texpr *texpr);
00718
00719 #ifdef __cplusplus
00720 }
00721 #endif
00722
00723 #endif