absyntax_utils/function_call_iterator.cc
author Mario de Sousa <msousa@fe.up.pt>
Thu, 31 Mar 2011 10:52:03 +0100
changeset 259 b6d7c71ff6d8
parent 202 da1a8186f86f
child 265 4d222f46f8cc
permissions -rwxr-xr-x
Adding debug conditional printf()s in stage 3.
Printing semantic error info to stderr instead of stdout.
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     1
/*
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     2
 * (c) 2003 Mario de Sousa
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     3
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     4
 * Offered to the public under the terms of the GNU General Public License
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     5
 * as published by the Free Software Foundation; either version 2 of the
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     6
 * License, or (at your option) any later version.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     7
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     8
 * This program is distributed in the hope that it will be useful, but
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
     9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    11
 * Public License for more details.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    12
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    13
 * 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
    14
 * 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
    15
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    16
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    17
/*
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    18
 * An IEC 61131-3 IL and ST compiler.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    19
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    20
 * Based on the
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    21
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    22
 *
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
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    26
/*
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    27
 * Function call parameter iterator.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    28
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    29
 * This is part of the 4th stage that generates
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    30
 * a c++ source program equivalent to the IL and ST
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    31
 * code.
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
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    34
/* given a function_body_c, iterate through each
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 181
diff changeset
    35
 * function/FB call in that code.
181
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    36
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    37
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    38
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    39
#include "function_call_iterator.hh"
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    40
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    41
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    42
//#define DEBUG
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    43
#ifdef DEBUG
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    44
#define TRACE(classname) printf("\n____%s____\n",classname);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    45
#else
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    46
#define TRACE(classname)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    47
#endif
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    48
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    49
#define ERROR error_exit(__FILE__,__LINE__)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    50
/* function defined in main.cc */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    51
extern void error_exit(const char *file_name, int line_no);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    52
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    53
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    54
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    55
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    56
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    57
/* initialise the iterator object.
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    58
 * We must be given a reference to the function declaration
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    59
 * that will be analysed...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    60
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    61
function_call_iterator_c::function_call_iterator_c(symbol_c *symbol) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    62
  this->start_symbol = symbol;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    63
  next_fcall = fcall_count = 0;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    64
  current_finvocation = NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    65
  current_fcall_name = NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    66
}
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    67
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    68
/* Skip to the next function call. After object creation,
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    69
 * the object references _before_ the first, so
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    70
 * this function must be called once to get the object to
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    71
 * reference the first function call...
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    72
 *
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    73
 * Returns the function_invocation_c!
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    74
 */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    75
//function_invocation_c *next(void) {TRACE("function_call_iterator_c::next(): called ");
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    76
symbol_c *function_call_iterator_c::next(void) {TRACE("function_call_iterator_c::next(): called ");
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    77
  fcall_count = 0;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    78
  next_fcall++;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    79
  current_finvocation = NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    80
  current_fcall_name = NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    81
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    82
  start_symbol->accept(*this);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    83
  return current_finvocation;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    84
}
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
/* Returns the name of the currently referenced function invocation */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    87
identifier_c *function_call_iterator_c::fname(void) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    88
  identifier_c *identifier = dynamic_cast<identifier_c *>(current_fcall_name);
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    89
  if (identifier == NULL) ERROR;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    90
  return identifier;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    91
}
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
/***************************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    95
/* B.3 - Language ST (Structured Text) */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    96
/***************************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    97
/***********************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    98
/* B 3.1 - Expressions */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
    99
/***********************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   100
  void *function_call_iterator_c::visit(function_invocation_c *symbol) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   101
    fcall_count++;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   102
    if (next_fcall == fcall_count) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   103
      current_finvocation = symbol;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   104
      current_fcall_name = symbol->function_name;
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
    return NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   107
  }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   108
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
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   111
/****************************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   112
/* B.2 - Language IL (Instruction List) */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   113
/****************************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   114
/***********************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   115
/* B 2.1 Instructions and Operands */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   116
/***********************************/
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   117
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   118
/* | function_name [il_operand_list] */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   119
// SYM_REF2(il_function_call_c, function_name, il_operand_list)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   120
  void *function_call_iterator_c::visit(il_function_call_c *symbol) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   121
    fcall_count++;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   122
    if (next_fcall == fcall_count) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   123
      current_finvocation = symbol;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   124
      current_fcall_name = symbol->function_name;
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
    return NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   127
  }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   128
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   129
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   130
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   131
/* | function_name '(' eol_list [il_param_list] ')' */
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   132
// SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   133
  void *function_call_iterator_c::visit(il_formal_funct_call_c *symbol) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   134
    fcall_count++;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   135
    if (next_fcall == fcall_count) {
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   136
      current_finvocation = symbol;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   137
      current_fcall_name = symbol->function_name;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   138
    }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   139
    return NULL;
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   140
  }
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   141
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   142
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   143
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   144
38d6eb056260 Moving absyntax utility files out from stage4/generate_c
mario
parents:
diff changeset
   145