msousa@417: /*
msousa@417: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@417: *
msousa@417: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt)
msousa@417: * Copyright (C) 2012 Manuele Conti (manuele.conti@sirius-es.it)
msousa@417: * Copyright (C) 2012 Matteo Facchinetti (matteo.facchinetti@sirius-es.it)
msousa@417: *
msousa@417: * This program is free software: you can redistribute it and/or modify
msousa@417: * it under the terms of the GNU General Public License as published by
msousa@417: * the Free Software Foundation, either version 3 of the License, or
msousa@417: * (at your option) any later version.
msousa@417: *
msousa@417: * This program is distributed in the hope that it will be useful,
msousa@417: * but WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@417: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
msousa@417: * GNU General Public License for more details.
msousa@417: *
msousa@417: * You should have received a copy of the GNU General Public License
msousa@417: * along with this program. If not, see .
msousa@417: *
msousa@417: *
msousa@417: * This code is made available on the understanding that it will not be
msousa@417: * used in safety-critical situations without a full and competent review.
msousa@417: */
msousa@417:
msousa@417: /*
msousa@417: * An IEC 61131-3 compiler.
msousa@417: *
msousa@417: * Based on the
msousa@417: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
msousa@417: *
msousa@417: */
msousa@417:
msousa@417:
msousa@541: /* TODO - things yet not checked by this data type checker...
msousa@541: *
msousa@541: * - check variable declarations
msousa@541: * - check data type declarations
msousa@541: * - check inside configurations (variable declarations)
msousa@541: * - check SFC code
msousa@541: * - must fix S and R IL functions (includes potientialy fixing stage4 code!)
msousa@541: */
msousa@541:
msousa@541:
msousa@552: /* NOTE: The algorithm implemented here assumes that flow control analysis has already been completed!
msousa@552: * BEFORE running this visitor, be sure to CALL the flow_control_analysis_c visitor!
msousa@552: */
msousa@552:
msousa@552:
msousa@417: /*
msousa@552: * Fill the candidate datatype list for all symbols that may legally 'have' a data type (e.g. variables, literals, function calls, expressions, etc.)
msousa@552: *
msousa@552: * The candidate datatype list will be filled with a list of all the data types that expression may legally take.
msousa@552: * For example, the very simple literal '0' (as in foo := 0), may represent a:
msousa@552: * BOOL, BYTE, WORD, DWORD, LWORD, USINT, SINT, UINT, INT, UDINT, DINT, ULINT, LINT (as well as the SAFE versions of these data tyes too!)
msousa@719: *
msousa@719: * WARNING: This visitor class starts off by building a map of all enumeration constants that are defined in the source code (i.e. a library_c symbol),
msousa@719: * and this map is later used to determine the datatpe of each use of an enumeration constant. By implication, the fill_candidate_datatypes_c
msousa@719: * visitor class will only work corretly if it is asked to visit a symbol of class library_c!!
msousa@417: */
msousa@417:
msousa@604: #include <../main.hh> /* required for UINT64_MAX, INT64_MAX, INT64_MIN, ... */
msousa@417: #include "fill_candidate_datatypes.hh"
msousa@417: #include "datatype_functions.hh"
msousa@417: #include
msousa@417: #include
msousa@417: #include
msousa@417: #include
msousa@417: #include
msousa@417:
msousa@612: #define GET_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.value)
msousa@612: #define VALID_CVALUE(dtype, symbol) (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status)
msousa@612: #define IS_OVERFLOW(dtype, symbol) (symbol_c::cs_overflow == (symbol)->const_value._##dtype.status)
conti@603:
msousa@724:
msousa@724:
msousa@724:
msousa@417: /* set to 1 to see debug info during execution */
msousa@454: static int debug = 0;
msousa@417:
msousa@716:
msousa@716:
msousa@716: /*****************************************************/
msousa@716: /* */
msousa@716: /* A small helper class... */
msousa@716: /* */
msousa@716: /*****************************************************/
msousa@716:
msousa@719: /* Add to the global_enumerated_value_symtable the global enum value constants, i.e. the enum constants used in the enumerated
msousa@719: * datatypes that are defined inside a TYPE ... END_TYPE declaration.
msousa@719: */
msousa@724: /* NOTE: we do not store any NULL values in this symbol table, so we can safely use NULL and the null value. */
msousa@724:
msousa@724: symbol_c null_enumvalue_symbol; /* cannot be static, so it may be used in the template!! */
msousa@724: typedef dsymtable_c enumerated_value_symtable_t;
msousa@724: static enumerated_value_symtable_t global_enumerated_value_symtable;
msousa@719:
msousa@719:
msousa@719: class populate_globalenumvalue_symtable_c: public iterator_visitor_c {
msousa@719: private:
msousa@719: symbol_c *current_enumerated_type;
msousa@719:
msousa@719: public:
msousa@719: populate_globalenumvalue_symtable_c(void) {current_enumerated_type = NULL;};
msousa@719: ~populate_globalenumvalue_symtable_c(void) {}
msousa@719:
msousa@719: public:
msousa@719: /*************************/
msousa@719: /* B.1 - Common elements */
msousa@719: /*************************/
msousa@719: /**********************/
msousa@719: /* B.1.3 - Data types */
msousa@719: /**********************/
msousa@719: /********************************/
msousa@719: /* B 1.3.3 - Derived data types */
msousa@719: /********************************/
msousa@719: /* enumerated_type_name ':' enumerated_spec_init */
msousa@719: void *visit(enumerated_type_declaration_c *symbol) {
msousa@719: //current_enumerated_type = symbol->enumerated_type_name;
msousa@719: current_enumerated_type = symbol;
msousa@719: symbol->enumerated_spec_init->accept(*this);
msousa@719: current_enumerated_type = NULL;
msousa@719: return NULL;
msousa@719: }
msousa@719:
msousa@719: /* enumerated_specification ASSIGN enumerated_value */
msousa@719: void *visit(enumerated_spec_init_c *symbol) {
msousa@719: return symbol->enumerated_specification->accept(*this);
msousa@719: }
msousa@719:
msousa@719: /* [enumerated_type_name '#'] identifier */
msousa@719: void *visit(enumerated_value_c *symbol) {
msousa@719: if (current_enumerated_type == NULL) ERROR;
msousa@719: if (symbol->type != NULL) ERROR;
msousa@719:
msousa@724: enumerated_value_symtable_t::iterator lower = global_enumerated_value_symtable.lower_bound(symbol->value);
msousa@724: enumerated_value_symtable_t::iterator upper = global_enumerated_value_symtable.upper_bound(symbol->value);
msousa@724: for (; lower != upper; lower++)
msousa@724: if (lower->second == current_enumerated_type) {
msousa@724: /* The same identifier is used more than once as an enumerated value/constant inside the same enumerated datat type! */
msousa@724: return NULL; /* No need to insert it! It is already in the table! */
msousa@724: }
msousa@719:
msousa@720: global_enumerated_value_symtable.insert(symbol->value, current_enumerated_type);
msousa@719: return NULL;
msousa@719: }
msousa@719:
msousa@719: /**************************************/
msousa@719: /* B.1.5 - Program organization units */
msousa@719: /**************************************/
msousa@719: /* B 1.5.1 - Functions */
msousa@719: void *visit(function_declaration_c *symbol) {return NULL;}
msousa@719: /* B 1.5.2 - Function Blocks */
msousa@719: void *visit(function_block_declaration_c *symbol) {return NULL;}
msousa@719: /* B 1.5.3 - Programs */
msousa@719: void *visit(program_declaration_c *symbol) {return NULL;}
msousa@719:
msousa@719: }; /* populate_globalenumvalue_symtable_c */
msousa@719:
msousa@719: static populate_globalenumvalue_symtable_c populate_globalenumvalue_symtable;
msousa@719:
msousa@719:
msousa@719: /*****************************************************/
msousa@719: /* */
msousa@719: /* A small helper class... */
msousa@719: /* */
msousa@719: /*****************************************************/
msousa@719:
msousa@716: /* Add to the local_enumerated_value_symtable the local enum value constants */
msousa@716: /* Notes:
msousa@716: * Some enumerations are
msousa@716: * (A) declared anonymously inside a VAR ... END_VAR declaration
msousa@716: * (e.g. VAR enum_var : (enumvalue1, enumvalue2); END_VAR)
msousa@716: * while others are
msousa@716: * (B) declared (with a name) inside a TYPE .. END_TYPE declaration.
msousa@716: *
msousa@716: * Values in (A) are added to the enumerated_value_symtable in absyntaxt_utils.cc.
msousa@716: * Values in (B) are only in scope inside the POU with the VAR END_VAR declaration.
msousa@716: *
msousa@716: * This class will add the enum values in (B) to the local_enumerated_value_symtable.
msousa@716: *
msousa@720: * If a locally defined enum value is identical to another locally defined enum_value, a
msousa@720: * duplicate entry is created.
msousa@716: * However, if a locally defined enum value is identical to another globally defined enum_value, the
msousa@720: * corresponding entry in local_enumerated_value_symtable is also set to the local datatype.
msousa@716: * This is because anonynous locally feined enum datatypes are anonymous, and its enum values cannot therefore
msousa@716: * be disambiguated using EnumType#enum_value (since the enum type does not have a name, it is anonymous!).
msousa@716: * For this reason we implement the semantics where locally defined enum values, when in scope, will 'cover'
msousa@716: * the globally defined enum value with the same name/identifier.
msousa@716: * For example:
msousa@716: *
msousa@716: * TYPE GlobalEnumT: (xxx1, xxx2, xxx3) END_TYPE
msousa@716: *
msousa@716: * FUNCTION_BLOCK FOO
msousa@716: * VAR_INPUT
msousa@716: * GlobalEnumVar: GlobalEnumT;
msousa@716: * LocalEnumVar : (xxx1, yyy2, yyy3);
msousa@716: * END_VAR
msousa@716: * LocalEnumVar := xxx1; <-- We consider it OK!!! xxx1 will reference the anonymous type used for LocalEnumVar
msousa@716: * GlobalEnumVar := xxx1; <-- We consider it an error. xxx1 will reference the anonymous type used for LocalEnumVar
msousa@716: * GlobalEnumVar := GlobalEnumT#xxx1;
msousa@716: * END_FUNCTION_BLOCK
msousa@716: */
msousa@716:
msousa@724: static enumerated_value_symtable_t local_enumerated_value_symtable;
msousa@716:
msousa@716:
msousa@738: class populate_localenumvalue_symtable_c: public iterator_visitor_c {
msousa@716: private:
msousa@716: symbol_c *current_enumerated_type;
msousa@716:
msousa@716: public:
msousa@738: populate_localenumvalue_symtable_c(void) {current_enumerated_type = NULL;};
msousa@738: ~populate_localenumvalue_symtable_c(void) {}
msousa@716:
msousa@716: public:
msousa@716: /*************************/
msousa@716: /* B.1 - Common elements */
msousa@716: /*************************/
msousa@716: /**********************/
msousa@716: /* B.1.3 - Data types */
msousa@716: /**********************/
msousa@716: /********************************/
msousa@716: /* B 1.3.3 - Derived data types */
msousa@716: /********************************/
msousa@719: /* TYPE type_declaration_list END_TYPE */
msousa@719: void *visit(data_type_declaration_c *symbol) {return NULL;} // do not visit the type declarations!!
msousa@719:
msousa@716: /* enumerated_specification ASSIGN enumerated_value */
msousa@716: void *visit(enumerated_spec_init_c *symbol) {
msousa@716: current_enumerated_type = symbol;
msousa@716: symbol->enumerated_specification->accept(*this);
msousa@716: /* DO NOT visit the symbol->enumerated_value !!! */
msousa@716: current_enumerated_type = NULL;
msousa@716: return NULL;
msousa@716: }
msousa@716:
msousa@716: /* [enumerated_type_name '#'] identifier */
msousa@716: void *visit(enumerated_value_c *symbol) {
msousa@716: /* if the enumerated_value_c is not inside a enumerated_spec_init_c (e.g. used as the inital value of a variable), we simply return */
msousa@716: if (current_enumerated_type == NULL) return NULL;
msousa@716: /* this is really an ERROR! The initial value may use the syntax NUM_TYPE#enum_value, but in that case we should have return'd in the above statement !! */
msousa@716: if (symbol->type != NULL) ERROR;
msousa@716:
msousa@724: enumerated_value_symtable_t::iterator lower = local_enumerated_value_symtable.lower_bound(symbol->value);
msousa@724: enumerated_value_symtable_t::iterator upper = local_enumerated_value_symtable.upper_bound(symbol->value);
msousa@724: for (; lower != upper; lower++)
msousa@724: if (lower->second == current_enumerated_type) {
msousa@724: /* The same identifier is used more than once as an enumerated value/constant inside the same enumerated datat type! */
msousa@724: return NULL; /* No need to insert it! It is already in the table! */
msousa@724: }
msousa@720:
msousa@720: /* add it to the local symbol table. */
msousa@720: local_enumerated_value_symtable.insert(symbol->value, current_enumerated_type);
msousa@716: return NULL;
msousa@716: }
msousa@716: }; // class populate_enumvalue_symtable_c
msousa@716:
msousa@738: static populate_localenumvalue_symtable_c populate_enumvalue_symtable;
msousa@716:
msousa@716:
msousa@716:
msousa@716:
msousa@716: /*****************************************************/
msousa@716: /* */
msousa@716: /* Main FILL candidate datatypes algorithm... */
msousa@716: /* */
msousa@716: /*****************************************************/
msousa@716:
msousa@716:
msousa@417: fill_candidate_datatypes_c::fill_candidate_datatypes_c(symbol_c *ignore) {
conti@661: il_operand = NULL;
conti@661: prev_il_instruction = NULL;
conti@661: search_varfb_instance_type = NULL;
msousa@717: current_enumerated_spec_type = NULL;
msousa@417: }
msousa@417:
msousa@417: fill_candidate_datatypes_c::~fill_candidate_datatypes_c(void) {
msousa@417: }
msousa@417:
msousa@719:
msousa@719:
msousa@719:
msousa@719:
msousa@719:
msousa@719:
msousa@478: symbol_c *fill_candidate_datatypes_c::widening_conversion(symbol_c *left_type, symbol_c *right_type, const struct widen_entry widen_table[]) {
msousa@417: int k;
msousa@417: /* find a widening table entry compatible */
msousa@417: for (k = 0; NULL != widen_table[k].left; k++)
msousa@478: if ((typeid(*left_type) == typeid(*widen_table[k].left)) && (typeid(*right_type) == typeid(*widen_table[k].right)))
conti@476: return widen_table[k].result;
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@421:
msousa@465: /* add a data type to a candidate data type list, while guaranteeing no duplicate entries! */
msousa@465: /* Returns true if it really did add the datatype to the list, or false if it was already present in the list! */
msousa@465: bool fill_candidate_datatypes_c::add_datatype_to_candidate_list(symbol_c *symbol, symbol_c *datatype) {
msousa@478: /* If it is an invalid data type, do not insert!
msousa@478: * NOTE: it reduces overall code size to do this test here, instead of doing every time before calling the add_datatype_to_candidate_list() function.
msousa@478: */
msousa@676: if (!get_datatype_info_c::is_type_valid(datatype)) /* checks for NULL and invalid_type_name_c */
msousa@478: return false;
msousa@478:
msousa@465: if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) >= 0)
msousa@465: /* already in the list, Just return! */
msousa@465: return false;
msousa@465:
msousa@465: /* not yet in the candidate data type list, so we insert it now! */
msousa@465: symbol->candidate_datatypes.push_back(datatype);
msousa@465: return true;
msousa@465: }
msousa@465:
msousa@465:
msousa@472: bool fill_candidate_datatypes_c::add_2datatypes_to_candidate_list(symbol_c *symbol, symbol_c *datatype1, symbol_c *datatype2) {
msousa@472: add_datatype_to_candidate_list(symbol, datatype1);
msousa@472: add_datatype_to_candidate_list(symbol, datatype2);
msousa@472: return true;
msousa@472: }
conti@603:
msousa@607:
msousa@607:
conti@603: void fill_candidate_datatypes_c::remove_incompatible_datatypes(symbol_c *symbol) {
msousa@607: #ifdef __REMOVE__
msousa@607: #error __REMOVE__ macro already exists. Choose another name!
msousa@607: #endif
msousa@607: #define __REMOVE__(datatype)\
msousa@693: remove_from_candidate_datatype_list(&get_datatype_info_c::datatype, symbol->candidate_datatypes);\
msousa@693: remove_from_candidate_datatype_list(&get_datatype_info_c::safe##datatype, symbol->candidate_datatypes);
msousa@607:
msousa@607: {/* Remove unsigned data types */
msousa@607: uint64_t value = 0;
msousa@607: if (VALID_CVALUE( uint64, symbol)) value = GET_CVALUE(uint64, symbol);
msousa@607: if (IS_OVERFLOW ( uint64, symbol)) value = (uint64_t)UINT32_MAX + (uint64_t)1;
msousa@607:
msousa@607: if (value > 1 ) {__REMOVE__(bool_type_name);}
msousa@607: if (value > UINT8_MAX ) {__REMOVE__(usint_type_name); __REMOVE__( byte_type_name);}
msousa@607: if (value > UINT16_MAX ) {__REMOVE__( uint_type_name); __REMOVE__( word_type_name);}
msousa@607: if (value > UINT32_MAX ) {__REMOVE__(udint_type_name); __REMOVE__(dword_type_name);}
msousa@607: if (IS_OVERFLOW( uint64, symbol)) {__REMOVE__(ulint_type_name); __REMOVE__(lword_type_name);}
conti@603: }
msousa@607:
msousa@607: {/* Remove signed data types */
msousa@607: int64_t value = 0;
msousa@607: if (VALID_CVALUE( int64, symbol)) value = GET_CVALUE(int64, symbol);
msousa@607: if (IS_OVERFLOW ( int64, symbol)) value = (int64_t)INT32_MAX + (int64_t)1;
msousa@607:
msousa@609: if ((value < INT8_MIN) || (value > INT8_MAX)) {__REMOVE__(sint_type_name);}
msousa@609: if ((value < INT16_MIN) || (value > INT16_MAX)) {__REMOVE__( int_type_name);}
msousa@609: if ((value < INT32_MIN) || (value > INT32_MAX)) {__REMOVE__(dint_type_name);}
msousa@609: if (IS_OVERFLOW( int64, symbol)) {__REMOVE__(lint_type_name);}
conti@603: }
msousa@607:
msousa@607: {/* Remove floating point data types */
msousa@607: real64_t value = 0;
msousa@607: if (VALID_CVALUE( real64, symbol)) value = GET_CVALUE(real64, symbol);
msousa@610: if (IS_OVERFLOW ( real64, symbol)) value = (real64_t)REAL32_MAX + (real64_t)1;
msousa@607: if (value > REAL32_MAX ) {__REMOVE__( real_type_name);}
msousa@607: if (value < -REAL32_MAX ) {__REMOVE__( real_type_name);}
msousa@607: if (IS_OVERFLOW( real64, symbol)) {__REMOVE__(lreal_type_name);}
conti@603: }
msousa@607: #undef __REMOVE__
conti@603: }
msousa@465:
msousa@421:
msousa@420: /* returns true if compatible function/FB invocation, otherwise returns false */
msousa@424: /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */
msousa@443: /*
msousa@443: * All parameters being passed to the called function MUST be in the parameter list to which f_call points to!
msousa@443: * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the
msousa@443: * beginning of the parameter list BEFORE calling handle_function_call().
msousa@443: */
msousa@420: bool fill_candidate_datatypes_c::match_nonformal_call(symbol_c *f_call, symbol_c *f_decl) {
msousa@449: symbol_c *call_param_value, *param_datatype;
msousa@417: identifier_c *param_name;
msousa@417: function_param_iterator_c fp_iterator(f_decl);
msousa@417: function_call_param_iterator_c fcp_iterator(f_call);
msousa@417: int extensible_parameter_highest_index = -1;
msousa@417: unsigned int i;
msousa@417:
msousa@417: /* Iterating through the non-formal parameters of the function call */
msousa@417: while((call_param_value = fcp_iterator.next_nf()) != NULL) {
msousa@417: /* Iterate to the next parameter of the function being called.
msousa@417: * Get the name of that parameter, and ignore if EN or ENO.
msousa@417: */
msousa@417: do {
msousa@417: param_name = fp_iterator.next();
msousa@417: /* If there is no other parameter declared, then we are passing too many parameters... */
msousa@420: if(param_name == NULL) return false;
msousa@417: } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
msousa@417:
msousa@417: /* Get the parameter type */
msousa@449: param_datatype = base_type(fp_iterator.param_type());
msousa@420:
msousa@420: /* check whether one of the candidate_data_types of the value being passed is the same as the param_type */
msousa@449: if (search_in_candidate_datatype_list(param_datatype, call_param_value->candidate_datatypes) < 0)
msousa@442: return false; /* return false if param_type not in the list! */
msousa@420: }
msousa@420: /* call is compatible! */
msousa@420: return true;
msousa@420: }
msousa@420:
msousa@421:
msousa@421:
msousa@420: /* returns true if compatible function/FB invocation, otherwise returns false */
msousa@424: /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */
msousa@455: bool fill_candidate_datatypes_c::match_formal_call(symbol_c *f_call, symbol_c *f_decl, symbol_c **first_param_datatype) {
msousa@449: symbol_c *call_param_value, *call_param_name, *param_datatype;
msousa@417: symbol_c *verify_duplicate_param;
msousa@417: identifier_c *param_name;
msousa@417: function_param_iterator_c fp_iterator(f_decl);
msousa@417: function_call_param_iterator_c fcp_iterator(f_call);
msousa@417: int extensible_parameter_highest_index = -1;
msousa@417: identifier_c *extensible_parameter_name;
msousa@417: unsigned int i;
msousa@455: bool is_first_param = true;
msousa@417:
msousa@417: /* Iterating through the formal parameters of the function call */
msousa@417: while((call_param_name = fcp_iterator.next_f()) != NULL) {
msousa@417: /* Obtaining the value being passed in the function call */
msousa@417: call_param_value = fcp_iterator.get_current_value();
msousa@417: /* the following should never occur. If it does, then we have a bug in our code... */
msousa@417: if (NULL == call_param_value) ERROR;
msousa@417:
msousa@449: /* Obtaining the assignment direction: := (assign_in) or => (assign_out) */
msousa@449: function_call_param_iterator_c::assign_direction_t call_param_dir = fcp_iterator.get_assign_direction();
msousa@449:
msousa@417: /* Checking if there are duplicated parameter values */
msousa@417: verify_duplicate_param = fcp_iterator.search_f(call_param_name);
msousa@417: if(verify_duplicate_param != call_param_value)
msousa@420: return false;
msousa@417:
msousa@417: /* Obtaining the type of the value being passed in the function call */
msousa@417: std::vector &call_param_types = call_param_value->candidate_datatypes;
msousa@417:
msousa@417: /* Find the corresponding parameter in function declaration */
msousa@417: param_name = fp_iterator.search(call_param_name);
msousa@421: if(param_name == NULL) return false;
msousa@449: /* Get the parameter data type */
msousa@449: param_datatype = base_type(fp_iterator.param_type());
msousa@449: /* Get the parameter direction: IN, OUT, IN_OUT */
msousa@449: function_param_iterator_c::param_direction_t param_dir = fp_iterator.param_direction();
msousa@449:
msousa@449: /* check whether direction (IN, OUT, IN_OUT) and assignment types (:= , =>) are compatible !!! */
msousa@449: if (function_call_param_iterator_c::assign_in == call_param_dir) {
msousa@449: if ((function_param_iterator_c::direction_in != param_dir) &&
msousa@449: (function_param_iterator_c::direction_inout != param_dir))
msousa@449: return false;
msousa@449: } else if (function_call_param_iterator_c::assign_out == call_param_dir) {
msousa@449: if ((function_param_iterator_c::direction_out != param_dir))
msousa@449: return false;
msousa@449: } else ERROR;
msousa@449:
msousa@421: /* check whether one of the candidate_data_types of the value being passed is the same as the param_type */
msousa@449: if (search_in_candidate_datatype_list(param_datatype, call_param_types) < 0)
msousa@442: return false; /* return false if param_type not in the list! */
msousa@455:
msousa@455: /* If this is the first parameter, then copy the datatype to *first_param_datatype */
msousa@455: if (is_first_param)
msousa@455: if (NULL != first_param_datatype)
msousa@455: *first_param_datatype = param_datatype;
msousa@455: is_first_param = false;
msousa@421: }
msousa@421: /* call is compatible! */
msousa@420: return true;
msousa@417: }
msousa@417:
msousa@421:
msousa@421:
msousa@421:
msousa@438: /* Handle a generic function call!
msousa@438: * Assumes that the parameter_list containing the values being passed in this function invocation
msousa@438: * has already had all the candidate_datatype lists filled in!
msousa@438: *
msousa@438: * All parameters being passed to the called function MUST be in the parameter list to which f_call points to!
msousa@438: * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the
msousa@438: * beginning of the parameter list BEFORE calling handle_function_call().
msousa@438: */
msousa@438: /*
msousa@438: typedef struct {
msousa@438: symbol_c *function_name,
msousa@438: symbol_c *nonformal_operand_list,
msousa@438: symbol_c * formal_operand_list,
msousa@438:
msousa@438: std::vector &candidate_functions,
msousa@438: symbol_c &*called_function_declaration,
msousa@438: int &extensible_param_count
msousa@438: } generic_function_call_t;
msousa@438: */
msousa@438: /*
msousa@438: void narrow_candidate_datatypes_c::narrow_function_invocation(symbol_c *fcall, generic_function_call_t fcall_data) {
msousa@438: void *fill_candidate_datatypes_c::handle_function_call(symbol_c *f_call, symbol_c *function_name, invocation_type_t invocation_type,
msousa@438: std::vector *candidate_datatypes,
msousa@438: std::vector *candidate_functions) {
msousa@438: */
msousa@438: void fill_candidate_datatypes_c::handle_function_call(symbol_c *fcall, generic_function_call_t fcall_data) {
msousa@438: function_declaration_c *f_decl;
msousa@438: list_c *parameter_list;
msousa@438: list_c *parameter_candidate_datatypes;
msousa@438: symbol_c *returned_parameter_type;
msousa@438:
msousa@438: if (debug) std::cout << "function()\n";
msousa@438:
msousa@438: function_symtable_t::iterator lower = function_symtable.lower_bound(fcall_data.function_name);
msousa@438: function_symtable_t::iterator upper = function_symtable.upper_bound(fcall_data.function_name);
msousa@438: /* If the name of the function being called is not found in the function symbol table, then this is an invalid call */
msousa@438: /* Since the lexical parser already checks for this, then if this occurs then we have an internal compiler error. */
msousa@438: if (lower == function_symtable.end()) ERROR;
msousa@438:
msousa@438: /* Look for all compatible function declarations, and add their return datatypes
msousa@438: * to the candidate_datatype list of this function invocation.
msousa@438: *
msousa@438: * If only one function exists, we add its return datatype to the candidate_datatype list,
msousa@438: * even if the parameters passed to it are invalid.
msousa@438: * This guarantees that the remainder of the expression in which the function call is inserted
msousa@438: * is treated as if the function call returns correctly, and therefore does not generate
msousa@438: * spurious error messages.
msousa@438: * Even if the parameters to the function call are invalid, doing this is still safe, as the
msousa@438: * expressions inside the function call will themselves have erros and will guarantee that
msousa@438: * compilation is aborted in stage3 (in print_datatypes_error_c).
msousa@438: */
msousa@720: if (function_symtable.count(fcall_data.function_name) == 1) {
msousa@438: f_decl = function_symtable.get_value(lower);
msousa@438: returned_parameter_type = base_type(f_decl->type_name);
msousa@465: if (add_datatype_to_candidate_list(fcall, returned_parameter_type))
msousa@465: /* we only add it to the function declaration list if this entry was not already present in the candidate datatype list! */
msousa@465: fcall_data.candidate_functions.push_back(f_decl);
msousa@465:
msousa@438: }
msousa@438: for(; lower != upper; lower++) {
msousa@438: bool compatible = false;
msousa@438:
msousa@438: f_decl = function_symtable.get_value(lower);
msousa@438: /* Check if function declaration in symbol_table is compatible with parameters */
msousa@438: if (NULL != fcall_data.nonformal_operand_list) compatible=match_nonformal_call(fcall, f_decl);
msousa@438: if (NULL != fcall_data. formal_operand_list) compatible= match_formal_call(fcall, f_decl);
msousa@438: if (compatible) {
msousa@438: /* Add the data type returned by the called functions.
msousa@438: * However, only do this if this data type is not already present in the candidate_datatypes list_c
msousa@438: */
msousa@465: returned_parameter_type = base_type(f_decl->type_name);
msousa@465: if (add_datatype_to_candidate_list(fcall, returned_parameter_type))
msousa@465: /* we only add it to the function declaration list if this entry was not already present in the candidate datatype list! */
msousa@438: fcall_data.candidate_functions.push_back(f_decl);
msousa@438: }
msousa@438: }
msousa@438: if (debug) std::cout << "end_function() [" << fcall->candidate_datatypes.size() << "] result.\n";
msousa@438: return;
msousa@438: }
msousa@438:
msousa@438:
msousa@447: /* handle implicit FB call in IL.
msousa@448: * e.g. CLK ton_var
msousa@447: * CU counter_var
msousa@447: */
msousa@489: void *fill_candidate_datatypes_c::handle_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) {
mjsousa@834: symbol_c *fb_decl = (NULL == il_operand)? NULL : search_varfb_instance_type->get_basetype_decl(il_operand);
mjsousa@834: if (! get_datatype_info_c::is_function_block(fb_decl)) fb_decl = NULL;
mjsousa@834:
mjsousa@834: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
mjsousa@834: /* However, when calling using the 'S' and 'R' operators, this error is not caught by stage 2, as these operators have two possible semantics */
mjsousa@834: // if (NULL == fb_type_id) ERROR;
mjsousa@834:
msousa@455: /* The narrow_candidate_datatypes_c does not rely on this called_fb_declaration pointer being == NULL to conclude that
msousa@455: * we have a datatype incompatibility error, so we set it to fb_decl to allow the print_datatype_error_c to print out
msousa@455: * more informative error messages!
msousa@450: */
msousa@455: called_fb_declaration = fb_decl;
msousa@455:
msousa@455: /* This implicit FB call does not change the value stored in the current/default IL variable */
msousa@456: /* It does, however, require that the datatype be compatible with the input parameter of the FB being called.
msousa@456: * If we were to follow the filling & narrowing algorithm correctly (implemented in fill_candidate_datatypes_c
msousa@456: * & narrow_candidate_datatypes_c respectively), we should be restricting the candidate_datatpes to the datatypes
msousa@456: * that are compatible to the FB call.
msousa@456: * However, doing the above will often result in some very confusing error messages for the user, especially in the case
msousa@456: * in which the FB call is wrong, so the resulting cadidate datatypes is an empty list. In this case, the user would see
msousa@456: * many error messages related to the IL instructions that follow the FB call, even though those IL instructions may be perfectly
msousa@456: * correct.
msousa@456: * For now, we will simply let the narrow_candidate_datatypes_c verify if the datatypes are compatible (something that should be done
msousa@456: * here).
msousa@456: */
msousa@455: if (NULL != prev_il_instruction)
msousa@467: il_instruction->candidate_datatypes = prev_il_instruction->candidate_datatypes;
msousa@455:
msousa@455: if (debug) std::cout << "handle_implicit_il_fb_call() [" << prev_il_instruction->candidate_datatypes.size() << "] ==> " << il_instruction->candidate_datatypes.size() << " result.\n";
msousa@489: return NULL;
msousa@447: }
msousa@438:
msousa@438:
msousa@479:
msousa@479:
mjsousa@834:
mjsousa@834: /* handle the S and R IL operators... */
mjsousa@834: /* operator_str should be set to either "S" or "R" */
mjsousa@834: void *fill_candidate_datatypes_c::handle_S_and_R_operator(symbol_c *symbol, const char *operator_str, symbol_c *&called_fb_declaration) {
mjsousa@834: /* NOTE: this operator has two possible semantic meanings:
mjsousa@834: * - Set/Reset the BOOL operand variable to true
mjsousa@834: * - call the FB specified by the operand.
mjsousa@834: * Which of the two semantics will have to be determined by the datatype of the operand!
mjsousa@834: */
mjsousa@834: symbol_c *prev_instruction_type, *operand_type;
mjsousa@834:
mjsousa@834: if (NULL == prev_il_instruction) return NULL;
mjsousa@834: if (NULL == il_operand) return NULL;
mjsousa@834:
mjsousa@837: /* Try the Set/Reset semantics */
mjsousa@834: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
mjsousa@834: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
mjsousa@834: prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
mjsousa@834: operand_type = il_operand->candidate_datatypes[j];
mjsousa@834: /* IEC61131-3, Table 52, Note (e) states that the datatype of the operand must be BOOL!
mjsousa@834: * IEC61131-3, Table 52, line 3 states that this operator should "Set operand to 1 if current result is Boolean 1"
mjsousa@834: * which implies that the prev_instruction_type MUST also be BOOL compatible.
mjsousa@834: */
mjsousa@834: if (get_datatype_info_c::is_BOOL_compatible(prev_instruction_type) && get_datatype_info_c::is_BOOL_compatible(operand_type))
mjsousa@834: add_datatype_to_candidate_list(symbol, prev_instruction_type);
mjsousa@834: }
mjsousa@834: }
mjsousa@834:
mjsousa@834: /* if the appropriate semantics is not a Set/Reset of a boolean variable, the we try for the FB invocation! */
mjsousa@837: if (symbol->candidate_datatypes.size() == 0) {
mjsousa@834: handle_implicit_il_fb_call(symbol, operator_str, called_fb_declaration);
mjsousa@837: /* If it is also not a valid FB call, make sure the candidate_datatypes is empty (handle_implicit_il_fb_call may leave it non-empty!!) */
mjsousa@837: /* From here on out, all later code will consider the symbol->called_fb_declaration being NULL as an indication that this operator must use the
mjsousa@837: * Set/Reset semantics, so we must also guarantee that the remainder of the state of this symbol is compatible with that assumption!
mjsousa@837: */
mjsousa@837: if (NULL == called_fb_declaration)
mjsousa@837: symbol->candidate_datatypes.clear();
mjsousa@837: }
mjsousa@834:
mjsousa@834: if (debug) std::cout << operator_str << " [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
mjsousa@834: return NULL;
mjsousa@834: }
mjsousa@834:
mjsousa@834:
mjsousa@834:
msousa@479: /* handle a binary IL operator, like ADD, SUB, etc... */
msousa@479: void *fill_candidate_datatypes_c::handle_binary_operator(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) {
mjsousa@834: if (NULL == l_expr) return NULL; /* if no prev_il_instruction */
mjsousa@834: if (NULL == r_expr) return NULL; /* if no IL operand!! */
msousa@479:
msousa@479: for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++)
msousa@479: for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++)
msousa@479: /* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
msousa@479: add_datatype_to_candidate_list(symbol, widening_conversion(l_expr->candidate_datatypes[i], r_expr->candidate_datatypes[j], widen_table));
conti@603: remove_incompatible_datatypes(symbol);
msousa@479: if (debug) std::cout << "[" << l_expr->candidate_datatypes.size() << "," << r_expr->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@479: return NULL;
msousa@479: }
msousa@479:
msousa@479:
msousa@652:
msousa@479: /* handle a binary ST expression, like '+', '-', etc... */
msousa@479: void *fill_candidate_datatypes_c::handle_binary_expression(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) {
msousa@479: l_expr->accept(*this);
msousa@479: r_expr->accept(*this);
msousa@479: return handle_binary_operator(widen_table, symbol, l_expr, r_expr);
msousa@479: }
msousa@479:
msousa@479:
msousa@479:
msousa@652: /* handle the two equality comparison operations, i.e. = (euqal) and != (not equal) */
msousa@652: /* This function is special, as it will also allow enumeration data types to be compared, with the result being a BOOL data type!
msousa@652: * This possibility os not expressed in the 'widening' tables, so we need to hard code it here
msousa@652: */
msousa@652: void *fill_candidate_datatypes_c::handle_equality_comparison(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) {
msousa@652: handle_binary_expression(widen_table, symbol, l_expr, r_expr);
msousa@652: for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++)
msousa@652: for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++) {
msousa@854: if ((l_expr->candidate_datatypes[i] == r_expr->candidate_datatypes[j]) && get_datatype_info_c::is_enumerated(l_expr->candidate_datatypes[i]))
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name);
msousa@652: }
msousa@652: return NULL;
msousa@652: }
msousa@652:
msousa@652:
msousa@479:
msousa@417: /* a helper function... */
msousa@417: symbol_c *fill_candidate_datatypes_c::base_type(symbol_c *symbol) {
msousa@718: /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used in the code. */
msousa@417: if (symbol == NULL) return NULL;
msousa@718: return search_base_type_c::get_basetype_decl(symbol);
msousa@417: }
msousa@417:
msousa@719:
msousa@719: /***************************/
msousa@719: /* B 0 - Programming Model */
msousa@719: /***************************/
msousa@719: /* main entry function! */
msousa@719: void *fill_candidate_datatypes_c::visit(library_c *symbol) {
msousa@719: symbol->accept(populate_globalenumvalue_symtable);
msousa@719: /* Now let the base class iterator_visitor_c iterate through all the library elements */
msousa@719: return iterator_visitor_c::visit(symbol);
msousa@719: }
msousa@719:
msousa@719:
msousa@417: /*********************/
msousa@417: /* B 1.2 - Constants */
msousa@417: /*********************/
msousa@417: /******************************/
msousa@417: /* B 1.2.1 - Numeric Literals */
msousa@417: /******************************/
msousa@472: #define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol)
msousa@472:
msousa@472: void *fill_candidate_datatypes_c::handle_any_integer(symbol_c *symbol) {
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name, &get_datatype_info_c::safebool_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::byte_type_name, &get_datatype_info_c::safebyte_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::word_type_name, &get_datatype_info_c::safeword_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::dword_type_name, &get_datatype_info_c::safedword_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::lword_type_name, &get_datatype_info_c::safelword_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::sint_type_name, &get_datatype_info_c::safesint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::int_type_name, &get_datatype_info_c::safeint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::dint_type_name, &get_datatype_info_c::safedint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::lint_type_name, &get_datatype_info_c::safelint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::usint_type_name, &get_datatype_info_c::safeusint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::uint_type_name, &get_datatype_info_c::safeuint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::udint_type_name, &get_datatype_info_c::safeudint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::ulint_type_name, &get_datatype_info_c::safeulint_type_name);
conti@603: remove_incompatible_datatypes(symbol);
msousa@479: if (debug) std::cout << "ANY_INT [" << symbol->candidate_datatypes.size()<< "]" << std::endl;
msousa@472: return NULL;
msousa@472: }
msousa@472:
msousa@472:
msousa@472:
msousa@488: void *fill_candidate_datatypes_c::handle_any_real(symbol_c *symbol) {
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::real_type_name, &get_datatype_info_c::safereal_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::lreal_type_name, &get_datatype_info_c::safelreal_type_name);
conti@603: remove_incompatible_datatypes(symbol);
msousa@417: if (debug) std::cout << "ANY_REAL [" << symbol->candidate_datatypes.size() << "]" << std::endl;
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@472:
msousa@488:
msousa@488: void *fill_candidate_datatypes_c::handle_any_literal(symbol_c *symbol, symbol_c *symbol_value, symbol_c *symbol_type) {
msousa@488: symbol_value->accept(*this);
msousa@488: if (search_in_candidate_datatype_list(symbol_type, symbol_value->candidate_datatypes) >= 0)
msousa@488: add_datatype_to_candidate_list(symbol, symbol_type);
conti@603: remove_incompatible_datatypes(symbol);
msousa@643: if (debug) std::cout << "ANY_LITERAL [" << symbol->candidate_datatypes.size() << "]\n";
msousa@488: return NULL;
msousa@488: }
msousa@488:
msousa@488:
msousa@488:
msousa@488: void *fill_candidate_datatypes_c::visit( real_c *symbol) {return handle_any_real(symbol);}
msousa@488: void *fill_candidate_datatypes_c::visit(neg_real_c *symbol) {return handle_any_real(symbol);}
msousa@488:
msousa@417:
msousa@472:
msousa@417: void *fill_candidate_datatypes_c::visit(neg_integer_c *symbol) {
msousa@650: /* Please read the comment in neg_expression_c method, as it also applies here */
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::int_type_name, &get_datatype_info_c::safeint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::sint_type_name, &get_datatype_info_c::safesint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::dint_type_name, &get_datatype_info_c::safedint_type_name);
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::lint_type_name, &get_datatype_info_c::safelint_type_name);
conti@603: remove_incompatible_datatypes(symbol);
msousa@417: if (debug) std::cout << "neg ANY_INT [" << symbol->candidate_datatypes.size() << "]" << std::endl;
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@472:
msousa@488:
msousa@479: void *fill_candidate_datatypes_c::visit(integer_c *symbol) {return handle_any_integer(symbol);}
msousa@479: void *fill_candidate_datatypes_c::visit(binary_integer_c *symbol) {return handle_any_integer(symbol);}
msousa@479: void *fill_candidate_datatypes_c::visit(octal_integer_c *symbol) {return handle_any_integer(symbol);}
msousa@479: void *fill_candidate_datatypes_c::visit(hex_integer_c *symbol) {return handle_any_integer(symbol);}
msousa@417:
msousa@472:
msousa@488:
msousa@427: // SYM_REF2(integer_literal_c, type, value)
msousa@472: /*
msousa@472: * integer_literal:
msousa@472: * integer_type_name '#' signed_integer
msousa@472: * | integer_type_name '#' binary_integer
msousa@472: * | integer_type_name '#' octal_integer
msousa@472: * | integer_type_name '#' hex_integer
msousa@472: */
msousa@488: void *fill_candidate_datatypes_c::visit( integer_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);}
msousa@488: void *fill_candidate_datatypes_c::visit( real_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);}
msousa@488: void *fill_candidate_datatypes_c::visit(bit_string_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);}
msousa@488:
msousa@488: void *fill_candidate_datatypes_c::visit( boolean_literal_c *symbol) {
msousa@488: if (NULL != symbol->type) return handle_any_literal(symbol, symbol->value, symbol->type);
msousa@488:
msousa@427: symbol->value->accept(*this);
msousa@488: symbol->candidate_datatypes = symbol->value->candidate_datatypes;
msousa@488: return NULL;
msousa@488: }
msousa@488:
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(boolean_true_c *symbol) {
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name, &get_datatype_info_c::safebool_type_name);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(boolean_false_c *symbol) {
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name, &get_datatype_info_c::safebool_type_name);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /*******************************/
msousa@417: /* B.1.2.2 Character Strings */
msousa@417: /*******************************/
msousa@417: void *fill_candidate_datatypes_c::visit(double_byte_character_string_c *symbol) {
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::wstring_type_name, &get_datatype_info_c::safewstring_type_name);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(single_byte_character_string_c *symbol) {
msousa@693: add_2datatypes_to_candidate_list(symbol, &get_datatype_info_c::string_type_name, &get_datatype_info_c::safestring_type_name);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /***************************/
msousa@417: /* B 1.2.3 - Time Literals */
msousa@417: /***************************/
msousa@417: /************************/
msousa@417: /* B 1.2.3.1 - Duration */
msousa@417: /************************/
msousa@417: void *fill_candidate_datatypes_c::visit(duration_c *symbol) {
msousa@472: add_datatype_to_candidate_list(symbol, symbol->type_name);
msousa@417: if (debug) std::cout << "TIME_LITERAL [" << symbol->candidate_datatypes.size() << "]\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /************************************/
msousa@417: /* B 1.2.3.2 - Time of day and Date */
msousa@417: /************************************/
msousa@479: void *fill_candidate_datatypes_c::visit(time_of_day_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;}
msousa@479: void *fill_candidate_datatypes_c::visit(date_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;}
msousa@479: void *fill_candidate_datatypes_c::visit(date_and_time_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;}
msousa@417:
msousa@417: /**********************/
msousa@417: /* B 1.3 - Data types */
msousa@417: /**********************/
msousa@417: /********************************/
msousa@417: /* B 1.3.3 - Derived data types */
msousa@417: /********************************/
msousa@806:
msousa@806: void *fill_candidate_datatypes_c::fill_type_decl(symbol_c *symbol, symbol_c *type_name, symbol_c *spec_init) {
msousa@806: /* NOTE: Unlike the rest of the 'fill' algorithm that works using a bottom->up approach, when handling
msousa@806: * data type declarations (section B.1.3.3 - Derived data types) we use a top->bottom approach.
msousa@806: * This is intentional, and not a bug! Explanation follows...
msousa@806: * Here we are essentially determining the base type of each defined data type. In many cases (especially structs,
msousa@806: * enumerations, arrays, etc...), the datatype is its own base type. However, the derived datatype is stored in
msousa@806: * multiple symbol_c classes (e.g. an enumeration uses enumerated_type_declaration_c, enumerated_spec_init_c,
msousa@806: * enumerated_value_list_c, enumerated_value_c, ...). Several of these could be chosen to work as the canonical base datatype
msousa@806: * symbol. Which symbol is used is really up to the search_base_type_c, and not this fill_candidate_datatypes_c.
msousa@806: * Here we must right the code to handle whatever the search_base_type_c chooses to use as the canonical symbol to represent
msousa@806: * the base datatype.
msousa@806: * Since the base datatype may be (and sometimes/often/always(?) actually is) the top level symbol_c (an enumerated_type_declaration_c
msousa@806: * in the case of the enumerations), it only makes sense to ask search_base_type_c for a basetype when we pass it the
msousa@806: * symbol in the highest level of the type declaration (the enumerated_type_declaration_c in the case of the enumerations).
msousa@806: * For this reason, we determine the basetype at the top level, and send that info down to the bottom level of the data type
msousa@806: * declaration. In summary, a top->down algorithm!
msousa@806: */
msousa@806: add_datatype_to_candidate_list(symbol, base_type(symbol));
msousa@806: type_name->candidate_datatypes = symbol->candidate_datatypes; // use top->down algorithm!!
msousa@806: spec_init->candidate_datatypes = symbol->candidate_datatypes; // use top->down algorithm!!
msousa@806: spec_init->accept(*this);
msousa@806: return NULL;
msousa@806: }
msousa@806:
msousa@806:
msousa@806: void *fill_candidate_datatypes_c::fill_spec_init(symbol_c *symbol, symbol_c *type_spec, symbol_c *init_value) {
msousa@806: /* NOTE: The note in the fill_type_decl() function is also partially valid here,
msousa@806: * i.e. here too we work using a top->down algorithm for the type_spec part, but a bottom->up algorithm
msousa@806: * for the init_value part!!
msousa@806: */
msousa@806: /* NOTE: When a variable is declared inside a POU as, for example
msousa@806: * VAR
msousa@806: * a : ARRAY[9] OF REAL;
msousa@806: * e : ENUM (black, white, gray);
msousa@806: * s : STRUCT x, y: REAL; END_STRUCT
msousa@806: * END_VAR
msousa@806: * the anonymous datatype will be defined directly by the ***_spec_init_c, and will not have an
msousa@806: * ****_type_declaration_c. In these cases, the anonymous data type is its own basetype, and the
msousa@806: * ***_spec_init_c class will act as the canonical symbol that represents the (anonymous) basetype.
msousa@806: *
msousa@806: * This method must handle the above case, as well as the case in which the ***_spec_init_c is called
msousa@806: * from an ****_type_declaration_c.
msousa@806: */
msousa@806: if (symbol->candidate_datatypes.size() == 0) // i.e., if this is an anonymous datatype!
msousa@806: add_datatype_to_candidate_list(symbol, base_type(symbol));
msousa@806:
msousa@806: // use top->down algorithm!!
msousa@806: type_spec->candidate_datatypes = symbol->candidate_datatypes;
msousa@806: type_spec->accept(*this);
msousa@806:
msousa@806: // use bottom->up algorithm!!
msousa@806: if (NULL != init_value) init_value->accept(*this);
msousa@806: /* NOTE: Even if the constant and the type are of incompatible data types, we let the
msousa@806: * ***_spec_init_c object inherit the data type of the type declaration (simple_specification)
msousa@806: * This will let us produce more informative error messages when checking data type compatibility
msousa@806: * with located variables (AT %QW3.4 : WORD).
msousa@806: */
msousa@806: // if (NULL != init_value) intersect_candidate_datatype_list(symbol /*origin, dest.*/, init_value /*with*/);
msousa@806: return NULL;
msousa@806: }
msousa@806:
msousa@806:
msousa@717: /* TYPE type_declaration_list END_TYPE */
msousa@717: // SYM_REF1(data_type_declaration_c, type_declaration_list)
msousa@717: /* NOTE: Not required. already handled by iterator_visitor_c base class */
msousa@717:
msousa@717: /* helper symbol for data_type_declaration */
msousa@717: // SYM_LIST(type_declaration_list_c)
msousa@717: /* NOTE: Not required. already handled by iterator_visitor_c base class */
msousa@717:
msousa@717: /* simple_type_name ':' simple_spec_init */
msousa@717: // SYM_REF2(simple_type_declaration_c, simple_type_name, simple_spec_init)
msousa@806: void *fill_candidate_datatypes_c::visit(simple_type_declaration_c *symbol) {return fill_type_decl(symbol, symbol->simple_type_name, symbol->simple_spec_init);}
msousa@806:
msousa@502:
msousa@502: /* simple_specification ASSIGN constant */
msousa@502: // SYM_REF2(simple_spec_init_c, simple_specification, constant)
msousa@806: void *fill_candidate_datatypes_c::visit(simple_spec_init_c *symbol) {return fill_spec_init(symbol, symbol->simple_specification, symbol->constant);}
msousa@502:
msousa@717:
msousa@717: /* subrange_type_name ':' subrange_spec_init */
msousa@717: // SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init)
msousa@806: void *fill_candidate_datatypes_c::visit(subrange_type_declaration_c *symbol) {return fill_type_decl(symbol, symbol->subrange_type_name, symbol->subrange_spec_init);}
msousa@717:
msousa@717: /* subrange_specification ASSIGN signed_integer */
msousa@717: // SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer)
msousa@806: void *fill_candidate_datatypes_c::visit(subrange_spec_init_c *symbol) {return fill_spec_init(symbol, symbol->subrange_specification, symbol->signed_integer);}
msousa@717:
msousa@717: /* integer_type_name '(' subrange')' */
msousa@717: // SYM_REF2(subrange_specification_c, integer_type_name, subrange)
msousa@806: // NOTE: not needed! Iterator visitor already handles this!
msousa@717:
msousa@417: /* signed_integer DOTDOT signed_integer */
msousa@717: /* dimension will be filled in during stage 3 (array_range_check_c) with the number of elements in this subrange */
msousa@717: // SYM_REF2(subrange_c, lower_limit, upper_limit, unsigned long long int dimension;)
msousa@417: void *fill_candidate_datatypes_c::visit(subrange_c *symbol) {
msousa@417: symbol->lower_limit->accept(*this);
msousa@417: symbol->upper_limit->accept(*this);
msousa@417:
msousa@417: for (unsigned int u = 0; u < symbol->upper_limit->candidate_datatypes.size(); u++) {
msousa@417: for(unsigned int l = 0; l < symbol->lower_limit->candidate_datatypes.size(); l++) {
msousa@676: if (get_datatype_info_c::is_type_equal(symbol->upper_limit->candidate_datatypes[u], symbol->lower_limit->candidate_datatypes[l]))
msousa@465: add_datatype_to_candidate_list(symbol, symbol->lower_limit->candidate_datatypes[l]);
msousa@417: }
msousa@417: }
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@717:
msousa@717: /* enumerated_type_name ':' enumerated_spec_init */
msousa@717: // SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init)
msousa@806: void *fill_candidate_datatypes_c::visit(enumerated_type_declaration_c *symbol) {return fill_type_decl(symbol, symbol->enumerated_type_name, symbol->enumerated_spec_init);}
msousa@717:
msousa@717:
msousa@717: /* enumerated_specification ASSIGN enumerated_value */
msousa@717: // SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value)
msousa@806: // NOTE: enumerated_specification is either an enumerated_value_list_c or identifier_c.
msousa@806: void *fill_candidate_datatypes_c::visit(enumerated_spec_init_c *symbol) {return fill_spec_init(symbol, symbol->enumerated_specification, symbol->enumerated_value);}
msousa@806:
msousa@717:
msousa@717: /* helper symbol for enumerated_specification->enumerated_spec_init */
msousa@717: /* enumerated_value_list ',' enumerated_value */
msousa@717: // SYM_LIST(enumerated_value_list_c)
msousa@717: void *fill_candidate_datatypes_c::visit(enumerated_value_list_c *symbol) {
msousa@806: if (symbol->candidate_datatypes.size() != 1) ERROR;
msousa@806: symbol_c *current_enumerated_spec_type = symbol->candidate_datatypes[0];
msousa@717:
msousa@717: /* We already know the datatype of the enumerated_value(s) in the list, so we set them directly instead of recursively calling the enumerated_value_c visit method! */
msousa@726: for(int i = 0; i < symbol->n; i++)
msousa@806: add_datatype_to_candidate_list(symbol->elements[i], current_enumerated_spec_type); // top->down algorithm!!
msousa@726:
msousa@717: return NULL;
msousa@717: }
msousa@717:
msousa@717:
msousa@717: /* enumerated_type_name '#' identifier */
msousa@717: // SYM_REF2(enumerated_value_c, type, value)
msousa@717: /* WARNING: The enumerated_value_c is used when delcaring an enumerated datatype
msousa@717: * (e.g. TYPE enumT: (xxx1, xxx2); END_TYPE ---> xxx1 and xxx2 will be enumerated_value_c)
msousa@717: * as well as in the source code of POU bodies
msousa@717: * (e.g. enumVar := xxx1 ---> xxx1 will be enumerated_value_c)
msousa@717: *
msousa@717: * The following method will only be used to visit enumerated_value_c that show up inside the
msousa@717: * source code of POU bodies (or the initial values of an enumerated type). When used inside an
msousa@717: * enumerated type declaration to list the possible enum values (whether inside
msousa@717: * a TYPE ... END_TYPE, or inside a VAR .. END_VAR), the visitor method for enumerated_value_list_c
msousa@717: * will NOT recursively call the following enumerated_value_c visitor method!
msousa@717: */
msousa@417: void *fill_candidate_datatypes_c::visit(enumerated_value_c *symbol) {
msousa@716: symbol_c *global_enumerated_type;
msousa@716: symbol_c *local_enumerated_type;
conti@735: symbol_c *enumerated_type = NULL;
msousa@417:
msousa@724: if (NULL != symbol->type) {
msousa@733: /* NOTE: This code must take into account the following situation:
msousa@724: *
msousa@724: * TYPE
msousa@724: * base_enum_t: (x1, x2, x3);
msousa@724: * enum_t1 : base_enum_t := x1;
msousa@724: * enum_t2 : base_enum_t := x2;
msousa@724: * enum_t12: enum_t1 := x2;
msousa@724: * END_TYPE
msousa@724: *
msousa@724: * considering the above, ALL of the following are correct!
msousa@724: * base_enum_t#x1
msousa@724: * enum_t1#x1
msousa@724: * enum_t2#x1
msousa@724: * enum_t12#x1
msousa@724: */
msousa@724: /* check whether the value really belongs to that datatype!! */
msousa@724: /* All local enum values are declared inside anonymous enumeration datatypes (i.e. inside a VAR ... END_VAR declaration, with
msousa@724: * the enum type having no type name), so thay cannot possibly be referenced using a datatype_t#enumvalue syntax.
msousa@724: * Because of this, we only look for the datatype identifier in the global enum value symbol table!
msousa@724: */
msousa@724: enumerated_type = NULL; // assume error...
msousa@724: enumerated_value_symtable_t::iterator lower = global_enumerated_value_symtable.lower_bound(symbol->value);
msousa@724: enumerated_value_symtable_t::iterator upper = global_enumerated_value_symtable.upper_bound(symbol->value);
msousa@724: for (; lower != upper; lower++)
msousa@733: if (get_datatype_info_c::is_type_equal(base_type(lower->second), base_type(symbol->type)))
msousa@724: enumerated_type = symbol->type;
msousa@724: }
msousa@417: else {
msousa@720: symbol_c *global_enumerated_type = global_enumerated_value_symtable.find_value (symbol->value);
msousa@720: symbol_c * local_enumerated_type = local_enumerated_value_symtable.find_value (symbol->value);
msousa@720: int global_multiplicity = global_enumerated_value_symtable.count(symbol->value);
msousa@720: int local_multiplicity = local_enumerated_value_symtable.count(symbol->value);
msousa@720:
msousa@720: if (( local_multiplicity == 0) && (global_multiplicity == 0))
msousa@716: enumerated_type = NULL; // not found!
msousa@720: else if ( local_multiplicity > 1)
msousa@720: enumerated_type = NULL; // Local duplicate, so it is ambiguous!
msousa@720: else if ( local_multiplicity == 1)
msousa@716: enumerated_type = local_enumerated_type;
msousa@720: else if ( global_multiplicity > 1)
msousa@720: enumerated_type = NULL; // Global duplicate, so it is ambiguous!
msousa@720: else if ( global_multiplicity == 1)
msousa@716: enumerated_type = global_enumerated_type;
msousa@716: else ERROR;
msousa@417: }
msousa@417: enumerated_type = base_type(enumerated_type);
msousa@417: if (NULL != enumerated_type)
msousa@465: add_datatype_to_candidate_list(symbol, enumerated_type);
msousa@417:
msousa@417: if (debug) std::cout << "ENUMERATE [" << symbol->candidate_datatypes.size() << "]\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@717: /* identifier ':' array_spec_init */
msousa@717: // SYM_REF2(array_type_declaration_c, identifier, array_spec_init)
msousa@806: void *fill_candidate_datatypes_c::visit(array_type_declaration_c *symbol) {return fill_type_decl(symbol, symbol->identifier, symbol->array_spec_init);}
msousa@717:
msousa@717: /* array_specification [ASSIGN array_initialization} */
msousa@717: /* array_initialization may be NULL ! */
msousa@717: // SYM_REF2(array_spec_init_c, array_specification, array_initialization)
msousa@806: void *fill_candidate_datatypes_c::visit(array_spec_init_c *symbol) {return fill_spec_init(symbol, symbol->array_specification, symbol->array_initialization);}
msousa@717:
msousa@717: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
msousa@717: // SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name)
msousa@717:
msousa@717: /* helper symbol for array_specification */
msousa@717: /* array_subrange_list ',' subrange */
msousa@717: // SYM_LIST(array_subrange_list_c)
msousa@717:
msousa@717: /* array_initialization: '[' array_initial_elements_list ']' */
msousa@717: /* helper symbol for array_initialization */
msousa@717: /* array_initial_elements_list ',' array_initial_elements */
msousa@717: // SYM_LIST(array_initial_elements_list_c)
msousa@717:
msousa@717: /* integer '(' [array_initial_element] ')' */
msousa@717: /* array_initial_element may be NULL ! */
msousa@717: // SYM_REF2(array_initial_elements_c, integer, array_initial_element)
msousa@717:
msousa@717: /* structure_type_name ':' structure_specification */
msousa@717: // SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
msousa@806: void *fill_candidate_datatypes_c::visit(structure_type_declaration_c *symbol) {return fill_type_decl(symbol, symbol->structure_type_name, symbol->structure_specification);}
msousa@717:
msousa@717: /* structure_type_name ASSIGN structure_initialization */
msousa@717: /* structure_initialization may be NULL ! */
msousa@717: // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
msousa@806: void *fill_candidate_datatypes_c::visit(initialized_structure_c *symbol) {return fill_spec_init(symbol, symbol->structure_type_name, symbol->structure_initialization);}
msousa@717:
msousa@717: /* helper symbol for structure_declaration */
msousa@717: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */
msousa@717: /* structure_element_declaration_list structure_element_declaration ';' */
msousa@717: // SYM_LIST(structure_element_declaration_list_c)
msousa@717:
msousa@717: /* structure_element_name ':' *_spec_init */
msousa@717: // SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init)
msousa@717:
msousa@717: /* helper symbol for structure_initialization */
msousa@717: /* structure_initialization: '(' structure_element_initialization_list ')' */
msousa@717: /* structure_element_initialization_list ',' structure_element_initialization */
msousa@717: // SYM_LIST(structure_element_initialization_list_c)
msousa@717:
msousa@717: /* structure_element_name ASSIGN value */
msousa@717: // SYM_REF2(structure_element_initialization_c, structure_element_name, value)
msousa@717:
msousa@717: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
msousa@717: // SYM_REF4(string_type_declaration_c, string_type_name, elementary_string_type_name, string_type_declaration_size, string_type_declaration_init/* may be == NULL! */)
msousa@717:
msousa@717:
msousa@810: /* function_block_type_name ASSIGN structure_initialization */
msousa@810: /* structure_initialization -> may be NULL ! */
msousa@810: // SYM_REF2(fb_spec_init_c, function_block_type_name, structure_initialization)
msousa@810: void *fill_candidate_datatypes_c::visit(fb_spec_init_c *symbol) {return fill_spec_init(symbol, symbol->function_block_type_name, symbol->structure_initialization);}
msousa@810:
msousa@717:
msousa@717:
msousa@417: /*********************/
msousa@417: /* B 1.4 - Variables */
msousa@417: /*********************/
msousa@417: void *fill_candidate_datatypes_c::visit(symbolic_variable_c *symbol) {
msousa@479: add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol)); /* will only add if non NULL */
msousa@417: if (debug) std::cout << "VAR [" << symbol->candidate_datatypes.size() << "]\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@502:
msousa@417: /********************************************/
msousa@417: /* B 1.4.1 - Directly Represented Variables */
msousa@417: /********************************************/
msousa@417: void *fill_candidate_datatypes_c::visit(direct_variable_c *symbol) {
msousa@417: /* Comment added by mario:
msousa@417: * The following code is safe, actually, as the lexical parser guarantees the correct IEC61131-3 syntax was used.
msousa@417: */
msousa@417: /* However, we should probably add an assertion in case we later change the lexical parser! */
msousa@417: /* if (symbol->value == NULL) ERROR;
msousa@417: * if (symbol->value[0] == '\0') ERROR;
msousa@417: * if (symbol->value[1] == '\0') ERROR;
msousa@417: */
msousa@417: switch (symbol->value[2]) {
msousa@693: case 'x': case 'X': /* bit - 1 bit */ add_datatype_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name); break;
msousa@693: case 'b': case 'B': /* byte - 8 bits */ add_datatype_to_candidate_list(symbol, &get_datatype_info_c::byte_type_name); break;
msousa@693: case 'w': case 'W': /* word - 16 bits */ add_datatype_to_candidate_list(symbol, &get_datatype_info_c::word_type_name); break;
msousa@693: case 'd': case 'D': /* dword - 32 bits */ add_datatype_to_candidate_list(symbol, &get_datatype_info_c::dword_type_name); break;
msousa@693: case 'l': case 'L': /* lword - 64 bits */ add_datatype_to_candidate_list(symbol, &get_datatype_info_c::lword_type_name); break;
msousa@479: /* if none of the above, then the empty string was used <=> boolean */
msousa@693: default: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name); break;
msousa@417: }
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /*************************************/
msousa@417: /* B 1.4.2 - Multi-element variables */
msousa@417: /*************************************/
msousa@417: /* subscripted_variable '[' subscript_list ']' */
msousa@417: // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
msousa@417: void *fill_candidate_datatypes_c::visit(array_variable_c *symbol) {
msousa@417: /* get the declaration of the data type __stored__ in the array... */
msousa@417: /* if we were to want the data type of the array itself, then we should call_param_name
msousa@417: * search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable)
msousa@417: */
mjsousa@827: add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol)); /* will only add if non NULL */
msousa@417:
msousa@417: /* recursively call the subscript list, so we can check the data types of the expressions used for the subscripts */
msousa@417: symbol->subscript_list->accept(*this);
msousa@417:
mjsousa@827: /* recursively call the subscripted_variable. We need to do this since the array variable may be stored inside a structured
mjsousa@827: * variable (i.e. if it is an element inside a struct), in which case we want to recursively visit every element of the struct,
mjsousa@827: * as it may contain more arrays whose subscripts must also be visited!
mjsousa@827: * e.g. structvar.a1[v1+2].b1.c1[v2+3].d1
mjsousa@827: * TYPE
mjsousa@827: * d_s: STRUCT d1: int; d2: int;
mjsousa@827: * d_a: ARRAY [1..3] OF d_s;
mjsousa@827: * c_s: STRUCT c1: d_a; c2: d_a;
mjsousa@827: * b_s: STRUCT b1: c_s; b2: c_s;
mjsousa@827: * b_a: ARRAY [1..3] OF b_s;
mjsousa@827: * a_s: STRUCT a1: b_a; a2: b_a;
mjsousa@827: * END_TYPE
mjsousa@827: * VAR
mjsousa@827: * structvar: a_s;
mjsousa@827: * END_VAR
mjsousa@827: */
mjsousa@827: symbol->subscripted_variable->accept(*this);
mjsousa@827:
msousa@417: if (debug) std::cout << "ARRAY_VAR [" << symbol->candidate_datatypes.size() << "]\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417: /* subscript_list ',' subscript */
msousa@417: // SYM_LIST(subscript_list_c)
msousa@417: /* NOTE: we inherit from iterator visitor, so we do not need to implement this method... */
msousa@455: // void *fill_candidate_datatypes_c::visit(subscript_list_c *symbol)
msousa@417:
msousa@417:
msousa@417: /* record_variable '.' field_selector */
msousa@417: /* WARNING: input and/or output variables of function blocks
msousa@417: * may be accessed as fields of a structured variable!
msousa@417: * Code handling a structured_variable_c must take
msousa@417: * this into account!
msousa@417: */
msousa@417: // SYM_REF2(structured_variable_c, record_variable, field_selector)
msousa@417: void *fill_candidate_datatypes_c::visit(structured_variable_c *symbol) {
mjsousa@827: /* NOTE: We do not need to recursively determine the data types of each field_selector, as the search_varfb_instance_type
mjsousa@827: * will do that for us. So we determine the candidate datatypes only for the full structured_variable.
mjsousa@827: */
msousa@479: add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol)); /* will only add if non NULL */
mjsousa@827: /* However, we do need to visit each record type recursively!
mjsousa@827: * Remember that a structured variable may be stored inside an array (e.g. arrayvar[33].elem1)
mjsousa@827: * The array subscripts may contain a complex expression (e.g. arrayvar[ varx + 33].elem1) whose datatype must be correctly determined!
mjsousa@827: * The expression, may even contain a function call to an overloaded function!
mjsousa@827: * (e.g. arrayvar[ varx + TRUNC(realvar)].elem1)
mjsousa@827: */
mjsousa@827: symbol->record_variable->accept(*this);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@502:
msousa@502:
msousa@502: /******************************************/
msousa@502: /* B 1.4.3 - Declaration & Initialisation */
msousa@502: /******************************************/
msousa@502:
msousa@502: void *fill_candidate_datatypes_c::visit(var1_list_c *symbol) {
msousa@502: for(int i = 0; i < symbol->n; i++) {
msousa@732: /* We don't really need to set the datatype of each variable. We just check the declaration itself!
msousa@732: add_datatype_to_candidate_list(symbol->elements[i], search_varfb_instance_type->get_basetype_decl(symbol->elements[i])); // will only add if non NULL
msousa@732: */
msousa@732: symbol->elements[i]->accept(*this); // handle the extensible_input_parameter_c, etc...
msousa@502: }
msousa@502: return NULL;
msousa@502: }
msousa@502:
msousa@502:
msousa@502: /* AT direct_variable */
msousa@502: // SYM_REF1(location_c, direct_variable)
msousa@502: void *fill_candidate_datatypes_c::visit(location_c *symbol) {
msousa@558: /* This is a special situation.
msousa@558: *
msousa@558: * The reason is that a located variable may be declared to be of any data type, as long as the size
msousa@558: * matches the location (lines 1 3 and 4 of table 17). For example:
msousa@558: * var1 AT %MB42.0 : BYTE;
msousa@558: * var1 AT %MB42.1 : SINT;
msousa@558: * var1 AT %MB42.2 : USINT;
msousa@558: * var1 AT %MW64 : INT;
msousa@558: * var1 AT %MD56 : DINT;
msousa@558: * var1 AT %MD57 : REAL;
msousa@558: * are all valid!!
msousa@558: *
msousa@558: * However, when used inside an expression, the direct variable (uses the same syntax as the location
msousa@558: * of a located variable) is limited to the following (ANY_BIT) data types:
msousa@558: * %MX --> BOOL
msousa@558: * %MB --> BYTE
msousa@558: * %MW --> WORD
msousa@558: * %MD --> DWORD
msousa@558: * %ML --> LWORD
msousa@558: *
msousa@558: * So, in order to be able to analyse expressions with direct variables
msousa@558: * e.g: var1 := 66 OR %MW34
msousa@558: * where the direct variable may only take the ANY_BIT data types, the fill_candidate_datatypes_c
msousa@558: * considers that only the ANY_BIT data types are allowed for a direct variable.
msousa@558: * However, it appears from the examples in the standard (lines 1 3 and 4 of table 17)
msousa@558: * a location may have any data type (presumably as long as the size in bits match).
msousa@558: * For this reason, a location_c may have more allowable data types than a direct_variable_c
msousa@558: */
msousa@558:
msousa@502: symbol->direct_variable->accept(*this);
msousa@558: for (unsigned int i = 0; i < symbol->direct_variable->candidate_datatypes.size(); i++) {
msousa@558: switch (get_sizeof_datatype_c::getsize(symbol->direct_variable->candidate_datatypes[i])) {
msousa@558: case 1: /* bit - 1 bit */
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safebool_type_name);
msousa@558: break;
msousa@558: case 8: /* byte - 8 bits */
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::byte_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safebyte_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::sint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safesint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::usint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safeusint_type_name);
msousa@558: break;
msousa@558: case 16: /* word - 16 bits */
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::word_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safeword_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::int_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safeint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::uint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safeuint_type_name);
msousa@558: break;
msousa@558: case 32: /* dword - 32 bits */
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::dword_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safedword_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::dint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safedint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::udint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safeudint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::real_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safereal_type_name);
msousa@558: break;
msousa@558: case 64: /* lword - 64 bits */
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::lword_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safelword_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::lint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safelint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::ulint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safeulint_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::lreal_type_name);
msousa@693: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::safelreal_type_name);
msousa@558: break;
msousa@558: default: /* if none of the above, then no valid datatype allowed... */
msousa@558: break;
msousa@558: } /* switch() */
msousa@558: } /* for */
msousa@558:
msousa@502: return NULL;
msousa@502: }
msousa@502:
msousa@502:
msousa@502: /* [variable_name] location ':' located_var_spec_init */
msousa@502: /* variable_name -> may be NULL ! */
msousa@502: // SYM_REF3(located_var_decl_c, variable_name, location, located_var_spec_init)
msousa@502: void *fill_candidate_datatypes_c::visit(located_var_decl_c *symbol) {
msousa@502: symbol->located_var_spec_init->accept(*this);
msousa@502: symbol->location->accept(*this);
msousa@523: if (NULL != symbol->variable_name) {
msousa@523: symbol->variable_name->candidate_datatypes = symbol->location->candidate_datatypes;
msousa@523: intersect_candidate_datatype_list(symbol->variable_name /*origin, dest.*/, symbol->located_var_spec_init /*with*/);
msousa@523: }
msousa@502: return NULL;
msousa@502: }
msousa@502:
msousa@502:
msousa@502:
msousa@417: /************************************/
msousa@417: /* B 1.5 Program organization units */
msousa@417: /************************************/
msousa@417: /*********************/
msousa@417: /* B 1.5.1 Functions */
msousa@417: /*********************/
msousa@417: void *fill_candidate_datatypes_c::visit(function_declaration_c *symbol) {
msousa@479: if (debug) printf("Filling candidate data types list of function %s\n", ((token_c *)(symbol->derived_function_name))->value);
msousa@716: local_enumerated_value_symtable.reset();
msousa@716: symbol->var_declarations_list->accept(populate_enumvalue_symtable);
msousa@716:
msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
msousa@417: symbol->var_declarations_list->accept(*this);
msousa@417: symbol->function_body->accept(*this);
msousa@417: delete search_varfb_instance_type;
msousa@417: search_varfb_instance_type = NULL;
msousa@716:
msousa@716: local_enumerated_value_symtable.reset();
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /***************************/
msousa@417: /* B 1.5.2 Function blocks */
msousa@417: /***************************/
msousa@417: void *fill_candidate_datatypes_c::visit(function_block_declaration_c *symbol) {
msousa@479: if (debug) printf("Filling candidate data types list of FB %s\n", ((token_c *)(symbol->fblock_name))->value);
msousa@716: local_enumerated_value_symtable.reset();
msousa@716: symbol->var_declarations->accept(populate_enumvalue_symtable);
msousa@716:
msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
msousa@417: symbol->var_declarations->accept(*this);
msousa@417: symbol->fblock_body->accept(*this);
msousa@417: delete search_varfb_instance_type;
msousa@417: search_varfb_instance_type = NULL;
msousa@716:
msousa@716: local_enumerated_value_symtable.reset();
msousa@807:
msousa@807: /* The FB declaration itself may be used as a dataype! We now do the fill algorithm considering
msousa@807: * function_block_declaration_c a data type declaration...
msousa@807: */
msousa@807: // The next line is essentially equivalent to doing--> symbol->candidate_datatypes.push_back(symbol);
msousa@807: add_datatype_to_candidate_list(symbol, base_type(symbol));
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /**********************/
msousa@417: /* B 1.5.3 - Programs */
msousa@417: /**********************/
msousa@417: void *fill_candidate_datatypes_c::visit(program_declaration_c *symbol) {
msousa@479: if (debug) printf("Filling candidate data types list in program %s\n", ((token_c *)(symbol->program_type_name))->value);
msousa@716: local_enumerated_value_symtable.reset();
msousa@716: symbol->var_declarations->accept(populate_enumvalue_symtable);
msousa@716:
msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
msousa@417: symbol->var_declarations->accept(*this);
msousa@417: symbol->function_block_body->accept(*this);
msousa@417: delete search_varfb_instance_type;
msousa@417: search_varfb_instance_type = NULL;
msousa@716:
msousa@716: local_enumerated_value_symtable.reset();
msousa@417: return NULL;
msousa@417: }
msousa@417:
Laurent@802: /********************************************/
Laurent@802: /* B 1.6 Sequential function chart elements */
Laurent@802: /********************************************/
Laurent@802:
Laurent@802: void *fill_candidate_datatypes_c::visit(transition_condition_c *symbol) {
Laurent@802: symbol_c *condition_type;
Laurent@802:
Laurent@802: if (symbol->transition_condition_il != NULL) {
Laurent@802: symbol->transition_condition_il->accept(*this);
Laurent@802: for (unsigned int i = 0; i < symbol->transition_condition_il->candidate_datatypes.size(); i++) {
Laurent@802: condition_type = symbol->transition_condition_il->candidate_datatypes[i];
Laurent@802: if (get_datatype_info_c::is_BOOL_compatible(condition_type))
Laurent@802: add_datatype_to_candidate_list(symbol, condition_type);
Laurent@802: }
Laurent@802: }
Laurent@802: if (symbol->transition_condition_st != NULL) {
Laurent@802: symbol->transition_condition_st->accept(*this);
Laurent@802: for (unsigned int i = 0; i < symbol->transition_condition_st->candidate_datatypes.size(); i++) {
Laurent@802: condition_type = symbol->transition_condition_st->candidate_datatypes[i];
Laurent@802: if (get_datatype_info_c::is_BOOL_compatible(condition_type))
Laurent@802: add_datatype_to_candidate_list(symbol, condition_type);
Laurent@802: }
Laurent@802: }
Laurent@802: return NULL;
Laurent@802: }
msousa@417:
msousa@417: /********************************/
msousa@417: /* B 1.7 Configuration elements */
msousa@417: /********************************/
msousa@417: void *fill_candidate_datatypes_c::visit(configuration_declaration_c *symbol) {
msousa@417: // TODO !!!
msousa@417: /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /****************************************/
msousa@417: /* B.2 - Language IL (Instruction List) */
msousa@417: /****************************************/
msousa@417: /***********************************/
msousa@417: /* B 2.1 Instructions and Operands */
msousa@417: /***********************************/
msousa@443:
msousa@443: /*| instruction_list il_instruction */
msousa@443: // SYM_LIST(instruction_list_c)
msousa@464: void *fill_candidate_datatypes_c::visit(instruction_list_c *symbol) {
msousa@465: /* In order to fill the data type candidates correctly
msousa@465: * in IL instruction lists containing JMPs to labels that come before the JMP instruction
msousa@464: * itself, we need to run the fill candidate datatypes algorithm twice on the Instruction List.
msousa@464: * e.g.: ...
msousa@464: * ld 23
msousa@464: * label1:st byte_var
msousa@464: * ld 34
msousa@464: * JMP label1
msousa@464: *
msousa@464: * Note that the second time we run the algorithm, most of the candidate datatypes are already filled
msousa@464: * in, so it will be able to produce tha correct candidate datatypes for the IL instruction referenced
msousa@464: * by the label, as in the 2nd pass we already know the candidate datatypes of the JMP instruction!
msousa@464: */
msousa@464: for(int j = 0; j < 2; j++) {
msousa@464: for(int i = 0; i < symbol->n; i++) {
msousa@464: symbol->elements[i]->accept(*this);
msousa@464: }
msousa@464: }
msousa@464: return NULL;
msousa@464: }
msousa@443:
msousa@443:
msousa@459:
msousa@443: /* | label ':' [il_incomplete_instruction] eol_list */
msousa@443: // SYM_REF2(il_instruction_c, label, il_instruction)
msousa@443: // void *visit(instruction_list_c *symbol);
msousa@443: void *fill_candidate_datatypes_c::visit(il_instruction_c *symbol) {
msousa@448: if (NULL == symbol->il_instruction) {
msousa@450: /* This empty/null il_instruction does not change the value of the current/default IL variable.
msousa@450: * So it inherits the candidate_datatypes from it's previous IL instructions!
msousa@450: */
msousa@459: intersect_prev_candidate_datatype_lists(symbol);
msousa@448: } else {
msousa@459: il_instruction_c fake_prev_il_instruction = *symbol;
msousa@459: intersect_prev_candidate_datatype_lists(&fake_prev_il_instruction);
msousa@459:
msousa@457: if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL;
msousa@459: else prev_il_instruction = &fake_prev_il_instruction;
msousa@448: symbol->il_instruction->accept(*this);
msousa@448: prev_il_instruction = NULL;
msousa@448:
msousa@448: /* This object has (inherits) the same candidate datatypes as the il_instruction */
msousa@467: symbol->candidate_datatypes = symbol->il_instruction->candidate_datatypes;
msousa@448: }
msousa@443:
msousa@443: return NULL;
msousa@443: }
msousa@443:
msousa@443:
msousa@443:
msousa@417: void *fill_candidate_datatypes_c::visit(il_simple_operation_c *symbol) {
msousa@417: /* determine the data type of the operand */
msousa@417: if (NULL != symbol->il_operand) {
msousa@417: symbol->il_operand->accept(*this);
msousa@417: }
msousa@417: /* recursive call to fill the candidate data types list */
msousa@417: il_operand = symbol->il_operand;
msousa@417: symbol->il_simple_operator->accept(*this);
msousa@417: il_operand = NULL;
msousa@443: /* This object has (inherits) the same candidate datatypes as the il_simple_operator */
msousa@467: symbol->candidate_datatypes = symbol->il_simple_operator->candidate_datatypes;
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@438:
msousa@438: /* | function_name [il_operand_list] */
msousa@438: /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */
msousa@438: // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;)
msousa@417: void *fill_candidate_datatypes_c::visit(il_function_call_c *symbol) {
msousa@438: /* The first parameter of a non formal function call in IL will be the 'current value' (i.e. the prev_il_instruction)
msousa@438: * In order to be able to handle this without coding special cases, we will simply prepend that symbol
msousa@451: * to the il_operand_list, and remove it after calling handle_function_call().
msousa@438: *
msousa@438: * However, if no further paramters are given, then il_operand_list will be NULL, and we will
msousa@438: * need to create a new object to hold the pointer to prev_il_instruction.
msousa@438: */
msousa@438: if (NULL == symbol->il_operand_list) symbol->il_operand_list = new il_operand_list_c;
msousa@438: if (NULL == symbol->il_operand_list) ERROR;
msousa@438:
msousa@438: symbol->il_operand_list->accept(*this);
msousa@438:
msousa@451: if (NULL != prev_il_instruction) {
msousa@451: ((list_c *)symbol->il_operand_list)->insert_element(prev_il_instruction, 0);
msousa@451:
msousa@451: generic_function_call_t fcall_param = {
msousa@451: /* fcall_param.function_name = */ symbol->function_name,
msousa@451: /* fcall_param.nonformal_operand_list = */ symbol->il_operand_list,
msousa@451: /* fcall_param.formal_operand_list = */ NULL,
msousa@451: /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function,
msousa@451: /* fcall_param.candidate_functions = */ symbol->candidate_functions,
msousa@451: /* fcall_param.called_function_declaration = */ symbol->called_function_declaration,
msousa@451: /* fcall_param.extensible_param_count = */ symbol->extensible_param_count
msousa@451: };
msousa@451: handle_function_call(symbol, fcall_param);
msousa@451:
msousa@451: /* Undo the changes to the abstract syntax tree we made above... */
msousa@451: ((list_c *)symbol->il_operand_list)->remove_element(0);
msousa@451: }
msousa@451:
msousa@451: /* Undo the changes to the abstract syntax tree we made above... */
msousa@451: if (((list_c *)symbol->il_operand_list)->n == 0) {
msousa@451: /* if the list becomes empty, then that means that it did not exist before we made these changes, so we delete it! */
msousa@451: delete symbol->il_operand_list;
msousa@451: symbol->il_operand_list = NULL;
msousa@451: }
msousa@451:
msousa@438: if (debug) std::cout << "il_function_call_c [" << symbol->candidate_datatypes.size() << "] result.\n";
msousa@438: return NULL;
msousa@438: }
msousa@438:
msousa@438:
msousa@417: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
msousa@417: // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
msousa@417: void *fill_candidate_datatypes_c::visit(il_expression_c *symbol) {
msousa@453: symbol_c *prev_il_instruction_backup = prev_il_instruction;
msousa@453:
msousa@690: /* Stage2 will insert an artificial (and equivalent) LD to the simple_instr_list if necessary. We can therefore ignore the 'il_operand' entry! */
msousa@690: // if (NULL != symbol->il_operand)
msousa@690: // symbol->il_operand->accept(*this);
msousa@453:
msousa@453: if(symbol->simple_instr_list != NULL)
msousa@417: symbol->simple_instr_list->accept(*this);
msousa@417:
msousa@690: /* Since stage2 will insert an artificial (and equivalent) LD to the simple_instr_list when an 'il_operand' exists, we know
msousa@690: * that if (symbol->il_operand != NULL), then the first IL instruction in the simple_instr_list will be the equivalent and artificial
msousa@690: * 'LD ' IL instruction.
msousa@690: * Just to be cosistent, we will copy the datatype info back into the il_operand, even though this should not be necessary!
msousa@690: */
msousa@690: if ((NULL != symbol->il_operand) && ((NULL == symbol->simple_instr_list) || (0 == ((list_c *)symbol->simple_instr_list)->n))) ERROR; // stage2 is not behaving as we expect it to!
msousa@690: if (NULL != symbol->il_operand)
msousa@690: symbol->il_operand->candidate_datatypes = ((list_c *)symbol->simple_instr_list)->elements[0]->candidate_datatypes;
msousa@690:
msousa@417: /* Now check the if the data type semantics of operation are correct, */
msousa@452: il_operand = symbol->simple_instr_list;
msousa@417: prev_il_instruction = prev_il_instruction_backup;
msousa@417: symbol->il_expr_operator->accept(*this);
msousa@417: il_operand = NULL;
msousa@454:
msousa@454: /* This object has the same candidate datatypes as the il_expr_operator. */
msousa@467: symbol->candidate_datatypes = symbol->il_expr_operator->candidate_datatypes;
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@462:
msousa@417: void *fill_candidate_datatypes_c::visit(il_jump_operation_c *symbol) {
msousa@417: /* recursive call to fill the candidate data types list */
msousa@417: il_operand = NULL;
msousa@417: symbol->il_jump_operator->accept(*this);
msousa@417: il_operand = NULL;
msousa@462: /* This object has the same candidate datatypes as the il_jump_operator. */
msousa@467: symbol->candidate_datatypes = symbol->il_jump_operator->candidate_datatypes;
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@439:
msousa@439: /* il_call_operator prev_declared_fb_name
msousa@439: * | il_call_operator prev_declared_fb_name '(' ')'
msousa@439: * | il_call_operator prev_declared_fb_name '(' eol_list ')'
msousa@439: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
msousa@439: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
msousa@439: */
mjsousa@834: /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 */
msousa@439: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
msousa@417: void *fill_candidate_datatypes_c::visit(il_fb_call_c *symbol) {
mjsousa@834: symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
mjsousa@834: if (! get_datatype_info_c::is_function_block(fb_decl)) fb_decl = NULL;
msousa@455:
msousa@439: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
msousa@439: if (NULL == fb_decl) ERROR;
msousa@439:
msousa@455: if (symbol-> il_param_list != NULL) symbol->il_param_list->accept(*this);
msousa@455: if (symbol->il_operand_list != NULL) symbol->il_operand_list->accept(*this);
msousa@439:
msousa@439: /* The print_datatypes_error_c does not rely on this called_fb_declaration pointer being != NULL to conclude that
msousa@439: * we have a datat type incompatibility error, so setting it to the correct fb_decl is actually safe,
msousa@439: * as the compiler will never reach the compilation stage!
msousa@439: */
msousa@439: symbol->called_fb_declaration = fb_decl;
msousa@439:
msousa@455: /* Let the il_call_operator (CAL, CALC, or CALCN) determine the candidate datatypes of the il_fb_call_c... */
msousa@455: /* NOTE: We ignore whether the call is 'compatible' or not when filling in the candidate datatypes list.
msousa@455: * Even if it is not compatible, we fill in the candidate datatypes list correctly so that the following
msousa@455: * IL instructions may be handled correctly and debuged.
msousa@455: * Doing this is actually safe, as the parameter_list will still contain errors that will be found by
msousa@455: * print_datatypes_error_c, so the code will never reach stage 4!
msousa@455: */
msousa@455: symbol->il_call_operator->accept(*this);
msousa@467: symbol->candidate_datatypes = symbol->il_call_operator->candidate_datatypes;
msousa@450:
msousa@439: if (debug) std::cout << "FB [] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@439: return NULL;
msousa@439: }
msousa@439:
msousa@417:
msousa@438: /* | function_name '(' eol_list [il_param_list] ')' */
msousa@438: /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */
msousa@438: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;)
msousa@417: void *fill_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol) {
msousa@438: symbol->il_param_list->accept(*this);
msousa@438:
msousa@438: generic_function_call_t fcall_param = {
msousa@441: /* fcall_param.function_name = */ symbol->function_name,
msousa@441: /* fcall_param.nonformal_operand_list = */ NULL,
msousa@441: /* fcall_param.formal_operand_list = */ symbol->il_param_list,
msousa@441: /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function,
msousa@441: /* fcall_param.candidate_functions = */ symbol->candidate_functions,
msousa@441: /* fcall_param.called_function_declaration = */ symbol->called_function_declaration,
msousa@441: /* fcall_param.extensible_param_count = */ symbol->extensible_param_count
msousa@438: };
msousa@438: handle_function_call(symbol, fcall_param);
msousa@438:
msousa@438: if (debug) std::cout << "il_formal_funct_call_c [" << symbol->candidate_datatypes.size() << "] result.\n";
msousa@438: return NULL;
msousa@417: }
msousa@417:
msousa@452:
msousa@452: // void *visit(il_operand_list_c *symbol);
msousa@452:
msousa@452:
msousa@452: /* | simple_instr_list il_simple_instruction */
msousa@452: /* This object is referenced by il_expression_c objects */
msousa@452: void *fill_candidate_datatypes_c::visit(simple_instr_list_c *symbol) {
msousa@453: if (symbol->n <= 0)
msousa@453: return NULL; /* List is empty! Nothing to do. */
msousa@453:
msousa@453: for(int i = 0; i < symbol->n; i++)
msousa@452: symbol->elements[i]->accept(*this);
msousa@453:
msousa@453: /* This object has (inherits) the same candidate datatypes as the last il_instruction */
msousa@467: symbol->candidate_datatypes = symbol->elements[symbol->n-1]->candidate_datatypes;
msousa@452:
msousa@452: if (debug) std::cout << "simple_instr_list_c [" << symbol->candidate_datatypes.size() << "] result.\n";
msousa@452: return NULL;
msousa@452: }
msousa@452:
msousa@457:
msousa@457:
msousa@457:
msousa@453: // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;)
msousa@453: void *fill_candidate_datatypes_c::visit(il_simple_instruction_c *symbol) {
msousa@459: if (symbol->prev_il_instruction.size() > 1) ERROR; /* There should be no labeled insructions inside an IL expression! */
msousa@457: if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL;
msousa@457: else prev_il_instruction = symbol->prev_il_instruction[0];
msousa@453: symbol->il_simple_instruction->accept(*this);
msousa@453: prev_il_instruction = NULL;
msousa@453:
msousa@453: /* This object has (inherits) the same candidate datatypes as the il_simple_instruction it points to */
msousa@467: symbol->candidate_datatypes = symbol->il_simple_instruction->candidate_datatypes;
msousa@453: return NULL;
msousa@453: }
msousa@453:
msousa@453:
msousa@417: /*
msousa@417: void *visit(il_param_list_c *symbol);
msousa@417: void *visit(il_param_assignment_c *symbol);
msousa@417: void *visit(il_param_out_assignment_c *symbol);
msousa@417: */
msousa@417:
msousa@417: /*******************/
msousa@417: /* B 2.2 Operators */
msousa@417: /*******************/
msousa@417: void *fill_candidate_datatypes_c::visit(LD_operator_c *symbol) {
mjsousa@834: if (NULL == il_operand) return NULL;
msousa@417: for(unsigned int i = 0; i < il_operand->candidate_datatypes.size(); i++) {
msousa@465: add_datatype_to_candidate_list(symbol, il_operand->candidate_datatypes[i]);
msousa@417: }
msousa@417: if (debug) std::cout << "LD [" << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(LDN_operator_c *symbol) {
mjsousa@834: if (NULL == il_operand) return NULL;
msousa@417: for(unsigned int i = 0; i < il_operand->candidate_datatypes.size(); i++) {
msousa@666: if (get_datatype_info_c::is_ANY_BIT_compatible(il_operand->candidate_datatypes[i]))
msousa@465: add_datatype_to_candidate_list(symbol, il_operand->candidate_datatypes[i]);
msousa@417: }
msousa@417: if (debug) std::cout << "LDN [" << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(ST_operator_c *symbol) {
msousa@417: symbol_c *prev_instruction_type, *operand_type;
msousa@417:
msousa@417: if (NULL == prev_il_instruction) return NULL;
mjsousa@834: if (NULL == il_operand) return NULL;
msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
msousa@417: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
msousa@417: prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
msousa@417: operand_type = il_operand->candidate_datatypes[j];
msousa@676: if (get_datatype_info_c::is_type_equal(prev_instruction_type, operand_type))
msousa@465: add_datatype_to_candidate_list(symbol, prev_instruction_type);
msousa@417: }
msousa@417: }
msousa@417: if (debug) std::cout << "ST [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(STN_operator_c *symbol) {
msousa@417: symbol_c *prev_instruction_type, *operand_type;
msousa@417:
msousa@417: if (NULL == prev_il_instruction) return NULL;
mjsousa@834: if (NULL == il_operand) return NULL;
msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
msousa@417: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
msousa@417: prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
msousa@417: operand_type = il_operand->candidate_datatypes[j];
msousa@676: if (get_datatype_info_c::is_type_equal(prev_instruction_type,operand_type) && get_datatype_info_c::is_ANY_BIT_compatible(operand_type))
msousa@465: add_datatype_to_candidate_list(symbol, prev_instruction_type);
msousa@417: }
msousa@417: }
msousa@417: if (debug) std::cout << "STN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(NOT_operator_c *symbol) {
msousa@470: /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional
msousa@470: * NOT []
msousa@470: * However, it does not define the semantic of the NOT operation when the is specified.
msousa@470: * We therefore consider it an error if an il_operand is specified!
msousa@612: * We do not need to generate an error message. This error will be caught somewhere else!
msousa@470: */
msousa@470: if (NULL == prev_il_instruction) return NULL;
mjsousa@834: if (NULL == il_operand) return NULL;
msousa@470: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
msousa@666: if (get_datatype_info_c::is_ANY_BIT_compatible(prev_il_instruction->candidate_datatypes[i]))
msousa@470: add_datatype_to_candidate_list(symbol, prev_il_instruction->candidate_datatypes[i]);
msousa@470: }
msousa@470: if (debug) std::cout << "NOT_operator [" << prev_il_instruction->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@447:
mjsousa@834: void *fill_candidate_datatypes_c::visit( S_operator_c *symbol) {return handle_S_and_R_operator (symbol, "S", symbol->called_fb_declaration);}
mjsousa@834: void *fill_candidate_datatypes_c::visit( R_operator_c *symbol) {return handle_S_and_R_operator (symbol, "R", symbol->called_fb_declaration);}
mjsousa@834:
mjsousa@834: void *fill_candidate_datatypes_c::visit( S1_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration);}
mjsousa@834: void *fill_candidate_datatypes_c::visit( R1_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration);}
msousa@489: void *fill_candidate_datatypes_c::visit( CLK_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration);}
mjsousa@834: void *fill_candidate_datatypes_c::visit( CU_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration);}
mjsousa@834: void *fill_candidate_datatypes_c::visit( CD_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration);}
mjsousa@834: void *fill_candidate_datatypes_c::visit( PV_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration);}
mjsousa@834: void *fill_candidate_datatypes_c::visit( IN_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration);}
mjsousa@834: void *fill_candidate_datatypes_c::visit( PT_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration);}
msousa@447:
msousa@483: void *fill_candidate_datatypes_c::visit( AND_operator_c *symbol) {return handle_binary_operator(widen_AND_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( OR_operator_c *symbol) {return handle_binary_operator( widen_OR_table, symbol, prev_il_instruction, il_operand);}
msousa@483: void *fill_candidate_datatypes_c::visit( XOR_operator_c *symbol) {return handle_binary_operator(widen_XOR_table, symbol, prev_il_instruction, il_operand);}
msousa@483: void *fill_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {return handle_binary_operator(widen_AND_table, symbol, prev_il_instruction, il_operand);}
msousa@483: void *fill_candidate_datatypes_c::visit( ORN_operator_c *symbol) {return handle_binary_operator( widen_OR_table, symbol, prev_il_instruction, il_operand);}
msousa@483: void *fill_candidate_datatypes_c::visit(XORN_operator_c *symbol) {return handle_binary_operator(widen_XOR_table, symbol, prev_il_instruction, il_operand);}
msousa@417:
msousa@489: void *fill_candidate_datatypes_c::visit( ADD_operator_c *symbol) {return handle_binary_operator(widen_ADD_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( SUB_operator_c *symbol) {return handle_binary_operator(widen_SUB_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( MUL_operator_c *symbol) {return handle_binary_operator(widen_MUL_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( DIV_operator_c *symbol) {return handle_binary_operator(widen_DIV_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( MOD_operator_c *symbol) {return handle_binary_operator(widen_MOD_table, symbol, prev_il_instruction, il_operand);}
msousa@489:
msousa@489: void *fill_candidate_datatypes_c::visit( GT_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( GE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( EQ_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( LT_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( LE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);}
msousa@489: void *fill_candidate_datatypes_c::visit( NE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);}
msousa@484:
msousa@484:
msousa@417:
msousa@487: void *fill_candidate_datatypes_c::handle_conditional_il_flow_control_operator(symbol_c *symbol) {
msousa@417: if (NULL == prev_il_instruction) return NULL;
msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
msousa@666: if (get_datatype_info_c::is_BOOL_compatible(prev_il_instruction->candidate_datatypes[i]))
msousa@465: add_datatype_to_candidate_list(symbol, prev_il_instruction->candidate_datatypes[i]);
msousa@417: }
msousa@487: return NULL;
msousa@487: }
msousa@487:
msousa@489: void *fill_candidate_datatypes_c::visit( CAL_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;}
msousa@489: void *fill_candidate_datatypes_c::visit( RET_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;}
msousa@489: void *fill_candidate_datatypes_c::visit( JMP_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;}
msousa@487: void *fill_candidate_datatypes_c::visit( CALC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);}
msousa@487: void *fill_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);}
msousa@487: void *fill_candidate_datatypes_c::visit( RETC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);}
msousa@487: void *fill_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);}
msousa@487: void *fill_candidate_datatypes_c::visit( JMPC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);}
msousa@487: void *fill_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);}
msousa@487:
msousa@487:
msousa@487:
msousa@487:
msousa@417: /* Symbol class handled together with function call checks */
msousa@417: // void *visit(il_assign_operator_c *symbol, variable_name);
msousa@417: /* Symbol class handled together with function call checks */
msousa@417: // void *visit(il_assign_operator_c *symbol, option, variable_name);
msousa@417:
msousa@417: /***************************************/
msousa@417: /* B.3 - Language ST (Structured Text) */
msousa@417: /***************************************/
msousa@417: /***********************/
msousa@417: /* B 3.1 - Expressions */
msousa@417: /***********************/
mjsousa@873: /* SYM_REF1(ref_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@873: void *fill_candidate_datatypes_c::visit( ref_expression_c *symbol) {
mjsousa@873: symbol->exp->accept(*this);
mjsousa@873: /* we should really check whether the expression is merely a variable. For now, leave it for the future! */
mjsousa@873: /* For now, we handle references (i.e. pointers) as ULINT datatypes! */
mjsousa@873: add_datatype_to_candidate_list(symbol, &get_datatype_info_c::ulint_type_name);
mjsousa@873: return NULL;
mjsousa@873: }
mjsousa@873:
msousa@652: void *fill_candidate_datatypes_c::visit( or_expression_c *symbol) {return handle_binary_expression (widen_OR_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( xor_expression_c *symbol) {return handle_binary_expression (widen_XOR_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( and_expression_c *symbol) {return handle_binary_expression (widen_AND_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652:
msousa@652: void *fill_candidate_datatypes_c::visit( equ_expression_c *symbol) {return handle_equality_comparison(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit(notequ_expression_c *symbol) {return handle_equality_comparison(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( lt_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( gt_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( le_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( ge_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652:
msousa@652: void *fill_candidate_datatypes_c::visit( add_expression_c *symbol) {return handle_binary_expression (widen_ADD_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( sub_expression_c *symbol) {return handle_binary_expression (widen_SUB_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( mul_expression_c *symbol) {return handle_binary_expression (widen_MUL_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( div_expression_c *symbol) {return handle_binary_expression (widen_DIV_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( mod_expression_c *symbol) {return handle_binary_expression (widen_MOD_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@652: void *fill_candidate_datatypes_c::visit( power_expression_c *symbol) {return handle_binary_expression (widen_EXPT_table, symbol, symbol->l_exp, symbol->r_exp);}
msousa@417:
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(neg_expression_c *symbol) {
msousa@435: /* NOTE: The standard defines the syntax for this 'negation' operation, but
msousa@435: * does not define the its semantics.
msousa@435: *
msousa@435: * We could be tempted to consider that the semantics of the
msousa@435: * 'negation' operation are similar/identical to the semantics of the
msousa@435: * SUB expression/operation. This would include assuming that the
msousa@435: * possible datatypes for the 'negation' operation is also
msousa@435: * the same as those for the SUB expression/operation, namely ANY_MAGNITUDE.
msousa@435: *
msousa@435: * However, this would then mean that the following ST code would be
msousa@435: * syntactically and semantically correct:
msousa@650: * VAR uint_var : UINT END_VAR;
msousa@435: * uint_var := - (uint_var);
msousa@435: *
msousa@650: * Assuming uint_var is not 0, the standard states that the above code should result in a
msousa@650: * runtime error since the operation will result in an overflow. Since the above operation
msousa@650: * is only valid when uint_var=0, it would probably make more sense for the programmer to
msousa@650: * use if (uint_var=0) ..., so we will simply assume that the above statement simply
msousa@650: * does not make sense in any situation (whether or not uint_var is 0), and therefore
msousa@650: * we will not allow it.
msousa@650: * (Notice that doing so does not ago against the standard, as the standard does not
msousa@650: * explicitly define the semantics of the NEG operator, nor the data types it may accept
msousa@650: * as input. We are simply assuming that the NEG operator may not be applied to unsigned
msousa@650: * ANY_NUM data types!).
msousa@435: *
msousa@435: * It is much easier for the compiler to detect this at compile time,
msousa@435: * and it is probably safer to the resulting code too.
msousa@435: *
msousa@435: * To detect these tyes of errors at compile time, the easisest solution
msousa@435: * is to only allow ANY_NUM datatytpes that are signed.
msousa@435: * So, that is what we do here!
msousa@650: *
msousa@650: * NOTE: The above argument also applies to the neg_integer_c method!
msousa@435: */
msousa@417: symbol->exp->accept(*this);
msousa@417: for (unsigned int i = 0; i < symbol->exp->candidate_datatypes.size(); i++) {
msousa@666: if (get_datatype_info_c::is_ANY_signed_MAGNITUDE_compatible(symbol->exp->candidate_datatypes[i]))
msousa@465: add_datatype_to_candidate_list(symbol, symbol->exp->candidate_datatypes[i]);
msousa@417: }
msousa@417: if (debug) std::cout << "neg [" << symbol->exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(not_expression_c *symbol) {
msousa@417: symbol->exp->accept(*this);
msousa@417: for (unsigned int i = 0; i < symbol->exp->candidate_datatypes.size(); i++) {
msousa@666: if (get_datatype_info_c::is_ANY_BIT_compatible(symbol->exp->candidate_datatypes[i]))
msousa@465: add_datatype_to_candidate_list(symbol, symbol->exp->candidate_datatypes[i]);
msousa@417: }
msousa@417: if (debug) std::cout << "not [" << symbol->exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(function_invocation_c *symbol) {
msousa@438: if (NULL != symbol->formal_param_list) symbol-> formal_param_list->accept(*this);
msousa@438: else if (NULL != symbol->nonformal_param_list) symbol->nonformal_param_list->accept(*this);
msousa@417: else ERROR;
msousa@438:
msousa@438: generic_function_call_t fcall_param = {
conti@763: function_name: symbol->function_name,
conti@763: nonformal_operand_list: symbol->nonformal_param_list,
conti@763: formal_operand_list: symbol->formal_param_list,
conti@763: POU_type: generic_function_call_t::POU_function,
conti@763: candidate_functions: symbol->candidate_functions,
conti@763: called_function_declaration: symbol->called_function_declaration,
conti@763: extensible_param_count: symbol->extensible_param_count
msousa@438: };
conti@763:
msousa@438: handle_function_call(symbol, fcall_param);
msousa@438:
msousa@438: if (debug) std::cout << "function_invocation_c [" << symbol->candidate_datatypes.size() << "] result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@421:
msousa@421:
msousa@417: /********************/
msousa@417: /* B 3.2 Statements */
msousa@417: /********************/
msousa@417: // SYM_LIST(statement_list_c)
msousa@417: /* The visitor of the base class search_visitor_c will handle calling each instruction in the list.
msousa@417: * We do not need to do anything here...
msousa@417: */
msousa@417: // void *fill_candidate_datatypes_c::visit(statement_list_c *symbol)
msousa@417:
msousa@417:
msousa@417: /*********************************/
msousa@417: /* B 3.2.1 Assignment Statements */
msousa@417: /*********************************/
msousa@417: void *fill_candidate_datatypes_c::visit(assignment_statement_c *symbol) {
msousa@417: symbol_c *left_type, *right_type;
msousa@417:
msousa@417: symbol->l_exp->accept(*this);
msousa@417: symbol->r_exp->accept(*this);
msousa@417: for (unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
msousa@417: left_type = symbol->l_exp->candidate_datatypes[i];
msousa@417: right_type = symbol->r_exp->candidate_datatypes[j];
msousa@676: if (get_datatype_info_c::is_type_equal(left_type, right_type))
msousa@465: add_datatype_to_candidate_list(symbol, left_type);
msousa@417: }
msousa@417: }
msousa@417: if (debug) std::cout << ":= [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n";
msousa@417: return NULL;
msousa@417: }
msousa@417:
conti@418: /*****************************************/
conti@418: /* B 3.2.2 Subprogram Control Statements */
conti@418: /*****************************************/
conti@418: void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
mjsousa@834: symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
mjsousa@834: if (! get_datatype_info_c::is_function_block(fb_decl )) fb_decl = NULL;
mjsousa@834: if (NULL == fb_decl) ERROR; /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
msousa@455:
msousa@455: if (symbol-> formal_param_list != NULL) symbol->formal_param_list->accept(*this);
msousa@455: if (symbol->nonformal_param_list != NULL) symbol->nonformal_param_list->accept(*this);
msousa@424:
msousa@431: /* The print_datatypes_error_c does not rely on this called_fb_declaration pointer being != NULL to conclude that
msousa@431: * we have a datat type incompatibility error, so setting it to the correct fb_decl is actually safe,
msousa@431: * as the compiler will never reach the compilation stage!
msousa@431: */
msousa@431: symbol->called_fb_declaration = fb_decl;
msousa@424:
conti@418: if (debug) std::cout << "FB [] ==> " << symbol->candidate_datatypes.size() << " result.\n";
conti@418: return NULL;
conti@418: }
conti@418:
msousa@417:
msousa@417:
msousa@417: /********************************/
msousa@417: /* B 3.2.3 Selection Statements */
msousa@417: /********************************/
msousa@417: void *fill_candidate_datatypes_c::visit(if_statement_c *symbol) {
msousa@417: symbol->expression->accept(*this);
msousa@417: if (NULL != symbol->statement_list)
msousa@417: symbol->statement_list->accept(*this);
msousa@417: if (NULL != symbol->elseif_statement_list)
msousa@417: symbol->elseif_statement_list->accept(*this);
msousa@417: if (NULL != symbol->else_statement_list)
msousa@417: symbol->else_statement_list->accept(*this);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(elseif_statement_c *symbol) {
msousa@417: symbol->expression->accept(*this);
msousa@417: if (NULL != symbol->statement_list)
msousa@417: symbol->statement_list->accept(*this);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417: /* CASE expression OF case_element_list ELSE statement_list END_CASE */
msousa@417: // SYM_REF3(case_statement_c, expression, case_element_list, statement_list)
msousa@417: void *fill_candidate_datatypes_c::visit(case_statement_c *symbol) {
msousa@417: symbol->expression->accept(*this);
msousa@417: if (NULL != symbol->case_element_list)
msousa@417: symbol->case_element_list->accept(*this);
msousa@417: if (NULL != symbol->statement_list)
msousa@417: symbol->statement_list->accept(*this);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417: /* helper symbol for case_statement */
msousa@417: // SYM_LIST(case_element_list_c)
msousa@417: /* NOTE: visitor method for case_element_list_c is not required since we inherit from iterator_visitor_c */
msousa@417:
msousa@417: /* case_list ':' statement_list */
msousa@417: // SYM_REF2(case_element_c, case_list, statement_list)
msousa@417: /* NOTE: visitor method for case_element_c is not required since we inherit from iterator_visitor_c */
msousa@417:
msousa@417: // SYM_LIST(case_list_c)
msousa@417: /* NOTE: visitor method for case_list_c is not required since we inherit from iterator_visitor_c */
msousa@417:
msousa@417: /********************************/
msousa@417: /* B 3.2.4 Iteration Statements */
msousa@417: /********************************/
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(for_statement_c *symbol) {
msousa@417: symbol->control_variable->accept(*this);
msousa@417: symbol->beg_expression->accept(*this);
msousa@417: symbol->end_expression->accept(*this);
msousa@417: if (NULL != symbol->by_expression)
msousa@417: symbol->by_expression->accept(*this);
msousa@417: if (NULL != symbol->statement_list)
msousa@417: symbol->statement_list->accept(*this);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(while_statement_c *symbol) {
msousa@417: symbol->expression->accept(*this);
msousa@417: if (NULL != symbol->statement_list)
msousa@417: symbol->statement_list->accept(*this);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417: void *fill_candidate_datatypes_c::visit(repeat_statement_c *symbol) {
msousa@417: symbol->expression->accept(*this);
msousa@417: if (NULL != symbol->statement_list)
msousa@417: symbol->statement_list->accept(*this);
msousa@417: return NULL;
msousa@417: }
msousa@417:
msousa@417:
msousa@417:
msousa@417:
msousa@417:
msousa@417: