absyntax_utils/add_en_eno_param_decl.cc
changeset 202 da1a8186f86f
child 265 4d222f46f8cc
equal deleted inserted replaced
201:e657008f43d0 202:da1a8186f86f
       
     1 /*
       
     2  * (c) 2009 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /*
       
    27  * Iterate through all declared functions and Function Blocks, 
       
    28  * and, for each function/FB, add a declaration of the EN and ENO 
       
    29  * parameters, if they have not already been explicitly declared.
       
    30  *
       
    31  * EN and ENO parameters declared explicitly (by the user in the source code)
       
    32  * and implicitly (by the comnpiler, i.e. by this visitor class) may be
       
    33  * distinguished later on by the 'method' flag in the en_param_declaration_c
       
    34  * and eno_param_declaration_c objects.
       
    35  */
       
    36 
       
    37 #include "add_en_eno_param_decl.hh"
       
    38 #include <strings.h>
       
    39 
       
    40 
       
    41 // #define DEBUG
       
    42 #ifdef DEBUG
       
    43 #define TRACE(classname) printf("\n____%s____\n",classname);
       
    44 #else
       
    45 #define TRACE(classname)
       
    46 #endif
       
    47 
       
    48 
       
    49 #define ERROR error_exit(__FILE__,__LINE__)
       
    50 /* function defined in main.cc */
       
    51 extern void error_exit(const char *file_name, int line_no);
       
    52 
       
    53 
       
    54 
       
    55 /* This class is a singleton.
       
    56  * So we need a pointer to the singe instance...
       
    57  */
       
    58 add_en_eno_param_decl_c *add_en_eno_param_decl_c::singleton = NULL;
       
    59 
       
    60 /* Constructor for the singleton class */
       
    61 symbol_c *add_en_eno_param_decl_c::add_to(symbol_c *tree_root) {
       
    62       if (NULL == singleton) {
       
    63         singleton = new add_en_eno_param_decl_c;
       
    64         if (NULL == singleton)
       
    65           return NULL;
       
    66       }
       
    67       tree_root->accept(*singleton);
       
    68       return tree_root;
       
    69     }
       
    70 
       
    71 /* Destructor for the singleton class */
       
    72 add_en_eno_param_decl_c::~add_en_eno_param_decl_c(void) {
       
    73       if (NULL != singleton) delete singleton;
       
    74       singleton = NULL;
       
    75     }
       
    76 
       
    77 
       
    78 
       
    79 void* add_en_eno_param_decl_c::iterate_list(list_c *list) {
       
    80   for (int i = 0; i < list->n; i++) {
       
    81     list->elements[i]->accept(*this);
       
    82   }
       
    83   return NULL;
       
    84 }
       
    85 
       
    86 
       
    87 input_declarations_c *add_en_eno_param_decl_c::build_en_param(void) {
       
    88   boolean_literal_c *boolean_literal = 
       
    89     new boolean_literal_c(new bool_type_name_c(), new boolean_true_c());
       
    90   identifier_c *identifier =
       
    91     new identifier_c("EN");
       
    92   en_param_declaration_c *en_param_declaration = 
       
    93     new en_param_declaration_c(identifier, new bool_type_name_c(), boolean_literal, new implicit_definition_c());
       
    94     /* the last paramater is to flag that this
       
    95      * declaration was inserted automatically, i.e. an implicit declaration 
       
    96      */
       
    97   input_declaration_list_c *input_declaration_list = new input_declaration_list_c();
       
    98   input_declaration_list->add_element(en_param_declaration);
       
    99 
       
   100   input_declarations_c *input_declarations = new input_declarations_c(NULL, input_declaration_list, new implicit_definition_c());
       
   101   return input_declarations;
       
   102 }
       
   103 
       
   104 
       
   105 output_declarations_c *add_en_eno_param_decl_c::build_eno_param(void) {
       
   106   identifier_c *identifier =
       
   107     new identifier_c("ENO");
       
   108   eno_param_declaration_c *eno_param_declaration =
       
   109     new eno_param_declaration_c(identifier, new bool_type_name_c(), new implicit_definition_c()); 
       
   110     /* the last paramater is to flag that this
       
   111      * declaration was inserted automatically, i.e. an implicit declaration 
       
   112      */
       
   113   var_init_decl_list_c *var_init_decl_list = new var_init_decl_list_c();
       
   114   var_init_decl_list->add_element(eno_param_declaration);
       
   115 
       
   116   output_declarations_c *output_declarations = new output_declarations_c(NULL, var_init_decl_list, new implicit_definition_c());
       
   117   return output_declarations;
       
   118 }
       
   119 
       
   120 
       
   121 
       
   122 
       
   123 /***************************/
       
   124 /* B 0 - Programming Model */
       
   125 /***************************/
       
   126 void *add_en_eno_param_decl_c::visit(library_c *symbol) {
       
   127   TRACE("library_c"); 
       
   128   return iterate_list(symbol);
       
   129 }
       
   130 
       
   131 
       
   132 /***********************/
       
   133 /* B 1.5.1 - Functions */
       
   134 /***********************/
       
   135 void *add_en_eno_param_decl_c::visit(function_declaration_c *symbol) {
       
   136   TRACE("function_declaration_c"); 
       
   137   return symbol->var_declarations_list->accept(*this);
       
   138 }
       
   139 
       
   140 void *add_en_eno_param_decl_c::visit(var_declarations_list_c *symbol) {
       
   141     TRACE("var_declarations_list_c");
       
   142     en_declared  = false; 
       
   143     eno_declared = false; 
       
   144     iterate_list(symbol);
       
   145     if(en_declared  == false) symbol->add_element(build_en_param());
       
   146     if(eno_declared == false) symbol->add_element(build_eno_param());
       
   147     return NULL;
       
   148 }
       
   149 
       
   150 
       
   151 
       
   152 /****************************************/
       
   153 /* 1.4.3 - Declaration & Initialisation */
       
   154 /****************************************/
       
   155 void *add_en_eno_param_decl_c::visit(input_declarations_c *symbol) {
       
   156   TRACE("input_declarations_c");
       
   157 //   current_param_direction = direction_in;
       
   158   return symbol->input_declaration_list->accept(*this);
       
   159 }
       
   160 
       
   161 void *add_en_eno_param_decl_c::visit(input_declaration_list_c *symbol) {TRACE("input_declaration_list_c"); return iterate_list(symbol);}
       
   162 
       
   163 void *add_en_eno_param_decl_c::visit(en_param_declaration_c *symbol) {
       
   164   TRACE("en_param_declaration_c");
       
   165   en_declared = true;
       
   166   return NULL;
       
   167 }
       
   168 
       
   169 void *add_en_eno_param_decl_c::visit(eno_param_declaration_c *symbol) {
       
   170   TRACE("eno_param_declaration_c");
       
   171   eno_declared = true;
       
   172   return NULL;
       
   173 }
       
   174 
       
   175 void *add_en_eno_param_decl_c::visit(output_declarations_c *symbol) {
       
   176   TRACE("output_declarations_c");
       
   177 //   current_param_direction = direction_out;
       
   178   return symbol->var_init_decl_list->accept(*this);
       
   179 }
       
   180 
       
   181 void *add_en_eno_param_decl_c::visit(var_init_decl_list_c *symbol) 
       
   182 {TRACE("var_init_decl_list_c"); return iterate_list(symbol);}
       
   183 
       
   184 
       
   185 /*****************************/
       
   186 /* B 1.5.2 - Function Blocks */
       
   187 /*****************************/
       
   188 /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
       
   189 void *add_en_eno_param_decl_c::visit(function_block_declaration_c *symbol) {
       
   190   TRACE("function_block_declaration_c"); 
       
   191   return symbol->var_declarations->accept(*this);
       
   192 }
       
   193 
       
   194 
       
   195 
       
   196