return ATOMIC_TYPE_INT;
assert(type->type == TYPE_ATOMIC);
- atomic_type_t *atomic_type = (atomic_type_t*) type;
- atomic_type_type_t atype = atomic_type->atype;
+ const atomic_type_t *atomic_type = &type->atomic;
+ atomic_type_type_t atype = atomic_type->atype;
return atype;
}
break;
case TYPE_ARRAY: {
- array_type_t *array_type = (array_type_t*) source_type;
- pointer_type_t *pointer_type
- = (pointer_type_t*) dest_type;
+ array_type_t *array_type = &source_type->array;
+ pointer_type_t *pointer_type = &dest_type->pointer;
if (types_compatible(array_type->element_type,
pointer_type->points_to)) {
return create_cast_expression(expression, dest_type);
/* the left type has all qualifiers from the right type */
unsigned missing_qualifiers
- = points_to_right->qualifiers & ~points_to_left->qualifiers;
+ = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
if(missing_qualifiers != 0) {
parser_print_error_prefix();
fprintf(stderr, "destination type ");
/* ยง 6.7.8.14/15 char array may be initialized by string literals */
if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
- array_type_t *array_type = (array_type_t*) type;
+ array_type_t *array_type = &type->array;
type_t *element_type = array_type->element_type;
if(element_type->type == TYPE_ATOMIC) {
- atomic_type_t *atomic_type = (atomic_type_t*) element_type;
+ atomic_type_t *atomic_type = &element_type->atomic;
atomic_type_type_t atype = atomic_type->atype;
/* TODO handle wide strings */
}
}
- type->qualifiers = (type_qualifier_t)type_qualifiers;
+ type->base.qualifiers = (type_qualifier_t)type_qualifiers;
type_t *result = typehash_insert(type);
if(newtype && result != (type_t*) type) {
fprintf(stderr, "')\n");
return;
}
- if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
+ if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
parser_print_error_prefix();
fprintf(stderr, "assignment to readonly location '");
print_expression(left);
type_visited++;
}
-void print_type_qualifiers(unsigned qualifiers)
+void print_type_qualifiers(type_qualifiers_t qualifiers)
{
if(qualifiers & TYPE_QUALIFIER_CONST) fputs("const ", out);
if(qualifiers & TYPE_QUALIFIER_VOLATILE) fputs("volatile ", out);
fputs("invalid", out);
return;
case TYPE_ENUM:
- print_type_enum((enum_type_t*) type);
+ print_type_enum(&type->enumt);
return;
case TYPE_ATOMIC:
- print_atomic_type((atomic_type_t*) type);
+ print_atomic_type(&type->atomic);
return;
case TYPE_COMPOUND_STRUCT:
case TYPE_COMPOUND_UNION:
- print_compound_type((compound_type_t*) type);
+ print_compound_type(&type->compound);
return;
case TYPE_BUILTIN:
- fputs(((builtin_type_t*) type)->symbol->string, out);
+ fputs(type->builtin.symbol->string, out);
return;
case TYPE_FUNCTION:
- print_function_type_pre((function_type_t*) type);
+ print_function_type_pre(&type->function);
return;
case TYPE_POINTER:
- print_pointer_type_pre((pointer_type_t*) type);
+ print_pointer_type_pre(&type->pointer);
return;
case TYPE_ARRAY:
- print_array_type_pre((array_type_t*) type);
+ print_array_type_pre(&type->array);
return;
case TYPE_TYPEDEF:
- print_typedef_type_pre((typedef_type_t*) type);
+ print_typedef_type_pre(&type->typedeft);
return;
case TYPE_TYPEOF:
- print_typeof_type_pre((typeof_type_t*) type);
+ print_typeof_type_pre(&type->typeoft);
return;
}
fputs("unknown", out);
{
switch(type->type) {
case TYPE_FUNCTION:
- print_function_type_post((const function_type_t*) type, NULL);
+ print_function_type_post(&type->function, NULL);
return;
case TYPE_POINTER:
- print_pointer_type_post((const pointer_type_t*) type);
+ print_pointer_type_post(&type->pointer);
return;
case TYPE_ARRAY:
- print_array_type_post((const array_type_t*) type);
+ print_array_type_post(&type->array);
return;
case TYPE_INVALID:
case TYPE_ATOMIC:
if(type->type != TYPE_ATOMIC)
return false;
- atomic_type_t *atomic_type = (atomic_type_t*) type;
- switch(atomic_type->atype) {
+ switch(type->atomic.atype) {
case ATOMIC_TYPE_BOOL:
case ATOMIC_TYPE_CHAR:
case ATOMIC_TYPE_SCHAR:
if(type->type != TYPE_ATOMIC)
return false;
- atomic_type_t *atomic_type = (atomic_type_t*) type;
- switch(atomic_type->atype) {
+ switch(type->atomic.atype) {
case ATOMIC_TYPE_FLOAT:
case ATOMIC_TYPE_DOUBLE:
case ATOMIC_TYPE_LONG_DOUBLE:
if(type->type != TYPE_ATOMIC)
return false;
- atomic_type_t *atomic_type = (atomic_type_t*) type;
- switch(atomic_type->atype) {
+ switch(type->atomic.atype) {
case ATOMIC_TYPE_CHAR:
case ATOMIC_TYPE_SCHAR:
case ATOMIC_TYPE_SHORT:
switch(type->type) {
case TYPE_COMPOUND_STRUCT:
case TYPE_COMPOUND_UNION: {
- const compound_type_t *compound_type
- = (const compound_type_t*) type;
- declaration_t *declaration = compound_type->declaration;
+ const compound_type_t *compound_type = &type->compound;
+ declaration_t *declaration = compound_type->declaration;
return !declaration->init.is_defined;
}
case TYPE_FUNCTION:
return true;
- case TYPE_ARRAY: {
- const array_type_t *array_type = (const array_type_t*) type;
-
- return array_type->size == NULL;
- }
+ case TYPE_ARRAY:
+ return type->array.size == NULL;
case TYPE_ATOMIC:
case TYPE_POINTER:
return true;
if(type1->type == TYPE_ATOMIC && type2->type == TYPE_ATOMIC) {
- const atomic_type_t *atomic1 = (const atomic_type_t*) type1;
- const atomic_type_t *atomic2 = (const atomic_type_t*) type2;
-
- return atomic1->atype == atomic2->atype;
+ return type1->atomic.atype == type2->atomic.atype;
}
return false;
assert(type1->type == TYPE_POINTER);
assert(type2->type == TYPE_POINTER);
-#if 0
- pointer_type_t *pointer_type1 = (pointer_type_t*) type1;
- pointer_type_t *pointer_type2 = (pointer_type_t*) type2;
- return types_compatible(pointer_type1->points_to,
- pointer_type2->points_to);
-#endif
+ /* TODO */
return true;
}
type_t *skip_typeref(type_t *type)
{
- unsigned qualifiers = type->qualifiers;
+ unsigned qualifiers = type->base.qualifiers;
while(1) {
switch(type->type) {
case TYPE_TYPEDEF: {
- qualifiers |= type->qualifiers;
- const typedef_type_t *typedef_type = (const typedef_type_t*) type;
+ qualifiers |= type->base.qualifiers;
+ const typedef_type_t *typedef_type = &type->typedeft;
if(typedef_type->resolved_type != NULL) {
type = typedef_type->resolved_type;
break;
continue;
}
case TYPE_TYPEOF: {
- const typeof_type_t *typeof_type = (const typeof_type_t *) type;
+ const typeof_type_t *typeof_type = &type->typeoft;
if(typeof_type->typeof_type != NULL) {
type = typeof_type->typeof_type;
} else {
continue;
}
case TYPE_BUILTIN: {
- const builtin_type_t *builtin_type = (const builtin_type_t*) type;
+ const builtin_type_t *builtin_type = &type->builtin;
type = builtin_type->real_type;
continue;
}
return result;
}
-type_t *make_atomic_type(atomic_type_type_t type, type_qualifiers_t qualifiers)
+type_t *make_atomic_type(atomic_type_type_t atype, type_qualifiers_t qualifiers)
{
- atomic_type_t *atomic_type
- = obstack_alloc(type_obst, sizeof(atomic_type[0]));
- memset(atomic_type, 0, sizeof(atomic_type[0]));
- atomic_type->type.type = TYPE_ATOMIC;
- atomic_type->type.qualifiers = qualifiers;
- atomic_type->atype = type;
+ type_t *type = obstack_alloc(type_obst, sizeof(atomic_type_t));
+ memset(type, 0, sizeof(atomic_type_t));
+
+ type->type = TYPE_ATOMIC;
+ type->base.qualifiers = qualifiers;
+ type->atomic.atype = atype;
- return identify_new_type((type_t*) atomic_type);
+ return identify_new_type(type);
}
type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers)
{
- pointer_type_t *pointer_type
- = obstack_alloc(type_obst, sizeof(pointer_type[0]));
- memset(pointer_type, 0, sizeof(pointer_type[0]));
- pointer_type->type.type = TYPE_POINTER;
- pointer_type->type.qualifiers = qualifiers;
- pointer_type->points_to = points_to;
+ type_t *type = obstack_alloc(type_obst, sizeof(pointer_type_t));
+ memset(type, 0, sizeof(pointer_type_t));
+
+ type->type = TYPE_POINTER;
+ type->base.qualifiers = qualifiers;
+ type->pointer.points_to = points_to;
- return identify_new_type((type_t*) pointer_type);
+ return identify_new_type(type);
}
static __attribute__((unused))
typedef unsigned int type_qualifiers_t;
-struct type_t {
+struct type_base_t {
type_type_t type;
type_qualifiers_t qualifiers;
};
struct atomic_type_t {
- type_t type;
+ type_base_t type;
atomic_type_type_t atype;
};
struct builtin_type_t {
- type_t type;
- symbol_t *symbol;
- type_t *real_type;
+ type_base_t type;
+ symbol_t *symbol;
+ type_t *real_type;
};
struct pointer_type_t {
- type_t type;
- type_t *points_to;
+ type_base_t type;
+ type_t *points_to;
};
struct array_type_t {
- type_t type;
+ type_base_t type;
type_t *element_type;
expression_t *size;
bool is_static;
};
struct function_type_t {
- type_t type;
+ type_base_t type;
type_t *result_type;
function_parameter_t *parameters;
bool variadic;
};
struct compound_type_t {
- type_t type;
+ type_base_t type;
/** the declaration of the compound type, its context field
- * contains the compound entries. */
+ * contains the compound entries. */
declaration_t *declaration;
};
struct enum_type_t {
- type_t type;
+ type_base_t type;
/** the declaration of the enum type. You can find the enum entries by
- * walking the declaration->next list until you don't find
- * STORAGE_CLASS_ENUM_ENTRY declarations anymore */
+ * walking the declaration->next list until you don't find
+ * STORAGE_CLASS_ENUM_ENTRY declarations anymore */
declaration_t *declaration;
};
struct typedef_type_t {
- type_t type;
+ type_base_t type;
declaration_t *declaration;
type_t *resolved_type;
};
struct typeof_type_t {
- type_t type;
+ type_base_t type;
expression_t *expression;
type_t *typeof_type;
type_t *resolved_type;
};
+union type_t {
+ type_type_t type;
+ type_base_t base;
+ atomic_type_t atomic;
+ builtin_type_t builtin;
+ pointer_type_t pointer;
+ array_type_t array;
+ function_type_t function;
+ compound_type_t compound;
+ enum_type_t enumt;
+ typedef_type_t typedeft;
+ typeof_type_t typeoft;
+};
+
type_t *make_atomic_type(atomic_type_type_t type, type_qualifiers_t qualifiers);
type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
if(type->type != TYPE_ATOMIC)
return false;
- const atomic_type_t *atomic_type = (const atomic_type_t*) type;
+ const atomic_type_t *atomic_type = &type->atomic;
return atomic_type->atype == atype;
}