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:
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: *
mario@181: * 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: */
mario@181:
mario@181:
mario@181: class search_var_instance_decl_c: public search_visitor_c {
mario@181:
mario@181: public:
mario@181: search_var_instance_decl_c(symbol_c *search_scope);
mario@181:
mario@181: public:
msousa@536: typedef enum {
msousa@536: input_vt , // VAR_INPUT
msousa@536: output_vt , // VAR_OUTPUT
msousa@536: inoutput_vt, // VAR_IN_OUT
msousa@536: private_vt , // VAR
msousa@536: temp_vt , // VAR_TEMP
msousa@536: external_vt, // VAR_EXTERNAL
msousa@536: global_vt , // VAR_GLOBAL
msousa@536: located_vt , // VAR AT
msousa@536: none_vt
msousa@536: } vt_t;
msousa@536:
msousa@536: typedef enum {
msousa@536: constant_opt ,
msousa@536: retain_opt ,
msousa@536: non_retain_opt,
msousa@536: none_opt
msousa@536: } opt_t;
msousa@536: #if 0
mario@181: /* the types of variables that need to be processed... */
mario@181: static const unsigned int none_vt = 0x0000;
mario@181: static const unsigned int input_vt = 0x0001; // VAR_INPUT
mario@181: static const unsigned int output_vt = 0x0002; // VAR_OUTPUT
mario@181: static const unsigned int inoutput_vt = 0x0004; // VAR_IN_OUT
mario@181: static const unsigned int private_vt = 0x0008; // VAR
mario@181: static const unsigned int temp_vt = 0x0010; // VAR_TEMP
mario@181: static const unsigned int external_vt = 0x0020; // VAR_EXTERNAL
mario@181: static const unsigned int global_vt = 0x0040; // VAR_GLOBAL
mario@181: static const unsigned int located_vt = 0x0080; // VAR AT
mario@181:
manuele@507: static const unsigned int none_opt = 0x0000;
manuele@507: static const unsigned int constant_opt = 0x0001;
manuele@507: static const unsigned int retain_opt = 0x0002;
manuele@507: static const unsigned int non_retain_opt = 0x0003;
msousa@536: #endif
msousa@536:
msousa@536: symbol_c * get_decl (symbol_c *variable_instance_name);
msousa@536: vt_t get_vartype(symbol_c *variable_instance_name);
msousa@536: opt_t get_option (symbol_c *variable_instance_name);
msousa@536:
msousa@536: /* NOTE: The following function will be completely deleted in the (hopefully near) future. */
msousa@536: /* Returns true if the variable is an ARRAY or a STRUCT, otherwise returns false.
msousa@536: * Note that for FB, also returns false!
msousa@536: */
msousa@536: bool type_is_complex(symbol_c *variable_name);
msousa@536:
msousa@536:
msousa@536:
msousa@536: private:
msousa@536: symbol_c *search_scope;
msousa@536: symbol_c *search_name;
msousa@536: symbol_c *current_type_decl;
msousa@536: /* variable used to store the type of variable currently being processed... */
msousa@536: /* Will contain a single value of generate_c_vardecl_c::XXXX_vt */
msousa@536: vt_t current_vartype;
msousa@536: opt_t current_option;
msousa@536:
msousa@536:
mario@181: private:
mario@181: /***************************/
mario@181: /* B 0 - Programming Model */
mario@181: /***************************/
mario@181: void *visit(library_c *symbol);
mario@181:
mario@181: /******************************************/
mario@181: /* B 1.4.3 - Declaration & Initialisation */
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 *visit(constant_option_c *symbol);
manuele@507: void *visit(retain_option_c *symbol);
manuele@507: void *visit(non_retain_option_c *symbol);
manuele@507:
manuele@507:
mario@181: void *visit(input_declarations_c *symbol);
mario@181: /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */
mario@181: /* option -> may be NULL ! */
mario@181: void *visit(output_declarations_c *symbol);
mario@181: /* VAR_IN_OUT var_declaration_list END_VAR */
mario@181: void *visit(input_output_declarations_c *symbol);
ccb@202: /* ENO : BOOL */
ccb@202: void *visit(eno_param_declaration_c *symbol);
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 *visit(var_declarations_c *symbol);
mario@181: /* VAR RETAIN var_init_decl_list END_VAR */
mario@181: void *visit(retentive_var_declarations_c *symbol);
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 *visit(located_var_declarations_c *symbol);
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 *visit(external_var_declarations_c *symbol);
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 *visit(global_var_declarations_c *symbol);
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 *visit(var1_init_decl_c *symbol);
mario@181: /* var1_list ',' variable_name */
mario@181: // SYM_LIST(var1_list_c)
mario@181: void *visit(var1_list_c *symbol);
mario@181: /* name_list ':' function_block_type_name ASSIGN structure_initialization */
mario@181: /* structure_initialization -> may be NULL ! */
mario@181: void *visit(fb_name_decl_c *symbol);
mario@181: /* name_list ',' fb_name */
mario@181: void *visit(fb_name_list_c *symbol);
mario@181: /* var1_list ':' array_spec_init */
mario@181: // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
mario@181: void *visit(array_var_init_decl_c *symbol);
mario@181: /* var1_list ':' initialized_structure */
mario@181: // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
mario@181: void *visit(structured_var_init_decl_c *symbol);
mario@181: /* var1_list ':' array_specification */
mario@181: // SYM_REF2(array_var_declaration_c, var1_list, array_specification)
mario@181: void *visit(array_var_declaration_c *symbol);
mario@181: /* var1_list ':' structure_type_name */
mario@181: // SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
mario@181: void *visit(structured_var_declaration_c *symbol);
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 *visit(external_declaration_c *symbol);
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 *visit(global_var_decl_c *symbol);
mario@181: /*| global_var_name location */
mario@181: //SYM_REF2(global_var_spec_c, global_var_name, location)
mario@181: void *visit(global_var_spec_c *symbol);
mario@181: /*| global_var_list ',' global_var_name */
mario@181: //SYM_LIST(global_var_list_c)
mario@181: void *visit(global_var_list_c *symbol);
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 *visit(located_var_decl_c *symbol);
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: /* AT direct_variable */
mario@181: // SYM_REF2(location_c, direct_variable, unused)
mario@181: void *visit(location_c *symbol);
mario@181: /*| global_var_list ',' global_var_name */
mario@181: // SYM_LIST(global_var_list_c)
mario@181: // TODO!!
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 *visit(single_byte_string_var_declaration_c *symbol);
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 *visit(double_byte_string_var_declaration_c *symbol);
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 *visit(function_declaration_c *symbol);
mario@181:
mario@181: /*****************************/
mario@181: /* B 1.5.2 - Function Blocks */
mario@181: /*****************************/
mario@181: void *visit(function_block_declaration_c *symbol);
mario@181:
mario@181: /**********************/
mario@181: /* B 1.5.3 - Programs */
mario@181: /**********************/
mario@181: void *visit(program_declaration_c *symbol);
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 *visit(configuration_declaration_c *symbol);
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 *visit(resource_declaration_c *symbol);
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 *visit(single_resource_declaration_c *symbol);
mario@181:
mario@181: }; // search_var_instance_decl_c
mario@181: