diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 12:40:19 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 12:40:19 -0600 |
commit | 53c5a841d7131cc119c6bafdf5387d732eb98c02 (patch) | |
tree | da5542f49abb4b3004aec86e4931dcf61d183e24 /include | |
parent | 47ba6bf1100fe1da8d80607053d88cfd2abe25ff (diff) | |
download | chill-53c5a841d7131cc119c6bafdf5387d732eb98c02.tar.gz chill-53c5a841d7131cc119c6bafdf5387d732eb98c02.tar.bz2 chill-53c5a841d7131cc119c6bafdf5387d732eb98c02.zip |
Added variable & typedef scoping
Diffstat (limited to 'include')
-rw-r--r-- | include/chillAST/chillAST_def.hh | 1 | ||||
-rw-r--r-- | include/chillAST/chillAST_node.hh | 38 | ||||
-rw-r--r-- | include/chillAST/chillASTs.hh | 177 |
3 files changed, 19 insertions, 197 deletions
diff --git a/include/chillAST/chillAST_def.hh b/include/chillAST/chillAST_def.hh index 7d729d6..59a7f95 100644 --- a/include/chillAST/chillAST_def.hh +++ b/include/chillAST/chillAST_def.hh @@ -180,6 +180,7 @@ typedef std::vector<chillAST_TypedefDecl *> chillAST_TypedefTable; chillAST_VarDecl *symbolTableFindName(chillAST_SymbolTable *table, const char *name); chillAST_VarDecl *symbolTableFindVariableNamed(chillAST_SymbolTable *table, const char *name); +chillAST_TypedefDecl *typedefTableFindName(chillAST_TypedefTable *table, const char *name); void printSymbolTable(chillAST_SymbolTable *st); void printSymbolTableMoreInfo(chillAST_SymbolTable *st); diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh index 6cc6cfd..9788588 100644 --- a/include/chillAST/chillAST_node.hh +++ b/include/chillAST/chillAST_node.hh @@ -143,15 +143,20 @@ public: virtual bool isAUnion() { return false; }; - virtual bool hasSymbolTable() { return false; }; // most nodes do NOT have a symbol table - virtual bool hasTypedefTable() { return false; }; // most nodes do NOT have a typedef table - virtual chillAST_SymbolTable *getSymbolTable() { return NULL; } // most nodes do NOT have a symbol table + virtual chillAST_SymbolTable* getSymbolTable() { return symbolTable; } + virtual chillAST_TypedefTable* getTypedefTable() {return typedefTable; } + virtual void addVariableToScope(chillAST_VarDecl *vd); + virtual void addTypedefToScope(chillAST_TypedefDecl *tdd); + chillAST_TypedefDecl* findTypeDecleration(const char *t); + chillAST_VarDecl* findVariableDecleration(const char *t); + chillAST_VarDecl* getVariableDeclaration(const char *vn); + chillAST_TypedefDecl* getTypeDeclaration(const char *tn); virtual chillAST_VarDecl *findVariableNamed(const char *name); // recursive chillAST_RecordDecl *findRecordDeclNamed(const char *name); // recursive - // void addDecl( chillAST_VarDecl *vd); // recursive, adds to first symbol table it can find + // void addDecl( chillAST_VarDecl *vd); // recursive, adds to first symbol table it can find int getNumChildren() { return children.size(); }; @@ -168,6 +173,7 @@ public: void setMetaComment(const char *c) { metacomment = strdup(c); }; virtual void chillMergeChildInfo(chillAST_Node){ + // TODO if (par) par->add to definition for vardecl/typedecl // TODO if (par) par->getSourceFile()->addFunc(this); for FuncDecl // TODO if (par) par->getSourceFile()->addMacro(this); For MacroDecl // TODO if (parent) parent->addVariableToSymbolTable(this); // should percolate up until something has a symbol table @@ -366,7 +372,7 @@ public: }; virtual chillAST_VarDecl *findArrayDecl(const char *name) { // scoping TODO - if (!hasSymbolTable()) return parent->findArrayDecl(name); // most things + if (!getSymbolTable()) return parent->findArrayDecl(name); // most things else fprintf(stderr, "(%s) forgot to implement gatherArrayVarDecls()\n", getTypeString()); } @@ -426,7 +432,7 @@ public: virtual void printonly(int indent = 0, FILE *fp = stderr) { print(indent, fp); }; - virtual void get_top_level_loops(std::vector<chillAST_ForStmt *> &loops) { + virtual void getTopLevelLoops(std::vector<chillAST_ForStmt *> &loops) { int n = children.size(); //fprintf(stderr, "get_top_level_loops of a %s with %d children\n", getTypeString(), n); for (int i = 0; i < n; i++) { @@ -507,26 +513,6 @@ public: exit(-1); } - virtual chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "%s looking for datatype %s\n", getTypeString(), t); - if (parent != NULL) return parent->findDatatype(t); // most nodes do this - return NULL; - } - - - virtual chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { - if (!parent) { - fprintf(stderr, "%s with no parent addVariableToSymbolTable()\n", getTypeString()); - exit(-1); - } - //fprintf(stderr, "%s::addVariableToSymbolTable() (default) headed up\n", getTypeString()); - return parent->addVariableToSymbolTable(vd); // default, defer to parent - } - - virtual void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - parent->addTypedefToTypedefTable(tdd); // default, defer to parent - } - void walk_parents() { fprintf(stderr, "wp: (%s) ", getTypeString()); print(); diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index 637df4b..5c9010c 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -63,7 +63,6 @@ public: chillAST_RecordDecl *getStructDef(); - bool isAStruct() { return isStruct; }; bool isAUnion() { return isUnion; }; @@ -86,7 +85,6 @@ public: chillAST_VarDecl *findSubpart(const char *name); - //TODO hide data, set/get type and alias chillAST_TypedefDecl(); chillAST_TypedefDecl(char *t, const char *nt); @@ -143,6 +141,7 @@ public: fprintf(stderr, "byref %d\n", tf); }; + bool nameis(const char *n) { return !strcmp(n, varname); }; bool isABuiltin; // if variable is builtin, we don't need to declare it void *uniquePtr; // DO NOT REFERENCE THROUGH THIS! just used to differentiate declarations bool isArray() { return (numdimensions != 0); }; @@ -296,38 +295,6 @@ class chillAST_CompoundStmt : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_COMPOUNDSTMT;} // variables that are special for this type of node - chillAST_SymbolTable *symbol_table; // symbols defined inside this compound statement - chillAST_TypedefTable *typedef_table; - - bool hasSymbolTable() { return true; }; - - bool hasTypeDefTable() { return true; }; - - chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "chillAST_CompoundStmt::findDatatype( %s )\n", t); - if (typedef_table) { - for (int i = 0; i < typedef_table->size(); i++) { - chillAST_TypedefDecl *tdd = (*typedef_table)[i]; - if (tdd->nameis(t)) return tdd; - } - } - if (parent) return parent->findDatatype(t); - return NULL; // should not happen - } - - chillAST_SymbolTable *getSymbolTable() { return symbol_table; } - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_CompoundStmt method - //fprintf(stderr, "\nchillAST_CompoundStmt addVariableToSymbolTable( %s )\n", vd->varname); - symbol_table = addSymbolToTable(symbol_table, vd); - //printSymbolTable( symbol_table ); - return symbol_table; - } - - void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - typedef_table = addTypedefToTable(typedef_table, tdd); - } - // constructors chillAST_CompoundStmt(); // never has any args ??? @@ -430,21 +397,11 @@ public: char *returnType; char *functionName; - // parameters - int numParameters() { return parameters.size(); }; - chillAST_SymbolTable parameters; - - // this is probably a mistake, but symbol_table here is pointing to BODY'S symbol table - //chillAST_SymbolTable *symbol_table; // symbols defined inside this function. REALLY the body's symbol table? + //! parameters + int numParameters() { return symbolTable->size(); }; + // chillAST_TypedefTable *typedef_table; // TODO typedef here doesn't make sense - chillAST_TypedefTable *typedef_table; // function typedef table - - - bool hasSymbolTable() { return true; }; // COULD HAVE - bool hasTypeDefTable() { return true; }; // COULD HAVE - - - //char *parametertypes; // a single string?? + //char *parametertypes; // a single string?? void printParameterTypes(FILE *fp); void setName(char *n) { functionName = strdup(n); /* probable memory leak */ }; @@ -468,25 +425,12 @@ public: void *uniquePtr; // DO NOT REFERENCE THROUGH THIS! USED AS A UNIQUE ID - - - - chillAST_FunctionDecl(); // { asttype = CHILLAST_NODE_FUNCTIONDECL; numparameters = 0;}; - chillAST_FunctionDecl(const char *rt, const char *fname); - chillAST_FunctionDecl(const char *rt, const char *fname, void *unique); void addParameter(chillAST_VarDecl *p); - chillAST_VarDecl *hasParameterNamed(const char *name); - - chillAST_VarDecl *findParameterNamed(const char *name) { return hasParameterNamed(name); }; - void addDecl(chillAST_VarDecl *vd); // just adds to symbol table?? TODO - chillAST_VarDecl *funcHasVariableNamed(const char *name); // functiondecl::hasVariableNamed - //chillAST_VarDecl *findVariableNamed( const char *name ) { return hasVariableNamed( name ); }; - void addChild(chillAST_Node *node); // special because inserts into BODY void insertChild(int i, chillAST_Node *node); // special because inserts into BODY @@ -520,53 +464,7 @@ public: chillAST_Node *constantFold(); - chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "%s looking for datatype %s\n", getTypeString(), t); - if (!typedef_table) { // not here - if (parent) return parent->findDatatype(t); // not here, check parents - else return NULL; // not defined here and no parent - } - - //fprintf(stderr, "%d typedefs\n", typedef_table->size()); - for (int i = 0; i < typedef_table->size(); i++) { - chillAST_TypedefDecl *tdd = (*typedef_table)[i]; - if (tdd->nameis(t)) return tdd; - } - if (parent) return parent->findDatatype(t); - return NULL; // should not happen - } - - chillAST_SymbolTable *getParameterSymbolTable() { return ¶meters; } - - chillAST_SymbolTable *getSymbolTable() { return body->getSymbolTable(); } //symbol_table; } // - void setSymbolTable(chillAST_SymbolTable *tab) { - // no longer keeping a local ?? symbol_table = tab; - if (!body) { // can never happen now - body = new chillAST_CompoundStmt(); - } // only if func is empty! - body->symbol_table = tab; - } - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_FunctionDecl method - //fprintf(stderr, "\nchillAST_FunctionDecl addVariableToSymbolTable( %s )\n", vd->varname); - - // this is all dealing with the body's symbol table - // the function has a symbol table called "parameters" but that is a special case - - addSymbolToTable(getSymbolTable(), vd); - if (!vd->parent) { - //fprintf(stderr, "setting parent of vardecl to be the function whose symbol table it is going into\n"); // ?? - vd->setParent(this); - insertChild(0, vd); - } - //printSymbolTable( getSymbolTable() ); - return getSymbolTable(); - } - - - void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - typedef_table = addTypedefToTable(typedef_table, tdd); - } + chillAST_SymbolTable *getParameterTable() { return getSymbolTable(); } void replaceChild(chillAST_Node *old, chillAST_Node *newchild) { body->replaceChild(old, newchild); @@ -596,42 +494,6 @@ public: } // get, set filename ? - chillAST_SymbolTable *global_symbol_table; // (global) symbols defined inside this source file - chillAST_TypedefTable *global_typedef_table; // source file - chillAST_VarDecl *findVariableNamed(const char *name); // looks in global_symbol_table; - - bool hasSymbolTable() { return true; }; // "has" vs "can have" TODO - bool hasTypeDefTable() { return true; }; - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_SourceFile method - fprintf(stderr, "\nchillAST_SourceFile addVariableToSymbolTable( %s )\n", vd->varname); - global_symbol_table = addSymbolToTable(global_symbol_table, vd); - //addChild( vd ); // ?? - //printSymbolTable( global_symbol_table ); - return global_symbol_table; - } - - void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - //fprintf(stderr, "SOURCEFILE adding typedef %s to typedeftable\n", tdd->getStructName()); - global_typedef_table = addTypedefToTable(global_typedef_table, tdd); - //fprintf(stderr, "now global typedef table has %d entries\n", global_typedef_table->size()); - } - - chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "%s looking for datatype %s\n", getTypeString(), t); - fprintf(stderr, "%d global typedefs\n", global_typedef_table->size()); - for (int i = 0; i < global_typedef_table->size(); i++) { - - chillAST_TypedefDecl *tdd = (*global_typedef_table)[i]; - //fprintf(stderr, "comparing to %s\n", tdd->getStructName()); - if (tdd->nameis(t)) { - //fprintf(stderr, "found it\n"); - return (chillAST_Node *) tdd; - } - } - return NULL; - } - std::vector<chillAST_FunctionDecl *> functions; // at top level, or anywhere? std::vector<chillAST_MacroDefinition *> macrodefinitions; @@ -666,7 +528,6 @@ public: class chillAST_MacroDefinition : public chillAST_Node { private: chillAST_Node *body; // rhs always a compound statement? - chillAST_SymbolTable *symbol_table; public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MACRODEFINITION;} char *macroName; @@ -684,11 +545,6 @@ public: chillAST_MacroDefinition(const char *name, const char *rhs); - void addParameter(chillAST_VarDecl *p); // parameters have no TYPE ?? - chillAST_VarDecl *hasParameterNamed(const char *name); - - chillAST_VarDecl *findParameterNamed(const char *name) { return hasParameterNamed(name); }; - void addChild(chillAST_Node *node); // special because inserts into BODY void insertChild(int i, chillAST_Node *node); // special because inserts into BODY @@ -699,19 +555,6 @@ public: void print(int indent = 0, FILE *fp = stderr); // in chill_ast.cc void dump(int indent = 0, FILE *fp = stderr); // in chill_ast.cc - bool hasSymbolTable() { return true; }; - - //const std::vector<chillAST_VarDecl *> getSymbolTable() { return symbol_table; } - chillAST_SymbolTable *getSymbolTable() { return symbol_table; } - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_MacroDefinition method ?? - //fprintf(stderr, "\nchillAST_MacroDefinition addVariableToSymbolTable( %s )\n", vd->varname); - symbol_table = addSymbolToTable(symbol_table, vd); - //printSymbolTable( symbol_table ); - return symbol_table; - } - - chillAST_Node *clone(); // none of these make sense for macros @@ -746,7 +589,6 @@ public: chillAST_Node *body; // always a compoundstmt? IR_CONDITION_TYPE conditionoperator; // from ir_code.hh - chillAST_SymbolTable *symbol_table; // symbols defined inside this forstmt (in init but not body?) body is compound stmt bool hasSymbolTable() { return true; }; // constructors @@ -846,13 +688,6 @@ public: void loseLoopWithLoopVar(char *var); // chillAST_ForStmt void replaceChild(chillAST_Node *old, chillAST_Node *newchild); - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_ForStmt method - //fprintf(stderr, "\nchillAST_ForStmt addVariableToSymbolTable( %s )\n", vd->varname); - symbol_table = addSymbolToTable(symbol_table, vd); - //printSymbolTable( symbol_table ); - return symbol_table; - } - void gatherStatements(std::vector<chillAST_Node *> &statements); bool lowerBound(int &l); |