stage4/generate_cc/search_base_type.cc
changeset 0 fb772792efd1
child 32 289256ec66f1
equal deleted inserted replaced
-1:000000000000 0:fb772792efd1
       
     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 /* Determine the data type on which another data type is based on.
       
    27  * If a new default initial value is given, we DO NOT consider it a
       
    28  * new base class, and continue looking further!
       
    29  *
       
    30  * E.g. TYPE new_int_t : INT; END_TYPE;
       
    31  *      TYPE new_int2_t : INT = 2; END_TYPE;
       
    32  *      TYPE new_subr_t : INT (4..5); END_TYPE;
       
    33  *
       
    34  *    new_int_t is really an INT!!
       
    35  *    new_int2_t is also really an INT!!
       
    36  *    new_subr_t is also really an INT!!
       
    37  */
       
    38 class search_base_type_c: public null_visitor_c {
       
    39   private:
       
    40     symbol_c *current_type_name;
       
    41 
       
    42   public:
       
    43     search_base_type_c(void) {current_type_name = NULL;}
       
    44 
       
    45   public:
       
    46     void *visit(identifier_c *type_name) {
       
    47       this->current_type_name = type_name;
       
    48       /* look up the type declaration... */
       
    49       symbol_c *type_decl = type_symtable.find_value(type_name);
       
    50       if (type_decl == type_symtable.end_value())
       
    51         /* Type declaration not found!! */
       
    52 	ERROR;
       
    53 
       
    54       return type_decl->accept(*this);
       
    55     }
       
    56 
       
    57 /***********************************/
       
    58 /* B 1.3.1 - Elementary Data Types */
       
    59 /***********************************/
       
    60     void *visit(time_type_name_c *symbol)	{return (void *)symbol;}
       
    61     void *visit(bool_type_name_c *symbol)	{return (void *)symbol;}
       
    62     void *visit(sint_type_name_c *symbol)	{return (void *)symbol;}
       
    63     void *visit(int_type_name_c *symbol)	{return (void *)symbol;}
       
    64     void *visit(dint_type_name_c *symbol)	{return (void *)symbol;}
       
    65     void *visit(lint_type_name_c *symbol)	{return (void *)symbol;}
       
    66     void *visit(usint_type_name_c *symbol)	{return (void *)symbol;}
       
    67     void *visit(uint_type_name_c *symbol)	{return (void *)symbol;}
       
    68     void *visit(udint_type_name_c *symbol)	{return (void *)symbol;}
       
    69     void *visit(ulint_type_name_c *symbol)	{return (void *)symbol;}
       
    70     void *visit(real_type_name_c *symbol)	{return (void *)symbol;}
       
    71     void *visit(lreal_type_name_c *symbol)	{return (void *)symbol;}
       
    72     void *visit(date_type_name_c *symbol)	{return (void *)symbol;}
       
    73     void *visit(tod_type_name_c *symbol)	{return (void *)symbol;}
       
    74     void *visit(dt_type_name_c *symbol)		{return (void *)symbol;}
       
    75     void *visit(byte_type_name_c *symbol)	{return (void *)symbol;}
       
    76     void *visit(word_type_name_c *symbol)	{return (void *)symbol;}
       
    77     void *visit(dword_type_name_c *symbol)	{return (void *)symbol;}
       
    78     void *visit(lword_type_name_c *symbol)	{return (void *)symbol;}
       
    79     void *visit(string_type_name_c *symbol)	{return (void *)symbol;}
       
    80     void *visit(wstring_type_name_c *symbol)	{return (void *)symbol;}
       
    81 
       
    82 /********************************/
       
    83 /* B 1.3.3 - Derived data types */
       
    84 /********************************/
       
    85 /*  simple_type_name ':' simple_spec_init */
       
    86     void *visit(simple_type_declaration_c *symbol) {
       
    87       return symbol->simple_spec_init->accept(*this);
       
    88     }
       
    89 /* simple_specification ASSIGN constant */
       
    90     void *visit(simple_spec_init_c *symbol) {
       
    91       return symbol->simple_specification->accept(*this);
       
    92     }
       
    93 
       
    94 /*  subrange_type_name ':' subrange_spec_init */
       
    95     void *visit(subrange_type_declaration_c *symbol) {
       
    96       return symbol->subrange_spec_init->accept(*this);
       
    97     }
       
    98 
       
    99 /* subrange_specification ASSIGN signed_integer */
       
   100     void *visit(subrange_spec_init_c *symbol) {
       
   101       return symbol->subrange_specification->accept(*this);
       
   102     }
       
   103 
       
   104 /*  integer_type_name '(' subrange')' */
       
   105     void *visit(subrange_specification_c *symbol) {
       
   106       return symbol->integer_type_name->accept(*this);
       
   107     }
       
   108 
       
   109 /*  signed_integer DOTDOT signed_integer */
       
   110     void *visit(subrange_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   111 
       
   112 /*  enumerated_type_name ':' enumerated_spec_init */
       
   113     void *visit(enumerated_type_declaration_c *symbol) {
       
   114       this->current_type_name = symbol->enumerated_type_name;
       
   115       return symbol->enumerated_spec_init->accept(*this);
       
   116     }
       
   117 
       
   118 /* enumerated_specification ASSIGN enumerated_value */
       
   119     void *visit(enumerated_spec_init_c *symbol) {
       
   120       return symbol->enumerated_specification->accept(*this);
       
   121     }
       
   122 
       
   123 /* helper symbol for enumerated_specification->enumerated_spec_init */
       
   124 /* enumerated_value_list ',' enumerated_value */
       
   125     void *visit(enumerated_value_list_c *symbol) {
       
   126       if (NULL == this->current_type_name) ERROR;
       
   127       return (void *)this->current_type_name;
       
   128     }
       
   129 
       
   130 /* enumerated_type_name '#' identifier */
       
   131 // SYM_REF2(enumerated_value_c, type, value)
       
   132     void *visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   133 
       
   134 /*  identifier ':' array_spec_init */
       
   135     void *visit(array_type_declaration_c *symbol) {
       
   136       this->current_type_name = symbol->identifier;
       
   137       return symbol->array_spec_init->accept(*this);
       
   138     }
       
   139 
       
   140 /* array_specification [ASSIGN array_initialization} */
       
   141 /* array_initialization may be NULL ! */
       
   142     void *visit(array_spec_init_c *symbol) {
       
   143       return symbol->array_specification->accept(*this);
       
   144     }
       
   145 
       
   146 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
       
   147     void *visit(array_specification_c *symbol)	{
       
   148       if (NULL == this->current_type_name) ERROR;
       
   149       return (void *)this->current_type_name;
       
   150     }
       
   151 
       
   152 /* helper symbol for array_specification */
       
   153 /* array_subrange_list ',' subrange */
       
   154     void *visit(array_subrange_list_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   155 
       
   156 /* array_initialization:  '[' array_initial_elements_list ']' */
       
   157 /* helper symbol for array_initialization */
       
   158 /* array_initial_elements_list ',' array_initial_elements */
       
   159     void *visit(array_initial_elements_list_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   160 
       
   161 /* integer '(' [array_initial_element] ')' */
       
   162 /* array_initial_element may be NULL ! */
       
   163     void *visit(array_initial_elements_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   164 
       
   165 /*  structure_type_name ':' structure_specification */
       
   166       /* NOTE: structure_specification will point to either a
       
   167        *       initialized_structure_c
       
   168        *       OR A
       
   169        *       structure_element_declaration_list_c
       
   170        */
       
   171     void *visit(structure_type_declaration_c *symbol)  {
       
   172       this->current_type_name = symbol->structure_type_name;
       
   173       return symbol->structure_specification->accept(*this);
       
   174     }
       
   175 
       
   176 /* structure_type_name ASSIGN structure_initialization */
       
   177 /* structure_initialization may be NULL ! */
       
   178     void *visit(initialized_structure_c *symbol)	{
       
   179       return symbol->structure_type_name->accept(*this);
       
   180     }
       
   181 
       
   182 /* helper symbol for structure_declaration */
       
   183 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   184 /* structure_element_declaration_list structure_element_declaration ';' */
       
   185     void *visit(structure_element_declaration_list_c *symbol)	{
       
   186       if (NULL == this->current_type_name) ERROR;
       
   187       return (void *)this->current_type_name;
       
   188     }
       
   189 
       
   190 /*  structure_element_name ':' *_spec_init */
       
   191     void *visit(structure_element_declaration_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   192 
       
   193 /* helper symbol for structure_initialization */
       
   194 /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   195 /* structure_element_initialization_list ',' structure_element_initialization */
       
   196     void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   197 
       
   198 /*  structure_element_name ASSIGN value */
       
   199     void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   200 
       
   201 /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
       
   202 /*
       
   203 SYM_REF4(string_type_declaration_c,	string_type_name,
       
   204 					elementary_string_type_name,
       
   205 					string_type_declaration_size,
       
   206 					string_type_declaration_init) // may be == NULL!
       
   207 */
       
   208     void *visit(string_type_declaration_c *symbol)	{return symbol;}
       
   209 };
       
   210 
       
   211 
       
   212 
       
   213