absyntax_utils/get_datatype_info.cc
changeset 938 31e3b3f2eff1
parent 929 469de0f54b0e
child 939 5074236fb3c4
equal deleted inserted replaced
937:887e7d90445a 938:31e3b3f2eff1
    60 /*****                                                *****/
    60 /*****                                                *****/
    61 /**********************************************************/
    61 /**********************************************************/
    62 /**********************************************************/
    62 /**********************************************************/
    63 /**********************************************************/
    63 /**********************************************************/
    64 
    64 
       
    65 
       
    66 /****************************************************************************************************/
       
    67 /****************************************************************************************************/
    65 /* Return the identifier (name) of a datatype, typically declared in a TYPE .. END_TYPE declaration */
    68 /* Return the identifier (name) of a datatype, typically declared in a TYPE .. END_TYPE declaration */
       
    69 /****************************************************************************************************/
       
    70 /****************************************************************************************************/
    66 class get_datatype_id_c: null_visitor_c {
    71 class get_datatype_id_c: null_visitor_c {
    67   private:
    72   private:
    68     static get_datatype_id_c *singleton;
    73     static get_datatype_id_c *singleton;
    69     
    74     
    70   public:
    75   public:
   155 get_datatype_id_c *get_datatype_id_c::singleton = NULL;
   160 get_datatype_id_c *get_datatype_id_c::singleton = NULL;
   156 
   161 
   157 
   162 
   158 
   163 
   159 
   164 
   160 /***********************************************************************/
   165 
   161 /***********************************************************************/
   166 
   162 
   167 /**************************************************/
       
   168 /**************************************************/
       
   169 /* transform elementary data type class to string */
       
   170 /**************************************************/
       
   171 /**************************************************/
   163 
   172 
   164 /* A small helper class, to transform elementary data type to string.
   173 /* A small helper class, to transform elementary data type to string.
   165  * this allows us to generate more relevant error messages...
   174  * this allows us to generate more relevant error messages...
   166  */
   175  */
   167 
   176 
   271 
   280 
   272 get_datatype_id_str_c *get_datatype_id_str_c::singleton = NULL;
   281 get_datatype_id_str_c *get_datatype_id_str_c::singleton = NULL;
   273 
   282 
   274 
   283 
   275 
   284 
       
   285 /*********************************************************/
       
   286 /*********************************************************/
       
   287 /* get the datatype of a field inside a struct data type */
       
   288 /*********************************************************/
       
   289 /*********************************************************/
       
   290 
       
   291 class get_struct_info_c : null_visitor_c {
       
   292   private:
       
   293     symbol_c *current_field;
       
   294     /* singleton class! */
       
   295     static get_struct_info_c *singleton;
       
   296 
       
   297   public:
       
   298     get_struct_info_c(void) {current_field = NULL;}
       
   299 
       
   300     static symbol_c *get_field_type_id(symbol_c *struct_type, symbol_c *field_name) {
       
   301       if (NULL == singleton)    singleton = new get_struct_info_c;
       
   302       if (NULL == singleton)    ERROR;
       
   303       singleton->current_field = field_name;
       
   304       return (symbol_c *)struct_type->accept(*singleton);
       
   305     }
       
   306 
       
   307 
       
   308   private:
       
   309     /*************************/
       
   310     /* B.1 - Common elements */
       
   311     /*************************/
       
   312     /********************************/
       
   313     /* B 1.3.3 - Derived data types */
       
   314     /********************************/
       
   315     /*  structure_type_name ':' structure_specification */
       
   316     /* NOTE: this is only used inside a TYPE ... END_TYPE declaration.  It is never used directly when declaring a new variable! */
       
   317     /* NOTE: structure_specification will point to either initialized_structure_c  OR  structure_element_declaration_list_c */
       
   318     void *visit(structure_type_declaration_c *symbol) {return symbol->structure_specification->accept(*this);}
       
   319 
       
   320     /* structure_type_name ASSIGN structure_initialization */
       
   321     /* structure_initialization may be NULL ! */
       
   322     // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
       
   323     /* NOTE: only the initialized structure is never used when declaring a new variable instance */
       
   324     void *visit(initialized_structure_c *symbol) {return symbol->structure_type_name->accept(*this);}
       
   325 
       
   326     /* helper symbol for structure_declaration */
       
   327     /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   328     /* structure_element_declaration_list structure_element_declaration ';' */
       
   329     void *visit(structure_element_declaration_list_c *symbol) {
       
   330       /* now search the structure declaration */
       
   331       for(int i = 0; i < symbol->n; i++) {
       
   332         void *tmp = symbol->elements[i]->accept(*this);
       
   333         if (NULL != tmp) return tmp;
       
   334       }
       
   335       return NULL; // not found!!
       
   336     }  
       
   337 
       
   338     /*  structure_element_name ':' spec_init */
       
   339     void *visit(structure_element_declaration_c *symbol) {
       
   340       if (compare_identifiers(symbol->structure_element_name, current_field) == 0)
       
   341         return symbol->spec_init; /* found the type of the element we were looking for! */
       
   342       return NULL;   /* not the element we are looking for! */
       
   343     }
       
   344       
       
   345 
       
   346     /* helper symbol for structure_initialization */
       
   347     /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   348     /* structure_element_initialization_list ',' structure_element_initialization */
       
   349     void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   350     /*  structure_element_name ASSIGN value */
       
   351     void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   352 
       
   353 
       
   354     /**************************************/
       
   355     /* B.1.5 - Program organization units */
       
   356     /**************************************/
       
   357     /*****************************/
       
   358     /* B 1.5.2 - Function Blocks */
       
   359     /*****************************/
       
   360     /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
       
   361     // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
       
   362     void *visit(function_block_declaration_c *symbol) {
       
   363       /* now search the function block declaration for the variable... */
       
   364       search_var_instance_decl_c search_decl(symbol);
       
   365       return search_decl.get_decl(current_field);
       
   366     }
       
   367       
       
   368     /*********************************************/
       
   369     /* B.1.6  Sequential function chart elements */
       
   370     /*********************************************/
       
   371     /* INITIAL_STEP step_name ':' action_association_list END_STEP */
       
   372     // SYM_REF2(initial_step_c, step_name, action_association_list)
       
   373     void *visit(initial_step_c *symbol) {
       
   374       identifier_c T("T");  identifier_c X("X");
       
   375       /* Hard code the datatypes of the implicit variables Stepname.X and Stepname.T */
       
   376       if (compare_identifiers(&T, current_field) == 0)  return &get_datatype_info_c::time_type_name;
       
   377       if (compare_identifiers(&X, current_field) == 0)  return &get_datatype_info_c::bool_type_name;
       
   378       return NULL;
       
   379     }
       
   380       
       
   381     /* STEP step_name ':' action_association_list END_STEP */
       
   382     // SYM_REF2(step_c, step_name, action_association_list)
       
   383     /* The code here should be identicial to the code in the visit(initial_step_c *) visitor! So we simply call the other visitor! */
       
   384     void *visit(step_c *symbol) {initial_step_c initial_step(NULL, NULL); return initial_step.accept(*this);}
       
   385       
       
   386 }; // get_struct_info_c
       
   387 
       
   388 get_struct_info_c *get_struct_info_c::singleton = NULL;
       
   389 
       
   390 
       
   391 
       
   392 
       
   393 
   276 /**********************************************************/
   394 /**********************************************************/
   277 /**********************************************************/
   395 /**********************************************************/
   278 /**********************************************************/
   396 /**********************************************************/
   279 /*****                                                *****/
   397 /*****                                                *****/
   280 /*****                                                *****/
   398 /*****                                                *****/
   292 
   410 
   293 symbol_c *get_datatype_info_c::get_id(symbol_c *datatype) {
   411 symbol_c *get_datatype_info_c::get_id(symbol_c *datatype) {
   294   return get_datatype_id_c::get_id(datatype);
   412   return get_datatype_id_c::get_id(datatype);
   295 }
   413 }
   296 
   414 
       
   415 
       
   416 symbol_c *get_datatype_info_c::get_struct_field_type_id(symbol_c *struct_datatype, symbol_c *struct_fieldname) {
       
   417   return get_struct_info_c::get_field_type_id(struct_datatype, struct_fieldname);
       
   418 }
   297 
   419 
   298 /* Returns true if both datatypes are equivalent (not necessarily equal!).
   420 /* Returns true if both datatypes are equivalent (not necessarily equal!).
   299  * WARNING: May return true even though the datatypes are not the same/identicial!!!
   421  * WARNING: May return true even though the datatypes are not the same/identicial!!!
   300  *          This occurs when at least one of the datatypes is of a generic
   422  *          This occurs when at least one of the datatypes is of a generic
   301  *          datatype (or a REF_TO a generic datatype). 
   423  *          datatype (or a REF_TO a generic datatype).