absyntax_utils/get_sizeof_datatype.hh
changeset 202 da1a8186f86f
child 257 90782e241346
equal deleted inserted replaced
201:e657008f43d0 202:da1a8186f86f
       
     1 /*
       
     2  * (c) 2009 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * 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.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /* Determine the size, in bits, of the data type.
       
    27  * 
       
    28  * NOTE: Currently, only elementary data types with well defined sizes (in the standard) are supported.
       
    29  *       - derived data types are not supported, and these will return 0
       
    30  *       - TIME, DATE, TIME_OF_DAY, and DATE_AND_TIME are not supported, and will return 0
       
    31  *       - STRING and WSTRING are not supported, and the standard merely defines bit per character,
       
    32  *              and not the maximum number of characters, so these will return 0
       
    33  *
       
    34  *       We also support the 'Numeric Literals' Data types.
       
    35  *       i.e., numeric literals are considerd basic data types
       
    36  *       as their data type is undefined (e.g. the datat type of '30'
       
    37  *       could be 'INT' or 'SINT' or 'LINT' or 'USINT' or ...
       
    38  *
       
    39  *       For numeric literals, we return the minimum number of bits
       
    40  *       required to store the value.
       
    41  *
       
    42  * E.g. TYPE new_int_t : INT; END_TYPE;
       
    43  *      TYPE new_int2_t : INT = 2; END_TYPE;
       
    44  *      TYPE new_subr_t : INT (4..5); END_TYPE;
       
    45  *
       
    46  *    sizeof(SINT) ->  8
       
    47  *    sizeof(INT)  -> 16
       
    48  *    sizeof(DINT) -> 32
       
    49  *    sizeof(LINT) -> 64
       
    50  *
       
    51  *    sizeof('1')       ->  1
       
    52  *    sizeof('015')     ->  4    # Leading zeros are ignored!
       
    53  *    sizeof('0')       ->  1    # This is a special case! Even the value 0 needs at least 1 bit to store!
       
    54  *    sizeof('16')      ->  5
       
    55  *    sizeof('2#00101') ->  3
       
    56  *    sizeof('8#334')   ->  9
       
    57  *    sizeof('16#2A')   ->  8
       
    58  *
       
    59  *    sizeof('7.4')     ->  32   # all real literals return 32 bits, the size of a 'REAL'
       
    60  *                               # TODO: study IEC 60559 for the range of values that may be
       
    61  *                               #       stored in a REAL (basic single width floating point format)
       
    62  *                               #       and in a LREAL (basic double width floating point format)
       
    63  *                               #       and see if some real literals need to return 64 instead!
       
    64  */
       
    65 
       
    66 #include "../absyntax/visitor.hh"
       
    67 
       
    68 class get_sizeof_datatype_c: public null_visitor_c {
       
    69 
       
    70   public:
       
    71     static int getsize(symbol_c *data_type_symbol);
       
    72     ~get_sizeof_datatype_c(void);
       
    73 
       
    74   private:
       
    75     /* this class is a singleton. So we need a pointer to the single instance... */
       
    76     static get_sizeof_datatype_c *singleton;
       
    77 
       
    78   private:
       
    79     /*********************/
       
    80     /* B 1.2 - Constants */
       
    81     /*********************/
       
    82 
       
    83     /******************************/
       
    84     /* B 1.2.1 - Numeric Literals */
       
    85     /******************************/
       
    86      /* Numeric literals without any explicit type cast have unknown data type, 
       
    87       * so we continue considering them as their own basic data types until
       
    88       * they can be resolved (for example, when using '30+x' where 'x' is a LINT variable, the
       
    89       * numeric literal '30' must then be considered a LINT so the ADD function may be called
       
    90       * with all inputs of the same data type.
       
    91       * If 'x' were a SINT, then the '30' would have to be a SINT too!
       
    92       */
       
    93     void *visit(real_c *symbol);
       
    94     void *visit(integer_c *symbol);
       
    95     void *visit(binary_integer_c *symbol);
       
    96     void *visit(octal_integer_c *symbol);
       
    97     void *visit(hex_integer_c *symbol);
       
    98 
       
    99   /***********************************/
       
   100   /* B 1.3.1 - Elementary Data Types */
       
   101   /***********************************/
       
   102 //     void *visit(time_type_name_c *symbol);
       
   103     void *visit(bool_type_name_c *symbol);
       
   104     void *visit(sint_type_name_c *symbol);
       
   105     void *visit(int_type_name_c *symbol);
       
   106     void *visit(dint_type_name_c *symbol);
       
   107     void *visit(lint_type_name_c *symbol);
       
   108     void *visit(usint_type_name_c *symbol);
       
   109     void *visit(uint_type_name_c *symbol);
       
   110     void *visit(udint_type_name_c *symbol);
       
   111     void *visit(ulint_type_name_c *symbol);
       
   112     void *visit(real_type_name_c *symbol);
       
   113     void *visit(lreal_type_name_c *symbol);
       
   114 //     void *visit(date_type_name_c *symbol);
       
   115 //     void *visit(tod_type_name_c *symbol);
       
   116 //     void *visit(dt_type_name_c *symbol)	;
       
   117     void *visit(byte_type_name_c *symbol);
       
   118     void *visit(word_type_name_c *symbol);
       
   119     void *visit(dword_type_name_c *symbol);
       
   120     void *visit(lword_type_name_c *symbol);
       
   121 //     void *visit(string_type_name_c *symbol);
       
   122 //     void *visit(wstring_type_name_c *symbol);
       
   123 
       
   124     /******************************************************/
       
   125     /* Extensions to the base standard as defined in      */
       
   126     /* "Safety Software Technical Specification,          */
       
   127     /*  Part 1: Concepts and Function Blocks,             */
       
   128     /*  Version 1.0 – Official Release"                   */
       
   129     /* by PLCopen - Technical Committee 5 - 2006-01-31    */
       
   130     /******************************************************/
       
   131     void *visit(safebool_type_name_c *symbol);
       
   132 
       
   133   /********************************/
       
   134   /* B 1.3.3 - Derived data types */
       
   135   /********************************/
       
   136 #if 0
       
   137   /*  simple_type_name ':' simple_spec_init */
       
   138     void *visit(simple_type_declaration_c *symbol);
       
   139   /* simple_specification ASSIGN constant */
       
   140     void *visit(simple_spec_init_c *symbol);
       
   141   /*  subrange_type_name ':' subrange_spec_init */
       
   142     void *visit(subrange_type_declaration_c *symbol);
       
   143   /* subrange_specification ASSIGN signed_integer */
       
   144     void *visit(subrange_spec_init_c *symbol);
       
   145   /*  integer_type_name '(' subrange')' */
       
   146     void *visit(subrange_specification_c *symbol);
       
   147   /*  signed_integer DOTDOT signed_integer */
       
   148     void *visit(subrange_c *symbol);
       
   149 
       
   150   /*  enumerated_type_name ':' enumerated_spec_init */
       
   151     void *visit(enumerated_type_declaration_c *symbol);
       
   152   /* enumerated_specification ASSIGN enumerated_value */
       
   153     void *visit(enumerated_spec_init_c *symbol);
       
   154   /* helper symbol for enumerated_specification->enumerated_spec_init */
       
   155   /* enumerated_value_list ',' enumerated_value */
       
   156     void *visit(enumerated_value_list_c *symbol);
       
   157   /* enumerated_type_name '#' identifier */
       
   158   // SYM_REF2(enumerated_value_c, type, value)
       
   159     void *visit(enumerated_value_c *symbol);
       
   160   /*  identifier ':' array_spec_init */
       
   161     void *visit(array_type_declaration_c *symbol);
       
   162   /* array_specification [ASSIGN array_initialization} */
       
   163   /* array_initialization may be NULL ! */
       
   164     void *visit(array_spec_init_c *symbol);
       
   165   /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
       
   166     void *visit(array_specification_c *symbol);
       
   167   /* helper symbol for array_specification */
       
   168   /* array_subrange_list ',' subrange */
       
   169     void *visit(array_subrange_list_c *symbol);
       
   170   /* array_initialization:  '[' array_initial_elements_list ']' */
       
   171   /* helper symbol for array_initialization */
       
   172   /* array_initial_elements_list ',' array_initial_elements */
       
   173     void *visit(array_initial_elements_list_c *symbol);
       
   174   /* integer '(' [array_initial_element] ')' */
       
   175   /* array_initial_element may be NULL ! */
       
   176     void *visit(array_initial_elements_c *symbol);
       
   177   /*  structure_type_name ':' structure_specification */
       
   178       /* NOTE: structure_specification will point to either a
       
   179        *       initialized_structure_c
       
   180        *       OR A
       
   181        *       structure_element_declaration_list_c
       
   182        */
       
   183     void *visit(structure_type_declaration_c *symbol);
       
   184   /* structure_type_name ASSIGN structure_initialization */
       
   185   /* structure_initialization may be NULL ! */
       
   186     void *visit(initialized_structure_c *symbol);
       
   187   /* helper symbol for structure_declaration */
       
   188   /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   189   /* structure_element_declaration_list structure_element_declaration ';' */
       
   190     void *visit(structure_element_declaration_list_c *symbol);
       
   191   /*  structure_element_name ':' *_spec_init */
       
   192     void *visit(structure_element_declaration_c *symbol);
       
   193   /* helper symbol for structure_initialization */
       
   194   /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   195   /* structure_element_initialization_list ',' structure_element_initialization */
       
   196     void *visit(structure_element_initialization_list_c *symbol);
       
   197   /*  structure_element_name ASSIGN value */
       
   198     void *visit(structure_element_initialization_c *symbol);
       
   199   /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
       
   200   /*
       
   201   SYM_REF4(string_type_declaration_c,	string_type_name,
       
   202   					elementary_string_type_name,
       
   203   					string_type_declaration_size,
       
   204   					string_type_declaration_init) // may be == NULL!
       
   205   */
       
   206     void *visit(string_type_declaration_c *symbol);
       
   207 #endif
       
   208 }; // get_sizeof_datatype_c
       
   209 
       
   210 
       
   211 
       
   212