lbessard@70: /* lbessard@70: * (c) 2003 Mario de Sousa lbessard@70: * lbessard@70: * Offered to the public under the terms of the GNU General Public License lbessard@70: * as published by the Free Software Foundation; either version 2 of the lbessard@70: * License, or (at your option) any later version. lbessard@70: * lbessard@70: * This program is distributed in the hope that it will be useful, but lbessard@70: * WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@70: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General lbessard@70: * Public License for more details. lbessard@70: * lbessard@70: * This code is made available on the understanding that it will not be lbessard@70: * used in safety-critical situations without a full and competent review. lbessard@70: */ lbessard@70: lbessard@70: /* lbessard@70: * An IEC 61131-3 IL and ST compiler. lbessard@70: * lbessard@70: * Based on the lbessard@70: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) lbessard@70: * lbessard@70: */ lbessard@70: lbessard@70: /* Determine the data type of a specific variable instance, including lbessard@70: * function block instances. lbessard@70: * A reference to the relevant variable declaration is returned. lbessard@70: * The variable instance may NOT be a member of a structure of a memeber lbessard@70: * of a structure of an element of an array of ... lbessard@70: * lbessard@70: * example: lbessard@70: * window.points[1].coordinate.x lbessard@70: * window.points[1].colour lbessard@70: * etc... ARE NOT ALLOWED! lbessard@70: * lbessard@70: * This class must only be passed the name of the variable that will appear lbessard@70: * in the variable declaration. In the above examples, this would be lbessard@70: * 'window' !! lbessard@70: * lbessard@70: * lbessard@70: * If you need to pass a complete name of a variable instance (such as lbessard@70: * 'window.points[1].coordinate.x') use the search_varfb_instance_type_c instead! lbessard@70: */ lbessard@70: /* Note that current_type_decl that this class returns may reference the lbessard@70: * name of a type, or the type declaration itself! lbessard@70: * For an example of the first, consider a variable declared as ... lbessard@70: * x : AAA; lbessard@70: * where the AAA type is previously declared as whatever. lbessard@70: * For an example of the second, consider a variable declared as ... lbessard@70: * x : array of int [10]; ----> is allowed lbessard@70: * lbessard@70: * If it is the first, we will return a reference to the name, if the second lbessard@70: * we return a reference to the declaration!! lbessard@70: */ lbessard@70: class search_var_instance_decl_c: public search_visitor_c { lbessard@70: lbessard@70: private: lbessard@70: symbol_c *search_scope; lbessard@70: symbol_c *search_name; lbessard@70: symbol_c *current_type_decl; lbessard@70: lbessard@70: /* variable used to store the type of variable currently being processed... */ lbessard@70: /* Will contain a single value of generate_c_vardecl_c::XXXX_vt */ lbessard@70: unsigned int current_vartype; lbessard@70: lbessard@70: public: lbessard@70: search_var_instance_decl_c(symbol_c *search_scope) { lbessard@70: this->current_vartype = none_vt; lbessard@70: this->search_scope = search_scope; lbessard@70: this->search_name = NULL; lbessard@70: this->current_type_decl = NULL; lbessard@70: } lbessard@70: lbessard@70: symbol_c *get_decl(symbol_c *variable_instance_name) { lbessard@70: this->search_name = variable_instance_name; lbessard@70: return (symbol_c *)search_scope->accept(*this); lbessard@70: } lbessard@70: lbessard@70: unsigned int get_vartype() { lbessard@70: return current_vartype; lbessard@70: } lbessard@70: lbessard@70: public: lbessard@70: lbessard@70: /* the types of variables that need to be processed... */ lbessard@70: static const unsigned int none_vt = 0x0000; lbessard@70: static const unsigned int input_vt = 0x0001; // VAR_INPUT lbessard@70: static const unsigned int output_vt = 0x0002; // VAR_OUTPUT lbessard@70: static const unsigned int inoutput_vt = 0x0004; // VAR_IN_OUT lbessard@70: static const unsigned int private_vt = 0x0008; // VAR lbessard@70: static const unsigned int temp_vt = 0x0010; // VAR_TEMP lbessard@70: static const unsigned int external_vt = 0x0020; // VAR_EXTERNAL lbessard@70: static const unsigned int global_vt = 0x0040; // VAR_GLOBAL lbessard@70: static const unsigned int located_vt = 0x0080; // VAR AT lbessard@70: lbessard@70: lbessard@70: /***************************/ lbessard@70: /* B 0 - Programming Model */ lbessard@70: /***************************/ lbessard@70: void *visit(library_c *symbol) { lbessard@70: /* we do not want to search multiple declaration scopes, lbessard@70: * so we do not visit all the functions, fucntion blocks, etc... lbessard@70: */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: lbessard@70: /******************************************/ lbessard@70: /* B 1.4.3 - Declaration & Initialisation */ lbessard@70: /******************************************/ lbessard@70: /* edge -> The F_EDGE or R_EDGE directive */ lbessard@70: // SYM_REF2(edge_declaration_c, edge, var1_list) lbessard@70: // TODO lbessard@70: lbessard@70: void *visit(input_declarations_c *symbol) { lbessard@70: current_vartype = input_vt; lbessard@70: void *res = symbol->input_declaration_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */ lbessard@70: /* option -> may be NULL ! */ lbessard@70: void *visit(output_declarations_c *symbol) { lbessard@70: current_vartype = output_vt; lbessard@70: void *res = symbol->var_init_decl_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /* VAR_IN_OUT var_declaration_list END_VAR */ lbessard@70: void *visit(input_output_declarations_c *symbol) { lbessard@70: current_vartype = inoutput_vt; lbessard@70: void *res = symbol->var_declaration_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /* VAR [CONSTANT] var_init_decl_list END_VAR */ lbessard@70: /* option -> may be NULL ! */ lbessard@70: /* helper symbol for input_declarations */ lbessard@70: void *visit(var_declarations_c *symbol) { lbessard@70: current_vartype = private_vt; lbessard@70: void *res = symbol->var_init_decl_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /* VAR RETAIN var_init_decl_list END_VAR */ lbessard@70: void *visit(retentive_var_declarations_c *symbol) { lbessard@70: current_vartype = private_vt; lbessard@70: void *res = symbol->var_init_decl_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */ lbessard@70: /* option -> may be NULL ! */ lbessard@70: //SYM_REF2(located_var_declarations_c, option, located_var_decl_list) lbessard@70: void *visit(located_var_declarations_c *symbol) { lbessard@70: current_vartype = located_vt; lbessard@70: void *res = symbol->located_var_decl_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */ lbessard@70: /* option -> may be NULL ! */ lbessard@70: //SYM_REF2(external_var_declarations_c, option, external_declaration_list) lbessard@70: void *visit(external_var_declarations_c *symbol) { lbessard@70: current_vartype = external_vt; lbessard@70: void *res = symbol->external_declaration_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */ lbessard@70: /* option -> may be NULL ! */ lbessard@70: //SYM_REF2(global_var_declarations_c, option, global_var_decl_list) lbessard@70: void *visit(global_var_declarations_c *symbol) { lbessard@70: current_vartype = global_vt; lbessard@70: void *res = symbol->global_var_decl_list->accept(*this); lbessard@70: if (res == NULL) { lbessard@70: current_vartype = none_vt; lbessard@70: } lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: /* var1_list is one of the following... lbessard@70: * simple_spec_init_c * lbessard@70: * subrange_spec_init_c * lbessard@70: * enumerated_spec_init_c * lbessard@70: */ lbessard@70: // SYM_REF2(var1_init_decl_c, var1_list, spec_init) lbessard@70: void *visit(var1_init_decl_c *symbol) { lbessard@70: current_type_decl = symbol->spec_init; lbessard@70: return symbol->var1_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* var1_list ',' variable_name */ lbessard@70: // SYM_LIST(var1_list_c) lbessard@70: void *visit(var1_list_c *symbol) { lbessard@70: list_c *list = symbol; lbessard@70: for(int i = 0; i < list->n; i++) { lbessard@70: if (compare_identifiers(list->elements[i], search_name) == 0) lbessard@70: /* by now, current_type_decl should be != NULL */ lbessard@70: return current_type_decl; lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* name_list ':' function_block_type_name ASSIGN structure_initialization */ lbessard@70: /* structure_initialization -> may be NULL ! */ lbessard@70: void *visit(fb_name_decl_c *symbol) { lbessard@70: current_type_decl = symbol->function_block_type_name; lbessard@70: return symbol->fb_name_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* name_list ',' fb_name */ lbessard@70: void *visit(fb_name_list_c *symbol) { lbessard@70: list_c *list = symbol; lbessard@70: for(int i = 0; i < list->n; i++) { lbessard@70: if (compare_identifiers(list->elements[i], search_name) == 0) lbessard@70: /* by now, current_fb_declaration should be != NULL */ lbessard@70: return current_type_decl; lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* var1_list ':' array_spec_init */ lbessard@70: // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) lbessard@70: void *visit(array_var_init_decl_c *symbol) { lbessard@70: current_type_decl = symbol->array_spec_init; lbessard@70: return symbol->var1_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* var1_list ':' initialized_structure */ lbessard@70: // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) lbessard@70: void *visit(structured_var_init_decl_c *symbol) { lbessard@70: current_type_decl = symbol->initialized_structure; lbessard@70: return symbol->var1_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* var1_list ':' array_specification */ lbessard@70: // SYM_REF2(array_var_declaration_c, var1_list, array_specification) lbessard@70: void *visit(array_var_declaration_c *symbol) { lbessard@70: current_type_decl = symbol->array_specification; lbessard@70: return symbol->var1_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* var1_list ':' structure_type_name */ lbessard@70: // SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) lbessard@70: void *visit(structured_var_declaration_c *symbol) { lbessard@70: current_type_decl = symbol->structure_type_name; lbessard@70: return symbol->var1_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* [variable_name] location ':' located_var_spec_init */ lbessard@70: /* variable_name -> may be NULL ! */ lbessard@70: // SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) lbessard@70: // TODO!! lbessard@70: lbessard@70: /* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ lbessard@70: // SYM_REF2(external_declaration_c, global_var_name, specification) lbessard@70: void *visit(external_declaration_c *symbol) { lbessard@70: if (compare_identifiers(symbol->global_var_name, search_name) == 0) lbessard@70: return symbol->specification; lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ lbessard@70: /* type_specification ->may be NULL ! */ lbessard@70: // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) lbessard@70: void *visit(global_var_decl_c *symbol) { lbessard@70: if (symbol->type_specification != NULL) { lbessard@70: current_type_decl = symbol->type_specification; lbessard@70: return symbol->global_var_spec->accept(*this); lbessard@70: } lbessard@70: else lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /*| global_var_name location */ lbessard@70: //SYM_REF2(global_var_spec_c, global_var_name, location) lbessard@70: void *visit(global_var_spec_c *symbol) { lbessard@70: if (symbol->global_var_name != NULL && compare_identifiers(symbol->global_var_name, search_name) == 0) lbessard@70: return current_type_decl; lbessard@70: else lbessard@70: return symbol->location->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /*| global_var_list ',' global_var_name */ lbessard@70: //SYM_LIST(global_var_list_c) lbessard@70: void *visit(global_var_list_c *symbol) { lbessard@70: list_c *list = symbol; lbessard@70: for(int i = 0; i < list->n; i++) { lbessard@70: if (compare_identifiers(list->elements[i], search_name) == 0) lbessard@70: /* by now, current_type_decl should be != NULL */ lbessard@70: return current_type_decl; lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* [variable_name] location ':' located_var_spec_init */ lbessard@70: /* variable_name -> may be NULL ! */ lbessard@70: //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) lbessard@70: void *visit(located_var_decl_c *symbol) { lbessard@70: if (symbol->variable_name != NULL && compare_identifiers(symbol->variable_name, search_name) == 0) { lbessard@70: return symbol->located_var_spec_init; lbessard@70: } lbessard@70: else lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ lbessard@70: /* type_specification ->may be NULL ! */ lbessard@70: // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) lbessard@70: // TODO!! lbessard@70: lbessard@70: /*| global_var_name location */ lbessard@70: // SYM_REF2(global_var_spec_c, global_var_name, location) lbessard@70: // TODO!! lbessard@70: lbessard@70: /* AT direct_variable */ lbessard@70: // SYM_REF2(location_c, direct_variable, unused) lbessard@70: // TODO!! lbessard@70: lbessard@70: /*| global_var_list ',' global_var_name */ lbessard@70: // SYM_LIST(global_var_list_c) lbessard@70: // TODO!! lbessard@70: lbessard@70: /* var1_list ':' single_byte_string_spec */ lbessard@70: // SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec) lbessard@70: void *visit(single_byte_string_var_declaration_c *symbol) { lbessard@70: current_type_decl = symbol->single_byte_string_spec; lbessard@70: return symbol->var1_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */ lbessard@70: /* integer ->may be NULL ! */ lbessard@70: /* single_byte_character_string ->may be NULL ! */ lbessard@70: // SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string) lbessard@70: // TODO!! lbessard@70: lbessard@70: /* var1_list ':' double_byte_string_spec */ lbessard@70: // SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec) lbessard@70: void *visit(double_byte_string_var_declaration_c *symbol) { lbessard@70: current_type_decl = symbol->double_byte_string_spec; lbessard@70: return symbol->var1_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */ lbessard@70: /* integer ->may be NULL ! */ lbessard@70: /* double_byte_character_string ->may be NULL ! */ lbessard@70: // SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string) lbessard@70: // TODO!! lbessard@70: lbessard@70: /* variable_name incompl_location ':' var_spec */ lbessard@70: // SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused) lbessard@70: // TODO!! lbessard@70: lbessard@70: /* AT incompl_location_token */ lbessard@70: // SYM_TOKEN(incompl_location_c) lbessard@70: // TODO!! lbessard@70: lbessard@70: lbessard@70: /**************************************/ lbessard@70: /* B.1.5 - Program organization units */ lbessard@70: /**************************************/ lbessard@70: /***********************/ lbessard@70: /* B 1.5.1 - Functions */ lbessard@70: /***********************/ lbessard@70: // SYM_REF4(function_declaration_c, derived_function_name, type_name, var_declarations_list, function_body) lbessard@70: void *visit(function_declaration_c *symbol) { lbessard@70: /* functions have a variable named after themselves, to store lbessard@70: * the variable that will be returned!! lbessard@70: */ lbessard@70: if (compare_identifiers(symbol->derived_function_name, search_name) == 0) lbessard@70: return symbol->type_name; lbessard@70: lbessard@70: /* no need to search through all the body, so we only lbessard@70: * visit the variable declarations...! lbessard@70: */ lbessard@70: return symbol->var_declarations_list->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /*****************************/ lbessard@70: /* B 1.5.2 - Function Blocks */ lbessard@70: /*****************************/ lbessard@70: void *visit(function_block_declaration_c *symbol) { lbessard@70: /* no need to search through all the body, so we only lbessard@70: * visit the variable declarations...! lbessard@70: */ lbessard@70: return symbol->var_declarations->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /**********************/ lbessard@70: /* B 1.5.3 - Programs */ lbessard@70: /**********************/ lbessard@70: void *visit(program_declaration_c *symbol) { lbessard@70: /* no need to search through all the body, so we only lbessard@70: * visit the variable declarations...! lbessard@70: */ lbessard@70: return symbol->var_declarations->accept(*this); lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /********************************/ lbessard@70: /* B 1.7 Configuration elements */ lbessard@70: /********************************/ lbessard@70: lbessard@70: /* lbessard@70: CONFIGURATION configuration_name lbessard@70: optional_global_var_declarations lbessard@70: (resource_declaration_list | single_resource_declaration) lbessard@70: optional_access_declarations lbessard@70: optional_instance_specific_initializations lbessard@70: END_CONFIGURATION lbessard@70: */ lbessard@70: /* lbessard@70: SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) lbessard@70: */ lbessard@70: void *visit(configuration_declaration_c *symbol) { lbessard@70: /* no need to search through all the configuration, so we only lbessard@70: * visit the global variable declarations...! lbessard@70: */ lbessard@70: if (symbol->global_var_declarations != NULL) lbessard@70: return symbol->global_var_declarations->accept(*this); lbessard@70: else lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* lbessard@70: RESOURCE resource_name ON resource_type_name lbessard@70: optional_global_var_declarations lbessard@70: single_resource_declaration lbessard@70: END_RESOURCE lbessard@70: */ lbessard@70: // SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) lbessard@70: void *visit(resource_declaration_c *symbol) { lbessard@70: /* no need to search through all the resource, so we only lbessard@70: * visit the global variable declarations...! lbessard@70: */ lbessard@70: if (symbol->global_var_declarations != NULL) lbessard@70: return symbol->global_var_declarations->accept(*this); lbessard@70: else lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* task_configuration_list program_configuration_list */ lbessard@70: // SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) lbessard@70: void *visit(single_resource_declaration_c *symbol) { lbessard@70: /* no need to search through all the resource, lbessard@70: * and there is no global variable declarations...! lbessard@70: */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: #if 0 lbessard@70: /*********************/ lbessard@70: /* B 1.4 - Variables */ lbessard@70: /*********************/ lbessard@70: SYM_REF2(symbolic_variable_c, var_name, unused) lbessard@70: lbessard@70: /********************************************/ lbessard@70: /* B.1.4.1 Directly Represented Variables */ lbessard@70: /********************************************/ lbessard@70: SYM_TOKEN(direct_variable_c) lbessard@70: lbessard@70: /*************************************/ lbessard@70: /* B.1.4.2 Multi-element Variables */ lbessard@70: /*************************************/ lbessard@70: /* subscripted_variable '[' subscript_list ']' */ lbessard@70: SYM_REF2(array_variable_c, subscripted_variable, subscript_list) lbessard@70: lbessard@70: /* subscript_list ',' subscript */ lbessard@70: SYM_LIST(subscript_list_c) lbessard@70: lbessard@70: /* record_variable '.' field_selector */ lbessard@70: /* WARNING: input and/or output variables of function blocks lbessard@70: * may be accessed as fields of a tructured variable! lbessard@70: * Code handling a structured_variable_c must take lbessard@70: * this into account! lbessard@70: */ lbessard@70: SYM_REF2(structured_variable_c, record_variable, field_selector) lbessard@70: lbessard@70: lbessard@70: #endif lbessard@70: }; lbessard@70: