mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@265: * msousa@417: * Copyright (C) 2003-2012 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant msousa@265: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * 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: /* msousa@265: * An IEC 61131-3 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!! ccb@202: * ccb@202: * Note that a FB declaration is also considered a base type, as ccb@202: * we may have FB instances declared of a specific FB type. mario@181: */ mario@181: #include "absyntax_utils.hh" msousa@596: #include "../main.hh" // required for ERROR() and ERROR_MSG() macros. mario@181: mario@181: msousa@718: /* pointer to singleton instance */ msousa@718: search_base_type_c *search_base_type_c::search_base_type_singleton = NULL; msousa@718: msousa@718: mario@181: mjsousa@858: search_base_type_c::search_base_type_c(void) {current_basetype_name = NULL; current_basetype = NULL; current_equivtype = NULL;} mario@181: msousa@718: /* static method! */ msousa@718: void search_base_type_c::create_singleton(void) { msousa@718: if (NULL == search_base_type_singleton) search_base_type_singleton = new search_base_type_c(); msousa@718: if (NULL == search_base_type_singleton) ERROR; msousa@718: } msousa@718: msousa@718: /* static method! */ mjsousa@858: symbol_c *search_base_type_c::get_equivtype_decl(symbol_c *symbol) { mjsousa@858: create_singleton(); mjsousa@858: if (NULL == symbol) return NULL; mjsousa@858: search_base_type_singleton->current_basetype_name = NULL; mjsousa@858: search_base_type_singleton->current_basetype = NULL; mjsousa@858: search_base_type_singleton->current_equivtype = NULL; mjsousa@858: symbol_c *basetype = (symbol_c *)symbol->accept(*search_base_type_singleton); mjsousa@858: if (NULL != search_base_type_singleton->current_equivtype) mjsousa@858: return search_base_type_singleton->current_equivtype; mjsousa@858: return basetype; mjsousa@858: } mjsousa@858: mjsousa@858: /* static method! */ msousa@417: symbol_c *search_base_type_c::get_basetype_decl(symbol_c *symbol) { msousa@718: create_singleton(); msousa@718: if (NULL == symbol) return NULL; mjsousa@858: search_base_type_singleton->current_basetype_name = NULL; msousa@726: search_base_type_singleton->current_basetype = NULL; mjsousa@858: search_base_type_singleton->current_equivtype = NULL; msousa@718: return (symbol_c *)symbol->accept(*search_base_type_singleton); msousa@718: } msousa@718: msousa@718: /* static method! */ msousa@417: symbol_c *search_base_type_c::get_basetype_id (symbol_c *symbol) { msousa@718: create_singleton(); msousa@726: if (NULL == symbol) return NULL; mjsousa@858: search_base_type_singleton->current_basetype_name = NULL; msousa@726: search_base_type_singleton->current_basetype = NULL; mjsousa@858: search_base_type_singleton->current_equivtype = NULL; msousa@718: symbol->accept(*search_base_type_singleton); mjsousa@858: return (symbol_c *)search_base_type_singleton->current_basetype_name; msousa@417: } msousa@417: msousa@417: msousa@417: msousa@417: /*************************/ msousa@417: /* B.1 - Common elements */ msousa@417: /*************************/ msousa@417: msousa@417: /*******************************************/ msousa@417: /* B 1.1 - Letters, digits and identifiers */ msousa@417: /*******************************************/ msousa@417: void *search_base_type_c::visit(identifier_c *type_name) { msousa@417: symbol_c *type_decl; msousa@417: mjsousa@858: this->current_basetype_name = type_name; msousa@726: /* if we have reached this point, it is because the current_basetype is not yet pointing to the base datatype we are looking for, msousa@726: * so we will be searching for the delcaration of the type named in type_name, which might be the base datatype (we search recursively!) msousa@726: */ msousa@726: this->current_basetype = NULL; msousa@417: msousa@417: /* look up the type declaration... */ msousa@417: type_decl = type_symtable.find_value(type_name); msousa@417: if (type_decl != type_symtable.end_value()) msousa@417: return type_decl->accept(*this); msousa@417: msousa@417: type_decl = function_block_type_symtable.find_value(type_name); msousa@417: if (type_decl != function_block_type_symtable.end_value()) msousa@417: return type_decl->accept(*this); msousa@417: msousa@417: /* Type declaration not found!! */ msousa@417: ERROR; msousa@417: msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: ccb@202: /*********************/ ccb@202: /* B 1.2 - Constants */ ccb@202: /*********************/ ccb@202: ccb@202: /******************************/ ccb@202: /* B 1.2.1 - Numeric Literals */ ccb@202: /******************************/ ccb@202: /* Numeric literals without any explicit type cast have unknown data type, ccb@202: * so we continue considering them as their own basic data types until ccb@202: * they can be resolved (for example, when using '30+x' where 'x' is a LINT variable, the ccb@202: * numeric literal '30' must then be considered a LINT so the ADD function may be called ccb@202: * with all inputs of the same data type. ccb@202: * If 'x' were a SINT, then the '30' would have to be a SINT too! ccb@202: */ msousa@617: void *search_base_type_c::visit(real_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(neg_real_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(integer_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(neg_integer_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(binary_integer_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(octal_integer_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(hex_integer_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(boolean_true_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(boolean_false_c *symbol) {return (void *)symbol;} ccb@202: ccb@202: mario@181: /***********************************/ mario@181: /* B 1.3.1 - Elementary Data Types */ mario@181: /***********************************/ msousa@617: void *search_base_type_c::visit(time_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(bool_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(sint_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(int_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(dint_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(lint_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(usint_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(uint_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(udint_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(ulint_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(real_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(lreal_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(date_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(tod_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(dt_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(byte_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(word_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(dword_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(lword_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(string_type_name_c *symbol) {return (void *)symbol;} msousa@617: void *search_base_type_c::visit(wstring_type_name_c *symbol) {return (void *)symbol;} msousa@257: msousa@257: 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: /******************************************************/ msousa@257: msousa@257: void *search_base_type_c::visit(safetime_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safebool_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safesint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safeint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safedint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safelint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safeusint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safeuint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safeudint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safeulint_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safereal_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safelreal_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safedate_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safetod_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safedt_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safebyte_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safeword_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safedword_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safelword_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safestring_type_name_c *symbol) {return (void *)symbol;} msousa@257: void *search_base_type_c::visit(safewstring_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) { mjsousa@858: this->current_equivtype = 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) { mjsousa@858: if (NULL == this->current_equivtype) mjsousa@858: this->current_equivtype = symbol; 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) { mjsousa@858: if (NULL == this->current_equivtype) mjsousa@858: this->current_equivtype = symbol; mario@181: return symbol->integer_type_name->accept(*this); mario@181: } mario@181: mario@181: /* signed_integer DOTDOT signed_integer */ msousa@617: 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) { mjsousa@858: this->current_basetype_name = symbol->enumerated_type_name; msousa@726: /* NOTE: We want search_base_type_c to return a enumerated_type_declaration_c as the base datatpe if possible msousa@726: * (i.e. if it is a named datatype declared inside a TYPE ... END_TYPE declarations, as opposed to an msousa@726: * anonymous datatype declared in a VAR ... AND_VAR declaration). msousa@726: * However, we cannot return this symbol just yet, as it may not be the final base datatype. msousa@726: * So we store it in a temporary current_basetype variable! msousa@726: */ msousa@726: this->current_basetype = symbol; 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) { msousa@726: // current_basetype may have been set in the previous enumerated_type_declaration_c visitor, in which case we do not want to overwrite the value! msousa@726: if (NULL == this->current_basetype) msousa@726: this->current_basetype = symbol; msousa@726: /* NOTE: the following line may call either the visitor to msousa@726: * - identifier_c, in which case this is not yet the base datatype we are looking for (it will set current_basetype to NULL!) msousa@726: * - enumerated_value_list_c, in which case we have found the base datatype. msousa@726: */ msousa@726: 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 */ msousa@652: void *search_base_type_c::visit(enumerated_value_list_c *symbol) { msousa@726: // current_basetype may have been set in the previous enumerated_type_declaration_c or enumerated_spec_init_c visitors, in which case we do not want to overwrite the value! msousa@726: if (NULL == this->current_basetype) msousa@726: this->current_basetype = symbol; msousa@726: return (void *)current_basetype; msousa@652: } mario@181: mario@181: /* enumerated_type_name '#' identifier */ mario@181: // SYM_REF2(enumerated_value_c, type, value) msousa@617: 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) { mjsousa@858: this->current_basetype_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) { msousa@417: /* Note that the 'array_specification' may be either an identifier of a previsously defined array type, msousa@417: * or an array_specification_c, so we can not stop here and simply return a array_spec_init_c, msousa@417: * especially if we are looking for the base class! msousa@417: */ mario@181: return symbol->array_specification->accept(*this); mario@181: } mario@181: mario@181: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ msousa@617: void *search_base_type_c::visit(array_specification_c *symbol) {return (void *)symbol;} mario@181: mario@181: /* helper symbol for array_specification */ mario@181: /* array_subrange_list ',' subrange */ msousa@617: 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 */ msousa@617: 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 ! */ msousa@617: 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) { mjsousa@858: this->current_basetype_name = symbol->structure_type_name; mario@181: return symbol->structure_specification->accept(*this); mario@181: } mario@181: Laurent@412: /* var1_list ':' structure_type_name */ Laurent@412: void *search_base_type_c::visit(structured_var_declaration_c *symbol) { Laurent@625: return symbol; Laurent@412: } Laurent@412: 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 ';' */ msousa@617: void *search_base_type_c::visit(structure_element_declaration_list_c *symbol) {return (void *)symbol;} mario@181: mario@181: /* structure_element_name ':' *_spec_init */ msousa@617: 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 */ msousa@617: 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 */ msousa@617: 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: */ mjsousa@909: void *search_base_type_c::visit(string_type_declaration_c *symbol) {return (void *)symbol;} mario@181: msousa@808: msousa@810: /* function_block_type_name ASSIGN structure_initialization */ msousa@808: /* structure_initialization -> may be NULL ! */ msousa@810: // SYM_REF2(fb_spec_init_c, function_block_type_name, structure_initialization) msousa@810: void *search_base_type_c::visit(fb_spec_init_c *symbol) { msousa@808: return symbol->function_block_type_name->accept(*this); msousa@808: } msousa@808: ccb@202: msousa@810: mjsousa@909: /* ref_spec: REF_TO (non_generic_type_name | function_block_type_name) */ mjsousa@909: // SYM_REF1(ref_spec_c, type_name) mjsousa@909: void *search_base_type_c::visit(ref_spec_c *symbol) {return (void *)symbol;} mjsousa@909: mjsousa@909: /* For the moment, we do not support initialising reference data types */ mjsousa@909: /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ]; */ mjsousa@909: /* NOTE: ref_initialization may be NULL!! */ mjsousa@909: // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization) mjsousa@909: void *search_base_type_c::visit(ref_spec_init_c *symbol) { mjsousa@909: return symbol->ref_spec->accept(*this); mjsousa@909: } mjsousa@909: mjsousa@909: /* ref_type_decl: identifier ':' ref_spec_init */ mjsousa@909: // SYM_REF2(ref_type_decl_c, ref_type_name, ref_spec_init) mjsousa@909: void *search_base_type_c::visit(ref_type_decl_c *symbol) { mjsousa@909: this->current_basetype_name = symbol->ref_type_name; mjsousa@909: return symbol->ref_spec_init->accept(*this); mjsousa@909: } mjsousa@909: mjsousa@909: mjsousa@909: ccb@202: /*****************************/ ccb@202: /* B 1.5.2 - Function Blocks */ ccb@202: /*****************************/ ccb@202: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ ccb@202: // SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body) Laurent@706: void *search_base_type_c::visit(function_block_declaration_c *symbol) { Laurent@706: return (void *)symbol; Laurent@706: } msousa@617: msousa@617: msousa@617: msousa@619: /*********************************************/ msousa@619: /* B.1.6 Sequential function chart elements */ msousa@619: /*********************************************/ msousa@619: /* INITIAL_STEP step_name ':' action_association_list END_STEP */ msousa@619: // SYM_REF2(initial_step_c, step_name, action_association_list) msousa@619: void *search_base_type_c::visit(initial_step_c *symbol) { mjsousa@858: this->current_basetype_name = NULL; /* this pseudo data type does not have a type name! */ msousa@619: return (void *)symbol; msousa@619: } msousa@619: msousa@619: /* STEP step_name ':' action_association_list END_STEP */ msousa@619: // SYM_REF2(step_c, step_name, action_association_list) msousa@619: void *search_base_type_c::visit(step_c *symbol) { mjsousa@858: this->current_basetype_name = NULL; /* this pseudo data type does not have a type name! */ msousa@619: return (void *)symbol; msousa@619: } msousa@619: