F diff --git a/src/backend/print/print.c b/src/backend/print/print.c
--- a/src/backend/print/print.c
+++ b/src/backend/print/print.c
fprintf(out,") returning ");
print_type(out,((struct Type_Function*)type)->return_type,should_print_struct_union);
return;
- case TS_BITFIELD:
- fprintf(out,"%zu bits of ",((struct Type_Bit_Field*)type)->number_of_bits);
- print_type(out,((struct Type_Bit_Field*)type)->base,should_print_struct_union);
- return;
case TS_NONE:
fprintf(out,"NONE");return;
case TS_ERROR:
}
fprintf(out,"denoted object ");
print_token(out,((struct Denoted_Object*)denoted)->id);
- switch(((struct Denoted_Object*)denoted)->object->storage_class)
- {
- case SCS_NONE:
- fprintf(out," with automatic storage duration");
- break;
- case SCS_STATIC:
- fprintf(out," static storage duration");
- break;
- assert(0);
- }
+ print_object(out,((struct Denoted_Object*)denoted)->object);
fprintf(out," is a ");
print_type(out,((struct Denoted_Object*)denoted)->object->type,1);
fprintf(out,"}");
}
+ void print_object(FILE *out,struct Object *object)
+ {
+ if(object->kind==OBJECT_KIND_NORMAL)
+ {
+ print_normal_object(out,object);
+ }else
+ {
+ print_bitfield_object(out,(struct Object_Bitfield*)object);
+ }
+ }
+ void print_normal_object(FILE *out,struct Object *object)
+ {
+ fprintf(out," normal object that ");
+ }
+ void print_bitfield_object(FILE *out,struct Object_Bitfield *object)
+ {
+ fprintf(out," bitfield object with %zu bits ",object->number_of_bits);
+ }
void print_translation_unit_tree(FILE *out,struct AST_Translation_Unit *unit)
{
struct Queue_Node *it;
break;
case TS_ARRAY:
case TS_FUNC:
- case TS_BITFIELD:
break;
case TS_NONE:
case TS_ERROR:
F diff --git a/src/backend/print/print.h b/src/backend/print/print.h
--- a/src/backend/print/print.h
+++ b/src/backend/print/print.h
void print_list_of_denoted(FILE *out,struct Queue *denoted);
void print_enumeration(FILE *out,struct Enum *enumeration);
void print_struct_union(FILE *out,struct Struct_Union *struct_union);
+ void print_object(FILE *out,struct Object *object);
+ void print_normal_object(FILE *out,struct Object *object);
+ void print_bitfield_object(FILE *out,struct Object_Bitfield *object);
void print_translation_unit_tree(FILE *out,struct AST_Translation_Unit *unit);
void print_ast(FILE *out,struct AST* tree);
void print_program_tokens(FILE *out,struct Program *program);
F diff --git a/src/frontend/parse/parse_declaration.c b/src/frontend/parse/parse_declaration.c
--- a/src/frontend/parse/parse_declaration.c
+++ b/src/frontend/parse/parse_declaration.c
*/
struct Denoted* parse_struct_declarator(struct Translation_Data *translation_data,struct Scope *scope,struct Denotation_Prototype *prototype)
{
- struct Denoted *hold;
+ struct Denoted_Object *hold_denoted_object;
+ struct AST_Expression *hold_number_of_bits_expression;
+
if(get_and_check(translation_data,KW_COLUMN))
{
/*unnamed bitfields are possible*/
- hold=get_denoted_object(NULL,SCS_NONE,prototype->type,NULL);
+ hold_denoted_object=(struct Denoted_Object*)get_denoted_object(NULL,SCS_NONE,prototype->type,NULL);
}else
{
- hold=parse_declarator(translation_data,scope,prototype);
+ /*we cast it but it is still unsure if it has object type so we imediately check*/
+ hold_denoted_object=(struct Denoted_Object*)parse_declarator(translation_data,scope,prototype);
+ if(hold_denoted_object->denotation!=DT_Object)
+ {
+ /*TODO error*/
+ push_translation_error("expected object type for bitfield",translation_data);
+ return get_denoted_error((struct Denoted*)hold_denoted_object);
+ }
if(get_and_check(translation_data,KW_COLUMN))
{
- /*TODO move error detection in get_type_bitfield*/
- ((struct Denoted_Object*)hold)->object->type=(struct Type*)get_type_bitfield(prototype->type,parse_expression(translation_data,scope),translation_data);
+ hold_number_of_bits_expression=(struct AST_Expression*)parse_expression(translation_data,scope);
+
+ hold_denoted_object->object=convert_object_to_bitfield(hold_denoted_object->object,hold_number_of_bits_expression,translation_data);
}
}
- return hold;
+ return (struct Denoted*)hold_denoted_object;
}
/*
enum-specifier-finish
F diff --git a/src/semantics/identifiers/linkage.c b/src/semantics/identifiers/linkage.c
--- a/src/semantics/identifiers/linkage.c
+++ b/src/semantics/identifiers/linkage.c
if(hold_object->linkage!=LINKAGE_NONE)
{
denoted_object->linkage=hold_object->linkage;
- denoted_object->object->storage_class=SCS_STATIC;
+ //denoted_object->object->storage_class=SCS_STATIC; //TODO remove these, storage class should be handled at object constructor
}else
{
denoted_object->linkage=LINKAGE_EXTERNAL;
- denoted_object->object->storage_class=SCS_STATIC;
+ //denoted_object->object->storage_class=SCS_STATIC;
}
}else
{
denoted_object->linkage=LINKAGE_EXTERNAL;
- denoted_object->object->storage_class=SCS_STATIC;
+ //denoted_object->object->storage_class=SCS_STATIC;
}
}
void account_for_upper_linkage_on_function(struct Scope *scope,struct Translation_Data *translation_data,struct Denoted_Function *denoted_function)
F diff --git a/src/semantics/memory/object.c b/src/semantics/memory/object.c
--- a/src/semantics/memory/object.c
+++ b/src/semantics/memory/object.c
{
struct Object *ret;
ret=malloc(sizeof(struct Object));
+ ret->kind=OBJECT_KIND_NORMAL;
ret->type=type;
ret->storage_class=storage_class;
-
-
ret->location=NULL;
+ return ret;
}
void delete_object(struct Object *object)
struct Object* retype_object(struct Object *object,struct Type *new_type)
{
- struct Object *ret;
- ret=malloc(sizeof(struct Object));
- ret->location=object->location;
- ret->storage_class=object->storage_class;
- ret->type=new_type;
+ if(object->kind==OBJECT_KIND_NORMAL)
+ {
+ struct Object *ret;
+ ret=malloc(sizeof(struct Object));
+ ret->location=object->location;
+ ret->storage_class=object->storage_class;
+ ret->type=new_type;
- return ret;
+ return ret;
+ }else if(object->kind==OBJECT_KIND_BITFIELD)
+ {
+ struct Object_Bitfield *ret;
+
+ ret=malloc(sizeof(struct Object_Bitfield));
+ ret->location=object->location;
+ ret->storage_class=object->storage_class;
+ ret->number_of_bits=((struct Object_Bitfield*)object)->number_of_bits;
+ ret->type=new_type;
+
+ return (struct Object*)ret;
+
+ }
+
+
+ assert(0);
}
ret->storage_class=SCS_NONE;
ret->type=type;
- return ret;
+ return (struct Object*)ret;
}
+ struct Object* convert_object_to_bitfield(struct Object *object,struct AST_Expression* number_of_bits_expression,struct Translation_Data *translation_data)
+ {
+ struct Object_Bitfield *ret;
+ long long int number_of_bits;
+ ret=malloc(sizeof(struct Object_Bitfield));
+ *(struct Object*)ret=*object;
+ ret->kind=OBJECT_KIND_BITFIELD;
+
+ number_of_bits=evaluate_const_expression_integer((struct AST*)number_of_bits_expression,translation_data);
+ ret->number_of_bits=number_of_bits;
+ if(number_of_bits<0)
+ push_translation_error("negative width bitfield is not allowed",translation_data);
+ return (struct Object*)ret;
+
+ }
#endif
F diff --git a/src/semantics/memory/object.h b/src/semantics/memory/object.h
--- a/src/semantics/memory/object.h
+++ b/src/semantics/memory/object.h
struct Object
{
+ enum Object_Kind kind;
struct Type *type;
struct Location *location;
enum Storage_Class_Specifier storage_class;
};
+ struct Object_Bitfield
+ {
+ enum Object_Kind kind;
+ struct Type *type;
+ struct Location *location;
+ enum Storage_Class_Specifier storage_class;
+ size_t number_of_bits;
+ };
void delete_object(struct Object *object);
struct Object* get_object(struct Type *type,enum Storage_Class_Specifier storage_class);
struct Object* retype_object(struct Object *object,struct Type *new_type);
struct Object* get_temp_object(struct Type *type);
+ struct Object* convert_object_to_bitfield(struct Object *object,struct AST_Expression* number_of_bits_expression,struct Translation_Data *translation_data);
#endif
F diff --git a/src/semantics/memory/object.hh b/src/semantics/memory/object.hh
--- a/src/semantics/memory/object.hh
+++ b/src/semantics/memory/object.hh
#ifndef WONKY_OBJECT_HH
#define WONKY_OBJECT_HH WONKY_OBJECT_HH
+ enum Object_Kind
+ {
+ OBJECT_KIND_NORMAL,
+ OBJECT_KIND_BITFIELD,
+ };
enum Storage_Class_Specifier
{
SCS_EXTERN,
SCS_NONE
};
-
struct Object;
+ struct Object;
+ struct Object_Bitfield;
#endif
F diff --git a/src/semantics/value/constraints.c b/src/semantics/value/constraints.c
--- a/src/semantics/value/constraints.c
+++ b/src/semantics/value/constraints.c
return 1;
}else if(operand->value->type==VALUE_LVALUE)
{
- if(operand_type->specifier==TS_BITFIELD || ((struct Expression_Value_LValue*)operand->value)->object->storage_class==SCS_REGISTER)
+ struct Object *hold_operand_object;
+ hold_operand_object=((struct Expression_Value_LValue*)operand->value)->object;
+ if(hold_operand_object->kind==OBJECT_KIND_BITFIELD || hold_operand_object->storage_class==SCS_REGISTER)
{
- push_translation_error("can't get the addres of such an operand",translation_data);
+ push_translation_error("can't get the address of such an operand",translation_data);
return 0;
}else
{
F diff --git a/src/semantics/value/type.c b/src/semantics/value/type.c
--- a/src/semantics/value/type.c
+++ b/src/semantics/value/type.c
}
return (struct Type*)ret;
}
- struct Type* get_type_bitfield(struct Type *base,struct AST* number_of_bits,struct Translation_Data *translation_data)
- {
- size_t hold_number_of_bits=0;
-
- assert(number_of_bits!=NULL);
- hold_number_of_bits=evaluate_const_expression_integer(number_of_bits,translation_data);
- delete_ast(number_of_bits);
-
- return get_type_bitfield_inner(base,hold_number_of_bits,translation_data);
-
- }
- struct Type* get_type_bitfield_inner(struct Type *base,size_t number_of_bits,struct Translation_Data *translation_data)
- {
-
- struct Type_Bit_Field *ret;
- ret=calloc(1,sizeof(struct Type_Bit_Field));
- ret->specifier=TS_BITFIELD;
- ret->number_of_bits=number_of_bits;
-
- ret->base=base;
-
- ret=(struct Type_Bit_Field*)type_check_and_push((struct Type*)ret,base->node,sizeof(struct Type_Bit_Field));
- return (struct Type*)ret;
- }
struct Type* get_function_type(struct Type *return_type,struct Queue *parameters,struct Normal_Scope* function_prototype_scope)
{
return ((struct Type_Array*)type)->size;
case TS_FUNC:
return 0;
- case TS_BITFIELD:
- return ((struct Type_Bit_Field*)type)->number_of_bits;
case TS_NONE:
return 0;
case TS_ERROR:
return 0;
else
return types_of_arrays_are_compatible_unqualified((struct Type_Array*)a,(struct Type_Array*)b);
- case TS_BITFIELD:
- if(b->specifier!=TS_BITFIELD)
- return 0;
- else
- return types_of_bitfields_are_compatible_unqalified((struct Type_Bit_Field*)a,(struct Type_Bit_Field*)b);
case TS_FUNC:
if(b->specifier!=TS_FUNC)
return 0;
else
return type;
- }else if(type->specifier==TS_BITFIELD)
- {
- struct Type_Bit_Field *cache_type;
- cache_type=(struct Type_Bit_Field*)type;
- if(cache_type->number_of_bits< 8 * INT_SIZE)
- return (struct Type*)get_type_insecure(TS_INT,TSIGN_NONE,TC_NONE,INT_SIZE,translation_data);
- else if(cache_type->number_of_bits== 8 * INT_SIZE)
- return (struct Type*)get_type_insecure(TS_INT,TSIGN_UNSIGNED,TC_NONE,INT_SIZE,translation_data);
- else
- return type;
}else
{
return type;
case TS_FUNC:
hold_unqualified_type=type;
break;
- case TS_BITFIELD:
- hold_unqualified_type=get_type_bitfield_inner(((struct Type_Bit_Field*)type)->base,((struct Type_Bit_Field*)type)->number_of_bits,translation_data);
- break;
default:
return NULL;
return ((struct Type_Enum*)type)->is_const;
case TS_POINTER:
return ((struct Type_Pointer*)type)->is_const;
- case TS_BITFIELD:
- return type_is_constant(((struct Type_Bit_Field*)type)->base);
}
return 0;
}
return ((struct Type_Enum*)type)->is_const;
case TS_POINTER:
return ((struct Type_Pointer*)type)->is_const;
- case TS_BITFIELD:
- return type_is_volatile(((struct Type_Bit_Field*)type)->base);
}
return 0;
}
F diff --git a/src/semantics/value/type.h b/src/semantics/value/type.h
--- a/src/semantics/value/type.h
+++ b/src/semantics/value/type.h
_Bool is_finished:1;
_Bool has_constant_member:1;
};
- struct Type_Bit_Field
- {
- enum Type_Specifier specifier;
- struct Map *node;
- size_t number_of_bits;
- struct Type *base;
- };
struct Type_Basic
{
enum Type_Specifier specifier;
struct Type* get_pointer_type(struct Type *points_to,char is_const,char is_volatile);
struct Type* get_array_type(struct Type *array_of,struct AST* number_of_elements,struct Translation_Data *translation_data);
struct Type* get_enum_type(struct Denotation_Prototype *prototype);
- struct Type* get_type_bitfield(struct Type *base,struct AST* number_of_bits,struct Translation_Data *translation_data);
- struct Type* get_type_bitfield_inner(struct Type *base,size_t number_of_bits,struct Translation_Data *translation_data);
struct Type* get_function_type(struct Type *return_type,struct Queue *parameters,struct Normal_Scope* function_prototype_scope);
F diff --git a/src/semantics/value/type.hh b/src/semantics/value/type.hh
--- a/src/semantics/value/type.hh
+++ b/src/semantics/value/type.hh
TS_POINTER,
TS_ARRAY,
TS_FUNC,
- TS_BITFIELD,
TS_NONE,
TS_ERROR
};