mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@265: * msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant msousa@265: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * mario@181: * mario@181: * This code is made available on the understanding that it will not be mario@181: * used in safety-critical situations without a full and competent review. mario@181: */ mario@181: mario@181: /* msousa@265: * An IEC 61131-3 compiler. mario@181: * mario@181: * Based on the mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) mario@181: * mario@181: */ mario@181: msousa@504: /* Search in a VAR* END_VAR declaration for the delcration of the specified variable instance. msousa@504: * Will return: msousa@504: * - the declaration itself (get_decl() ) msousa@504: * - the type of declaration in which the variable was declared (get_vartype() ) msousa@504: * msousa@504: * The variable instance may NOT be a member of a structure of a memeber mario@181: * of a structure of an element of an array of ... mario@181: * msousa@504: * For example, considering the following 'variables': mario@181: * window.points[1].coordinate.x mario@181: * window.points[1].colour msousa@504: * offset[99] msousa@504: * msousa@504: * passing a reference to 'points', 'points[1]', 'points[1].colour', 'colour' msousa@504: * ARE NOT ALLOWED! mario@181: * mario@181: * This class must only be passed the name of the variable that will appear mario@181: * in the variable declaration. In the above examples, this would be msousa@504: * 'window.points[1].coordinate.x' msousa@504: * 'window.points[1].coordinate' msousa@504: * 'window.points[1]' msousa@504: * 'window' msousa@504: * 'window.points[1].colour' msousa@504: * 'offset' msousa@504: * 'offset[99]' msousa@504: * msousa@504: * msousa@504: */ msousa@504: msousa@504: /* Note: msousa@504: * Determining the declaration type of a specific variable instance (including msousa@504: * function block instances) really means determining whether the variable was declared in a msousa@504: * VAR_INPUT msousa@504: * VAR_OUTPUT msousa@504: * VAR_IN_OUT msousa@504: * VAR msousa@504: * VAR_TEMP msousa@504: * VAR_EXTERNAL msousa@504: * VAR_GLOBAL msousa@504: * VAR AT -> Located variable! msousa@504: * msousa@504: */ msousa@504: msousa@504: /* Note: msousa@504: * The current_type_decl that this class returns may reference the mario@181: * name of a type, or the type declaration itself! mario@181: * For an example of the first, consider a variable declared as ... mario@181: * x : AAA; mario@181: * where the AAA type is previously declared as whatever. mario@181: * For an example of the second, consider a variable declared as ... mario@181: * x : array of int [10]; ----> is allowed mario@181: * mario@181: * If it is the first, we will return a reference to the name, if the second mario@181: * we return a reference to the declaration!! mario@181: */ msousa@504: msousa@504: mario@181: #include "absyntax_utils.hh" mario@181: mario@181: msousa@504: msousa@504: msousa@504: mario@181: search_var_instance_decl_c::search_var_instance_decl_c(symbol_c *search_scope) { mario@181: this->current_vartype = none_vt; mario@181: this->search_scope = search_scope; mario@181: this->search_name = NULL; mario@181: this->current_type_decl = NULL; manuele@507: this->current_option = none_opt; mario@181: } mario@181: msousa@504: symbol_c *search_var_instance_decl_c::get_decl(symbol_c *variable) { laurent@217: this->current_vartype = none_vt; manuele@507: this->current_option = none_opt; msousa@504: this->search_name = get_var_name_c::get_name(variable); mario@181: return (symbol_c *)search_scope->accept(*this); mario@181: } mario@181: msousa@504: unsigned int search_var_instance_decl_c::get_vartype(symbol_c *variable) { msousa@417: this->current_vartype = none_vt; manuele@507: this->current_option = none_opt; msousa@504: this->search_name = get_var_name_c::get_name(variable); msousa@417: search_scope->accept(*this); manuele@507: return this->current_vartype; manuele@507: } manuele@507: manuele@507: unsigned int search_var_instance_decl_c::get_option(symbol_c *variable) { manuele@507: this->current_vartype = none_vt; manuele@507: this->current_option = none_opt; manuele@507: this->search_name = get_var_name_c::get_name(variable); manuele@507: search_scope->accept(*this); manuele@507: return this->current_option; manuele@507: } msousa@417: mario@181: /***************************/ mario@181: /* B 0 - Programming Model */ mario@181: /***************************/ mario@181: void *search_var_instance_decl_c::visit(library_c *symbol) { mario@181: /* we do not want to search multiple declaration scopes, laurent@217: * so we do not visit all the functions, function blocks, etc... mario@181: */ mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: mario@181: /******************************************/ laurent@217: /* B 1.4.3 - Declaration & Initialization */ mario@181: /******************************************/ laurent@217: mario@181: /* edge -> The F_EDGE or R_EDGE directive */ mario@181: // SYM_REF2(edge_declaration_c, edge, var1_list) mario@181: // TODO manuele@507: void *search_var_instance_decl_c::visit(constant_option_c *symbol) { manuele@507: current_option = constant_opt; manuele@507: return NULL; manuele@507: } manuele@507: manuele@507: void *search_var_instance_decl_c::visit(retain_option_c *symbol) { manuele@507: current_option = retain_opt; manuele@507: return NULL; manuele@507: } manuele@507: manuele@507: void *search_var_instance_decl_c::visit(non_retain_option_c *symbol) { manuele@507: current_option = non_retain_opt; manuele@507: return NULL; manuele@507: } mario@181: mario@181: void *search_var_instance_decl_c::visit(input_declarations_c *symbol) { mario@181: current_vartype = input_vt; manuele@507: current_option = none_opt; /* not really required. Just to make the code more readable */ manuele@507: if (NULL != symbol->option) manuele@507: symbol->option->accept(*this); mario@181: void *res = symbol->input_declaration_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; manuele@507: current_option = none_opt; mario@181: } mario@181: return res; mario@181: } mario@181: mario@181: /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */ mario@181: /* option -> may be NULL ! */ mario@181: void *search_var_instance_decl_c::visit(output_declarations_c *symbol) { mario@181: current_vartype = output_vt; manuele@507: current_option = none_opt; /* not really required. Just to make the code more readable */ manuele@507: if (NULL != symbol->option) manuele@507: symbol->option->accept(*this); mario@181: void *res = symbol->var_init_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; manuele@507: current_option = none_opt; mario@181: } mario@181: return res; mario@181: } mario@181: mario@181: /* VAR_IN_OUT var_declaration_list END_VAR */ mario@181: void *search_var_instance_decl_c::visit(input_output_declarations_c *symbol) { mario@181: current_vartype = inoutput_vt; manuele@507: current_option = none_opt; /* not really required. Just to make the code more readable */ mario@181: void *res = symbol->var_declaration_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; mario@181: } mario@181: return res; mario@181: } mario@181: ccb@202: /* ENO : BOOL */ ccb@202: void *search_var_instance_decl_c::visit(eno_param_declaration_c *symbol) { ccb@202: if (compare_identifiers(symbol->name, search_name) == 0) ccb@202: return symbol->type; ccb@202: return NULL; ccb@202: } ccb@202: ccb@202: mario@181: /* VAR [CONSTANT] var_init_decl_list END_VAR */ mario@181: /* option -> may be NULL ! */ mario@181: /* helper symbol for input_declarations */ mario@181: void *search_var_instance_decl_c::visit(var_declarations_c *symbol) { mario@181: current_vartype = private_vt; manuele@507: current_option = none_opt; /* not really required. Just to make the code more readable */ manuele@507: if (NULL != symbol->option) manuele@507: symbol->option->accept(*this); mario@181: void *res = symbol->var_init_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; manuele@507: current_option = none_opt; mario@181: } mario@181: return res; mario@181: } mario@181: mario@181: /* VAR RETAIN var_init_decl_list END_VAR */ mario@181: void *search_var_instance_decl_c::visit(retentive_var_declarations_c *symbol) { mario@181: current_vartype = private_vt; manuele@507: current_option = retain_opt; mario@181: void *res = symbol->var_init_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; manuele@507: current_option = none_opt; mario@181: } mario@181: return res; mario@181: } mario@181: mario@181: /* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */ mario@181: /* option -> may be NULL ! */ mario@181: //SYM_REF2(located_var_declarations_c, option, located_var_decl_list) mario@181: void *search_var_instance_decl_c::visit(located_var_declarations_c *symbol) { mario@181: current_vartype = located_vt; manuele@507: current_option = none_opt; /* not really required. Just to make the code more readable */ manuele@507: if (NULL != symbol->option) manuele@507: symbol->option->accept(*this); mario@181: void *res = symbol->located_var_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; manuele@507: current_option = none_opt; mario@181: } mario@181: return res; mario@181: } mario@181: mario@181: /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */ mario@181: /* option -> may be NULL ! */ mario@181: //SYM_REF2(external_var_declarations_c, option, external_declaration_list) mario@181: void *search_var_instance_decl_c::visit(external_var_declarations_c *symbol) { mario@181: current_vartype = external_vt; manuele@507: current_option = none_opt; /* not really required. Just to make the code more readable */ manuele@507: if (NULL != symbol->option) manuele@507: symbol->option->accept(*this); mario@181: void *res = symbol->external_declaration_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; manuele@507: current_option = none_opt; mario@181: } mario@181: return res; mario@181: } mario@181: mario@181: /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */ mario@181: /* option -> may be NULL ! */ mario@181: //SYM_REF2(global_var_declarations_c, option, global_var_decl_list) mario@181: void *search_var_instance_decl_c::visit(global_var_declarations_c *symbol) { mario@181: current_vartype = global_vt; manuele@507: current_option = none_opt; /* not really required. Just to make the code more readable */ manuele@507: if (NULL != symbol->option) manuele@507: symbol->option->accept(*this); mario@181: void *res = symbol->global_var_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; manuele@507: current_option = none_opt; mario@181: } mario@181: return res; mario@181: } mario@181: mario@181: /* var1_list is one of the following... mario@181: * simple_spec_init_c * mario@181: * subrange_spec_init_c * mario@181: * enumerated_spec_init_c * mario@181: */ mario@181: // SYM_REF2(var1_init_decl_c, var1_list, spec_init) mario@181: void *search_var_instance_decl_c::visit(var1_init_decl_c *symbol) { mario@181: current_type_decl = symbol->spec_init; mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: /* var1_list ',' variable_name */ mario@181: // SYM_LIST(var1_list_c) mario@181: void *search_var_instance_decl_c::visit(var1_list_c *symbol) { mario@181: list_c *list = symbol; mario@181: for(int i = 0; i < list->n; i++) { mario@181: if (compare_identifiers(list->elements[i], search_name) == 0) mario@181: /* by now, current_type_decl should be != NULL */ mario@181: return current_type_decl; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: /* name_list ':' function_block_type_name ASSIGN structure_initialization */ mario@181: /* structure_initialization -> may be NULL ! */ mario@181: void *search_var_instance_decl_c::visit(fb_name_decl_c *symbol) { mario@181: current_type_decl = symbol->function_block_type_name; mario@181: return symbol->fb_name_list->accept(*this); mario@181: } mario@181: mario@181: /* name_list ',' fb_name */ mario@181: void *search_var_instance_decl_c::visit(fb_name_list_c *symbol) { mario@181: list_c *list = symbol; mario@181: for(int i = 0; i < list->n; i++) { mario@181: if (compare_identifiers(list->elements[i], search_name) == 0) msousa@504: /* by now, current_fb_declaration should be != NULL */ mario@181: return current_type_decl; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: /* var1_list ':' array_spec_init */ mario@181: // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) mario@181: void *search_var_instance_decl_c::visit(array_var_init_decl_c *symbol) { mario@181: current_type_decl = symbol->array_spec_init; mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: /* var1_list ':' initialized_structure */ mario@181: // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) mario@181: void *search_var_instance_decl_c::visit(structured_var_init_decl_c *symbol) { mario@181: current_type_decl = symbol->initialized_structure; mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: /* var1_list ':' array_specification */ mario@181: // SYM_REF2(array_var_declaration_c, var1_list, array_specification) mario@181: void *search_var_instance_decl_c::visit(array_var_declaration_c *symbol) { mario@181: current_type_decl = symbol->array_specification; mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: /* var1_list ':' structure_type_name */ mario@181: // SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) mario@181: void *search_var_instance_decl_c::visit(structured_var_declaration_c *symbol) { mario@181: current_type_decl = symbol->structure_type_name; mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: /* [variable_name] location ':' located_var_spec_init */ mario@181: /* variable_name -> may be NULL ! */ mario@181: // SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) mario@181: // TODO!! mario@181: mario@181: /* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ mario@181: // SYM_REF2(external_declaration_c, global_var_name, specification) mario@181: void *search_var_instance_decl_c::visit(external_declaration_c *symbol) { mario@181: if (compare_identifiers(symbol->global_var_name, search_name) == 0) mario@181: return symbol->specification; mario@181: return NULL; mario@181: } mario@181: mario@181: /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ mario@181: /* type_specification ->may be NULL ! */ mario@181: // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) mario@181: void *search_var_instance_decl_c::visit(global_var_decl_c *symbol) { mario@181: if (symbol->type_specification != NULL) { mario@181: current_type_decl = symbol->type_specification; mario@181: return symbol->global_var_spec->accept(*this); mario@181: } mario@181: else mario@181: return NULL; mario@181: } mario@181: mario@181: /*| global_var_name location */ mario@181: //SYM_REF2(global_var_spec_c, global_var_name, location) mario@181: void *search_var_instance_decl_c::visit(global_var_spec_c *symbol) { mario@181: if (symbol->global_var_name != NULL && compare_identifiers(symbol->global_var_name, search_name) == 0) mario@181: return current_type_decl; mario@181: else mario@181: return symbol->location->accept(*this); mario@181: } mario@181: mario@181: /*| global_var_list ',' global_var_name */ mario@181: //SYM_LIST(global_var_list_c) mario@181: void *search_var_instance_decl_c::visit(global_var_list_c *symbol) { mario@181: list_c *list = symbol; mario@181: for(int i = 0; i < list->n; i++) { mario@181: if (compare_identifiers(list->elements[i], search_name) == 0) mario@181: /* by now, current_type_decl should be != NULL */ mario@181: return current_type_decl; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: /* [variable_name] location ':' located_var_spec_init */ mario@181: /* variable_name -> may be NULL ! */ mario@181: //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) mario@181: void *search_var_instance_decl_c::visit(located_var_decl_c *symbol) { mario@181: if (symbol->variable_name != NULL && compare_identifiers(symbol->variable_name, search_name) == 0) mario@181: return symbol->located_var_spec_init; mario@181: else { mario@181: current_type_decl = symbol->located_var_spec_init; mario@181: return symbol->location->accept(*this); mario@181: } mario@181: } mario@181: mario@181: /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ mario@181: /* type_specification ->may be NULL ! */ mario@181: // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) mario@181: // TODO!! mario@181: mario@181: /* AT direct_variable */ mario@181: // SYM_REF2(location_c, direct_variable, unused) mario@181: void *search_var_instance_decl_c::visit(location_c *symbol) { mario@181: if (compare_identifiers(symbol->direct_variable, search_name) == 0) mario@181: return current_type_decl; mario@181: else mario@181: return NULL; mario@181: } mario@181: mario@181: /*| global_var_list ',' global_var_name */ mario@181: // SYM_LIST(global_var_list_c) mario@181: // TODO!! mario@181: mario@181: /* var1_list ':' single_byte_string_spec */ mario@181: // SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec) mario@181: void *search_var_instance_decl_c::visit(single_byte_string_var_declaration_c *symbol) { mario@181: current_type_decl = symbol->single_byte_string_spec; mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: /* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */ mario@181: /* integer ->may be NULL ! */ mario@181: /* single_byte_character_string ->may be NULL ! */ mario@181: // SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string) mario@181: // TODO!! mario@181: mario@181: /* var1_list ':' double_byte_string_spec */ mario@181: // SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec) mario@181: void *search_var_instance_decl_c::visit(double_byte_string_var_declaration_c *symbol) { mario@181: current_type_decl = symbol->double_byte_string_spec; mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: /* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */ mario@181: /* integer ->may be NULL ! */ mario@181: /* double_byte_character_string ->may be NULL ! */ mario@181: // SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string) mario@181: // TODO!! mario@181: mario@181: /* variable_name incompl_location ':' var_spec */ mario@181: // SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused) mario@181: // TODO!! mario@181: mario@181: /* AT incompl_location_token */ mario@181: // SYM_TOKEN(incompl_location_c) mario@181: // TODO!! mario@181: mario@181: mario@181: /**************************************/ mario@181: /* B.1.5 - Program organization units */ mario@181: /**************************************/ mario@181: /***********************/ mario@181: /* B 1.5.1 - Functions */ mario@181: /***********************/ mario@181: // SYM_REF4(function_declaration_c, derived_function_name, type_name, var_declarations_list, function_body) mario@181: void *search_var_instance_decl_c::visit(function_declaration_c *symbol) { mario@181: /* functions have a variable named after themselves, to store mario@181: * the variable that will be returned!! mario@181: */ mario@181: if (compare_identifiers(symbol->derived_function_name, search_name) == 0) mario@181: return symbol->type_name; mario@181: mario@181: /* no need to search through all the body, so we only mario@181: * visit the variable declarations...! mario@181: */ mario@181: return symbol->var_declarations_list->accept(*this); mario@181: } mario@181: mario@181: /*****************************/ mario@181: /* B 1.5.2 - Function Blocks */ mario@181: /*****************************/ mario@181: void *search_var_instance_decl_c::visit(function_block_declaration_c *symbol) { mario@181: /* no need to search through all the body, so we only mario@181: * visit the variable declarations...! mario@181: */ mario@181: return symbol->var_declarations->accept(*this); mario@181: } mario@181: mario@181: /**********************/ mario@181: /* B 1.5.3 - Programs */ mario@181: /**********************/ mario@181: void *search_var_instance_decl_c::visit(program_declaration_c *symbol) { mario@181: /* no need to search through all the body, so we only mario@181: * visit the variable declarations...! mario@181: */ mario@181: return symbol->var_declarations->accept(*this); mario@181: } mario@181: mario@181: mario@181: /********************************/ mario@181: /* B 1.7 Configuration elements */ mario@181: /********************************/ mario@181: mario@181: /* mario@181: CONFIGURATION configuration_name mario@181: optional_global_var_declarations mario@181: (resource_declaration_list | single_resource_declaration) mario@181: optional_access_declarations mario@181: optional_instance_specific_initializations mario@181: END_CONFIGURATION mario@181: */ mario@181: /* mario@181: SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) mario@181: */ mario@181: void *search_var_instance_decl_c::visit(configuration_declaration_c *symbol) { mario@181: /* no need to search through all the configuration, so we only mario@181: * visit the global variable declarations...! mario@181: */ mario@181: if (symbol->global_var_declarations != NULL) mario@181: return symbol->global_var_declarations->accept(*this); mario@181: else mario@181: return NULL; mario@181: } mario@181: mario@181: /* mario@181: RESOURCE resource_name ON resource_type_name mario@181: optional_global_var_declarations mario@181: single_resource_declaration mario@181: END_RESOURCE mario@181: */ mario@181: // SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) mario@181: void *search_var_instance_decl_c::visit(resource_declaration_c *symbol) { mario@181: /* no need to search through all the resource, so we only mario@181: * visit the global variable declarations...! mario@181: */ mario@181: if (symbol->global_var_declarations != NULL) mario@181: return symbol->global_var_declarations->accept(*this); mario@181: else mario@181: return NULL; mario@181: } mario@181: mario@181: /* task_configuration_list program_configuration_list */ mario@181: // SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) mario@181: void *search_var_instance_decl_c::visit(single_resource_declaration_c *symbol) { mario@181: /* no need to search through all the resource, mario@181: * and there is no global variable declarations...! mario@181: */ mario@181: return NULL; mario@181: } mario@181: