stage3/enum_declaration_check.cc
author mjsousa
Wed, 15 Apr 2015 23:25:07 +0100
changeset 999 dd50a82ae8da
parent 752 8f05bde3efa8
permissions -rw-r--r--
transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
752
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
/*
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
 *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
 *  This program is free software: you can redistribute it and/or modify
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
 *  it under the terms of the GNU General Public License as published by
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
 *  the Free Software Foundation, either version 3 of the License, or
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
 *  (at your option) any later version.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    11
 *  This program is distributed in the hope that it will be useful,
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
 *  GNU General Public License for more details.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
 *  You should have received a copy of the GNU General Public License
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
 * This code is made available on the understanding that it will not be
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    21
 * used in safety-critical situations without a full and competent review.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
 */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
/*
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
 * An IEC 61131-3 compiler.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    26
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    27
 * Based on the
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    28
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    29
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    30
 */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    32
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    33
/* Declaration sequence is a source code part needed to declare variables.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
 * There are some checks we need to do before start with other analysis:
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    35
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    36
 *   - Check external option redefinition.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    37
 *   - Check external data type redefinition.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    38
 *   - Check initial values consistently with the data types of the variables/data types being declared.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    39
 *   - Check whether a function block uses a CONSTANT qualifier as described in 2.5.2.1.
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    40
 *
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    41
 */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    42
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    43
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    44
#include "enum_declaration_check.hh"
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    45
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
#define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    49
#define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    52
  if (current_display_error_level >= error_level) {                                                                         \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
    fprintf(stderr, __VA_ARGS__);                                                                                           \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
    fprintf(stderr, "\n");                                                                                                  \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
    error_count++;                                                                                                     \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    59
  }                                                                                                                         \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    60
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    62
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    64
    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    65
            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    66
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    67
    fprintf(stderr, __VA_ARGS__);                                                                                           \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    68
    fprintf(stderr, "\n");                                                                                                  \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
    warning_found = true;                                                                                                   \
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    70
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    71
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    72
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    73
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    74
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    75
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
/*****************************************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
/*                                                   */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
/*  A small helper class...                          */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    82
/*                                                   */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    83
/*****************************************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
/* Add to the local_enumerated_value_symtable the local enum value constants */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
 
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    87
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
class populate_enumvalue_symtable_c: public iterator_visitor_c {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
  private:
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    90
    symbol_c                        *current_enumerated_type;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    91
    symbol_c::enumvalue_symtable_t  *enumvalue_symtable;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    92
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    93
  private:
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    94
    int &error_count;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    95
    int &current_display_error_level;    
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    97
  public:
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    98
     populate_enumvalue_symtable_c(int &error_count_, int &current_display_error_level_) 
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    99
       : error_count(error_count_), current_display_error_level(current_display_error_level_) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   100
       current_enumerated_type = NULL;  
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   101
       enumvalue_symtable = NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   102
     };
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   103
    ~populate_enumvalue_symtable_c(void) {}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   104
    
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   105
    void populate(symbol_c::enumvalue_symtable_t *symtable, symbol_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   106
       enumvalue_symtable = symtable;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   107
       symbol->accept(*this);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   108
       enumvalue_symtable = NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   109
     }
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   110
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   111
  protected:
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   112
  /*************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   113
  /* B.1 - Common elements */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   114
  /*************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   115
  /**********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   116
  /* B.1.3 - Data types */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   117
  /**********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   118
  /********************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   119
  /* B 1.3.3 - Derived data types */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   120
  /********************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   121
  /*  enumerated_type_name ':' enumerated_spec_init */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   122
  void *visit(enumerated_type_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   123
    current_enumerated_type = symbol;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   124
    symbol->enumerated_spec_init->accept(*this);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   125
    current_enumerated_type = NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   126
    return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   127
  }
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   128
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   129
  /* enumerated_specification ASSIGN enumerated_value */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   130
  void *visit(enumerated_spec_init_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   131
    if (NULL == current_enumerated_type)
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   132
      current_enumerated_type = symbol;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   133
    symbol->enumerated_specification->accept(*this);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   134
    /* DO NOT visit the symbol->enumerated_value   !!! */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   135
    current_enumerated_type = NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   136
    return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   137
  }
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   138
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   139
  /* [enumerated_type_name '#'] identifier */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   140
  void *visit(enumerated_value_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   141
    token_c *value = dynamic_cast <token_c *>(symbol->value);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   142
    if (NULL == value) ERROR;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   143
    const char *value_str = value->value;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   144
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   145
    if (current_enumerated_type == NULL) ERROR;  
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   146
    /* this is really an ERROR! The initial value may use the syntax NUM_TYPE#enum_value, but in that case we should not have reached this visit method !! */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   147
    if (symbol->type != NULL) ERROR;  
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   148
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   149
    symbol_c::enumvalue_symtable_t::iterator lower = enumvalue_symtable->lower_bound(value_str);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   150
    symbol_c::enumvalue_symtable_t::iterator upper = enumvalue_symtable->upper_bound(value_str);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   151
    for (; lower != upper; lower++)
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   152
      if (lower->second == current_enumerated_type) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   153
        /*  The same identifier is used more than once as an enumerated value/constant inside the same enumerated datat type! */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   154
        STAGE3_ERROR(0, symbol, symbol, "Duplicate identifier in enumerated data type.");
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   155
        return NULL; /* No need to insert it! It is already in the table! */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   156
      }
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   157
    
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   158
    /* add it to the local symbol table. */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   159
    enumvalue_symtable->insert(std::pair<const char *, symbol_c *>(value_str, current_enumerated_type));
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   160
    return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   161
  }
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   162
}; // class populate_enumvalue_symtable_c
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   163
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   164
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   165
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   166
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   167
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   168
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   169
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   170
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   171
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   172
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   173
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   174
enum_declaration_check_c::enum_declaration_check_c(symbol_c *ignore) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   175
  error_count = 0;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   176
  current_display_error_level = 0;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   177
  global_enumvalue_symtable = NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   178
  populate_enumvalue_symtable = new populate_enumvalue_symtable_c(error_count, current_display_error_level);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   179
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   180
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   181
enum_declaration_check_c::~enum_declaration_check_c(void) {}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   182
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   183
int enum_declaration_check_c::get_error_count() {return error_count;}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   184
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   185
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   186
/***************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   187
/* B 0 - Programming Model */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   188
/***************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   189
void *enum_declaration_check_c::visit(library_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   190
  global_enumvalue_symtable = &(symbol->enumvalue_symtable);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   191
  iterator_visitor_c::visit(symbol); // fall back to base class
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   192
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   193
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   194
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   195
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   196
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   197
/**********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   198
/* B.1.3 - Data types */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   199
/**********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   200
void *enum_declaration_check_c::visit(data_type_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   201
  if (NULL == global_enumvalue_symtable) ERROR;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   202
  populate_enumvalue_symtable->populate(global_enumvalue_symtable, symbol);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   203
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   204
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   205
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   206
/*********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   207
/* B 1.5.1 Functions */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   208
/*********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   209
void *enum_declaration_check_c::visit(function_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   210
  populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->var_declarations_list);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   211
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   212
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   213
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   214
/***************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   215
/* B 1.5.2 Function blocks */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   216
/***************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   217
void *enum_declaration_check_c::visit(function_block_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   218
  populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->var_declarations);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   219
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   220
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   221
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   222
/**********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   223
/* B 1.5.3 - Programs */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   224
/**********************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   225
void *enum_declaration_check_c::visit(program_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   226
  populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->var_declarations);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   227
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   228
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   229
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   230
/********************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   231
/* B 1.7 Configuration elements */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   232
/********************************/
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   233
void *enum_declaration_check_c::visit(configuration_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   234
  if (NULL != symbol->global_var_declarations)
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   235
    populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->global_var_declarations);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   236
  if (NULL != symbol->resource_declarations)
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   237
    /* May reference either a list of resource_declaration_c, or a single_resource_declaration_c */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   238
    populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->resource_declarations);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   239
  
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   240
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   241
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   242
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   243
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   244
void *enum_declaration_check_c::visit(resource_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   245
  if (NULL != symbol->global_var_declarations)
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   246
    populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->global_var_declarations);
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   247
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   248
}
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   249
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   250
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   251
void *enum_declaration_check_c::visit(single_resource_declaration_c *symbol) {
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   252
  /* do NOT visit! */
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   253
  return NULL;
8f05bde3efa8 Add files missing from a few commits ago...
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   254
}