mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 mario@181: * msousa@417: * Copyright (C) 2003-2012 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant mario@181: * 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: mario@181: mario@181: /* Determine the data type of a variable. mario@181: * The variable may be a simple variable, a function block instance, a mario@181: * struture element within a data structured type (a struct or a fb), or mario@181: * an array element. mario@181: * A mixture of array element of a structure element of a structure element mario@181: * of a .... is also suported! mario@181: * mario@181: * example: mario@181: * window.points[1].coordinate.x mario@181: * window.points[1].colour mario@181: * etc... ARE ALLOWED! mario@181: * mario@181: * This class must be passed the scope within which the mario@181: * variable was declared, and the variable name... msousa@321: * msousa@321: * msousa@321: * msousa@321: * msousa@321: * msousa@321: * This class has several members, depending on the exact data the caller msousa@321: * is looking for... msousa@321: * msousa@321: * - item i: we can get either the name of the data type(A), msousa@321: * or it's declaration (B) msousa@321: * (notice however that some variables belong to a data type that does msousa@321: * not have a name, only a declaration as in msousa@321: * VAR a: ARRAY [1..3] of INT; END_VAR msousa@321: * ) msousa@321: * - item ii: we can get either the direct data type (1), msousa@321: * or the base type (2) msousa@321: * msousa@321: * By direct type, I mean the data type of the variable. By base type, I msousa@321: * mean the data type on which the direct type is based on. For example, in msousa@321: * a subrange on INT, the direct type is the subrange itself, while the msousa@321: * base type is INT. msousa@321: * e.g. msousa@321: * This means that if we find that the variable is of type MY_INT, msousa@321: * which was previously declared to be msousa@321: * TYPE MY_INT: INT := 9; msousa@321: * option (1) will return MY_INT msousa@321: * option (2) will return INT msousa@321: * msousa@321: * msousa@321: * Member functions: msousa@321: * ================ msousa@417: * get_basetype_id() ---> returns 2A (implemented, although currently it is not needed! ) msousa@321: * get_basetype_decl() ---> returns 2B msousa@321: * get_type_id() ---> returns 1A msousa@321: * msousa@417: * Since we haven't yet needed it, we don't yet implement msousa@417: * get_type_decl() ---> returns 1B msousa@321: */ mario@181: msousa@417: class search_varfb_instance_type_c : null_visitor_c { mario@181: mario@181: private: mario@181: search_var_instance_decl_c search_var_instance_decl; msousa@417: msousa@417: // symbol_c *current_type_decl; msousa@417: symbol_c *current_type_id; msousa@417: symbol_c *current_basetype_decl; msousa@417: symbol_c *current_basetype_id; msousa@417: msousa@417: symbol_c *current_field_selector; msousa@417: msousa@417: /* sets all the above variables to NULL, or false */ msousa@417: void init(void); mario@181: mario@181: public: msousa@531: search_varfb_instance_type_c(symbol_c *search_scope ); msousa@531: symbol_c *get_basetype_decl (symbol_c *variable_name); msousa@531: symbol_c *get_basetype_id (symbol_c *variable_name); msousa@531: // symbol_c *get_type_decl (symbol_c *variable_name); msousa@531: symbol_c *get_type_id (symbol_c *variable_name); mario@181: msousa@531: mario@181: mario@181: private: mario@181: /* a helper function... */ mario@181: void *visit_list(list_c *list); mario@181: mario@181: /* a helper function... */ mario@181: void *base_type(symbol_c *symbol); mario@181: mario@181: mario@181: private: msousa@420: /*************************/ msousa@420: /* B.1 - Common elements */ msousa@420: /*************************/ msousa@420: /*******************************************/ msousa@420: /* B 1.1 - Letters, digits and identifiers */ msousa@420: /*******************************************/ msousa@420: void *visit(identifier_c *variable_name); msousa@420: mario@181: /********************************/ mario@181: /* B 1.3.3 - Derived data types */ mario@181: /********************************/ mario@181: /* identifier ':' array_spec_init */ mario@181: void *visit(array_type_declaration_c *symbol); mario@181: 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: mario@181: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ mario@181: void *visit(array_specification_c *symbol); mario@181: mario@181: /* structure_type_name ':' structure_specification */ mario@181: void *visit(structure_type_declaration_c *symbol); mario@181: mario@181: /* structure_type_name ASSIGN structure_initialization */ mario@181: /* structure_initialization may be NULL ! */ mario@181: // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) mario@181: void *visit(initialized_structure_c *symbol); 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 *visit(structure_element_declaration_list_c *symbol); mario@181: mario@181: /* structure_element_name ':' spec_init */ mario@181: void *visit(structure_element_declaration_c *symbol); 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 *visit(structure_element_initialization_list_c *symbol); /* should never get called... */ mario@181: /* structure_element_name ASSIGN value */ mario@181: void *visit(structure_element_initialization_c *symbol); /* should never get called... */ mario@181: mario@181: msousa@417: /*********************/ msousa@417: /* B 1.4 - Variables */ msousa@417: /*********************/ msousa@417: void *visit(symbolic_variable_c *symbol); msousa@417: msousa@417: /********************************************/ msousa@417: /* B.1.4.1 Directly Represented Variables */ msousa@417: /********************************************/ msousa@417: /*************************************/ msousa@417: /* B 1.4.2 - Multi-element variables */ msousa@417: /*************************************/ msousa@417: void *visit(array_variable_c *symbol); msousa@417: void *visit(structured_variable_c *symbol); msousa@417: mario@181: /**************************************/ mario@181: /* B.1.5 - Program organization units */ mario@181: /**************************************/ mario@181: /*****************************/ mario@181: /* B 1.5.2 - Function Blocks */ mario@181: /*****************************/ mario@181: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ mario@181: // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) mario@181: void *visit(function_block_declaration_c *symbol); mario@181: msousa@619: 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: /* 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); mjsousa@936: mjsousa@936: /***************************************/ mjsousa@936: /* B.3 - Language ST (Structured Text) */ mjsousa@936: /***************************************/ mjsousa@936: /***********************/ mjsousa@936: /* B 3.1 - Expressions */ mjsousa@936: /***********************/ mjsousa@936: /* SYM_REF1(deref_expression_c, exp) --> an extension to the IEC 61131-3 standard - based on the IEC 61131-3 v3 standard. Returns address of the varible! */ mjsousa@936: void *visit(deref_operator_c *symbol); mjsousa@936: void *visit(deref_expression_c *symbol); mjsousa@936: mario@181: }; // search_varfb_instance_type_c mario@181: mario@181: mario@181: mario@181: mario@181: mario@181: