stage4/generate_cc/search_base_type.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 /* 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     void *visit(constant_int_type_name_c *symbol)    {return (void *)symbol;}
       
    82     void *visit(constant_real_type_name_c *symbol)    {return (void *)symbol;}
       
    83 
       
    84 /********************************/
       
    85 /* B 1.3.3 - Derived data types */
       
    86 /********************************/
       
    87 /*  simple_type_name ':' simple_spec_init */
       
    88     void *visit(simple_type_declaration_c *symbol) {
       
    89       return symbol->simple_spec_init->accept(*this);
       
    90     }
       
    91 /* simple_specification ASSIGN constant */
       
    92     void *visit(simple_spec_init_c *symbol) {
       
    93       return symbol->simple_specification->accept(*this);
       
    94     }
       
    95 
       
    96 /*  subrange_type_name ':' subrange_spec_init */
       
    97     void *visit(subrange_type_declaration_c *symbol) {
       
    98       return symbol->subrange_spec_init->accept(*this);
       
    99     }
       
   100 
       
   101 /* subrange_specification ASSIGN signed_integer */
       
   102     void *visit(subrange_spec_init_c *symbol) {
       
   103       return symbol->subrange_specification->accept(*this);
       
   104     }
       
   105 
       
   106 /*  integer_type_name '(' subrange')' */
       
   107     void *visit(subrange_specification_c *symbol) {
       
   108       return symbol->integer_type_name->accept(*this);
       
   109     }
       
   110 
       
   111 /*  signed_integer DOTDOT signed_integer */
       
   112     void *visit(subrange_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   113 
       
   114 /*  enumerated_type_name ':' enumerated_spec_init */
       
   115     void *visit(enumerated_type_declaration_c *symbol) {
       
   116       this->current_type_name = symbol->enumerated_type_name;
       
   117       return symbol->enumerated_spec_init->accept(*this);
       
   118     }
       
   119 
       
   120 /* enumerated_specification ASSIGN enumerated_value */
       
   121     void *visit(enumerated_spec_init_c *symbol) {
       
   122       return symbol->enumerated_specification->accept(*this);
       
   123     }
       
   124 
       
   125 /* helper symbol for enumerated_specification->enumerated_spec_init */
       
   126 /* enumerated_value_list ',' enumerated_value */
       
   127     void *visit(enumerated_value_list_c *symbol) {
       
   128       if (NULL == this->current_type_name) ERROR;
       
   129       return (void *)this->current_type_name;
       
   130     }
       
   131 
       
   132 /* enumerated_type_name '#' identifier */
       
   133 // SYM_REF2(enumerated_value_c, type, value)
       
   134     void *visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   135 
       
   136 /*  identifier ':' array_spec_init */
       
   137     void *visit(array_type_declaration_c *symbol) {
       
   138       this->current_type_name = symbol->identifier;
       
   139       return symbol->array_spec_init->accept(*this);
       
   140     }
       
   141 
       
   142 /* array_specification [ASSIGN array_initialization} */
       
   143 /* array_initialization may be NULL ! */
       
   144     void *visit(array_spec_init_c *symbol) {
       
   145       return symbol->array_specification->accept(*this);
       
   146     }
       
   147 
       
   148 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
       
   149     void *visit(array_specification_c *symbol)	{
       
   150       if (NULL == this->current_type_name) ERROR;
       
   151       return (void *)this->current_type_name;
       
   152     }
       
   153 
       
   154 /* helper symbol for array_specification */
       
   155 /* array_subrange_list ',' subrange */
       
   156     void *visit(array_subrange_list_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   157 
       
   158 /* array_initialization:  '[' array_initial_elements_list ']' */
       
   159 /* helper symbol for array_initialization */
       
   160 /* array_initial_elements_list ',' array_initial_elements */
       
   161     void *visit(array_initial_elements_list_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   162 
       
   163 /* integer '(' [array_initial_element] ')' */
       
   164 /* array_initial_element may be NULL ! */
       
   165     void *visit(array_initial_elements_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   166 
       
   167 /*  structure_type_name ':' structure_specification */
       
   168       /* NOTE: structure_specification will point to either a
       
   169        *       initialized_structure_c
       
   170        *       OR A
       
   171        *       structure_element_declaration_list_c
       
   172        */
       
   173     void *visit(structure_type_declaration_c *symbol)  {
       
   174       this->current_type_name = symbol->structure_type_name;
       
   175       return symbol->structure_specification->accept(*this);
       
   176     }
       
   177 
       
   178 /* structure_type_name ASSIGN structure_initialization */
       
   179 /* structure_initialization may be NULL ! */
       
   180     void *visit(initialized_structure_c *symbol)	{
       
   181       return symbol->structure_type_name->accept(*this);
       
   182     }
       
   183 
       
   184 /* helper symbol for structure_declaration */
       
   185 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   186 /* structure_element_declaration_list structure_element_declaration ';' */
       
   187     void *visit(structure_element_declaration_list_c *symbol)	{
       
   188       if (NULL == this->current_type_name) ERROR;
       
   189       return (void *)this->current_type_name;
       
   190     }
       
   191 
       
   192 /*  structure_element_name ':' *_spec_init */
       
   193     void *visit(structure_element_declaration_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   194 
       
   195 /* helper symbol for structure_initialization */
       
   196 /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   197 /* structure_element_initialization_list ',' structure_element_initialization */
       
   198     void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   199 
       
   200 /*  structure_element_name ASSIGN value */
       
   201     void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   202 
       
   203 /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
       
   204 /*
       
   205 SYM_REF4(string_type_declaration_c,	string_type_name,
       
   206 					elementary_string_type_name,
       
   207 					string_type_declaration_size,
       
   208 					string_type_declaration_init) // may be == NULL!
       
   209 */
       
   210     void *visit(string_type_declaration_c *symbol)	{return symbol;}
       
   211 };
       
   212 
       
   213 
       
   214 
       
   215