absyntax_utils/search_varfb_instance_type.cc
author laurent
Wed, 14 Sep 2011 22:58:39 +0200
changeset 377 60b012b7793f
parent 372 25332e048742
child 382 ac6dfec701c9
permissions -rwxr-xr-x
Adding support for compiling direct array specification inside variable declaration
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     1
/*
265
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
     3
 *
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 265
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
265
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
     6
 *
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    10
 *  (at your option) any later version.
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    11
 *
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    15
 *  GNU General Public License for more details.
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    16
 *
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    19
 *
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    20
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    23
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    24
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    25
/*
265
4d222f46f8cc Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 238
diff changeset
    26
 * An IEC 61131-3 compiler.
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    27
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    28
 * Based on the
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    29
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    30
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    31
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    32
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    33
/* Determine the data type of a variable.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    34
 * The variable may be a simple variable, a function block instance, a
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    35
 * struture element within a data structured type (a struct or a fb), or
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    36
 * an array element.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    37
 * A mixture of array element of a structure element of a structure element
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    38
 * of a .... is also suported!
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    39
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    40
 *  example:
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    41
 *    window.points[1].coordinate.x
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    42
 *    window.points[1].colour
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    43
 *    etc... ARE ALLOWED!
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    44
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    45
 * This class must be passed the scope within which the
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    46
 * variable was declared, and the variable name...
321
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    47
 *
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    48
 *
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    49
 *
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    50
 *
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    51
 *
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    52
 * This class has several members, depending on the exact data the caller
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    53
 * is looking for...
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    54
 *
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    55
 *    - item i: we can get either the name of the data type(A),
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    56
 *              or it's declaration (B)
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    57
 *             (notice however that some variables belong to a data type that does
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    58
 *              not have a name, only a declaration as in
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    59
 *              VAR a: ARRAY [1..3] of INT; END_VAR
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    60
 *             )
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    61
 *    - item ii: we can get either the direct data type (1), 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    62
 *               or the base type (2)
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    63
 * 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    64
 *   By direct type, I mean the data type of the variable. By base type, I 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    65
 * mean the data type on which the direct type is based on. For example, in 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    66
 * a subrange on INT, the direct type is the subrange itself, while the 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    67
 * base type is INT.
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    68
 * e.g.
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    69
 *   This means that if we find that the variable is of type MY_INT,
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    70
 *   which was previously declared to be
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    71
 *   TYPE MY_INT: INT := 9;
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    72
 *   option (1) will return MY_INT
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    73
 *   option (2) will return INT
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    74
 * 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    75
 *
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    76
 * Member functions:
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    77
 * ================
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    78
 *   get_basetype_decl()  ---> returns 2B 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    79
 *   get_type_id()        ---> returns 1A
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    80
 * 
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    81
 *   Since we haven't yet needed them, we don't yet implement
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    82
 *   get_basetype_id()    ----> would return 2A
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    83
 *   get_type_decl()      ----> would return 1B
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    84
 */ 
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    85
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    86
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 181
diff changeset
    87
/*
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 181
diff changeset
    88
 * TODO: this code has a memory leak...
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 181
diff changeset
    89
 *       We call 'new' in several locations, but bever get to 'delete' the object instances...
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 181
diff changeset
    90
 */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    91
#include "absyntax_utils.hh"
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    92
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    93
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    94
search_varfb_instance_type_c::search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    95
  this->decompose_var_instance_name = NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    96
  this->current_structelement_name = NULL;
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
    97
  this->current_typeid = NULL;
