ccb@204: /* ccb@204: * (c) 2003 Mario de Sousa ccb@204: * ccb@204: * Offered to the public under the terms of the GNU General Public License ccb@204: * as published by the Free Software Foundation; either version 2 of the ccb@204: * License, or (at your option) any later version. ccb@204: * ccb@204: * This program is distributed in the hope that it will be useful, but ccb@204: * WITHOUT ANY WARRANTY; without even the implied warranty of ccb@204: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General ccb@204: * Public License for more details. ccb@204: * ccb@204: * This code is made available on the understanding that it will not be ccb@204: * used in safety-critical situations without a full and competent review. ccb@204: */ ccb@204: ccb@204: /* ccb@204: * An IEC 61131-3 IL and ST compiler. ccb@204: * ccb@204: * Based on the ccb@204: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) ccb@204: * ccb@204: */ ccb@204: ccb@204: /* Verify whether the semantic rules of data type compatibility are being followed. ccb@204: * ccb@204: * For example: ccb@204: */ ccb@204: ccb@204: #include "../absyntax_utils/absyntax_utils.hh" ccb@204: ccb@204: class visit_expression_type_c: public search_constant_type_c { ccb@204: ccb@204: private: ccb@204: search_varfb_instance_type_c *search_varfb_instance_type; ccb@204: search_base_type_c search_base_type; ccb@204: ccb@204: /* When calling a function block, we must first find it's type, ccb@204: * by searching through the declarations of the variables currently ccb@204: * in scope. ccb@204: * This class does just that... ccb@204: * A new bject instance is instantiated whenever we start checking semantics ccb@204: * for a function block type declaration, or a program declaration. ccb@204: * This object instance will then later be called while the ccb@204: * function block's or the program's body is being handled. ccb@204: * ccb@204: * Note that functions cannot contain calls to function blocks, ccb@204: * so we do not create an object instance when handling ccb@204: * a function declaration. ccb@204: */ ccb@204: // search_var_instance_decl_c *search_var_instance_decl; ccb@204: ccb@204: /* This variable was created to pass information from ccb@204: * visit_expression_type_c::visit(case_statement_c *symbol) function to ccb@204: * visit_expression_type_c::visit(case_list_c *symbol) function. ccb@204: */ ccb@204: symbol_c *case_expression_type; ccb@204: ccb@204: /* In IL code, once we find a type mismatch error, it is best to ccb@204: * ignore any further errors until the end of the logicl operation, ccb@204: * i.e. until the next LD. ccb@204: * However, we cannot clear the il_error flag on all LD operations, ccb@204: * as these may also be used within parenthesis. LD operations ccb@204: * within parenthesis may not clear the error flag. ccb@204: * We therefore need a counter to know how deep inside a parenthesis ccb@204: * structure we are. ccb@204: */ ccb@204: int il_parenthesis_level; ccb@204: bool il_error; ccb@204: ccb@204: symbol_c *il_default_variable_type; ccb@204: symbol_c *il_operand_type; ccb@204: ccb@204: ccb@204: public: ccb@204: visit_expression_type_c(symbol_c *search_scope); ccb@204: virtual ~visit_expression_type_c(void); ccb@204: ccb@204: /* A helper function... */ ccb@204: bool is_ANY_ELEMENTARY_type(symbol_c *type_symbol); ccb@204: bool is_ANY_MAGNITUDE_type(symbol_c *type_symbol); ccb@204: bool is_ANY_DATE_type(symbol_c *type_symbol); ccb@204: bool is_ANY_STRING_type(symbol_c *type_symbol); ccb@204: bool is_ANY_INT_type(symbol_c *type_symbol); ccb@204: bool is_ANY_REAL_type(symbol_c *type_symbol); ccb@204: bool is_ANY_NUM_type(symbol_c *type_symbol); ccb@204: bool is_ANY_BIT_type(symbol_c *type_symbol); ccb@204: bool is_BOOL_type(symbol_c *type_symbol); ccb@204: ccb@204: bool is_literal_integer_type(symbol_c *type_symbol); ccb@204: bool is_literal_real_type(symbol_c *type_symbol); ccb@204: bool is_literal_bool_type(symbol_c *type_symbol); ccb@204: ccb@204: /* Determine the common data type between two data types. ccb@204: * If no common data type found, return NULL. ccb@204: * ccb@204: * If data types are identical, return the first (any would do...). ccb@204: * If any of the datat types is a literal, we confirm that ccb@204: * the literal uses less bits than the fixed size data type. ccb@204: * e.g. BYTE and 1024 returns NULL ccb@204: * BYTE and 255 returns BYTE ccb@204: * ccb@204: * If two literals, then return the literal that requires more bits... ccb@204: */ ccb@204: symbol_c *common_type__(symbol_c *first_type, symbol_c *second_type); ccb@204: /* Determine the common data type between two data types. ccb@204: * Unlike the common_type__() function, we stop the compiler with an ERROR ccb@204: * if no common data type is found. ccb@204: */ ccb@204: symbol_c *common_type(symbol_c *first_type, symbol_c *second_type); ccb@204: /* Return TRUE if there is a common data type, otherwise return FALSE */ ccb@204: bool is_compatible_type(symbol_c *first_type, symbol_c *second_type); ccb@204: ccb@204: void compute_input_operatores(symbol_c *symbol, const char *input_operator); ccb@204: void check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl); ccb@204: ccb@204: /* check the semantics of a FB or Function non-formal call */ ccb@204: /* e.g. foo(1, 2, 3, 4); */ ccb@204: void check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar = false); ccb@204: /* check the semantics of a FB or Function formal call */ ccb@204: /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ ccb@204: void check_formal_call(symbol_c *f_call, symbol_c *f_decl); ccb@204: ccb@204: ccb@204: void *compute_standard_function_default(function_invocation_c *st_symbol, il_formal_funct_call_c *il_symbol); ccb@204: void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type); ccb@204: ccb@204: ccb@204: /* A helper function... */ ccb@204: typedef bool (visit_expression_type_c::*is_data_type_t)(symbol_c *type_symbol); /* a pointer to a function! */ ccb@204: symbol_c *compute_boolean_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); ccb@204: symbol_c *compute_numeric_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); ccb@204: ccb@204: /* a helper function... */ ccb@204: symbol_c *base_type(symbol_c *symbol); ccb@204: ccb@204: /* a helper function... */ ccb@204: void *verify_null(symbol_c *symbol); ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: /*********************/ ccb@204: /* B 1.4 - Variables */ ccb@204: /*********************/ ccb@204: void *visit(symbolic_variable_c *symbol); ccb@204: ccb@204: /********************************************/ ccb@204: /* B 1.4.1 - Directly Represented Variables */ ccb@204: /********************************************/ ccb@204: void *visit(direct_variable_c *symbol); ccb@204: ccb@204: /*************************************/ ccb@204: /* B 1.4.2 - Multi-element variables */ ccb@204: /*************************************/ ccb@204: ccb@204: void *visit(array_variable_c *symbol); ccb@204: void *visit(structured_variable_c *symbol); ccb@204: ccb@204: /****************************************/ ccb@204: /* B.2 - Language IL (Instruction List) */ ccb@204: /****************************************/ ccb@204: /***********************************/ ccb@204: /* B 2.1 Instructions and Operands */ ccb@204: /***********************************/ ccb@204: // void *visit(instruction_list_c *symbol); ccb@204: void *visit(il_simple_operation_c *symbol); ccb@204: void *visit(il_function_call_c *symbol); ccb@204: void *visit(il_expression_c *symbol); ccb@204: // void *visit(il_jump_operation_c *symbol); ccb@204: void *visit(il_fb_call_c *symbol); ccb@204: void *visit(il_formal_funct_call_c *symbol); ccb@204: /* ccb@204: void *visit(il_operand_list_c *symbol); ccb@204: void *visit(simple_instr_list_c *symbol); ccb@204: void *visit(il_param_list_c *symbol); ccb@204: void *visit(il_param_assignment_c *symbol); ccb@204: void *visit(il_param_out_assignment_c *symbol); ccb@204: */ ccb@204: ccb@204: /*******************/ ccb@204: /* B 2.2 Operators */ ccb@204: /*******************/ ccb@204: void *visit(LD_operator_c *symbol); ccb@204: void *visit(LDN_operator_c *symbol); ccb@204: void *visit(ST_operator_c *symbol); ccb@204: void *visit(STN_operator_c *symbol); ccb@204: void *visit(NOT_operator_c *symbol); ccb@204: void *visit(S_operator_c *symbol); ccb@204: void *visit(R_operator_c *symbol); ccb@204: void *visit(S1_operator_c *symbol); ccb@204: void *visit(R1_operator_c *symbol); ccb@204: void *visit(CLK_operator_c *symbol); ccb@204: void *visit(CU_operator_c *symbol); ccb@204: void *visit(CD_operator_c *symbol); ccb@204: void *visit(PV_operator_c *symbol); ccb@204: void *visit(IN_operator_c *symbol); ccb@204: void *visit(PT_operator_c *symbol); ccb@204: void *visit(AND_operator_c *symbol); ccb@204: void *visit(OR_operator_c *symbol); ccb@204: void *visit(XOR_operator_c *symbol); ccb@204: void *visit(ANDN_operator_c *symbol); ccb@204: void *visit(ORN_operator_c *symbol); ccb@204: void *visit(XORN_operator_c *symbol); ccb@204: void *visit(ADD_operator_c *symbol); ccb@204: void *visit(SUB_operator_c *symbol); ccb@204: void *visit(MUL_operator_c *symbol); ccb@204: void *visit(DIV_operator_c *symbol); ccb@204: void *visit(MOD_operator_c *symbol); ccb@204: void *visit(GT_operator_c *symbol); ccb@204: void *visit(GE_operator_c *symbol); ccb@204: void *visit(EQ_operator_c *symbol); ccb@204: void *visit(LT_operator_c *symbol); ccb@204: void *visit(LE_operator_c *symbol); ccb@204: void *visit(NE_operator_c *symbol); ccb@204: void *visit(CAL_operator_c *symbol); ccb@204: void *visit(CALC_operator_c *symbol); ccb@204: void *visit(CALCN_operator_c *symbol); ccb@204: void *visit(RET_operator_c *symbol); ccb@204: void *visit(RETC_operator_c *symbol); ccb@204: void *visit(RETCN_operator_c *symbol); ccb@204: void *visit(JMP_operator_c *symbol); ccb@204: void *visit(JMPC_operator_c *symbol); ccb@204: void *visit(JMPCN_operator_c *symbol); ccb@204: /* Symbol class handled together with function call checks */ ccb@204: // void *visit(il_assign_operator_c *symbol, variable_name); ccb@204: /* Symbol class handled together with function call checks */ ccb@204: // void *visit(il_assign_operator_c *symbol, option, variable_name); ccb@204: ccb@204: ccb@204: ccb@204: /***************************************/ ccb@204: /* B.3 - Language ST (Structured Text) */ ccb@204: /***************************************/ ccb@204: /***********************/ ccb@204: /* B 3.1 - Expressions */ ccb@204: /***********************/ ccb@204: void *visit(or_expression_c *symbol); ccb@204: void *visit(xor_expression_c *symbol); ccb@204: void *visit(and_expression_c *symbol); ccb@204: void *visit(equ_expression_c *symbol); ccb@204: void *visit(notequ_expression_c *symbol); ccb@204: void *visit(lt_expression_c *symbol); ccb@204: void *visit(gt_expression_c *symbol); ccb@204: void *visit(le_expression_c *symbol); ccb@204: void *visit(ge_expression_c *symbol); ccb@204: void *visit(add_expression_c *symbol); ccb@204: void *visit(sub_expression_c *symbol); ccb@204: void *visit(mul_expression_c *symbol); ccb@204: void *visit(div_expression_c *symbol); ccb@204: void *visit(mod_expression_c *symbol); ccb@204: void *visit(power_expression_c *symbol); ccb@204: void *visit(neg_expression_c *symbol); ccb@204: void *visit(not_expression_c *symbol); ccb@204: void *visit(function_invocation_c *symbol); ccb@204: ccb@204: /*********************************/ ccb@204: /* B 3.2.1 Assignment Statements */ ccb@204: /*********************************/ ccb@204: void *visit(assignment_statement_c *symbol); ccb@204: ccb@204: /*****************************************/ ccb@204: /* B 3.2.2 Subprogram Control Statements */ ccb@204: /*****************************************/ ccb@204: void *visit(fb_invocation_c *symbol); ccb@204: ccb@204: /********************************/ ccb@204: /* B 3.2.3 Selection Statements */ ccb@204: /********************************/ ccb@204: ccb@204: void *visit(if_statement_c *symbol); ccb@204: // void *visit(elseif_statement_list_c *symbol); ccb@204: void *visit(elseif_statement_c *symbol); ccb@204: void *visit(case_statement_c *symbol); ccb@204: // void *visit(case_element_list_c *symbol); ccb@204: // void *visit(case_element_c *symbol); ccb@204: void *visit(case_list_c *symbol); ccb@204: ccb@204: /********************************/ ccb@204: /* B 3.2.4 Iteration Statements */ ccb@204: /********************************/ ccb@204: ccb@204: void *visit(for_statement_c *symbol); ccb@204: void *visit(while_statement_c *symbol); ccb@204: void *visit(repeat_statement_c *symbol); ccb@204: ccb@204: ccb@204: //TODO: delete this functions. Why are they needed? ccb@204: void *visit(program_declaration_c *symbol); ccb@204: void *visit(function_declaration_c *symbol); ccb@204: void *visit(function_block_declaration_c *symbol); ccb@204: ccb@204: }; // visit_expression_type_c ccb@204: