absyntax_utils/get_datatype_info.cc
changeset 929 469de0f54b0e
parent 922 1ba7a814098d
child 938 31e3b3f2eff1
equal deleted inserted replaced
928:fa7a6800503d 929:469de0f54b0e
    73       if (NULL == singleton) ERROR;
    73       if (NULL == singleton) ERROR;
    74       return (symbol_c *)symbol->accept(*singleton);
    74       return (symbol_c *)symbol->accept(*singleton);
    75     }
    75     }
    76     
    76     
    77   protected:
    77   protected:
       
    78     /***********************************/
       
    79     /* B 1.3.1 - Elementary Data Types */
       
    80     /***********************************/
       
    81     void *visit(time_type_name_c        *symbol) {return (void *)symbol;};
       
    82     void *visit(bool_type_name_c        *symbol) {return (void *)symbol;};
       
    83     void *visit(sint_type_name_c        *symbol) {return (void *)symbol;};
       
    84     void *visit(int_type_name_c         *symbol) {return (void *)symbol;};
       
    85     void *visit(dint_type_name_c        *symbol) {return (void *)symbol;};
       
    86     void *visit(lint_type_name_c        *symbol) {return (void *)symbol;};
       
    87     void *visit(usint_type_name_c       *symbol) {return (void *)symbol;};
       
    88     void *visit(uint_type_name_c        *symbol) {return (void *)symbol;};
       
    89     void *visit(udint_type_name_c       *symbol) {return (void *)symbol;};
       
    90     void *visit(ulint_type_name_c       *symbol) {return (void *)symbol;};
       
    91     void *visit(real_type_name_c        *symbol) {return (void *)symbol;};
       
    92     void *visit(lreal_type_name_c       *symbol) {return (void *)symbol;};
       
    93     void *visit(date_type_name_c        *symbol) {return (void *)symbol;};
       
    94     void *visit(tod_type_name_c         *symbol) {return (void *)symbol;};
       
    95     void *visit(dt_type_name_c          *symbol) {return (void *)symbol;};
       
    96     void *visit(byte_type_name_c        *symbol) {return (void *)symbol;};
       
    97     void *visit(word_type_name_c        *symbol) {return (void *)symbol;};
       
    98     void *visit(lword_type_name_c       *symbol) {return (void *)symbol;};
       
    99     void *visit(dword_type_name_c       *symbol) {return (void *)symbol;};
       
   100     void *visit(string_type_name_c      *symbol) {return (void *)symbol;};
       
   101     void *visit(wstring_type_name_c     *symbol) {return (void *)symbol;};
       
   102 
       
   103     void *visit(safetime_type_name_c    *symbol) {return (void *)symbol;};
       
   104     void *visit(safebool_type_name_c    *symbol) {return (void *)symbol;};
       
   105     void *visit(safesint_type_name_c    *symbol) {return (void *)symbol;};
       
   106     void *visit(safeint_type_name_c     *symbol) {return (void *)symbol;};
       
   107     void *visit(safedint_type_name_c    *symbol) {return (void *)symbol;};
       
   108     void *visit(safelint_type_name_c    *symbol) {return (void *)symbol;};
       
   109     void *visit(safeusint_type_name_c   *symbol) {return (void *)symbol;};
       
   110     void *visit(safeuint_type_name_c    *symbol) {return (void *)symbol;};
       
   111     void *visit(safeudint_type_name_c   *symbol) {return (void *)symbol;};
       
   112     void *visit(safeulint_type_name_c   *symbol) {return (void *)symbol;};
       
   113     void *visit(safereal_type_name_c    *symbol) {return (void *)symbol;};
       
   114     void *visit(safelreal_type_name_c   *symbol) {return (void *)symbol;};
       
   115     void *visit(safedate_type_name_c    *symbol) {return (void *)symbol;};
       
   116     void *visit(safetod_type_name_c     *symbol) {return (void *)symbol;};
       
   117     void *visit(safedt_type_name_c      *symbol) {return (void *)symbol;};
       
   118     void *visit(safebyte_type_name_c    *symbol) {return (void *)symbol;};
       
   119     void *visit(safeword_type_name_c    *symbol) {return (void *)symbol;};
       
   120     void *visit(safelword_type_name_c   *symbol) {return (void *)symbol;};
       
   121     void *visit(safedword_type_name_c   *symbol) {return (void *)symbol;};
       
   122     void *visit(safestring_type_name_c  *symbol) {return (void *)symbol;};
       
   123     void *visit(safewstring_type_name_c *symbol) {return (void *)symbol;};
       
   124 
    78     /********************************/
   125     /********************************/
    79     /* B 1.3.3 - Derived data types */
   126     /* B 1.3.3 - Derived data types */
    80     /********************************/
   127     /********************************/
    81     /*  simple_type_name ':' simple_spec_init */
   128     /*  simple_type_name ':' simple_spec_init */
    82     void *visit(simple_type_declaration_c     *symbol)  {return symbol->simple_type_name;}
   129     void *visit(simple_type_declaration_c     *symbol)  {return symbol->simple_type_name;}
    90     void *visit(structure_type_declaration_c  *symbol)  {return symbol->structure_type_name;}
   137     void *visit(structure_type_declaration_c  *symbol)  {return symbol->structure_type_name;}
    91     /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
   138     /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
    92     void *visit(string_type_declaration_c     *symbol)  {return symbol->string_type_name;}
   139     void *visit(string_type_declaration_c     *symbol)  {return symbol->string_type_name;}
    93     /* ref_type_decl: identifier ':' ref_spec_init */
   140     /* ref_type_decl: identifier ':' ref_spec_init */
    94     void *visit(ref_type_decl_c               *symbol)  {return symbol->ref_type_name;}
   141     void *visit(ref_type_decl_c               *symbol)  {return symbol->ref_type_name;}
       
   142     /* NOTE: DO NOT place any code here that references symbol->anotations_map["generate_c_annotaton__implicit_type_id"] !!
       
   143      *       All anotations in the symbol->anotations_map[] are considered a stage4 construct. In the above example,
       
   144      *       That anotation is specific to the generate_c stage4 code, and must therefore NOT be referenced
       
   145      *       in the absyntax_utils code, as this last code should be independent of the stage4 version!
       
   146      */ 
    95     
   147     
    96     /*****************************/
   148     /*****************************/
    97     /* B 1.5.2 - Function Blocks */
   149     /* B 1.5.2 - Function Blocks */
    98     /*****************************/
   150     /*****************************/
    99     /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
   151     /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
   202     void *visit(structure_type_declaration_c  *symbol)  {return symbol->structure_type_name->accept(*this);}
   254     void *visit(structure_type_declaration_c  *symbol)  {return symbol->structure_type_name->accept(*this);}
   203     /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
   255     /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
   204     void *visit(string_type_declaration_c     *symbol)  {return symbol->string_type_name->accept(*this);}
   256     void *visit(string_type_declaration_c     *symbol)  {return symbol->string_type_name->accept(*this);}
   205     /* ref_type_decl: identifier ':' ref_spec_init */
   257     /* ref_type_decl: identifier ':' ref_spec_init */
   206     void *visit(ref_type_decl_c               *symbol)  {return symbol->ref_type_name->accept(*this);}
   258     void *visit(ref_type_decl_c               *symbol)  {return symbol->ref_type_name->accept(*this);}
       
   259     /* NOTE: DO NOT place any code here that references symbol->anotations_map["generate_c_annotaton__implicit_type_id"] !!
       
   260      *       All anotations in the symbol->anotations_map[] are considered a stage4 construct. In the above example,
       
   261      *       That anotation is specific to the generate_c stage4 code, and must therefore NOT be referenced
       
   262      *       in the absyntax_utils code, as this last code should be independent of the stage4 version!
       
   263      */ 
   207     
   264     
   208     /*****************************/
   265     /*****************************/
   209     /* B 1.5.2 - Function Blocks */
   266     /* B 1.5.2 - Function Blocks */
   210     /*****************************/
   267     /*****************************/
   211     /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
   268     /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
   246  * NOTE: Currently only the ANY generic datatype is implemented!
   303  * NOTE: Currently only the ANY generic datatype is implemented!
   247  * NOTE: Currently stage1_2 only allows the use of the ANY keyword when in conjuntion with
   304  * NOTE: Currently stage1_2 only allows the use of the ANY keyword when in conjuntion with
   248  *       the REF_TO keyword (i.e. REF_TO ANY), so when handling non REF_TO datatypes,
   305  *       the REF_TO keyword (i.e. REF_TO ANY), so when handling non REF_TO datatypes,
   249  *       this function will currently only return true if the dataypes are identicial.
   306  *       this function will currently only return true if the dataypes are identicial.
   250  */
   307  */
       
   308 
       
   309 /* NOTE: Currently the datatype model used by matiec considers any implicitly defined datatype
       
   310  *       (e.g. an array datatype defined in the variable declaration itself, instead of inside a TYPE ... END_TYPE
       
   311  *       construct) to be different (i.e. not the same datatype, and therefore not compatible) to any other
       
   312  *       datatype, including with datatypes declared identically to the implicit datatype.
       
   313  *       e.g.
       
   314  *         TYPE my_array_t: ARRAY [1..3] OF INT; END_TYPE;
       
   315  *         FUNCTION_BLOCK FOO
       
   316  *          VAR my_array:  ARRAY [1..3] OF INT; END_VAR
       
   317  *         ...
       
   318  *         END_FUNCTION_BLOCK
       
   319  * 
       
   320  *         In the abive code, my_array is NOT considered to te compatible with my_Array_t !!!
       
   321  * 
       
   322  *       In essence, the currently supported datatype model considers all datatypes to be different to each other,
       
   323  *       even though the stored data is the same (Let us call this rule (0))!
       
   324  *       There are 2 exceptions to the above rule:
       
   325  *        (1) Datatypes that are directly derived from other datatypes.
       
   326  *              (this rule is specified in the standard, so we follow it!)
       
   327  *        (2) REF_TO datatypes that reference the same datatype
       
   328  *              (I dont think the standard says anything about this!)
       
   329  *
       
   330  *         TYPE 
       
   331  *          my_array_1_t: ARRAY [1..3] OF INT; 
       
   332  *          my_array_2_t: ARRAY [1..3] OF INT; 
       
   333  *          my_array_3_t: my_array_1_t; 
       
   334  *          A_ref_t: REF_TO my_array_1_t;
       
   335  *          B_ref_t: REF_TO my_array_1_t;
       
   336  *          C_ref_t: A_ref_t;
       
   337  *         END_TYPE;
       
   338  *                 
       
   339  *         In the above code, my_array_1_t is a distinct datatype to my_array_2_t
       
   340  *           (this is different to C and C++, where they would be considered the same datatype!)
       
   341  *           (following rule (0))
       
   342  *         In the above code, my_array_3_t is the same datatype as my_array_1_t
       
   343  *           (following rule (1))
       
   344  *         In the above code, A_ref_t is the same datatype as B_ref_t
       
   345  *           (following rule (2))
       
   346  *         In the above code, A_ref_t is the same datatype as C_ref_t
       
   347  *           (following rule (1))
       
   348  *
       
   349  *       Note that rule (0) means that a function/FB with a parameter whose datatype is implicitly defined
       
   350  *       can never be passed a value!
       
   351  *         FUNCTION_BLOCK FOO
       
   352  *          VAR_INPUT my_array:  ARRAY [1..3] OF INT; END_VAR
       
   353  *         ...
       
   354  *         END_FUNCTION_BLOCK
       
   355  *
       
   356  *       Any call to FB foo can never pass a value to parameter my_array, as its datatype is distinct
       
   357  *       to all other datatypes, and therefore passing any other variable to my_array will result in an
       
   358  *       'incompatible datatypes' error!
       
   359  *       The above seems natural o me (Mario) in a programming language that is very strongly typed.
       
   360  * 
       
   361  *       However, if we did not have exception (2), the following would also be invalid:
       
   362  *         TYPE my_array_t: ARRAY [1..3] OF INT; END_TYPE;
       
   363  *         FUNCTION_BLOCK FOO_t
       
   364  *          VAR_INPUT my_array: REF_TO my_array_t; END_VAR
       
   365  *         ...
       
   366  *         END_FUNCTION_BLOCK
       
   367  * 
       
   368  *         FUNCTION_BLOCK BAR
       
   369  *          VAR
       
   370  *            my_array: my_array_t; 
       
   371  *            foo: FOO_t;
       
   372  *          END_VAR
       
   373  *          foo(REF(my_array));  <----- invalid, without rule 2!!
       
   374  *         ...
       
   375  *         END_FUNCTION_BLOCK
       
   376  * 
       
   377  *       Rule/exception (2) goes against the datatype model used for all other datatypes.
       
   378  *       This rule was adopted as without it, the datatype of the value returned by the REF() 
       
   379  *       operator would be considered distinct to all other datatypes, and therefore the
       
   380  *       REF() operator would be essentially useless.
       
   381  */
   251 bool get_datatype_info_c::is_type_equal(symbol_c *first_type, symbol_c *second_type) {
   382 bool get_datatype_info_c::is_type_equal(symbol_c *first_type, symbol_c *second_type) {
   252   if (!is_type_valid( first_type))                                   {return false;}
   383   if (!is_type_valid( first_type))                                   {return false;}
   253   if (!is_type_valid(second_type))                                   {return false;}
   384   if (!is_type_valid(second_type))                                   {return false;}
   254 
   385 
   255   /* GENERIC DATATYPES */
   386   /* GENERIC DATATYPES */
   264   /* ANY_DERIVED */
   395   /* ANY_DERIVED */
   265   if (is_ref_to(first_type) && is_ref_to(second_type)) {
   396   if (is_ref_to(first_type) && is_ref_to(second_type)) {
   266     return is_type_equal(search_base_type_c::get_basetype_decl(get_ref_to(first_type )),
   397     return is_type_equal(search_base_type_c::get_basetype_decl(get_ref_to(first_type )),
   267                          search_base_type_c::get_basetype_decl(get_ref_to(second_type)));
   398                          search_base_type_c::get_basetype_decl(get_ref_to(second_type)));
   268   }
   399   }
   269 
       
   270   return (first_type == second_type);
   400   return (first_type == second_type);
   271 }
   401 }
   272 
   402 
   273 
   403 
   274 bool get_datatype_info_c::is_type_valid(symbol_c *type) {
   404 bool get_datatype_info_c::is_type_valid(symbol_c *type) {