mario@181: /*
msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@265: *
msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
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: *
mario@181: *
mario@181: * This code is made available on the understanding that it will not be
mario@181: * used in safety-critical situations without a full and competent review.
mario@181: */
mario@181:
mario@181: /*
msousa@265: * An IEC 61131-3 compiler.
mario@181: *
mario@181: * Based on the
mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
mario@181: *
mario@181: */
mario@181:
mario@181:
mario@181: /* Determine the data type of an ST expression.
mario@181: * A reference to the relevant type definition is returned.
mario@181: *
mario@181: * For example:
mario@181: * 2 + 3 -> returns reference to a int_type_name_c object.
mario@181: * 22.2 - 5 -> returns reference to a real_type_name_c object.
mario@181: * etc...
mario@181: */
mario@181:
mario@181:
mario@181:
mario@181: #include "absyntax_utils.hh"
mario@181: #include
mario@181:
mario@181: search_expression_type_c::search_expression_type_c(symbol_c *search_scope) {
mario@181: search_varfb_instance_type = new search_varfb_instance_type_c(search_scope);
mario@181: }
mario@181:
mario@181: search_expression_type_c::~search_expression_type_c(void) {
mario@181: delete search_varfb_instance_type;
mario@181: }
mario@181:
mario@181: /* A helper function... */
mario@181: bool search_expression_type_c::is_bool_type(symbol_c *type_symbol) {
mario@181: bool_type_name_c tt;
mario@181: if (type_symbol == NULL) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(boolean_true_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(boolean_false_c)) {return true;}
msousa@257: return false;
mario@181: }
mario@181:
mario@181: /* A helper function... */
mario@181: bool search_expression_type_c::is_time_type(symbol_c *type_symbol) {
mario@181: if (type_symbol == NULL) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safetime_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safedate_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safetod_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safedt_type_name_c)) {return true;}
mario@181: return false;
mario@181: }
mario@181:
mario@181: /* A helper function... */
mario@181: bool search_expression_type_c::is_string_type(symbol_c *type_symbol) {
mario@181: if (type_symbol == NULL) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safestring_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safewstring_type_name_c)) {return true;}
mario@181: return false;
mario@181: }
mario@181:
mario@181: /* A helper function... */
ccb@202: bool search_expression_type_c::is_literal_integer_type(symbol_c *type_symbol) {
ccb@202: if (type_symbol == NULL) {return true;}
ccb@202: if (typeid(*type_symbol) == typeid(integer_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(neg_integer_c)) {return true;}
ccb@202: if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;}
ccb@202: if (typeid(*type_symbol) == typeid(octal_integer_c)) {return true;}
ccb@202: if (typeid(*type_symbol) == typeid(hex_integer_c)) {return true;}
ccb@202: return false;
ccb@202: }
ccb@202:
ccb@202: /* A helper function... */
mario@181: bool search_expression_type_c::is_integer_type(symbol_c *type_symbol) {
mario@181: if (type_symbol == NULL) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safesint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safeint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safedint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safelint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safeusint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safeuint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safeudint_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safeulint_type_name_c)) {return true;}
ccb@202: return is_literal_integer_type(type_symbol);
ccb@202: }
ccb@202:
ccb@202: /* A helper function... */
ccb@202: bool search_expression_type_c::is_literal_real_type(symbol_c *type_symbol) {
ccb@202: if (type_symbol == NULL) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(real_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(neg_real_c)) {return true;}
ccb@202: return false;
ccb@202: }
ccb@202:
ccb@202: /* A helper function... */
mario@181: bool search_expression_type_c::is_real_type(symbol_c *type_symbol) {
mario@181: if (type_symbol == NULL) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safereal_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safelreal_type_name_c)) {return true;}
ccb@202: return is_literal_real_type(type_symbol);
mario@181: }
mario@181:
mario@181: bool search_expression_type_c::is_num_type(symbol_c *type_symbol) {
mario@181: if (type_symbol == NULL) {return true;}
mario@181: return is_real_type(type_symbol) || is_integer_type(type_symbol);
mario@181: }
mario@181:
mario@181: bool search_expression_type_c::is_nbinary_type(symbol_c *type_symbol) {
mario@181: if (type_symbol == NULL) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safebyte_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safeword_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safedword_type_name_c)) {return true;}
msousa@257: if (typeid(*type_symbol) == typeid(safelword_type_name_c)) {return true;}
ccb@202: return is_literal_integer_type(type_symbol);
mario@181: }
mario@181:
mario@181: bool search_expression_type_c::is_binary_type(symbol_c *type_symbol) {
mario@181: if (type_symbol == NULL) {return true;}
msousa@257: // if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
msousa@257: // if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;}
msousa@257: return (is_nbinary_type(type_symbol) || is_bool_type(type_symbol));
mario@181: }
mario@181:
mario@181: bool search_expression_type_c::is_same_type(symbol_c *first_type, symbol_c *second_type) {
mario@181: if (first_type == NULL || second_type == NULL) {return true;}
mario@181: if (typeid(*first_type) == typeid(*second_type)) {return true;}
msousa@257: if (is_bool_type(first_type) && is_bool_type(second_type)) {return true;}
msousa@257: if (is_integer_type(first_type) && is_literal_integer_type(second_type)) {return true;}
msousa@257: if (is_literal_integer_type(first_type) && is_integer_type(second_type)) {return true;}
msousa@257: if (is_binary_type(first_type) && is_literal_integer_type(second_type)) {return true;}
msousa@257: if (is_literal_integer_type(first_type) && is_binary_type(second_type)) {return true;}
msousa@257: if (is_real_type(first_type) && is_literal_real_type(second_type)) {return true;}
msousa@257: if (is_literal_real_type(first_type) && is_real_type(second_type)) {return true;}
mario@181: return false;
mario@181: }
mario@181:
mario@181: symbol_c* search_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) {
mario@181: if (first_type == NULL && second_type == NULL) {return NULL;}
mario@181: if (first_type == NULL) {return second_type;}
mario@181: if (second_type == NULL) {return first_type;}
mario@181: if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
msousa@257: if (is_integer_type(first_type) && is_literal_integer_type(second_type)) {return first_type;}
msousa@257: if (is_literal_integer_type(first_type) && is_integer_type(second_type)) {return second_type;}
msousa@257: if (is_binary_type(first_type) && is_literal_integer_type(second_type)) {return first_type;}
msousa@257: if (is_literal_integer_type(first_type) && is_binary_type(second_type)) {return second_type;}
msousa@257: if (is_real_type(first_type) && is_literal_real_type(second_type)) {return first_type;}
msousa@257: if (is_literal_real_type(first_type) && is_real_type(second_type)) {return second_type;}
ccb@202: return NULL;
ccb@202: }
ccb@202:
laurent@336: symbol_c *search_expression_type_c::default_literal_type(symbol_c* symbol) {
laurent@336: if (is_literal_integer_type(symbol)) {
laurent@336: return (symbol_c *)&search_constant_type_c::lint_type_name;
laurent@336: }
laurent@336: else if (is_literal_real_type(symbol)) {
laurent@336: return (symbol_c *)&search_constant_type_c::lreal_type_name;
laurent@336: }
laurent@336: return symbol;
laurent@336: }
laurent@336:
ccb@202:
ccb@202: integer_c search_expression_type_c::integer("1"); // what default value should we use here ???
mario@181: #include "search_type_code.c"
mario@181:
mario@181: /*static bool_type_name_c bool_type_name;*/
mario@181:
mario@181: /* A helper function... */
mario@181: void *search_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) {
mario@181: if (!is_same_type(left_type, right_type))
mario@181: ERROR;
mario@181: if (!is_bool_type(left_type) && !is_binary_type(left_type))
mario@181: ERROR;
ccb@202: if (is_literal_integer_type(left_type)) {return (void *)right_type;}
mario@181: else {return (void *)left_type;}
mario@181: }
mario@181:
mario@181: /* A helper function... */
mario@181: void *search_expression_type_c::compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) {
mario@181: if (!is_same_type(left_type, right_type))
mario@181: ERROR;
mario@181: if (!is_integer_type(left_type) && !is_real_type(left_type))
mario@181: ERROR;
ccb@202: if (is_literal_integer_type(left_type) || is_literal_real_type(left_type)) {return (void *)right_type;}
mario@181: else {return (void *)left_type;}
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: /* a helper function... */
mario@181: symbol_c *search_expression_type_c::base_type(symbol_c *symbol) {
mario@181: return (symbol_c *)symbol->accept(search_base_type);
mario@181: }
mario@181:
mario@181: /*********************/
mario@181: /* B 1.4 - Variables */
mario@181: /*********************/
mario@181:
mario@181: void *search_expression_type_c::visit(symbolic_variable_c *symbol) {
mario@181: symbol_c *res;
mario@181:
mario@181: /* Nope, now we assume it is a variable, and determine its type... */
msousa@321: res = search_varfb_instance_type->get_basetype_decl(symbol);
mario@181: if (NULL != res) return res;
mario@181:
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: /********************************************/
mario@181: /* B 1.4.1 - Directly Represented Variables */
mario@181: /********************************************/
mario@181: void *search_expression_type_c::visit(direct_variable_c *symbol) {
lbessard@196: symbol_c *res;
lbessard@196:
lbessard@196: /* Nope, now we assume it is a variable, and determine its type... */
msousa@321: res = search_varfb_instance_type->get_basetype_decl(symbol);
lbessard@196: if (NULL != res) return res;
lbessard@196:
lbessard@196: return NULL;
mario@181: }
mario@181:
mario@181: /*************************************/
mario@181: /* B 1.4.2 - Multi-element variables */
mario@181: /*************************************/
mario@181:
mario@181: void *search_expression_type_c::visit(array_variable_c *symbol) {
mario@181: symbol_c *res;
mario@181:
mario@181: /* Nope, now we assume it is a variable, and determine its type... */
msousa@321: res = search_varfb_instance_type->get_basetype_decl(symbol);
mario@181: if (NULL != res) return res;
mario@181:
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(structured_variable_c *symbol) {
mario@181: symbol_c *res;
mario@181:
mario@181: /* Nope, now we assume it is a variable, and determine its type... */
msousa@321: res = search_varfb_instance_type->get_basetype_decl(symbol);
mario@181: if (NULL != res) return res;
mario@181:
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: /***************************************/
mario@181: /* B.3 - Language ST (Structured Text) */
mario@181: /***************************************/
mario@181: /***********************/
mario@181: /* B 3.1 - Expressions */
mario@181: /***********************/
mario@181: void *search_expression_type_c::visit(or_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: return compute_boolean_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(xor_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: return compute_boolean_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(and_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: return compute_boolean_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(equ_expression_c *symbol) {return (void *)&bool_type_name;}
mario@181: void *search_expression_type_c::visit(notequ_expression_c *symbol) {return (void *)&bool_type_name;}
mario@181: void *search_expression_type_c::visit(lt_expression_c *symbol) {return (void *)&bool_type_name;}
mario@181: void *search_expression_type_c::visit(gt_expression_c *symbol) {return (void *)&bool_type_name;}
mario@181: void *search_expression_type_c::visit(le_expression_c *symbol) {return (void *)&bool_type_name;}
mario@181: void *search_expression_type_c::visit(ge_expression_c *symbol) {return (void *)&bool_type_name;}
mario@181:
mario@181: void *search_expression_type_c::visit(add_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;}
mario@181: if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;}
mario@181: if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;}
mario@181: return compute_numeric_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(sub_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;}
mario@181: if (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) {return (void *)&time_type_name;}
mario@181: if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;}
mario@181: if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) {return (void *)&time_type_name;}
mario@181: if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;}
mario@181: if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)) {return (void *)&time_type_name;}
mario@181: return compute_numeric_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(mul_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: if (typeid(*left_type) == typeid(time_type_name_c) && is_num_type(right_type)) {
mario@181: return (void *)&time_type_name;
mario@181: }
mario@181: return compute_numeric_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(div_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: if (typeid(*left_type) == typeid(time_type_name_c) && is_num_type(right_type)){
mario@181: return (void *)&time_type_name;
mario@181: }
mario@181: return compute_numeric_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(mod_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: return compute_numeric_expression(left_type, right_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(power_expression_c *symbol) {
mario@181: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
mario@181: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
mario@181: if (is_real_type(left_type) && is_num_type(right_type)) {
mario@181: return (void *)left_type;
mario@181: }
mario@181: ERROR;
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(neg_expression_c *symbol) {
mario@181: symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this));
mario@181: if (is_num_type(exp_type) || typeid(*exp_type) == typeid(time_type_name_c)){
mario@181: return (void *)exp_type;
mario@181: }
mario@181: ERROR;
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(not_expression_c *symbol) {
mario@181: symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this));
mario@181: return compute_boolean_expression(exp_type, exp_type);
mario@181: }
mario@181:
mario@181: void *search_expression_type_c::visit(function_invocation_c *symbol) {
mario@181: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
mario@181: if (f_decl == function_symtable.end_value()) {
mario@181: void *res = compute_standard_function_default(symbol);
mario@181: if (res == NULL)
mario@181: ERROR;
mario@181: return res;
mario@181: }
mario@181: return base_type(f_decl->type_name);
mario@181: }
mario@181:
mario@181: /*bool_type_name_c search_expression_type_c::bool_type_name;*/
mario@181: