diff --git a/src/grammar.y b/src/grammar.y index 1b3b04a..9d5d5a3 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -171,6 +171,7 @@ definition: setAsKeyword(node, true); } cur = CreateScope(cur, 0, 0); + setFunScope(node, cur); printdebug("Created a new scope"); } L_PAREN { TableNode * parameter = getParameter(getTypeEntry(table_lookup(getAncestor(cur), $1))); @@ -290,11 +291,11 @@ function_declaration: if(getAdInfoType(table_lookup(cur, $4))==TYPE_FUNCTION_TYPE){ //printf("%s\n",$2); //printf("%s\n",getName(table_lookup(cur, $4))); - CreateEntry(cur,TYPE_FUNCTION_DECLARATION, table_lookup(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, table_lookup(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false,NULL)); } else{ throw_error(ERROR_TYPE, "Function declatation (%s) is not a valid function type", $2); - CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false,NULL)); } } @@ -302,11 +303,11 @@ function_declaration: | EXTERNAL FUNCTION ID COLON ID { if(getAdInfoType(look_up(cur, $5))==TYPE_FUNCTION_TYPE){ - CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false)); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false,NULL)); } else{ throw_error(ERROR_TYPE, "Function declatation (%s) is not a valid function type", $3); - CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false)); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, CreateFunctionDeclarationInfo(-1, false,NULL)); } } @@ -467,7 +468,7 @@ declaration: printdebug("invalid (function) type passed in declaration list in dblock", @2.first_line, @2.first_column); d = TYPE_FUNCTION_DECLARATION; - if(CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1)) == undefined){ + if(CreateEntry(cur,d,(TableNode*)$1,$3,NULL) == undefined){ throw_error(ERROR_TYPE, "Duplicate defination of function in declaration list"); } diff --git a/src/symbol_table.c b/src/symbol_table.c index a7c05be..f4fa1ea 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -456,12 +456,13 @@ int getRecSize(SymbolTable *tn) { // using "as" the input record can be decomposed to give the illusion of // multiple inputs Below function also has the line number where the function is // first defined -AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) { - AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); +AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular, SymbolTable *scope) { + AdInfo *info = (AdInfo *)calloc(1,sizeof(AdInfo)); info->FunDecAdInfo = (function_declaration_info *)malloc( sizeof(function_declaration_info)); info->FunDecAdInfo->startlinenumber = line; info->FunDecAdInfo->regularoras = asorregular; + info->FunDecAdInfo->scope = scope; return info; } // gets the line at which the function was first defined. (Can be used to print @@ -530,6 +531,68 @@ bool getAsKeyword(TableNode *definition) { return definition->additionalinfo->FunDecAdInfo->regularoras; } +SymbolTable *getFunScope(TableNode *definition) { + if (definition == NULL) { + printdebug( + "passed a NULL entry to getScope " + "function. Invalid."); + return NULL; + } + if (getAdInfoType(definition) != TYPE_FUNCTION_DECLARATION) { + printdebug( + "passed in invalid entry type to getScope " + "function. Invalid."); + return NULL; + } + if (definition->additionalinfo == NULL) { + printdebug( + "node has NULL additionalinfo. Invalid."); + return NULL; + } + if(definition->additionalinfo->FunDecAdInfo == NULL) { + printdebug( + "node has NULL additionalinfo. Invalid."); + return NULL; + } + if(definition->additionalinfo->FunDecAdInfo->scope == NULL) { + printdebug( + "node has no scope initialized."); + return NULL; + } + + return definition->additionalinfo->FunDecAdInfo->scope; +} +TableNode *setFunScope(TableNode *tn, SymbolTable *scope) { + if (tn == NULL) { + printdebug( + "passing in a NULL entry to setFunScope. " + "invalid"); + return undefined; + } + if (getAdInfoType(tn) != TYPE_FUNCTION_DECLARATION) { + printdebug( + "passing in an invalid entry to setFunScope. " + "invalid"); + return undefined; + } + if(tn->additionalinfo == NULL) { + printdebug( + "node has NULL additionalinfo. Invalid."); + return undefined; + } + if(tn->additionalinfo->FunDecAdInfo == NULL) { + printdebug( + "node has NULL additionalinfo. Invalid."); + return undefined; + } + if(scope == NULL) { + printdebug( + "passed in an empty scope."); + return undefined; + } + tn->additionalinfo->FunDecAdInfo->scope = scope; + return tn; +} TableNode *setAsKeyword(TableNode *tn, bool as) { if (tn == NULL) { printdebug( @@ -787,10 +850,10 @@ SymbolTable *init(SymbolTable *start) { chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR); stri->additionalinfo = CreateArrayInfo(1, chara); boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL); - reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false); + reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false,NULL); reservetype->additionalinfo = CreateFunctionTypeInfo(integ, addr); releasetype->additionalinfo = CreateFunctionTypeInfo(addr, integ); - release->additionalinfo = CreateFunctionDeclarationInfo(0, false); + release->additionalinfo = CreateFunctionDeclarationInfo(0, false,NULL); integ->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for integ addr->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for addr @@ -1308,10 +1371,16 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) { char *functiontype = (char *)malloc(100); sprintf(functiontype, " %s", getName(getTypeEntry(entry))); + char* functionScope = (char *)malloc(100); + if(getLine(getFunScope(entry)) < 1){ + sprintf(functionScope, " Function not defined before runtime"); + }else{ + sprintf(functionScope, " Function Definition that starts at line %d",getLine(getFunScope(entry))); + } if (parentScopeNum == 0) { - st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition"); + st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, functionScope); } else { - st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, " Function Definition"); + st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, functionScope); } } diff --git a/src/symbol_table.h b/src/symbol_table.h index e730595..f838314 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -20,6 +20,7 @@ extern FILE *ir_flag; struct TableNode; typedef struct TFList TFList; typedef struct CGNode CGNode; +typedef struct SymbolTable SymbolTable; typedef struct Constant_Stack { struct TableNode *theType; @@ -47,6 +48,7 @@ typedef struct { typedef struct { int startlinenumber; bool regularoras; + SymbolTable* scope; } function_declaration_info; typedef struct { @@ -120,7 +122,9 @@ int getRecLength(TableNode *definition); SymbolTable *getRecList(TableNode *definition); TableNode *setRecSize(TableNode *tn, int n); int getRecSize(SymbolTable *tn); -AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular); +AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular,SymbolTable *scope); +TableNode *setFunScope(TableNode *tn, SymbolTable *scope); +SymbolTable *getFunScope(TableNode *definition); int getStartLine(TableNode *definition); TableNode *setStartLine(TableNode *tn, int start); bool getAsKeyword(TableNode *definition);