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: mjsousa@858: /* Determine the data type on which another data type is based on. */ mjsousa@858: mjsousa@858: /* mjsousa@858: * What is a Base Type? mjsousa@858: * A base type is the fundamental data type from which the type is derived. mjsousa@858: * The main idea is that if two datatyes (A and B) share a common base type, mjsousa@858: * then these two datatypes may be used interchangeably in an expression. mjsousa@858: * mjsousa@858: * What is an Equivalent Type? mjsousa@858: * An equivalent type is the data type from which the type is derived. mjsousa@858: * The Base type and the Equivalent type will always be the same, with the mjsousa@858: * exception of subranges! mjsousa@858: * mjsousa@858: * E.g. TYPE new_int_t : INT; END_TYPE; mjsousa@858: * TYPE new_int2_t : INT := 2; END_TYPE; mjsousa@858: * TYPE new_int3_t : new_int2_t := 3; END_TYPE; mjsousa@858: * TYPE new_sub_t : INT (4..10); END_TYPE; mjsousa@858: * TYPE new_sub2_t : new_sub_t := 5 ; END_TYPE; mjsousa@858: * TYPE new_sub3_t : new_sub2_t := 6 ; END_TYPE; mjsousa@858: * TYPE new_sub4_t : new_int3_t (4..10); END_TYPE; <----- This is NOT legal syntax! mjsousa@858: * mjsousa@858: * new_int_t : base type->INT equivalent type->INT mjsousa@858: * new_int2_t : base type->INT equivalent type->INT mjsousa@858: * new_int3_t : base type->INT equivalent type->INT mjsousa@858: * new_sub_t : base type->INT equivalent type->new_sub_t mjsousa@858: * new_sub2_t : base type->INT equivalent type->new_sub_t mjsousa@858: * new_sub3_t : base type->INT equivalent type->new_sub_t mjsousa@858: * mjsousa@858: * Note too 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: mario@181: mario@181: class search_base_type_c: public null_visitor_c { mario@181: mario@181: private: mjsousa@858: symbol_c *current_basetype_name; msousa@726: symbol_c *current_basetype; mjsousa@858: symbol_c *current_equivtype; msousa@718: static search_base_type_c *search_base_type_singleton; // Make this a singleton class! msousa@718: msousa@718: private: msousa@718: static void create_singleton(void); mjsousa@945: void *handle_datatype_identifier(token_c *type_name); mario@181: mario@181: public: mario@181: search_base_type_c(void); mjsousa@858: static symbol_c *get_equivtype_decl(symbol_c *symbol); /* get the Equivalent Type declaration */ mjsousa@858: static symbol_c *get_basetype_decl (symbol_c *symbol); /* get the Base Type declaration */ mjsousa@858: static symbol_c *get_basetype_id (symbol_c *symbol); /* get the Base Type identifier */ mario@181: mario@181: public: msousa@417: /*************************/ msousa@417: /* B.1 - Common elements */ msousa@417: /*************************/ msousa@417: /*******************************************/ msousa@417: /* B 1.1 - Letters, digits and identifiers */ msousa@417: /*******************************************/ mjsousa@945: void *visit( identifier_c *type_name); mjsousa@945: void *visit(derived_datatype_identifier_c *type_name); msousa@417: msousa@417: /*********************/ msousa@417: /* B 1.2 - Constants */ msousa@417: /*********************/ msousa@417: /******************************/ msousa@417: /* B 1.2.1 - Numeric Literals */ msousa@417: /******************************/ 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: */ ccb@202: void *visit(real_c *symbol); msousa@257: void *visit(neg_real_c *symbol); ccb@202: void *visit(integer_c *symbol); msousa@257: void *visit(neg_integer_c *symbol); ccb@202: void *visit(binary_integer_c *symbol); ccb@202: void *visit(octal_integer_c *symbol); ccb@202: void *visit(hex_integer_c *symbol); msousa@257: void *visit(boolean_true_c *symbol); msousa@257: void *visit(boolean_false_c *symbol); ccb@202: ccb@202: mario@181: /***********************************/ mario@181: /* B 1.3.1 - Elementary Data Types */ mario@181: /***********************************/ mario@181: void *visit(time_type_name_c *symbol); mario@181: void *visit(bool_type_name_c *symbol); mario@181: void *visit(sint_type_name_c *symbol); mario@181: void *visit(int_type_name_c *symbol); mario@181: void *visit(dint_type_name_c *symbol); mario@181: void *visit(lint_type_name_c *symbol); mario@181: void *visit(usint_type_name_c *symbol); mario@181: void *visit(uint_type_name_c *symbol); mario@181: void *visit(udint_type_name_c *symbol); mario@181: void *visit(ulint_type_name_c *symbol); mario@181: void *visit(real_type_name_c *symbol); mario@181: void *visit(lreal_type_name_c *symbol); mario@181: void *visit(date_type_name_c *symbol); mario@181: void *visit(tod_type_name_c *symbol); Laurent@706: void *visit(dt_type_name_c *symbol); mario@181: void *visit(byte_type_name_c *symbol); mario@181: void *visit(word_type_name_c *symbol); mario@181: void *visit(dword_type_name_c *symbol); mario@181: void *visit(lword_type_name_c *symbol); mario@181: void *visit(string_type_name_c *symbol); mario@181: void *visit(wstring_type_name_c *symbol); ccb@202: 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: void *visit(safetime_type_name_c *symbol); mario@181: void *visit(safebool_type_name_c *symbol); msousa@257: void *visit(safesint_type_name_c *symbol); msousa@257: void *visit(safeint_type_name_c *symbol); msousa@257: void *visit(safedint_type_name_c *symbol); msousa@257: void *visit(safelint_type_name_c *symbol); msousa@257: void *visit(safeusint_type_name_c *symbol); msousa@257: void *visit(safeuint_type_name_c *symbol); msousa@257: void *visit(safeudint_type_name_c *symbol); msousa@257: void *visit(safeulint_type_name_c *symbol); msousa@257: void *visit(safereal_type_name_c *symbol); msousa@257: void *visit(safelreal_type_name_c *symbol); msousa@257: void *visit(safedate_type_name_c *symbol); msousa@257: void *visit(safetod_type_name_c *symbol); Laurent@706: void *visit(safedt_type_name_c *symbol); msousa@257: void *visit(safebyte_type_name_c *symbol); msousa@257: void *visit(safeword_type_name_c *symbol); msousa@257: void *visit(safedword_type_name_c *symbol); msousa@257: void *visit(safelword_type_name_c *symbol); msousa@257: void *visit(safestring_type_name_c *symbol); msousa@257: void *visit(safewstring_type_name_c *symbol); mario@181: mario@181: /********************************/ mjsousa@921: /* B.1.3.2 - Generic data types */ mjsousa@921: /********************************/ mjsousa@921: void *visit(generic_type_any_c *symbol); mjsousa@921: mjsousa@921: /********************************/ mario@181: /* B 1.3.3 - Derived data types */ mario@181: /********************************/ mario@181: /* simple_type_name ':' simple_spec_init */ mario@181: void *visit(simple_type_declaration_c *symbol); mario@181: /* simple_specification ASSIGN constant */ mario@181: void *visit(simple_spec_init_c *symbol); mario@181: /* subrange_type_name ':' subrange_spec_init */ mario@181: void *visit(subrange_type_declaration_c *symbol); mario@181: /* subrange_specification ASSIGN signed_integer */ mario@181: void *visit(subrange_spec_init_c *symbol); mario@181: /* integer_type_name '(' subrange')' */ mario@181: void *visit(subrange_specification_c *symbol); mario@181: /* signed_integer DOTDOT signed_integer */ mario@181: void *visit(subrange_c *symbol); mario@181: mario@181: /* enumerated_type_name ':' enumerated_spec_init */ mario@181: void *visit(enumerated_type_declaration_c *symbol); mario@181: /* enumerated_specification ASSIGN enumerated_value */ mario@181: void *visit(enumerated_spec_init_c *symbol); mario@181: /* helper symbol for enumerated_specification->enumerated_spec_init */ mario@181: /* enumerated_value_list ',' enumerated_value */ mario@181: void *visit(enumerated_value_list_c *symbol); mario@181: /* enumerated_type_name '#' identifier */ mario@181: // SYM_REF2(enumerated_value_c, type, value) mario@181: void *visit(enumerated_value_c *symbol); mario@181: /* identifier ':' array_spec_init */ mario@181: void *visit(array_type_declaration_c *symbol); mario@181: /* array_specification [ASSIGN array_initialization} */ mario@181: /* array_initialization may be NULL ! */ mario@181: void *visit(array_spec_init_c *symbol); mario@181: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ mario@181: void *visit(array_specification_c *symbol); mario@181: /* helper symbol for array_specification */ mario@181: /* array_subrange_list ',' subrange */ mario@181: void *visit(array_subrange_list_c *symbol); 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 *visit(array_initial_elements_list_c *symbol); mario@181: /* integer '(' [array_initial_element] ')' */ mario@181: /* array_initial_element may be NULL ! */ mario@181: void *visit(array_initial_elements_c *symbol); 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 *visit(structure_type_declaration_c *symbol); Laurent@412: /* var1_list ':' structure_type_name */ Laurent@412: void *visit(structured_var_declaration_c *symbol); mario@181: /* structure_type_name ASSIGN structure_initialization */ mario@181: /* structure_initialization may be NULL ! */ mario@181: void *visit(initialized_structure_c *symbol); 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 *visit(structure_element_declaration_list_c *symbol); mario@181: /* structure_element_name ':' *_spec_init */ mario@181: void *visit(structure_element_declaration_c *symbol); 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 *visit(structure_element_initialization_list_c *symbol); mario@181: /* structure_element_name ASSIGN value */ mario@181: void *visit(structure_element_initialization_c *symbol); mario@181: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ mario@181: /* Laurent@706: 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 *visit(string_type_declaration_c *symbol); ccb@202: msousa@810: /* function_block_type_name ASSIGN structure_initialization */ msousa@808: /* structure_initialization -> may be NULL ! */ msousa@810: void *visit(fb_spec_init_c *symbol); msousa@808: mjsousa@909: /* REF_TO (non_generic_type_name | function_block_type_name) */ mjsousa@909: void *visit(ref_spec_c *symbol); // Defined in IEC 61131-3 v3 mjsousa@909: /* ref_spec [ ASSIGN ref_initialization ]; *//* NOTE: ref_initialization may be NULL!! */ mjsousa@909: void *visit(ref_spec_init_c *symbol); // Defined in IEC 61131-3 v3 mjsousa@909: /* identifier ':' ref_spec_init */ mjsousa@909: void *visit(ref_type_decl_c *symbol); // Defined in IEC 61131-3 v3 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) ccb@202: void *visit(function_block_declaration_c *symbol); ccb@202: 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 *visit(initial_step_c *symbol); 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 *visit(step_c *symbol); msousa@619: msousa@619: mario@181: }; // search_base_type_c mario@181: mario@181: mario@181: mario@181: