603 return (NULL != common_type__(first_type, second_type)); |
603 return (NULL != common_type__(first_type, second_type)); |
604 } |
604 } |
605 |
605 |
606 |
606 |
607 |
607 |
608 #define is_num_type is_ANY_NUM_compatible |
|
609 #define is_integer_type is_ANY_INT_compatible |
|
610 #define is_real_type is_ANY_REAL_compatible |
|
611 #define is_binary_type is_ANY_BIT_compatible |
|
612 /* actually the ROR, ROL, SHL, and SHR function also accept boolean type! */ |
|
613 #define is_nbinary_type is_ANY_BIT_compatible |
|
614 #define compute_standard_function_default visit_expression_type_c::compute_standard_function_default |
|
615 #define compute_standard_function_il visit_expression_type_c::compute_standard_function_il |
|
616 #define search_expression_type_c visit_expression_type_c |
|
617 #define search(x) search_f(x) |
|
618 #define next() next_nf() |
|
619 // #define search_constant_type_c::constant_int_type_name search_expression_type_c::integer |
|
620 #define constant_int_type_name integer |
|
621 #define is_same_type is_compatible_type |
|
622 #include "../absyntax_utils/search_type_code.c" |
|
623 #undef is_same_type |
|
624 #undef constant_int_type_name |
|
625 // #undef search_constant_type_c::constant_int_type_name |
|
626 #undef next |
|
627 #undef search |
|
628 #undef compute_standard_function_default |
|
629 #undef compute_standard_function_il |
|
630 #undef search_expression_type_c |
|
631 #undef is_real_type |
|
632 #undef is_binary_type |
|
633 #undef is_nbinary_type |
|
634 #undef is_integer_type |
|
635 #undef is_num_type |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
608 |
642 /* A helper function... */ |
609 /* A helper function... */ |
643 /* |
610 /* |
644 symbol_c *visit_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type, |
611 symbol_c *visit_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type, |
645 is_data_type_t is_data_type) { |
612 is_data_type_t is_data_type) { |
672 else |
639 else |
673 return common_type(left_type, right_type); |
640 return common_type(left_type, right_type); |
674 } |
641 } |
675 |
642 |
676 |
643 |
677 # if 0 |
|
678 /* A helper function... */ |
|
679 symbol_c *visit_expression_type_c::compute_numeric_expression(symbol_c *left_type, symbol_c *right_type, |
|
680 is_data_type_t is_data_type) { |
|
681 bool error = false; |
|
682 |
|
683 if (!(this->*is_data_type)(left_type)) { |
|
684 STAGE3_ERROR(left_type, right_type, "Invalid data type of left operand."); |
|
685 error = true; |
|
686 } |
|
687 if (!(this->*is_data_type)(right_type)) { |
|
688 STAGE3_ERROR(left_type, right_type, "Invalid data type of right operand."); |
|
689 error = true; |
|
690 } |
|
691 if (!is_compatible_type(left_type, right_type)) { |
|
692 STAGE3_ERROR(left_type, right_type, "Type mismatch between operands."); |
|
693 error = true; |
|
694 } |
|
695 |
|
696 /* |
|
697 if (is_literal_integer_type(left_type) || is_literal_real_type(left_type)) { |
|
698 return right_type; |
|
699 } else { |
|
700 return left_type; |
|
701 } |
|
702 */ |
|
703 |
|
704 if (error) |
|
705 return NULL; |
|
706 else |
|
707 return common_type(left_type, right_type); |
|
708 |
|
709 /* humour the compiler... */ |
|
710 /* |
|
711 return NULL; |
|
712 */ |
|
713 } |
|
714 #endif |
|
715 |
|
716 |
|
717 |
|
718 |
644 |
719 |
645 |
720 /* A helper function... */ |
646 /* A helper function... */ |
721 /* check the semantics of a FB or Function non-formal call */ |
647 /* check the semantics of a FB or Function non-formal call */ |
722 /* e.g. foo(1, 2, 3, 4); */ |
648 /* e.g. foo(1, 2, 3, 4); */ |
723 void visit_expression_type_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar) { |
649 /* If error_count pointer is != NULL, we do not really print out the errors, |
|
650 * but rather only count how many errors were found. |
|
651 * This is used to support overloaded functions, where we have to check each possible |
|
652 * function, one at a time, untill we find a function call without any errors. |
|
653 */ |
|
654 void visit_expression_type_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar, int *error_count) { |
724 symbol_c *call_param_value, *call_param_type, *param_type; |
655 symbol_c *call_param_value, *call_param_type, *param_type; |
725 identifier_c *param_name; |
656 identifier_c *param_name; |
726 function_param_iterator_c fp_iterator(f_decl); |
657 function_param_iterator_c fp_iterator(f_decl); |
727 function_call_param_iterator_c fcp_iterator(f_call); |
658 function_call_param_iterator_c fcp_iterator(f_call); |
728 |
659 int extensible_parameter_highest_index = -1; |
|
660 |
|
661 /* reset error counter */ |
|
662 if (error_count != NULL) *error_count = 0; |
729 /* if use_il_defvar, then the first parameter for the call comes from the il_default_variable */ |
663 /* if use_il_defvar, then the first parameter for the call comes from the il_default_variable */ |
730 if (use_il_defvar) { |
664 if (use_il_defvar) { |
731 /* The first parameter of the function corresponds to the il_default_variable_type of the function call */ |
665 /* The first parameter of the function corresponds to the il_default_variable_type of the function call */ |
732 do { |
666 do { |
733 param_name = fp_iterator.next(); |
667 param_name = fp_iterator.next(); |
738 * in the function. |
672 * in the function. |
739 */ |
673 */ |
740 } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
674 } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
741 /* If the function does not have any parameters (param_name == NULL) |
675 /* If the function does not have any parameters (param_name == NULL) |
742 * then we cannot compare its type with the il_default_variable_type. |
676 * then we cannot compare its type with the il_default_variable_type. |
|
677 * |
|
678 * However, I (Mario) think this is invalid syntax, as it seems to me all functions must |
|
679 * have at least one parameter. |
|
680 * However, we will make this semantic verification consider it possible, as later |
|
681 * versions of the standard may change that syntax. |
|
682 * So, instead of generating a syntax error message, we simply check whether the call |
|
683 * is passing any more parameters besides the default variable (the il default variable may be ignored |
|
684 * in this case, and not consider it as being a parameter being passed to the function). |
|
685 * If it does, then we have found a semantic error, otherwise the function call is |
|
686 * correct, and we simply return. |
743 */ |
687 */ |
744 if(param_name != NULL) { |
688 if(param_name == NULL) { |
|
689 if (fcp_iterator.next_nf() != NULL) |
|
690 STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call."); |
|
691 return; |
|
692 } else { |
|
693 /* param_name != NULL */ |
745 param_type = fp_iterator.param_type(); |
694 param_type = fp_iterator.param_type(); |
746 if(!is_valid_assignment(param_type, il_default_variable_type)) |
695 if(!is_valid_assignment(param_type, il_default_variable_type)) { |
747 STAGE3_ERROR(f_call, f_call, "In function/FB call, first parameter has invalid data type."); |
696 if (error_count != NULL) (*error_count)++; |
|
697 else STAGE3_ERROR(f_call, f_call, "In function/FB call, first parameter has invalid data type."); |
|
698 } |
|
699 } |
|
700 |
|
701 /* the fisrt parameter (il_def_variable) is correct */ |
|
702 if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) { |
|
703 extensible_parameter_highest_index = fp_iterator.extensible_param_index(); |
748 } |
704 } |
749 } // if (use_il_defvar) |
705 } // if (use_il_defvar) |
|
706 |
|
707 |
750 |
708 |
751 /* Iterating through the non-formal parameters of the function call */ |
709 /* Iterating through the non-formal parameters of the function call */ |
752 while((call_param_value = fcp_iterator.next_nf()) != NULL) { |
710 while((call_param_value = fcp_iterator.next_nf()) != NULL) { |
753 /* Obtaining the type of the value being passed in the function call */ |
711 /* Obtaining the type of the value being passed in the function call */ |
754 call_param_type = base_type((symbol_c*)call_param_value->accept(*this)); |
712 call_param_type = base_type((symbol_c*)call_param_value->accept(*this)); |
755 if (call_param_type == NULL) { |
713 if (call_param_type == NULL) { |
756 STAGE3_ERROR(call_param_value, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
714 if (error_count != NULL) (*error_count)++; |
|
715 /* the following error will usually occur when ST code uses an identifier, that could refer to an enumerated constant, |
|
716 * but was not actually used as a constant in any definitions of an enumerated data type |
|
717 */ |
|
718 else STAGE3_ERROR(call_param_value, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
757 continue; |
719 continue; |
758 } |
720 } |
759 |
721 |
760 /* Iterate to the next parameter of the function being called. |
722 /* Iterate to the next parameter of the function being called. |
761 * Get the name of that parameter, and ignore if EN or ENO. |
723 * Get the name of that parameter, and ignore if EN or ENO. |
762 */ |
724 */ |
763 do { |
725 do { |
764 param_name = fp_iterator.next(); |
726 param_name = fp_iterator.next(); |
765 /* If there is no parameter declared with that name */ |
727 /* If there is no other parameter declared, then we are passing too many parameters... */ |
766 if(param_name == NULL) {STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call."); break;} |
728 if(param_name == NULL) { |
|
729 if (error_count != NULL) (*error_count)++; |
|
730 /* Note: We don't want to print out the follwoing error message multiple times, so we return instead of continuing with 'break' */ |
|
731 else STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call."); return; |
|
732 } |
767 } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
733 } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
768 |
734 |
769 if(param_name != NULL) { |
735 /* Get the parameter type */ |
770 /* Get the parameter type */ |
736 param_type = base_type(fp_iterator.param_type()); |
771 param_type = base_type(fp_iterator.param_type()); |
737 /* If the declared parameter and the parameter from the function call do not have the same type */ |
772 /* If the declared parameter and the parameter from the function call do no have the same type */ |
738 if(!is_valid_assignment(param_type, call_param_type)) { |
773 if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_value, call_param_value, "Type mismatch in function/FB call parameter."); |
739 if (error_count != NULL) (*error_count)++; |
|
740 else STAGE3_ERROR(call_param_value, call_param_value, "Type mismatch in function/FB call parameter."); |
774 } |
741 } |
775 } |
742 |
|
743 if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) { |
|
744 extensible_parameter_highest_index = fp_iterator.extensible_param_index(); |
|
745 } |
|
746 } |
|
747 |
|
748 /* The function call may not have any errors! */ |
|
749 /* In the case of a call to an extensible function, we store the highest index |
|
750 * of the extensible parameters this particular call uses, in the symbol_c object |
|
751 * of the function call itself! |
|
752 * In calls to non-extensible functions, this value will be set to -1. |
|
753 * This information is later used in stage4 to correctly generate the |
|
754 * output code. |
|
755 */ |
|
756 int extensible_param_count = -1; |
|
757 if (extensible_parameter_highest_index >=0) /* if call to extensible function */ |
|
758 extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index(); |
|
759 il_function_call_c *il_function_call = dynamic_cast<il_function_call_c *>(f_call); |
|
760 function_invocation_c *function_invocation = dynamic_cast<function_invocation_c *>(f_call); |
|
761 if (il_function_call != NULL) il_function_call ->extensible_param_count = extensible_param_count; |
|
762 else if (function_invocation != NULL) function_invocation->extensible_param_count = extensible_param_count; |
|
763 // else ERROR; /* this function is also called by Function Blocks, so this is not an error! */ |
776 } |
764 } |
777 |
765 |
778 |
766 |
779 /* check semantics of FB call in the IL language using input operators */ |
767 /* check semantics of FB call in the IL language using input operators */ |
780 /* e.g. CU, CLK, IN, PT, SR, ... */ |
768 /* e.g. CU, CLK, IN, PT, SR, ... */ |
812 |
800 |
813 |
801 |
814 /* A helper function... */ |
802 /* A helper function... */ |
815 /* check the semantics of a FB or Function formal call */ |
803 /* check the semantics of a FB or Function formal call */ |
816 /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ |
804 /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ |
817 void visit_expression_type_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl) { |
805 /* If error_count pointer is != NULL, we do not really print out the errors, |
|
806 * but rather only count how many errors were found. |
|
807 * This is used to support overloaded functions, where we have to check each possible |
|
808 * function, one at a time, untill we find a function call without any errors. |
|
809 */ |
|
810 void visit_expression_type_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl, int *error_count) { |
818 symbol_c *call_param_value, *call_param_type, *call_param_name, *param_type; |
811 symbol_c *call_param_value, *call_param_type, *call_param_name, *param_type; |
819 symbol_c *verify_duplicate_param; |
812 symbol_c *verify_duplicate_param; |
820 identifier_c *param_name; |
813 identifier_c *param_name; |
821 function_param_iterator_c fp_iterator(f_decl); |
814 function_param_iterator_c fp_iterator(f_decl); |
822 function_call_param_iterator_c fcp_iterator(f_call); |
815 function_call_param_iterator_c fcp_iterator(f_call); |
|
816 int extensible_parameter_highest_index = -1; |
|
817 identifier_c *extensible_parameter_name; |
|
818 |
|
819 /* reset error counter */ |
|
820 if (error_count != NULL) *error_count = 0; |
823 |
821 |
824 /* Iterating through the formal parameters of the function call */ |
822 /* Iterating through the formal parameters of the function call */ |
825 while((call_param_name = fcp_iterator.next_f()) != NULL) { |
823 while((call_param_name = fcp_iterator.next_f()) != NULL) { |
826 |
824 |
827 /* Obtaining the value being passed in the function call */ |
825 /* Obtaining the value being passed in the function call */ |
830 if (NULL == call_param_value) ERROR; |
828 if (NULL == call_param_value) ERROR; |
831 |
829 |
832 /* Checking if there are duplicated parameter values */ |
830 /* Checking if there are duplicated parameter values */ |
833 verify_duplicate_param = fcp_iterator.search_f(call_param_name); |
831 verify_duplicate_param = fcp_iterator.search_f(call_param_name); |
834 if(verify_duplicate_param != call_param_value){ |
832 if(verify_duplicate_param != call_param_value){ |
835 STAGE3_ERROR(call_param_name, verify_duplicate_param, "Duplicated parameter values."); |
833 if (error_count != NULL) (*error_count)++; |
|
834 else STAGE3_ERROR(call_param_name, verify_duplicate_param, "Duplicated parameter values."); |
836 } |
835 } |
837 |
836 |
838 /* Obtaining the type of the value being passed in the function call */ |
837 /* Obtaining the type of the value being passed in the function call */ |
839 call_param_type = (symbol_c*)call_param_value->accept(*this); |
838 call_param_type = (symbol_c*)call_param_value->accept(*this); |
840 if (call_param_type == NULL) { |
839 if (call_param_type == NULL) { |
841 STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
840 if (error_count != NULL) (*error_count)++; |
|
841 else STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
842 /* The data value being passed is possibly any enumerated type value. |
842 /* The data value being passed is possibly any enumerated type value. |
843 * We do not yet handle semantic verification of enumerated types. |
843 * We do not yet handle semantic verification of enumerated types. |
844 */ |
844 */ |
845 ERROR; |
845 ERROR; |
846 } |
846 } |
848 if (call_param_type == NULL) STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
848 if (call_param_type == NULL) STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
849 |
849 |
850 /* Find the corresponding parameter of the function being called */ |
850 /* Find the corresponding parameter of the function being called */ |
851 param_name = fp_iterator.search(call_param_name); |
851 param_name = fp_iterator.search(call_param_name); |
852 if(param_name == NULL) { |
852 if(param_name == NULL) { |
853 STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call."); |
853 if (error_count != NULL) (*error_count)++; |
|
854 else STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call."); |
854 } else { |
855 } else { |
855 /* Get the parameter type */ |
856 /* Get the parameter type */ |
856 param_type = base_type(fp_iterator.param_type()); |
857 param_type = base_type(fp_iterator.param_type()); |
857 /* If the declared parameter and the parameter from the function call have the same type */ |
858 /* If the declared parameter and the parameter from the function call have the same type */ |
858 if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_name, call_param_value, "Type mismatch function/FB call parameter."); |
859 if(!is_valid_assignment(param_type, call_param_type)) { |
|
860 if (error_count != NULL) (*error_count)++; |
|
861 else STAGE3_ERROR(call_param_name, call_param_value, "Type mismatch function/FB call parameter."); |
|
862 } |
|
863 if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) { |
|
864 extensible_parameter_highest_index = fp_iterator.extensible_param_index(); |
|
865 extensible_parameter_name = param_name; |
|
866 } |
859 } |
867 } |
860 } |
868 } |
|
869 |
|
870 /* In the case of a call to an extensible function, we store the highest index |
|
871 * of the extensible parameters this particular call uses, in the symbol_c object |
|
872 * of the function call itself! |
|
873 * In calls to non-extensible functions, this value will be set to -1. |
|
874 * This information is later used in stage4 to correctly generate the |
|
875 * output code. |
|
876 */ |
|
877 int extensible_param_count = -1; |
|
878 if (extensible_parameter_highest_index >=0) /* if call to extensible function */ |
|
879 extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index(); |
|
880 il_formal_funct_call_c *il_formal_funct_call = dynamic_cast<il_formal_funct_call_c *>(f_call); |
|
881 function_invocation_c *function_invocation = dynamic_cast<function_invocation_c *>(f_call); |
|
882 if (il_formal_funct_call != NULL) il_formal_funct_call->extensible_param_count = extensible_param_count; |
|
883 else if (function_invocation != NULL) function_invocation->extensible_param_count = extensible_param_count; |
|
884 // else ERROR; /* this function is also called by Function Blocks, so this is not an error! */ |
|
885 |
|
886 /* We have iterated through all the formal parameters of the function call, |
|
887 * and everything seems fine. |
|
888 * If the function being called in an extensible function, we now check |
|
889 * whether the extensible paramters in the formal invocation do not skip |
|
890 * any indexes... |
|
891 * |
|
892 * f(in1:=0, in2:=0, in4:=0) --> ERROR!! |
|
893 */ |
|
894 if (extensible_parameter_highest_index >=0) { /* if call to extensible function */ |
|
895 for (int i=fp_iterator.first_extensible_param_index(); i < extensible_parameter_highest_index; i++) { |
|
896 char tmp[256]; |
|
897 if (snprintf(tmp, 256, "%s%d", extensible_parameter_name->value, i) >= 256) ERROR; |
|
898 if (fcp_iterator.search_f(tmp) == NULL) { |
|
899 /* error in invocation of extensible function */ |
|
900 if (error_count != NULL) (*error_count)++; |
|
901 else STAGE3_ERROR(f_call, f_call, "Missing extensible parameters in call to extensible function."); |
|
902 } |
|
903 } |
|
904 } |
861 } |
905 } |
862 |
906 |
863 |
907 |
864 |
908 |
865 |
909 |
988 //SYM_REF2(il_function_call_c, function_name, il_operand_list) |
1032 //SYM_REF2(il_function_call_c, function_name, il_operand_list) |
989 void *visit_expression_type_c::visit(il_function_call_c *symbol) { |
1033 void *visit_expression_type_c::visit(il_function_call_c *symbol) { |
990 if (il_error) |
1034 if (il_error) |
991 return NULL; |
1035 return NULL; |
992 |
1036 |
|
1037 symbol_c *return_data_type = NULL; |
|
1038 |
993 /* First find the declaration of the function being called! */ |
1039 /* First find the declaration of the function being called! */ |
994 function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
1040 function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name); |
995 |
1041 function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name); |
996 symbol_c *return_data_type = NULL; |
1042 if (lower == function_symtable.end()) ERROR; |
997 |
1043 |
998 if (f_decl == function_symtable.end_value()) { |
1044 int error_count = 0; |
999 function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
1045 int *error_count_ptr = NULL; |
1000 if (current_function_type == function_none) ERROR; |
1046 |
1001 /* This code is for the functions that the user did not declare and that are |
1047 function_symtable_t::iterator second = lower; |
1002 * part of the IL or ST languagem (built-in functions). |
1048 second++; |
1003 * For now we won't do the semantics analysis for that kind of functions. |
1049 if (second != upper) |
1004 */ |
1050 /* This is a call to an overloaded function... */ |
1005 /* |
1051 error_count_ptr = &error_count; |
1006 return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol); |
1052 |
1007 if (NULL == return_data_type) ERROR; |
1053 for(; lower != upper; lower++) { |
1008 |
1054 function_declaration_c *f_decl = function_symtable.get_value(lower); |
1009 function_call_param_iterator_c fcp_iterator(symbol); |
|
1010 |
|
1011 int nb_param = 0; |
|
1012 if (symbol->il_param_list != NULL) |
|
1013 nb_param += ((list_c *)symbol->il_param_list)->n; |
|
1014 |
|
1015 identifier_c en_param_name("EN");*/ |
|
1016 /* Get the value from EN param */ |
|
1017 /*symbol_c *EN_param_value = fcp_iterator.search(&en_param_name); |
|
1018 if (EN_param_value == NULL) |
|
1019 EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); |
|
1020 else |
|
1021 nb_param --; |
|
1022 ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) |
|
1023 |
|
1024 identifier_c eno_param_name("EN0");*/ |
|
1025 /* Get the value from ENO param */ |
|
1026 /*symbol_c *ENO_param_value = fcp_iterator.search(&eno_param_name); |
|
1027 if (ENO_param_value != NULL) |
|
1028 nb_param --; |
|
1029 ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) |
|
1030 |
1055 |
1031 #include "st_code_gen.c" |
1056 check_nonformal_call(symbol, f_decl, true, error_count_ptr); |
1032 */ |
1057 |
1033 } else { |
1058 if (0 == error_count) { |
1034 /* determine the base data type returned by the function being called... */ |
1059 /* Either: |
1035 return_data_type = base_type(f_decl->type_name); |
1060 * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!) |
1036 /* If the following occurs, then we must have some big bug in the syntax parser (stage 2)... */ |
1061 * (ii) we have a call to an overloaded function, with no errors! |
1037 if (NULL == return_data_type) ERROR; |
1062 */ |
1038 |
1063 |
1039 /* check semantics of data passed in the function call... */ |
1064 /* Store the pointer to the declaration of the function being called. |
1040 check_nonformal_call(symbol, f_decl, true); |
1065 * This data will be used by stage 4 to call the correct function. |
1041 |
1066 * Mostly needed to disambiguate overloaded functions... |
1042 /* set the new ddata type of the default variable for the following verifications... */ |
1067 * See comments in absyntax.def for more details |
1043 il_default_variable_type = return_data_type; |
1068 */ |
1044 } |
1069 symbol->called_function_declaration = f_decl; |
|
1070 /* determine the base data type returned by the function being called... */ |
|
1071 return_data_type = base_type(f_decl->type_name); |
|
1072 /* If the following occurs, then we must have some big bug in the syntax parser (stage 2)... */ |
|
1073 if (NULL == return_data_type) ERROR; |
|
1074 /* set the new data type of the default variable for the following verifications... */ |
|
1075 il_default_variable_type = return_data_type; |
|
1076 return NULL; |
|
1077 } |
|
1078 } |
|
1079 |
|
1080 /* No compatible function was found for this function call */ |
|
1081 STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type."); |
1045 return NULL; |
1082 return NULL; |
1046 } |
1083 } |
1047 |
1084 |
1048 |
1085 |
1049 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
1086 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
1159 /* SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) */ |
1196 /* SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) */ |
1160 void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) { |
1197 void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) { |
1161 if (il_error) |
1198 if (il_error) |
1162 return NULL; |
1199 return NULL; |
1163 |
1200 |
1164 function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
1165 |
|
1166 symbol_c *return_data_type = NULL; |
1201 symbol_c *return_data_type = NULL; |
1167 |
1202 function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name); |
1168 if (f_decl == function_symtable.end_value()) { |
1203 function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name); |
|
1204 |
|
1205 if (lower == function_symtable.end()) { |
1169 function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
1206 function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
1170 if (current_function_type == function_none) ERROR; |
1207 if (current_function_type == function_none) ERROR; |
1171 |
1208 return NULL; |
1172 /* This code is for the functions that the user did not declare and that are |
1209 } |
1173 * part of the IL or ST languagem (built-in functions). |
1210 |
1174 * For now we won't do the semantics analysis for that kind of functions. |
1211 int error_count = 0; |
1175 */ |
1212 int *error_count_ptr = NULL; |
1176 #if 0 |
1213 |
1177 return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol); |
1214 function_symtable_t::iterator second = lower; |
1178 if (NULL == return_data_type) ERROR; |
1215 second++; |
1179 |
1216 if (second != upper) |
1180 function_call_param_iterator_c fcp_iterator(symbol); |
1217 /* This is a call to an overloaded function... */ |
1181 |
1218 error_count_ptr = &error_count; |
1182 int nb_param = 0; |
1219 |
1183 if (symbol->il_param_list != NULL) |
1220 for(; lower != upper; lower++) { |
1184 nb_param += ((list_c *)symbol->il_param_list)->n; |
1221 function_declaration_c *f_decl = function_symtable.get_value(lower); |
1185 |
1222 |
1186 identifier_c en_param_name("EN"); |
|
1187 /* Get the value from EN param */ |
|
1188 symbol_c *EN_param_value = fcp_iterator.search(&en_param_name); |
|
1189 if (EN_param_value == NULL) |
|
1190 EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); |
|
1191 else |
|
1192 nb_param --; |
|
1193 ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) |
|
1194 |
|
1195 identifier_c eno_param_name("EN0"); |
|
1196 /* Get the value from ENO param */ |
|
1197 symbol_c *ENO_param_value = fcp_iterator.search(&eno_param_name); |
|
1198 if (ENO_param_value != NULL) |
|
1199 nb_param --; |
|
1200 ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) |
|
1201 |
|
1202 #include "st_code_gen.c" |
|
1203 #endif |
|
1204 } else { |
|
1205 /* determine the base data type returned by the function being called... */ |
|
1206 return_data_type = base_type(f_decl->type_name); |
|
1207 /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */ |
|
1208 if (NULL == return_data_type) ERROR; |
|
1209 |
|
1210 /* check semantics of data passed in the function call... */ |
1223 /* check semantics of data passed in the function call... */ |
1211 check_formal_call(symbol, f_decl); |
1224 check_formal_call(symbol, f_decl, error_count_ptr); |
1212 |
1225 |
1213 /* the data type of the data returned by the function, and stored in the il default variable... */ |
1226 if (0 == error_count) { |
1214 il_default_variable_type = return_data_type; |
1227 /* Either: |
1215 } |
1228 * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!) |
|
1229 * (ii) we have a call to an overloaded function, with no errors! |
|
1230 */ |
|
1231 |
|
1232 /* Store the pointer to the declaration of the function being called. |
|
1233 * This data will be used by stage 4 to call the correct function. |
|
1234 * Mostly needed to disambiguate overloaded functions... |
|
1235 * See comments in absyntax.def for more details |
|
1236 */ |
|
1237 symbol->called_function_declaration = f_decl; |
|
1238 /* determine the base data type returned by the function being called... */ |
|
1239 return_data_type = base_type(f_decl->type_name); |
|
1240 /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */ |
|
1241 if (NULL == return_data_type) ERROR; |
|
1242 /* the data type of the data returned by the function, and stored in the il default variable... */ |
|
1243 il_default_variable_type = return_data_type; |
|
1244 return NULL; |
|
1245 } |
|
1246 } |
|
1247 |
|
1248 /* No compatible function was found for this function call */ |
|
1249 STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type."); |
1216 return NULL; |
1250 return NULL; |
1217 } |
1251 } |
1218 |
1252 |
1219 |
1253 |
1220 #if 0 |
1254 #if 0 |
1727 /***********************/ |
1762 /***********************/ |
1728 |
1763 |
1729 void *visit_expression_type_c::visit(or_expression_c *symbol) { |
1764 void *visit_expression_type_c::visit(or_expression_c *symbol) { |
1730 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1765 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1731 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1766 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1732 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
1767 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp); |
1733 } |
1768 } |
1734 |
1769 |
1735 |
1770 |
1736 void *visit_expression_type_c::visit(xor_expression_c *symbol) { |
1771 void *visit_expression_type_c::visit(xor_expression_c *symbol) { |
1737 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1772 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1738 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1773 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1739 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
1774 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp); |
1740 } |
1775 } |
1741 |
1776 |
1742 |
1777 |
1743 void *visit_expression_type_c::visit(and_expression_c *symbol) { |
1778 void *visit_expression_type_c::visit(and_expression_c *symbol) { |
1744 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1779 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1745 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1780 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1746 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
1781 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp); |
1747 } |
1782 } |
1748 |
1783 |
1749 |
1784 |
1750 void *visit_expression_type_c::visit(equ_expression_c *symbol) { |
1785 void *visit_expression_type_c::visit(equ_expression_c *symbol) { |
1751 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1786 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1752 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1787 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1753 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
1788 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp); |
1754 return &search_expression_type_c::bool_type_name; |
1789 return &search_expression_type_c::bool_type_name; |
1755 } |
1790 } |
1756 |
1791 |
1757 |
1792 |
1758 void *visit_expression_type_c::visit(notequ_expression_c *symbol) { |
1793 void *visit_expression_type_c::visit(notequ_expression_c *symbol) { |
1759 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1794 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1760 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1795 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1761 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
1796 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp); |
1762 return &search_expression_type_c::bool_type_name; |
1797 return &search_expression_type_c::bool_type_name; |
1763 } |
1798 } |
1764 |
1799 |
1765 |
1800 |
1766 void *visit_expression_type_c::visit(lt_expression_c *symbol) { |
1801 void *visit_expression_type_c::visit(lt_expression_c *symbol) { |
1767 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1802 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1768 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1803 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1769 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
1804 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp); |
1770 return &search_expression_type_c::bool_type_name; |
1805 return &search_expression_type_c::bool_type_name; |
1771 } |
1806 } |
1772 |
1807 |
1773 |
1808 |
1774 void *visit_expression_type_c::visit(gt_expression_c *symbol) { |
1809 void *visit_expression_type_c::visit(gt_expression_c *symbol) { |
1775 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1810 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1776 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1811 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1777 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
1812 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp); |
1778 return &search_expression_type_c::bool_type_name; |
1813 return &search_expression_type_c::bool_type_name; |
1779 } |
1814 } |
1780 |
1815 |
1781 |
1816 |
1782 void *visit_expression_type_c::visit(le_expression_c *symbol) { |
1817 void *visit_expression_type_c::visit(le_expression_c *symbol) { |
1783 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1818 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1784 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1819 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1785 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
1820 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp); |
1786 return &search_expression_type_c::bool_type_name; |
1821 return &search_expression_type_c::bool_type_name; |
1787 } |
1822 } |
1788 |
1823 |
1789 |
1824 |
1790 void *visit_expression_type_c::visit(ge_expression_c *symbol) { |
1825 void *visit_expression_type_c::visit(ge_expression_c *symbol) { |
1791 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1826 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1792 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1827 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1793 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
1828 compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp); |
1794 return &search_expression_type_c::bool_type_name; |
1829 return &search_expression_type_c::bool_type_name; |
1795 } |
1830 } |
1796 |
1831 |
1797 |
1832 |
1798 void *visit_expression_type_c::visit(add_expression_c *symbol) { |
1833 void *visit_expression_type_c::visit(add_expression_c *symbol) { |
1920 * this next line is really only to check for integers/reals of undefined type on 'right_type'... |
1955 * this next line is really only to check for integers/reals of undefined type on 'right_type'... |
1921 */ |
1956 */ |
1922 if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) |
1957 if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) |
1923 return (void *)&safetime_type_name; |
1958 return (void *)&safetime_type_name; |
1924 |
1959 |
1925 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible); |
1960 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible, symbol->l_exp, symbol->r_exp); |
1926 } |
1961 } |
1927 |
1962 |
1928 |
1963 |
1929 void *visit_expression_type_c::visit(mod_expression_c *symbol) { |
1964 void *visit_expression_type_c::visit(mod_expression_c *symbol) { |
1930 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1965 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1931 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1966 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
1932 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_INT_compatible); |
1967 return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_INT_compatible, symbol->l_exp, symbol->r_exp); |
1933 } |
1968 } |
1934 |
1969 |
1935 |
1970 |
1936 void *visit_expression_type_c::visit(power_expression_c *symbol) { |
1971 void *visit_expression_type_c::visit(power_expression_c *symbol) { |
1937 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1972 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
1954 } |
1989 } |
1955 |
1990 |
1956 |
1991 |
1957 void *visit_expression_type_c::visit(not_expression_c *symbol) { |
1992 void *visit_expression_type_c::visit(not_expression_c *symbol) { |
1958 symbol_c *type = base_type((symbol_c *)symbol->exp->accept(*this)); |
1993 symbol_c *type = base_type((symbol_c *)symbol->exp->accept(*this)); |
1959 return compute_expression(type, type, &visit_expression_type_c::is_ANY_BIT_compatible); |
1994 return compute_expression(type, type, &visit_expression_type_c::is_ANY_BIT_compatible, NULL, symbol->exp); |
1960 } |
1995 } |
1961 |
1996 |
1962 |
1997 |
1963 void *visit_expression_type_c::visit(function_invocation_c *symbol) { |
1998 void *visit_expression_type_c::visit(function_invocation_c *symbol) { |
1964 function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
1999 function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name); |
1965 if (f_decl == function_symtable.end_value()) { |
2000 function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name); |
1966 /* TODO: the following code is for standard library functions. We do not yet support this... */ |
2001 if (lower == function_symtable.end()) ERROR; |
1967 void *res = compute_standard_function_default(symbol); |
2002 |
1968 if (res != NULL) return res; |
2003 function_symtable_t::iterator second = lower; |
1969 ERROR; |
2004 second++; |
1970 } |
2005 if (second == upper) { |
1971 |
2006 /* call to a function that is not overloaded. */ |
1972 /* now check the semantics of the function call... */ |
2007 /* now check the semantics of the function call... */ |
1973 /* If the syntax parser is working correctly, exactly one of the |
2008 /* If the syntax parser is working correctly, exactly one of the |
1974 * following two symbols will be NULL, while the other is != NULL. |
2009 * following two symbols will be NULL, while the other is != NULL. |
1975 */ |
2010 */ |
1976 if (symbol-> formal_param_list != NULL) check_formal_call (symbol, f_decl); |
2011 function_declaration_c *f_decl = function_symtable.get_value(lower); |
1977 if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl); |
2012 if (symbol-> formal_param_list != NULL) check_formal_call (symbol, f_decl); |
1978 |
2013 if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl); |
1979 return base_type(f_decl->type_name); |
2014 /* Store the pointer to the declaration of the function being called. |
|
2015 * This data will be used by stage 4 to call the correct function. |
|
2016 * Mostly needed to disambiguate overloaded functions... |
|
2017 * See comments in absyntax.def for more details |
|
2018 */ |
|
2019 symbol->called_function_declaration = f_decl; |
|
2020 return base_type(f_decl->type_name); |
|
2021 } |
|
2022 |
|
2023 /* This is a call to an overloaded function... */ |
|
2024 if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!!\n"); |
|
2025 for(; lower != upper; lower++) { |
|
2026 if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!! iterating...\n"); |
|
2027 int error_count = 0; |
|
2028 function_declaration_c *f_decl = function_symtable.get_value(lower); |
|
2029 if (symbol-> formal_param_list != NULL) check_formal_call (symbol, f_decl, &error_count); |
|
2030 if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count); |
|
2031 if (0 == error_count) { |
|
2032 /* Store the pointer to the declaration of the function being called. |
|
2033 * This data will be used by stage 4 to call the correct function. |
|
2034 * Mostly needed to disambiguate overloaded functions... |
|
2035 * See comments in absyntax.def for more details |
|
2036 */ |
|
2037 symbol->called_function_declaration = f_decl; |
|
2038 return base_type(f_decl->type_name); |
|
2039 } |
|
2040 } |
|
2041 |
|
2042 /* No compatible function was found for this function call */ |
|
2043 STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type."); |
|
2044 return NULL; |
1980 } |
2045 } |
1981 |
2046 |
1982 /********************/ |
2047 /********************/ |
1983 /* B 3.2 Statements */ |
2048 /* B 3.2 Statements */ |
1984 /********************/ |
2049 /********************/ |