stage4/generate_c/search_base_type.cc
changeset 181 38d6eb056260
parent 180 64334c5a00b1
child 182 231633d1d2e4
equal deleted inserted replaced
180:64334c5a00b1 181:38d6eb056260
     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     bool is_subrange;
       
    42     bool is_enumerated;
       
    43 
       
    44   public:
       
    45     search_base_type_c(void) {current_type_name = NULL;}
       
    46     
       
    47   public:
       
    48     void *visit(identifier_c *type_name) {
       
    49       this->current_type_name = type_name;
       
    50       /* look up the type declaration... */
       
    51       symbol_c *type_decl = type_symtable.find_value(type_name);
       
    52       if (type_decl == type_symtable.end_value())
       
    53         /* Type declaration not found!! */
       
    54         ERROR;
       
    55 
       
    56       return type_decl->accept(*this);
       
    57     }
       
    58     
       
    59     bool type_is_subrange(symbol_c* type_decl) {
       
    60       this->is_subrange = false;
       
    61       type_decl->accept(*this);
       
    62       return this->is_subrange;
       
    63     }
       
    64 
       
    65     bool type_is_enumerated(symbol_c* type_decl) {
       
    66       this->is_enumerated = false;
       
    67       type_decl->accept(*this);
       
    68       return this->is_enumerated;
       
    69     }
       
    70     
       
    71 /***********************************/
       
    72 /* B 1.3.1 - Elementary Data Types */
       
    73 /***********************************/
       
    74     void *visit(time_type_name_c *symbol)	{return (void *)symbol;}
       
    75     void *visit(bool_type_name_c *symbol)	{return (void *)symbol;}
       
    76     void *visit(sint_type_name_c *symbol)	{return (void *)symbol;}
       
    77     void *visit(int_type_name_c *symbol)	{return (void *)symbol;}
       
    78     void *visit(dint_type_name_c *symbol)	{return (void *)symbol;}
       
    79     void *visit(lint_type_name_c *symbol)	{return (void *)symbol;}
       
    80     void *visit(usint_type_name_c *symbol)	{return (void *)symbol;}
       
    81     void *visit(uint_type_name_c *symbol)	{return (void *)symbol;}
       
    82     void *visit(udint_type_name_c *symbol)	{return (void *)symbol;}
       
    83     void *visit(ulint_type_name_c *symbol)	{return (void *)symbol;}
       
    84     void *visit(real_type_name_c *symbol)	{return (void *)symbol;}
       
    85     void *visit(lreal_type_name_c *symbol)	{return (void *)symbol;}
       
    86     void *visit(date_type_name_c *symbol)	{return (void *)symbol;}
       
    87     void *visit(tod_type_name_c *symbol)	{return (void *)symbol;}
       
    88     void *visit(dt_type_name_c *symbol)		{return (void *)symbol;}
       
    89     void *visit(byte_type_name_c *symbol)	{return (void *)symbol;}
       
    90     void *visit(word_type_name_c *symbol)	{return (void *)symbol;}
       
    91     void *visit(dword_type_name_c *symbol)	{return (void *)symbol;}
       
    92     void *visit(lword_type_name_c *symbol)	{return (void *)symbol;}
       
    93     void *visit(string_type_name_c *symbol)	{return (void *)symbol;}
       
    94     void *visit(wstring_type_name_c *symbol)	{return (void *)symbol;}
       
    95     void *visit(constant_int_type_name_c *symbol)    {return (void *)symbol;}
       
    96     void *visit(constant_real_type_name_c *symbol)    {return (void *)symbol;}
       
    97     void *visit(direct_variable_type_name_c *symbol)    {return (void *)symbol;}
       
    98     /******************************************************/
       
    99     /* Extensions to the base standard as defined in      */
       
   100     /* "Safety Software Technical Specification,          */
       
   101     /*  Part 1: Concepts and Function Blocks,             */
       
   102     /*  Version 1.0 – Official Release"                   */
       
   103     /* by PLCopen - Technical Committee 5 - 2006-01-31    */
       
   104     /******************************************************/
       
   105     void *visit(safebool_type_name_c *symbol)	{return (void *)symbol;}
       
   106 
       
   107 /********************************/
       
   108 /* B 1.3.3 - Derived data types */
       
   109 /********************************/
       
   110 /*  simple_type_name ':' simple_spec_init */
       
   111     void *visit(simple_type_declaration_c *symbol) {
       
   112       return symbol->simple_spec_init->accept(*this);
       
   113     }
       
   114 /* simple_specification ASSIGN constant */
       
   115     void *visit(simple_spec_init_c *symbol) {
       
   116       return symbol->simple_specification->accept(*this);
       
   117     }
       
   118 
       
   119 /*  subrange_type_name ':' subrange_spec_init */
       
   120     void *visit(subrange_type_declaration_c *symbol) {
       
   121       return symbol->subrange_spec_init->accept(*this);
       
   122     }
       
   123 
       
   124 /* subrange_specification ASSIGN signed_integer */
       
   125     void *visit(subrange_spec_init_c *symbol) {
       
   126       this->is_subrange = true;
       
   127       return symbol->subrange_specification->accept(*this);
       
   128     }
       
   129 
       
   130 /*  integer_type_name '(' subrange')' */
       
   131     void *visit(subrange_specification_c *symbol) {
       
   132       return symbol->integer_type_name->accept(*this);
       
   133     }
       
   134 
       
   135 /*  signed_integer DOTDOT signed_integer */
       
   136     void *visit(subrange_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   137 
       
   138 /*  enumerated_type_name ':' enumerated_spec_init */
       
   139     void *visit(enumerated_type_declaration_c *symbol) {
       
   140       this->current_type_name = symbol->enumerated_type_name;
       
   141       return symbol->enumerated_spec_init->accept(*this);
       
   142     }
       
   143 
       
   144 /* enumerated_specification ASSIGN enumerated_value */
       
   145     void *visit(enumerated_spec_init_c *symbol) {
       
   146       this->is_enumerated = true;
       
   147       return symbol->enumerated_specification->accept(*this);
       
   148     }
       
   149 
       
   150 /* helper symbol for enumerated_specification->enumerated_spec_init */
       
   151 /* enumerated_value_list ',' enumerated_value */
       
   152     void *visit(enumerated_value_list_c *symbol) {
       
   153       if (NULL == this->current_type_name) ERROR;
       
   154       return (void *)this->current_type_name;
       
   155     }
       
   156 
       
   157 /* enumerated_type_name '#' identifier */
       
   158 // SYM_REF2(enumerated_value_c, type, value)
       
   159     void *visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   160 
       
   161 /*  identifier ':' array_spec_init */
       
   162     void *visit(array_type_declaration_c *symbol) {
       
   163       this->current_type_name = symbol->identifier;
       
   164       return symbol->array_spec_init->accept(*this);
       
   165     }
       
   166 
       
   167 /* array_specification [ASSIGN array_initialization} */
       
   168 /* array_initialization may be NULL ! */
       
   169     void *visit(array_spec_init_c *symbol) {
       
   170       return symbol->array_specification->accept(*this);
       
   171     }
       
   172 
       
   173 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
       
   174     void *visit(array_specification_c *symbol)	{
       
   175       if (NULL == this->current_type_name) ERROR;
       
   176       return symbol->non_generic_type_name->accept(*this);
       
   177     }
       
   178 
       
   179 /* helper symbol for array_specification */
       
   180 /* array_subrange_list ',' subrange */
       
   181     void *visit(array_subrange_list_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   182 
       
   183 /* array_initialization:  '[' array_initial_elements_list ']' */
       
   184 /* helper symbol for array_initialization */
       
   185 /* array_initial_elements_list ',' array_initial_elements */
       
   186     void *visit(array_initial_elements_list_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   187 
       
   188 /* integer '(' [array_initial_element] ')' */
       
   189 /* array_initial_element may be NULL ! */
       
   190     void *visit(array_initial_elements_c *symbol)	{ERROR; return NULL;} /* should never get called... */
       
   191 
       
   192 /*  structure_type_name ':' structure_specification */
       
   193       /* NOTE: structure_specification will point to either a
       
   194        *       initialized_structure_c
       
   195        *       OR A
       
   196        *       structure_element_declaration_list_c
       
   197        */
       
   198     void *visit(structure_type_declaration_c *symbol)  {
       
   199       this->current_type_name = symbol->structure_type_name;
       
   200       return symbol->structure_specification->accept(*this);
       
   201     }
       
   202 
       
   203 /* structure_type_name ASSIGN structure_initialization */
       
   204 /* structure_initialization may be NULL ! */
       
   205     void *visit(initialized_structure_c *symbol)	{
       
   206       return symbol->structure_type_name->accept(*this);
       
   207     }
       
   208 
       
   209 /* helper symbol for structure_declaration */
       
   210 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   211 /* structure_element_declaration_list structure_element_declaration ';' */
       
   212     void *visit(structure_element_declaration_list_c *symbol)	{
       
   213       if (NULL == this->current_type_name) ERROR;
       
   214       return (void *)symbol;
       
   215     }
       
   216 
       
   217 /*  structure_element_name ':' *_spec_init */
       
   218     void *visit(structure_element_declaration_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   219 
       
   220 /* helper symbol for structure_initialization */
       
   221 /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   222 /* structure_element_initialization_list ',' structure_element_initialization */
       
   223     void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   224 
       
   225 /*  structure_element_name ASSIGN value */
       
   226     void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   227 
       
   228 /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
       
   229 /*
       
   230 SYM_REF4(string_type_declaration_c,	string_type_name,
       
   231 					elementary_string_type_name,
       
   232 					string_type_declaration_size,
       
   233 					string_type_declaration_init) // may be == NULL!
       
   234 */
       
   235     void *visit(string_type_declaration_c *symbol)	{return symbol;}
       
   236 
       
   237 
       
   238 };
       
   239 
       
   240 
       
   241 
       
   242