F diff --git a/misc/map.c b/misc/map.c --- a/misc/map.c +++ b/misc/map.c}- /*this isn't c++ i promise :)*//*requres that cpy has no loops*/Map* Map_Copy(Map *cpy){return ret;}+ struct Map* Map_Check_And_Get(Map *tree, void *str,size_t size)+ {+ size_t temp;+ Map_Scour(tree,str,size,&temp,&tree);++ if(temp<size)+ {+ return NULL;+ }else+ {+ return tree;+ }+ }++ struct Map* Map_Push_And_Get(struct Map* tree,void *str,size_t size,void *id)+ {+ size_t temp;+ Map_Scour(tree,str,size,&temp,&tree);++ if(temp == size)+ {+ if(tree->ID!=NULL)tree->ID=id;+ tree->is_final=1;+ return tree;+ }+ for(temp;temp<size;++temp)+ {+ Map_Init(tree=tree->delta[((unsigned char*)str)[temp]]=malloc(sizeof(Map)));+ }++ tree->ID=id;+ tree->is_final=1;+ return tree;+ }#endif //#ifndef GMAPF diff --git a/misc/map.h b/misc/map.h --- a/misc/map.h +++ b/misc/map.hchar is_final;Map *delta[256];/*ID cannot point to itself ( this is used in the deletion of the map ) */- void *ID; //I have integer ids in mind but can be a translation for exmpl.+ void *ID;};void Map_Init(Map *tree);void Map_Map(Map *tree,void (*map)(void*));void Map_Destroy(Map *tree);struct Condensed_Map* Map_Condense(Map* tree);++ struct Map* Map_Push_And_Get(struct Map* tree,void *str,size_t size,void *id);+ struct Map* Map_Check_And_Get(Map *tree, void *str,size_t size);+#endifF diff --git a/misc/print.c b/misc/print.c --- a/misc/print.c +++ b/misc/print.creturn;case TS_FUNC:fprintf(out,"function taking arguments (");- print_list_of_denoted(out,((struct Type_Function*)type)->parameters);+ print_function_args(out,(struct Type_Function*)type);fprintf(out,") returning ");print_type(out,((struct Type_Function*)type)->return_type,should_print_struct_union);return;fprintf(out,"typedef ");print_token(out,((struct Denoted_Typedef*)denoted)->id);fprintf(out," to ");- print_type(out,((struct Denoted_Typedef*)denoted)->type,0);+ print_type(out,((struct Denoted_Typedef*)denoted)->node->ID,0);return;case DT_Function:print_token(out,((struct Denoted_Function*)denoted)->id);print_translation_error(out,(struct Translation_Error*)it->data);}}+ void print_function_args(FILE *out,struct Type_Function *func)+ {+ size_t i;+ if(func->number_of_arguments==0)+ return;++ print_type(out,func->arguments[0],0);+ for(i=1;i<func->number_of_arguments;++i)+ {+ fprintf(out,", ");+ print_type(out,func->arguments[i],0);+ }+ }#undef TOK#undef INDENTF diff --git a/misc/print.h b/misc/print.h --- a/misc/print.h +++ b/misc/print.hvoid print_keyword_enum(FILE *out,enum KEYWORDS kw);void print_function_definition(FILE *out,struct Denoted_Function *function);void print_errors(FILE *out,struct Queue *errors);+ void print_function_args(FILE *out,struct Type_Function *func);#endifF diff --git a/parse/parse_declaration.c b/parse/parse_declaration.c --- a/parse/parse_declaration.c +++ b/parse/parse_declaration.cQueue_Push(where_to_push,get_function_declaration_tree(scope,(struct Denoted_Function*)hold));}else if(hold->denotation==DT_Typedef){- Queue_Push(where_to_push,get_type_definition_tree((struct Denoted_Typedef*)hold,scope));+ Queue_Push(where_to_push,get_type_definition_tree((struct Denoted_Typedef*)hold));}else if(hold->denotation==DT_Object){- Queue_Push(where_to_push,get_object_declaration_tree((struct Denoted_Object*)hold,NULL,scope));+ Queue_Push(where_to_push,get_object_declaration_tree((struct Denoted_Object*)hold,NULL));}else{/*TODO error*/{enum KEYWORDS hold_kw;struct Denotation_Prototype *ret;- ret=(struct Denotation_Prototype*)get_denotation_prototype();+ ret=(struct Denotation_Prototype*)get_denotation_prototype(translation_data->types);while(1){hold=check_ordinary(scope,(struct token*)translation_data->tokens->first->data);if(hold!=NULL && hold->denotation==DT_Typedef){- ret->type=((struct Denoted_Typedef*)hold)->type;+ ret->pair->node=((struct Denoted_Typedef*)hold)->node;+ ret->pair->type=(struct Type*)ret->pair->node->ID;chomp(translation_data);- break;+ return ret;}/*falltrough - this has not been typedefed*/}exit:if(ret->specifier==TS_ENUM){- ret->type=get_enum_type(ret);+ get_enum_type(ret);}else if(ret->specifier==TS_STRUCT || ret->specifier==TS_UNION){- ret->type=get_struct_union_type(ret);- }else if(ret->type==NULL)+ get_struct_union_type(ret);+ }else if(ret->pair->type==NULL){- ret->type=get_basic_type(ret);+ get_basic_type(ret);}return ret;}*/struct Denoted* parse_declarator(struct Translation_Data *translation_data,struct Scope *scope,struct Denotation_Prototype *prototype){- struct Denoted_Base temp;- temp.id=NULL;- temp.denotation=DT_Prototype;- temp.type=prototype->type;- parse_declarator_inner(translation_data,scope,&temp);- return extract_denoted(&temp,prototype,0);+ struct Denoted_Base *temp;+ temp=get_denoted_base(prototype);+ parse_declarator_inner(translation_data,scope,temp);+ return extract_denoted(temp,prototype,0);}enum KEYWORDS hold;while(get_and_check(translation_data,KW_STAR)){- base->type=get_pointer_type(base->type);- hold=kw_get(translation_data);+ char is_constant=0,is_volatile=0;while(1){+ hold=kw_get(translation_data);if(hold==KW_CONST){- ((struct Type_Pointer*)(base->type))->is_const=1;+ is_constant=1;}else if(hold==KW_VOLATILE){- ((struct Type_Pointer*)(base->type))->is_volatile=1;+ is_volatile=1;}else{break;}}+ get_pointer_type(base->pair,is_volatile,is_constant);}parse_direct_declarator(translation_data,scope,base);{if(get_and_check(translation_data,KW_CLOSE_SQUARE)){- base->type=get_array_type(base->type,NULL);+ get_array_type(base->pair,NULL);}else{- base->type=get_array_type(base->type,parse_expression(translation_data,scope));+ get_array_type(base->pair,parse_expression(translation_data,scope));if(!get_and_check(translation_data,KW_CLOSE_SQUARE)){/*TODO error*/push_translation_error("']' expected",translation_data);- base->type=get_type_error(base->type);- base->denotation=DT_Error;+ get_type_error(base->pair);return;}}}else if(get_and_check(translation_data,KW_OPEN_NORMAL)){struct Queue *parameters;- struct Scope *function_prototype_scope;+ struct Normal_Scope *function_prototype_scope;- function_prototype_scope=get_scope(scope);+ function_prototype_scope=(struct Normal_Scope*)get_normal_scope(scope,FUNCTION_PROTOTYPE_SCOPE);parameters=malloc(sizeof(struct Queue));Queue_Init(parameters);parse_paramenter_list(translation_data,function_prototype_scope,parameters);- base->type=get_function_type(base->type,parameters,function_prototype_scope);+ get_function_type(base->pair,parameters,function_prototype_scope);}else{if(get_and_check(translation_data,KW_OPEN_CURLY)){base->is_finished=1;- while(parse_struct_declaration(translation_data,base->inner_namespace,base->members))+ while(parse_struct_declaration(translation_data,(struct Scope*)base->inner_namespace,base->members)){if(get_and_check(translation_data,KW_CLOSE_CURLY)){/*unnamed bitfields are possible*/struct Denoted_Object *obj;- obj=(struct Denoted_Object*)get_denoted_object(NULL,SC_NONE,prototype->type);- obj->object->type=get_type_bitfield(prototype->type,parse_expression(translation_data,scope));+ obj=(struct Denoted_Object*)get_denoted_object(NULL,SC_NONE,prototype->pair->type);+ get_type_bitfield(prototype->pair,parse_expression(translation_data,scope));return (struct Denoted*)obj;}else{if(hold->denotation==DT_Object){- ((struct Denoted_Object*)hold)->object->type=get_type_bitfield(((struct Denoted_Object*)hold)->object->type,parse_expression(translation_data,scope));+ /*TODO make bitfield*/return hold;}else{parameter-list:(declaratoion-specifiers (declarator | abstract-declarator),)+*/- void parse_paramenter_list(struct Translation_Data *translation_data,struct Scope *function_prototype_scope,struct Queue *parameters)+ void parse_paramenter_list(struct Translation_Data *translation_data,struct Normal_Scope *function_prototype_scope,struct Queue *parameters){if(get_and_check(translation_data,KW_CLOSE_NORMAL))return;struct Denotation_Prototype *prototype;- struct Denoted_Base temp;+ struct Denoted_Base *base;struct Denoted *hold;- temp.denotation=DT_Prototype;do{- prototype=parse_declaration_specifiers(translation_data,function_prototype_scope);+++ prototype=parse_declaration_specifiers(translation_data,(struct Scope*)function_prototype_scope);+ base=get_denoted_base(prototype);+- temp.id=NULL;- temp.type=prototype->type;+ parse_declarator_inner(translation_data,(struct Scope*)function_prototype_scope,base);+ if(base->denotation!=DT_Object)+ {+ /*TODO error*/+ push_translation_error("expected object declaration in function prototype",translation_data);+ // delete_denoted(hold);+ delete_denoted_prototype(prototype);+ delete_denoted_base(base);+ return;+ }- parse_declarator_inner(translation_data,function_prototype_scope,&temp);+ hold=extract_denoted(base,prototype,1);- hold=extract_denoted(&temp,prototype,1);+ Scope_Push((struct Scope*)function_prototype_scope,hold);+ Queue_Push(parameters,((struct Denoted_Object*)hold)->object->type);- Scope_Push(function_prototype_scope,hold);- Queue_Push(parameters,hold);+ delete_denoted(hold);+ delete_denoted_prototype(prototype);+ delete_denoted_base(base);- free(prototype);}while(get_and_check(translation_data,KW_COMMA));if(!get_and_check(translation_data,KW_CLOSE_NORMAL)){struct Denotation_Prototype *prototype;struct Type *ret;prototype=parse_specifier_qualifier_list(translation_data,scope);- ret=parse_abstract_declarator(translation_data,scope,prototype->type);- free(prototype);+ ret=parse_abstract_declarator(translation_data,scope,prototype);+ delete_denoted_prototype(prototype);return ret;}/*abstract-declarator:( pointer )* abstract-direct-declarator*/- struct Type* parse_abstract_declarator(struct Translation_Data *translation_data,struct Scope *scope,struct Type *base)+ struct Type* parse_abstract_declarator(struct Translation_Data *translation_data,struct Scope *scope,struct Denotation_Prototype *prototype){- struct Denoted_Base hold;- hold.denotation=DT_Prototype;- hold.id=NULL;- hold.type=base;- parse_declarator_inner(translation_data,scope,&hold);+ struct Denoted_Base *base;+ struct Type *ret;+- if(hold.denotation==DT_Error || hold.id!=NULL)+ get_denoted_base(prototype);+ parse_declarator_inner(translation_data,scope,base);++ if(base->denotation==DT_Error || base->id!=NULL){/*TODO error*/push_translation_error("unexpedted id in abstract declarator",translation_data);- return get_type_error(hold.type);+ delete_denoted_prototype(prototype);+ ret=base->pair->type;+ delete_denoted_base(base);+ return ret;}- return hold.type;+ delete_denoted_prototype(prototype);+ ret=base->pair->type;+ delete_denoted_base(base);+ return ret;+}/*F diff --git a/parse/parse_declaration.h b/parse/parse_declaration.h --- a/parse/parse_declaration.h +++ b/parse/parse_declaration.h#include <lexer.h>#include <assert.h>- struct Denoted_Base;- struct Denotation_Prototype;- struct Struct_Union;- struct Denoted_Object;- struct Enum;void parse_declaration(struct Translation_Data *translation_data,struct Scope *scope,struct Queue *where_to_push,char parse_function_definitions);char parse_struct_declaration(struct Translation_Data *translation_data,struct Scope *struct_scope,struct Queue* members);struct Denoted* parse_struct_declarator(struct Translation_Data *translation_data,struct Scope *scope,struct Denotation_Prototype *prototype);void parse_enum_specifier_finish(struct Translation_Data *translation_data,struct Scope *scope,struct Enum *enumeration);- void parse_paramenter_list(struct Translation_Data *translation_data,struct Scope *function_prototype_scope,struct Queue *parameters);+ void parse_paramenter_list(struct Translation_Data *translation_data,struct Normal_Scope *function_prototype_scope,struct Queue *parameters);struct Type* parse_type_name(struct Translation_Data *translation_data,struct Scope *scope);- struct Type* parse_abstract_declarator(struct Translation_Data *translation_data,struct Scope *scope,struct Type *base);+ struct Type* parse_abstract_declarator(struct Translation_Data *translation_data,struct Scope *scope,struct Denotation_Prototype *prototype);struct AST* parse_initializer(struct Translation_Data *translation_data,struct Scope *scope,struct Denoted_Object *base);#endifF diff --git a/parse/parse_statement.c b/parse/parse_statement.c --- a/parse/parse_statement.c +++ b/parse/parse_statement.cstruct AST_Labeled_Statement *ret;if(check(translation_data,KW_ID,0)){- ret=get_labeled_statement_tree(Queue_Pop(translation_data->tokens),NULL,scope,type);+ ret=get_labeled_statement_tree(Queue_Pop(translation_data->tokens),NULL,type);if(get_and_check(translation_data,KW_COLUMN)){ret->statement=parse_statement(translation_data,scope);struct AST* parse_finish_default_statement(struct Translation_Data* translation_data,struct Scope *scope){struct AST_Labeled_Statement *ret;- ret=get_labeled_statement_tree(NULL,NULL,scope,ST_DEFAULT);+ ret=get_labeled_statement_tree(NULL,NULL,ST_DEFAULT);if(get_and_check(translation_data,KW_COLUMN)){ret->statement=parse_statement(translation_data,scope);F diff --git a/semantics/ast.c b/semantics/ast.c --- a/semantics/ast.c +++ b/semantics/ast.c- struct AST_Labeled_Statement* get_labeled_statement_tree(struct token *label,struct AST* statement,struct Scope *scope,enum AST_Type type)+ struct AST_Labeled_Statement* get_labeled_statement_tree(struct token *label,struct AST* statement,enum AST_Type type){struct AST_Labeled_Statement *ret;ret=malloc(sizeof(struct AST_Labeled_Statement));ret->type=type;ret->label=label;ret->statement=statement;- ret->scope=scope;return ret;}ret=malloc(sizeof(struct AST_Compound_Statement));ret->type=ST_COMPOUND;Queue_Init(&ret->components);- ret->scope=get_scope(parent_scope);+ ret->scope=get_normal_scope(parent_scope,BLOCK_SCOPE);return ret;}struct AST_If_Statement* get_if_statement_tree()- struct AST_Type_Definition* get_type_definition_tree(struct Denoted_Typedef *definition,struct Scope *scope)+ struct AST_Type_Definition* get_type_definition_tree(struct Denoted_Typedef *definition){struct AST_Type_Definition *ret;ret=malloc(sizeof(struct AST_Type_Definition));ret->type=ST_TYPE_DEFINITION;ret->definition=definition;- ret->scope=scope;return ret;}- struct AST_Object_Declaration* get_object_declaration_tree(struct Denoted_Object *object,struct AST *initializer,struct Scope *scope)+ struct AST_Object_Declaration* get_object_declaration_tree(struct Denoted_Object *object,struct AST *initializer){struct AST_Object_Declaration *ret;ret=malloc(sizeof(struct AST_Object_Declaration));ret->type=ST_OBJECT_DECLARATION;ret->object=object;- ret->scope=scope;ret->initializer=initializer;return ret;ret=malloc(sizeof(struct AST_Function_Definition));ret->type=ST_FUNCTION_DEFINITION;ret->function=function;- ret->scope=scope;return ret;}ret=malloc(sizeof(struct AST_Function_Declaration));ret->type=ST_FUNCTION_DECLARATION;ret->function=function;- ret->scope=scope;return ret;}struct AST_Translation_Unit* get_translation_unit_tree(struct Scope* parent_scope)ret=malloc(sizeof(struct AST_Translation_Unit));ret->type=TRANSLATION_UNIT;Queue_Init(&ret->components);- ret->scope=get_scope(parent_scope);+ ret->scope=get_normal_scope(parent_scope,FILE_SCOPE);return ret;}+ void delete_ast(struct AST* ast)+ {+ switch(ast->type)+ {+ case OP_COMMA:+ case OP_ADDITION:+ case OP_SUBTRACTION:+ case OP_MUL:+ case OP_DIV:+ case OP_REMAINDER:+ case OP_ASSIGN:+ case OP_ADD_ASSIGN:+ case OP_SUBTRACT_ASSIGN:+ case OP_MULTIPLY_ASSIGN:+ case OP_REMAINDER_ASSIGN:+ case OP_DIV_ASSIGN:+ case OP_SHIFT_LEFT_ASSIGN:+ case OP_AND_ASSIGN:+ case OP_SHIFT_RIGHT_ASSIGN:+ case OP_XOR_ASSIGN:+ case OP_PIPE_ASSIGN:+ case OP_LOGICAL_AND:+ case OP_LOGICAL_OR:+ case OP_BITWISE_OR:+ case OP_BITWISE_AND:+ case OP_BITWISE_XOR:+ case OP_MEMBER_TROUGH_PTR:+ case OP_MEMBER:+ case OP_ARR_SUBSCRIPT:+ case OP_SHIFT_LEFT:+ case OP_LESS_EQ:+ case OP_NOT_EQUAL:+ case OP_EQUAL:+ case OP_GREATER:+ case OP_LESS:+ case OP_GREATER_EQ:+ case OP_SHIFT_RIGHT:+ delete_ast_binary_expression((struct AST_Binary_Expression*)ast);+ break;+ case OP_COND:+ delete_ast_conditional_expression((struct AST_Conditional_Expression*)ast);+ break;+ case OP_FUNCTION:+ delete_ast_function_expression((struct AST_Function_Expression*)ast);+ break;+ case OP_NOP:+ /*it is just a ast node*/+ free(ast);+ break;+ case OP_LOGICAL_NOT:+ case OP_BITWISE_NOT:+ case OP_ADDR_OF:+ case OP_DEREFERENCE:+ case OP_POSTFIX_INC:+ case OP_POSTFIX_DEC:+ case OP_PREFIX_INC:+ case OP_PREFIX_DEC:+ case OP_UNARY_PLUS:+ case OP_UNARY_MINUS:+ case OP_CAST:+ case OP_SIZEOF:+ delete_ast_unary_expression((struct AST_Unary_Expression*)ast);+ break;+ case OP_LVALUE:+ delete_ast_lvalue_expression((struct AST_Lvalue_Expression*)ast);+ break;+ case OP_RVALUE:+ delete_ast_rvalue_expression((struct AST_Rvalue_Expression*)ast);+ break;+ case ST_COMPOUND:+ delete_ast_compound_statement((struct AST_Compound_Statement*)ast);+ break;+ case ST_SWITCH:+ delete_ast_switch_statement((struct AST_Switch_Statement*)ast);+ break;+ case ST_IF:+ delete_ast_if_statemtent((struct AST_If_Statement*)ast);+ break;+ case ST_WHILE:+ delete_ast_while_statemtent((struct AST_While_Statement*)ast);+ break;+ case ST_DO_WHILE:+ delete_ast_do_while_statement((struct AST_Do_While_Statement*)ast);+ break;+ case ST_GOTO:+ delete_ast_goto_statemtent((struct AST_Goto_Statement*)ast);+ break;+ case ST_LABEL:+ case ST_CASE:+ delete_ast_labeled_statement((struct AST_Labeled_Statement*)ast);+ break;+ case ST_DEFAULT:+ case ST_CONTINUE:+ case ST_BREAK:+ /*I think it doesnt come with anything*/+ free(ast);+ break;+ case ST_RETURN:+ delete_ast_return_statement((struct AST_Return_Statement*)ast);+ break;+ case ST_FOR:+ delete_ast_for_statement((struct AST_For_Statement*)ast);+ break;+ case ST_OBJECT_DECLARATION:+ delete_ast_object_declaration((struct AST_Object_Declaration*)ast);+ break;+ case ST_TYPE_DEFINITION:+ delete_ast_type_definition((struct AST_Type_Definition*)ast);+ break;+ case ST_FUNCTION_DEFINITION:+ delete_ast_function_definition((struct AST_Function_Definition*)ast);+ break;+ case ST_FUNCTION_DECLARATION:+ delete_ast_function_declaration((struct AST_Function_Declaration*)ast);+ break;+ case TRANSLATION_UNIT:+ delete_ast_translation_unit((struct AST_Translation_Unit*)ast);+ break;+ case ERROR:+ delete_ast_error((struct AST_Error*)ast);+ break;+ case ERROR_DECLARATION:+ delete_ast_declaration_error((struct AST_Declaration_Error*)ast);+ break;+ default:+ assert(0);+ }+ }+ void delete_ast_error(struct AST_Error *error)+ {+ if(error->error!=NULL)+ delete_ast(error->error);+ free(error);+ }+ void delete_ast_declaration_error(struct AST_Declaration_Error *error)+ {+ if(error->error!=NULL)+ delete_denoted_error((struct Denoted_Error*)error->error);+ free(error);+ }+ void delete_ast_binary_expression(struct AST_Binary_Expression *binary_expression)+ {+ if(binary_expression->left!=NULL)+ delete_ast(binary_expression->left);+ if(binary_expression->right!=NULL)+ delete_ast(binary_expression->right);++ free(binary_expression);+ }+ void delete_ast_conditional_expression(struct AST_Conditional_Expression *cond_expression)+ {++ if(cond_expression->left!=NULL)+ delete_ast(cond_expression->left);+ if(cond_expression->center!=NULL)+ delete_ast(cond_expression->center);+ if(cond_expression->right!=NULL)+ delete_ast(cond_expression->right);++ free(cond_expression);+ }+ void delete_ast_function_expression(struct AST_Function_Expression *function_expression)+ {+ struct Queue_Node *it;+ if(function_expression->id!=NULL)+ delete_ast(function_expression->id);+ while(function_expression->arguments.size>0)+ delete_ast(Queue_Pop(&function_expression->arguments));++ free(function_expression);++ }+ void delete_ast_rvalue_expression(struct AST_Rvalue_Expression *rval_expression)+ {+ free(rval_expression->id);+ free(rval_expression);+ }+ void delete_ast_lvalue_expression(struct AST_Lvalue_Expression *lval_expression)+ {+ free(lval_expression->id);+ free(lval_expression);+ }+ void delete_ast_unary_expression(struct AST_Unary_Expression *unary_expression)+ {+ if(unary_expression->operand!=NULL)+ delete_ast(unary_expression->operand);+ free(unary_expression);+ }+ void delete_ast_labeled_statement(struct AST_Labeled_Statement *labeled_statement)+ {+ if(labeled_statement->label!=NULL)+ free(labeled_statement->label);+ if(labeled_statement->statement!=NULL)+ delete_ast(labeled_statement->statement);+ free(labeled_statement);+ }+ void delete_ast_compound_statement(struct AST_Compound_Statement *compound_statement)+ {+ if(compound_statement->scope!=NULL)+ delete_scope(compound_statement->scope);+ while(compound_statement->components.size>0)+ delete_ast((struct AST*)Queue_Pop(&compound_statement->components));+ free(compound_statement);+ }+ void delete_ast_for_statement(struct AST_For_Statement *for_statement)+ {+ if(for_statement->condition!=NULL)+ delete_ast(for_statement->condition);+ if(for_statement->initialisation!=NULL)+ delete_ast(for_statement->initialisation);+ if(for_statement->update!=NULL)+ delete_ast(for_statement->update);+ if(for_statement->body_statement!=NULL)+ delete_ast(for_statement->body_statement);+ free(for_statement);+ }+ void delete_ast_while_statemtent(struct AST_While_Statement *while_statement)+ {+ if(while_statement->condition!=NULL)+ delete_ast(while_statement->condition);+ if(while_statement->body_statement!=NULL)+ delete_ast(while_statement->body_statement);+ free(while_statement);+ }+ void delete_ast_do_while_statement(struct AST_Do_While_Statement *do_while_statement)+ {+ if(do_while_statement->condition!=NULL)+ delete_ast(do_while_statement->condition);+ if(do_while_statement->body_statement!=NULL)+ delete_ast(do_while_statement->body_statement);+ free(do_while_statement);+ }+ void delete_ast_if_statemtent(struct AST_If_Statement *if_statement)+ {+ if(if_statement->condition!=NULL)+ delete_ast(if_statement->condition);+ if(if_statement->body_statement!=NULL)+ delete_ast(if_statement->body_statement);+ if(if_statement->else_statement!=NULL)+ delete_ast(if_statement->else_statement);+ free(if_statement);+ }+ void delete_ast_goto_statemtent(struct AST_Goto_Statement *goto_statement)+ {+ if(goto_statement->label!=NULL)+ free(goto_statement->label);+ free(goto_statement);+ }+ void delete_ast_switch_statement(struct AST_Switch_Statement *switch_statement)+ {+ if(switch_statement->condition!=NULL)+ delete_ast(switch_statement->condition);+ if(switch_statement->body_statement!=NULL)+ delete_ast(switch_statement->body_statement);+ free(switch_statement);+ }+ void delete_ast_return_statement(struct AST_Return_Statement *return_statement)+ {+ if(return_statement->return_expression!=NULL)+ delete_ast(return_statement->return_expression);+ free(return_statement);+ }+ void delete_ast_type_definition(struct AST_Type_Definition *type_definition)+ {+ /*deleting denoted objects in scopes*/+ free(type_definition);+ }+ void delete_ast_object_declaration(struct AST_Object_Declaration *object_declaration)+ {+ if(object_declaration->initializer!=NULL)+ delete_ast(object_declaration->initializer);++ }+ void delete_ast_function_definition(struct AST_Function_Definition *function_definition)+ {+ free(function_definition);+ }+ void delete_ast_function_declaration(struct AST_Function_Declaration *function_declaration)+ {+ free(function_declaration);+ }+ void delete_ast_translation_unit(struct AST_Translation_Unit *translation_unit)+ {+ while(translation_unit->components.size>0)+ delete_ast((struct AST*)Queue_Pop(&translation_unit->components));+ if(translation_unit->scope!=NULL)+ delete_scope(translation_unit->scope);+ free(translation_unit);+ }F diff --git a/semantics/ast.h b/semantics/ast.h --- a/semantics/ast.h +++ b/semantics/ast.hstruct Type *value_type;struct token *id;/*TODO*/- void *object;+ struct Object *object;};struct AST_Unary_Expression{enum AST_Type type;struct token *label;struct AST *statement;- struct Scope *scope;};struct AST_Compound_Statement{enum AST_Type type;+struct Scope *scope;struct Queue components;};{enum AST_Type type;struct Denoted_Typedef *definition;- struct Scope *scope;};struct AST_Object_Declaration{enum AST_Type type;struct Denoted_Object *object;struct AST *initializer;- struct Scope *scope;};struct AST_Function_Definition{enum AST_Type type;struct Denoted_Function *function;- struct Scope *scope;};struct AST_Function_Declaration{enum AST_Type type;struct Denoted_Function *function;- struct Scope *scope;};struct AST_Translation_Unit{struct AST_Unary_Expression* get_unary_expression_tree(struct AST *operand,enum AST_Type type);struct AST_Rvalue_Expression* get_rvalue_expression_tree(struct token *id);struct AST_Lvalue_Expression* get_lvalue_expression_tree(struct token *id,struct Scope* scope);- struct AST_Labeled_Statement* get_labeled_statement_tree(struct token *label,struct AST* statement,struct Scope *scope,enum AST_Type type);+ struct AST_Labeled_Statement* get_labeled_statement_tree(struct token *label,struct AST* statement,enum AST_Type type);struct AST_Compound_Statement* get_compound_statement_tree(struct Scope *parent_scope);struct AST_If_Statement* get_if_statement_tree();struct AST_Switch_Statement* get_switch_statement_tree();struct AST_Return_Statement* get_return_statement_tree(struct AST* return_expression);struct AST_Goto_Statement* get_goto_statement_tree(struct token *label,struct Scope *scope);struct AST* get_nop_tree();- struct AST_Type_Definition* get_type_definition_tree(struct Denoted_Typedef *definition,struct Scope *scope);- struct AST_Object_Declaration* get_object_declaration_tree(struct Denoted_Object *object,struct AST *initializer,struct Scope *scope);+ struct AST_Type_Definition* get_type_definition_tree(struct Denoted_Typedef *definition);+ struct AST_Object_Declaration* get_object_declaration_tree(struct Denoted_Object *object,struct AST *initializer);struct AST_Function_Declaration* get_function_declaration_tree(struct Scope *scope,struct Denoted_Function *function);struct AST_Function_Definition* get_function_definition_tree(struct Scope *scope,struct Denoted_Function *function);struct AST_Translation_Unit* get_translation_unit_tree(struct Scope* parent_scope);+ void delete_ast(struct AST* ast);+ void delete_ast_error(struct AST_Error *error);+ void delete_ast_declaration_error(struct AST_Declaration_Error *error);+ void delete_ast_binary_expression(struct AST_Binary_Expression *binary_expression);+ void delete_ast_conditional_expression(struct AST_Conditional_Expression *cond_expression);+ void delete_ast_function_expression(struct AST_Function_Expression *function_expression);+ void delete_ast_rvalue_expression(struct AST_Rvalue_Expression *rval_expression);+ void delete_ast_lvalue_expression(struct AST_Lvalue_Expression *lval_expression);+ void delete_ast_unary_expression(struct AST_Unary_Expression *unary_expression);+ void delete_ast_labeled_statement(struct AST_Labeled_Statement *labeled_statement);+ void delete_ast_compound_statement(struct AST_Compound_Statement *compound_statement);+ void delete_ast_for_statement(struct AST_For_Statement *for_statement);+ void delete_ast_while_statemtent(struct AST_While_Statement *while_statement);+ void delete_ast_do_while_statement(struct AST_Do_While_Statement *do_while_statement);+ void delete_ast_if_statemtent(struct AST_If_Statement *if_statement);+ void delete_ast_goto_statemtent(struct AST_Goto_Statement *goto_statement);+ void delete_ast_switch_statement(struct AST_Switch_Statement *switch_statement);+ void delete_ast_return_statement(struct AST_Return_Statement *return_statement);+ void delete_ast_type_definition(struct AST_Type_Definition *type_definition);+ void delete_ast_object_declaration(struct AST_Object_Declaration *object_declaration);+ void delete_ast_function_definition(struct AST_Function_Definition *function_definition);+ void delete_ast_function_declaration(struct AST_Function_Declaration *function_declaration);+ void delete_ast_translation_unit(struct AST_Translation_Unit *translation_unit);#define BIN_EXPR_PTR(x) ((struct AST_Binary_Expression*)(x))F diff --git a/semantics/denoted.c b/semantics/denoted.c --- a/semantics/denoted.c +++ b/semantics/denoted.c#ifndef GCC_DENOTED_C#define GCC_DENOTED_C GCC_DENOTED_C- #include "denoted.h"+ #include <denoted.h>struct Denoted* get_denoted_error(struct Denoted *error){return (struct Denoted*)ret;}- struct Denoted* get_denoted_base(struct token *id,struct Type *type,enum Denotation_Type denotation)+ struct Denoted_Base* get_denoted_base(struct Denotation_Prototype *prototype){struct Denoted_Base *ret;ret=malloc(sizeof(struct Denoted_Base));- ret->denotation=denotation;- ret->id=id;- ret->type=type;-+ ret->denotation=prototype->denotation;+ ret->id=NULL;+ ret->pair=get_type_map_pair(prototype->pair->type,prototype->pair->node);- return (struct Denoted*)ret;+ return ret;}struct Denoted* get_denoted_function(struct token *id,struct Type *return_type,enum Function_Specifier fs){struct Denoted_Typedef *ret;ret=malloc(sizeof(struct Denoted_Typedef));ret->denotation=DT_Typedef;- ret->type=base->type;+ ret->node=base->pair->node;ret->id=base->id;return (struct Denoted*)ret;return (struct Denoted*)ret;}- struct Denoted* get_denotation_prototype()+ struct Denoted* get_denotation_prototype(struct Map *types){struct Denotation_Prototype *ret;ret=malloc(sizeof(struct Denotation_Prototype));ret->denotation=DT_Prototype;- ret->type=NULL;+ ret->pair=get_type_map_pair(NULL,types);ret->storage_class=SC_NONE;ret->specifier=TS_NONE;ret->constraint=TC_NONE;}struct Denoted* extract_denoted(struct Denoted_Base *base,struct Denotation_Prototype *prototype,char allow_abstract){- if(base->type->specifier==TS_FUNC)+ if(base->pair->type->specifier==TS_FUNC){if(base->id==NULL && !allow_abstract){- return get_denoted_error(get_denoted_function(NULL,((struct Type_Function*)base->type)->return_type,prototype->function_specifier));+ return get_denoted_error(get_denoted_function(NULL,((struct Type_Function*)base->pair->type)->return_type,prototype->function_specifier));}else{- return get_denoted_function(base->id,base->type,prototype->function_specifier);+ return get_denoted_function(base->id,base->pair->type,prototype->function_specifier);}}else if(prototype->storage_class==SC_TYPEDEF){{if(base->id==NULL && !allow_abstract){- return get_denoted_error(get_denoted_object(base->id,prototype->storage_class,base->type));+ return get_denoted_error(get_denoted_object(base->id,prototype->storage_class,base->pair->type));}else{- return get_denoted_object(base->id,prototype->storage_class,base->type);+ return get_denoted_object(base->id,prototype->storage_class,base->pair->type);}}}++ void delete_denoted(struct Denoted *denoted)+ {+ switch(denoted->denotation)+ {+ case DT_Label:+ free(denoted);+ break;+ case DT_Object:+ delete_denoted_object((struct Denoted_Object*)denoted);+ break;+ case DT_Typedef:+ delete_denoted_typedef((struct Denoted_Typedef*)denoted);+ break;+ case DT_Function:+ delete_denoted_function((struct Denoted_Function*)denoted);+ break;+ case DT_Enum:+ delete_denoted_enum((struct Denoted_Enum*)denoted);+ break;+ case DT_Enum_Constant:+ delete_denoted_enum_constant((struct Denoted_Enum_Const*)denoted);+ break;+ case DT_Struct_Union_Tag:+ delete_denoted_struct_union((struct Denoted_Struct_Union*)denoted);+ break;+ case DT_Error:+ delete_denoted_error((struct Denoted_Error*)denoted);+ break;+ case DT_Prototype:+ default:+ assert(0);+ }+ }+ void delete_denoted_error(struct Denoted_Error *error)+ {+ if(error->error!=NULL)+ delete_denoted(error->error);+ free(error);+ }+ void delete_denoted_function(struct Denoted_Function *function)+ {+ if(function->id!=NULL)+ free(function->id);+ if(function->body!=NULL)+ delete_ast_compound_statement(function->body);+ free(function);+ }+ void delete_denoted_object(struct Denoted_Object *object)+ {+ if(object->id!=NULL)+ free(object->id);+ if(object->object!=NULL)+ delete_object(object->object);+ free(object);+ }+ void delete_denoted_typedef(struct Denoted_Typedef *typedefed)+ {+ if(typedefed->id!=NULL)+ free(typedefed->id);+ free(typedefed);+ }+ void delete_denoted_enum(struct Denoted_Enum *enumeration)+ {+ if(enumeration->id!=NULL)+ free(enumeration->id);+ if(enumeration->enumeration!=NULL)+ delete_enum(enumeration->enumeration);+ free(enumeration);+ }+ void delete_denoted_enum_constant(struct Denoted_Enum_Const *enum_const)+ {+ if(enum_const->id!=NULL)+ free(enum_const->id);+ if(enum_const->expression!=NULL)+ delete_ast(enum_const->expression);+ free(enum_const);+ }+ void delete_denoted_struct_union(struct Denoted_Struct_Union *su)+ {+ if(su->id!=NULL)+ free(su->id);+ if(su->struct_union!=NULL)+ delete_struct_union(su->struct_union);+ free(su);+ }+ void delete_object(struct Object *object)+ {+ if(object->location!=NULL)+ delete_location(object->location);+ free(object);+ }+ void delete_denoted_prototype(struct Denotation_Prototype *prototype)+ {+ free(prototype->pair);+ free(prototype);+ }+ void delete_denoted_base(struct Denoted_Base *base)+ {+ free(base->pair);+ free(base);+ }#endifF diff --git a/semantics/denoted.h b/semantics/denoted.h --- a/semantics/denoted.h +++ b/semantics/denoted.h{enum Denotation_Type denotation;struct token *id;- struct Type *type;+ struct Type_Map_Pair *pair;};struct Denoted_Function{{enum Denotation_Type denotation;struct token *id;- struct Type *type;+ struct Map *node;};struct Denoted_Enumstruct Denotation_Prototype{enum Denotation_Type denotation;- struct Type *type;+ struct Type_Map_Pair *pair;enum Storage_Class storage_class;+ struct Denoted_Base* get_denoted_base(struct Denotation_Prototype *prototype);struct Denoted* get_denoted_error(struct Denoted *error);struct Denoted* get_denoted_function(struct token *id,struct Type *return_type,enum Function_Specifier fs);struct Denoted* get_denoted_object(struct token *id, enum Storage_Class sc,struct Type *type);struct Denoted* get_denoted_enum_const_num(struct token *id,struct Enum *parent,int value);struct Denoted* get_denoted_enum(struct token *id,struct Enum *enumerator);struct Denoted* get_denoted_struct_union(struct token *id,struct Struct_Union *struct_union);- struct Denoted* get_denoted_base(struct token *id,struct Type *type,enum Denotation_Type denotation);struct Denoted* extract_denoted(struct Denoted_Base *base,struct Denotation_Prototype *prototype,char allow_abstract);- struct Denoted* get_denotation_prototype();+ struct Denoted* get_denotation_prototype(struct Map *types);+++ void delete_denoted(struct Denoted *denoted);+ void delete_denoted_error(struct Denoted_Error *error);+ void delete_denoted_function(struct Denoted_Function *function);+ void delete_denoted_object(struct Denoted_Object *object);+ void delete_denoted_typedef(struct Denoted_Typedef *typedefed);+ void delete_denoted_enum(struct Denoted_Enum *enumeration);+ void delete_denoted_enum_constant(struct Denoted_Enum_Const *enum_const);+ void delete_denoted_struct_union(struct Denoted_Struct_Union *su);+ void delete_object(struct Object *object);+ void delete_denoted_prototype(struct Denotation_Prototype *prototype);+ void delete_denoted_base(struct Denoted_Base *base);++#endifF diff --git a/semantics/location.c b/semantics/location.c --- a/semantics/location.c +++ b/semantics/location.creturn ret;}++++ void delete_location(struct Location *location)+ {+ free(location);+ }#endifF diff --git a/semantics/location.h b/semantics/location.h --- a/semantics/location.h +++ b/semantics/location.hstruct Location_Relative* get_relative_location(struct Location *base,size_t offset);struct Location* get_location_for_denoted_object(struct Location *base,struct Type *type,struct token *id);++ void delete_location(struct Location *location);+#endifF diff --git a/semantics/program.c b/semantics/program.c --- a/semantics/program.c +++ b/semantics/program.cQueue_Init(ret->translation_units);Queue_Init(ret->source_files);Queue_Init(ret->errors);- ret->externs=get_scope(NULL);+ /*this isn't really a scope,+ TODO rework*/+ ret->externs=get_normal_scope(NULL,EXTERN_SCOPE);return ret;}ret->macros=malloc(sizeof(struct Map));Map_Init(ret->macros);+ ret->types=malloc(sizeof(struct Map));+ Map_Init(ret->types);+ret->number_of_errors_when_last_checked=0;return ret;F diff --git a/semantics/program.h b/semantics/program.h --- a/semantics/program.h +++ b/semantics/program.hstruct Queue *source_files;struct Map *macros;++ struct Map *types;};struct Program* get_program();F diff --git a/semantics/scope.c b/semantics/scope.c --- a/semantics/scope.c +++ b/semantics/scope.c#include "scope.h"- struct Scope* get_scope(struct Scope *parent)+ struct Scope* get_normal_scope(struct Scope *parent,enum Scope_Type type){- struct Scope *ret;- ret=malloc(sizeof(struct Scope));- Map_Init(&ret->labels);+ struct Normal_Scope *ret;+ assert(type==BLOCK_SCOPE || type==EXTERN_SCOPE || type==FILE_SCOPE || type==FUNCTION_PROTOTYPE_SCOPE);+ ret=malloc(sizeof(struct Normal_Scope));+ ret->type=type;++ assert((type!=EXTERN_SCOPE) || parent==NULL);+ assert((type!=FILE_SCOPE) || parent->type==EXTERN_SCOPE);+ ret->parent=parent;+Map_Init(&ret->tags);Map_Init(&ret->ordinary);++ return (struct Scope*)ret;++ }+ struct Scope* get_function_scope(struct Scope *parent)+ {+ struct Function_Scope *ret;+ assert(parent!=NULL && parent->type==FILE_SCOPE);++ ret=malloc(sizeof(struct Function_Scope));+ ret->type=FUNCTION_SCOPE;ret->parent=parent;- if(parent==NULL)- {- ret->location=get_global_location();- }else++ Map_Init(&ret->labels);+++ return (struct Scope*)ret;+ }++ void delete_normal_scope(struct Normal_Scope *scope)+ {++ }++ void delete_function_scope(struct Function_Scope *scope)+ {++ }++ void delete_scope(struct Scope *scope)+ {+ switch(scope->type){- ret->location=(struct Location*)get_relative_location(parent->location,0);+ case BLOCK_SCOPE:+ case FILE_SCOPE:+ case EXTERN_SCOPE:+ case FUNCTION_PROTOTYPE_SCOPE:+ delete_normal_scope((struct Normal_Scope*)scope);+ break;+ case FUNCTION_SCOPE:+ delete_function_scope((struct Function_Scope*)scope);+ default:+ assert(0);}- return ret;}+void* check_label(struct Scope *current,struct token *id){void *hold;hold=NULL;- while(current!=NULL && hold==NULL)+ while(current!=NULL && current->type!=FUNCTION_SCOPE){- hold=Map_Check(¤t->labels,id->data,id->data_size);current=current->parent;}++ if(current!=NULL)+ {+ hold=Map_Check(&((struct Function_Scope*)current)->labels,id->data,id->data_size);+ }return hold;}{void *hold;hold=NULL;- while(current!=NULL && hold==NULL)+ while(current!=NULL && hold==NULL && current->type!=FUNCTION_SCOPE){- hold=Map_Check(¤t->tags,id->data,id->data_size);+ hold=Map_Check(&((struct Normal_Scope*)current)->tags,id->data,id->data_size);current=current->parent;}return hold;hold=NULL;while(current!=NULL && hold==NULL){- hold=Map_Check(¤t->ordinary,id->data,id->data_size);+ hold=Map_Check(&((struct Normal_Scope*)current)->ordinary,id->data,id->data_size);current=current->parent;}return hold;}void push_tag(struct Scope *current,struct token *id,struct Denoted *denot){- Map_Push(¤t->tags,id->data,id->data_size,denot);+ /*TODO fix this shit*/+ assert(current->type!=FUNCTION_SCOPE);++ Map_Push(&((struct Normal_Scope*)current)->tags,id->data,id->data_size,denot);}void push_ordinary(struct Scope *current,struct token *id,struct Denoted *denot){- Map_Push(¤t->ordinary,id->data,id->data_size,denot);+ assert(current->type!=FUNCTION_SCOPE);+ Map_Push(&((struct Normal_Scope*)current)->ordinary,id->data,id->data_size,denot);}#endifF diff --git a/semantics/scope.h b/semantics/scope.h --- a/semantics/scope.h +++ b/semantics/scope.h#include <denoted.h>#include <location.h>++ enum Scope_Type;struct Scope{- Map labels;+ enum Scope_Type type;+ struct Scope *parent;+ };+ struct Normal_Scope+ {+ enum Scope_Type type;+ struct Scope *parent;Map tags;- /*In go denoted*/Map ordinary;-+ };+ struct Function_Scope+ {+ enum Scope_Type type;struct Scope *parent;- struct Location *location;+ Map labels;};- struct Scope* get_scope(struct Scope *parent);+ struct Scope* get_normal_scope(struct Scope *parent,enum Scope_Type type);+ struct Scope* get_function_scope(struct Scope *parent);void* check_label(struct Scope *current,struct token *id);-void push_label(struct Scope *current,struct token *id);/*TODO*/struct Denoted* check_tag(struct Scope *current,struct token *id);void push_ordinary(struct Scope *current,struct token *id,struct Denoted *denot);void Scope_Push(struct Scope *scope,struct Denoted *declarator);+char check_if_typedefed(struct Scope* scope,struct token *id);+++++ void delete_scope(struct Scope *scope);+ void delete_normal_scope(struct Normal_Scope *scope);+ void delete_function_scope(struct Function_Scope *scope);#endifF diff --git a/semantics/scope.hh b/semantics/scope.hh --- a/semantics/scope.hh +++ b/semantics/scope.hh#ifndef GCC_SCOPE_HH#define GCC_SCOPE_HH GCC_SCOPE_HH+ enum Scope_Type{+ EXTERN_SCOPE,+ FILE_SCOPE,+ BLOCK_SCOPE,+ FUNCTION_PROTOTYPE_SCOPE,+ FUNCTION_SCOPE+ };struct Scope;-+ struct Normal_Scope;+ struct Function_Scope;#endifF diff --git a/semantics/type.c b/semantics/type.c --- a/semantics/type.c +++ b/semantics/type.c#include "type.h"+ void type_check_and_push_heavy(struct Type_Map_Pair *pair,struct Type *type)+ {+ switch(type->specifier)+ {+ case TS_VOID:+ case TS_CHAR:+ case TS_INT:+ case TS_FLOAT:+ case TS_DOUBLE:+ type_check_and_push(pair,type,sizeof(struct Type_Basic));+ break;+ case TS_STRUCT:+ case TS_UNION:+ type_check_and_push(pair,type,sizeof(struct Type_Struct_Union));+ break;+ case TS_ENUM:+ type_check_and_push(pair,type,sizeof(struct Type_Enum));+ break;+ case TS_POINTER:+ type_check_and_push(pair,type,sizeof(struct Type_Pointer));+ break;+ case TS_ARRAY:+ type_check_and_push(pair,type,sizeof(struct Type_Array));+ break;+ case TS_FUNC:+ type_check_and_push(pair,type,sizeof(struct Type_Function));+ break;+ case TS_BITFIELD:+ type_check_and_push(pair,type,sizeof(struct Type_Bit_Field));+ break;+ case TS_ERROR:+ type_check_and_push(pair,type,sizeof(struct Type_Error));+ break;+ case TS_NONE:+ default:+ assert(0);+ }+ }++ void type_check_and_push(struct Type_Map_Pair *pair,struct Type *type,size_t struct_size)+ {++ struct Map *hold_node;+ hold_node=Map_Check_And_Get(pair->node,type,struct_size);++ if(hold_node==NULL)+ {+ pair->type=type;+ pair->node=Map_Push_And_Get(pair->node,type,struct_size,pair->type);+ }else+ {+ free(type);+ pair->node=hold_node;+ pair->type=(struct Type*)hold_node->ID;+ }+ }- struct Type* get_type_error(struct Type* error)+ void get_type_error(struct Type_Map_Pair *pair){struct Type_Error *ret;+ret=malloc(sizeof(struct Type_Error));ret->specifier=TS_ERROR;- ret->error=error;+ ret->error=pair->type;++ type_check_and_push(pair,(struct Type*)ret,sizeof(struct Type_Error));- return (struct Type*)ret;}- /*could return error */- struct Type* get_struct_union_type(struct Denotation_Prototype *prototype)+ void get_struct_union_type(struct Denotation_Prototype *prototype){struct Type_Struct_Union *ret;+ assert(prototype->denotation=DT_Prototype);+ prototype->denotation=DT_Object;+ret=malloc(sizeof(struct Type_Struct_Union));ret->specifier=prototype->specifier;ret->struct_union=prototype->struct_union;ret->is_const=prototype->is_const;ret->is_volatile=prototype->is_volatile;+ type_check_and_push(prototype->pair,(struct Type*)ret,sizeof(struct Type_Struct_Union));if(prototype->constraint!=TC_NONE || prototype->sign!=TSIGN_NONE || (prototype->specifier!=TS_UNION && prototype->specifier!=TS_STRUCT)){- return (struct Type*)get_type_error((struct Type*)ret);- }else- {- return (struct Type*)ret;+ get_type_error(prototype->pair);}}struct Struct_Union* get_struct_union_base(struct Scope *scope ,enum Type_Specifier struct_or_union){struct Struct_Union *ret;++ret=malloc(sizeof(struct Struct_Union));ret->specifier=struct_or_union;ret->members=malloc(sizeof(struct Queue));Queue_Init(ret->members);- ret->inner_namespace=get_scope(scope);+ ret->inner_namespace=(struct Normal_Scope*)get_normal_scope(scope,BLOCK_SCOPE);ret->is_finished=0;return ret;return ret;}- /*could return error*/- struct Type* get_basic_type(struct Denotation_Prototype *prototype)+ void get_basic_type(struct Denotation_Prototype *prototype){struct Type_Basic *ret;ret=malloc(sizeof(struct Type_Basic));++ assert(prototype->denotation=DT_Prototype);+ prototype->denotation=DT_Object;+ret->size=prototype->size;ret->is_const=prototype->is_const;ret->is_volatile=prototype->is_volatile;}- switch(ret->specifier)+ type_check_and_push(prototype->pair,(struct Type*)ret,sizeof(struct Type_Basic));++ switch(prototype->pair->type->specifier){case TS_DOUBLE:- if(ret->constraint==TC_LONG_LONG- || ret->constraint==TC_SHORT- || ret->sign!=TSIGN_NONE)+ if(prototype->constraint==TC_LONG_LONG+ || prototype->constraint==TC_SHORT+ || prototype->sign!=TSIGN_NONE){- return (struct Type*)get_type_error((struct Type*)ret);+ get_type_error(prototype->pair);}break;case TS_CHAR:- if(ret->constraint!=TC_NONE)+ if(prototype->constraint!=TC_NONE){- return (struct Type*)get_type_error((struct Type*)ret);+ get_type_error(prototype->pair);}break;case TS_INT:break;default:- if(ret->constraint!=TC_NONE || ret->sign!=TSIGN_NONE)+ if(prototype->constraint!=TC_NONE || prototype->sign!=TSIGN_NONE){- return (struct Type*)get_type_error((struct Type*)ret);+ get_type_error(prototype->pair);}}- return (struct Type*)ret;}- struct Type* get_pointer_type(struct Type* points_to)+ void get_pointer_type(struct Type_Map_Pair *pair,char is_volatile,char is_constant){struct Type_Pointer *ret;ret=malloc(sizeof(struct Type_Pointer));ret->specifier=TS_POINTER;ret->size=PTR_SIZE;- ret->points_to=points_to;- ret->is_const=ret->is_volatile=0;- return (struct Type*)ret;+ ret->points_to=pair->type;+ ret->is_const=is_constant;+ ret->is_volatile=is_volatile;++ type_check_and_push(pair,(struct Type*)ret,sizeof(struct Type_Pointer));}- struct Type* get_array_type(struct Type *is_array_of,struct AST* number_of_elements)+ void get_array_type(struct Type_Map_Pair *pair,struct AST* number_of_elements){struct Type_Array *ret;ret=malloc(sizeof(struct Type_Array));if(number_of_elements!=NULL){ret->number_of_elements=evaluate_const_expression_integer(number_of_elements);+ delete_ast(number_of_elements);}else{ret->number_of_elements=0;}- ret->expression=number_of_elements;- ret->is_array_of=is_array_of;+ ret->is_array_of=pair->type;+ type_check_and_push(pair,(struct Type*)ret,sizeof(struct Type_Array));- return (struct Type*)ret;}- struct Type* get_enum_type(struct Denotation_Prototype *prototype)+ void get_enum_type(struct Denotation_Prototype *prototype){struct Type_Enum *ret;++ assert(prototype->denotation=DT_Prototype);+ prototype->denotation=DT_Object;+ret=malloc(sizeof(struct Type_Enum));ret->specifier=TS_ENUM;ret->enumeration=prototype->enumerator;ret->is_const=prototype->is_const;ret->is_volatile=prototype->is_volatile;++ type_check_and_push(prototype->pair,(struct Type*)ret,sizeof(struct Type_Enum));if(prototype->sign!=TSIGN_NONE || prototype->constraint!=TC_NONE){- return get_type_error((struct Type*)ret);+ get_type_error(prototype->pair);}- return (struct Type*)ret;}- struct Type* get_type_bitfield(struct Type* base,struct AST* number_of_bits)+ void get_type_bitfield(struct Type_Map_Pair *pair,struct AST* number_of_bits){struct Type_Bit_Field *ret;ret=malloc(sizeof(struct Type_Bit_Field));ret->specifier=TS_BITFIELD;- ret->expression=number_of_bits;++ assert(number_of_bits!=NULL);ret->number_of_bits=evaluate_const_expression_integer(number_of_bits);- ret->base=base;+ delete_ast(number_of_bits);++ ret->base=pair->type;- return (struct Type*)ret;+ type_check_and_push(pair,(struct Type*)ret,sizeof(struct Type_Bit_Field));}- struct Type* get_function_type(struct Type* return_type,struct Queue *parameters,struct Scope* function_prototype_scope)++ void get_function_type(struct Type_Map_Pair *pair,struct Queue *parameters,struct Normal_Scope* function_prototype_scope){struct Type_Function *ret;+ size_t i;+ struct Map *hold_node;+ret=malloc(sizeof(struct Type_Function));ret->specifier=TS_FUNC;- ret->return_type=return_type;- ret->parameters=parameters;++ ret->return_type=pair->type;++ret->function_prototype_scope=function_prototype_scope;- return (struct Type*)ret;++ ret->number_of_arguments=parameters->size;+ ret->arguments=malloc(sizeof(struct Type*)*ret->number_of_arguments);+ for(i=0;parameters->size>0;++i)+ {+ ret->arguments[i]=(struct Type*)Queue_Pop(parameters);+ }+ type_check_and_push(pair,(struct Type*)ret,sizeof(struct Type_Function));+}char is_type(struct Translation_Data *translation_data,struct Scope *scope){}}++ struct Type_Map_Pair* get_type_map_pair(struct Type *type,struct Map *types)+ {+ struct Type_Map_Pair *ret;+ ret=malloc(sizeof(struct Type_Map_Pair));+ ret->type=type;+ ret->node=types;++ return ret;+ }++ void delete_enum(struct Enum *enumeration)+ {+ while(enumeration->consts->size>0)+ Queue_Pop(enumeration->consts);+ free(enumeration);+ }+ void delete_struct_union(struct Struct_Union *su)+ {+ delete_scope((struct Scope*)su->inner_namespace);+ while(su->members->size>0)+ Queue_Pop(su->members);++ free(su);+ }#endifF diff --git a/semantics/type.h b/semantics/type.h --- a/semantics/type.h +++ b/semantics/type.h#include <scope.h>#include <limits.h>#include <program.h>+ #include <map.h>+ #include <ast.h>#define PTR_SIZE 4{enum Type_Specifier specifier;size_t size;+ /*queue of denoted objects for preserving the order of the members*/struct Queue *members;- struct Scope *inner_namespace;+ struct Normal_Scope *inner_namespace;char is_finished;};enum Type_Specifier specifier;size_t number_of_bits;struct Type *base;- struct AST *expression;};struct Type_Basic{size_t size;size_t number_of_elements;struct Type *is_array_of;-- struct AST *expression;};struct Type_Function{enum Type_Specifier specifier;struct Type *return_type;- struct Queue *parameters;- struct Scope *function_prototype_scope;+ /*types*/+ size_t number_of_arguments;+ /*array of pointers to Type*/+ struct Type** arguments;+ struct Normal_Scope *function_prototype_scope;};struct Type_Enumchar is_finished;};- struct Type* get_type_error(struct Type* error);- struct Type* get_struct_union_type(struct Denotation_Prototype *prototype);+ struct Type_Map_Pair+ {+ struct Type *type;+ /*corresponding map node*/+ struct Map *node;+ };++++ void type_check_and_push_heavy(struct Type_Map_Pair *pair,struct Type *type);+ void type_check_and_push(struct Type_Map_Pair *pair,struct Type *type,size_t struct_size);++ struct Type_Map_Pair* get_type_map_pair(struct Type *type,struct Map *types);+ void get_type_error(struct Type_Map_Pair *pair);+ void get_struct_union_type(struct Denotation_Prototype *prototype);struct Struct_Union* get_struct_union_base(struct Scope *scope ,enum Type_Specifier struct_or_union);struct Enum *get_enum_base();- struct Type* get_basic_type(struct Denotation_Prototype *prototype);- struct Type* get_pointer_type(struct Type* points_to);- struct Type* get_array_type(struct Type *is_array_of,struct AST* number_of_elements);- struct Type* get_enum_type(struct Denotation_Prototype *prototype);- struct Type* get_type_bitfield(struct Type* base,struct AST* number_of_bits);- struct Type* get_function_type(struct Type* return_type,struct Queue *parameters,struct Scope* function_prototype_scope);+ void get_basic_type(struct Denotation_Prototype *prototype);+ void get_pointer_type(struct Type_Map_Pair *pair,char is_volatile,char is_constant);+ void get_array_type(struct Type_Map_Pair *pair,struct AST* number_of_elements);+ void get_enum_type(struct Denotation_Prototype *prototype);+ void get_type_bitfield(struct Type_Map_Pair *pair,struct AST* number_of_bits);++ void delete_enum(struct Enum *enumeration);+ void delete_struct_union(struct Struct_Union *su);+++ void get_function_type(struct Type_Map_Pair *pair,struct Queue *parameters,struct Normal_Scope* function_prototype_scope);char is_type(struct Translation_Data *translation_data,struct Scope *scope);size_t get_type_size(struct Type *type);++++#endifF diff --git a/semantics/type.hh b/semantics/type.hh --- a/semantics/type.hh +++ b/semantics/type.hhstruct Type_Function;struct Type_Enum;struct Enum;+ struct Type_Map_Pair;+#endifF diff --git a/tests/test3.c b/tests/test3.c --- a/tests/test3.c +++ b/tests/test3.c- int a;- #include "test2.c"- #define max(a,b) (a>b?a:b)- #define version 1- #define current_version 3---- int main()- {- max(1,1);- long long long maxi;- return version;- }+ int a;+ int b;