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