stage3/lvalue_check.hh
author mjsousa
Sat, 07 May 2016 21:17:49 +0100
changeset 1010 242907849850
parent 845 5fa872f3d073
permissions -rw-r--r--
Correctly identify errors when parsing erroneous code (make sure flex goes back to INITIAL state when code contains errors that do not allow determining whether ST or IL is being parsed)
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     1
/*
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     3
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     4
 *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     5
 *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     6
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     7
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     8
 *  This program is free software: you can redistribute it and/or modify
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     9
 *  it under the terms of the GNU General Public License as published by
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    10
 *  the Free Software Foundation, either version 3 of the License, or
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    11
 *  (at your option) any later version.
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    12
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    13
 *  This program is distributed in the hope that it will be useful,
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    16
 *  GNU General Public License for more details.
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    17
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    18
 *  You should have received a copy of the GNU General Public License
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    20
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    21
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    22
 * This code is made available on the understanding that it will not be
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    23
 * used in safety-critical situations without a full and competent review.
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    24
 */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    25
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    26
/*
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    27
 * An IEC 61131-3 compiler.
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    28
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    29
 * Based on the
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    30
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    31
 *
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    32
 */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    33
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    34
#include <vector>
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    35
#include "../absyntax_utils/absyntax_utils.hh"
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    36
#include "datatype_functions.hh"
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    37
553
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    38
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    39
/* Expressions on the left hand side of assignment statements have aditional restrictions on their datatype.
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    40
 * For example, they cannot be literals, CONSTANT type variables, function invocations, etc...
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    41
 * This class wil do those checks.
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    42
 * 
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    43
 * Note that assignment may also be done when passing variables to OUTPUT or IN_OUT function parameters,so we check those too.
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    44
 */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    45
553
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    46
b654ca7a031a Add some comments...
mjsousa <msousa@fe.up.pt>
parents: 535
diff changeset
    47
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    48
class lvalue_check_c: public iterator_visitor_c {
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    49
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    50
  private:
509
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    51
    search_varfb_instance_type_c *search_varfb_instance_type;
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    52
    search_var_instance_decl_c *search_var_instance_decl;
510
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 509
diff changeset
    53
    int error_count;
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    54
    int current_display_error_level;
512
f915ab676d7e Fixing check for assingment to FOR control variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 510
diff changeset
    55
    std::vector <token_c *> control_variables;
528
6510ee2eaab9 Remove erroneous check for S1 and R1, and add missing declrataion.
Mario de Sousa <msousa@fe.up.pt>
parents: 527
diff changeset
    56
    symbol_c *current_il_operand;
509
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    57
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    58
    void verify_is_lvalue              (symbol_c *lvalue);
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    59
    void check_assignment_to_controlvar(symbol_c *lvalue);
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    60
    void check_assignment_to_output    (symbol_c *lvalue);
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    61
    void check_assignment_to_constant  (symbol_c *lvalue);
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    62
    void check_assignment_to_expression(symbol_c *lvalue);
845
5fa872f3d073 Add code to check if an IN_OUT variable is being passed an IL list in formal IL FB/function invocations.
mjsousa
parents: 718
diff changeset
    63
    void check_assignment_to_il_list   (symbol_c *lvalue);
5fa872f3d073 Add code to check if an IN_OUT variable is being passed an IL list in formal IL FB/function invocations.
mjsousa
parents: 718
diff changeset
    64
    
509
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    65
    void check_formal_call   (symbol_c *f_call, symbol_c *f_decl);
35d391c38a30 Fixing some bugs in lvalue checking (other bugs remain - to be fixed later)
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
    66
    void check_nonformal_call(symbol_c *f_call, symbol_c *f_decl);
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    67
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    68
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    69
  public:
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    70
    lvalue_check_c(symbol_c *ignore);
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    71
    virtual ~lvalue_check_c(void);
510
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 509
diff changeset
    72
    int get_error_count();
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    73
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    74
    /**************************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    75
    /* B 1.5 - Program organisation units */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    76
    /**************************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    77
    /***********************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    78
    /* B 1.5.1 - Functions */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    79
    /***********************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    80
    void *visit(function_declaration_c *symbol);
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    81
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    82
    /*****************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    83
    /* B 1.5.2 - Function blocks */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    84
    /*****************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    85
    void *visit(function_block_declaration_c *symbol);
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    86
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    87
    /**********************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    88
    /* B 1.5.3 - Programs */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    89
    /**********************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    90
    void *visit(program_declaration_c *symbol);
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    91
527
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    92
    /****************************************/
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    93
    /* B.2 - Language IL (Instruction List) */
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    94
    /****************************************/
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    95
    /***********************************/
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    96
    /* B 2.1 Instructions and Operands */
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    97
    /***********************************/
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    98
    void *visit(il_instruction_c *symbol);
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
    99
    void *visit(il_simple_operation_c *symbol);
535
70140bd7fe67 Add lvalue checking for IL function and FB invocations.
Mario de Sousa <msousa@fe.up.pt>
parents: 528
diff changeset
   100
    void *visit(il_function_call_c *symbol);
70140bd7fe67 Add lvalue checking for IL function and FB invocations.
Mario de Sousa <msousa@fe.up.pt>
parents: 528
diff changeset
   101
    void *visit(il_fb_call_c *symbol);
70140bd7fe67 Add lvalue checking for IL function and FB invocations.
Mario de Sousa <msousa@fe.up.pt>
parents: 528
diff changeset
   102
    void *visit(il_formal_funct_call_c *symbol);
527
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   103
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   104
    /*******************/
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   105
    /* B 2.2 Operators */
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   106
    /*******************/
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   107
    void *visit(ST_operator_c *symbol);
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   108
    void *visit(STN_operator_c *symbol);
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   109
    void *visit(S_operator_c *symbol);
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   110
    void *visit(R_operator_c *symbol);
1d83209aabfe Start implement lvalue check in IL instruction.
Manuele Conti <conti.ma@alice.it>
parents: 526
diff changeset
   111
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   112
    /***************************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   113
    /* B.3 - Language ST (Structured Text) */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   114
    /***************************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   115
    /***********************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   116
    /* B 3.1 - Expressions */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   117
    /***********************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   118
    void *visit(function_invocation_c *symbol);
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   119
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   120
    /*********************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   121
    /* B 3.2.1 Assignment Statements */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   122
    /*********************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   123
    void *visit(assignment_statement_c *symbol);
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   124
526
6e610449861a Add lvalue check on fb invocation.
Manuele Conti <conti.ma@alice.it>
parents: 512
diff changeset
   125
    /*****************************************/
6e610449861a Add lvalue check on fb invocation.
Manuele Conti <conti.ma@alice.it>
parents: 512
diff changeset
   126
    /* B 3.2.2 Subprogram Control Statements */
6e610449861a Add lvalue check on fb invocation.
Manuele Conti <conti.ma@alice.it>
parents: 512
diff changeset
   127
    /*****************************************/
6e610449861a Add lvalue check on fb invocation.
Manuele Conti <conti.ma@alice.it>
parents: 512
diff changeset
   128
    void *visit(fb_invocation_c *symbol);
6e610449861a Add lvalue check on fb invocation.
Manuele Conti <conti.ma@alice.it>
parents: 512
diff changeset
   129
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   130
    /********************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   131
    /* B 3.2.4 Iteration Statements */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   132
    /********************************/
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   133
    void *visit(for_statement_c *symbol);
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   134
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   135
}; /* lvalue_check_c */
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   136
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   137
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   138
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   139
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   140
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   141
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   142