msousa@417: /* msousa@417: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@417: * msousa@417: * Copyright (C) 2009-2011 Mario de Sousa (msousa@fe.up.pt) msousa@486: * Copyright (C) 2011-2012 Manuele Conti (manuele.conti@sirius-es.it) msousa@486: * Copyright (C) 2011-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@552: /* NOTE: The algorithm implemented here assumes that the symbol_c.candidate_datatype, and the symbol_c.datatype msousa@552: * annotations have already been apropriately filled in! msousa@552: * BEFORE running this visitor, be sure to CALL the fill_candidate_datatypes_c, and the narrow_candidate_datatypes_c visitors! msousa@552: */ msousa@552: msousa@417: msousa@417: /* msousa@552: * By analysing the candidate datatype lists, as well as the chosen datatype for each expression, determine msousa@552: * if an datatype error has been found, and if so, print out an error message. msousa@417: */ msousa@417: msousa@552: msousa@417: #include "print_datatypes_error.hh" msousa@434: #include "datatype_functions.hh" msousa@434: msousa@417: #include msousa@417: #include msousa@417: #include msousa@417: #include msousa@417: #include msousa@417: msousa@417: msousa@427: msousa@427: msousa@427: msousa@427: msousa@417: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) msousa@417: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) msousa@417: msousa@427: #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ msousa@427: if (current_display_error_level >= error_level) { \ conti@458: fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ msousa@427: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ msousa@427: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ msousa@427: fprintf(stderr, __VA_ARGS__); \ msousa@427: fprintf(stderr, "\n"); \ msousa@427: il_error = true; \ msousa@510: error_count++; \ msousa@427: } \ msousa@427: } msousa@417: msousa@417: conti@458: #define STAGE3_WARNING(symbol1, symbol2, ...) { \ conti@458: fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ conti@458: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ conti@458: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ conti@458: fprintf(stderr, __VA_ARGS__); \ conti@458: fprintf(stderr, "\n"); \ conti@458: warning_found = true; \ conti@458: } conti@458: conti@458: msousa@417: /* set to 1 to see debug info during execution */ msousa@417: static int debug = 0; msousa@417: msousa@417: print_datatypes_error_c::print_datatypes_error_c(symbol_c *ignore) { msousa@510: error_count = 0; conti@458: warning_found = false; msousa@427: current_display_error_level = error_level_default; msousa@417: } msousa@417: msousa@417: print_datatypes_error_c::~print_datatypes_error_c(void) { msousa@417: } msousa@417: msousa@510: int print_datatypes_error_c::get_error_count() { msousa@510: return error_count; msousa@417: } msousa@417: msousa@459: msousa@459: msousa@459: msousa@459: msousa@689: /* Verify if the datatypes of all symbols in the vector are valid and equal! */ msousa@689: static bool are_all_datatypes_equal(std::vector &symbol_vect) { msousa@689: if (symbol_vect.size() <= 0) return false; msousa@689: msousa@689: bool res = get_datatype_info_c::is_type_valid(symbol_vect[0]->datatype); msousa@689: for (unsigned int i = 1; i < symbol_vect.size(); i++) msousa@689: res &= get_datatype_info_c::is_type_equal(symbol_vect[i-1]->datatype, symbol_vect[i]->datatype); msousa@459: return res; msousa@459: } msousa@459: msousa@459: msousa@459: msousa@459: 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: void print_datatypes_error_c::handle_function_invocation(symbol_c *fcall, generic_function_call_t fcall_data) { msousa@438: symbol_c *param_value, *param_name; msousa@438: function_call_param_iterator_c fcp_iterator(fcall); msousa@438: bool function_invocation_error = false; msousa@441: const char *POU_str = NULL; msousa@441: msousa@441: if (generic_function_call_t::POU_FB == fcall_data.POU_type) POU_str = "FB"; msousa@441: if (generic_function_call_t::POU_function == fcall_data.POU_type) POU_str = "function"; msousa@441: if (NULL == POU_str) ERROR; msousa@438: msousa@438: if ((NULL != fcall_data.formal_operand_list) && (NULL != fcall_data.nonformal_operand_list)) msousa@438: ERROR; msousa@438: msousa@438: symbol_c *f_decl = fcall_data.called_function_declaration; msousa@441: if ((NULL == f_decl) && (generic_function_call_t::POU_FB ==fcall_data.POU_type)) { msousa@441: /* Due to the way the syntax analysis is buit (i.e. stage 2), this should never occur. */ msousa@441: /* I.e., a FB invocation using an undefined FB variable is not possible in the current implementation of stage 2. */ msousa@441: ERROR; msousa@441: } msousa@438: if (NULL == f_decl) { msousa@438: /* we now try to find any function declaration with the same name, just so we can provide some relevant error messages */ msousa@438: function_symtable_t::iterator lower = function_symtable.lower_bound(fcall_data.function_name); msousa@438: if (lower == function_symtable.end()) ERROR; msousa@438: f_decl = function_symtable.get_value(lower); msousa@438: } msousa@438: msousa@438: if (NULL != fcall_data.formal_operand_list) { msousa@438: fcall_data.formal_operand_list->accept(*this); msousa@438: if (NULL != f_decl) { msousa@438: function_param_iterator_c fp_iterator(f_decl); msousa@438: while ((param_name = fcp_iterator.next_f()) != NULL) { msousa@438: param_value = fcp_iterator.get_current_value(); mjsousa@848: msousa@449: /* Check if there are duplicate parameter values */ msousa@449: if(fcp_iterator.search_f(param_name) != param_value) { msousa@449: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, param_name, param_name, "Duplicate parameter '%s' when invoking %s '%s'", ((token_c *)param_name)->value, POU_str, ((token_c *)fcall_data.function_name)->value); msousa@449: continue; /* jump to next parameter */ msousa@449: } msousa@449: msousa@438: /* Find the corresponding parameter in function declaration */ msousa@438: if (NULL == fp_iterator.search(param_name)) { msousa@449: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, param_name, param_name, "Invalid parameter '%s' when invoking %s '%s'", ((token_c *)param_name)->value, POU_str, ((token_c *)fcall_data.function_name)->value); msousa@449: continue; /* jump to next parameter */ msousa@449: } msousa@449: msousa@449: /* check whether direction (IN, OUT, IN_OUT) and assignment types (:= , =>) are compatible !!! */ 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: /* 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: 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: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, param_name, param_name, "Invalid assignment syntax ':=' used for parameter '%s', when invoking %s '%s'", ((token_c *)param_name)->value, POU_str, ((token_c *)fcall_data.function_name)->value); msousa@449: continue; /* jump to next parameter */ msousa@449: } 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: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, param_name, param_name, "Invalid assignment syntax '=>' used for parameter '%s', when invoking %s '%s'", ((token_c *)param_name)->value, POU_str, ((token_c *)fcall_data.function_name)->value); msousa@449: continue; /* jump to next parameter */ msousa@449: } msousa@449: } else ERROR; msousa@449: mjsousa@848: if (!get_datatype_info_c::is_type_valid(param_value->datatype)) { msousa@438: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, param_value, param_value, "Data type incompatibility between parameter '%s' and value being passed, when invoking %s '%s'", ((token_c *)param_name)->value, POU_str, ((token_c *)fcall_data.function_name)->value); msousa@449: continue; /* jump to next parameter */ msousa@438: } msousa@438: } msousa@438: } msousa@438: } msousa@438: if (NULL != fcall_data.nonformal_operand_list) { msousa@438: if (f_decl) msousa@438: for (int i = 1; (param_value = fcp_iterator.next_nf()) != NULL; i++) { msousa@455: /* TODO: verify if it is lvalue when INOUT or OUTPUT parameters! */ msousa@455: msousa@492: /* This handle_function_invocation() will be called to handle IL function calls, where the first parameter comes from the previous IL instruction. msousa@492: * In this case, the previous IL instruction will be artifically (and temporarily) added to the begining ot the parameter list msousa@492: * so we (in this function) can handle this situation like all the other function calls. msousa@492: * However, msousa@492: * a) if NO previous IL function exists, then we get a fake previous IL function, with no location data (i.e. not found anywhere in the source code. msousa@492: * b) the function call may actually have several prev IL instructions (if several JMP instructions jump directly to the il function call). msousa@492: * In order to handle these situations gracefully, we first check whether the first parameter is really an IL istruction! msousa@492: */ msousa@492: il_instruction_c *il_instruction_symbol = dynamic_cast(param_value); msousa@492: if ((NULL != il_instruction_symbol) && (i == 1)) { msousa@492: /* We are in a situation where an IL function call is passed the first parameter, which is actually the previous IL instruction */ msousa@492: /* However, this is really a fake previous il instruction (see visit(il_instruction_c *) ) msousa@492: * We will iterate through all the real previous IL instructions, and analyse each of them one by one */ msousa@492: if (il_instruction_symbol->prev_il_instruction.size() == 0) { msousa@492: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, fcall, fcall, "No available data to pass to first parameter of IL function %s. Missing a previous LD instruction?", ((token_c *)fcall_data.function_name)->value); msousa@492: } msousa@492: #if 0 msousa@492: /* NOTE: We currently comment out this code... msousa@492: * This does not currently work, since the narrow operation is currently done on the intersection msousa@492: * of all the previous IL instructions, so we currently either accept them all, or none at all. msousa@492: * In order to be able to produce these user freindly error messages, we will need to update the msousa@492: * narrow algorithm. We leave this untill somebody aks for it... msousa@492: * So, for now, we simply comment out this code. msousa@492: */ msousa@492: for (unsigned int p = 0; p < il_instruction_symbol->prev_il_instruction.size(); p++) { msousa@492: symbol_c *value = il_instruction_symbol->prev_il_instruction[p]; msousa@676: if (!get_datatype_info_c::is_type_valid(value->datatype)) { msousa@492: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, fcall, fcall, "Data type incompatibility for value passed to first parameter when invoking function '%s'", ((token_c *)fcall_data.function_name)->value); mjsousa@958: STAGE3_ERROR(0, value, value, "This is the IL instruction producing the incompatible data type to first parameter of function '%s'", ((token_c *)fcall_data.function_name)->value); msousa@492: } msousa@492: } msousa@492: #else msousa@676: if (!get_datatype_info_c::is_type_valid(il_instruction_symbol->datatype)) { msousa@492: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, fcall, fcall, "Data type incompatibility between value in IL 'accumulator' and first parameter of function '%s'", ((token_c *)fcall_data.function_name)->value); msousa@492: } msousa@492: #endif msousa@492: if (function_invocation_error) msousa@492: /* when handling a IL function call, and an error is found in the first parameter, then we bug out and do not print out any more error messages. */ msousa@492: return; mjsousa@847: } else { mjsousa@847: if (!get_datatype_info_c::is_type_valid(param_value->datatype)) { mjsousa@847: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, param_value, param_value, "Data type incompatibility for value passed in position %d when invoking %s '%s'", i, POU_str, ((token_c *)fcall_data.function_name)->value); mjsousa@847: } mjsousa@847: param_value->accept(*this); msousa@438: } msousa@438: } msousa@438: } msousa@438: msousa@449: if (NULL == fcall_data.called_function_declaration) { msousa@449: function_invocation_error = true; mjsousa@958: STAGE3_ERROR(0, fcall, fcall, "Unable to resolve which overloaded %s '%s' is being invoked.", POU_str, ((token_c *)fcall_data.function_name)->value); msousa@449: } msousa@449: msousa@438: if (function_invocation_error) { msousa@438: /* No compatible function exists */ mjsousa@958: STAGE3_ERROR(2, fcall, fcall, "Invalid parameters when invoking %s '%s'", POU_str, ((token_c *)fcall_data.function_name)->value); msousa@438: } msousa@438: msousa@438: return; msousa@438: } msousa@438: msousa@455: msousa@455: msousa@482: void *print_datatypes_error_c::handle_implicit_il_fb_invocation(const char *param_name, symbol_c *il_operator, symbol_c *called_fb_declaration) { msousa@448: if (NULL == il_operand) { msousa@448: STAGE3_ERROR(0, il_operator, il_operator, "Missing operand for FB call operator '%s'.", param_name); msousa@482: return NULL; msousa@448: } mjsousa@834: // il_operand->accept(*this); // NOT required. The il_simple_operation_c already visits it!! msousa@455: msousa@448: if (NULL == called_fb_declaration) { msousa@455: STAGE3_ERROR(0, il_operator, il_operand, "Invalid FB call: operand is not a FB instance."); msousa@482: return NULL; msousa@448: } msousa@455: msousa@459: if (fake_prev_il_instruction->prev_il_instruction.empty()) { msousa@448: STAGE3_ERROR(0, il_operator, il_operand, "FB invocation operator '%s' must be preceded by a 'LD' (or equivalent) operator.", param_name); msousa@482: return NULL; msousa@448: } msousa@455: msousa@448: /* Find the corresponding parameter in function declaration */ msousa@448: function_param_iterator_c fp_iterator(called_fb_declaration); msousa@448: if (NULL == fp_iterator.search(param_name)) { msousa@455: /* TODO: must also check whther it is an IN parameter!! */ msousa@455: /* NOTE: although all standard FBs have the implicit FB calls defined as input parameters msousa@455: * (i.e., for all standard FBs, CLK, PT, IN, CU, CD, S1, R1, etc... is always an input parameter) msousa@455: * if a non-standard (i.e. a FB not defined in the standard library) FB is being called, then msousa@455: * this (CLK, PT, IN, CU, ...) parameter may just have been defined as OUT or INOUT, msousa@455: * which will not work for an implicit FB call! msousa@455: */ msousa@448: STAGE3_ERROR(0, il_operator, il_operand, "FB called by '%s' operator does not have a parameter named '%s'", param_name, param_name); msousa@482: return NULL; msousa@448: } msousa@689: if (!are_all_datatypes_equal(fake_prev_il_instruction->prev_il_instruction)) { msousa@455: STAGE3_ERROR(0, il_operator, il_operand, "Data type incompatibility between parameter '%s' and value being passed.", param_name); msousa@482: return NULL; msousa@448: } msousa@455: msousa@455: msousa@455: /* NOTE: The error_level currently being used for errors in variables/constants etc... is rather high. msousa@455: * However, in the case of an implicit FB call, if the datatype of the operand == NULL, this may be msousa@455: * the __only__ indication of an error! So we test it here again, to make sure thtis error will really msousa@455: * be printed out! msousa@455: */ mjsousa@848: if (!get_datatype_info_c::is_type_valid(il_operand->datatype)) { msousa@455: /* Note: the case of (NULL == fb_declaration) was already caught above! */ msousa@455: // if (NULL != fb_declaration) { msousa@455: STAGE3_ERROR(0, il_operator, il_operator, "Invalid FB call: Datatype incompatibility between the FB's '%s' parameter and value being passed, or paramater '%s' is not a 'VAR_INPUT' parameter.", param_name, param_name); msousa@482: return NULL; msousa@455: // } msousa@455: } msousa@482: msousa@482: return NULL; msousa@448: } msousa@438: msousa@438: msousa@417: /*********************/ msousa@417: /* B 1.2 - Constants */ msousa@417: /*********************/ msousa@417: /******************************/ msousa@417: /* B 1.2.1 - Numeric Literals */ msousa@417: /******************************/ msousa@417: void *print_datatypes_error_c::visit(real_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for ANY_REAL data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_REAL data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(integer_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for ANY_INT data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_INT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(neg_real_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for ANY_REAL data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_REAL data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(neg_integer_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for ANY_INT data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_INT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(binary_integer_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for ANY_INT data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_INT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(octal_integer_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for ANY_INT data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_INT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(hex_integer_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for ANY_INT data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_INT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(integer_literal_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@778: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for %s data type.", get_datatype_info_c::get_id_str(symbol->type)); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_INT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(real_literal_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@778: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for %s data type.", get_datatype_info_c::get_id_str(symbol->type)); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_REAL data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(bit_string_literal_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@778: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for %s data type.", get_datatype_info_c::get_id_str(symbol->type)); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_BIT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(boolean_literal_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@778: STAGE3_ERROR(0, symbol, symbol, "Value is not valid for %s data type.", get_datatype_info_c::get_id_str(symbol->type)); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_BOOL data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(boolean_true_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Value is not valid for ANY_BOOL data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_BOOL data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(boolean_false_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Value is not valid for ANY_BOOL data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "ANY_BOOL data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /*******************************/ msousa@417: /* B.1.2.2 Character Strings */ msousa@417: /*******************************/ msousa@417: void *print_datatypes_error_c::visit(double_byte_character_string_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for WSTRING data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "WSTRING data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(single_byte_character_string_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Numerical value exceeds range for STRING data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "STRING data type not valid in this location."); msousa@417: } 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 *print_datatypes_error_c::visit(duration_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Invalid syntax for TIME data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "TIME data type not valid in this location."); msousa@417: } 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@417: void *print_datatypes_error_c::visit(time_of_day_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Invalid syntax for TOD data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "TOD data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(date_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Invalid syntax for DATE data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "DATE data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(date_and_time_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) { msousa@427: STAGE3_ERROR(0, symbol, symbol, "Invalid syntax for DT data type."); mjsousa@848: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@427: STAGE3_ERROR(4, symbol, symbol, "DT data type not valid in this location."); msousa@417: } msousa@417: return NULL; msousa@417: } 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@502: void *print_datatypes_error_c::visit(simple_spec_init_c *symbol) { msousa@676: if (!get_datatype_info_c::is_type_valid(symbol->simple_specification->datatype)) { msousa@502: STAGE3_ERROR(0, symbol->simple_specification, symbol->simple_specification, "Invalid data type."); msousa@502: } else if (NULL != symbol->constant) { msousa@676: if (!get_datatype_info_c::is_type_valid(symbol->constant->datatype)) msousa@502: STAGE3_ERROR(0, symbol->constant, symbol->constant, "Initial value has incompatible data type."); msousa@676: } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { msousa@502: ERROR; /* If we have an error here, then we must also have an error in one of msousa@502: * the two previous tests. If we reach this point, some strange error is ocurring! msousa@502: */ msousa@502: } msousa@502: return NULL; msousa@502: } msousa@502: msousa@417: msousa@417: void *print_datatypes_error_c::visit(enumerated_value_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) msousa@427: STAGE3_ERROR(0, symbol, symbol, "Ambiguous enumerate value or Variable not declared in this scope."); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@1047: msousa@1047: void *print_datatypes_error_c::visit(structure_element_initialization_c *symbol) { msousa@1047: symbol->value->accept(*this); msousa@1047: if (!get_datatype_info_c::is_type_valid(symbol->datatype)) msousa@1047: STAGE3_ERROR(0, symbol, symbol, "Initialization element identifier (%s) is not declared in referenced structure/FB scope, or is set to value of incompatible datatype.", msousa@1047: symbol->structure_element_name->token->value); msousa@1047: return NULL; msousa@1047: } msousa@1047: msousa@1047: msousa@417: /*********************/ msousa@417: /* B 1.4 - Variables */ msousa@417: /*********************/ msousa@417: void *print_datatypes_error_c::visit(symbolic_variable_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() == 0) msousa@427: STAGE3_ERROR(0, symbol, symbol, "Variable not declared in this scope."); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /********************************************/ msousa@417: /* B 1.4.1 - Directly Represented Variables */ msousa@417: /********************************************/ msousa@417: void *print_datatypes_error_c::visit(direct_variable_c *symbol) { msousa@502: if (symbol->candidate_datatypes.size() == 0) ERROR; msousa@676: if (!get_datatype_info_c::is_type_valid(symbol->datatype)) msousa@502: STAGE3_ERROR(4, symbol, symbol, "Direct variable has incompatible data type with expression."); 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 *print_datatypes_error_c::visit(array_variable_c *symbol) { mjsousa@827: /* the subscripted variable may be a structure or another array variable, that must also be visisted! */ mjsousa@827: /* Please read the comments in the array_variable_c and structured_variable_c visitors in the fill_candidate_datatypes.cc file! */ mjsousa@827: symbol->subscripted_variable->accept(*this); mjsousa@827: msousa@417: if (symbol->candidate_datatypes.size() == 0) msousa@427: STAGE3_ERROR(0, symbol, symbol, "Array variable not declared in this scope."); msousa@417: msousa@417: /* recursively call the subscript list to print any errors in the expressions used in the subscript...*/ msousa@417: symbol->subscript_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /* subscript_list ',' subscript */ msousa@417: // SYM_LIST(subscript_list_c) conti@549: void *print_datatypes_error_c::visit(subscript_list_c *symbol) { conti@549: for (int i = 0; i < symbol->n; i++) { msousa@550: int start_error_count = error_count; msousa@1041: symbol->get_element(i)->accept(*this); msousa@551: /* The following error message will only get printed if the current_display_error_level is set higher than 0! */ msousa@1041: if ((start_error_count == error_count) && (!get_datatype_info_c::is_type_valid(symbol->get_element(i)->datatype))) conti@549: STAGE3_ERROR(0, symbol, symbol, "Invalid data type for array subscript field."); conti@549: } conti@549: return NULL; conti@549: } 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 *print_datatypes_error_c::visit(structured_variable_c *symbol) { mjsousa@827: /* the record variable may be another structure or even an array variable, that must also be visisted! */ mjsousa@827: /* Please read the comments in the array_variable_c and structured_variable_c visitors in the fill_candidate_datatypes.cc file! */ mjsousa@827: symbol->record_variable->accept(*this); mjsousa@827: msousa@417: if (symbol->candidate_datatypes.size() == 0) mjsousa@827: STAGE3_ERROR(0, symbol, symbol, "Undeclared structured (or FB) variable, or non-existant field (variable) in structure (FB)."); 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: /* AT direct_variable */ msousa@502: // SYM_REF1(location_c, direct_variable) msousa@502: void *print_datatypes_error_c::visit(location_c *symbol) { msousa@502: symbol->direct_variable->accept(*this); 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 *print_datatypes_error_c::visit(located_var_decl_c *symbol) { msousa@502: symbol->located_var_spec_init->accept(*this); msousa@502: /* It does not make sense to call symbol->location->accept(*this). The check is done right here if the following if() */ msousa@502: // symbol->location->accept(*this); msousa@676: if ((get_datatype_info_c::is_type_valid(symbol->located_var_spec_init->datatype)) && (!get_datatype_info_c::is_type_valid(symbol->location->datatype))) msousa@558: STAGE3_ERROR(0, symbol, symbol, "Bit size of data type is incompatible with bit size of location."); msousa@502: return NULL; 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 *print_datatypes_error_c::visit(function_declaration_c *symbol) { msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@732: symbol->var_declarations_list->accept(*this); msousa@417: if (debug) printf("Print error data types list in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); msousa@417: il_parenthesis_level = 0; msousa@417: il_error = false; msousa@417: symbol->function_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /***************************/ msousa@417: /* B 1.5.2 Function blocks */ msousa@417: /***************************/ msousa@417: void *print_datatypes_error_c::visit(function_block_declaration_c *symbol) { msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@732: symbol->var_declarations->accept(*this); msousa@417: if (debug) printf("Print error data types list in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); msousa@417: il_parenthesis_level = 0; msousa@417: il_error = false; msousa@417: symbol->fblock_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /**********************/ msousa@417: /* B 1.5.3 - Programs */ msousa@417: /**********************/ msousa@417: void *print_datatypes_error_c::visit(program_declaration_c *symbol) { msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@502: symbol->var_declarations->accept(*this); msousa@417: if (debug) printf("Print error data types list in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); msousa@417: il_parenthesis_level = 0; msousa@417: il_error = false; msousa@417: symbol->function_block_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: mjsousa@895: /********************************************/ mjsousa@895: /* B 1.6 Sequential function chart elements */ mjsousa@895: /********************************************/ mjsousa@895: void *print_datatypes_error_c::visit(transition_condition_c *symbol) { mjsousa@895: if (symbol->transition_condition_il != NULL) symbol->transition_condition_il->accept(*this); mjsousa@895: if (symbol->transition_condition_st != NULL) symbol->transition_condition_st->accept(*this); mjsousa@895: mjsousa@895: if (!get_datatype_info_c::is_type_valid(symbol->datatype)) mjsousa@895: STAGE3_ERROR(0, symbol, symbol, "Transition condition has invalid data type (should be BOOL)."); mjsousa@895: return NULL; mjsousa@895: } mjsousa@895: msousa@417: msousa@417: /********************************/ msousa@417: /* B 1.7 Configuration elements */ msousa@417: /********************************/ msousa@417: void *print_datatypes_error_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@454: msousa@417: // void *visit(instruction_list_c *symbol); msousa@454: msousa@454: /* | label ':' [il_incomplete_instruction] eol_list */ msousa@454: // SYM_REF2(il_instruction_c, label, il_instruction) msousa@454: void *print_datatypes_error_c::visit(il_instruction_c *symbol) { msousa@454: if (NULL != symbol->il_instruction) { msousa@459: il_instruction_c tmp_prev_il_instruction(NULL, NULL); msousa@492: #if 0 msousa@492: /* NOTE: The following is currently no longer needed. Since the following code is actually cool, msousa@492: * we don't delete it, but simply comment it out. It might just come in handy later on... msousa@492: */ msousa@486: /* When handling a il function call, this fake_prev_il_instruction may be used as a standard function call parameter, so it is important that msousa@486: * it contain some valid location info so error messages make sense. msousa@486: */ msousa@486: if (symbol->prev_il_instruction.size() > 0) { msousa@486: /* since we don't want to copy all that data one variable at a time, we copy it all at once */ msousa@486: /* This has the advantage that, if we ever add some more data to the base symbol_c later on, we will not need to msousa@486: * change the following line to guarantee that the data is copied correctly! msousa@486: * However, it does have the drawback of copying more data than what we want! msousa@486: * In order to only copy the data in the base class symbol_c, we use the tmp_symbol pointer! msousa@486: * I (mario) have checked with a debugger, and it is working as intended! msousa@486: */ msousa@486: symbol_c *tmp_symbol1 = symbol->prev_il_instruction[0]; msousa@486: symbol_c *tmp_symbol2 = &tmp_prev_il_instruction; msousa@486: *tmp_symbol2 = *tmp_symbol1; msousa@486: /* we do not want to copy the datatype variable, so we reset it to NULL */ msousa@486: tmp_prev_il_instruction.datatype = NULL; msousa@486: /* We don't need to worry about the candidate_datatype list (which we don't want to copy just yet), since that will msousa@486: * be reset to the correct value when we call intersect_prev_candidate_datatype_lists() later on... msousa@486: */ msousa@486: } msousa@492: #endif msousa@492: /* the print error algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the msousa@459: * list of the prev_il_instructions. msousa@459: * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction), msousa@459: * and shove that data into this single variable. msousa@459: */ msousa@459: tmp_prev_il_instruction.prev_il_instruction = symbol->prev_il_instruction; msousa@459: intersect_prev_candidate_datatype_lists(&tmp_prev_il_instruction); msousa@689: if (are_all_datatypes_equal(symbol->prev_il_instruction)) msousa@459: if (symbol->prev_il_instruction.size() > 0) msousa@459: tmp_prev_il_instruction.datatype = (symbol->prev_il_instruction[0])->datatype; msousa@486: msousa@459: /* Tell the il_instruction the datatype that it must generate - this was chosen by the next il_instruction (remember: we are iterating backwards!) */ msousa@459: fake_prev_il_instruction = &tmp_prev_il_instruction; msousa@454: symbol->il_instruction->accept(*this); msousa@459: fake_prev_il_instruction = NULL; msousa@454: } msousa@454: msousa@454: return NULL; msousa@454: } msousa@454: msousa@454: msousa@454: msousa@417: void *print_datatypes_error_c::visit(il_simple_operation_c *symbol) { msousa@417: il_operand = symbol->il_operand; mjsousa@834: if (NULL != il_operand) symbol->il_operand->accept(*this); /* recursive call to see whether data types are compatible */ mjsousa@834: msousa@417: symbol->il_simple_operator->accept(*this); msousa@417: il_operand = NULL; msousa@417: return NULL; msousa@417: } msousa@417: 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 *print_datatypes_error_c::visit(il_function_call_c *symbol) { msousa@451: /* The first parameter of a non formal function call in IL will be the 'current value' (i.e. the prev_il_instruction) msousa@451: * 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@451: * msousa@451: * However, if no further paramters are given, then il_operand_list will be NULL, and we will msousa@451: * need to create a new object to hold the pointer to prev_il_instruction. msousa@451: * This change will also be undone later in print_datatypes_error_c. msousa@451: */ msousa@451: if (NULL == symbol->il_operand_list) symbol->il_operand_list = new il_operand_list_c; msousa@451: if (NULL == symbol->il_operand_list) ERROR; msousa@451: msousa@459: ((list_c *)symbol->il_operand_list)->insert_element(fake_prev_il_instruction, 0); msousa@451: msousa@438: generic_function_call_t fcall_param = { msousa@441: /* fcall_param.function_name = */ symbol->function_name, msousa@441: /* fcall_param.nonformal_operand_list = */ symbol->il_operand_list, msousa@441: /* fcall_param.formal_operand_list = */ NULL, 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: msousa@448: /* TODO: check what error message (if any) the compiler will give out if this function invocation msousa@448: * is not preceded by a LD operator (or another equivalent operator or list of operators). msousa@448: */ msousa@438: handle_function_invocation(symbol, fcall_param); msousa@438: msousa@459: /* We now undo those changes to the abstract syntax tree made above! */ msousa@438: ((list_c *)symbol->il_operand_list)->remove_element(0); msousa@438: if (((list_c *)symbol->il_operand_list)->n == 0) { msousa@438: /* if the list becomes empty, then that means that it did not exist before we made these changes, so we delete it! */ msousa@438: delete symbol->il_operand_list; msousa@438: symbol->il_operand_list = NULL; msousa@438: } msousa@438: msousa@417: return NULL; msousa@417: } msousa@417: msousa@454: msousa@454: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ msousa@454: // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); msousa@417: void *print_datatypes_error_c::visit(il_expression_c *symbol) { msousa@689: /* Stage2 will insert an artificial (and equivalent) LD to the simple_instr_list if necessary. We can therefore ignore the 'il_operand' entry! */ msousa@689: msousa@454: /* first give the parenthesised IL list a chance to print errors */ msousa@459: il_instruction_c *save_fake_prev_il_instruction = fake_prev_il_instruction; msousa@454: symbol->simple_instr_list->accept(*this); msousa@459: fake_prev_il_instruction = save_fake_prev_il_instruction; msousa@454: msousa@454: /* Now handle the operation (il_expr_operator) that will use the result coming from the parenthesised IL list (i.e. simple_instr_list) */ msousa@454: il_operand = symbol->simple_instr_list; /* This is not a bug! The parenthesised expression will be used as the operator! */ msousa@454: symbol->il_expr_operator->accept(*this); msousa@454: msousa@454: return NULL; msousa@454: } msousa@454: msousa@417: msousa@441: /* il_call_operator prev_declared_fb_name msousa@441: * | il_call_operator prev_declared_fb_name '(' ')' msousa@441: * | il_call_operator prev_declared_fb_name '(' eol_list ')' msousa@441: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' msousa@441: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' msousa@441: */ msousa@441: /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ msousa@441: // 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 *print_datatypes_error_c::visit(il_fb_call_c *symbol) { msousa@441: int extensible_param_count; /* unused vairable! Needed for compilation only! */ msousa@441: std::vector candidate_functions; /* unused vairable! Needed for compilation only! */ msousa@441: generic_function_call_t fcall_param = { msousa@441: /* fcall_param.function_name = */ symbol->fb_name, msousa@441: /* fcall_param.nonformal_operand_list = */ symbol->il_operand_list, 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_FB, msousa@441: /* fcall_param.candidate_functions = */ candidate_functions, /* will not be used, but must provide a reference to be able to compile */ msousa@441: /* fcall_param.called_function_declaration = */ symbol->called_fb_declaration, msousa@441: /* fcall_param.extensible_param_count = */ extensible_param_count /* will not be used, but must provide a reference to be able to compile */ msousa@441: }; msousa@441: msousa@441: handle_function_invocation(symbol, fcall_param); msousa@455: /* check the semantics of the CALC, CALCN operators! */ msousa@455: symbol->il_call_operator->accept(*this); msousa@417: return NULL; msousa@417: } 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 *print_datatypes_error_c::visit(il_formal_funct_call_c *symbol) { 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: msousa@438: handle_function_invocation(symbol, fcall_param); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@453: // void *visit(il_operand_list_c *symbol); msousa@454: // void *visit(simple_instr_list_c *symbol); msousa@453: msousa@453: // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) msousa@454: void *print_datatypes_error_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@459: msousa@459: il_instruction_c tmp_prev_il_instruction(NULL, NULL); msousa@689: #if 0 msousa@459: /* the print error algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the msousa@459: * list of the prev_il_instructions. msousa@459: * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction), msousa@459: * and shove that data into this single variable. msousa@459: */ msousa@459: if (symbol->prev_il_instruction.size() > 0) msousa@459: tmp_prev_il_instruction.candidate_datatypes = symbol->prev_il_instruction[0]->candidate_datatypes; msousa@459: tmp_prev_il_instruction.prev_il_instruction = symbol->prev_il_instruction; msousa@689: #endif msousa@689: msousa@689: /* the print error algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the msousa@689: * list of the prev_il_instructions. msousa@689: * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction), msousa@689: * and shove that data into this single variable. msousa@689: */ msousa@689: tmp_prev_il_instruction.prev_il_instruction = symbol->prev_il_instruction; msousa@689: intersect_prev_candidate_datatype_lists(&tmp_prev_il_instruction); msousa@689: if (are_all_datatypes_equal(symbol->prev_il_instruction)) msousa@689: if (symbol->prev_il_instruction.size() > 0) msousa@689: tmp_prev_il_instruction.datatype = (symbol->prev_il_instruction[0])->datatype; msousa@689: msousa@459: msousa@459: /* copy the candidate_datatypes list */ msousa@459: fake_prev_il_instruction = &tmp_prev_il_instruction; msousa@454: symbol->il_simple_instruction->accept(*this); msousa@459: fake_prev_il_instruction = NULL; msousa@454: return NULL; msousa@459: msousa@459: msousa@459: // if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL; msousa@459: // else prev_il_instruction = symbol->prev_il_instruction[0]; msousa@459: msousa@459: // symbol->il_simple_instruction->accept(*this); msousa@459: // prev_il_instruction = NULL; msousa@459: // return NULL; msousa@454: } msousa@454: msousa@454: msousa@453: // void *visit(il_param_list_c *symbol); msousa@453: // void *visit(il_param_assignment_c *symbol); msousa@453: // void *visit(il_param_out_assignment_c *symbol); msousa@417: msousa@417: /*******************/ msousa@417: /* B 2.2 Operators */ msousa@417: /*******************/ msousa@482: void *print_datatypes_error_c::print_binary_operator_errors(const char *il_operator, symbol_c *symbol, bool deprecated_operation) { mjsousa@834: if (NULL == il_operand) { mjsousa@834: STAGE3_ERROR(0, symbol, symbol, "Missing operand for %s operator.", il_operator); // message (a) mjsousa@834: } else if ((symbol->candidate_datatypes.size() == 0) && (il_operand->candidate_datatypes.size() > 0)) { mjsousa@834: STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '%s' operator.", il_operator); // message (b) mjsousa@848: } else if (NULL == symbol->datatype) { // do NOT use !get_datatype_info_c::is_type_valid() here! mjsousa@834: STAGE3_WARNING(symbol, symbol, "Result of '%s' operation is never used.", il_operator); // message (c) msousa@482: } else if (deprecated_operation) mjsousa@834: STAGE3_WARNING(symbol, symbol, "Deprecated operation for '%s' operator.", il_operator); // message (d) mjsousa@834: return NULL; mjsousa@834: } mjsousa@834: mjsousa@834: mjsousa@834: void *print_datatypes_error_c::visit( LD_operator_c *symbol) {return print_binary_operator_errors("LD" , symbol);} // I believe it will never emit messages (b) and (c)!! mjsousa@834: void *print_datatypes_error_c::visit( LDN_operator_c *symbol) {return print_binary_operator_errors("LDN" , symbol);} // I believe it will never emit message (c) mjsousa@834: void *print_datatypes_error_c::visit( ST_operator_c *symbol) {return print_binary_operator_errors("ST" , symbol);} // I believe it will never emit message (c) mjsousa@834: void *print_datatypes_error_c::visit( STN_operator_c *symbol) {return print_binary_operator_errors("STN" , symbol);} // I believe it will never emit message (c) msousa@417: msousa@417: void *print_datatypes_error_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@470: */ mjsousa@834: if (il_operand != NULL) { msousa@470: STAGE3_ERROR(0, symbol, symbol, "'NOT' operator may not have an operand."); mjsousa@834: } else if (symbol->candidate_datatypes.size() == 0) msousa@470: STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for 'NOT' operator."); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(S_operator_c *symbol) { mjsousa@834: if (NULL != symbol->called_fb_declaration) /* FB call semantics */ return handle_implicit_il_fb_invocation("S", symbol, symbol->called_fb_declaration); mjsousa@834: else /* Reset semantics */ return print_binary_operator_errors ("S", symbol); msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(R_operator_c *symbol) { mjsousa@834: if (NULL != symbol->called_fb_declaration) /* FB call semantics */ return handle_implicit_il_fb_invocation("R", symbol, symbol->called_fb_declaration); mjsousa@834: else /* Reset semantics */ return print_binary_operator_errors ("R", symbol); msousa@417: } msousa@417: msousa@482: void *print_datatypes_error_c::visit( S1_operator_c *symbol) {return handle_implicit_il_fb_invocation( "S1", symbol, symbol->called_fb_declaration);} msousa@482: void *print_datatypes_error_c::visit( R1_operator_c *symbol) {return handle_implicit_il_fb_invocation( "R1", symbol, symbol->called_fb_declaration);} msousa@482: void *print_datatypes_error_c::visit(CLK_operator_c *symbol) {return handle_implicit_il_fb_invocation("CLK", symbol, symbol->called_fb_declaration);} msousa@482: void *print_datatypes_error_c::visit( CU_operator_c *symbol) {return handle_implicit_il_fb_invocation( "CU", symbol, symbol->called_fb_declaration);} msousa@482: void *print_datatypes_error_c::visit( CD_operator_c *symbol) {return handle_implicit_il_fb_invocation( "CD", symbol, symbol->called_fb_declaration);} msousa@482: void *print_datatypes_error_c::visit( PV_operator_c *symbol) {return handle_implicit_il_fb_invocation( "PV", symbol, symbol->called_fb_declaration);} msousa@482: void *print_datatypes_error_c::visit( IN_operator_c *symbol) {return handle_implicit_il_fb_invocation( "IN", symbol, symbol->called_fb_declaration);} msousa@482: void *print_datatypes_error_c::visit( PT_operator_c *symbol) {return handle_implicit_il_fb_invocation( "PT", symbol, symbol->called_fb_declaration);} msousa@482: msousa@483: void *print_datatypes_error_c::visit( AND_operator_c *symbol) {return print_binary_operator_errors("AND" , symbol);} msousa@483: void *print_datatypes_error_c::visit( OR_operator_c *symbol) {return print_binary_operator_errors( "OR" , symbol);} msousa@483: void *print_datatypes_error_c::visit( XOR_operator_c *symbol) {return print_binary_operator_errors("XOR" , symbol);} msousa@483: void *print_datatypes_error_c::visit(ANDN_operator_c *symbol) {return print_binary_operator_errors("ANDN", symbol);} msousa@483: void *print_datatypes_error_c::visit( ORN_operator_c *symbol) {return print_binary_operator_errors( "ORN", symbol);} msousa@483: void *print_datatypes_error_c::visit(XORN_operator_c *symbol) {return print_binary_operator_errors("XORN", symbol);} msousa@483: void *print_datatypes_error_c::visit( ADD_operator_c *symbol) {return print_binary_operator_errors("ADD" , symbol, symbol->deprecated_operation);} msousa@483: void *print_datatypes_error_c::visit( SUB_operator_c *symbol) {return print_binary_operator_errors("SUB" , symbol, symbol->deprecated_operation);} msousa@483: void *print_datatypes_error_c::visit( MUL_operator_c *symbol) {return print_binary_operator_errors("MUL" , symbol, symbol->deprecated_operation);} msousa@483: void *print_datatypes_error_c::visit( DIV_operator_c *symbol) {return print_binary_operator_errors("DIV" , symbol, symbol->deprecated_operation);} msousa@483: void *print_datatypes_error_c::visit( MOD_operator_c *symbol) {return print_binary_operator_errors("MOD" , symbol);} msousa@417: msousa@484: void *print_datatypes_error_c::visit( GT_operator_c *symbol) {return print_binary_operator_errors( "GT" , symbol);} msousa@484: void *print_datatypes_error_c::visit( GE_operator_c *symbol) {return print_binary_operator_errors( "GE" , symbol);} msousa@484: void *print_datatypes_error_c::visit( EQ_operator_c *symbol) {return print_binary_operator_errors( "EQ" , symbol);} msousa@484: void *print_datatypes_error_c::visit( LT_operator_c *symbol) {return print_binary_operator_errors( "LT" , symbol);} msousa@484: void *print_datatypes_error_c::visit( LE_operator_c *symbol) {return print_binary_operator_errors( "LE" , symbol);} msousa@484: void *print_datatypes_error_c::visit( NE_operator_c *symbol) {return print_binary_operator_errors( "NE" , symbol);} msousa@484: msousa@484: msousa@417: msousa@455: msousa@455: void *print_datatypes_error_c::handle_conditional_flow_control_IL_instruction(symbol_c *symbol, const char *oper) { mjsousa@841: if (!get_datatype_info_c::is_type_valid(symbol->datatype)) msousa@455: STAGE3_ERROR(0, symbol, symbol, "%s operator must be preceded by an IL instruction producing a BOOL value.", oper); msousa@455: return NULL; msousa@455: } msousa@455: msousa@487: void *print_datatypes_error_c::visit( CAL_operator_c *symbol) {return NULL;} msousa@487: void *print_datatypes_error_c::visit( CALC_operator_c *symbol) {return handle_conditional_flow_control_IL_instruction(symbol, "CALC" );} msousa@487: void *print_datatypes_error_c::visit(CALCN_operator_c *symbol) {return handle_conditional_flow_control_IL_instruction(symbol, "CALCN");} msousa@487: void *print_datatypes_error_c::visit( RET_operator_c *symbol) {return NULL;} msousa@487: void *print_datatypes_error_c::visit( RETC_operator_c *symbol) {return handle_conditional_flow_control_IL_instruction(symbol, "RETC" );} msousa@487: void *print_datatypes_error_c::visit(RETCN_operator_c *symbol) {return handle_conditional_flow_control_IL_instruction(symbol, "RETCN");} msousa@487: void *print_datatypes_error_c::visit( JMP_operator_c *symbol) {return NULL;} msousa@487: void *print_datatypes_error_c::visit( JMPC_operator_c *symbol) {return handle_conditional_flow_control_IL_instruction(symbol, "JMPC" );} msousa@487: void *print_datatypes_error_c::visit(JMPCN_operator_c *symbol) {return handle_conditional_flow_control_IL_instruction(symbol, "JMPCN");} msousa@487: msousa@487: msousa@417: 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@936: /* SYM_REF1(deref_operator_c, exp) --> an extension to the IEC 61131-3 standard - based on the IEC 61131-3 v3 standard. Returns address of the varible! */ mjsousa@936: void *print_datatypes_error_c::visit(deref_operator_c *symbol) { mjsousa@936: symbol->exp->accept(*this); mjsousa@936: /* we should really check whether the expression is merely a variable. For now, leave it for the future! */ mjsousa@936: if ((symbol->candidate_datatypes.size() == 0) && (symbol->exp->candidate_datatypes.size() > 0)) mjsousa@936: STAGE3_ERROR(0, symbol, symbol, "^ operator must be preceded by a value of type REF_TO."); mjsousa@936: return NULL; mjsousa@936: } mjsousa@936: mjsousa@933: /* SYM_REF1(deref_expression_c, exp) --> an extension to the IEC 61131-3 standard - based on the IEC 61131-3 v3 standard. Returns address of the varible! */ mjsousa@933: void *print_datatypes_error_c::visit(deref_expression_c *symbol) { mjsousa@933: symbol->exp->accept(*this); mjsousa@933: /* we should really check whether the expression is merely a variable. For now, leave it for the future! */ mjsousa@933: if ((symbol->candidate_datatypes.size() == 0) && (symbol->exp->candidate_datatypes.size() > 0)) mjsousa@933: STAGE3_ERROR(0, symbol, symbol, "DREF operator must be used with a value of type REF_TO."); mjsousa@933: return NULL; mjsousa@933: } mjsousa@933: 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 *print_datatypes_error_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: if ((symbol->candidate_datatypes.size() == 0) && (symbol->exp->candidate_datatypes.size() > 0)) mjsousa@873: STAGE3_ERROR(0, symbol, symbol, "REF operator must be used with a variable."); mjsousa@873: return NULL; mjsousa@873: } mjsousa@873: msousa@417: msousa@485: void *print_datatypes_error_c::print_binary_expression_errors(const char *operation, symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool deprecated_operation) { msousa@485: l_expr->accept(*this); msousa@485: r_expr->accept(*this); msousa@417: if ((symbol->candidate_datatypes.size() == 0) && msousa@485: (l_expr->candidate_datatypes.size() > 0) && msousa@485: (r_expr->candidate_datatypes.size() > 0)) msousa@485: STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '%s' expression.", operation); msousa@485: if (deprecated_operation) msousa@485: STAGE3_WARNING(symbol, symbol, "Deprecated operation for '%s' expression.", operation); msousa@485: return NULL; msousa@485: } msousa@485: msousa@485: msousa@485: void *print_datatypes_error_c::visit( or_expression_c *symbol) {return print_binary_expression_errors( "OR", symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( xor_expression_c *symbol) {return print_binary_expression_errors("XOR", symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( and_expression_c *symbol) {return print_binary_expression_errors("AND", symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( equ_expression_c *symbol) {return print_binary_expression_errors( "=" , symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit(notequ_expression_c *symbol) {return print_binary_expression_errors( "<>", symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( lt_expression_c *symbol) {return print_binary_expression_errors( "<" , symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( gt_expression_c *symbol) {return print_binary_expression_errors( ">" , symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( le_expression_c *symbol) {return print_binary_expression_errors( "<=", symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( ge_expression_c *symbol) {return print_binary_expression_errors( ">=", symbol, symbol->l_exp, symbol->r_exp);} msousa@485: void *print_datatypes_error_c::visit( add_expression_c *symbol) {return print_binary_expression_errors( "+" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);} msousa@485: void *print_datatypes_error_c::visit( sub_expression_c *symbol) {return print_binary_expression_errors( "-" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);} msousa@485: void *print_datatypes_error_c::visit( mul_expression_c *symbol) {return print_binary_expression_errors( "*" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);} msousa@485: void *print_datatypes_error_c::visit( div_expression_c *symbol) {return print_binary_expression_errors( "/" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);} msousa@485: void *print_datatypes_error_c::visit( mod_expression_c *symbol) {return print_binary_expression_errors("MOD", symbol, symbol->l_exp, symbol->r_exp);} msousa@490: void *print_datatypes_error_c::visit( power_expression_c *symbol) {return print_binary_expression_errors( "**", symbol, symbol->l_exp, symbol->r_exp);} msousa@417: msousa@417: msousa@417: void *print_datatypes_error_c::visit(neg_expression_c *symbol) { msousa@417: symbol->exp->accept(*this); msousa@417: if ((symbol->candidate_datatypes.size() == 0) && msousa@417: (symbol->exp->candidate_datatypes.size() > 0)) msousa@427: STAGE3_ERROR(0, symbol, symbol, "Invalid data type for 'NEG' expression."); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *print_datatypes_error_c::visit(not_expression_c *symbol) { msousa@417: symbol->exp->accept(*this); msousa@417: if ((symbol->candidate_datatypes.size() == 0) && msousa@417: (symbol->exp->candidate_datatypes.size() > 0)) msousa@427: STAGE3_ERROR(0, symbol, symbol, "Invalid data type for 'NOT' expression."); msousa@417: return NULL; msousa@417: } msousa@417: msousa@441: /* NOTE: The parameter 'called_function_declaration', 'extensible_param_count' and 'candidate_functions' are used to pass data between the stage 3 and stage 4. */ msousa@441: /* formal_param_list -> may be NULL ! */ msousa@441: /* nonformal_param_list -> may be NULL ! */ msousa@441: // SYM_REF3(function_invocation_c, function_name, formal_param_list, nonformal_param_list, symbol_c *called_function_declaration; int extensible_param_count; std::vector candidate_functions;) msousa@417: void *print_datatypes_error_c::visit(function_invocation_c *symbol) { msousa@441: generic_function_call_t fcall_param = { msousa@441: /* fcall_param.function_name = */ symbol->function_name, msousa@441: /* fcall_param.nonformal_operand_list = */ symbol->nonformal_param_list, msousa@441: /* fcall_param.formal_operand_list = */ symbol->formal_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@441: }; msousa@441: msousa@441: handle_function_invocation(symbol, fcall_param); msousa@441: return NULL; msousa@441: } msousa@441: msousa@441: msousa@417: msousa@417: /********************/ msousa@417: /* B 3.2 Statements */ msousa@417: /********************/ msousa@417: msousa@417: /*********************************/ msousa@417: /* B 3.2.1 Assignment Statements */ msousa@417: /*********************************/ msousa@417: void *print_datatypes_error_c::visit(assignment_statement_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); mjsousa@848: if ((!get_datatype_info_c::is_type_valid(symbol->l_exp->datatype)) && mjsousa@848: (!get_datatype_info_c::is_type_valid(symbol->r_exp->datatype)) && mjsousa@848: (symbol->l_exp->candidate_datatypes.size() > 0) && mjsousa@848: (symbol->r_exp->candidate_datatypes.size() > 0)) msousa@491: STAGE3_ERROR(0, symbol, symbol, "Incompatible data types for ':=' operation."); msousa@417: return NULL; msousa@417: } msousa@417: msousa@425: msousa@425: /*****************************************/ msousa@425: /* B 3.2.2 Subprogram Control Statements */ msousa@425: /*****************************************/ msousa@425: /* fb_name '(' [param_assignment_list] ')' */ msousa@425: /* formal_param_list -> may be NULL ! */ msousa@425: /* nonformal_param_list -> may be NULL ! */ msousa@439: /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ msousa@425: // SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list, symbol_c *called_fb_declaration;) msousa@425: void *print_datatypes_error_c::visit(fb_invocation_c *symbol) { msousa@441: int extensible_param_count; /* unused vairable! Needed for compilation only! */ msousa@441: std::vector candidate_functions; /* unused vairable! Needed for compilation only! */ msousa@441: generic_function_call_t fcall_param = { msousa@441: /* fcall_param.function_name = */ symbol->fb_name, msousa@441: /* fcall_param.nonformal_operand_list = */ symbol->nonformal_param_list, msousa@441: /* fcall_param.formal_operand_list = */ symbol->formal_param_list, msousa@441: /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_FB, msousa@441: /* fcall_param.candidate_functions = */ candidate_functions, /* will not be used, but must provide a reference to be able to compile */ msousa@441: /* fcall_param.called_function_declaration = */ symbol->called_fb_declaration, msousa@441: /* fcall_param.extensible_param_count = */ extensible_param_count /* will not be used, but must provide a reference to be able to compile */ msousa@441: }; msousa@441: msousa@441: handle_function_invocation(symbol, fcall_param); msousa@425: return NULL; msousa@425: } msousa@425: msousa@425: msousa@417: /********************************/ msousa@417: /* B 3.2.3 Selection Statements */ msousa@417: /********************************/ msousa@417: msousa@417: void *print_datatypes_error_c::visit(if_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); mjsousa@848: if ((!get_datatype_info_c::is_type_valid(symbol->expression->datatype)) && mjsousa@848: (symbol->expression->candidate_datatypes.size() > 0)) { mjsousa@995: STAGE3_ERROR(0, symbol->expression, symbol->expression, "Invalid data type for 'IF' condition (should be BOOL)."); msousa@417: } 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: void *print_datatypes_error_c::visit(elseif_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); mjsousa@848: if ((!get_datatype_info_c::is_type_valid(symbol->expression->datatype)) && mjsousa@848: (symbol->expression->candidate_datatypes.size() > 0)) { mjsousa@995: STAGE3_ERROR(0, symbol->expression, symbol->expression, "Invalid data type for 'ELSIF' condition (should be BOOL)."); msousa@417: } 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 *print_datatypes_error_c::visit(case_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); mjsousa@848: if ((!get_datatype_info_c::is_type_valid(symbol->expression->datatype)) && mjsousa@848: (symbol->expression->candidate_datatypes.size() > 0)) { mjsousa@995: STAGE3_ERROR(0, symbol->expression, symbol->expression, "'CASE' quantity not an integer or enumerated."); msousa@417: } 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: /* B 3.2.4 Iteration Statements */ msousa@417: /********************************/ msousa@417: msousa@417: void *print_datatypes_error_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: /* Control variable */ mjsousa@848: if ((!get_datatype_info_c::is_type_valid(symbol->control_variable->datatype)) && mjsousa@848: (symbol->control_variable->candidate_datatypes.size() > 0)) { mjsousa@995: STAGE3_ERROR(0, symbol->control_variable, symbol->control_variable, "Invalid data type for 'FOR' control variable."); msousa@417: } msousa@417: /* BEG expression */ mjsousa@848: if ((!get_datatype_info_c::is_type_valid(symbol->beg_expression->datatype)) && mjsousa@848: (symbol->beg_expression->candidate_datatypes.size() > 0)) { mjsousa@995: STAGE3_ERROR(0, symbol->beg_expression, symbol->beg_expression, "Invalid data type for 'FOR' begin expression."); msousa@417: } msousa@417: /* END expression */ mjsousa@848: if ((!get_datatype_info_c::is_type_valid(symbol->end_expression->datatype)) && mjsousa@848: (symbol->end_expression->candidate_datatypes.size() > 0)) { mjsousa@995: STAGE3_ERROR(0, symbol->end_expression, symbol->end_expression, "Invalid data type for 'FOR' end expression."); msousa@417: } msousa@417: /* BY expression */ msousa@417: if ((NULL != symbol->by_expression) && mjsousa@848: (!get_datatype_info_c::is_type_valid(symbol->by_expression->datatype)) && mjsousa@848: (symbol->end_expression->candidate_datatypes.size() > 0)) { mjsousa@995: STAGE3_ERROR(0, symbol->by_expression, symbol->by_expression, "Invalid data type for 'FOR' by expression."); msousa@417: } msousa@417: /* DO statement */ msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(while_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); msousa@676: if (!get_datatype_info_c::is_type_valid(symbol->expression->datatype)) { mjsousa@995: STAGE3_ERROR(0, symbol->expression, symbol->expression, "Invalid data type for 'WHILE' condition."); msousa@417: return NULL; msousa@417: } msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *print_datatypes_error_c::visit(repeat_statement_c *symbol) { msousa@676: if (!get_datatype_info_c::is_type_valid(symbol->expression->datatype)) { mjsousa@995: STAGE3_ERROR(0, symbol->expression, symbol->expression, "Invalid data type for 'REPEAT' condition."); msousa@417: return NULL; msousa@417: } msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: symbol->expression->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: msousa@417: msousa@417: