stage3/visit_expression_type.hh
changeset 270 f1674941c3ad
parent 267 0a1204bcc9af
child 279 c0453b7f99df
equal deleted inserted replaced
256:101c7f148b9a 270:f1674941c3ad
     1 /*
     1 /*
     2  * (c) 2003 Mario de Sousa
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
     3  *
     3  *
     4  * Offered to the public under the terms of the GNU General Public License
     4  *  Copyright (C) 2009-2011  Mario de Sousa (msousa@fe.up.pt)
     5  * as published by the Free Software Foundation; either version 2 of the
     5  *
     6  * License, or (at your option) any later version.
     6  *  This program is free software: you can redistribute it and/or modify
     7  *
     7  *  it under the terms of the GNU General Public License as published by
     8  * This program is distributed in the hope that it will be useful, but
     8  *  the Free Software Foundation, either version 3 of the License, or
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     9  *  (at your option) any later version.
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
    10  *
    11  * Public License for more details.
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
    12  *
    19  *
    13  * This code is made available on the understanding that it will not be
    20  * This code is made available on the understanding that it will not be
    14  * used in safety-critical situations without a full and competent review.
    21  * used in safety-critical situations without a full and competent review.
    15  */
    22  */
    16 
    23 
    17 /*
    24 /*
    18  * An IEC 61131-3 IL and ST compiler.
    25  * An IEC 61131-3 compiler.
    19  *
    26  *
    20  * Based on the
    27  * Based on the
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    22  *
    29  *
    23  */
    30  */
    65      * We therefore need a counter to know how deep inside a parenthesis 
    72      * We therefore need a counter to know how deep inside a parenthesis 
    66      * structure we are.
    73      * structure we are.
    67      */
    74      */
    68     int  il_parenthesis_level;
    75     int  il_parenthesis_level;
    69     bool il_error;
    76     bool il_error;
    70 
    77     bool error_found;
       
    78 
       
    79     /* the current data type of the data stored in the IL stack, i.e. the default variable */
    71     symbol_c *il_default_variable_type;
    80     symbol_c *il_default_variable_type;
       
    81     /* the current IL operand being analysed - its symbol and its data type */
    72     symbol_c *il_operand_type;
    82     symbol_c *il_operand_type;
       
    83     symbol_c *il_operand;
    73 
    84 
    74 
    85 
    75   public:
    86   public:
    76     visit_expression_type_c(symbol_c *search_scope);
    87     visit_expression_type_c(symbol_c *ignore);
    77     virtual ~visit_expression_type_c(void);
    88     virtual ~visit_expression_type_c(void);
    78 
    89 
       
    90     bool get_error_found(void);
       
    91 
       
    92     typedef struct {
       
    93       symbol_c *value;
       
    94       symbol_c *type;
       
    95     } value_and_type_t; 
       
    96 
    79     /* A helper function... */
    97     /* A helper function... */
    80     bool is_ANY_ELEMENTARY_type(symbol_c *type_symbol);
    98     bool is_ANY_ELEMENTARY_type         (symbol_c *type_symbol);
    81     bool is_ANY_MAGNITUDE_type(symbol_c *type_symbol);
    99     bool is_ANY_SAFEELEMENTARY_type     (symbol_c *type_symbol);
    82     bool is_ANY_DATE_type(symbol_c *type_symbol);
   100     bool is_ANY_ELEMENTARY_compatible   (symbol_c *type_symbol);
    83     bool is_ANY_STRING_type(symbol_c *type_symbol);
   101 
    84     bool is_ANY_INT_type(symbol_c *type_symbol);
   102     bool is_ANY_MAGNITUDE_type          (symbol_c *type_symbol);
    85     bool is_ANY_REAL_type(symbol_c *type_symbol);
   103     bool is_ANY_SAFEMAGNITUDE_type      (symbol_c *type_symbol);
    86     bool is_ANY_NUM_type(symbol_c *type_symbol);
   104     bool is_ANY_MAGNITUDE_compatible    (symbol_c *type_symbol);
    87     bool is_ANY_BIT_type(symbol_c *type_symbol);
   105 
    88     bool is_BOOL_type(symbol_c *type_symbol);
   106     bool is_ANY_DATE_type               (symbol_c *type_symbol);
    89 
   107     bool is_ANY_SAFEDATE_type           (symbol_c *type_symbol);
    90     bool is_literal_integer_type(symbol_c *type_symbol);
   108     bool is_ANY_DATE_compatible         (symbol_c *type_symbol);
    91     bool is_literal_real_type(symbol_c *type_symbol);
   109 
    92     bool is_literal_bool_type(symbol_c *type_symbol);
   110     bool is_ANY_STRING_type             (symbol_c *type_symbol);
       
   111     bool is_ANY_SAFESTRING_type         (symbol_c *type_symbol);
       
   112     bool is_ANY_STRING_compatible       (symbol_c *type_symbol);   
       
   113 
       
   114     bool is_ANY_INT_type                (symbol_c *type_symbol);
       
   115     bool is_ANY_SAFEINT_type            (symbol_c *type_symbol);
       
   116     bool is_ANY_INT_compatible          (symbol_c *type_symbol);
       
   117 
       
   118     bool is_ANY_REAL_type               (symbol_c *type_symbol);
       
   119     bool is_ANY_SAFEREAL_type           (symbol_c *type_symbol);
       
   120     bool is_ANY_REAL_compatible         (symbol_c *type_symbol);
       
   121 
       
   122     bool is_ANY_NUM_type                (symbol_c *type_symbol);
       
   123     bool is_ANY_SAFENUM_type            (symbol_c *type_symbol);
       
   124     bool is_ANY_NUM_compatible          (symbol_c *type_symbol);
       
   125 
       
   126     bool is_ANY_BIT_type                (symbol_c *type_symbol);
       
   127     bool is_ANY_SAFEBIT_type            (symbol_c *type_symbol);
       
   128     bool is_ANY_BIT_compatible          (symbol_c *type_symbol);
       
   129 
       
   130     bool is_BOOL_type                   (symbol_c *type_symbol);
       
   131     bool is_SAFEBOOL_type               (symbol_c *type_symbol);
       
   132     bool is_ANY_BOOL_compatible         (symbol_c *type_symbol);
       
   133     
       
   134     bool is_nonneg_literal_integer_type (symbol_c *type_symbol);
       
   135     bool is_literal_integer_type        (symbol_c *type_symbol);
       
   136     bool is_literal_real_type           (symbol_c *type_symbol);
       
   137     bool is_literal_bool_type           (symbol_c *type_symbol);
    93 
   138 
    94     /* Determine the common data type between two data types.
   139     /* Determine the common data type between two data types.
    95      * If no common data type found, return NULL.
   140      * If no common data type found, return NULL.
    96      *
   141      *
    97      * If data types are identical, return the first (any would do...).
   142      * If data types are identical, return the first (any would do...).
   106     /* Determine the common data type between two data types.
   151     /* Determine the common data type between two data types.
   107      * Unlike the common_type__() function, we stop the compiler with an ERROR
   152      * Unlike the common_type__() function, we stop the compiler with an ERROR
   108      *  if no common data type is found.
   153      *  if no common data type is found.
   109      */
   154      */
   110     symbol_c *common_type(symbol_c *first_type, symbol_c *second_type);
   155     symbol_c *common_type(symbol_c *first_type, symbol_c *second_type);
   111     /* Return TRUE if there is a common data type, otherwise return FALSE */
   156 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
       
   157  * such as: 
       
   158  *     var_type     value_type
       
   159  *    BOOL           BYTE#7     -> returns false
       
   160  *    INT            INT#7      -> returns true
       
   161  *    INT            7          -> returns true
       
   162  *    REAL           7.89       -> returns true
       
   163  *    REAL           7          -> returns true
       
   164  *    INT            7.89       -> returns false
       
   165  *    SAFEBOOL       BOOL#1     -> returns false   !!!
       
   166  *   etc...
       
   167  *
       
   168  * NOTE: It is assumed that the var_type is the data type of an lvalue
       
   169  */
       
   170     bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type);
       
   171 
       
   172 /* Return TRUE if there is a common data type, otherwise return FALSE
       
   173  * i.e., return TRUE if both data types may be used simultaneously in an expression
       
   174  * such as:
       
   175  *    BOOL#0     AND BYTE#7  -> returns false
       
   176  *    0          AND BYTE#7  -> returns true
       
   177  *    INT#10     AND INT#7   -> returns true
       
   178  *    INT#10     AND 7       -> returns true
       
   179  *    REAL#34.3  AND 7.89    -> returns true
       
   180  *    REAL#34.3  AND 7       -> returns true
       
   181  *    INT#10     AND 7.89    -> returns false
       
   182  *    SAFEBOOL#0 AND BOOL#1  -> returns true   !!!
       
   183  *   etc...
       
   184  */
   112     bool is_compatible_type(symbol_c *first_type, symbol_c *second_type);
   185     bool is_compatible_type(symbol_c *first_type, symbol_c *second_type);
   113 
   186 
   114     void compute_input_operatores(symbol_c *symbol, const char *input_operator);
   187     void compute_input_operatores(symbol_c *symbol, const char *input_operator);
   115     void check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl);
   188     void check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl);
   116 
   189 
   126     void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type);
   199     void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type);
   127 
   200 
   128 
   201 
   129     /* A helper function... */
   202     /* A helper function... */
   130     typedef bool (visit_expression_type_c::*is_data_type_t)(symbol_c *type_symbol);  /* a pointer to a function! */
   203     typedef bool (visit_expression_type_c::*is_data_type_t)(symbol_c *type_symbol);  /* a pointer to a function! */
   131     symbol_c *compute_boolean_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
   204 //    symbol_c *compute_boolean_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
   132     symbol_c *compute_numeric_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
   205 //    symbol_c *compute_numeric_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
       
   206 //    symbol_c *compute_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
       
   207     symbol_c *compute_expression(symbol_c *left_type, symbol_c *right_type, is_data_type_t is_data_type,
       
   208                                  symbol_c *left_expr=NULL, symbol_c *right_expr=NULL);
       
   209 
   133 
   210 
   134     /* a helper function... */
   211     /* a helper function... */
   135     symbol_c *base_type(symbol_c *symbol);
   212     symbol_c *base_type(symbol_c *symbol);
   136 
   213 
   137     /* a helper function... */
   214     /* a helper function... */
   138     void *verify_null(symbol_c *symbol);
   215     void *verify_null(symbol_c *symbol);
   139 
       
   140 
       
   141 
       
   142 
   216 
   143     /*********************/
   217     /*********************/
   144     /* B 1.4 - Variables */
   218     /* B 1.4 - Variables */
   145     /*********************/
   219     /*********************/
   146     void *visit(symbolic_variable_c *symbol);
   220     void *visit(symbolic_variable_c *symbol);