absyntax_utils/absyntax_utils.cc
changeset 342 8fb0fbfcc71b
parent 338 3037bb7e8a82
child 350 2c3c4dc34979
equal deleted inserted replaced
337:9a2262fb7994 342:8fb0fbfcc71b
   115 symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable;
   115 symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable;
   116 
   116 
   117 /* A symbol table with all user declared type definitions... */
   117 /* A symbol table with all user declared type definitions... */
   118 /* Note that function block types and program types have their
   118 /* Note that function block types and program types have their
   119  * own symbol tables, so do not get placed in this symbol table!
   119  * own symbol tables, so do not get placed in this symbol table!
       
   120  *
       
   121  * The symbol_c * associated to the value will point to the data type declaration.
   120  */
   122  */
   121 symbol_c null_symbol4;
   123 symbol_c null_symbol4;
   122 symtable_c<symbol_c *, &null_symbol4> type_symtable;
   124 symtable_c<symbol_c *, &null_symbol4> type_symtable;
   123 
   125 
   124 /* A symbol table with all values declared for enumerated type... */
   126 /* A symbol table with all values declared for enumerated type... */
   125 /* Note that if the value is defined multiple times the value
   127 /* Notes:
       
   128  * - if the value is defined multiple times the value
   126  * is the null pointer.
   129  * is the null pointer.
   127  */
   130  *
   128 symtable_c<symbol_c *, &null_symbol4> enumerated_value_symtable;
   131  * - The stored symbol_c * associated to the value points to the enumerated_type_name
       
   132  * (i.e. the name of the enumerated data type) in which the the value/identifier
       
   133  * is used/declared.
       
   134  *
       
   135  * - We could re-use the null_symbol4 object, but it is safer to use a distinct object
       
   136  *   (i.e. it might make it easier to find strange bugs).
       
   137  */
       
   138 symbol_c null_symbol5;
       
   139 symtable_c<symbol_c *, &null_symbol5> enumerated_value_symtable;
   129 
   140 
   130 
   141 
   131 /***********************************************************************/
   142 /***********************************************************************/
   132 /***********************************************************************/
   143 /***********************************************************************/
   133 /***********************************************************************/
   144 /***********************************************************************/
   212   void *visit(enumerated_value_c *symbol) {
   223   void *visit(enumerated_value_c *symbol) {
   213     if (current_enumerated_type != NULL) {
   224     if (current_enumerated_type != NULL) {
   214       if (symbol->type != NULL) ERROR;
   225       if (symbol->type != NULL) ERROR;
   215 
   226 
   216       symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value);
   227       symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value);
   217       if (value_type == current_enumerated_type) ERROR;
   228       /* NOTE: The following condition checks whether the same identifier is used more than once
       
   229        *       when defining the enumerated values of the type declaration of the new enumerated type.
       
   230        *       If this occurs, then the program beeing compiled contains a semantic error, which
       
   231        *       must be caught and reported by the semantic analyser. However, since
       
   232        *       this code is run before the semantic analyser, we must not yet raise the ERROR (internal
       
   233        *       compiler error message).
       
   234        *       For this reason, the follosing check is commented out.
       
   235        */
       
   236       /* if (value_type == current_enumerated_type) ERROR; */
   218 
   237 
   219       if (value_type == enumerated_value_symtable.end_value())
   238       if (value_type == enumerated_value_symtable.end_value())
       
   239 	/* This identifier has not yet been used in any previous declaration of an enumeration data type.
       
   240 	 * so we add it to the symbol table.
       
   241 	 */
   220         enumerated_value_symtable.insert(symbol->value, current_enumerated_type);
   242         enumerated_value_symtable.insert(symbol->value, current_enumerated_type);
   221       else if (value_type != NULL)
   243       else if (value_type != NULL)
       
   244 	/* This identifier has already been used in a previous declaration of an enumeration data type.
       
   245 	 * so we set the symbol in symbol table pointing to NULL.
       
   246 	 */
   222         enumerated_value_symtable.set(symbol->value, NULL);
   247         enumerated_value_symtable.set(symbol->value, NULL);
   223     }
   248     }
   224     return NULL;
   249     return NULL;
   225   }
   250   }
   226 
   251