stage3/print_datatypes_error.hh
changeset 625 c0bda77b37a0
parent 552 3c39d80fdede
child 718 a9f8cc778444
equal deleted inserted replaced
412:aad38592bdde 625:c0bda77b37a0
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2009-2011  Mario de Sousa (msousa@fe.up.pt)
       
     5  *  Copyright (C) 2011-2012  Manuele Conti (manuele.conti@sirius-es.it)
       
     6  *  Copyright (C) 2011-2012  Matteo Facchinetti (matteo.facchinetti@sirius-es.it)
       
     7  *
       
     8  *
       
     9  *  This program is free software: you can redistribute it and/or modify
       
    10  *  it under the terms of the GNU General Public License as published by
       
    11  *  the Free Software Foundation, either version 3 of the License, or
       
    12  *  (at your option) any later version.
       
    13  *
       
    14  *  This program is distributed in the hope that it will be useful,
       
    15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  *  GNU General Public License for more details.
       
    18  *
       
    19  *  You should have received a copy of the GNU General Public License
       
    20  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    21  *
       
    22  *
       
    23  * This code is made available on the understanding that it will not be
       
    24  * used in safety-critical situations without a full and competent review.
       
    25  */
       
    26 
       
    27 /*
       
    28  * An IEC 61131-3 compiler.
       
    29  *
       
    30  * Based on the
       
    31  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    32  *
       
    33  */
       
    34 
       
    35 /* NOTE: The algorithm implemented here assumes that the symbol_c.candidate_datatype, and the symbol_c.datatype 
       
    36  *       annotations have already been apropriately filled in!
       
    37  *       BEFORE running this visitor, be sure to CALL the fill_candidate_datatypes_c, and the narrow_candidate_datatypes_c visitors!
       
    38  */
       
    39 
       
    40 
       
    41 /*
       
    42  *  By analysing the candidate datatype lists, as well as the chosen datatype for each expression, determine
       
    43  *  if an datatype error has been found, and if so, print out an error message.
       
    44  */
       
    45 
       
    46 
       
    47 #include "../absyntax_utils/absyntax_utils.hh"
       
    48 #include "datatype_functions.hh"
       
    49 
       
    50 
       
    51 class print_datatypes_error_c: public iterator_visitor_c {
       
    52 
       
    53   private:
       
    54     /* The level of detail that the user wants us to display error messages. */
       
    55 //     #define error_level_default (1)
       
    56     #define error_level_default (1)
       
    57     #define error_level_nagging (4)
       
    58     unsigned int current_display_error_level;
       
    59     
       
    60     search_varfb_instance_type_c *search_varfb_instance_type;
       
    61     search_base_type_c search_base_type;
       
    62     /* When calling a function block, we must first find it's type,
       
    63      * by searching through the declarations of the variables currently
       
    64      * in scope.
       
    65      * This class does just that...
       
    66      * A new object instance is instantiated whenever we start checking semantics
       
    67      * for a function block type declaration, or a program declaration.
       
    68      * This object instance will then later be called while the
       
    69      * function block's or the program's body is being handled.
       
    70      *
       
    71      * Note that functions cannot contain calls to function blocks,
       
    72      * so we do not create an object instance when handling
       
    73      * a function declaration.
       
    74      */
       
    75 
       
    76     /* In IL code, once we find a type mismatch error, it is best to
       
    77      * ignore any further errors until the end of the logical operation,
       
    78      * i.e. until the next LD.
       
    79      * However, we cannot clear the il_error flag on all LD operations,
       
    80      * as these may also be used within parenthesis. LD operations
       
    81      * within parenthesis may not clear the error flag.
       
    82      * We therefore need a counter to know how deep inside a parenthesis
       
    83      * structure we are.
       
    84      */
       
    85     int  il_parenthesis_level;
       
    86     bool il_error;
       
    87     int  error_count;
       
    88     bool warning_found;
       
    89 
       
    90     /* the current data type of the data stored in the IL stack, i.e. the default variable */
       
    91     il_instruction_c *fake_prev_il_instruction;
       
    92     /* the narrow algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the 
       
    93      * list of the prev_il_instructions.
       
    94      * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction),
       
    95      * and shove that data into this single variable.
       
    96      */
       
    97     symbol_c *il_operand_type;
       
    98     symbol_c *il_operand;
       
    99 
       
   100     /* some helper functions... */
       
   101     symbol_c *base_type(symbol_c *symbol);
       
   102     void handle_function_invocation(symbol_c *fcall, generic_function_call_t fcall_data);
       
   103     void *handle_implicit_il_fb_invocation(const char *param_name, symbol_c *il_operator, symbol_c *called_fb_declaration);  
       
   104     void *handle_conditional_flow_control_IL_instruction(symbol_c *symbol, const char *oper);
       
   105 
       
   106     void *print_binary_operator_errors  (const char *il_operator, symbol_c *symbol,                                     bool deprecated_operation = false);
       
   107     void *print_binary_expression_errors(const char *operation  , symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool deprecated_operation = false);
       
   108 
       
   109     
       
   110   public:
       
   111     print_datatypes_error_c(symbol_c *ignore);
       
   112     virtual ~print_datatypes_error_c(void);
       
   113     int get_error_count();
       
   114 
       
   115 
       
   116     /*********************/
       
   117     /* B 1.2 - Constants */
       
   118     /*********************/
       
   119     /******************************/
       
   120     /* B 1.2.1 - Numeric Literals */
       
   121     /******************************/
       
   122     void *visit(real_c *symbol);
       
   123     void *visit(integer_c *symbol);
       
   124     void *visit(neg_real_c *symbol);
       
   125     void *visit(neg_integer_c *symbol);
       
   126     void *visit(binary_integer_c *symbol);
       
   127     void *visit(octal_integer_c *symbol);
       
   128     void *visit(hex_integer_c *symbol);
       
   129     void *visit(integer_literal_c *symbol);
       
   130     void *visit(real_literal_c *symbol);
       
   131     void *visit(bit_string_literal_c *symbol);
       
   132     void *visit(boolean_literal_c *symbol);
       
   133     void *visit(boolean_true_c *symbol);
       
   134     void *visit(boolean_false_c *symbol);
       
   135 
       
   136     /*******************************/
       
   137     /* B.1.2.2   Character Strings */
       
   138     /*******************************/
       
   139     void *visit(double_byte_character_string_c *symbol);
       
   140     void *visit(single_byte_character_string_c *symbol);
       
   141 
       
   142     /***************************/
       
   143     /* B 1.2.3 - Time Literals */
       
   144     /***************************/
       
   145     /************************/
       
   146     /* B 1.2.3.1 - Duration */
       
   147     /************************/
       
   148     void *visit(duration_c *symbol);
       
   149 
       
   150     /************************************/
       
   151     /* B 1.2.3.2 - Time of day and Date */
       
   152     /************************************/
       
   153     void *visit(time_of_day_c *symbol);
       
   154     void *visit(date_c *symbol);
       
   155     void *visit(date_and_time_c *symbol);
       
   156 
       
   157     /**********************/
       
   158     /* B 1.3 - Data types */
       
   159     /**********************/
       
   160     /********************************/
       
   161     /* B 1.3.3 - Derived data types */
       
   162     /********************************/
       
   163     void *visit(simple_spec_init_c *symbol);
       
   164     void *visit(data_type_declaration_c *symbol);
       
   165     void *visit(enumerated_value_c *symbol);
       
   166 
       
   167     /*********************/
       
   168     /* B 1.4 - Variables */
       
   169     /*********************/
       
   170     void *visit(symbolic_variable_c *symbol);
       
   171 
       
   172     /********************************************/
       
   173     /* B 1.4.1 - Directly Represented Variables */
       
   174     /********************************************/
       
   175     void *visit(direct_variable_c *symbol);
       
   176 
       
   177     /*************************************/
       
   178     /* B 1.4.2 - Multi-element variables */
       
   179     /*************************************/
       
   180     void *visit(array_variable_c *symbol);
       
   181     void *visit(subscript_list_c *symbol);
       
   182     void *visit(structured_variable_c *symbol);
       
   183 
       
   184     /******************************************/
       
   185     /* B 1.4.3 - Declaration & Initialisation */
       
   186     /******************************************/
       
   187     void *visit(location_c *symbol);
       
   188     void *visit(located_var_decl_c *symbol);
       
   189 
       
   190     /**************************************/
       
   191     /* B 1.5 - Program organization units */
       
   192     /**************************************/
       
   193     /***********************/
       
   194     /* B 1.5.1 - Functions */
       
   195     /***********************/
       
   196     void *visit(function_declaration_c *symbol);
       
   197 
       
   198     /*****************************/
       
   199     /* B 1.5.2 - Function blocks */
       
   200     /*****************************/
       
   201     void *visit(function_block_declaration_c *symbol);
       
   202 
       
   203     /**********************/
       
   204     /* B 1.5.3 - Programs */
       
   205     /**********************/
       
   206     void *visit(program_declaration_c *symbol);
       
   207 
       
   208     /********************************/
       
   209     /* B 1.7 Configuration elements */
       
   210     /********************************/
       
   211     void *visit(configuration_declaration_c *symbol);
       
   212 
       
   213     /****************************************/
       
   214     /* B.2 - Language IL (Instruction List) */
       
   215     /****************************************/
       
   216     /***********************************/
       
   217     /* B 2.1 Instructions and Operands */
       
   218     /***********************************/
       
   219 //  void *visit(instruction_list_c *symbol);
       
   220     void *visit(il_instruction_c *symbol);
       
   221     void *visit(il_simple_operation_c *symbol);
       
   222     void *visit(il_function_call_c *symbol);
       
   223     void *visit(il_expression_c *symbol);
       
   224     void *visit(il_fb_call_c *symbol);
       
   225     void *visit(il_formal_funct_call_c *symbol);
       
   226 //  void *visit(il_operand_list_c *symbol);
       
   227 //  void *visit(simple_instr_list_c *symbol);
       
   228     void *visit(il_simple_instruction_c*symbol);
       
   229 //  void *visit(il_param_list_c *symbol);
       
   230 //  void *visit(il_param_assignment_c *symbol);
       
   231 //  void *visit(il_param_out_assignment_c *symbol);
       
   232 
       
   233     /*******************/
       
   234     /* B 2.2 Operators */
       
   235     /*******************/
       
   236     void *visit(LD_operator_c *symbol);
       
   237     void *visit(LDN_operator_c *symbol);
       
   238     void *visit(ST_operator_c *symbol);
       
   239     void *visit(STN_operator_c *symbol);
       
   240     void *visit(NOT_operator_c *symbol);
       
   241     void *visit(S_operator_c *symbol);
       
   242     void *visit(R_operator_c *symbol);
       
   243     void *visit(S1_operator_c *symbol);
       
   244     void *visit(R1_operator_c *symbol);
       
   245     void *visit(CLK_operator_c *symbol);
       
   246     void *visit(CU_operator_c *symbol);
       
   247     void *visit(CD_operator_c *symbol);
       
   248     void *visit(PV_operator_c *symbol);
       
   249     void *visit(IN_operator_c *symbol);
       
   250     void *visit(PT_operator_c *symbol);
       
   251     void *visit(AND_operator_c *symbol);
       
   252     void *visit(OR_operator_c *symbol);
       
   253     void *visit(XOR_operator_c *symbol);
       
   254     void *visit(ANDN_operator_c *symbol);
       
   255     void *visit(ORN_operator_c *symbol);
       
   256     void *visit(XORN_operator_c *symbol);
       
   257     void *visit(ADD_operator_c *symbol);
       
   258     void *visit(SUB_operator_c *symbol);
       
   259     void *visit(MUL_operator_c *symbol);
       
   260     void *visit(DIV_operator_c *symbol);
       
   261     void *visit(MOD_operator_c *symbol);
       
   262     void *visit(GT_operator_c *symbol);
       
   263     void *visit(GE_operator_c *symbol);
       
   264     void *visit(EQ_operator_c *symbol);
       
   265     void *visit(LT_operator_c *symbol);
       
   266     void *visit(LE_operator_c *symbol);
       
   267     void *visit(NE_operator_c *symbol);
       
   268     void *visit(CAL_operator_c *symbol);
       
   269     void *visit(CALC_operator_c *symbol);
       
   270     void *visit(CALCN_operator_c *symbol);
       
   271     void *visit(RET_operator_c *symbol);
       
   272     void *visit(RETC_operator_c *symbol);
       
   273     void *visit(RETCN_operator_c *symbol);
       
   274     void *visit(JMP_operator_c *symbol);
       
   275     void *visit(JMPC_operator_c *symbol);
       
   276     void *visit(JMPCN_operator_c *symbol);
       
   277     /* Symbol class handled together with function call checks */
       
   278     // void *visit(il_assign_operator_c *symbol, variable_name);
       
   279     /* Symbol class handled together with function call checks */
       
   280     // void *visit(il_assign_operator_c *symbol, option, variable_name);
       
   281 
       
   282     /***************************************/
       
   283     /* B.3 - Language ST (Structured Text) */
       
   284     /***************************************/
       
   285     /***********************/
       
   286     /* B 3.1 - Expressions */
       
   287     /***********************/
       
   288     void *visit(or_expression_c *symbol);
       
   289     void *visit(xor_expression_c *symbol);
       
   290     void *visit(and_expression_c *symbol);
       
   291     void *visit(equ_expression_c *symbol);
       
   292     void *visit(notequ_expression_c *symbol);
       
   293     void *visit(lt_expression_c *symbol);
       
   294     void *visit(gt_expression_c *symbol);
       
   295     void *visit(le_expression_c *symbol);
       
   296     void *visit(ge_expression_c *symbol);
       
   297     void *visit(add_expression_c *symbol);
       
   298     void *visit(sub_expression_c *symbol);
       
   299     void *visit(mul_expression_c *symbol);
       
   300     void *visit(div_expression_c *symbol);
       
   301     void *visit(mod_expression_c *symbol);
       
   302     void *visit(power_expression_c *symbol);
       
   303     void *visit(neg_expression_c *symbol);
       
   304     void *visit(not_expression_c *symbol);
       
   305     void *visit(function_invocation_c *symbol);
       
   306 
       
   307     /*********************************/
       
   308     /* B 3.2.1 Assignment Statements */
       
   309     /*********************************/
       
   310     void *visit(assignment_statement_c *symbol);
       
   311 
       
   312     /*****************************************/
       
   313     /* B 3.2.2 Subprogram Control Statements */
       
   314     /*****************************************/
       
   315     void *visit(fb_invocation_c *symbol);
       
   316 
       
   317     /********************************/
       
   318     /* B 3.2.3 Selection Statements */
       
   319     /********************************/
       
   320     void *visit(if_statement_c *symbol);
       
   321     //     void *visit(elseif_statement_list_c *symbol);
       
   322     void *visit(elseif_statement_c *symbol);
       
   323     void *visit(case_statement_c *symbol);
       
   324     //     void *visit(case_element_list_c *symbol);
       
   325     //     void *visit(case_element_c *symbol);
       
   326     // void *visit(case_list_c *symbol);
       
   327 
       
   328     /********************************/
       
   329     /* B 3.2.4 Iteration Statements */
       
   330     /********************************/
       
   331     void *visit(for_statement_c *symbol);
       
   332     void *visit(while_statement_c *symbol);
       
   333     void *visit(repeat_statement_c *symbol);
       
   334 
       
   335 }; // print_datatypes_error_c
       
   336 
       
   337 
       
   338 
       
   339 
       
   340 
       
   341 
       
   342