absyntax_utils/search_varfb_instance_type.cc
author Mario de Sousa <msousa@fe.up.pt>
Wed, 31 Aug 2011 14:10:53 +0100
changeset 371 926490780952
parent 367 6d94128ba5ad
child 372 25332e048742
permissions -rwxr-xr-x
Cleaning up: Getting some sense into the code handling structures.
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;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
    98
}
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
    99
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
   100
symbol_c *search_varfb_instance_type_c::get_basetype_decl(symbol_c *variable_name) {
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   101
  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
   102
  this->current_typeid = NULL;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   103
  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
   104
  if (NULL == decompose_var_instance_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   105
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   106
  /* 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
   107
   * 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
   108
   * window!
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   109
   */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   110
  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
   111
  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
   112
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   113
  /* 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
   114
  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
   115
  if (NULL == var_decl) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   116
    /* variable instance declaration not found! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   117
      ERROR;
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
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   120
  /* 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
   121
   * of the struct or function block member.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   122
   * This is done by this class visiting the var_decl.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   123
   * This class, while visiting, will recursively call
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   124
   * decompose_var_instance_name->get_next() when and if required...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   125
   */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   126
  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
   127
  /* 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
   128
   * 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
   129
   * //   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
   130
   */
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
   131
  if (NULL == res) return NULL;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   132
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   133
  /* 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
   134
  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
   135
  /* 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
   136
   * 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
   137
   *  (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
   138
   *   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
   139
   * 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
   140
   * //   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
   141
   */
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
   142
  if (NULL != var_name) return NULL;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   143
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   144
  return res;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   145
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   146
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   147
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
   148
  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
   149
  this->current_typeid = NULL;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   150
  this->is_complex = false;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   151
  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
   152
  if (NULL == decompose_var_instance_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   153
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   154
  /* 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
   155
   * 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
   156
   * window!
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   157
   */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   158
  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
   159
  if (NULL == var_name_part) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   160
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   161
  /* 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
   162
  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
   163
  if (NULL == var_decl) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   164
    /* variable instance declaration not found! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   165
    return 0;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   166
  }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   167
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   168
  /* 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
   169
   * of the struct or function block member.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   170
   * This is done by this class visiting the var_decl.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   171
   * This class, while visiting, will recursively call
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   172
   * decompose_var_instance_name->get_next() when and if required...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   173
   */
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   174
  var_decl->accept(*this);
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   175
  unsigned int res = search_var_instance_decl.get_vartype();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   176
  
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   177
  /* 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
   178
  symbol_c *var_name = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   179
  if (NULL != var_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   180
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   181
  return res;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   182
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   183
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
   184
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
   185
  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
   186
  symbol_c *vartype = this->get_basetype_decl(variable_name);
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   187
  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
   188
    return this->current_typeid;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   189
  else
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   190
    return vartype;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   191
}
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   192
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   193
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
   194
  return this->is_complex;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   195
}
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   196
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   197
/* a helper function... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   198
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
   199
  if (NULL == current_structelement_name) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   200
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   201
  for(int i = 0; i < list->n; i++) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   202
    void *res = list->elements[i]->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   203
    if (res != NULL)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   204
      return res;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   205
  }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   206
  /* not found! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   207
  return NULL;
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
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   210
/* a helper function... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   211
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
   212
    search_base_type_c search_base_type;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   213
    return symbol->accept(search_base_type);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   214
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   215
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   216
/* We override the base class' visitor to identifier_c.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   217
 * 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
   218
 * 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
   219
 * of a function block type...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   220
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   221
void *search_varfb_instance_type_c::visit(identifier_c *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
   222
  this->current_typeid = type_name;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   223
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   224
  /* look up the type declaration... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   225
  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
   226
  if (fb_decl != function_block_type_symtable.end_value())
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   227
    /* Type declaration found!! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   228
    return fb_decl->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   229
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   230
  /* No. It is not a function block, so we let
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   231
   * the base class take care of it...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   232
   */
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   233
  return search_base_type_c::visit(type_name);
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   234
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   235
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   236
/********************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   237
/* B 1.3.3 - Derived data types */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   238
/********************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   239
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   240
/*  identifier ':' array_spec_init */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   241
void *search_varfb_instance_type_c::visit(array_type_declaration_c *symbol) {
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 235
diff changeset
   242
  this->is_complex = true;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   243
  return symbol->array_spec_init->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   244
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   245
    
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 235
diff changeset
   246
/* array_specification [ASSIGN array_initialization] */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   247
/* array_initialization may be NULL ! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   248
void *search_varfb_instance_type_c::visit(array_spec_init_c *symbol) {
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 202
diff changeset
   249
  this->is_complex = true;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   250
  return symbol->array_specification->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   251
}
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 235
diff changeset
   252
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   253
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   254
void *search_varfb_instance_type_c::visit(array_specification_c *symbol) {
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 226
diff changeset
   255
  this->is_complex = true;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   256
  return symbol->non_generic_type_name->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   257
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   258
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   259
/*  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
   260
/* 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
   261
 * 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
   262
 */
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   263
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
   264
  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
   265
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   266
  if (NULL == current_structelement_name) ERROR;
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   267
  return symbol->structure_specification->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   268
  /* NOTE: structure_specification will point to either a
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   269
   *       initialized_structure_c
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   270
   *       OR A
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   271
   *       structure_element_declaration_list_c
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   272
   */
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
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   275
/* structure_type_name ASSIGN structure_initialization */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   276
/* structure_initialization may be NULL ! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   277
// 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
   278
/* 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
   279
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
   280
  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
   281
  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
   282
  
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   283
  /* 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
   284
  symbol_c *var_name = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   285
  if (NULL == var_name) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   286
    /* this is it... !
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   287
     * No need to look any further...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   288
     * 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
   289
     * 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
   290
     * so this function block is already the most base type.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   291
     * We simply return it.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   292
     */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   293
    return (void *)symbol;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   294
   }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   295
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   296
  /* 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
   297
  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
   298
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   299
  /* 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
   300
  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
   301
}
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   302
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   303
/* 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
   304
/* 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
   305
/* 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
   306
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
   307
  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
   308
  /* 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
   309
  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
   310
}
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   311
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   312
/*  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
   313
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
   314
  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
   315
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   316
  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
   317
    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
   318
    /* 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
   319
    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
   320
  }  
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   321
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   322
  /* 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
   323
  /* 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
   324
  return NULL;
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   325
}
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   326
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   327
/* 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
   328
/* 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
   329
/* 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
   330
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
   331
/*  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
   332
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
   333
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   334
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   335
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   336
/**************************************/
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   337
/* 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
   338
/**************************************/
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
/* 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
   341
/*****************************/
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   342
/*  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
   343
// 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
   344
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
   345
  /* 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
   346
  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
   347
  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
   348
    /* 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
   349
     * 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
   350
     * 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
   351
     * 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
   352
     * 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
   353
     * 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
   354
     */
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   355
    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
   356
   }
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   357
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   358
   /* now search the function block declaration for the variable... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   359
   search_var_instance_decl_c search_decl(symbol);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   360
   symbol_c *var_decl = search_decl.get_decl(var_name);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   361
   if (NULL == var_decl) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   362
     /* variable instance declaration not found! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   363
     return NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   364
   }
371
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   365
#if 0
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   366
   /* We have found the declaration.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   367
    * Should we look any further?
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   368
    */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   369
   var_name = decompose_var_instance_name->next_part();
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   370
   if (NULL == var_name) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   371
     /* this is it... ! */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   372
     return base_type(var_decl);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   373
   }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   374
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   375
  current_structelement_name = var_name;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   376
  /* recursively find out the data type of var_name... */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   377
  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
   378
#endif  
926490780952 Cleaning up: Getting some sense into the code handling structures.
Mario de Sousa <msousa@fe.up.pt>
parents: 367
diff changeset
   379
  /* 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
   380
  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
   381
}