stage4/generate_cc/search_fb_instance_decl.cc
changeset 70 e1f0ebd2d9ec
parent 69 41cb5b80416e
child 71 c2c867171c07
equal deleted inserted replaced
69:41cb5b80416e 70:e1f0ebd2d9ec
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /* Returns the function block type declaration
       
    27  * of a specific function block instance.
       
    28  */
       
    29 
       
    30 
       
    31 
       
    32 /* Returns the type name of a specific function block
       
    33  * instance. This class will search the variable
       
    34  * declarations inside the scope given to it
       
    35  * searching for the declaration of the function
       
    36  * block instance.
       
    37  *
       
    38  * The class constructor must be given the search scope
       
    39  * (function, function block or program within which
       
    40  * the function block instance was declared).
       
    41  *
       
    42  * This class will search the tree from the root given to the
       
    43  * constructor. Another option would be to build a symbol table,
       
    44  * and search that instead. Building the symbol table would be done
       
    45  * while visiting the variable declaration objects in the parse
       
    46  * tree. Unfortuantely, generate_cc_c does not visit these
       
    47  * objects, delegating it to another class. This means that
       
    48  * we would need another specialised class just to build the
       
    49  * symbol table. We might just as well have a specialised class
       
    50  * that searches the tree itself for the relevant info. This
       
    51  * class is exactly that...!
       
    52  */
       
    53 class search_fb_instance_decl_c: public search_visitor_c {
       
    54 
       
    55   private:
       
    56     symbol_c *search_scope;
       
    57 
       
    58     symbol_c *search_name;
       
    59     symbol_c *current_fb_type_name;
       
    60 
       
    61   public:
       
    62     search_fb_instance_decl_c(symbol_c *search_scope) {
       
    63       this->search_scope = search_scope;
       
    64       this->current_fb_type_name = NULL;
       
    65     }
       
    66 
       
    67     symbol_c *get_type_name(symbol_c *fb_instance_name) {
       
    68       this->search_name = fb_instance_name;
       
    69       return (symbol_c *)search_scope->accept(*this);
       
    70     }
       
    71 
       
    72   public:
       
    73 /***************************/
       
    74 /* B 0 - Programming Model */
       
    75 /***************************/
       
    76     void *visit(library_c *symbol) {
       
    77       /* we do not want to search multiple declaration scopes,
       
    78        * so we do not visit all the functions, fucntion blocks, etc...
       
    79        */
       
    80       return NULL;
       
    81     }
       
    82 
       
    83 /******************************************/
       
    84 /* B 1.4.3 - Declaration & Initialisation */
       
    85 /******************************************/
       
    86 
       
    87 /* name_list ':' function_block_type_name ASSIGN structure_initialization */
       
    88 /* structure_initialization -> may be NULL ! */
       
    89     void *visit(fb_name_decl_c *symbol) {
       
    90       current_fb_type_name = symbol->function_block_type_name;
       
    91       return symbol->fb_name_list->accept(*this);
       
    92     }
       
    93 
       
    94 /* name_list ',' fb_name */
       
    95     void *visit(fb_name_list_c *symbol) {
       
    96       list_c *list = symbol;
       
    97       for(int i = 0; i < list->n; i++) {
       
    98         if (compare_identifiers(list->elements[i], search_name) == 0)
       
    99 	  /* by now, current_fb_declaration should be != NULL */
       
   100           return current_fb_type_name;
       
   101       }
       
   102       return NULL;
       
   103     }
       
   104 
       
   105 /**************************************/
       
   106 /* B.1.5 - Program organization units */
       
   107 /**************************************/
       
   108 /***********************/
       
   109 /* B 1.5.1 - Functions */
       
   110 /***********************/
       
   111     void *visit(function_declaration_c *symbol) {
       
   112       /* no need to search through all the body, so we only
       
   113        * visit the variable declarations...!
       
   114        */
       
   115       return symbol->var_declarations_list->accept(*this);
       
   116     }
       
   117 
       
   118 /*****************************/
       
   119 /* B 1.5.2 - Function Blocks */
       
   120 /*****************************/
       
   121     void *visit(function_block_declaration_c *symbol) {
       
   122       /* no need to search through all the body, so we only
       
   123        * visit the variable declarations...!
       
   124        */
       
   125       return symbol->var_declarations->accept(*this);
       
   126     }
       
   127 
       
   128 /**********************/
       
   129 /* B 1.5.3 - Programs */
       
   130 /**********************/
       
   131     void *visit(program_declaration_c *symbol) {
       
   132       /* no need to search through all the body, so we only
       
   133        * visit the variable declarations...!
       
   134        */
       
   135       return symbol->var_declarations->accept(*this);
       
   136     }
       
   137 };
       
   138 
       
   139 
       
   140 
       
   141