absyntax_utils/get_var_name.hh
author Edouard Tisserant
Wed, 24 Nov 2021 08:58:58 +0100
changeset 1095 36bbd0b64816
parent 889 5f380b99e95e
permissions -rw-r--r--
Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
511
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
/*
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
 *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
 *  This program is free software: you can redistribute it and/or modify
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
 *  it under the terms of the GNU General Public License as published by
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
 *  the Free Software Foundation, either version 3 of the License, or
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
 *  (at your option) any later version.
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    11
 *  This program is distributed in the hope that it will be useful,
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
 *  GNU General Public License for more details.
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
 *  You should have received a copy of the GNU General Public License
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
 * This code is made available on the understanding that it will not be
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    21
 * used in safety-critical situations without a full and competent review.
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
 */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
/*
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
 * An IEC 61131-3 compiler.
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    26
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    27
 * Based on the
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    28
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    29
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    30
 */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    32
/*
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    33
 *  A small helper visitor class, that will   
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
 *  return the name (tokn_c *) of a variable, as it will
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    35
 *  appear in the variable declaration.
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    36
 */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    37
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    38
/*  For ex.:
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    39
 *       VAR
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    40
 *          A : int;
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    41
 *          B : ARRAY [1..9] of int;
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    42
 *          C : some_struct_t;
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    43
 *       END_VAR
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    44
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    45
 *          A    := 56;
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
 *          B[8] := 99;
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
 *          C.e  := 77;
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    49
 *       Calling this visitor class with symbolic_variable_c instance referencing 'A' in
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
 *       the line 'A := 56', will return the string "A".
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    52
 *       Calling this visitor class with array_variable_c instance referencing 'B[8]' in
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
 *       the line 'B[8] := 99', will return the string "B".
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
 *
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
 *       Calling this visitor class with array_variable_c instance referencing 'C.e' in
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
 *       the line 'C.e := 77', will return the string "C".
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
 */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    59
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    60
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    62
class get_var_name_c : public search_visitor_c {    
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
  public:
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    64
    get_var_name_c(void)  {};
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    65
    ~get_var_name_c(void) {};  
889
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 511
diff changeset
    66
    static token_c  *get_name(symbol_c *symbol);
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 511
diff changeset
    67
    static symbol_c *get_last_field(symbol_c *symbol);
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 511
diff changeset
    68
    
511
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
  private:
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    70
    static get_var_name_c *singleton_instance_;
889
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 511
diff changeset
    71
    symbol_c *last_field;
511
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    72
    
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    73
  private:  
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    74
    /*************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    75
    /* B.1 - Common elements */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
    /*************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
    /*******************************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
    /* B 1.1 - Letters, digits and identifiers */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
    /*******************************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
    // SYM_TOKEN(identifier_c)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
    void *visit(identifier_c *symbol);
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    82
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    83
    /*********************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
    /* B 1.4 - Variables */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
    /*********************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
    // SYM_REF2(symbolic_variable_c, var_name, unused)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    87
    void *visit(symbolic_variable_c *symbol);
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
    /********************************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    90
    /* B.1.4.1   Directly Represented Variables */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    91
    /********************************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    92
    // SYM_TOKEN(direct_variable_c)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    93
    // void *visit(direct_variable_c *symbol);
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    94
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    95
    /*************************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
    /* B.1.4.2   Multi-element Variables */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    97
    /*************************************/
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    98
    /*  subscripted_variable '[' subscript_list ']' */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    99
    // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   100
    void *visit(array_variable_c *symbol);
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   101
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   102
    /* subscript_list ',' subscript */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   103
    // SYM_LIST(subscript_list_c)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   104
    // void *visit(subscript_list_c *symbol);
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   105
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   106
    /*  record_variable '.' field_selector */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   107
    /*  WARNING: input and/or output variables of function blocks
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   108
     *           may be accessed as fields of a tructured variable!
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   109
     *           Code handling a structured_variable_c must take
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   110
     *           this into account!
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   111
     */
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   112
    // SYM_REF2(structured_variable_c, record_variable, field_selector)
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   113
    void *visit(structured_variable_c *symbol);
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   114
};
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   115
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   116
b22ae67d8003 Moving get_var_name_c into its own file.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   117