372
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
    98
  this->current_basetypeid = NULL;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
    99
}
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   100
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   101
symbol_c *search_varfb_instance_type_c::get_type_decl(symbol_c *variable_name) {
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   102
  this->current_structelement_name = NULL;
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   103
  this->current_typeid = NULL;
372
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   104
  this->current_basetypeid = NULL;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   105
  this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   106
  if (NULL == decompose_var_instance_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   107
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   108
  /* find the part of the variable name that will appear in the
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   109
   * variable declaration, for e.g., in window.point.x, this would be
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   110
   * window!
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   111
   */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   112
  symbol_c *var_name_part = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   113
  if (NULL == var_name_part) ERROR;
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 181
diff changeset
   114
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   115
  /* Now we try to find the variable instance declaration, to determine its type... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   116
  symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part);
372
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   117
  if (NULL == var_decl) ERROR;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   118
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   119
  /* if it is a struct or function block, we must search the type
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   120
   * of the struct or function block member.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   121
   * This is done by this class visiting the var_decl.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   122
   * This class, while visiting, will recursively call
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   123
   * decompose_var_instance_name->get_next() when and if required...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   124
   */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   125
  symbol_c *res = (symbol_c *)var_decl->accept(*this);
367
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   126
  /* NOTE: A Null result is not really an internal compiler error, but rather an error in 
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   127
   * the IEC 61131-3 source code being compiled. This means we cannot just abort the compiler with ERROR.
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   128
   * //   if (NULL == res) ERROR;
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   129
   */
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   130
  if (NULL == res) return NULL;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   131
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   132
  /* make sure that we have decomposed all structure elements of the variable name */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   133
  symbol_c *var_name = decompose_var_instance_name->next_part();
367
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   134
  /* NOTE: A non-NULL result is not really an internal compiler error, but rather an error in 
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   135
   * the IEC 61131-3 source code being compiled. 
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   136
   *  (for example, 'int_var.struct_elem' in the source code, when 'int_var' is a simple integer,
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   137
   *   and not a structure, will result in this result being non-NULL!)
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   138
   * This means we cannot just abort the compiler with ERROR.
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   139
   * //   if (NULL != var_name) ERROR;
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   140
   */
6d94128ba5ad Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
Mario de Sousa <msousa@fe.up.pt>
parents: 326
diff changeset
   141
  if (NULL != var_name) return NULL;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   142
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   143
  return res;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   144
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   145
372
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   146
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   147
symbol_c *search_varfb_instance_type_c::get_basetype_decl(symbol_c *variable_name) {
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   148
  symbol_c *res = get_type_decl(variable_name);
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   149
  if (NULL == res) return NULL;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   150
  return (symbol_c *)base_type(res);
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   151
}  
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   152
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   153
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   154
unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   155
  this->current_structelement_name = NULL;
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   156
  this->current_typeid = NULL;
372
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   157
  this->current_basetypeid = NULL;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   158
  this->is_complex = false;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   159
  this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   160
  if (NULL == decompose_var_instance_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   161
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   162
  /* find the part of the variable name that will appear in the
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   163
   * variable declaration, for e.g., in window.point.x, this would be
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   164
   * window!
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   165
   */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   166
  symbol_c *var_name_part = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   167
  if (NULL == var_name_part) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   168
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   169
  /* Now we try to find the variable instance declaration, to determine its type... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   170
  symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   171
  if (NULL == var_decl) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   172
    /* variable instance declaration not found! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   173
    return 0;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   174
  }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   175
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   176
  /* if it is a struct or function block, we must search the type
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   177
   * of the struct or function block member.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   178
   * This is done by this class visiting the var_decl.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   179
   * This class, while visiting, will recursively call
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   180
   * decompose_var_instance_name->get_next() when and if required...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   181
   */
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   182
  var_decl->accept(*this);
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   183
  unsigned int res = search_var_instance_decl.get_vartype();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   184
  
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   185
  /* make sure that we have decomposed all structure elements of the variable name */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   186
  symbol_c *var_name = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   187
  if (NULL != var_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   188
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   189
  return res;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   190
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   191
321
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
   192
symbol_c *search_varfb_instance_type_c::get_type_id(symbol_c *variable_name) {
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   193
  this->current_typeid = NULL;
372
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   194
  symbol_c *vartype = this->get_type_decl(variable_name);
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   195
  if (this->current_typeid != NULL)
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   196
    return this->current_typeid;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   197
  else
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   198
    return vartype;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   199
}
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   200
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   201
bool search_varfb_instance_type_c::type_is_complex(void) {
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   202
  return this->is_complex;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   203
}
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   204
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   205
/* a helper function... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   206
void *search_varfb_instance_type_c::visit_list(list_c *list)	{
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   207
  if (NULL == current_structelement_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   208
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   209
  for(int i = 0; i < list->n; i++) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   210
    void *res = list->elements[i]->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   211
    if (res != NULL)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   212
      return res;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   213
  }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   214
  /* not found! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   215
  return NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   216
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   217
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   218
/* a helper function... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   219
void *search_varfb_instance_type_c::base_type(symbol_c *symbol)	{
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   220
    search_base_type_c search_base_type;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   221
    return symbol->accept(search_base_type);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   222
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   223
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   224
/* We override the base class' visitor to identifier_c.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   225
 * This is so because the base class does not consider a function block
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   226
 * to be a type, unlike this class that allows a variable instance
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   227
 * of a function block type...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   228
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   229
void *search_varfb_instance_type_c::visit(identifier_c *type_name) {
372
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   230
  /* we only store the new type id if none had been found yet.
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   231
   * Since we will recursively carry on looking at the base type 
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   232
   * to determine the base type declaration and id, we must only set this variable
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   233
   * the first time.
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   234
   * e.g. TYPE myint1_t : int    := 1;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   235
   *           myint2_t : int1_t := 2;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   236
   *           myint3_t : int2_t := 3;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   237
   *      END_TYPE;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   238
   *      VAR
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   239
   *          myint1 : myint1_t;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   240
   *          myint2 : myint2_t;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   241
   *          myint3 : myint3_t;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   242
   *      END_VAR
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   243
   *        
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   244
   *     If we ask for typeid of     myint3, it must return myint3_t
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   245
   *     If we ask for basetypeid of myint3, it must return int
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   246
   *
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   247
   *     When determining the data type of myint3, we will recursively go all the way
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   248
   *     down to int, but we must still only store myint3_t as the base type id.
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   249
   */
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   250
  if (NULL == this->current_typeid)
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   251
    this->current_typeid = type_name;
25332e048742 Implement get_type_decl(), as it seems Manuele will need it later...
Mario de Sousa <msousa@fe.up.pt>
parents: 371
diff changeset
   252
  this->current_basetypeid = type_name;
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   253
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   254
  /* look up the type declaration... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   255
  symbol_c *fb_decl = function_block_type_symtable.find_value(type_name);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   256
  if (fb_decl != function_block_type_symtable.end_value())
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   257
    /* Type declaration found!! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   258
    return fb_decl->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   259
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   260
  /* No. It is not a function block, so we let
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   261
   * the base class take care of it...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   262
   */
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   263
  return search_base_type_c::visit(type_name);
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   264
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   265
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   266
/********************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   267
/* B 1.3.3 - Derived data types */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   268
/********************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   269
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   270
/*  identifier ':' array_spec_init */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   271
void *search_varfb_instance_type_c::visit(array_type_declaration_c *symbol) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   272
  return symbol->array_spec_init->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   273
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   274
    
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 235
diff changeset
   275
/* array_specification [ASSIGN array_initialization] */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   276
/* array_initialization may be NULL ! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   277
void *search_varfb_instance_type_c::visit(array_spec_init_c *symbol) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   278
  return symbol->array_specification->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   279
}
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 235
diff changeset
   280
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   281
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   282
void *search_varfb_instance_type_c::visit(array_specification_c *symbol) {
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 226
diff changeset
   283
  this->is_complex = true;
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 372
diff changeset
   284
  this->current_typeid = symbol;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   285
  return symbol->non_generic_type_name->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   286
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   287
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   288
/*  structure_type_name ':' structure_specification */
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   289
/* NOTE: this is only used inside a TYPE ... END_TYPE declaration. 
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   290
 * It is never used directly when declaring a new variable! 
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   291
 */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   292
void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) {
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 226
diff changeset
   293
  this->is_complex = true;
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   294
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   295
  if (NULL == current_structelement_name) ERROR;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   296
  return symbol->structure_specification->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   297
  /* NOTE: structure_specification will point to either a
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   298
   *       initialized_structure_c
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   299
   *       OR A
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   300
   *       structure_element_declaration_list_c
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   301
   */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   302
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   303
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   304
/* structure_type_name ASSIGN structure_initialization */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   305
/* structure_initialization may be NULL ! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   306
// SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   307
/* NOTE: only the initialized structure is ever used when declaring a new variable instance */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   308
void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol)	{
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   309
  this->is_complex = true;
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   310
  if (NULL != current_structelement_name) ERROR;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   311
  
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   312
  /* make sure that we have decomposed all strcuture elements of the variable name */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   313
  symbol_c *var_name = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   314
  if (NULL == var_name) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   315
    /* this is it... !
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   316
     * No need to look any further...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   317
     * Note also that, unlike for the struct types, a function block may
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   318
     * not be defined based on another (i.e. no inheritance is allowed),
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   319
     * so this function block is already the most base type.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   320
     * We simply return it.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   321
     */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   322
    return (void *)symbol;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   323
   }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   324
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   325
  /* look for the var_name in the structure declaration */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   326
  current_structelement_name = var_name;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   327
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   328
  /* recursively find out the data type of current_structelement_name... */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   329
  return symbol->structure_type_name->accept(*this);
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   330
}
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   331
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   332
/* helper symbol for structure_declaration */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   333
/* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   334
/* structure_element_declaration_list structure_element_declaration ';' */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   335
void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol)	{
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   336
  if (NULL == current_structelement_name) ERROR;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   337
  /* now search the structure declaration */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   338
  return visit_list(symbol);
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   339
}
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   340
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   341
/*  structure_element_name ':' spec_init */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   342
void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) {
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   343
  if (NULL == current_structelement_name) ERROR;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   344
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   345
  if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) {
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   346
    current_structelement_name = NULL;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   347
    /* found the type of the element we were looking for! */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   348
    return symbol->spec_init->accept(*this);
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   349
  }  
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   350
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   351
  /* Did not find the type of the element we were looking for! */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   352
  /* Will keep looking... */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   353
  return NULL;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   354
}
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   355
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   356
/* helper symbol for structure_initialization */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   357
/* structure_initialization: '(' structure_element_initialization_list ')' */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   358
/* structure_element_initialization_list ',' structure_element_initialization */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   359
void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   360
/*  structure_element_name ASSIGN value */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   361
void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   362
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   363
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   364
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   365
/**************************************/
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   366
/* B.1.5 - Program organization units */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   367
/**************************************/
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   368
/*****************************/
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   369
/* B 1.5.2 - Function Blocks */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   370
/*****************************/
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   371
/*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   372
// SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   373
void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) {
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   374
  /* make sure that we have decomposed all strcuture elements of the variable name */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   375
  symbol_c *var_name = decompose_var_instance_name->next_part();
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   376
  if (NULL == var_name) {
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   377
    /* this is it... !
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   378
     * No need to look any further...
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   379
     * Note also that, unlike for the struct types, a function block may
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   380
     * not be defined based on another (i.e. no inheritance is allowed),
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   381
     * so this function block is already the most base type.
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   382
     * We simply return it.
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   383
     */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   384
    return (void *)symbol;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   385
   }
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   386
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   387
   /* now search the function block declaration for the variable... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   388
   search_var_instance_decl_c search_decl(symbol);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   389
   symbol_c *var_decl = search_decl.get_decl(var_name);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   390
   if (NULL == var_decl) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   391
     /* variable instance declaration not found! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   392
     return NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   393
   }
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   394
#if 0
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   395
   /* We have found the declaration.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   396
    * Should we look any further?
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   397
    */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   398
   var_name = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   399
   if (NULL == var_name) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   400
     /* this is it... ! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   401
     return base_type(var_decl);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   402
   }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   403
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   404
  current_structelement_name = var_name;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   405
  /* recursively find out the data type of var_name... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   406
  return symbol->var_declarations->accept(*this);
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   407
#endif  
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   408
  /* carry on recursively, in case the variable has more elements to be decomposed... */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   409
  return var_decl->accept(*this);
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   410
}