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