etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * An IEC 61131-3 IL and ST compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* Determine the data type on which another data type is based on. etisserant@0: * If a new default initial value is given, we DO NOT consider it a etisserant@0: * new base class, and continue looking further! etisserant@0: * etisserant@0: * E.g. TYPE new_int_t : INT; END_TYPE; etisserant@0: * TYPE new_int2_t : INT = 2; END_TYPE; etisserant@0: * TYPE new_subr_t : INT (4..5); END_TYPE; etisserant@0: * etisserant@0: * new_int_t is really an INT!! etisserant@0: * new_int2_t is also really an INT!! etisserant@0: * new_subr_t is also really an INT!! etisserant@0: */ etisserant@0: class search_base_type_c: public null_visitor_c { etisserant@0: private: etisserant@0: symbol_c *current_type_name; etisserant@0: etisserant@0: public: etisserant@0: search_base_type_c(void) {current_type_name = NULL;} etisserant@0: etisserant@0: public: etisserant@0: void *visit(identifier_c *type_name) { etisserant@0: this->current_type_name = type_name; etisserant@0: /* look up the type declaration... */ etisserant@0: symbol_c *type_decl = type_symtable.find_value(type_name); etisserant@0: if (type_decl == type_symtable.end_value()) etisserant@0: /* Type declaration not found!! */ etisserant@0: ERROR; etisserant@0: etisserant@0: return type_decl->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /***********************************/ etisserant@0: /* B 1.3.1 - Elementary Data Types */ etisserant@0: /***********************************/ etisserant@0: void *visit(time_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(bool_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(sint_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(int_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(dint_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(lint_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(usint_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(uint_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(udint_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(ulint_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(real_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(lreal_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(date_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(tod_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(dt_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(byte_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(word_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(dword_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(lword_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(string_type_name_c *symbol) {return (void *)symbol;} etisserant@0: void *visit(wstring_type_name_c *symbol) {return (void *)symbol;} etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 1.3.3 - Derived data types */ etisserant@0: /********************************/ etisserant@0: /* simple_type_name ':' simple_spec_init */ etisserant@0: void *visit(simple_type_declaration_c *symbol) { etisserant@0: return symbol->simple_spec_init->accept(*this); etisserant@0: } etisserant@0: /* simple_specification ASSIGN constant */ etisserant@0: void *visit(simple_spec_init_c *symbol) { etisserant@0: return symbol->simple_specification->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* subrange_type_name ':' subrange_spec_init */ etisserant@0: void *visit(subrange_type_declaration_c *symbol) { etisserant@0: return symbol->subrange_spec_init->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* subrange_specification ASSIGN signed_integer */ etisserant@0: void *visit(subrange_spec_init_c *symbol) { etisserant@0: return symbol->subrange_specification->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* integer_type_name '(' subrange')' */ etisserant@0: void *visit(subrange_specification_c *symbol) { etisserant@0: return symbol->integer_type_name->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* signed_integer DOTDOT signed_integer */ etisserant@0: void *visit(subrange_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* enumerated_type_name ':' enumerated_spec_init */ etisserant@0: void *visit(enumerated_type_declaration_c *symbol) { etisserant@0: this->current_type_name = symbol->enumerated_type_name; etisserant@0: return symbol->enumerated_spec_init->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* enumerated_specification ASSIGN enumerated_value */ etisserant@0: void *visit(enumerated_spec_init_c *symbol) { etisserant@0: return symbol->enumerated_specification->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* helper symbol for enumerated_specification->enumerated_spec_init */ etisserant@0: /* enumerated_value_list ',' enumerated_value */ etisserant@0: void *visit(enumerated_value_list_c *symbol) { etisserant@0: if (NULL == this->current_type_name) ERROR; etisserant@0: return (void *)this->current_type_name; etisserant@0: } etisserant@0: etisserant@0: /* enumerated_type_name '#' identifier */ etisserant@0: // SYM_REF2(enumerated_value_c, type, value) etisserant@0: void *visit(enumerated_value_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* identifier ':' array_spec_init */ etisserant@0: void *visit(array_type_declaration_c *symbol) { etisserant@0: this->current_type_name = symbol->identifier; etisserant@0: return symbol->array_spec_init->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* array_specification [ASSIGN array_initialization} */ etisserant@0: /* array_initialization may be NULL ! */ etisserant@0: void *visit(array_spec_init_c *symbol) { etisserant@0: return symbol->array_specification->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ etisserant@0: void *visit(array_specification_c *symbol) { etisserant@0: if (NULL == this->current_type_name) ERROR; etisserant@0: return (void *)this->current_type_name; etisserant@0: } etisserant@0: etisserant@0: /* helper symbol for array_specification */ etisserant@0: /* array_subrange_list ',' subrange */ etisserant@0: void *visit(array_subrange_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* array_initialization: '[' array_initial_elements_list ']' */ etisserant@0: /* helper symbol for array_initialization */ etisserant@0: /* array_initial_elements_list ',' array_initial_elements */ etisserant@0: void *visit(array_initial_elements_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* integer '(' [array_initial_element] ')' */ etisserant@0: /* array_initial_element may be NULL ! */ etisserant@0: void *visit(array_initial_elements_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* structure_type_name ':' structure_specification */ etisserant@0: /* NOTE: structure_specification will point to either a etisserant@0: * initialized_structure_c etisserant@0: * OR A etisserant@0: * structure_element_declaration_list_c etisserant@0: */ etisserant@0: void *visit(structure_type_declaration_c *symbol) { etisserant@0: this->current_type_name = symbol->structure_type_name; etisserant@0: return symbol->structure_specification->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* structure_type_name ASSIGN structure_initialization */ etisserant@0: /* structure_initialization may be NULL ! */ etisserant@0: void *visit(initialized_structure_c *symbol) { etisserant@0: return symbol->structure_type_name->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* helper symbol for structure_declaration */ etisserant@0: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ etisserant@0: /* structure_element_declaration_list structure_element_declaration ';' */ etisserant@0: void *visit(structure_element_declaration_list_c *symbol) { etisserant@0: if (NULL == this->current_type_name) ERROR; etisserant@0: return (void *)this->current_type_name; etisserant@0: } etisserant@0: etisserant@0: /* structure_element_name ':' *_spec_init */ etisserant@0: void *visit(structure_element_declaration_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* helper symbol for structure_initialization */ etisserant@0: /* structure_initialization: '(' structure_element_initialization_list ')' */ etisserant@0: /* structure_element_initialization_list ',' structure_element_initialization */ etisserant@0: void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* structure_element_name ASSIGN value */ etisserant@0: void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ etisserant@0: etisserant@0: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ etisserant@0: /* etisserant@0: SYM_REF4(string_type_declaration_c, string_type_name, etisserant@0: elementary_string_type_name, etisserant@0: string_type_declaration_size, etisserant@0: string_type_declaration_init) // may be == NULL! etisserant@0: */ etisserant@0: void *visit(string_type_declaration_c *symbol) {return symbol;} etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: etisserant@0: