# HG changeset patch # User Mario de Sousa # Date 1350126829 -3600 # Node ID ca4f17211251bff9354fcbb30a166861aa42e403 # Parent 59fc28d4b06c4715aee421cd98a30e2054fb7236 Move is_datatype_valid/equal() to get_datatype_info_c diff -r 59fc28d4b06c -r ca4f17211251 absyntax_utils/get_datatype_info.cc --- a/absyntax_utils/get_datatype_info.cc Thu Oct 11 19:16:35 2012 +0100 +++ b/absyntax_utils/get_datatype_info.cc Sat Oct 13 12:13:49 2012 +0100 @@ -3,6 +3,7 @@ * * Copyright (C) 2003-2012 Mario de Sousa (msousa@fe.up.pt) * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant + * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -58,6 +59,40 @@ + +bool get_datatype_info_c::is_type_equal(symbol_c *first_type, symbol_c *second_type) { + if ((NULL == first_type) || (NULL == second_type)) + return false; + if (typeid(* first_type) == typeid(invalid_type_name_c)) + return false; + if (typeid(*second_type) == typeid(invalid_type_name_c)) + return false; + + if (get_datatype_info_c::is_ANY_ELEMENTARY(first_type)) { + if (typeid(*first_type) == typeid(*second_type)) + return true; + } else /* ANY_DERIVED */ + return (first_type == second_type); + + return false; +} + + + +bool get_datatype_info_c::is_type_valid(symbol_c *type) { + if (NULL == type) + return false; + if (typeid(*type) == typeid(invalid_type_name_c)) + return false; + + return true; +} + + + + + + bool get_datatype_info_c::is_sfc_initstep(symbol_c *type_symbol) { symbol_c *type_decl = search_base_type.get_basetype_decl(type_symbol); if (typeid(*type_decl) == typeid(initial_step_c)) {return true;} /* INITIAL_STEP step_name ':' action_association_list END_STEP */ /* A pseudo data type! */ @@ -259,6 +294,7 @@ if (type_symbol == NULL) {return false;} if (is_ANY_SAFEREAL(type_symbol)) {return true;} if (is_ANY_SAFEINT (type_symbol)) {return true;} + return false; } @@ -287,6 +323,7 @@ if (type_symbol == NULL) {return false;} if (is_ANY_SAFEREAL (type_symbol)) {return true;} if (is_ANY_signed_SAFEINT(type_symbol)) {return true;} + return false; } diff -r 59fc28d4b06c -r ca4f17211251 absyntax_utils/get_datatype_info.hh --- a/absyntax_utils/get_datatype_info.hh Thu Oct 11 19:16:35 2012 +0100 +++ b/absyntax_utils/get_datatype_info.hh Sat Oct 13 12:13:49 2012 +0100 @@ -57,6 +57,9 @@ public: + static bool is_type_equal(symbol_c *first_type, symbol_c *second_type); + static bool is_type_valid(symbol_c *type); + static bool is_ANY_REAL_literal(symbol_c *type_symbol); /* Can't we do away with this?? */ static bool is_ANY_INT_literal (symbol_c *type_symbol); /* Can't we do away with this?? */ diff -r 59fc28d4b06c -r ca4f17211251 stage3/datatype_functions.cc --- a/stage3/datatype_functions.cc Thu Oct 11 19:16:35 2012 +0100 +++ b/stage3/datatype_functions.cc Sat Oct 13 12:13:49 2012 +0100 @@ -403,7 +403,7 @@ return -1; for(unsigned int i = 0; i < candidate_datatypes.size(); i++) - if (is_type_equal(datatype, candidate_datatypes[i])) + if (get_datatype_info_c::is_type_equal(datatype, candidate_datatypes[i])) return i; /* Not found ! */ return -1; @@ -464,30 +464,3 @@ -bool is_type_equal(symbol_c *first_type, symbol_c *second_type) { - if ((NULL == first_type) || (NULL == second_type)) - return false; - if (typeid(* first_type) == typeid(invalid_type_name_c)) - return false; - if (typeid(*second_type) == typeid(invalid_type_name_c)) - return false; - - if (get_datatype_info_c::is_ANY_ELEMENTARY(first_type)) { - if (typeid(*first_type) == typeid(*second_type)) - return true; - } else /* ANY_DERIVED */ - return (first_type == second_type); - - return false; -} - - - -bool is_type_valid(symbol_c *type) { - if (NULL == type) - return false; - if (typeid(*type) == typeid(invalid_type_name_c)) - return false; - - return true; -} diff -r 59fc28d4b06c -r ca4f17211251 stage3/datatype_functions.hh --- a/stage3/datatype_functions.hh Thu Oct 11 19:16:35 2012 +0100 +++ b/stage3/datatype_functions.hh Sat Oct 13 12:13:49 2012 +0100 @@ -163,10 +163,4 @@ -bool is_type_equal(symbol_c *first_type, symbol_c *second_type); -bool is_type_valid(symbol_c *type); - - - - #endif /* _HELPER_FUNCTIONS_HH_ */ diff -r 59fc28d4b06c -r ca4f17211251 stage3/declaration_check.cc --- a/stage3/declaration_check.cc Thu Oct 11 19:16:35 2012 +0100 +++ b/stage3/declaration_check.cc Sat Oct 13 12:13:49 2012 +0100 @@ -111,7 +111,7 @@ /* For the moment, we will just use search_base_type_c instead... */ symbol_c *glo_type = search_base_type.get_basetype_decl(glo_decl); symbol_c *ext_type = search_base_type.get_basetype_decl(ext_decl); - if (! is_type_equal(glo_type, ext_type)) + if (! get_datatype_info_c::is_type_equal(glo_type, ext_type)) STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error an external redefinition data type."); } } diff -r 59fc28d4b06c -r ca4f17211251 stage3/fill_candidate_datatypes.cc --- a/stage3/fill_candidate_datatypes.cc Thu Oct 11 19:16:35 2012 +0100 +++ b/stage3/fill_candidate_datatypes.cc Sat Oct 13 12:13:49 2012 +0100 @@ -96,7 +96,7 @@ /* If it is an invalid data type, do not insert! * NOTE: it reduces overall code size to do this test here, instead of doing every time before calling the add_datatype_to_candidate_list() function. */ - if (!is_type_valid(datatype)) /* checks for NULL and invalid_type_name_c */ + if (!get_datatype_info_c::is_type_valid(datatype)) /* checks for NULL and invalid_type_name_c */ return false; if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) >= 0) @@ -612,7 +612,7 @@ for (unsigned int u = 0; u < symbol->upper_limit->candidate_datatypes.size(); u++) { for(unsigned int l = 0; l < symbol->lower_limit->candidate_datatypes.size(); l++) { - if (is_type_equal(symbol->upper_limit->candidate_datatypes[u], symbol->lower_limit->candidate_datatypes[l])) + if (get_datatype_info_c::is_type_equal(symbol->upper_limit->candidate_datatypes[u], symbol->lower_limit->candidate_datatypes[l])) add_datatype_to_candidate_list(symbol, symbol->lower_limit->candidate_datatypes[l]); } } @@ -1190,7 +1190,7 @@ for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; operand_type = il_operand->candidate_datatypes[j]; - if (is_type_equal(prev_instruction_type, operand_type)) + if (get_datatype_info_c::is_type_equal(prev_instruction_type, operand_type)) add_datatype_to_candidate_list(symbol, prev_instruction_type); } } @@ -1206,7 +1206,7 @@ for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; operand_type = il_operand->candidate_datatypes[j]; - if (is_type_equal(prev_instruction_type,operand_type) && get_datatype_info_c::is_ANY_BIT_compatible(operand_type)) + if (get_datatype_info_c::is_type_equal(prev_instruction_type,operand_type) && get_datatype_info_c::is_ANY_BIT_compatible(operand_type)) add_datatype_to_candidate_list(symbol, prev_instruction_type); } } @@ -1244,7 +1244,7 @@ * the prev_instruction_type MUST be BOOL compatible. * I am not too sure about operand_type, does it have to be BOOL compatible, or can it be ANY_BIT compatible? Must check! */ - if (is_type_equal(prev_instruction_type,operand_type) && get_datatype_info_c::is_BOOL_compatible(operand_type)) + if (get_datatype_info_c::is_type_equal(prev_instruction_type,operand_type) && get_datatype_info_c::is_BOOL_compatible(operand_type)) add_datatype_to_candidate_list(symbol, prev_instruction_type); } } @@ -1266,7 +1266,7 @@ * the prev_instruction_type MUST be BOOL compatible. * I am not too sure about operand_type, does it have to be BOOL compatible, or can it be ANY_BIT compatible? Must check! */ - if (is_type_equal(prev_instruction_type,operand_type) && get_datatype_info_c::is_BOOL_compatible(operand_type)) + if (get_datatype_info_c::is_type_equal(prev_instruction_type,operand_type) && get_datatype_info_c::is_BOOL_compatible(operand_type)) add_datatype_to_candidate_list(symbol, prev_instruction_type); } } @@ -1458,7 +1458,7 @@ for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { left_type = symbol->l_exp->candidate_datatypes[i]; right_type = symbol->r_exp->candidate_datatypes[j]; - if (is_type_equal(left_type, right_type)) + if (get_datatype_info_c::is_type_equal(left_type, right_type)) add_datatype_to_candidate_list(symbol, left_type); } } diff -r 59fc28d4b06c -r ca4f17211251 stage3/narrow_candidate_datatypes.cc --- a/stage3/narrow_candidate_datatypes.cc Thu Oct 11 19:16:35 2012 +0100 +++ b/stage3/narrow_candidate_datatypes.cc Sat Oct 13 12:13:49 2012 +0100 @@ -96,7 +96,7 @@ symbol->datatype = datatype; else { /* had already been set previously to some data type. Let's check if they are the same! */ - if (!is_type_equal(symbol->datatype, datatype)) + if (!get_datatype_info_c::is_type_equal(symbol->datatype, datatype)) symbol->datatype = &(search_constant_type_c::invalid_type_name); // else /* we leave it unchanged, as it is the same as the requested data type! */ @@ -273,7 +273,7 @@ /* set the called_function_declaration taking into account the datatype that we need to return */ for(unsigned int i = 0; i < fcall->candidate_datatypes.size(); i++) { - if (is_type_equal(fcall->candidate_datatypes[i], fcall->datatype)) { + if (get_datatype_info_c::is_type_equal(fcall->candidate_datatypes[i], fcall->datatype)) { fcall_data.called_function_declaration = fcall_data.candidate_functions[i]; break; } @@ -398,7 +398,7 @@ * FB call for any datatype. In that case, then the datatype required to pass to the first parameter of the * FB call must be left unchanged! */ - if ((NULL == il_instruction->datatype) || (is_type_equal(param_value.datatype, il_instruction->datatype))) { + if ((NULL == il_instruction->datatype) || (get_datatype_info_c::is_type_equal(param_value.datatype, il_instruction->datatype))) { set_datatype_in_prev_il_instructions(param_value.datatype, fake_prev_il_instruction); } else { set_datatype_in_prev_il_instructions(&search_constant_type_c::invalid_type_name, fake_prev_il_instruction); @@ -1096,7 +1096,7 @@ } } // if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */ - if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR; + if (get_datatype_info_c::is_type_valid(symbol->datatype) && (count <= 0)) ERROR; l_expr->accept(*this); r_expr->accept(*this); @@ -1278,7 +1278,7 @@ void *narrow_candidate_datatypes_c::visit(case_list_c *symbol) { for (int i = 0; i < symbol->n; i++) { for (unsigned int k = 0; k < symbol->elements[i]->candidate_datatypes.size(); k++) { - if (is_type_equal(symbol->datatype, symbol->elements[i]->candidate_datatypes[k])) + if (get_datatype_info_c::is_type_equal(symbol->datatype, symbol->elements[i]->candidate_datatypes[k])) symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[k]; } /* NOTE: this may be an integer, a subrange_c, or a enumerated value! */ @@ -1301,7 +1301,7 @@ symbol->control_variable->accept(*this); /* BEG expression */ for(unsigned int i = 0; i < symbol->beg_expression->candidate_datatypes.size(); i++) { - if (is_type_equal(symbol->control_variable->datatype,symbol->beg_expression->candidate_datatypes[i]) && + if (get_datatype_info_c::is_type_equal(symbol->control_variable->datatype,symbol->beg_expression->candidate_datatypes[i]) && get_datatype_info_c::is_ANY_INT(symbol->beg_expression->candidate_datatypes[i])) { symbol->beg_expression->datatype = symbol->beg_expression->candidate_datatypes[i]; } @@ -1309,7 +1309,7 @@ symbol->beg_expression->accept(*this); /* END expression */ for(unsigned int i = 0; i < symbol->end_expression->candidate_datatypes.size(); i++) { - if (is_type_equal(symbol->control_variable->datatype,symbol->end_expression->candidate_datatypes[i]) && + if (get_datatype_info_c::is_type_equal(symbol->control_variable->datatype,symbol->end_expression->candidate_datatypes[i]) && get_datatype_info_c::is_ANY_INT(symbol->end_expression->candidate_datatypes[i])) { symbol->end_expression->datatype = symbol->end_expression->candidate_datatypes[i]; } @@ -1318,7 +1318,7 @@ /* BY expression */ if (NULL != symbol->by_expression) { for(unsigned int i = 0; i < symbol->by_expression->candidate_datatypes.size(); i++) { - if (is_type_equal(symbol->control_variable->datatype,symbol->by_expression->candidate_datatypes[i]) && + if (get_datatype_info_c::is_type_equal(symbol->control_variable->datatype,symbol->by_expression->candidate_datatypes[i]) && get_datatype_info_c::is_ANY_INT(symbol->by_expression->candidate_datatypes[i])) { symbol->by_expression->datatype = symbol->by_expression->candidate_datatypes[i]; } diff -r 59fc28d4b06c -r ca4f17211251 stage3/print_datatypes_error.cc --- a/stage3/print_datatypes_error.cc Thu Oct 11 19:16:35 2012 +0100 +++ b/stage3/print_datatypes_error.cc Sat Oct 13 12:13:49 2012 +0100 @@ -109,10 +109,10 @@ bool res; if (symbol->prev_il_instruction.size() > 0) - res = is_type_valid(symbol->prev_il_instruction[0]->datatype); + res = get_datatype_info_c::is_type_valid(symbol->prev_il_instruction[0]->datatype); for (unsigned int i = 1; i < symbol->prev_il_instruction.size(); i++) - res &= is_type_equal(symbol->prev_il_instruction[i-1]->datatype, symbol->prev_il_instruction[i]->datatype); + res &= get_datatype_info_c::is_type_equal(symbol->prev_il_instruction[i-1]->datatype, symbol->prev_il_instruction[i]->datatype); return res; } @@ -250,14 +250,14 @@ */ for (unsigned int p = 0; p < il_instruction_symbol->prev_il_instruction.size(); p++) { symbol_c *value = il_instruction_symbol->prev_il_instruction[p]; - if (!is_type_valid(value->datatype)) { + if (!get_datatype_info_c::is_type_valid(value->datatype)) { function_invocation_error = true; STAGE3_ERROR(0, fcall, fcall, "Data type incompatibility for value passed to first parameter when invoking function '%s'", ((identifier_c *)fcall_data.function_name)->value); STAGE3_ERROR(0, value, value, "This is the IL instruction producing the incompatible data type to first parameter of function '%s'", ((identifier_c *)fcall_data.function_name)->value); } } #else - if (!is_type_valid(il_instruction_symbol->datatype)) { + if (!get_datatype_info_c::is_type_valid(il_instruction_symbol->datatype)) { function_invocation_error = true; STAGE3_ERROR(0, fcall, fcall, "Data type incompatibility between value in IL 'accumulator' and first parameter of function '%s'", ((identifier_c *)fcall_data.function_name)->value); } @@ -266,7 +266,7 @@ /* when handling a IL function call, and an error is found in the first parameter, then we bug out and do not print out any more error messages. */ return; } - else if (!is_type_valid(param_value->datatype)) { + else if (!get_datatype_info_c::is_type_valid(param_value->datatype)) { function_invocation_error = true; STAGE3_ERROR(0, param_value, param_value, "Data type incompatibility for value passed in position %d when invoking %s '%s'", i, POU_str, ((identifier_c *)fcall_data.function_name)->value); } @@ -537,12 +537,12 @@ /* B 1.3.3 - Derived data types */ /********************************/ void *print_datatypes_error_c::visit(simple_spec_init_c *symbol) { - if (!is_type_valid(symbol->simple_specification->datatype)) { + if (!get_datatype_info_c::is_type_valid(symbol->simple_specification->datatype)) { STAGE3_ERROR(0, symbol->simple_specification, symbol->simple_specification, "Invalid data type."); } else if (NULL != symbol->constant) { - if (!is_type_valid(symbol->constant->datatype)) + if (!get_datatype_info_c::is_type_valid(symbol->constant->datatype)) STAGE3_ERROR(0, symbol->constant, symbol->constant, "Initial value has incompatible data type."); - } else if (!is_type_valid(symbol->datatype)) { + } else if (!get_datatype_info_c::is_type_valid(symbol->datatype)) { ERROR; /* If we have an error here, then we must also have an error in one of * the two previous tests. If we reach this point, some strange error is ocurring! */ @@ -577,7 +577,7 @@ /********************************************/ void *print_datatypes_error_c::visit(direct_variable_c *symbol) { if (symbol->candidate_datatypes.size() == 0) ERROR; - if (!is_type_valid(symbol->datatype)) + if (!get_datatype_info_c::is_type_valid(symbol->datatype)) STAGE3_ERROR(4, symbol, symbol, "Direct variable has incompatible data type with expression."); return NULL; } @@ -648,7 +648,7 @@ symbol->located_var_spec_init->accept(*this); /* It does not make sense to call symbol->location->accept(*this). The check is done right here if the following if() */ // symbol->location->accept(*this); - if ((is_type_valid(symbol->located_var_spec_init->datatype)) && (!is_type_valid(symbol->location->datatype))) + if ((get_datatype_info_c::is_type_valid(symbol->located_var_spec_init->datatype)) && (!get_datatype_info_c::is_type_valid(symbol->location->datatype))) STAGE3_ERROR(0, symbol, symbol, "Bit size of data type is incompatible with bit size of location."); return NULL; } @@ -1264,7 +1264,7 @@ void *print_datatypes_error_c::visit(while_statement_c *symbol) { symbol->expression->accept(*this); - if (!is_type_valid(symbol->expression->datatype)) { + if (!get_datatype_info_c::is_type_valid(symbol->expression->datatype)) { STAGE3_ERROR(0, symbol, symbol, "Invalid data type for 'WHILE' condition."); return NULL; } @@ -1274,7 +1274,7 @@ } void *print_datatypes_error_c::visit(repeat_statement_c *symbol) { - if (!is_type_valid(symbol->expression->datatype)) { + if (!get_datatype_info_c::is_type_valid(symbol->expression->datatype)) { STAGE3_ERROR(0, symbol, symbol, "Invalid data type for 'REPEAT' condition."); return NULL; }