stage4/generate_cc/search_expression_type.cc
changeset 32 289256ec66f1
parent 22 08bcc40be1fa
child 33 4507beb2aac6
equal deleted inserted replaced
31:c6959b0f539d 32:289256ec66f1
    29  * For example:
    29  * For example:
    30  *       2 + 3       -> returns reference to a int_type_name_c object.
    30  *       2 + 3       -> returns reference to a int_type_name_c object.
    31  *       22.2 - 5    -> returns reference to a real_type_name_c object.
    31  *       22.2 - 5    -> returns reference to a real_type_name_c object.
    32  *       etc...
    32  *       etc...
    33  */
    33  */
       
    34 
       
    35 typedef enum {function_add,
       
    36               function_sub,
       
    37               function_and,
       
    38               function_or,
       
    39               function_sqrt,
       
    40               function_none} function_type_t;
       
    41 
       
    42 
       
    43 function_type_t get_function_type(identifier_c *function_name) {
       
    44   if (!strcasecmp(function_name->value, "ADD"))
       
    45     return function_add;
       
    46   else if (!strcasecmp(function_name->value, "SUB"))
       
    47     return function_sub;
       
    48   else if (!strcasecmp(function_name->value, "AND"))
       
    49     return function_and;
       
    50   else if (!strcasecmp(function_name->value, "OR"))
       
    51     return function_or;
       
    52   else if (!strcasecmp(function_name->value, "SQRT"))
       
    53     return function_sqrt;
       
    54   else return function_none;
       
    55 }
       
    56 
       
    57 
       
    58 symbol_c *generate_param_name(const char *name) {
       
    59   symbol_c *param_name = new identifier_c(name);
       
    60   return param_name;
       
    61 }
       
    62 
       
    63 
       
    64 symbol_c *generate_param_name(const char *format, int number) {
       
    65   char name[10];
       
    66   sprintf(name, format, number);
       
    67   symbol_c *param_name = new identifier_c(name);
       
    68   return param_name;
       
    69 }
       
    70 
       
    71 
    34 class search_expression_type_c: public search_constant_type_c {
    72 class search_expression_type_c: public search_constant_type_c {
    35   private:
    73   private:
    36     search_varfb_instance_type_c *search_varfb_instance_type;
    74     search_varfb_instance_type_c *search_varfb_instance_type;
    37     search_base_type_c search_base_type;
    75     search_base_type_c search_base_type;
    38 
    76 
    49     bool is_bool_type(symbol_c *type_symbol) {
    87     bool is_bool_type(symbol_c *type_symbol) {
    50       return (typeid(*type_symbol) == typeid(bool_type_name_c));
    88       return (typeid(*type_symbol) == typeid(bool_type_name_c));
    51     }
    89     }
    52 
    90 
    53     /* A helper function... */
    91     /* A helper function... */
    54     bool is_time_compatible(symbol_c *type_symbol) {
    92     bool is_time_type(symbol_c *type_symbol) {
    55       if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;}
    93       if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;}
    56       if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;}
    94       if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;}
    57       if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;}
    95       if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;}
    58       if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;}
    96       if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;}
    59       return false;
    97       return false;
    60     }
    98     }
    61 
    99 
    62     /* A helper function... */
   100     /* A helper function... */
    63     bool is_numeric_compatible(symbol_c *type_symbol) {
   101     bool is_integer_type(symbol_c *type_symbol) {
    64       if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;}
   102       if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;}
    65       if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;}
   103       if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;}
    66       if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;}
   104       if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;}
    67       if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;}
   105       if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;}
    68       if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
   106       if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
    69       if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;}
   107       if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;}
    70       if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
   108       if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
    71       if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
   109       if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
       
   110       if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;}
       
   111       return false;
       
   112     }
       
   113 
       
   114     bool is_real_type(symbol_c *type_symbol) {
    72       if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;}
   115       if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;}
    73       if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
   116       if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
       
   117       if (typeid(*type_symbol) == typeid(constant_real_type_name_c)) {return true;}
       
   118       return false;
       
   119     }
       
   120 
       
   121     bool is_num_type(symbol_c *type_symbol) {
       
   122       return is_real_type(type_symbol) || is_integer_type(type_symbol);
       
   123     }
       
   124 
       
   125     bool is_binary_type(symbol_c *type_symbol) {
    74       if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
   126       if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
    75       if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;}
   127       if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;}
    76       if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;}
   128       if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;}
    77       if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;}
   129       if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;}
    78       if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;}
   130       if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;}
       
   131       if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;}
    79       return false;
   132       return false;
    80     }
   133     }
    81     
   134     
       
   135     bool is_same_type(symbol_c *first_type, symbol_c *second_type) {
       
   136       if (typeid(*first_type) == typeid(*second_type)) {return true;}
       
   137       if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;}
       
   138       if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_integer_type(second_type))) {return true;}
       
   139       if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;}
       
   140       if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_binary_type(second_type))) {return true;}
       
   141       if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return true;}
       
   142       if ((typeid(*first_type) == typeid(constant_real_type_name_c) && is_real_type(second_type))) {return true;}
       
   143       return false;
       
   144     }
       
   145 
       
   146     symbol_c* common_type(symbol_c *first_type, symbol_c *second_type) {
       
   147       if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
       
   148       if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return first_type;}
       
   149       if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_integer_type(second_type))) {return second_type;}
       
   150       if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return first_type;}
       
   151       if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_binary_type(second_type))) {return second_type;}
       
   152       if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return first_type;}
       
   153       if ((typeid(*first_type) == typeid(constant_real_type_name_c) && is_real_type(second_type))) {return second_type;}
       
   154       return NULL;
       
   155     }
       
   156     
    82   private:
   157   private:
       
   158 
       
   159   void *compute_standard_function_st(function_invocation_c *symbol) {
       
   160     symbol_c *current_type = NULL;
       
   161     symbol_c *return_type = NULL;
       
   162     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
       
   163     
       
   164     function_call_param_iterator_c function_call_param_iterator(symbol);
       
   165     
       
   166     for(int current_param = 0; current_param < ((list_c *)symbol->parameter_assignment_list)->n; current_param++) {
       
   167       symbol_c *param_name = NULL;
       
   168       switch (current_function_type) {
       
   169         case function_add:
       
   170         case function_and:
       
   171         case function_or:
       
   172           param_name = generate_param_name("IN%d", current_param + 1);
       
   173           break;
       
   174         case function_sub:
       
   175           if (current_param < 2)
       
   176             param_name = generate_param_name("IN%d", current_param + 1);
       
   177           else ERROR;
       
   178           break;
       
   179         default: ERROR;
       
   180       }
       
   181       
       
   182       /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   183       symbol_c *param_value = function_call_param_iterator.search(param_name);
       
   184       delete param_name;
       
   185       
       
   186       /* Get the value from a foo(<param_value>) style call */
       
   187       if (param_value == NULL)
       
   188         param_value = function_call_param_iterator.next();
       
   189       
       
   190       if (param_value == NULL) ERROR;
       
   191       
       
   192       symbol_c *param_type = (symbol_c *)param_value->accept(*this);
       
   193       
       
   194       switch (current_function_type) {
       
   195         case function_add:
       
   196           if (current_param == 0)
       
   197             current_type = param_type;
       
   198           else if (current_param == 1) {
       
   199             if ((is_integer_type(current_type) && is_same_type(current_type, param_type)) ||
       
   200                 (is_real_type(current_type) && is_same_type(current_type, param_type))) {
       
   201               current_type = common_type(current_type, param_type);
       
   202               return_type = current_type;
       
   203             }
       
   204             else if (is_time_type(current_type)) {
       
   205               if ((typeid(*current_type) == typeid(time_type_name_c)) && (typeid(*param_type) == typeid(time_type_name_c))) {return_type = (symbol_c *)&time_type_name;}
       
   206               else if (typeid(*current_type) == typeid(tod_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&tod_type_name;}
       
   207               else if (typeid(*current_type) == typeid(dt_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&dt_type_name;}
       
   208               else ERROR;
       
   209             }
       
   210             else ERROR;
       
   211           }
       
   212           else if (!is_time_type(current_type) && is_same_type(current_type, param_type)) {
       
   213             current_type = common_type(current_type, param_type);
       
   214             return_type = current_type;
       
   215           }
       
   216           else ERROR;
       
   217           break;
       
   218         case function_sub:
       
   219           if (current_param == 0)
       
   220             current_type = param_type;
       
   221           else if (current_param == 1) {
       
   222             if ((is_integer_type(current_type) && is_same_type(current_type, param_type)) ||
       
   223                 (is_real_type(current_type) && is_same_type(current_type, param_type)))
       
   224               return_type = common_type(current_type, param_type);
       
   225             else if (is_time_type(current_type)) {
       
   226               if (typeid(*current_type) == typeid(time_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
       
   227               else if (typeid(*current_type) == typeid(date_type_name_c) && typeid(*param_type) == typeid(date_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
       
   228               else if (typeid(*current_type) == typeid(tod_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&tod_type_name;}
       
   229               else if (typeid(*current_type) == typeid(tod_type_name_c) && typeid(*param_type) == typeid(tod_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
       
   230               else if (typeid(*current_type) == typeid(dt_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&dt_type_name;}
       
   231               else if (typeid(*current_type) == typeid(dt_type_name_c) && typeid(*param_type) == typeid(dt_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
       
   232               else ERROR;
       
   233             }
       
   234             else ERROR;
       
   235           }
       
   236           else ERROR;
       
   237           break;
       
   238         case function_and:
       
   239         case function_or:
       
   240           if (current_param == 0)
       
   241             if (is_binary_type(param_type))
       
   242               current_type = param_type;
       
   243             else ERROR;
       
   244           else if (is_same_type(current_type, param_type))
       
   245             return_type = common_type(current_type, param_type);
       
   246           else ERROR;
       
   247           break;
       
   248         default: ERROR;
       
   249       }
       
   250     }
       
   251     
       
   252     return (void *)return_type;
       
   253   }
       
   254 
       
   255   public:
       
   256 
       
   257   void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_type) {
       
   258     symbol_c *current_type = NULL;
       
   259     symbol_c *return_type = NULL;
       
   260     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
       
   261     if (current_function_type == function_none) ERROR;
       
   262     
       
   263     function_call_param_iterator_c function_call_param_iterator(symbol);
       
   264     
       
   265     int nb_param = 1;
       
   266     if (symbol->il_operand_list != NULL)
       
   267       nb_param += ((list_c *)symbol->il_operand_list)->n;
       
   268 
       
   269     for(int current_param = 0; current_param < nb_param; current_param++) {
       
   270       
       
   271       if (current_param != 0) {
       
   272         symbol_c *param_name = NULL;
       
   273         switch (current_function_type) {
       
   274           default: ERROR;
       
   275         }
       
   276         
       
   277         /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   278         symbol_c *param_value = function_call_param_iterator.search(param_name);
       
   279         delete param_name;
       
   280         
       
   281         /* Get the value from a foo(<param_value>) style call */
       
   282         if (param_value == NULL)
       
   283           param_value = function_call_param_iterator.next();
       
   284         
       
   285         if (param_value == NULL) ERROR;
       
   286         
       
   287         param_type = (symbol_c *)param_value->accept(*this);
       
   288       }
       
   289       
       
   290       switch (current_function_type) {
       
   291         case function_sqrt:
       
   292           if (current_param == 0 && is_real_type(param_type))
       
   293             return_type = param_type;
       
   294           else ERROR;
       
   295           break;
       
   296         default: ERROR;
       
   297       }
       
   298     }
       
   299     
       
   300     return (void *)return_type;
       
   301   }
    83 
   302 
    84   static bool_type_name_c bool_type_name;
   303   static bool_type_name_c bool_type_name;
    85 
   304 
    86   /* A helper function... */
   305   /* A helper function... */
    87   void *compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) {
   306   void *compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) {
    88     if (typeid(*left_type) == typeid(bool_type_name_c) and typeid(*right_type) == typeid(bool_type_name_c)) {return (void *)left_type;}
   307     if (!is_same_type(left_type, right_type))
    89     if (is_numeric_compatible(left_type) && is_numeric_compatible(right_type)) {return (void *)left_type;}
   308       ERROR;
    90     ERROR;
   309     if (!is_bool_type(left_type) && !is_binary_type(left_type))
    91     return NULL;
   310       ERROR;
       
   311     if (typeid(*left_type) == typeid(constant_int_type_name_c)) {return (void *)right_type;}
       
   312     else {return (void *)left_type;}
    92   }
   313   }
    93 
   314 
    94   /* A helper function... */
   315   /* A helper function... */
    95   void *compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) {
   316   void *compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) {
    96     if (is_numeric_compatible(left_type) && is_numeric_compatible(right_type)) {return (void *)left_type;}
   317     if (!is_same_type(left_type, right_type))
    97     ERROR;
   318       ERROR;
       
   319     if (!is_integer_type(left_type) && !is_real_type(left_type))
       
   320       ERROR;
       
   321     if ((typeid(*left_type) == typeid(constant_int_type_name_c)) || (typeid(*left_type) == typeid(constant_real_type_name_c))) {return (void *)right_type;}
       
   322     else {return (void *)left_type;}
    98     return NULL;
   323     return NULL;
    99   }
   324   }
   100 
   325 
   101   /* a helper function... */
   326   /* a helper function... */
   102   symbol_c *base_type(symbol_c *symbol) {
   327   symbol_c *base_type(symbol_c *symbol) {
   173   void *visit(ge_expression_c *symbol) {return (void *)&bool_type_name;}
   398   void *visit(ge_expression_c *symbol) {return (void *)&bool_type_name;}
   174 
   399 
   175   void *visit(add_expression_c *symbol) {
   400   void *visit(add_expression_c *symbol) {
   176     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   401     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   177     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
   402     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
   178     if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;}
   403     if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;}
       
   404     if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;}
       
   405     if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;}
   179     return compute_numeric_expression(left_type, right_type);
   406     return compute_numeric_expression(left_type, right_type);
   180   }
   407   }
   181 
   408 
   182   void *visit(sub_expression_c *symbol) {
   409   void *visit(sub_expression_c *symbol) {
   183     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   410     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   184     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
   411     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
   185     if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;}
   412     if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;}
       
   413     if (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) {return (void *)&time_type_name;}
       
   414     if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;}
       
   415     if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) {return (void *)&time_type_name;}
       
   416     if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;}
       
   417     if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)) {return (void *)&time_type_name;}
   186     return compute_numeric_expression(left_type, right_type);
   418     return compute_numeric_expression(left_type, right_type);
   187   }
   419   }
   188   
   420   
   189   void *visit(mul_expression_c *symbol) {
   421   void *visit(mul_expression_c *symbol) {
   190     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   422     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   191     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
   423     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
   192     if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;}
   424     if (typeid(*left_type) == typeid(time_type_name_c) && is_integer_type(right_type)) {return (void *)&time_type_name;}
       
   425     if (typeid(*left_type) == typeid(time_type_name_c) && is_real_type(right_type)) {return (void *)&time_type_name;}
   193     return compute_numeric_expression(left_type, right_type);
   426     return compute_numeric_expression(left_type, right_type);
   194   }
   427   }
   195   
   428   
   196   void *visit(div_expression_c *symbol) {
   429   void *visit(div_expression_c *symbol) {
   197     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   430     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   198     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
   431     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
   432     if (typeid(*left_type) == typeid(time_type_name_c) && is_integer_type(right_type)) {return (void *)&time_type_name;}
       
   433     if (typeid(*left_type) == typeid(time_type_name_c) && is_real_type(right_type)) {return (void *)&time_type_name;}
   199     return compute_numeric_expression(left_type, right_type);
   434     return compute_numeric_expression(left_type, right_type);
   200   }
   435   }
   201 
   436 
   202   void *visit(mod_expression_c *symbol) {
   437   void *visit(mod_expression_c *symbol) {
   203     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   438     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
   253   }
   488   }
   254   
   489   
   255   void *visit(function_invocation_c *symbol) {
   490   void *visit(function_invocation_c *symbol) {
   256 	  function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
   491 	  function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
   257 	
   492 	
   258 	  if (f_decl == function_symtable.end_value())
   493 	  if (f_decl == function_symtable.end_value()) {
   259 	    /* should never occur. The function being called MUST be in the symtable... */
   494       void *res = compute_standard_function_st(symbol);
   260 	    ERROR;
   495 	    if (res == NULL)
       
   496         ERROR;
       
   497       return res;
       
   498     }
   261 
   499 
   262     return base_type(f_decl->type_name);
   500     return base_type(f_decl->type_name);
   263   }
   501   }
   264 
   502 
   265 };
   503 };