stage4/generate_c/type_initial_value.cc
changeset 181 38d6eb056260
parent 180 64334c5a00b1
child 182 231633d1d2e4
equal deleted inserted replaced
180:64334c5a00b1 181:38d6eb056260
     1 /*
       
     2  * (c) 2003 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 /*
       
    27  * Determine the default initial value of a type declaration.
       
    28  *
       
    29  * This is part of the 4th stage that generates
       
    30  * a c++ source program equivalent to the IL and ST
       
    31  * code.
       
    32  */
       
    33 
       
    34 
       
    35 
       
    36 
       
    37 //#include <stdio.h>  /* required for NULL */
       
    38 //#include <string>
       
    39 //#include <iostream>
       
    40 
       
    41 //#include "../../util/symtable.hh"
       
    42 
       
    43 //#include "generate_c.hh"
       
    44 
       
    45 
       
    46 
       
    47 
       
    48 
       
    49 /* Given a type definition declration, determine its default
       
    50  * initial value. Note that types based on other types
       
    51  * may have to iterate through each type it is based on
       
    52  * to determine the initial value.
       
    53  * E.g.
       
    54  *  TYPE
       
    55  *    A_t : INT := 10;
       
    56  *    B_t : A_t := 20;
       
    57  *    C_t : B_t;
       
    58  *    D_t : C_t := 40;
       
    59  *  END_TYPE
       
    60  * Where the default initial value for C_t is 20!
       
    61  */
       
    62 /* NOTE: The main program only needs one instance of
       
    63  *       this class of object. This class
       
    64  *       is therefore a singleton.
       
    65  */
       
    66 class type_initial_value_c : public null_visitor_c {
       
    67   private:
       
    68     static type_initial_value_c *_instance;
       
    69     /* constants for the default values of elementary data types... */
       
    70     static real_c		*real_0;
       
    71     static integer_c		*integer_0, *integer_1;
       
    72     static boolean_literal_c	*bool_0;
       
    73     static date_literal_c	*date_literal_0;
       
    74     static daytime_c		*daytime_literal_0;
       
    75     static duration_c		*time_0;
       
    76     static date_c		*date_0;
       
    77     static time_of_day_c	*tod_0;
       
    78     static date_and_time_c	*dt_0;
       
    79     static single_byte_character_string_c *string_0;
       
    80     static double_byte_character_string_c *wstring_0;
       
    81 
       
    82   public:
       
    83     static type_initial_value_c *instance(void) {
       
    84       if (_instance != NULL)
       
    85         return _instance;
       
    86 
       
    87       _instance = new type_initial_value_c;
       
    88 
       
    89       real_0 = new real_c("0");
       
    90       integer_0 = new integer_c("0");
       
    91       integer_1 = new integer_c("1");
       
    92       bool_0 = new boolean_literal_c(new bool_type_name_c(),new boolean_false_c());
       
    93       /* FIXME: Our current implementation only allows dates from 1970 onwards,
       
    94        * but the standard defines the date 0001-01-01 as the default value
       
    95        * for the DATE data type. Untill we fix our implementation, we use 1970-01-01
       
    96        * as our default value!!
       
    97        */
       
    98 //      date_literal_0 =  new date_literal_c(integer_1, integer_1, integer_1);
       
    99       date_literal_0 =  new date_literal_c(new integer_c("1970"), integer_1, integer_1);
       
   100       daytime_literal_0 = new daytime_c(integer_0, integer_0, real_0);
       
   101       time_0 = new duration_c(NULL, new seconds_c(integer_0, NULL));  // T#0S
       
   102       date_0 = new date_c(date_literal_0);  //  D#0001-01-01
       
   103       tod_0 = new time_of_day_c(daytime_literal_0);  //  TOD#00:00:00
       
   104       dt_0 = new date_and_time_c(date_literal_0, daytime_literal_0);  //  DT#0001-01-01-00:00:00
       
   105       string_0  = new single_byte_character_string_c("''");
       
   106       wstring_0 = new double_byte_character_string_c("\"\"");
       
   107 
       
   108       return _instance;
       
   109     }
       
   110 
       
   111   protected:
       
   112     type_initial_value_c(void) {}
       
   113 
       
   114   public:
       
   115     symbol_c *get(identifier_c *type_name) {
       
   116       TRACE("type_initial_value_c::get(): called ");
       
   117       return (symbol_c *)type_name->accept(*this);
       
   118     }
       
   119 
       
   120 
       
   121   private:
       
   122     void *handle_type_spec(symbol_c *base_type_name, symbol_c *type_spec_init) {
       
   123       if (type_spec_init != NULL)
       
   124          return type_spec_init;
       
   125        /* no initial value specified, so we return
       
   126        * the initial value of the type this type is based on...
       
   127         */
       
   128       return base_type_name->accept(*this);
       
   129     }
       
   130 
       
   131   public:
       
   132     void *visit(identifier_c *type_name) {
       
   133       /* look up the type declaration... */
       
   134       symbol_c *type_decl = type_symtable.find_value(type_name);
       
   135       if (type_decl == type_symtable.end_value())
       
   136         /* Type declaration not found!! */
       
   137 	/* NOTE: Variables declared out of function block 'data types',
       
   138 	 *    for eg:  VAR  timer: TON; END_VAR
       
   139 	 * do not have a default value, so (TON) will never be found in the
       
   140 	 * type symbol table. This means we cannot simply consider this
       
   141 	 * an error and abort, but must rather return a NULL.
       
   142 	 */
       
   143 	return NULL;
       
   144 
       
   145       return type_decl->accept(*this);
       
   146     }
       
   147 
       
   148 /***********************************/
       
   149 /* B 1.3.1 - Elementary Data Types */
       
   150 /***********************************/
       
   151     void *visit(time_type_name_c *symbol)	{return (void *)time_0;}
       
   152     void *visit(bool_type_name_c *symbol)	{return (void *)bool_0;}
       
   153     void *visit(sint_type_name_c *symbol)	{return (void *)integer_0;}
       
   154     void *visit(int_type_name_c *symbol)	{return (void *)integer_0;}
       
   155     void *visit(dint_type_name_c *symbol)	{return (void *)integer_0;}
       
   156     void *visit(lint_type_name_c *symbol)	{return (void *)integer_0;}
       
   157     void *visit(usint_type_name_c *symbol)	{return (void *)integer_0;}
       
   158     void *visit(uint_type_name_c *symbol)	{return (void *)integer_0;}
       
   159     void *visit(udint_type_name_c *symbol)	{return (void *)integer_0;}
       
   160     void *visit(ulint_type_name_c *symbol)	{return (void *)integer_0;}
       
   161     void *visit(real_type_name_c *symbol)	{return (void *)real_0;}
       
   162     void *visit(lreal_type_name_c *symbol)	{return (void *)real_0;}
       
   163     void *visit(date_type_name_c *symbol)	{return (void *)date_0;}
       
   164     void *visit(tod_type_name_c *symbol)	{return (void *)tod_0;}
       
   165     void *visit(dt_type_name_c *symbol)		{return (void *)dt_0;}
       
   166     void *visit(byte_type_name_c *symbol)	{return (void *)integer_0;}
       
   167     void *visit(word_type_name_c *symbol)	{return (void *)integer_0;}
       
   168     void *visit(dword_type_name_c *symbol)	{return (void *)integer_0;}
       
   169     void *visit(lword_type_name_c *symbol)	{return (void *)integer_0;}
       
   170     void *visit(string_type_name_c *symbol)	{return (void *)string_0;}
       
   171     void *visit(wstring_type_name_c *symbol)	{return (void *)wstring_0;}
       
   172 
       
   173 /********************************/
       
   174 /* B 1.3.3 - Derived data types */
       
   175 /********************************/
       
   176 /*  simple_type_name ':' simple_spec_init */
       
   177     void *visit(simple_type_declaration_c *symbol) {
       
   178       return symbol->simple_spec_init->accept(*this);
       
   179     }
       
   180 /* simple_specification ASSIGN constant */
       
   181     void *visit(simple_spec_init_c *symbol) {
       
   182       return handle_type_spec(symbol->simple_specification, symbol->constant);
       
   183     }
       
   184 /*  subrange_type_name ':' subrange_spec_init */
       
   185     void *visit(subrange_type_declaration_c *symbol) {
       
   186       return symbol->subrange_spec_init->accept(*this);
       
   187     }
       
   188 /* subrange_specification ASSIGN signed_integer */
       
   189     void *visit(subrange_spec_init_c *symbol) {
       
   190       return handle_type_spec(symbol->subrange_specification, symbol->signed_integer);
       
   191     }
       
   192 /*  integer_type_name '(' subrange')' */
       
   193     void *visit(subrange_specification_c *symbol) {
       
   194      /* if no initial value explicitly given, then use the lowest value of the subrange */
       
   195       if (symbol->subrange != NULL)
       
   196         return symbol->subrange->accept(*this);
       
   197       else
       
   198         return symbol->integer_type_name->accept(*this);
       
   199     }
       
   200 /*  signed_integer DOTDOT signed_integer */
       
   201     void *visit(subrange_c *symbol)	{return symbol->lower_limit;}
       
   202 /*  enumerated_type_name ':' enumerated_spec_init */
       
   203     void *visit(enumerated_type_declaration_c *symbol) {
       
   204       return symbol->enumerated_spec_init->accept(*this);
       
   205     }
       
   206 /* enumerated_specification ASSIGN enumerated_value */
       
   207     void *visit(enumerated_spec_init_c *symbol) {
       
   208       return handle_type_spec(symbol->enumerated_specification, symbol->enumerated_value);
       
   209     }
       
   210 /* helper symbol for enumerated_specification->enumerated_spec_init */
       
   211 /* enumerated_value_list ',' enumerated_value */
       
   212     void *visit(enumerated_value_list_c *symbol) {
       
   213      /* if no initial value explicitly given, then use the lowest value of the subrange */
       
   214       return (void *)symbol->elements[0];
       
   215     }
       
   216 /* enumerated_type_name '#' identifier */
       
   217 // SYM_REF2(enumerated_value_c, type, value)
       
   218     void *visit(enumerated_value_c *symbol)	{ERROR; return NULL;}
       
   219 /*  identifier ':' array_spec_init */
       
   220     void *visit(array_type_declaration_c *symbol) {
       
   221       return symbol->array_spec_init->accept(*this);
       
   222     }
       
   223 /* array_specification [ASSIGN array_initialization} */
       
   224 /* array_initialization may be NULL ! */
       
   225     void *visit(array_spec_init_c *symbol) {
       
   226       return handle_type_spec(symbol->array_specification, symbol->array_initialization);
       
   227     }
       
   228 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
       
   229     void *visit(array_specification_c *symbol)	{
       
   230       //symbol_c *init_value = (symbol_c *)symbol->non_generic_type_name->accept(*this);
       
   231 
       
   232       /* Now build a array_initial_elements_list_c list, and populate it
       
   233        * with 1 element of the array_initial_elements_c class
       
   234        */
       
   235       /* The array_initial_elements_c will contain a reference to the init_value,
       
   236        * and another constant representing the number of elements in the array.
       
   237        * In essence, we are building the equivilant of the following ST/IL code:
       
   238        *    New_array_t : ARRAY [1..30, 51..60] of INT := [40(XXX)];
       
   239        * from the user given code
       
   240        *    New_array_t : ARRAY [1..30, 51..60] of INT;
       
   241        * and replacing XXX with the default initial value of INT.
       
   242        */
       
   243       /* now we need to determine the number of elements in the array... */
       
   244       /* Easier said than done, as the array may have a list of subranges, as in the
       
   245        * example given above!!
       
   246        */
       
   247       /* TODO !!!!!*/
       
   248       /* For now, just assume an array with 1 element.
       
   249        * I (Mario) want to finish off this part of the code before getting boged down
       
   250        * in something else...
       
   251        */
       
   252 	// NOTE: We are leaking memory, as the integer will never get free'd!!
       
   253       //integer_c *integer = new integer_c("1");
       
   254 	// NOTE: We are leaking memory, as the array_initial_elements will never get free'd!!
       
   255       //array_initial_elements_c *array_initial_elements = new array_initial_elements_c(integer, init_value);
       
   256 	// NOTE: We are leaking memory, as the array_initial_elements_list will never get free'd!!
       
   257       array_initial_elements_list_c *array_initial_elements_list  = new array_initial_elements_list_c();
       
   258       //array_initial_elements_list->add_element(array_initial_elements);
       
   259       return array_initial_elements_list;
       
   260     }
       
   261 /* helper symbol for array_specification */
       
   262 /* array_subrange_list ',' subrange */
       
   263     void *visit(array_subrange_list_c *symbol)	{ERROR; return NULL;}
       
   264 /* array_initialization:  '[' array_initial_elements_list ']' */
       
   265 /* helper symbol for array_initialization */
       
   266 /* array_initial_elements_list ',' array_initial_elements */
       
   267     void *visit(array_initial_elements_list_c *symbol)	{ERROR; return NULL;}
       
   268 /* integer '(' [array_initial_element] ')' */
       
   269 /* array_initial_element may be NULL ! */
       
   270     void *visit(array_initial_elements_c *symbol)	{ERROR; return NULL;}
       
   271 
       
   272 
       
   273 
       
   274     /* TODO: from this point forward... */
       
   275 
       
   276 /*  structure_type_name ':' structure_specification */
       
   277     void *visit(structure_type_declaration_c *symbol) {return NULL;}
       
   278 /* structure_type_name ASSIGN structure_initialization */
       
   279 /* structure_initialization may be NULL ! */
       
   280     void *visit(initialized_structure_c *symbol)	{
       
   281       return handle_type_spec(symbol->structure_type_name, symbol->structure_initialization);
       
   282     }
       
   283 /* helper symbol for structure_declaration */
       
   284 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   285 /* structure_element_declaration_list structure_element_declaration ';' */
       
   286     void *visit(structure_element_declaration_list_c *symbol)	{
       
   287       structure_element_initialization_list_c *structure_element_initialization_list = new structure_element_initialization_list_c();
       
   288       return structure_element_initialization_list;
       
   289     }
       
   290 /*  structure_element_name ':' *_spec_init */
       
   291     void *visit(structure_element_declaration_c *symbol)	{return NULL;}
       
   292 /* helper symbol for structure_initialization */
       
   293 /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   294 /* structure_element_initialization_list ',' structure_element_initialization */
       
   295     void *visit(structure_element_initialization_list_c *symbol)	{return NULL;}
       
   296 /*  structure_element_name ASSIGN value */
       
   297     void *visit(structure_element_initialization_c *symbol)	{return NULL;}
       
   298 /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
       
   299 /*
       
   300  * NOTE:
       
   301  * (Summary: Contrary to what is expected, the
       
   302  *           string_type_declaration_c is not used to store
       
   303  *           simple string type declarations that do not include
       
   304  *           size limits.
       
   305  *           For e.g.:
       
   306  *             str1_type: STRING := "hello!"
       
   307  *           will be stored in a simple_type_declaration_c
       
   308  *           instead of a string_type_declaration_c.
       
   309  *           The following:
       
   310  *             str2_type: STRING [64] := "hello!"
       
   311  *           will be stored in a sring_type_declaration_c
       
   312  *
       
   313  *           Read on for why this is done...
       
   314  * End Summary)
       
   315  *
       
   316  * According to the spec, the valid construct
       
   317  * TYPE new_str_type : STRING := "hello!"; END_TYPE
       
   318  * has two possible routes to type_declaration...
       
   319  *
       
   320  * Route 1:
       
   321  * type_declaration: single_element_type_declaration
       
   322  * single_element_type_declaration: simple_type_declaration
       
   323  * simple_type_declaration: identifier ':' simple_spec_init
       
   324  * simple_spec_init: simple_specification ASSIGN constant
       
   325  * (shift:  identifier <- 'new_str_type')
       
   326  * simple_specification: elementary_type_name
       
   327  * elementary_type_name: STRING
       
   328  * (shift: elementary_type_name <- STRING)
       
   329  * (reduce: simple_specification <- elementary_type_name)
       
   330  * (shift: constant <- "hello!")
       
   331  * (reduce: simple_spec_init: simple_specification ASSIGN constant)
       
   332  * (reduce: ...)
       
   333  *
       
   334  *
       
   335  * Route 2:
       
   336  * type_declaration: string_type_declaration
       
   337  * string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init
       
   338  * (shift:  identifier <- 'new_str_type')
       
   339  * elementary_string_type_name: STRING
       
   340  * (shift: elementary_string_type_name <- STRING)
       
   341  * (shift: string_type_declaration_size <-  empty )
       
   342  * string_type_declaration_init: ASSIGN character_string
       
   343  * (shift: character_string <- "hello!")
       
   344  * (reduce: string_type_declaration_init <- ASSIGN character_string)
       
   345  * (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init )
       
   346  * (reduce: type_declaration <- string_type_declaration)
       
   347  *
       
   348  *
       
   349  * At first glance it seems that removing route 1 would make
       
   350  * the most sense. Unfortunately the construct 'simple_spec_init'
       
   351  * shows up multiple times in other rules, so changing this construct
       
   352  * would also mean changing all the rules in which it appears.
       
   353  * I (Mario) therefore chose to remove route 2 instead. This means
       
   354  * that the above declaration gets stored in a
       
   355  * simple_type_declaration_c, and not in a string_type_declaration_c
       
   356  * as would be expected!
       
   357  */
       
   358 /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
       
   359 #if 0
       
   360 SYM_REF4(string_type_declaration_c,	string_type_name,
       
   361 					elementary_string_type_name,
       
   362 					string_type_declaration_size,
       
   363 					string_type_declaration_init) /* may be == NULL! */
       
   364 #endif
       
   365     void *visit(string_type_declaration_c *symbol)	{return NULL;}
       
   366 };
       
   367 
       
   368 type_initial_value_c	*type_initial_value_c::_instance = NULL;
       
   369 real_c			*type_initial_value_c::real_0 = NULL;
       
   370 integer_c		*type_initial_value_c::integer_0 = NULL;
       
   371 integer_c		*type_initial_value_c::integer_1 = NULL;
       
   372 boolean_literal_c	*type_initial_value_c::bool_0 = NULL;
       
   373 date_literal_c	*type_initial_value_c::date_literal_0 = NULL;
       
   374 daytime_c	*type_initial_value_c::daytime_literal_0 = NULL;
       
   375 duration_c	*type_initial_value_c::time_0 = NULL;
       
   376 date_c	*type_initial_value_c::date_0 = NULL;
       
   377 time_of_day_c	*type_initial_value_c::tod_0 = NULL;
       
   378 date_and_time_c	*type_initial_value_c::dt_0 = NULL;
       
   379 single_byte_character_string_c *type_initial_value_c::string_0 = NULL;
       
   380 double_byte_character_string_c *type_initial_value_c::wstring_0 = NULL;
       
   381 
       
   382 
       
   383 
       
   384 
       
   385 
       
   386