mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@265: * msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant msousa@265: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * mario@181: * mario@181: * This code is made available on the understanding that it will not be mario@181: * used in safety-critical situations without a full and competent review. mario@181: */ mario@181: mario@181: /* msousa@265: * An IEC 61131-3 compiler. mario@181: * mario@181: * Based on the mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) mario@181: * mario@181: */ mario@181: mario@181: /* Determine the data type of a specific variable instance, including mario@181: * function block instances. mario@181: * A reference to the relevant variable declaration is returned. laurent@217: * The variable instance may NOT be a member of a structure of a member mario@181: * of a structure of an element of an array of ... mario@181: * mario@181: * example: mario@181: * window.points[1].coordinate.x mario@181: * window.points[1].colour mario@181: * etc... 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 mario@181: * 'window' !! mario@181: * mario@181: * mario@181: * If you need to pass a complete name of a variable instance (such as mario@181: * 'window.points[1].coordinate.x') use the search_varfb_instance_type_c instead! mario@181: */ mario@181: mario@181: /* Note that 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: */ mario@181: #include "absyntax_utils.hh" mario@181: mario@181: 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; mario@181: } mario@181: mario@181: symbol_c *search_var_instance_decl_c::get_decl(symbol_c *variable_instance_name) { laurent@217: this->current_vartype = none_vt; mario@181: this->search_name = variable_instance_name; mario@181: return (symbol_c *)search_scope->accept(*this); mario@181: } mario@181: msousa@417: unsigned int search_var_instance_decl_c::get_vartype(symbol_c *variable_instance_name) { msousa@417: this->current_vartype = none_vt; msousa@417: this->search_name = variable_instance_name; msousa@417: search_scope->accept(*this); mario@181: return current_vartype; mario@181: } mario@181: 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 mario@181: mario@181: void *search_var_instance_decl_c::visit(input_declarations_c *symbol) { mario@181: current_vartype = input_vt; mario@181: void *res = symbol->input_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: 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; mario@181: void *res = symbol->var_init_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; 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; 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; mario@181: void *res = symbol->var_init_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; 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; mario@181: void *res = symbol->var_init_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; 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; mario@181: void *res = symbol->located_var_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; 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; mario@181: void *res = symbol->external_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: 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; mario@181: void *res = symbol->global_var_decl_list->accept(*this); mario@181: if (res == NULL) { mario@181: current_vartype = none_vt; 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) mario@181: /* 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: mario@181: #if 0 mario@181: /*********************/ mario@181: /* B 1.4 - Variables */ mario@181: /*********************/ mario@181: SYM_REF2(symbolic_variable_c, var_name, unused) mario@181: mario@181: /********************************************/ mario@181: /* B.1.4.1 Directly Represented Variables */ mario@181: /********************************************/ mario@181: SYM_TOKEN(direct_variable_c) mario@181: mario@181: /*************************************/ mario@181: /* B.1.4.2 Multi-element Variables */ mario@181: /*************************************/ mario@181: /* subscripted_variable '[' subscript_list ']' */ mario@181: SYM_REF2(array_variable_c, subscripted_variable, subscript_list) mario@181: mario@181: /* subscript_list ',' subscript */ mario@181: SYM_LIST(subscript_list_c) mario@181: mario@181: /* record_variable '.' field_selector */ mario@181: /* WARNING: input and/or output variables of function blocks mario@181: * may be accessed as fields of a tructured variable! mario@181: * Code handling a structured_variable_c must take mario@181: * this into account! mario@181: */ mario@181: SYM_REF2(structured_variable_c, record_variable, field_selector) mario@181: mario@181: mario@181: mario@181: }; mario@181: #endif