absyntax_utils/search_varfb_instance_type.cc
changeset 371 926490780952
parent 367 6d94128ba5ad
child 372 25332e048742
equal deleted inserted replaced
370:f3cec8694f65 371:926490780952
    92 
    92 
    93 
    93 
    94 search_varfb_instance_type_c::search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) {
    94 search_varfb_instance_type_c::search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) {
    95   this->decompose_var_instance_name = NULL;
    95   this->decompose_var_instance_name = NULL;
    96   this->current_structelement_name = NULL;
    96   this->current_structelement_name = NULL;
    97   this->current_rawtype = NULL;
    97   this->current_typeid = NULL;
    98 }
    98 }
    99 
    99 
   100 symbol_c *search_varfb_instance_type_c::get_basetype_decl(symbol_c *variable_name) {
   100 symbol_c *search_varfb_instance_type_c::get_basetype_decl(symbol_c *variable_name) {
   101   this->current_structelement_name = NULL;
   101   this->current_structelement_name = NULL;
   102   this->current_rawtype = NULL;
   102   this->current_typeid = NULL;
   103   this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
   103   this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
   104   if (NULL == decompose_var_instance_name) ERROR;
   104   if (NULL == decompose_var_instance_name) ERROR;
   105 
   105 
   106   /* find the part of the variable name that will appear in the
   106   /* find the part of the variable name that will appear in the
   107    * variable declaration, for e.g., in window.point.x, this would be
   107    * variable declaration, for e.g., in window.point.x, this would be
   144   return res;
   144   return res;
   145 }
   145 }
   146 
   146 
   147 unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) {
   147 unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) {
   148   this->current_structelement_name = NULL;
   148   this->current_structelement_name = NULL;
   149   this->current_rawtype = NULL;
   149   this->current_typeid = NULL;
   150   this->is_complex = false;
   150   this->is_complex = false;
   151   this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
   151   this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
   152   if (NULL == decompose_var_instance_name) ERROR;
   152   if (NULL == decompose_var_instance_name) ERROR;
   153 
   153 
   154   /* find the part of the variable name that will appear in the
   154   /* find the part of the variable name that will appear in the
   180 
   180 
   181   return res;
   181   return res;
   182 }
   182 }
   183 
   183 
   184 symbol_c *search_varfb_instance_type_c::get_type_id(symbol_c *variable_name) {
   184 symbol_c *search_varfb_instance_type_c::get_type_id(symbol_c *variable_name) {
   185   this->current_rawtype = NULL;
   185   this->current_typeid = NULL;
   186   symbol_c *rawtype = this->get_basetype_decl(variable_name);
   186   symbol_c *vartype = this->get_basetype_decl(variable_name);
   187   if (this->current_rawtype != NULL)
   187   if (this->current_typeid != NULL)
   188     return this->current_rawtype;
   188     return this->current_typeid;
   189   else
   189   else
   190 	return rawtype;
   190     return vartype;
   191 }
   191 }
   192 
   192 
   193 bool search_varfb_instance_type_c::type_is_complex(void) {
   193 bool search_varfb_instance_type_c::type_is_complex(void) {
   194   return this->is_complex;
   194   return this->is_complex;
   195 }
   195 }
   217  * This is so because the base class does not consider a function block
   217  * This is so because the base class does not consider a function block
   218  * to be a type, unlike this class that allows a variable instance
   218  * to be a type, unlike this class that allows a variable instance
   219  * of a function block type...
   219  * of a function block type...
   220  */
   220  */
   221 void *search_varfb_instance_type_c::visit(identifier_c *type_name) {
   221 void *search_varfb_instance_type_c::visit(identifier_c *type_name) {
       
   222   this->current_typeid = type_name;
       
   223 
   222   /* look up the type declaration... */
   224   /* look up the type declaration... */
   223   symbol_c *fb_decl = function_block_type_symtable.find_value(type_name);
   225   symbol_c *fb_decl = function_block_type_symtable.find_value(type_name);
   224   if (fb_decl != function_block_type_symtable.end_value())
   226   if (fb_decl != function_block_type_symtable.end_value())
   225     /* Type declaration found!! */
   227     /* Type declaration found!! */
   226     return fb_decl->accept(*this);
   228     return fb_decl->accept(*this);
   227 
   229 
   228   this->current_rawtype = type_name;
       
   229 
       
   230   /* No. It is not a function block, so we let
   230   /* No. It is not a function block, so we let
   231    * the base class take care of it...
   231    * the base class take care of it...
   232    */
   232    */
   233   if (NULL == decompose_var_instance_name->next_part(false)) {
   233   return search_base_type_c::visit(type_name);
   234     return base_type(type_name);
       
   235   }
       
   236   else {
       
   237 	return search_base_type_c::visit(type_name);
       
   238   }
       
   239 }
   234 }
   240 
   235 
   241 /********************************/
   236 /********************************/
   242 /* B 1.3.3 - Derived data types */
   237 /* B 1.3.3 - Derived data types */
   243 /********************************/
   238 /********************************/
   260   this->is_complex = true;
   255   this->is_complex = true;
   261   return symbol->non_generic_type_name->accept(*this);
   256   return symbol->non_generic_type_name->accept(*this);
   262 }
   257 }
   263 
   258 
   264 /*  structure_type_name ':' structure_specification */
   259 /*  structure_type_name ':' structure_specification */
       
   260 /* NOTE: this is only used inside a TYPE ... END_TYPE declaration. 
       
   261  * It is never used directly when declaring a new variable! 
       
   262  */
   265 void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) {
   263 void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) {
   266   this->is_complex = true;
   264   this->is_complex = true;
       
   265 
       
   266   if (NULL == current_structelement_name) ERROR;
   267   return symbol->structure_specification->accept(*this);
   267   return symbol->structure_specification->accept(*this);
   268   /* NOTE: structure_specification will point to either a
   268   /* NOTE: structure_specification will point to either a
   269    *       initialized_structure_c
   269    *       initialized_structure_c
   270    *       OR A
   270    *       OR A
   271    *       structure_element_declaration_list_c
   271    *       structure_element_declaration_list_c
   273 }
   273 }
   274 
   274 
   275 /* structure_type_name ASSIGN structure_initialization */
   275 /* structure_type_name ASSIGN structure_initialization */
   276 /* structure_initialization may be NULL ! */
   276 /* structure_initialization may be NULL ! */
   277 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
   277 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
       
   278 /* NOTE: only the initialized structure is ever used when declaring a new variable instance */
   278 void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol)	{
   279 void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol)	{
   279   this->is_complex = true;
   280   this->is_complex = true;
   280   /* recursively find out the data type of var_name... */
   281   if (NULL != current_structelement_name) ERROR;
   281   return symbol->structure_type_name->accept(*this);
   282   
   282 }
       
   283 
       
   284 /* helper symbol for structure_declaration */
       
   285 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   286 /* structure_element_declaration_list structure_element_declaration ';' */
       
   287 void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol)	{
       
   288   /* make sure that we have decomposed all structure elements of the variable name */
       
   289   current_structelement_name = decompose_var_instance_name->next_part();
       
   290   /* now search the structure declaration */
       
   291   return visit_list(symbol);
       
   292 }
       
   293 
       
   294 /*  structure_element_name ':' spec_init */
       
   295 void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) {
       
   296   if (NULL == current_structelement_name) ERROR;
       
   297 
       
   298   if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0)
       
   299     return symbol->spec_init->accept(*this);
       
   300 
       
   301   return NULL;
       
   302 }
       
   303 
       
   304 /* helper symbol for structure_initialization */
       
   305 /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   306 /* structure_element_initialization_list ',' structure_element_initialization */
       
   307 void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   308 /*  structure_element_name ASSIGN value */
       
   309 void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   310 
       
   311 
       
   312 
       
   313 /**************************************/
       
   314 /* B.1.5 - Program organization units */
       
   315 /**************************************/
       
   316 /*****************************/
       
   317 /* B 1.5.2 - Function Blocks */
       
   318 /*****************************/
       
   319 /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
       
   320 // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
       
   321 void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) {
       
   322   /* make sure that we have decomposed all strcuture elements of the variable name */
   283   /* make sure that we have decomposed all strcuture elements of the variable name */
   323 
       
   324   symbol_c *var_name = decompose_var_instance_name->next_part();
   284   symbol_c *var_name = decompose_var_instance_name->next_part();
   325   if (NULL == var_name) {
   285   if (NULL == var_name) {
   326     /* this is it... !
   286     /* this is it... !
   327      * No need to look any further...
   287      * No need to look any further...
   328      * Note also that, unlike for the struct types, a function block may
   288      * Note also that, unlike for the struct types, a function block may
   331      * We simply return it.
   291      * We simply return it.
   332      */
   292      */
   333     return (void *)symbol;
   293     return (void *)symbol;
   334    }
   294    }
   335 
   295 
       
   296   /* look for the var_name in the structure declaration */
       
   297   current_structelement_name = var_name;
       
   298 
       
   299   /* recursively find out the data type of current_structelement_name... */
       
   300   return symbol->structure_type_name->accept(*this);
       
   301 }
       
   302 
       
   303 /* helper symbol for structure_declaration */
       
   304 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   305 /* structure_element_declaration_list structure_element_declaration ';' */
       
   306 void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol)	{
       
   307   if (NULL == current_structelement_name) ERROR;
       
   308   /* now search the structure declaration */
       
   309   return visit_list(symbol);
       
   310 }
       
   311 
       
   312 /*  structure_element_name ':' spec_init */
       
   313 void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) {
       
   314   if (NULL == current_structelement_name) ERROR;
       
   315 
       
   316   if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) {
       
   317     current_structelement_name = NULL;
       
   318     /* found the type of the element we were looking for! */
       
   319     return symbol->spec_init->accept(*this);
       
   320   }  
       
   321 
       
   322   /* Did not find the type of the element we were looking for! */
       
   323   /* Will keep looking... */
       
   324   return NULL;
       
   325 }
       
   326 
       
   327 /* helper symbol for structure_initialization */
       
   328 /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   329 /* structure_element_initialization_list ',' structure_element_initialization */
       
   330 void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   331 /*  structure_element_name ASSIGN value */
       
   332 void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   333 
       
   334 
       
   335 
       
   336 /**************************************/
       
   337 /* B.1.5 - Program organization units */
       
   338 /**************************************/
       
   339 /*****************************/
       
   340 /* B 1.5.2 - Function Blocks */
       
   341 /*****************************/
       
   342 /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
       
   343 // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
       
   344 void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) {
       
   345   /* make sure that we have decomposed all strcuture elements of the variable name */
       
   346   symbol_c *var_name = decompose_var_instance_name->next_part();
       
   347   if (NULL == var_name) {
       
   348     /* this is it... !
       
   349      * No need to look any further...
       
   350      * Note also that, unlike for the struct types, a function block may
       
   351      * not be defined based on another (i.e. no inheritance is allowed),
       
   352      * so this function block is already the most base type.
       
   353      * We simply return it.
       
   354      */
       
   355     return (void *)symbol;
       
   356    }
       
   357 
   336    /* now search the function block declaration for the variable... */
   358    /* now search the function block declaration for the variable... */
   337    search_var_instance_decl_c search_decl(symbol);
   359    search_var_instance_decl_c search_decl(symbol);
   338    symbol_c *var_decl = search_decl.get_decl(var_name);
   360    symbol_c *var_decl = search_decl.get_decl(var_name);
   339    if (NULL == var_decl) {
   361    if (NULL == var_decl) {
   340      /* variable instance declaration not found! */
   362      /* variable instance declaration not found! */
   341      return NULL;
   363      return NULL;
   342    }
   364    }
   343 
   365 #if 0
   344    /* We have found the declaration.
   366    /* We have found the declaration.
   345     * Should we look any further?
   367     * Should we look any further?
   346     */
   368     */
   347    var_name = decompose_var_instance_name->next_part();
   369    var_name = decompose_var_instance_name->next_part();
   348    if (NULL == var_name) {
   370    if (NULL == var_name) {
   351    }
   373    }
   352 
   374 
   353   current_structelement_name = var_name;
   375   current_structelement_name = var_name;
   354   /* recursively find out the data type of var_name... */
   376   /* recursively find out the data type of var_name... */
   355   return symbol->var_declarations->accept(*this);
   377   return symbol->var_declarations->accept(*this);
   356 }
   378 #endif  
       
   379   /* carry on recursively, in case the variable has more elements to be decomposed... */
       
   380   return var_decl->accept(*this);
       
   381 }