conti@508: /*
conti@508: * matiec - a compiler for the programming languages defined in IEC 61131-3
conti@508: *
conti@508: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt)
conti@508: * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it)
conti@508: *
conti@508: *
conti@508: * This program is free software: you can redistribute it and/or modify
conti@508: * it under the terms of the GNU General Public License as published by
conti@508: * the Free Software Foundation, either version 3 of the License, or
conti@508: * (at your option) any later version.
conti@508: *
conti@508: * This program is distributed in the hope that it will be useful,
conti@508: * but WITHOUT ANY WARRANTY; without even the implied warranty of
conti@508: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
conti@508: * GNU General Public License for more details.
conti@508: *
conti@508: * You should have received a copy of the GNU General Public License
conti@508: * along with this program. If not, see .
conti@508: *
conti@508: *
conti@508: * This code is made available on the understanding that it will not be
conti@508: * used in safety-critical situations without a full and competent review.
conti@508: */
conti@508:
conti@508: /*
conti@508: * An IEC 61131-3 compiler.
conti@508: *
conti@508: * Based on the
conti@508: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
conti@508: *
conti@508: */
conti@508:
conti@508:
msousa@553:
msousa@553: /* Expressions on the left hand side of assignment statements have aditional restrictions on their datatype.
msousa@553: * For example, they cannot be literals, CONSTANT type variables, function invocations, etc...
msousa@553: * This class wil do those checks.
msousa@553: *
msousa@553: * Note that assignment may also be done when passing variables to OUTPUT or IN_OUT function parameters,so we check those too.
msousa@553: */
msousa@553:
msousa@553:
msousa@553:
conti@508: #include "lvalue_check.hh"
conti@508:
conti@508: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2))
conti@508: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2))
conti@508:
conti@508: #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \
conti@508: if (current_display_error_level >= error_level) { \
conti@508: fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \
conti@508: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
conti@508: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\
conti@508: fprintf(stderr, __VA_ARGS__); \
conti@508: fprintf(stderr, "\n"); \
msousa@510: error_count++; \
conti@508: } \
conti@508: }
conti@508:
conti@508:
conti@508: #define STAGE3_WARNING(symbol1, symbol2, ...) { \
conti@508: fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \
conti@508: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
conti@508: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\
conti@508: fprintf(stderr, __VA_ARGS__); \
conti@508: fprintf(stderr, "\n"); \
conti@508: warning_found = true; \
conti@508: }
conti@508:
conti@508:
conti@508: lvalue_check_c::lvalue_check_c(symbol_c *ignore) {
msousa@510: error_count = 0;
msousa@555: current_display_error_level = 0;
msousa@528: current_il_operand = NULL;
conti@661: search_varfb_instance_type = NULL;
conti@661: search_var_instance_decl = NULL;
conti@508: }
conti@508:
conti@508: lvalue_check_c::~lvalue_check_c(void) {
conti@508: }
conti@508:
msousa@510: int lvalue_check_c::get_error_count() {
msousa@510: return error_count;
conti@508: }
conti@508:
msousa@509:
msousa@509: #include
conti@508: /* No writing to iterator variables (used in FOR loops) inside the loop itself */
msousa@509: void lvalue_check_c::check_assignment_to_controlvar(symbol_c *lvalue) {
conti@508: for (unsigned int i = 0; i < control_variables.size(); i++) {
msousa@512: token_c *lvalue_name = get_var_name_c::get_name(lvalue);
msousa@512: if (compare_identifiers(lvalue_name, control_variables[i]) == 0) {
msousa@512: STAGE3_ERROR(0, lvalue, lvalue, "Assignment to FOR control variable is not allowed.");
conti@508: break;
conti@508: }
conti@508: }
conti@508: }
conti@508:
msousa@509:
msousa@510: /* fb_instance.var := ... is not valid if var is output variable */
msousa@510: /* NOTE, if a fb_instance1.fb_instance2.fb_instance3.var is used, we must iteratively check that none of the
msousa@510: * FB records are declared as OUTPUT variables!!
msousa@510: * This is the reason why we have the while() loop in this function!
msousa@615: *
msousa@615: * Note, however, that the first record (fb_instance1 in the above example) may be an output variable!
msousa@510: */
msousa@510: void lvalue_check_c::check_assignment_to_output(symbol_c *lvalue) {
msousa@510: decompose_var_instance_name_c decompose_lvalue(lvalue);
msousa@510:
msousa@519: /* Get the first element/record of the potentially structured variable symbol */
msousa@519: /* Note that if symbol is pointing to an expression (or simply a literal value), it will return a NULL.
msousa@519: * Once we have implemented the check_assignment_to_expression() method, and abort calling the other checks (including this one)
msousa@519: * when an expression is found, we may replace this check with an assertion...
msousa@519: * if (NULL == struct_elem) ERROR;
msousa@519: */
mjsousa@826: symbol_c *struct_elem = decompose_lvalue.get_next();
conti@518: if (NULL == struct_elem) return;
msousa@519:
msousa@510: symbol_c *type_decl = search_var_instance_decl->get_decl(struct_elem);
msousa@510: // symbol_c *type_id = spec_init_sperator_c::get_spec(type_decl); /* this is not required! search_base_type_c can handle spec_init symbols! */
msousa@718: symbol_c *basetype_id = search_base_type_c::get_basetype_id(/*type_id*/ type_decl);
msousa@510: /* If we can not determine the data type of the element, then the code must have a data type semantic error.
msousa@510: * This will have been caught by the data type semantic verifier, so we do not bother with this anymore!
msousa@510: */
msousa@510: if (NULL == basetype_id) return;
msousa@510:
msousa@510: /* Determine if the record/structure element is of a FB type. */
msousa@510: /* NOTE: If the structure element is not a FB type, then we can quit this check.
msousa@510: * Remember that the standard does not allow a STRUCT data type to have elements that are FB instances!
msousa@510: * Similarly, arrays of FB instances is also not allowed.
msousa@510: * So, as soon as we find one record/structure element that is not a FB, no other record/structure element
msousa@510: * will be of FB type, which means we can quit this check!
msousa@510: */
mjsousa@971: function_block_type_symtable_t::iterator iter = function_block_type_symtable.find(basetype_id);
mjsousa@971: if (function_block_type_symtable.end() == iter) return;
mjsousa@971: function_block_declaration_c *fb_decl = iter->second;
msousa@510:
mjsousa@826: while (NULL != (struct_elem = decompose_lvalue.get_next())) {
msousa@510: search_var_instance_decl_c fb_search_var_instance_decl(fb_decl);
msousa@510: if (search_var_instance_decl_c::output_vt == fb_search_var_instance_decl.get_vartype(struct_elem)) {
msousa@510: STAGE3_ERROR(0, struct_elem, struct_elem, "Assignment to FB output variable is not allowed.");
msousa@510: return; /* no need to carry on checking once the first error is found! */
conti@508: }
msousa@510:
msousa@510: /* prepare for any possible further record/structure elements */
msousa@510: type_decl = fb_search_var_instance_decl.get_decl(struct_elem);
msousa@718: basetype_id = search_base_type_c::get_basetype_id(type_decl);
msousa@510: if (NULL == basetype_id) return; /* same comment as above... */
mjsousa@971:
mjsousa@971: iter = function_block_type_symtable.find(basetype_id);
mjsousa@971: if (function_block_type_symtable.end() == iter) return; /* same comment as above... */
mjsousa@971: fb_decl = iter->second;
conti@508: }
conti@508: }
conti@508:
msousa@509:
conti@508: /* No writing to CONSTANTs */
msousa@509: void lvalue_check_c::check_assignment_to_constant(symbol_c *lvalue) {
msousa@509: unsigned int option = search_var_instance_decl->get_option(lvalue);
conti@508: if (option == search_var_instance_decl_c::constant_opt) {
mjs@846: STAGE3_ERROR(0, lvalue, lvalue, "Assignment to CONSTANT variables is not allowed.");
msousa@509: }
msousa@509: }
msousa@509:
msousa@509:
msousa@509: /* No assigning values to expressions. */
msousa@509: void lvalue_check_c::check_assignment_to_expression(symbol_c *lvalue) {
conti@524: /* This may occur in function invocations, when passing values (possibly an expression) to one
conti@524: * of the function's OUTPUT or IN_OUT parameters.
conti@524: */
msousa@525: if (
msousa@525: /*********************/
msousa@525: /* B 1.2 - Constants */
msousa@525: /*********************/
mjsousa@926: /*********************************/
mjsousa@926: /* B 1.2.XX - Reference Literals */
mjsousa@926: /*********************************/
mjsousa@926: (typeid( *lvalue ) == typeid( ref_value_null_literal_c )) || /* defined in IEC 61131-3 v3 - Basically the 'NULL' keyword! */
msousa@525: /******************************/
msousa@525: /* B 1.2.1 - Numeric Literals */
msousa@525: /******************************/
msousa@525: (typeid( *lvalue ) == typeid( real_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( integer_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( binary_integer_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( octal_integer_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( hex_integer_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( neg_real_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( neg_integer_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( integer_literal_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( real_literal_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( bit_string_literal_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( boolean_literal_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( boolean_true_c )) || /* should not really be needed */
msousa@525: (typeid( *lvalue ) == typeid( boolean_false_c )) || /* should not really be needed */
msousa@525: /*******************************/
msousa@525: /* B.1.2.2 Character Strings */
msousa@525: /*******************************/
msousa@525: (typeid( *lvalue ) == typeid( double_byte_character_string_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( single_byte_character_string_c )) ||
msousa@525: /***************************/
msousa@525: /* B 1.2.3 - Time Literals */
msousa@525: /***************************/
msousa@525: /************************/
msousa@525: /* B 1.2.3.1 - Duration */
msousa@525: /************************/
msousa@525: (typeid( *lvalue ) == typeid( duration_c )) ||
msousa@525: /************************************/
msousa@525: /* B 1.2.3.2 - Time of day and Date */
msousa@525: /************************************/
msousa@525: (typeid( *lvalue ) == typeid( time_of_day_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( daytime_c )) || /* should not really be needed */
msousa@525: (typeid( *lvalue ) == typeid( date_c )) || /* should not really be needed */
msousa@525: (typeid( *lvalue ) == typeid( date_literal_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( date_and_time_c )) ||
msousa@525: /***************************************/
msousa@525: /* B.3 - Language ST (Structured Text) */
msousa@525: /***************************************/
msousa@525: /***********************/
msousa@525: /* B 3.1 - Expressions */
msousa@525: /***********************/
mjsousa@926: (typeid( *lvalue ) == typeid( ref_expression_c )) || /* an extension to the IEC 61131-3 standard - based on the IEC 61131-3 v3 standard. Returns address of the variable! */
msousa@525: (typeid( *lvalue ) == typeid( or_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( xor_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( and_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( equ_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( notequ_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( lt_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( gt_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( le_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( ge_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( add_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( sub_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( mul_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( div_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( mod_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( power_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( neg_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( not_expression_c )) ||
msousa@525: (typeid( *lvalue ) == typeid( function_invocation_c )))
mjs@846: STAGE3_ERROR(0, lvalue, lvalue, "Assignment to an expression or a literal value is not allowed.");
msousa@525: }
msousa@525:
msousa@525:
msousa@525:
mjsousa@845: /* No assigning values to IL lists. */
mjsousa@845: void lvalue_check_c::check_assignment_to_il_list(symbol_c *lvalue) {
mjsousa@845: /* This may occur in formal invocations in IL, where an embedded IL list may be used instead of a symbolic_variable
mjsousa@845: * when passing an IN_OUT parameter! Note that it will never occur to OUT parameters, as the syntax does not allow it,
mjsousa@845: * although this does not affect out algorithm here!
mjsousa@845: */
mjsousa@845: if (
mjsousa@845: /****************************************/
mjsousa@845: /* B.2 - Language IL (Instruction List) */
mjsousa@845: /****************************************/
mjsousa@845: /***********************************/
mjsousa@845: /* B 2.1 Instructions and Operands */
mjsousa@845: /***********************************/
mjsousa@845: (typeid( *lvalue ) == typeid( simple_instr_list_c)))
mjs@846: STAGE3_ERROR(0, lvalue, lvalue, "Assigning an IL list to an IN_OUT parameter is not allowed.");
mjsousa@845: }
msousa@525:
msousa@509:
msousa@509:
msousa@509:
msousa@509: void lvalue_check_c::verify_is_lvalue(symbol_c *lvalue) {
mjsousa@833: if (NULL == lvalue) return; // missing operand in source code being compiled. Error will be caught and reported by datatype checking!
msousa@529: int init_error_count = error_count; /* stop the checks once an error has been found... */
msousa@529: if (error_count == init_error_count) check_assignment_to_expression(lvalue);
mjsousa@845: if (error_count == init_error_count) check_assignment_to_il_list (lvalue);
msousa@529: if (error_count == init_error_count) check_assignment_to_controlvar(lvalue);
mjsousa@845: if (error_count == init_error_count) check_assignment_to_output (lvalue);
mjsousa@845: if (error_count == init_error_count) check_assignment_to_constant (lvalue);
msousa@509: }
msousa@509:
msousa@509:
msousa@509:
msousa@509:
msousa@509: /* check whether all values passed to OUT or IN_OUT parameters are legal lvalues. */
msousa@513: /*
msousa@513: * All parameters being passed to the called function MUST be in the parameter list to which f_call points to!
msousa@513: * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the
msousa@513: * beginning of the parameter list BEFORE calling handle_function_call().
msousa@513: */
msousa@513: #include /* required for strcmp() */
msousa@509: void lvalue_check_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl) {
mjsousa@828: /* if data type semantic verification was unable to determine which function is being called,
mjsousa@828: * then it does not make sense to go ahead and check for lvalues to unknown parameters.
mjsousa@828: * We simply bug out!
mjsousa@828: */
mjsousa@828: if (NULL == f_decl) return;
mjsousa@828:
msousa@513: symbol_c *call_param_value;
msousa@513: identifier_c *param_name;
msousa@513: function_param_iterator_c fp_iterator(f_decl);
msousa@513: function_call_param_iterator_c fcp_iterator(f_call);
msousa@513:
msousa@513: /* Iterating through the non-formal parameters of the function call */
msousa@513: while((call_param_value = fcp_iterator.next_nf()) != NULL) {
msousa@513: /* Iterate to the next parameter of the function being called.
msousa@513: * Get the name of that parameter, and ignore if EN or ENO.
msousa@513: */
msousa@513: do {
msousa@513: param_name = fp_iterator.next();
msousa@513: /* If there is no other parameter declared, then we are passing too many parameters... */
msousa@513: /* This error should have been caught in data type verification, so we simply abandon our check! */
msousa@513: if(param_name == NULL) return;
msousa@513: } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
msousa@513:
msousa@535: /* Determine the direction (IN, OUT, IN_OUT) of the parameter... */
msousa@513: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
msousa@513:
msousa@615: /* We only process the parameter value if the paramater itself is valid... */
msousa@615: if (param_name != NULL) {
msousa@615: /* If the parameter is either OUT or IN_OUT, we check if 'call_param_value' is a valid lvalue */
msousa@615: if ((function_param_iterator_c::direction_out == param_direction) || (function_param_iterator_c::direction_inout == param_direction))
msousa@615: verify_is_lvalue(call_param_value);
mjsousa@831: /* parameter values to IN parameters may be expressions with function invocations that must also be checked! */
mjsousa@831: /* parameter values to OUT or IN_OUT parameters may contain arrays, whose subscripts contain expressions that must be checked! */
mjsousa@831: call_param_value->accept(*this);
msousa@513: }
msousa@513: }
msousa@513: }
msousa@513:
msousa@513:
msousa@513:
msousa@513:
msousa@509:
msousa@509:
msousa@509: /* check whether all values passed to OUT or IN_OUT parameters are legal lvalues. */
msousa@509: void lvalue_check_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl) {
msousa@509: /* if data type semantic verification was unable to determine which function is being called,
msousa@509: * then it does not make sense to go ahead and check for lvalues to unknown parameters.
msousa@509: * We simply bug out!
msousa@509: */
msousa@509: if (NULL == f_decl) return;
msousa@509:
msousa@509: symbol_c *call_param_name;
msousa@509: function_param_iterator_c fp_iterator(f_decl);
conti@508: function_call_param_iterator_c fcp_iterator(f_call);
msousa@509:
msousa@509: /* Iterating through the formal parameters of the function call */
msousa@509: while((call_param_name = fcp_iterator.next_f()) != NULL) {
msousa@509:
msousa@509: /* Obtaining the value being passed in the function call */
msousa@509: symbol_c *call_param_value = fcp_iterator.get_current_value();
msousa@509: if (NULL == call_param_value) ERROR;
msousa@509:
msousa@509: /* Find the corresponding parameter in function declaration, and it's direction (IN, OUT, IN_OUT) */
msousa@509: identifier_c *param_name = fp_iterator.search(call_param_name);
msousa@509: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
msousa@509:
msousa@615: /* We only process the parameter value if the paramater itself is valid... */
msousa@615: if (param_name != NULL) {
msousa@615: /* If the parameter is either OUT or IN_OUT, we check if 'call_param_value' is a valid lvalue */
msousa@615: if ((function_param_iterator_c::direction_out == param_direction) || (function_param_iterator_c::direction_inout == param_direction))
msousa@615: verify_is_lvalue(call_param_value);
mjsousa@831: /* parameter values to IN parameters may be expressions with function invocations that must also be checked! */
mjsousa@831: /* parameter values to OUT or IN_OUT parameters may contain arrays, whose subscripts contain expressions that must be checked! */
mjsousa@831: call_param_value->accept(*this);
msousa@615: }
msousa@509: }
msousa@509: }
msousa@509:
msousa@509:
msousa@509:
msousa@509:
conti@508:
conti@508:
conti@508:
conti@508: /**************************************/
conti@508: /* B 1.5 - Program organisation units */
conti@508: /**************************************/
conti@508: /***********************/
conti@508: /* B 1.5.1 - Functions */
conti@508: /***********************/
conti@508: void *lvalue_check_c::visit(function_declaration_c *symbol) {
conti@508: search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
conti@508: search_var_instance_decl = new search_var_instance_decl_c(symbol);
conti@508: symbol->function_body->accept(*this);
conti@508: delete search_varfb_instance_type;
conti@508: delete search_var_instance_decl;
conti@508: search_varfb_instance_type = NULL;
conti@508: search_var_instance_decl = NULL;
conti@508: return NULL;
conti@508: }
conti@508:
conti@508: /*****************************/
conti@508: /* B 1.5.2 - Function blocks */
conti@508: /*****************************/
conti@508: void *lvalue_check_c::visit(function_block_declaration_c *symbol) {
conti@508: search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
conti@508: search_var_instance_decl = new search_var_instance_decl_c(symbol);
conti@508: symbol->fblock_body->accept(*this);
conti@508: delete search_varfb_instance_type;
conti@508: delete search_var_instance_decl;
conti@508: search_varfb_instance_type = NULL;
conti@508: search_var_instance_decl = NULL;
conti@508: return NULL;
conti@508: }
conti@508:
conti@508: /**********************/
conti@508: /* B 1.5.3 - Programs */
conti@508: /**********************/
conti@508: void *lvalue_check_c::visit(program_declaration_c *symbol) {
conti@508: search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
conti@508: search_var_instance_decl = new search_var_instance_decl_c(symbol);
conti@508: symbol->function_block_body->accept(*this);
conti@508: delete search_varfb_instance_type;
conti@508: delete search_var_instance_decl;
conti@508: search_varfb_instance_type = NULL;
conti@508: search_var_instance_decl = NULL;
conti@508: return NULL;
conti@508: }
conti@508:
conti@527: /****************************************/
conti@527: /* B.2 - Language IL (Instruction List) */
conti@527: /****************************************/
conti@527: /***********************************/
conti@527: /* B 2.1 Instructions and Operands */
conti@527: /***********************************/
conti@527: void *lvalue_check_c::visit(il_instruction_c *symbol) {
msousa@532: /* il_instruction will be NULL when parsing a label with no instruction
msousa@532: * e.g.: label1: <---- il_instruction = NULL!
msousa@532: * LD 33
msousa@532: * ...
msousa@532: */
msousa@532: if (NULL != symbol->il_instruction)
msousa@532: symbol->il_instruction->accept(*this);
conti@527: return NULL;
conti@527: }
conti@527:
conti@527: void *lvalue_check_c::visit(il_simple_operation_c *symbol) {
conti@527: current_il_operand = symbol->il_operand;
conti@527: symbol->il_simple_operator->accept(*this);
conti@527: current_il_operand = NULL;
conti@527: return NULL;
conti@527: }
conti@527:
conti@527:
msousa@535:
msousa@535:
msousa@535: /* | function_name [il_operand_list] */
msousa@535: /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */
msousa@535: // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;)
msousa@535: void *lvalue_check_c::visit(il_function_call_c *symbol) {
msousa@535: /* The first parameter of a non formal function call in IL will be the 'current value' (i.e. the prev_il_instruction)
msousa@535: * In order to be able to handle this without coding special cases, we will simply prepend that symbol
msousa@535: * to the il_operand_list, and remove it after calling handle_function_call().
msousa@535: *
msousa@535: * However, if no further paramters are given, then il_operand_list will be NULL, and we will
msousa@535: * need to create a new object to hold the pointer to prev_il_instruction.
msousa@535: * This change will also be undone at the end of this method.
msousa@535: */
msousa@535: /* TODO: Copying the location data will result in confusing error message.
msousa@535: * We need to make this better, by inserting code to handle this special situation explicitly!
msousa@535: */
msousa@535: /* NOTE: When calling a function, using the 'current value' as the first parameter of the function invocation
msousa@535: * implies that we can only call functions whose first parameter is IN. It would not do to pass
msousa@535: * the 'current value' to an OUT or IN_OUT parameter.
msousa@535: * In order to make sure that this will be caught by the check_nonformal_call() function,
msousa@535: * we add a symbol that cannot be an lvalue; in this case, a real_c (REAL literal).
msousa@535: */
msousa@535: real_c param_value(NULL);
msousa@535: *((symbol_c *)(¶m_value)) = *((symbol_c *)symbol); /* copy the symbol location (file, line, offset) data */
msousa@535: if (NULL == symbol->il_operand_list) symbol->il_operand_list = new il_operand_list_c;
msousa@535: if (NULL == symbol->il_operand_list) ERROR;
msousa@535: ((list_c *)symbol->il_operand_list)->insert_element(¶m_value, 0);
msousa@535:
msousa@535: check_nonformal_call(symbol, symbol->called_function_declaration);
msousa@535:
msousa@535: /* Undo the changes to the abstract syntax tree we made above... */
msousa@535: ((list_c *)symbol->il_operand_list)->remove_element(0);
msousa@535: if (((list_c *)symbol->il_operand_list)->n == 0) {
msousa@535: /* if the list becomes empty, then that means that it did not exist before we made these changes, so we delete it! */
msousa@535: delete symbol->il_operand_list;
msousa@535: symbol->il_operand_list = NULL;
msousa@535: }
msousa@535:
msousa@535: return NULL;
msousa@535: }
msousa@535:
msousa@535:
msousa@535:
msousa@535:
msousa@535:
msousa@535:
msousa@535: /* il_call_operator prev_declared_fb_name
msousa@535: * | il_call_operator prev_declared_fb_name '(' ')'
msousa@535: * | il_call_operator prev_declared_fb_name '(' eol_list ')'
msousa@535: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
msousa@535: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
msousa@535: */
msousa@535: /* 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@535: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
msousa@535: void *lvalue_check_c::visit(il_fb_call_c *symbol) {
msousa@535: if (NULL != symbol->il_operand_list) check_nonformal_call(symbol, symbol->called_fb_declaration);
msousa@535: if (NULL != symbol-> il_param_list) check_formal_call(symbol, symbol->called_fb_declaration);
msousa@535: return NULL;
msousa@535: }
msousa@535:
msousa@535:
msousa@535: /* | function_name '(' eol_list [il_param_list] ')' */
msousa@535: /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */
msousa@535: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;)
msousa@535: void *lvalue_check_c::visit(il_formal_funct_call_c *symbol) {
msousa@535: check_formal_call(symbol, symbol->called_function_declaration);
msousa@535: return NULL;
msousa@535: }
msousa@535:
msousa@535:
msousa@535:
msousa@535:
msousa@535:
msousa@535:
conti@527: /*******************/
conti@527: /* B 2.2 Operators */
conti@527: /*******************/
conti@527: void *lvalue_check_c::visit(ST_operator_c *symbol) {
conti@527: verify_is_lvalue(current_il_operand);
conti@527: return NULL;
conti@527: }
conti@527:
conti@527: void *lvalue_check_c::visit(STN_operator_c *symbol) {
conti@527: verify_is_lvalue(current_il_operand);
conti@527: return NULL;
conti@527: }
conti@527:
conti@527: void *lvalue_check_c::visit(S_operator_c *symbol) {
conti@527: verify_is_lvalue(current_il_operand);
conti@527: return NULL;
conti@527: }
conti@527:
conti@527: void *lvalue_check_c::visit(R_operator_c *symbol) {
conti@527: verify_is_lvalue(current_il_operand);
conti@527: return NULL;
conti@527: }
conti@527:
conti@527:
conti@508: /***************************************/
conti@508: /* B.3 - Language ST (Structured Text) */
conti@508: /***************************************/
conti@508: /***********************/
conti@508: /* B 3.1 - Expressions */
conti@508: /***********************/
msousa@509: // 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;)
conti@508: void *lvalue_check_c::visit(function_invocation_c *symbol) {
msousa@510: if (NULL != symbol->formal_param_list ) check_formal_call (symbol, symbol->called_function_declaration);
msousa@510: if (NULL != symbol->nonformal_param_list) check_nonformal_call(symbol, symbol->called_function_declaration);
conti@508: return NULL;
conti@508: }
conti@508:
conti@508: /*********************************/
conti@508: /* B 3.2.1 Assignment Statements */
conti@508: /*********************************/
conti@508: void *lvalue_check_c::visit(assignment_statement_c *symbol) {
msousa@509: verify_is_lvalue(symbol->l_exp);
conti@508: /* We call visit r_exp to check function_call */
conti@508: symbol->r_exp->accept(*this);
conti@508: return NULL;
conti@508: }
conti@508:
conti@526: /*****************************************/
conti@526: /* B 3.2.2 Subprogram Control Statements */
conti@526: /*****************************************/
conti@526: void *lvalue_check_c::visit(fb_invocation_c *symbol) {
conti@526: if (NULL != symbol->formal_param_list ) check_formal_call (symbol, symbol->called_fb_declaration);
conti@526: if (NULL != symbol->nonformal_param_list) check_nonformal_call(symbol, symbol->called_fb_declaration);
conti@526: return NULL;
conti@526: }
conti@526:
conti@508: /********************************/
conti@508: /* B 3.2.4 Iteration Statements */
conti@508: /********************************/
conti@508: void *lvalue_check_c::visit(for_statement_c *symbol) {
conti@557: verify_is_lvalue(symbol->control_variable);
msousa@512: control_variables.push_back(get_var_name_c::get_name(symbol->control_variable));
conti@508: symbol->statement_list->accept(*this);
conti@508: control_variables.pop_back();
conti@508: return NULL;
conti@508: }
conti@508:
conti@508:
conti@508:
conti@508:
conti@508:
conti@508:
conti@508: