stage4/generate_c/generate_c_vardecl.cc
author Laurent Bessard
Thu, 15 Nov 2012 22:28:53 +0100
changeset 706 31553c22f318
parent 635 a20a70021d37
child 793 268bf4ca5fa1
permissions -rw-r--r--
Adding support for declaring function block instances as global and external variables
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
     1
/*
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
     3
 *
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
     6
 *
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    10
 *  (at your option) any later version.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    11
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    15
 *  GNU General Public License for more details.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    16
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 242
diff changeset
    19
 *
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    20
 *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    23
 */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    24
592
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
    25
#include <limits>  // required for std::numeric_limits<XXX>
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
    26
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    27
class initialization_analyzer_c: public null_visitor_c {
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    28
  public:
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    29
    typedef enum {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    30
      simple_it,
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    31
      array_it,
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    32
      struct_it
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    33
    } initialization_type_t;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    34
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    35
  private:
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    36
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    37
    initialization_type_t current_type;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    38
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    39
  public:
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    40
    initialization_analyzer_c(symbol_c* symbol) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    41
      current_type = simple_it;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    42
      symbol->accept(*this);
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    43
    }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    44
    ~initialization_analyzer_c(void) {}
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    45
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    46
    initialization_type_t get_initialization_type(void) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    47
      return current_type;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    48
    }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    49
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    50
    void *visit(array_initial_elements_list_c *symbol) {
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    51
      current_type = array_it;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    52
      return NULL;
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
    53
    }
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    54
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    55
    void *visit(structure_element_initialization_list_c *symbol) {
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    56
      current_type = struct_it;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    57
      return NULL;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    58
    }
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
    59
};
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    60
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    61
class generate_c_array_initialization_c: public generate_c_typedecl_c {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    62
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    63
  public:
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    64
    typedef enum {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    65
      none_am,
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
    66
      arraysize_am,
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
    67
      typedecl_am,
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
    68
      initializationvalue_am
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    69
    } arrayinitialization_mode_t;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    70
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    71
    arrayinitialization_mode_t current_mode;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    72
    
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    73
    symbol_c* array_base_type;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    74
    symbol_c* array_default_value;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    75
    symbol_c* array_default_initialization;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    76
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    77
  private:
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    78
    int current_dimension;
592
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
    79
    unsigned long long int array_size;
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
    80
    unsigned long long int defined_values_count;
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
    81
    unsigned long long int current_initialization_count;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    82
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    83
  public:
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    84
    generate_c_array_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    85
    ~generate_c_array_initialization_c(void) {}
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    86
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
    87
    void init_array_size(symbol_c *array_specification) {
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    88
      array_size = 1;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    89
      defined_values_count = 0;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    90
      current_initialization_count = 0;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    91
      array_base_type = array_default_value = array_default_initialization = NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    92
      
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
    93
      current_mode = arraysize_am;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    94
      array_specification->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    95
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
    96
635
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
    97
    void set_array_default_initialisation(symbol_c *array_initialization) {
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
    98
      array_default_initialization = array_initialization;
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
    99
    }
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   100
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   101
    void init_array(symbol_c *var1_list, symbol_c *array_specification, symbol_c *array_initialization) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   102
      int i;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   103
      
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   104
      init_array_size(array_specification);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   105
      
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   106
      s4o.print("\n");
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   107
      s4o.print(s4o.indent_spaces + "{\n");
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   108
      s4o.indent_right();
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   109
      s4o.print(s4o.indent_spaces);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   110
      s4o.print("static const ");
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   111
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   112
      current_mode = typedecl_am;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   113
      array_specification->accept(*this);
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   114
      s4o.print(" temp = ");
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   115
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   116
      init_array_values(array_initialization);
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   117
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   118
      s4o.print(";\n");
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   119
      var1_list->accept(*this);
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   120
      s4o.indent_left();
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   121
      s4o.print(s4o.indent_spaces + "}");
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   122
    }
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   123
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   124
    void init_array_values(symbol_c *array_initialization) {
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   125
      s4o.print("{{");
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   126
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   127
      current_mode = initializationvalue_am;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   128
      array_initialization->accept(*this);
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   129
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   130
      if (array_default_initialization != NULL && defined_values_count < array_size)
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   131
        array_default_initialization->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   132
      if (defined_values_count < array_size) {
592
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   133
        for (unsigned long long int i = defined_values_count; i < array_size; i++) {
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   134
          if (defined_values_count > 0)
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   135
            s4o.print(",");
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   136
          array_default_value->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   137
          defined_values_count++;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   138
        }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   139
      }
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   140
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   141
      s4o.print("}}");
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   142
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   143
    
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   144
    void *visit(identifier_c *type_name) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   145
      symbol_c *type_decl;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   146
      switch (current_mode) {
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   147
        case arraysize_am:
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   148
          /* look up the type declaration... */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   149
          type_decl = type_symtable.find_value(type_name);
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   150
          if (type_decl == type_symtable.end_value())
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   151
            /* Type declaration not found!! */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   152
            ERROR;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   153
          type_decl->accept(*this);
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   154
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   155
        default:
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   156
          print_token(type_name);
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   157
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   158
      }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   159
      return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   160
    }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   161
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   162
    void *visit(var1_list_c *symbol) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   163
      int i, j;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   164
      
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   165
      for (i = 0; i < symbol->n; i++) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   166
        s4o.print(s4o.indent_spaces);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   167
        s4o.print(SET_VAR);
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   168
        s4o.print("(");
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   169
        print_variable_prefix();
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
   170
        s4o.print(",");
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   171
        symbol->elements[i]->accept(*this);
334
63b52a8a12f3 Fix bug on initialization of temp variables not using setter
laurent
parents: 279
diff changeset
   172
        s4o.print(",temp);\n");
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   173
      }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   174
      return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   175
    }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   176
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   177
/********************************/
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   178
/* B 1.3.3 - Derived data types */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   179
/********************************/
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   180
    
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   181
    /* array_specification [ASSIGN array_initialization] */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   182
    /* array_initialization may be NULL ! */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   183
    void *visit(array_spec_init_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   184
      switch (current_mode) {
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   185
        case arraysize_am:
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   186
          array_default_initialization = symbol->array_initialization;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   187
          break;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   188
        default:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   189
          break;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   190
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   191
      symbol->array_specification->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   192
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   193
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   194
    
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   195
    /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   196
    void *visit(array_specification_c *symbol) {
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   197
      identifier_c* type_name;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   198
      switch (current_mode) {
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   199
        case arraysize_am:
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   200
          symbol->array_subrange_list->accept(*this);
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   201
          array_base_type = symbol->non_generic_type_name;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   202
          array_default_value = (symbol_c *)symbol->non_generic_type_name->accept(*type_initial_value_c::instance());;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   203
          if (array_default_value == NULL) ERROR;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   204
          break;
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   205
        case typedecl_am:
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   206
          s4o.print("__");
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   207
          symbol->non_generic_type_name->accept(*this);
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   208
          symbol->array_subrange_list->accept(*this);
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   209
          break;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   210
        default:
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   211
          symbol->array_subrange_list->accept(*this);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   212
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   213
      } 
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   214
      return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   215
    }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   216
    
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   217
    /*  signed_integer DOTDOT signed_integer */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   218
    //SYM_REF2(subrange_c, lower_limit, upper_limit)
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   219
    void *visit(subrange_c *symbol) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   220
      switch (current_mode) {
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   221
        case arraysize_am:
592
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   222
          /* res = a * b; --->  Check for overflow by pre-condition: If (UINT_MAX / a) < b => overflow! */
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   223
          if ((std::numeric_limits< unsigned long long int >::max() / array_size) < symbol->dimension)
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   224
            STAGE4_ERROR(symbol, symbol, "The array containing this subrange has a total number of elements larger than the maximum currently supported (%llu).", 
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   225
                         std::numeric_limits< unsigned long long int >::max());
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   226
          array_size *= symbol->dimension;
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   227
          break;
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   228
        case typedecl_am:
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 334
diff changeset
   229
          s4o.print("_");
594
c8092e909886 Clean up code (remove parsing of integers in stage 4).
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   230
          s4o.print(symbol->dimension);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   231
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   232
        default:
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   233
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   234
      }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   235
      return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   236
    }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   237
    
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   238
    /* helper symbol for array_initialization */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   239
    /* array_initial_elements_list ',' array_initial_elements */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   240
    void *visit(array_initial_elements_list_c *symbol) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   241
      switch (current_mode) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   242
        case initializationvalue_am:
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   243
          current_initialization_count = 0;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   244
          for (int i = 0; i < symbol->n; i++) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   245
            if (current_initialization_count >= defined_values_count) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   246
              if (defined_values_count >= array_size)
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   247
                ERROR;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   248
              if (defined_values_count > 0)
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   249
                s4o.print(",");
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   250
              symbol->elements[i]->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   251
              defined_values_count++;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   252
            }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   253
            else {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   254
              array_initial_elements_c *array_initial_element = dynamic_cast<array_initial_elements_c *>(symbol->elements[i]);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   255
            
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   256
              if (array_initial_element != NULL) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   257
                symbol->elements[i]->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   258
              }
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   259
            }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   260
            current_initialization_count++;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   261
          }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   262
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   263
        default:
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   264
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   265
      }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   266
      return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   267
    }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   268
    
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   269
    /* integer '(' [array_initial_element] ')' */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   270
    /* array_initial_element may be NULL ! */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   271
    void *visit(array_initial_elements_c *symbol) {
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   272
      unsigned long long int initial_element_count;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   273
 
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   274
      /* This code assumes that unsigned long long int is >= uint64_t */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   275
      if (std::numeric_limits< uint64_t >::max() > std::numeric_limits< unsigned long long int >::max()) 
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   276
        ERROR_MSG("Assertion (sizeof(uint64_t) > sizeof(unsigned long long int)) failed! Compiler cannot execute correctly on the current platform!");
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   277
      
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   278
      switch (current_mode) {
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   279
        case initializationvalue_am:
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   280
          if      (VALID_CVALUE( int64, symbol->integer) && (GET_CVALUE( int64, symbol->integer) >= 0))
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   281
            initial_element_count = GET_CVALUE( int64, symbol->integer);
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   282
          else if (VALID_CVALUE(uint64, symbol->integer))
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   283
            initial_element_count = GET_CVALUE(uint64, symbol->integer);
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   284
          else ERROR;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   285
     
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   286
          if (current_initialization_count < defined_values_count) {
592
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   287
            unsigned long long int temp_element_number = 0;
99a284cec1f2 Add 'dimension' parameter to subrange_c, fill it correctly, and use it.
Mario de Sousa <msousa@fe.up.pt>
parents: 587
diff changeset
   288
            unsigned long long int diff = defined_values_count - current_initialization_count;
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   289
            if (diff <= initial_element_count)
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   290
              temp_element_number = initial_element_count - diff;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   291
            current_initialization_count += initial_element_count - 1;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   292
            initial_element_count = temp_element_number;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   293
            if (initial_element_count > 0) {
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   294
              defined_values_count++;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   295
              s4o.print(",");
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   296
            }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   297
          }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   298
          else
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   299
            current_initialization_count += initial_element_count - 1;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   300
          if (defined_values_count + initial_element_count > array_size)
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   301
            ERROR;
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   302
          for (unsigned long long int i = 0; i < initial_element_count; i++) {
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   303
            if (i > 0)
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   304
              s4o.print(",");
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   305
            if (symbol->array_initial_element != NULL) {
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   306
              symbol->array_initial_element->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   307
            }
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   308
            else {
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   309
              array_default_value->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   310
            }
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   311
          }
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   312
          if (initial_element_count > 1)
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   313
            defined_values_count += initial_element_count - 1;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   314
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   315
        default:
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   316
          break;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   317
      }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   318
      return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   319
    }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   320
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   321
    void *visit(structure_element_initialization_list_c *symbol);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   322
};
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   323
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   324
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   325
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   326
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   327
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   328
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   329
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   330
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   331
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   332
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   333
/* given a structure_element_declaration_list_c, iterate 
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   334
 * through each element, returning the name
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   335
 * of each element...structure_element_iterator_c
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   336
 */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   337
class structure_element_iterator_c : public null_visitor_c {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   338
  
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   339
  private:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   340
    /* a pointer to the structure_element_declaration_list_c
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   341
     * currently being analysed.
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   342
     */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   343
    symbol_c *type_decl;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   344
    int next_element, element_count;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   345
    identifier_c *current_element_name;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   346
    symbol_c *current_element_type;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   347
    symbol_c *current_element_default_value;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   348
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   349
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   350
  public:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   351
    /* start off at the first parameter once again... */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   352
    void reset(void) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   353
      next_element = element_count = 0;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   354
      current_element_name = NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   355
      current_element_type = current_element_default_value = NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   356
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   357
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   358
    /* initialise the iterator object.
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   359
     * We must be given a reference to the function declaration
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   360
     * that will be analysed...
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   361
     */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   362
    structure_element_iterator_c(symbol_c *type_decl) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   363
      this->type_decl = type_decl;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   364
      reset();
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   365
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   366
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   367
    /* Skip to the next element. After object creation,
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   368
     * the object references on 
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   369
     * element _before_ the first, so
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   370
     * this function must be called once to get the object to
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   371
     * reference the first element...
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   372
     *
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   373
     * Returns the element's name!
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   374
     */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   375
    identifier_c *next(void) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   376
      void *res;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   377
      identifier_c *identifier;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   378
      element_count = 0;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   379
      next_element++;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   380
      res = type_decl->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   381
      if (res != NULL) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   382
        symbol_c *sym = (symbol_c *)res;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   383
        identifier = dynamic_cast<identifier_c *>(sym);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   384
        if (identifier == NULL)
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   385
          ERROR;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   386
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   387
      else
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   388
        return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   389
      
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   390
      current_element_name = identifier;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   391
      return current_element_name;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   392
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   393
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   394
    /* Returns the currently referenced element's default value,
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   395
     * or NULL if none is specified in the structure declaration itself.
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   396
     */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   397
    symbol_c *default_value(void) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   398
      return current_element_default_value;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   399
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   400
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   401
    /* Returns the currently referenced element's type name. */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   402
    symbol_c *element_type(void) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   403
      return current_element_type;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   404
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   405
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   406
/********************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   407
/* B 1.3.3 - Derived data types */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   408
/********************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   409
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   410
    /* helper symbol for structure_declaration */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   411
    /* structure_element_declaration_list structure_element_declaration ';' */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   412
    void *visit(structure_element_declaration_list_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   413
      void *res;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   414
      for (int i = 0; i < symbol->n; i++) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   415
        res = symbol->elements[i]->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   416
        if (res != NULL)
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   417
          return res;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   418
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   419
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   420
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   421
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   422
    /*  structure_element_name ':' *_spec_init */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   423
    void *visit(structure_element_declaration_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   424
      element_count++;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   425
      if (next_element == element_count) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   426
        current_element_default_value = spec_init_sperator_c::get_init(symbol->spec_init);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   427
        current_element_type = spec_init_sperator_c::get_spec(symbol->spec_init);
635
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   428
        if (current_element_type == NULL) ERROR;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   429
        return symbol->structure_element_name;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   430
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   431
      /* not yet the desired element... */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   432
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   433
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   434
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   435
};
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   436
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   437
/*
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   438
 * Structure init element iterator.
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   439
 * It will search through the elements of a structure initialization
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   440
 */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   441
class structure_init_element_iterator_c : public null_visitor_c {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   442
  private:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   443
    /* a pointer to the structure initialization
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   444
     * currently being analysed.
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   445
     */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   446
    symbol_c *structure_initialization;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   447
    identifier_c *search_element_name;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   448
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   449
  public:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   450
    /* initialise the iterator object.
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   451
     * We must be given a reference to the structure initialization
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   452
     * that will be analysed...
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   453
     */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   454
    structure_init_element_iterator_c(symbol_c *structure_initialization) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   455
      this->structure_initialization = structure_initialization;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   456
      search_element_name = NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   457
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   458
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   459
    /* Search for the value passed to the element named <element_name>...  */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   460
    symbol_c *search(symbol_c *element_name) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   461
      if (NULL == element_name) ERROR;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   462
      search_element_name = dynamic_cast<identifier_c *>(element_name);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   463
      if (NULL == search_element_name) ERROR;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   464
      void *res = structure_initialization->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   465
      return (symbol_c *)res;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   466
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   467
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   468
    /* helper symbol for structure_initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   469
    /* structure_element_initialization_list ',' structure_element_initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   470
    void *visit(structure_element_initialization_list_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   471
      void *res;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   472
      for (int i = 0; i < symbol->n; i++) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   473
        res = symbol->elements[i]->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   474
        if (res != NULL)
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   475
          return res;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   476
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   477
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   478
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   479
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   480
    /*  structure_element_name ASSIGN value */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   481
    void *visit(structure_element_initialization_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   482
      identifier_c *element_name = dynamic_cast<identifier_c *>(symbol->structure_element_name);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   483
      
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   484
      if (element_name == NULL) ERROR;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   485
      
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   486
      if (strcasecmp(search_element_name->value, element_name->value) == 0)
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   487
        /* FOUND! This is the same element!! */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   488
        return (void *)symbol->value;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   489
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   490
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   491
};
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   492
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   493
class generate_c_structure_initialization_c: public generate_c_typedecl_c {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   494
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   495
  public:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   496
    typedef enum {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   497
      none_sm,
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   498
      initdefault_sm,
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   499
      typedecl_sm,
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   500
      initializationvalue_sm
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   501
    } structureinitialization_mode_t;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   502
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   503
    structureinitialization_mode_t current_mode;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   504
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   505
  private:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   506
    symbol_c* structure_type_decl;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   507
    symbol_c* current_element_type;
635
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   508
    symbol_c* current_element_default_value;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   509
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   510
  public:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   511
    generate_c_structure_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   512
    ~generate_c_structure_initialization_c(void) {}
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   513
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   514
    void init_structure_default(symbol_c *structure_type_name) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   515
      structure_type_decl = NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   516
      current_element_type = NULL;
635
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   517
      current_element_default_value = NULL;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   518
      
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   519
      current_mode = initdefault_sm;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   520
      structure_type_name->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   521
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   522
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   523
    void init_structure(symbol_c *var1_list, symbol_c *structure_type_name, symbol_c *structure_initialization) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   524
      int i;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   525
      
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   526
      init_structure_default(structure_type_name);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   527
      
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   528
      s4o.print("\n");
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   529
      s4o.print(s4o.indent_spaces + "{\n");
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   530
      s4o.indent_right();
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   531
      s4o.print(s4o.indent_spaces);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   532
      s4o.print("static const ");
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   533
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   534
      current_mode = typedecl_sm;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   535
      structure_type_name->accept(*this);
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   536
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   537
      s4o.print(" temp = ");
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   538
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   539
      init_structure_values(structure_initialization);
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   540
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   541
      s4o.print(";\n");
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   542
      var1_list->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   543
      s4o.indent_left();
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   544
      s4o.print(s4o.indent_spaces + "}");
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   545
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   546
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   547
    void init_structure_values(symbol_c *structure_initialization) {
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   548
      current_mode = initializationvalue_sm;
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   549
      structure_initialization->accept(*this);
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   550
    }
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   551
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   552
    void *visit(identifier_c *type_name) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   553
      symbol_c *type_decl;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   554
      switch (current_mode) {
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   555
        case initdefault_sm:
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   556
          /* look up the type declaration... */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   557
          type_decl = type_symtable.find_value(type_name);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   558
          if (type_decl == type_symtable.end_value())
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   559
            /* Type declaration not found!! */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   560
            ERROR;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   561
          type_decl->accept(*this);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   562
          break;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   563
        default:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   564
          print_token(type_name);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   565
          break;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   566
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   567
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   568
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   569
    
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   570
    void *visit(var1_list_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   571
      int i, j;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   572
      
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   573
      for (i = 0; i < symbol->n; i++) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   574
        s4o.print(s4o.indent_spaces);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   575
        s4o.print(SET_VAR);
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   576
        s4o.print("(");
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   577
        print_variable_prefix();
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
   578
        s4o.print(",");
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   579
        symbol->elements[i]->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   580
        s4o.print(",temp);\n");
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   581
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   582
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   583
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   584
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   585
/********************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   586
/* B 1.3.3 - Derived data types */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   587
/********************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   588
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   589
    /* helper symbol for structure_declaration */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   590
    /* structure_element_declaration_list structure_element_declaration ';' */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   591
    void *visit(structure_element_declaration_list_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   592
      switch (current_mode) {
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   593
        case initdefault_sm:
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   594
          structure_type_decl = (symbol_c *)symbol;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   595
          break;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   596
        default:
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   597
          break;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   598
      } 
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   599
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   600
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   601
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   602
    /* helper symbol for structure_initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   603
    /* structure_element_initialization_list ',' structure_element_initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   604
    void *visit(structure_element_initialization_list_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   605
      s4o.print("{");
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   606
      structure_element_iterator_c structure_iterator(structure_type_decl);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   607
      
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   608
      identifier_c *element_name;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   609
      structure_init_element_iterator_c structure_init_element_iterator(symbol);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   610
      for(int i = 1; (element_name = structure_iterator.next()) != NULL; i++) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   611
        if (i > 1)
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   612
          s4o.print(",");
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   613
        
635
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   614
        current_element_type = structure_iterator.element_type();
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   615
        if (current_element_type == NULL) ERROR;
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   616
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   617
        /* Check whether default value specified in structure declaration...*/
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   618
        current_element_default_value = structure_iterator.default_value();
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   619
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   620
        /* Get the value from an initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   621
        symbol_c *element_value = structure_init_element_iterator.search(element_name);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   622
        
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   623
        if (element_value == NULL) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   624
          /* No value given for parameter, so we must use the default... */
635
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   625
          element_value = current_element_default_value;
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   626
        }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   627
        
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   628
        if (element_value == NULL) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   629
          /* If not, get the default value of this variable's type */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   630
          element_value = (symbol_c *)current_element_type->accept(*type_initial_value_c::instance());
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   631
        }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   632
        
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   633
        if (element_value == NULL) ERROR;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   634
        
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   635
        initialization_analyzer_c initialization_analyzer(element_value);
164
a0679c78143a Bug with structure of structure fixed
lbessard
parents: 160
diff changeset
   636
            
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   637
        if (initialization_analyzer.get_initialization_type() == initialization_analyzer_c::struct_it) {
164
a0679c78143a Bug with structure of structure fixed
lbessard
parents: 160
diff changeset
   638
          generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
a0679c78143a Bug with structure of structure fixed
lbessard
parents: 160
diff changeset
   639
          structure_initialization->init_structure_default(current_element_type);
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   640
          structure_initialization->init_structure_values(element_value);
164
a0679c78143a Bug with structure of structure fixed
lbessard
parents: 160
diff changeset
   641
          delete structure_initialization;
a0679c78143a Bug with structure of structure fixed
lbessard
parents: 160
diff changeset
   642
        }
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   643
        else {
164
a0679c78143a Bug with structure of structure fixed
lbessard
parents: 160
diff changeset
   644
          element_value->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   645
        }
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   646
      }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   647
      s4o.print("}");
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   648
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   649
    }
164
a0679c78143a Bug with structure of structure fixed
lbessard
parents: 160
diff changeset
   650
    
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   651
    /* helper symbol for array_initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   652
    /* array_initial_elements_list ',' array_initial_elements */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   653
    void *visit(array_initial_elements_list_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   654
      generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   655
      array_initialization->init_array_size(current_element_type);
635
a20a70021d37 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 596
diff changeset
   656
      if (current_element_default_value != NULL)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   657
        array_initialization->set_array_default_initialisation(current_element_default_value);
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   658
      array_initialization->init_array_values(symbol);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   659
      delete array_initialization;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   660
      return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   661
    }
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   662
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   663
};
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   664
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   665
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   666
/* helper symbol for array_initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   667
/* structure_element_initialization_list ',' structure_element_initialization */
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   668
void *generate_c_array_initialization_c::visit(structure_element_initialization_list_c *symbol) {
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   669
  generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   670
  structure_initialization->init_structure_default(array_base_type);
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   671
  structure_initialization->init_structure_values(symbol);
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   672
  delete structure_initialization;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   673
  return NULL;
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   674
}
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   675
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   676
/***********************************************************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   677
/***********************************************************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   678
/***********************************************************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   679
/***********************************************************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   680
/***********************************************************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   681
/***********************************************************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   682
/***********************************************************************/
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
   683
/***********************************************************************/
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   684
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   685
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   686
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   687
class generate_c_vardecl_c: protected generate_c_typedecl_c {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   688
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   689
  /* A Helper class to the main class... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   690
  /* print a string, except the first time it is called */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   691
  /* used to print the separator "," before each variable
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   692
   * declaration, except the first...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   693
   *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   694
   * Needs to be done in a seperate class because this is called
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   695
   * from within various locations within the code.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   696
   */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   697
  class next_var_c {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   698
    private:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   699
      bool print_flag;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   700
      std::string str1, str2;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   701
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   702
      next_var_c *embedded_scope;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   703
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   704
    public:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   705
      next_var_c(std::string s1, std::string s2) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   706
        str1 = s1;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   707
        str2 = s2;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   708
        print_flag = false;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   709
        embedded_scope = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   710
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   711
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   712
      std::string get(void) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   713
        if (NULL != embedded_scope)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   714
          return embedded_scope->get();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   715
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   716
        bool old_print_flag = print_flag;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   717
        print_flag = true;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   718
        if (!old_print_flag)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   719
          return str1;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   720
        else
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   721
          return str2;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   722
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   723
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   724
      /* Create a new next_var_c for an embedded scope.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   725
       * From now on, every call to get() will return the
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   726
       * inner-most scope...!
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   727
       */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   728
      void push(std::string s1, std::string s2) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   729
        if (NULL != embedded_scope)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   730
          return embedded_scope->push(s1, s2);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   731
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   732
        embedded_scope = new next_var_c(s1, s2);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   733
        if (NULL == embedded_scope)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   734
          ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   735
        return;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   736
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   737
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   738
      /* Remove the inner-most scope... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   739
      void pop(void) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   740
        if (NULL != embedded_scope)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   741
          return embedded_scope->pop();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   742
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   743
        delete embedded_scope;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   744
        embedded_scope = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   745
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   746
  };
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   747
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   748
  private:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   749
    /* used to generate the ',' separating the parameters in a function call */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   750
    next_var_c *nv;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   751
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   752
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   753
  public:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   754
    /* the types of variables that need to be processed... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   755
    static const unsigned int none_vt	  = 0x0000;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   756
    static const unsigned int input_vt	  = 0x0001;  // VAR_INPUT
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   757
    static const unsigned int output_vt	  = 0x0002;  // VAR_OUTPUT
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   758
    static const unsigned int inoutput_vt = 0x0004;  // VAR_IN_OUT
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   759
    static const unsigned int private_vt  = 0x0008;  // VAR
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   760
    static const unsigned int temp_vt	  = 0x0010;  // VAR_TEMP
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   761
    static const unsigned int external_vt = 0x0020;  // VAR_EXTERNAL
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   762
    static const unsigned int global_vt   = 0x0040;  // VAR_GLOBAL
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   763
						     //    Globals declared inside a resource will not be declared
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   764
						     //    unless global_vt is acompanied by resource_vt
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   765
    static const unsigned int located_vt  = 0x0080;  // VAR <var_name> AT <location>
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   766
    static const unsigned int program_vt  = 0x0100;  // PROGRAM (inside a configuration!)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   767
						     //    Programs declared inside a resource will not be declared
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   768
						     //    unless program_vt is acompanied by resource_vt
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   769
    static const unsigned int en_vt       = 0x0200;  // EN  declaration
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   770
    static const unsigned int eno_vt      = 0x0400;  // ENO declaration
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   771
    static const unsigned int resource_vt = 0x8000;  // RESOURCE (inside a configuration!)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   772
                                                     //    This, just of itself, will not print out any declarations!!
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   773
						     //    It must be acompanied by either program_vt and/or global_vt
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   774
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   775
    /* the qualifier of variables that need to be processed... */
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   776
    static const unsigned int none_vq        = 0x0000;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   777
    static const unsigned int constant_vq    = 0x0001;  // CONSTANT
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   778
    static const unsigned int retain_vq      = 0x0002;  // RETAIN
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   779
    static const unsigned int non_retain_vq  = 0x0004;  // NON_RETAIN
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   780
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   781
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   782
    /* How variables should be declared: as local variables or
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   783
     * variables within a function call interface.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   784
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   785
     * This will define the format of the output generated
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   786
     * by this class.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   787
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   788
     * finterface_vf: function interface parameters
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   789
     *               e.g.  f(  int a, long b, real c );
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   790
     *                         ---------------------
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   791
     *               This class/function will produce the
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   792
     *               underlined code of the above examples,
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   793
     *               and no more!!
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   794
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   795
     * localinit_vf: local declaration. Will include variable
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   796
     *           initialisation if it is not a located variable.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   797
     *           e.g.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   798
     *                int a = 9;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   799
     *                long b = 99;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   800
     *                real c = 99.9;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   801
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   802
     * local_vf: local declaration. Will NOT include variable
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   803
     *           initialisation.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   804
     *           e.g.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   805
     *                int a;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   806
     *                long b;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   807
     *                real c;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   808
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   809
     * init_vf: local initialisation without declaration.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   810
     *           e.g.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   811
     *                a = 9;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   812
     *                b = 99;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   813
     *                c = 99.9;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   814
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   815
     * constructorinit_vf: initialising of member variables...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   816
     *                e.g. for a constructor...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   817
     *                class_name_c(void)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   818
     *                : a(9), b(99), c(99.9)  { // code... }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   819
     *                  --------------------
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   820
     *               This class/function will produce the
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   821
     *               underlined code of the above examples,
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   822
     *               and no more!!
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   823
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   824
     * globalinit_vf: initialising of static c++ variables. These
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   825
     *                variables may have been declared as static inside
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   826
     *                a class, in which case the scope within which they were
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   827
     *                previously delcared must be passed as the second parameter
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   828
     *                to the print() function.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   829
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   830
     *                e.g.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   831
     *                __plc_pt_c<INT, 8*sizeof(INT)> START_P::loc = __plc_pt_c<INT, 8*sizeof(INT)>("I2");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   832
     */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   833
    typedef enum {finterface_vf,
145
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
   834
                  foutputassign_vf,
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
   835
                  local_vf,
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   836
                  localinit_vf,
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   837
                  init_vf,
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   838
                  constructorinit_vf,
396
155560bfe837 Fixing bug with external variables refering to global variables defined in configurations
laurent
parents: 392
diff changeset
   839
                  globalinit_vf,
155560bfe837 Fixing bug with external variables refering to global variables defined in configurations
laurent
parents: 392
diff changeset
   840
                  globalprototype_vf,
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   841
                 } varformat_t;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   842
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   843
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   844
  private:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   845
    /* variable used to store the types of variables that need to be processed... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   846
    /* Only set in the constructor...! */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   847
    /* Will contain a set of values of generate_c_vardecl_c::XXXX_vt */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   848
    unsigned int wanted_vartype;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   849
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   850
    /* variable used to store the type of variable currently being processed... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   851
    /* Will contain a single value of generate_c_vardecl_c::XXXX_vt */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   852
    unsigned int current_vartype;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   853
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   854
    /* variable used to store the qualifier of variable currently being processed... */
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   855
    /* Will contain a single value of generate_c_vardecl_c::XXXX_vq */
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   856
    unsigned int current_varqualifier;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   857
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   858
    /* How variables should be declared: as local variables or
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   859
     * variables within a function interface...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   860
     */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   861
    /* Only set in the constructor...! */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   862
    varformat_t wanted_varformat;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   863
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   864
    /* The number of variables already declared. */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   865
    /* Used to declare 'void' in case no variables are declared in a function interface... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   866
    int finterface_var_count;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   867
138
8b32fd4d0194 Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents: 128
diff changeset
   868
    /* Current parsed resource name, for resource 
8b32fd4d0194 Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents: 128
diff changeset
   869
     * specific global variable declaration (with #define...)*/
8b32fd4d0194 Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents: 128
diff changeset
   870
    symbol_c *resource_name;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   871
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   872
    /* Holds the references to the type and initial value
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   873
     * of the variables currently being declared.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   874
     * Please read the comment under var1_init_decl_c for further
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   875
     * details...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   876
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   877
     * We make an effort to keep these pointers pointing to NULL
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   878
     * whenever we are outside the scope of variable declaration
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   879
     * (i.e. when we are traversing a part of the parse tree which
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   880
     * is not part of variable declaration) in order tio try to catch
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   881
     * any bugs as soon as possible.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   882
     */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   883
    symbol_c *current_var_type_symbol;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   884
    symbol_c *current_var_init_symbol;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   885
    void update_type_init(symbol_c *symbol /* a spec_init_c, subrange_spec_init_c, etc... */ ) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   886
      this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   887
      this->current_var_init_symbol = spec_init_sperator_c::get_init(symbol);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   888
      if (NULL == this->current_var_type_symbol)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   889
        ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   890
      if (NULL == this->current_var_init_symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   891
        /* We try to find the data type's default value... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   892
        this->current_var_init_symbol = (symbol_c *)this->current_var_type_symbol->accept(*type_initial_value_c::instance());
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   893
      /* Note that Function Block 'data types' do not have a default value, so we cannot abort if no default value is found! */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   894
      /*
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   895
      if (NULL == this->current_var_init_symbol)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   896
        ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   897
      */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   898
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   899
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   900
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   901
    void void_type_init(void) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   902
      this->current_var_type_symbol = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   903
      this->current_var_init_symbol = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   904
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   905
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   906
    /* Only used when wanted_varformat == globalinit_vf
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   907
     * Holds a pointer to an identifier_c, which in turns contains
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   908
     * the identifier of the scope within which the static member was
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   909
     * declared.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   910
     */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   911
    symbol_c *globalnamespace;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   912
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   913
    void *print_retain(void) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   914
      s4o.print(",");
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   915
      switch (current_varqualifier) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   916
        case retain_vq:
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   917
          s4o.print("1");
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   918
          break;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   919
        case non_retain_vq:
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   920
          s4o.print("0");
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   921
          break;
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   922
        default:
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   923
          s4o.print("retain");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   924
          break;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   925
      }
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   926
      return NULL;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   927
    }
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
   928
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   929
    /* Actually produce the output where variables are declared... */
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   930
    /* Note that located variables and EN/ENO are the exception, they
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   931
     * being declared in the located_var_decl_c,
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
   932
     * en_param_declaration_c and eno_param_declaration_c visitors...
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   933
     */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   934
    void *declare_variables(symbol_c *symbol, bool is_fb = false) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   935
      list_c *list = dynamic_cast<list_c *>(symbol);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   936
      /* should NEVER EVER occur!! */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   937
      if (list == NULL) ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   938
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   939
      /* now to produce the c equivalent... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   940
      if ((wanted_varformat == local_vf) ||
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   941
          (wanted_varformat == init_vf) ||
236
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   942
          (wanted_varformat == localinit_vf)) {
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   943
        for(int i = 0; i < list->n; i++) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   944
          s4o.print(s4o.indent_spaces);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
   945
          if (wanted_varformat == local_vf) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   946
            if (!is_fb) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   947
              s4o.print(DECLARE_VAR);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   948
              s4o.print("(");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   949
            }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   950
            this->current_var_type_symbol->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   951
            if (is_fb)
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   952
              s4o.print(" ");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   953
            else
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   954
              s4o.print(",");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   955
            print_variable_prefix();
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
   956
          }
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
   957
          else if (wanted_varformat == localinit_vf) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   958
            this->current_var_type_symbol->accept(*this);
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   959
            s4o.print(" ");
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 377
diff changeset
   960
            print_variable_prefix();
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   961
          }
334
63b52a8a12f3 Fix bug on initialization of temp variables not using setter
laurent
parents: 279
diff changeset
   962
          else if (wanted_varformat == init_vf) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   963
            s4o.print(SET_VAR);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   964
            s4o.print("(");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   965
            print_variable_prefix();
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   966
            s4o.print(",");
334
63b52a8a12f3 Fix bug on initialization of temp variables not using setter
laurent
parents: 279
diff changeset
   967
          }
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   968
          list->elements[i]->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   969
          if (wanted_varformat != local_vf) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   970
            if (wanted_varformat == localinit_vf &&
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
   971
                (current_vartype & inoutput_vt) != 0) {
236
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   972
              s4o.print(";\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   973
              s4o.print(s4o.indent_spaces);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   974
              s4o.print("if (__");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   975
              list->elements[i]->accept(*this);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   976
              s4o.print(" != NULL) {\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   977
              s4o.indent_right();
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   978
              s4o.print(s4o.indent_spaces);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   979
              list->elements[i]->accept(*this);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   980
              s4o.print(" = *__");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   981
              list->elements[i]->accept(*this);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   982
              s4o.print(";\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   983
              s4o.indent_left();
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   984
              s4o.print(s4o.indent_spaces);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   985
              s4o.print("}\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   986
              s4o.print(s4o.indent_spaces);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   987
              s4o.print("else {\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   988
              s4o.indent_right();
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   989
              s4o.print(s4o.indent_spaces);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   990
              s4o.print("static const ");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   991
              this->current_var_type_symbol->accept(*this);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   992
              s4o.print(" temp = ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   993
              this->current_var_init_symbol->accept(*this);
236
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   994
              s4o.print(";\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   995
              s4o.print(s4o.indent_spaces);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   996
              list->elements[i]->accept(*this);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   997
              s4o.print(" = temp;\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   998
              s4o.indent_left();
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
   999
              s4o.print(s4o.indent_spaces);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1000
              s4o.print("}\n");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1001
            }
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1002
            else if (wanted_varformat == init_vf) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1003
              s4o.print(",");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1004
              this->current_var_init_symbol->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1005
              s4o.print(");\n");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1006
            }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1007
            else {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1008
              if (this->current_var_init_symbol != NULL) {
236
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1009
                s4o.print(" = ");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1010
                this->current_var_init_symbol->accept(*this);
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1011
              }
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1012
              s4o.print(";\n");
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1013
            }
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1014
          }
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1015
          else if (is_fb)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1016
            s4o.print(";\n");
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1017
          else
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1018
            s4o.print(")\n");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1019
        }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1020
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1021
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1022
      if (wanted_varformat == finterface_vf) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1023
        for(int i = 0; i < list->n; i++) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1024
          finterface_var_count++;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1025
          s4o.print(nv->get());
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1026
          s4o.print("\n" + s4o.indent_spaces);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1027
          this->current_var_type_symbol->accept(*this);
236
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1028
          if ((current_vartype & (output_vt | inoutput_vt)) != 0)
145
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1029
            s4o.print(" *__");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1030
          else
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1031
            s4o.print(" ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1032
          list->elements[i]->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1033
          /* We do not print the initial value at function declaration!
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1034
           * It is up to the caller to pass the correct default value
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1035
           * if none is specified in the ST source code
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1036
           */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1037
          /* if (this->current_var_init_symbol != NULL) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1038
               s4o.print(" = "); this->current_var_init_symbol->accept(*this);}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1039
           */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1040
        }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1041
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1042
145
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1043
      if (wanted_varformat == foutputassign_vf) {
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1044
        for(int i = 0; i < list->n; i++) {
236
c55b02ca2342 Using InOut variables in functions fixed
laurent
parents: 235
diff changeset
  1045
          if ((current_vartype & (output_vt | inoutput_vt)) != 0) {
145
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1046
            s4o.print(s4o.indent_spaces + "if (__");
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1047
            list->elements[i]->accept(*this);
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1048
            s4o.print(" != NULL) {\n");
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1049
            s4o.indent_right();
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1050
            s4o.print(s4o.indent_spaces + "*__");
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1051
            list->elements[i]->accept(*this);
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1052
            s4o.print(" = ");
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1053
            list->elements[i]->accept(*this);
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1054
            s4o.print(";\n");
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1055
            s4o.indent_left();
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1056
            s4o.print(s4o.indent_spaces + "}\n");
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1057
          }
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1058
        }
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1059
      }
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 138
diff changeset
  1060
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1061
      if (wanted_varformat == constructorinit_vf) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1062
        for(int i = 0; i < list->n; i++) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1063
          if (is_fb) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1064
            s4o.print(nv->get());
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1065
            this->current_var_type_symbol->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1066
            s4o.print(FB_INIT_SUFFIX);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1067
            s4o.print("(&");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1068
            this->print_variable_prefix();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1069
            list->elements[i]->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1070
            print_retain();
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1071
            s4o.print(");");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1072
          }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1073
          else if (this->current_var_init_symbol != NULL) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1074
            s4o.print(nv->get());
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1075
            s4o.print(INIT_VAR);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1076
            s4o.print("(");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1077
            this->print_variable_prefix();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1078
            list->elements[i]->accept(*this);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1079
            s4o.print(",");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1080
            this->current_var_init_symbol->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1081
            print_retain();
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1082
            s4o.print(")");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1083
          }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1084
        }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1085
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1086
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1087
      return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1088
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1089
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1090
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1091
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1092
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1093
  public:
138
8b32fd4d0194 Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents: 128
diff changeset
  1094
    generate_c_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype, symbol_c* res_name = NULL)
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1095
    : generate_c_typedecl_c(s4o_ptr) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1096
      wanted_varformat = varformat;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1097
      wanted_vartype   = vartype;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1098
      current_vartype  = none_vt;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1099
      current_varqualifier = none_vq;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1100
      current_var_type_symbol = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1101
      current_var_init_symbol = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1102
      globalnamespace         = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1103
      nv = NULL;
138
8b32fd4d0194 Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents: 128
diff changeset
  1104
      resource_name = res_name;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1105
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1106
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 152
diff changeset
  1107
    ~generate_c_vardecl_c(void) {}
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1108
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1109
    void print(symbol_c *symbol, symbol_c *scope = NULL, const char *variable_prefix = NULL) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1110
      this->set_variable_prefix(variable_prefix);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1111
      if (globalinit_vf == wanted_varformat)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1112
        globalnamespace = scope;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1113
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1114
      finterface_var_count = 0;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1115
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1116
      switch (wanted_varformat) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1117
        case constructorinit_vf: nv = new next_var_c("", "\n"+s4o.indent_spaces); break;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1118
        case finterface_vf:      /* fall through... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1119
        case localinit_vf:       /* fall through... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1120
        case local_vf:           nv = new next_var_c("", ", "); break;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1121
        default:                 nv = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1122
      } /* switch() */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1123
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1124
      symbol->accept(*this);
189
7ca02034c4f6 Bug on function without input parameters fixed
lbessard
parents: 164
diff changeset
  1125
      
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1126
      delete nv;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1127
      nv = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1128
      globalnamespace = NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1129
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1130
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1131
  protected:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1132
/***************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1133
/* B 0 - Programming Model */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1134
/***************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1135
  /* leave for derived classes... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1136
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1137
/*************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1138
/* B.1 - Common elements */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1139
/*************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1140
/*******************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1141
/* B 1.1 - Letters, digits and identifiers */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1142
/*******************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1143
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1144
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1145
/*********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1146
/* B 1.2 - Constants */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1147
/*********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1148
  /* originally empty... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1149
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1150
/******************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1151
/* B 1.2.1 - Numeric Literals */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1152
/******************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1153
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1154
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1155
/*******************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1156
/* B.1.2.2   Character Strings */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1157
/*******************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1158
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1159
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1160
/***************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1161
/* B 1.2.3 - Time Literals */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1162
/***************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1163
/************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1164
/* B 1.2.3.1 - Duration */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1165
/************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1166
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1167
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1168
/************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1169
/* B 1.2.3.2 - Time of day and Date */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1170
/************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1171
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1172
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1173
/**********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1174
/* B.1.3 - Data types */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1175
/**********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1176
/***********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1177
/* B 1.3.1 - Elementary Data Types */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1178
/***********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1179
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1180
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1181
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1182
/* B.1.3.2 - Generic data types */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1183
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1184
  /* originally empty... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1185
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1186
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1187
/* B 1.3.3 - Derived data types */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1188
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1189
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1190
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1191
/*********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1192
/* B 1.4 - Variables */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1193
/*********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1194
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1195
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1196
/********************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1197
/* B.1.4.1   Directly Represented Variables */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1198
/********************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1199
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1200
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1201
/*************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1202
/* B.1.4.2   Multi-element Variables */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1203
/*************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1204
  /* done in base class(es) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1205
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1206
/******************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1207
/* B 1.4.3 - Declaration & Initialisation */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1208
/******************************************/
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1209
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1210
void *visit(constant_option_c *symbol) {
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1211
  current_varqualifier = constant_vq;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1212
  return NULL;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1213
}
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1214
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1215
void *visit(retain_option_c *symbol) {
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1216
  current_varqualifier = retain_vq;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1217
  return NULL;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1218
}
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1219
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1220
void *visit(non_retain_option_c *symbol) {
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1221
  current_varqualifier = non_retain_vq;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1222
  return NULL;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1223
}
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1224
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1225
void *visit(input_declarations_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1226
  TRACE("input_declarations_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1227
  if ((wanted_vartype & input_vt) != 0) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1228
/*
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1229
    // TO DO ...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1230
    if (symbol->option != NULL)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1231
      symbol->option->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1232
*/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1233
    //s4o.indent_right();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1234
    current_vartype = input_vt;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1235
    if (symbol->option != NULL)
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1236
      symbol->option->accept(*this);
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1237
    symbol->input_declaration_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1238
    current_vartype = none_vt;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1239
    current_varqualifier = none_vt;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1240
    //s4o.indent_left();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1241
  }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1242
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1243
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1244
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1245
/* helper symbol for input_declarations */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1246
void *visit(input_declaration_list_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1247
  TRACE("input_declaration_list_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1248
  print_list(symbol);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1249
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1250
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1251
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1252
void *visit(edge_declaration_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1253
  TRACE("edge_declaration_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1254
  // TO DO ...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1255
  symbol->var1_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1256
  s4o.print(" : BOOL ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1257
  symbol->edge->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1258
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1259
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1260
146
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1261
void *visit(en_param_declaration_c *symbol) {
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1262
  TRACE("en_declaration_c");
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1263
  if (wanted_varformat == finterface_vf) {
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1264
    finterface_var_count++;
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1265
  }  
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1266
  if ((wanted_vartype & en_vt) != 0) {
146
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1267
    if (wanted_varformat == finterface_vf) {
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1268
      s4o.print(nv->get());
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1269
      s4o.print("\n" + s4o.indent_spaces);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1270
      symbol->type->accept(*this);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1271
      s4o.print(" ");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1272
      symbol->name->accept(*this);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1273
    }
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1274
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1275
    if ((wanted_varformat == local_vf) ||
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1276
        (wanted_varformat == init_vf) ||
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1277
        (wanted_varformat == localinit_vf)) {
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1278
      s4o.print(s4o.indent_spaces);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1279
      if (wanted_varformat == local_vf) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1280
        s4o.print(DECLARE_VAR);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1281
        s4o.print("(");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1282
        symbol->type->accept(*this);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1283
        s4o.print(",");
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1284
      }
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1285
      else if (wanted_varformat == localinit_vf) {
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1286
        symbol->type->accept(*this);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1287
        s4o.print(" ");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1288
      }
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1289
      print_variable_prefix();
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1290
      symbol->name->accept(*this);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1291
      if (wanted_varformat == local_vf)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1292
        s4o.print(")\n");
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1293
      else {
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1294
        s4o.print(" = ");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1295
        symbol->value->accept(*this);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1296
        s4o.print(";\n");
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1297
      }
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1298
    }
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1299
146
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1300
    if (wanted_varformat == constructorinit_vf) {
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1301
      /* NOTE: I (Mario) think this is dead code - never gets executed. Must confirm it before deleting it... */
146
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1302
      s4o.print(nv->get());
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1303
      s4o.print(INIT_VAR);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1304
      s4o.print("(");
146
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1305
      this->print_variable_prefix();
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1306
      // s4o.print("EN = __BOOL_LITERAL(TRUE);");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1307
      symbol->name->accept(*this);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1308
      s4o.print(",");
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1309
      symbol->value->accept(*this);
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 217
diff changeset
  1310
      print_retain();
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1311
      s4o.print(")");
146
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1312
    }
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
  1313
  }
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1314
  return NULL;
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1315
}
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1316
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1317
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1318
void *visit(eno_param_declaration_c *symbol) {
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1319
  TRACE("eno_declaration_c");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1320
  if (wanted_varformat == finterface_vf) {
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1321
    finterface_var_count++;
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1322
  }
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1323
  if ((wanted_vartype & eno_vt) != 0) {
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1324
    if (wanted_varformat == finterface_vf) {
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1325
      s4o.print(nv->get());
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1326
      // s4o.print("\n" + s4o.indent_spaces + "BOOL *ENO");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1327
      s4o.print("\n" + s4o.indent_spaces);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1328
      symbol->type->accept(*this);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1329
      s4o.print(" *__");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1330
      symbol->name->accept(*this);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1331
    }
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1332
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1333
    if ((wanted_varformat == local_vf) ||
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1334
        (wanted_varformat == init_vf) ||
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1335
        (wanted_varformat == localinit_vf)) {
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1336
      s4o.print(s4o.indent_spaces);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1337
      if (wanted_varformat == local_vf) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1338
        s4o.print(DECLARE_VAR);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1339
        s4o.print("(");
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1340
        symbol->type->accept(*this);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1341
        s4o.print(",");
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1342
      }
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1343
      else if (wanted_varformat == localinit_vf) {
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1344
        symbol->type->accept(*this);
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1345
        s4o.print(" ");
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1346
      }
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1347
      print_variable_prefix();
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1348
      symbol->name->accept(*this);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1349
      if (wanted_varformat == local_vf)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1350
        s4o.print(")\n");
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 202
diff changeset
  1351
      else
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 635
diff changeset
  1352
        s4o.print(" = __BOOL_LITERAL(TRUE);\n");
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 195
diff changeset
  1353
    }
da1a8186f86f Initial (very roug