mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 mario@181: * msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant mario@181: * 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: /* Determine the data type of a specific constant or variable. mario@181: * A reference to the relevant type definition is returned. mario@181: * mario@181: * For example: mario@181: * 22 -> returns reference to a int_type_name_c object. mario@181: * 22.2 -> returns reference to a real_type_name_c object. mario@181: * LREAL#22.2 -> returns reference to a lreal_type_name_c object. mario@181: * etc... mario@181: */ mario@181: mario@181: laurent@328: #include "../util/symtable.hh" mario@181: #include "search_constant_type.hh" msousa@339: #include "absyntax_utils.hh" mario@181: laurent@328: mario@181: #define ERROR error_exit(__FILE__,__LINE__) mario@181: /* function defined in main.cc */ mario@181: extern void error_exit(const char *file_name, int line_no); mario@181: mario@181: symbol_c *search_constant_type_c::get_type(symbol_c *constant) { mario@181: return (symbol_c *)constant->accept(*this); mario@181: } mario@181: mario@181: mario@181: /*********************/ mario@181: /* B 1.2 - Constants */ mario@181: /*********************/ mario@181: mario@181: /******************************/ mario@181: /* B 1.2.1 - Numeric Literals */ mario@181: /******************************/ ccb@202: /* Numeric literals without any explicit type cast have unknown data type, ccb@202: * so we continue considering them as their own basic data types until ccb@202: * they can be resolved (for example, when using '30+x' where 'x' is a LINT variable, the ccb@202: * numeric literal '30' must then be considered a LINT so the ADD function may be called ccb@202: * with all inputs of the same data type. ccb@202: * If 'x' were a SINT, then the '30' would have to be a SINT too! ccb@202: */ ccb@202: void *search_constant_type_c::visit(real_c *symbol) {return (void *)symbol;} msousa@257: void *search_constant_type_c::visit(neg_real_c *symbol) {return (void *)symbol;} ccb@202: void *search_constant_type_c::visit(integer_c *symbol) {return (void *)symbol;} msousa@257: void *search_constant_type_c::visit(neg_integer_c *symbol) {return (void *)symbol;} ccb@202: void *search_constant_type_c::visit(binary_integer_c *symbol) {return (void *)symbol;} ccb@202: void *search_constant_type_c::visit(octal_integer_c *symbol) {return (void *)symbol;} ccb@202: void *search_constant_type_c::visit(hex_integer_c *symbol) {return (void *)symbol;} mario@181: mario@181: void *search_constant_type_c::visit(integer_literal_c *symbol) mario@181: {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} mario@181: void *search_constant_type_c::visit(real_literal_c *symbol) mario@181: {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} mario@181: void *search_constant_type_c::visit(bit_string_literal_c *symbol) mario@181: {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} mario@181: void *search_constant_type_c::visit(boolean_literal_c *symbol) mario@181: {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));} mario@181: ccb@202: void *search_constant_type_c::visit(boolean_true_c *symbol) {return (void *)symbol;} ccb@202: void *search_constant_type_c::visit(boolean_false_c *symbol) {return (void *)symbol;} ccb@202: ccb@202: mario@181: /*******************************/ mario@181: /* B.1.2.2 Character Strings */ mario@181: /*******************************/ mario@181: void *search_constant_type_c::visit(double_byte_character_string_c *symbol) {return (void *)&wstring_type_name;} mario@181: void *search_constant_type_c::visit(single_byte_character_string_c *symbol) {return (void *)&string_type_name;} mario@181: mario@181: /***************************/ mario@181: /* B 1.2.3 - Time Literals */ mario@181: /***************************/ mario@181: /************************/ mario@181: /* B 1.2.3.1 - Duration */ mario@181: /************************/ mario@181: void *search_constant_type_c::visit(neg_time_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ msousa@257: void *search_constant_type_c::visit(duration_c *symbol) {return (void *)(symbol->type_name);} mario@181: void *search_constant_type_c::visit(fixed_point_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ msousa@547: void *search_constant_type_c::visit(interval_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ mario@181: mario@181: /************************************/ mario@181: /* B 1.2.3.2 - Time of day and Date */ mario@181: /************************************/ msousa@257: void *search_constant_type_c::visit(time_of_day_c *symbol) {return (void *)(symbol->type_name);} mario@181: void *search_constant_type_c::visit(daytime_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ msousa@257: void *search_constant_type_c::visit(date_c *symbol) {return (void *)(symbol->type_name);} mario@181: void *search_constant_type_c::visit(date_literal_c *symbol) {ERROR; return NULL;} /* this member function should never be called. */ msousa@257: void *search_constant_type_c::visit(date_and_time_c *symbol) {return (void *)(symbol->type_name);} msousa@257: laurent@328: /********************************/ laurent@328: /* B 1.3.3 - Derived data types */ laurent@328: /********************************/ laurent@328: void *search_constant_type_c::visit(enumerated_value_c *symbol) { msousa@386: if (symbol->type != NULL) msousa@386: return (void *)(symbol->type); laurent@328: msousa@386: symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value); msousa@386: if (value_type == enumerated_value_symtable.end_value()) msousa@386: return NULL; msousa@386: return (void *)value_type; laurent@328: } mario@181: msousa@459: msousa@459: msousa@459: msousa@459: invalid_type_name_c search_constant_type_c::invalid_type_name; msousa@459: msousa@459: mario@181: real_type_name_c search_constant_type_c::real_type_name; mario@181: sint_type_name_c search_constant_type_c::sint_type_name; mario@181: lint_type_name_c search_constant_type_c::lint_type_name; mario@181: dint_type_name_c search_constant_type_c::dint_type_name; mario@181: date_type_name_c search_constant_type_c::date_type_name; ccb@202: dword_type_name_c search_constant_type_c::dword_type_name; ccb@202: dt_type_name_c search_constant_type_c::dt_type_name; ccb@202: tod_type_name_c search_constant_type_c::tod_type_name; ccb@202: udint_type_name_c search_constant_type_c::udint_type_name; mario@181: word_type_name_c search_constant_type_c::word_type_name; ccb@202: wstring_type_name_c search_constant_type_c::wstring_type_name; ccb@202: string_type_name_c search_constant_type_c::string_type_name; ccb@202: lword_type_name_c search_constant_type_c::lword_type_name; mario@181: uint_type_name_c search_constant_type_c::uint_type_name; ccb@202: lreal_type_name_c search_constant_type_c::lreal_type_name; mario@181: byte_type_name_c search_constant_type_c::byte_type_name; ccb@202: usint_type_name_c search_constant_type_c::usint_type_name; ccb@202: ulint_type_name_c search_constant_type_c::ulint_type_name; mario@181: bool_type_name_c search_constant_type_c::bool_type_name; mario@181: time_type_name_c search_constant_type_c::time_type_name; ccb@202: int_type_name_c search_constant_type_c::int_type_name; mario@181: msousa@257: safetime_type_name_c search_constant_type_c::safetime_type_name; msousa@257: safetod_type_name_c search_constant_type_c::safetod_type_name; msousa@257: safedt_type_name_c search_constant_type_c::safedt_type_name; msousa@417: safedate_type_name_c search_constant_type_c::safedate_type_name; msousa@417: safereal_type_name_c search_constant_type_c::safereal_type_name; msousa@417: safesint_type_name_c search_constant_type_c::safesint_type_name; msousa@417: safelint_type_name_c search_constant_type_c::safelint_type_name; msousa@417: safedint_type_name_c search_constant_type_c::safedint_type_name; msousa@417: safedword_type_name_c search_constant_type_c::safedword_type_name; msousa@417: safeudint_type_name_c search_constant_type_c::safeudint_type_name; msousa@417: safeword_type_name_c search_constant_type_c::safeword_type_name; msousa@417: safewstring_type_name_c search_constant_type_c::safewstring_type_name; msousa@417: safestring_type_name_c search_constant_type_c::safestring_type_name; msousa@417: safelword_type_name_c search_constant_type_c::safelword_type_name; msousa@417: safeuint_type_name_c search_constant_type_c::safeuint_type_name; msousa@417: safelreal_type_name_c search_constant_type_c::safelreal_type_name; msousa@417: safebyte_type_name_c search_constant_type_c::safebyte_type_name; msousa@417: safeusint_type_name_c search_constant_type_c::safeusint_type_name; msousa@417: safeulint_type_name_c search_constant_type_c::safeulint_type_name; msousa@417: safebool_type_name_c search_constant_type_c::safebool_type_name; msousa@417: safeint_type_name_c search_constant_type_c::safeint_type_name; msousa@257: msousa@257: