ccb@202: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@265: * msousa@265: * Copyright (C) 2009-2011 Mario de Sousa (msousa@fe.up.pt) msousa@265: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * ccb@202: * ccb@202: * This code is made available on the understanding that it will not be ccb@202: * used in safety-critical situations without a full and competent review. ccb@202: */ ccb@202: ccb@202: /* msousa@265: * An IEC 61131-3 compiler. ccb@202: * ccb@202: * Based on the ccb@202: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) ccb@202: * ccb@202: */ ccb@202: ccb@202: ccb@202: /* ccb@202: * Iterate through all declared functions and Function Blocks, ccb@202: * and, for each function/FB, add a declaration of the EN and ENO ccb@202: * parameters, if they have not already been explicitly declared. ccb@202: * ccb@202: * EN and ENO parameters declared explicitly (by the user in the source code) ccb@202: * and implicitly (by the comnpiler, i.e. by this visitor class) may be ccb@202: * distinguished later on by the 'method' flag in the en_param_declaration_c ccb@202: * and eno_param_declaration_c objects. ccb@202: */ ccb@202: ccb@202: #include "add_en_eno_param_decl.hh" ccb@202: #include ccb@202: ccb@202: ccb@202: // #define DEBUG ccb@202: #ifdef DEBUG ccb@202: #define TRACE(classname) printf("\n____%s____\n",classname); ccb@202: #else ccb@202: #define TRACE(classname) ccb@202: #endif ccb@202: ccb@202: ccb@202: #define ERROR error_exit(__FILE__,__LINE__) ccb@202: /* function defined in main.cc */ ccb@202: extern void error_exit(const char *file_name, int line_no); ccb@202: ccb@202: ccb@202: ccb@202: /* This class is a singleton. ccb@202: * So we need a pointer to the singe instance... ccb@202: */ ccb@202: add_en_eno_param_decl_c *add_en_eno_param_decl_c::singleton = NULL; ccb@202: ccb@202: /* Constructor for the singleton class */ ccb@202: symbol_c *add_en_eno_param_decl_c::add_to(symbol_c *tree_root) { ccb@202: if (NULL == singleton) { ccb@202: singleton = new add_en_eno_param_decl_c; ccb@202: if (NULL == singleton) ccb@202: return NULL; ccb@202: } ccb@202: tree_root->accept(*singleton); ccb@202: return tree_root; ccb@202: } ccb@202: ccb@202: /* Destructor for the singleton class */ ccb@202: add_en_eno_param_decl_c::~add_en_eno_param_decl_c(void) { ccb@202: if (NULL != singleton) delete singleton; ccb@202: singleton = NULL; ccb@202: } ccb@202: ccb@202: ccb@202: ccb@202: void* add_en_eno_param_decl_c::iterate_list(list_c *list) { ccb@202: for (int i = 0; i < list->n; i++) { ccb@202: list->elements[i]->accept(*this); ccb@202: } ccb@202: return NULL; ccb@202: } ccb@202: ccb@202: ccb@202: input_declarations_c *add_en_eno_param_decl_c::build_en_param(void) { ccb@202: boolean_literal_c *boolean_literal = ccb@202: new boolean_literal_c(new bool_type_name_c(), new boolean_true_c()); ccb@202: identifier_c *identifier = ccb@202: new identifier_c("EN"); ccb@202: en_param_declaration_c *en_param_declaration = ccb@202: new en_param_declaration_c(identifier, new bool_type_name_c(), boolean_literal, new implicit_definition_c()); ccb@202: /* the last paramater is to flag that this ccb@202: * declaration was inserted automatically, i.e. an implicit declaration ccb@202: */ ccb@202: input_declaration_list_c *input_declaration_list = new input_declaration_list_c(); ccb@202: input_declaration_list->add_element(en_param_declaration); ccb@202: ccb@202: input_declarations_c *input_declarations = new input_declarations_c(NULL, input_declaration_list, new implicit_definition_c()); ccb@202: return input_declarations; ccb@202: } ccb@202: ccb@202: ccb@202: output_declarations_c *add_en_eno_param_decl_c::build_eno_param(void) { ccb@202: identifier_c *identifier = ccb@202: new identifier_c("ENO"); ccb@202: eno_param_declaration_c *eno_param_declaration = ccb@202: new eno_param_declaration_c(identifier, new bool_type_name_c(), new implicit_definition_c()); ccb@202: /* the last paramater is to flag that this ccb@202: * declaration was inserted automatically, i.e. an implicit declaration ccb@202: */ ccb@202: var_init_decl_list_c *var_init_decl_list = new var_init_decl_list_c(); ccb@202: var_init_decl_list->add_element(eno_param_declaration); ccb@202: ccb@202: output_declarations_c *output_declarations = new output_declarations_c(NULL, var_init_decl_list, new implicit_definition_c()); ccb@202: return output_declarations; ccb@202: } ccb@202: ccb@202: ccb@202: ccb@202: ccb@202: /***************************/ ccb@202: /* B 0 - Programming Model */ ccb@202: /***************************/ ccb@202: void *add_en_eno_param_decl_c::visit(library_c *symbol) { ccb@202: TRACE("library_c"); ccb@202: return iterate_list(symbol); ccb@202: } ccb@202: ccb@202: ccb@202: /***********************/ ccb@202: /* B 1.5.1 - Functions */ ccb@202: /***********************/ ccb@202: void *add_en_eno_param_decl_c::visit(function_declaration_c *symbol) { ccb@202: TRACE("function_declaration_c"); ccb@202: return symbol->var_declarations_list->accept(*this); ccb@202: } ccb@202: ccb@202: void *add_en_eno_param_decl_c::visit(var_declarations_list_c *symbol) { ccb@202: TRACE("var_declarations_list_c"); ccb@202: en_declared = false; ccb@202: eno_declared = false; ccb@202: iterate_list(symbol); ccb@202: if(en_declared == false) symbol->add_element(build_en_param()); ccb@202: if(eno_declared == false) symbol->add_element(build_eno_param()); ccb@202: return NULL; ccb@202: } ccb@202: ccb@202: ccb@202: ccb@202: /****************************************/ ccb@202: /* 1.4.3 - Declaration & Initialisation */ ccb@202: /****************************************/ ccb@202: void *add_en_eno_param_decl_c::visit(input_declarations_c *symbol) { ccb@202: TRACE("input_declarations_c"); ccb@202: // current_param_direction = direction_in; ccb@202: return symbol->input_declaration_list->accept(*this); ccb@202: } ccb@202: ccb@202: void *add_en_eno_param_decl_c::visit(input_declaration_list_c *symbol) {TRACE("input_declaration_list_c"); return iterate_list(symbol);} ccb@202: ccb@202: void *add_en_eno_param_decl_c::visit(en_param_declaration_c *symbol) { ccb@202: TRACE("en_param_declaration_c"); ccb@202: en_declared = true; ccb@202: return NULL; ccb@202: } ccb@202: ccb@202: void *add_en_eno_param_decl_c::visit(eno_param_declaration_c *symbol) { ccb@202: TRACE("eno_param_declaration_c"); ccb@202: eno_declared = true; ccb@202: return NULL; ccb@202: } ccb@202: ccb@202: void *add_en_eno_param_decl_c::visit(output_declarations_c *symbol) { ccb@202: TRACE("output_declarations_c"); ccb@202: // current_param_direction = direction_out; ccb@202: return symbol->var_init_decl_list->accept(*this); ccb@202: } ccb@202: ccb@202: void *add_en_eno_param_decl_c::visit(var_init_decl_list_c *symbol) ccb@202: {TRACE("var_init_decl_list_c"); return iterate_list(symbol);} ccb@202: ccb@202: ccb@202: /*****************************/ ccb@202: /* B 1.5.2 - Function Blocks */ ccb@202: /*****************************/ ccb@202: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ ccb@202: void *add_en_eno_param_decl_c::visit(function_block_declaration_c *symbol) { ccb@202: TRACE("function_block_declaration_c"); ccb@202: return symbol->var_declarations->accept(*this); ccb@202: } ccb@202: ccb@202: ccb@202: ccb@202: