mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@265: * msousa@265: * Copyright (C) 2003-2011 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 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@321: * get_basetype_decl() ---> returns 2B msousa@321: * get_type_id() ---> returns 1A msousa@321: * msousa@321: * Since we haven't yet needed them, we don't yet implement msousa@321: * get_basetype_id() ----> would return 2A msousa@321: * get_type_decl() ----> would return 1B msousa@321: */ mario@181: mario@181: ccb@202: /* ccb@202: * TODO: this code has a memory leak... ccb@202: * We call 'new' in several locations, but bever get to 'delete' the object instances... ccb@202: */ mario@181: #include "absyntax_utils.hh" mario@181: mario@181: mario@181: search_varfb_instance_type_c::search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) { mario@181: this->decompose_var_instance_name = NULL; mario@181: this->current_structelement_name = NULL; msousa@371: this->current_typeid = NULL; msousa@372: this->current_basetypeid = NULL; msousa@372: } msousa@372: msousa@372: symbol_c *search_varfb_instance_type_c::get_type_decl(symbol_c *variable_name) { mario@181: this->current_structelement_name = NULL; msousa@371: this->current_typeid = NULL; msousa@372: this->current_basetypeid = NULL; mario@181: this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); mario@181: if (NULL == decompose_var_instance_name) ERROR; mario@181: mario@181: /* find the part of the variable name that will appear in the mario@181: * variable declaration, for e.g., in window.point.x, this would be mario@181: * window! mario@181: */ mario@181: symbol_c *var_name_part = decompose_var_instance_name->next_part(); mario@181: if (NULL == var_name_part) ERROR; ccb@202: mario@181: /* Now we try to find the variable instance declaration, to determine its type... */ mario@181: symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); msousa@372: if (NULL == var_decl) ERROR; mario@181: mario@181: /* if it is a struct or function block, we must search the type mario@181: * of the struct or function block member. mario@181: * This is done by this class visiting the var_decl. mario@181: * This class, while visiting, will recursively call mario@181: * decompose_var_instance_name->get_next() when and if required... mario@181: */ mario@181: symbol_c *res = (symbol_c *)var_decl->accept(*this); msousa@367: /* NOTE: A Null result is not really an internal compiler error, but rather an error in msousa@367: * the IEC 61131-3 source code being compiled. This means we cannot just abort the compiler with ERROR. msousa@367: * // if (NULL == res) ERROR; msousa@367: */ msousa@367: if (NULL == res) return NULL; mario@181: laurent@226: /* make sure that we have decomposed all structure elements of the variable name */ mario@181: symbol_c *var_name = decompose_var_instance_name->next_part(); msousa@367: /* NOTE: A non-NULL result is not really an internal compiler error, but rather an error in msousa@367: * the IEC 61131-3 source code being compiled. msousa@367: * (for example, 'int_var.struct_elem' in the source code, when 'int_var' is a simple integer, msousa@367: * and not a structure, will result in this result being non-NULL!) msousa@367: * This means we cannot just abort the compiler with ERROR. msousa@367: * // if (NULL != var_name) ERROR; msousa@367: */ msousa@367: if (NULL != var_name) return NULL; mario@181: mario@181: return res; mario@181: } mario@181: msousa@372: msousa@372: symbol_c *search_varfb_instance_type_c::get_basetype_decl(symbol_c *variable_name) { msousa@372: symbol_c *res = get_type_decl(variable_name); msousa@372: if (NULL == res) return NULL; msousa@372: return (symbol_c *)base_type(res); msousa@372: } msousa@372: msousa@372: mario@181: unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) { mario@181: this->current_structelement_name = NULL; msousa@371: this->current_typeid = NULL; msousa@372: this->current_basetypeid = NULL; laurent@226: this->is_complex = false; mario@181: this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); mario@181: if (NULL == decompose_var_instance_name) ERROR; mario@181: mario@181: /* find the part of the variable name that will appear in the mario@181: * variable declaration, for e.g., in window.point.x, this would be mario@181: * window! mario@181: */ mario@181: symbol_c *var_name_part = decompose_var_instance_name->next_part(); mario@181: if (NULL == var_name_part) ERROR; mario@181: mario@181: /* Now we try to find the variable instance declaration, to determine its type... */ mario@181: symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); mario@181: if (NULL == var_decl) { mario@181: /* variable instance declaration not found! */ mario@181: return 0; mario@181: } mario@181: mario@181: /* if it is a struct or function block, we must search the type mario@181: * of the struct or function block member. mario@181: * This is done by this class visiting the var_decl. mario@181: * This class, while visiting, will recursively call mario@181: * decompose_var_instance_name->get_next() when and if required... mario@181: */ laurent@226: var_decl->accept(*this); mario@181: unsigned int res = search_var_instance_decl.get_vartype(); mario@181: laurent@226: /* make sure that we have decomposed all structure elements of the variable name */ mario@181: symbol_c *var_name = decompose_var_instance_name->next_part(); mario@181: if (NULL != var_name) ERROR; mario@181: mario@181: return res; mario@181: } mario@181: msousa@321: symbol_c *search_varfb_instance_type_c::get_type_id(symbol_c *variable_name) { msousa@371: this->current_typeid = NULL; msousa@372: symbol_c *vartype = this->get_type_decl(variable_name); msousa@371: if (this->current_typeid != NULL) msousa@371: return this->current_typeid; laurent@226: else msousa@371: return vartype; laurent@226: } laurent@226: laurent@226: bool search_varfb_instance_type_c::type_is_complex(void) { laurent@226: return this->is_complex; laurent@226: } laurent@226: mario@181: /* a helper function... */ mario@181: void *search_varfb_instance_type_c::visit_list(list_c *list) { mario@181: if (NULL == current_structelement_name) ERROR; mario@181: mario@181: for(int i = 0; i < list->n; i++) { mario@181: void *res = list->elements[i]->accept(*this); mario@181: if (res != NULL) mario@181: return res; mario@181: } mario@181: /* not found! */ mario@181: return NULL; mario@181: } mario@181: mario@181: /* a helper function... */ mario@181: void *search_varfb_instance_type_c::base_type(symbol_c *symbol) { mario@181: search_base_type_c search_base_type; mario@181: return symbol->accept(search_base_type); mario@181: } mario@181: mario@181: /* We override the base class' visitor to identifier_c. mario@181: * This is so because the base class does not consider a function block mario@181: * to be a type, unlike this class that allows a variable instance mario@181: * of a function block type... mario@181: */ mario@181: void *search_varfb_instance_type_c::visit(identifier_c *type_name) { msousa@372: /* we only store the new type id if none had been found yet. msousa@372: * Since we will recursively carry on looking at the base type msousa@372: * to determine the base type declaration and id, we must only set this variable msousa@372: * the first time. msousa@372: * e.g. TYPE myint1_t : int := 1; msousa@372: * myint2_t : int1_t := 2; msousa@372: * myint3_t : int2_t := 3; msousa@372: * END_TYPE; msousa@372: * VAR msousa@372: * myint1 : myint1_t; msousa@372: * myint2 : myint2_t; msousa@372: * myint3 : myint3_t; msousa@372: * END_VAR msousa@372: * msousa@372: * If we ask for typeid of myint3, it must return myint3_t msousa@372: * If we ask for basetypeid of myint3, it must return int msousa@372: * msousa@372: * When determining the data type of myint3, we will recursively go all the way msousa@372: * down to int, but we must still only store myint3_t as the base type id. msousa@372: */ msousa@372: if (NULL == this->current_typeid) msousa@372: this->current_typeid = type_name; msousa@372: this->current_basetypeid = type_name; msousa@371: mario@181: /* look up the type declaration... */ mario@181: symbol_c *fb_decl = function_block_type_symtable.find_value(type_name); mario@181: if (fb_decl != function_block_type_symtable.end_value()) mario@181: /* Type declaration found!! */ mario@181: return fb_decl->accept(*this); mario@181: mario@181: /* No. It is not a function block, so we let mario@181: * the base class take care of it... mario@181: */ msousa@371: return search_base_type_c::visit(type_name); mario@181: } mario@181: mario@181: /********************************/ mario@181: /* B 1.3.3 - Derived data types */ mario@181: /********************************/ mario@181: mario@181: /* identifier ':' array_spec_init */ mario@181: void *search_varfb_instance_type_c::visit(array_type_declaration_c *symbol) { mario@181: return symbol->array_spec_init->accept(*this); mario@181: } mario@181: laurent@238: /* array_specification [ASSIGN array_initialization] */ mario@181: /* array_initialization may be NULL ! */ mario@181: void *search_varfb_instance_type_c::visit(array_spec_init_c *symbol) { mario@181: return symbol->array_specification->accept(*this); mario@181: } laurent@238: mario@181: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ mario@181: void *search_varfb_instance_type_c::visit(array_specification_c *symbol) { laurent@235: this->is_complex = true; laurent@377: this->current_typeid = symbol; mario@181: return symbol->non_generic_type_name->accept(*this); mario@181: } mario@181: mario@181: /* structure_type_name ':' structure_specification */ msousa@371: /* NOTE: this is only used inside a TYPE ... END_TYPE declaration. msousa@371: * It is never used directly when declaring a new variable! msousa@371: */ mario@181: void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) { laurent@235: this->is_complex = true; msousa@371: msousa@371: if (NULL == current_structelement_name) ERROR; mario@181: return symbol->structure_specification->accept(*this); 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: } mario@181: Laurent@412: /* var1_list ':' structure_type_name */ Laurent@412: void *search_varfb_instance_type_c::visit(structured_var_declaration_c *symbol) { Laurent@412: this->is_complex = true; Laurent@412: if (NULL != current_structelement_name) ERROR; Laurent@412: Laurent@412: /* make sure that we have decomposed all structure elements of the variable name */ Laurent@412: symbol_c *var_name = decompose_var_instance_name->next_part(); Laurent@412: if (NULL == var_name) { Laurent@412: /* this is it... ! Laurent@412: * No need to look any further... Laurent@412: * Note also that, unlike for the struct types, a function block may Laurent@412: * not be defined based on another (i.e. no inheritance is allowed), Laurent@412: * so this function block is already the most base type. Laurent@412: * We simply return it. Laurent@412: */ Laurent@412: return (void *)symbol; Laurent@412: } Laurent@412: Laurent@412: /* reset current_type_id because of new structure element part */ Laurent@412: this->current_typeid = NULL; Laurent@412: Laurent@412: /* look for the var_name in the structure declaration */ Laurent@412: current_structelement_name = var_name; Laurent@412: Laurent@412: /* recursively find out the data type of current_structelement_name... */ Laurent@412: return symbol->structure_type_name->accept(*this); Laurent@412: } Laurent@412: 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) msousa@371: /* NOTE: only the initialized structure is ever used when declaring a new variable instance */ mario@181: void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol) { laurent@226: this->is_complex = true; msousa@371: if (NULL != current_structelement_name) ERROR; msousa@371: laurent@382: /* make sure that we have decomposed all structure elements of the variable name */ laurent@382: symbol_c *var_name = decompose_var_instance_name->next_part(); laurent@382: if (NULL == var_name) { laurent@382: /* this is it... ! laurent@382: * No need to look any further... laurent@382: * Note also that, unlike for the struct types, a function block may laurent@382: * not be defined based on another (i.e. no inheritance is allowed), laurent@382: * so this function block is already the most base type. laurent@382: * We simply return it. laurent@382: */ laurent@382: return (void *)symbol; laurent@382: } laurent@382: laurent@382: /* reset current_type_id because of new structure element part */ laurent@382: this->current_typeid = NULL; laurent@382: laurent@382: /* look for the var_name in the structure declaration */ laurent@382: current_structelement_name = var_name; laurent@382: laurent@382: /* recursively find out the data type of current_structelement_name... */ laurent@382: return symbol->structure_type_name->accept(*this); laurent@382: } laurent@382: laurent@382: /* helper symbol for structure_declaration */ laurent@382: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ laurent@382: /* structure_element_declaration_list structure_element_declaration ';' */ laurent@382: void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol) { laurent@382: if (NULL == current_structelement_name) ERROR; laurent@382: /* now search the structure declaration */ laurent@382: return visit_list(symbol); laurent@382: } laurent@382: laurent@382: /* structure_element_name ':' spec_init */ laurent@382: void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) { laurent@382: if (NULL == current_structelement_name) ERROR; laurent@382: laurent@382: if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) { laurent@382: current_structelement_name = NULL; laurent@382: /* found the type of the element we were looking for! */ laurent@382: return symbol->spec_init->accept(*this); laurent@382: } laurent@382: laurent@382: /* Did not find the type of the element we were looking for! */ laurent@382: /* Will keep looking... */ laurent@382: return NULL; laurent@382: } laurent@382: laurent@382: /* helper symbol for structure_initialization */ laurent@382: /* structure_initialization: '(' structure_element_initialization_list ')' */ laurent@382: /* structure_element_initialization_list ',' structure_element_initialization */ laurent@382: void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ laurent@382: /* structure_element_name ASSIGN value */ laurent@382: void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ laurent@382: laurent@382: laurent@382: laurent@382: /**************************************/ laurent@382: /* B.1.5 - Program organization units */ laurent@382: /**************************************/ laurent@382: /*****************************/ laurent@382: /* B 1.5.2 - Function Blocks */ laurent@382: /*****************************/ laurent@382: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ laurent@382: // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) laurent@382: void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) { laurent@382: /* make sure that we have decomposed all structure elements of the variable name */ mario@181: symbol_c *var_name = decompose_var_instance_name->next_part(); mario@181: if (NULL == var_name) { mario@181: /* this is it... ! mario@181: * No need to look any further... mario@181: * Note also that, unlike for the struct types, a function block may mario@181: * not be defined based on another (i.e. no inheritance is allowed), mario@181: * so this function block is already the most base type. mario@181: * We simply return it. mario@181: */ mario@181: return (void *)symbol; mario@181: } mario@181: laurent@382: /* reset current_type_id because of new structure element part */ laurent@382: this->current_typeid = NULL; msousa@371: mario@181: /* now search the function block declaration for the variable... */ mario@181: search_var_instance_decl_c search_decl(symbol); mario@181: symbol_c *var_decl = search_decl.get_decl(var_name); mario@181: if (NULL == var_decl) { mario@181: /* variable instance declaration not found! */ mario@181: return NULL; mario@181: } msousa@371: #if 0 mario@181: /* We have found the declaration. mario@181: * Should we look any further? mario@181: */ mario@181: var_name = decompose_var_instance_name->next_part(); mario@181: if (NULL == var_name) { mario@181: /* this is it... ! */ mario@181: return base_type(var_decl); mario@181: } mario@181: mario@181: current_structelement_name = var_name; mario@181: /* recursively find out the data type of var_name... */ mario@181: return symbol->var_declarations->accept(*this); laurent@382: #endif msousa@371: /* carry on recursively, in case the variable has more elements to be decomposed... */ msousa@371: return var_decl->accept(*this); msousa@371: }