stage3/narrow_candidate_datatypes.cc
changeset 806 f218434eeaf8
parent 798 d21e598b0b75
child 807 4d71292f8732
equal deleted inserted replaced
805:b737cfc92614 806:f218434eeaf8
   475 /* empty!! */
   475 /* empty!! */
   476 
   476 
   477 /********************************/
   477 /********************************/
   478 /* B 1.3.3 - Derived data types */
   478 /* B 1.3.3 - Derived data types */
   479 /********************************/
   479 /********************************/
       
   480 void *narrow_candidate_datatypes_c::narrow_spec_init(symbol_c *symbol, symbol_c *type_decl, symbol_c *init_value) {
       
   481 	// If we are handling an anonymous datatype (i.e. a datatype implicitly declared inside a VAR ... END_VAR declaration)
       
   482 	// then the symbol->datatype has not yet been set by the previous visit(type_decl) method, because it does not exist!
       
   483 	// So we set the datatype ourselves!
       
   484 	if ((NULL == symbol->datatype) && (symbol->candidate_datatypes.size() == 1))
       
   485 		symbol->datatype = symbol->candidate_datatypes[0];
       
   486   
       
   487 	set_datatype(symbol->datatype, type_decl);
       
   488 	type_decl->accept(*this);
       
   489 
       
   490 	if (NULL != init_value) {
       
   491 		set_datatype(symbol->datatype, init_value);
       
   492 		init_value->accept(*this);
       
   493 	}
       
   494 	return NULL;
       
   495 }
       
   496 
       
   497 
       
   498 void *narrow_candidate_datatypes_c::narrow_type_decl(symbol_c *symbol, symbol_c *type_name, symbol_c *spec_init) {
       
   499 	if (symbol->candidate_datatypes.size() == 1) {
       
   500 		symbol->datatype = symbol->candidate_datatypes[0];
       
   501   
       
   502 		set_datatype(symbol->datatype, type_name);
       
   503 		set_datatype(symbol->datatype, spec_init);
       
   504 		spec_init->accept(*this);
       
   505 	}
       
   506 	return NULL;
       
   507 }
       
   508 
       
   509 
   480 /*  TYPE type_declaration_list END_TYPE */
   510 /*  TYPE type_declaration_list END_TYPE */
   481 // SYM_REF1(data_type_declaration_c, type_declaration_list)
   511 // SYM_REF1(data_type_declaration_c, type_declaration_list)
   482 /* NOTE: Not required. already handled by iterator_visitor_c base class */
   512 /* NOTE: Not required. already handled by iterator_visitor_c base class */
   483 
   513 
   484 /* helper symbol for data_type_declaration */
   514 /* helper symbol for data_type_declaration */
   485 // SYM_LIST(type_declaration_list_c)
   515 // SYM_LIST(type_declaration_list_c)
   486 /* NOTE: Not required. already handled by iterator_visitor_c base class */
   516 /* NOTE: Not required. already handled by iterator_visitor_c base class */
   487 
   517 
   488 /*  simple_type_name ':' simple_spec_init */
   518 /*  simple_type_name ':' simple_spec_init */
   489 // SYM_REF2(simple_type_declaration_c, simple_type_name, simple_spec_init)
   519 // SYM_REF2(simple_type_declaration_c, simple_type_name, simple_spec_init)
   490 /* NOTE: Not required. already handled by iterator_visitor_c base class */
   520 void *narrow_candidate_datatypes_c::visit(simple_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->simple_type_name, symbol->simple_spec_init);}
   491 
   521 
   492 /* simple_specification ASSIGN constant */
   522 /* simple_specification ASSIGN constant */
   493 // SYM_REF2(simple_spec_init_c, simple_specification, constant)
   523 // SYM_REF2(simple_spec_init_c, simple_specification, constant)
   494 void *narrow_candidate_datatypes_c::visit(simple_spec_init_c *symbol) {
   524 void *narrow_candidate_datatypes_c::visit(simple_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->simple_specification, symbol->constant);}
   495 	if (symbol->candidate_datatypes.size() == 1)
       
   496 	  symbol->datatype = symbol->candidate_datatypes[0];
       
   497 	  
       
   498 	if (symbol->simple_specification->candidate_datatypes.size() == 1)
       
   499 	  symbol->simple_specification->datatype = symbol->simple_specification->candidate_datatypes[0];
       
   500 
       
   501 	if (NULL != symbol->constant) {
       
   502 		set_datatype(symbol->datatype, symbol->constant);
       
   503 		symbol->constant->accept(*this);
       
   504 	}
       
   505 	return NULL;
       
   506 }
       
   507 
       
   508 
   525 
   509 /*  subrange_type_name ':' subrange_spec_init */
   526 /*  subrange_type_name ':' subrange_spec_init */
   510 // SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init)
   527 // SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init)
       
   528 void *narrow_candidate_datatypes_c::visit(subrange_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->subrange_type_name, symbol->subrange_spec_init);}
   511 
   529 
   512 /* subrange_specification ASSIGN signed_integer */
   530 /* subrange_specification ASSIGN signed_integer */
   513 // SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer)
   531 // SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer)
       
   532 void *narrow_candidate_datatypes_c::visit(subrange_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->subrange_specification, symbol->signed_integer);}
   514 
   533 
   515 /*  integer_type_name '(' subrange')' */
   534 /*  integer_type_name '(' subrange')' */
   516 // SYM_REF2(subrange_specification_c, integer_type_name, subrange)
   535 // SYM_REF2(subrange_specification_c, integer_type_name, subrange)
       
   536 void *narrow_candidate_datatypes_c::visit(subrange_specification_c *symbol) {
       
   537 	set_datatype(symbol->datatype, symbol->integer_type_name);
       
   538 	symbol->integer_type_name->accept(*this);
       
   539 	set_datatype(symbol->datatype, symbol->integer_type_name);
       
   540 	symbol->integer_type_name->accept(*this);
       
   541 	return NULL;
       
   542 }
   517 
   543 
   518 /*  signed_integer DOTDOT signed_integer */
   544 /*  signed_integer DOTDOT signed_integer */
   519 /* dimension will be filled in during stage 3 (array_range_check_c) with the number of elements in this subrange */
   545 /* dimension will be filled in during stage 3 (array_range_check_c) with the number of elements in this subrange */
   520 // SYM_REF2(subrange_c, lower_limit, upper_limit, unsigned long long int dimension;)
   546 // SYM_REF2(subrange_c, lower_limit, upper_limit, unsigned long long int dimension;)
   521 void *narrow_candidate_datatypes_c::visit(subrange_c *symbol) {
   547 void *narrow_candidate_datatypes_c::visit(subrange_c *symbol) {
   522 	symbol->lower_limit->datatype = symbol->datatype;
   548 	set_datatype(symbol->datatype, symbol->lower_limit);
   523 	symbol->lower_limit->accept(*this);
   549 	symbol->lower_limit->accept(*this);
   524 	symbol->upper_limit->datatype = symbol->datatype;
   550 	set_datatype(symbol->datatype, symbol->upper_limit);
   525 	symbol->upper_limit->accept(*this);
   551 	symbol->upper_limit->accept(*this);
   526 	return NULL;
   552 	return NULL;
   527 }
   553 }
   528 
   554 
   529 
   555 
   530 /*  enumerated_type_name ':' enumerated_spec_init */
   556 /*  enumerated_type_name ':' enumerated_spec_init */
   531 // SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init)
   557 // SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init)
   532 void *narrow_candidate_datatypes_c::visit(enumerated_type_declaration_c *symbol) {
   558 void *narrow_candidate_datatypes_c::visit(enumerated_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->enumerated_type_name, symbol->enumerated_spec_init);}
   533   if (symbol->candidate_datatypes.size() != 1) ERROR;
       
   534   
       
   535   symbol->datatype = symbol->candidate_datatypes[0];
       
   536   set_datatype(symbol->datatype, symbol->enumerated_type_name);
       
   537   set_datatype(symbol->datatype, symbol->enumerated_spec_init);
       
   538   
       
   539   symbol->enumerated_spec_init->accept(*this);
       
   540   return NULL;
       
   541 }
       
   542 
   559 
   543 
   560 
   544 /* enumerated_specification ASSIGN enumerated_value */
   561 /* enumerated_specification ASSIGN enumerated_value */
   545 // SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value)
   562 // SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value)
   546 void *narrow_candidate_datatypes_c::visit(enumerated_spec_init_c *symbol) {
   563 void *narrow_candidate_datatypes_c::visit(enumerated_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->enumerated_specification, symbol->enumerated_value);}
   547   /* If we are handling an anonymous datatype (i.e. a datatype implicitly declared inside a VAR ... END_VAR declaration)
       
   548    * then the symbol->datatype has not yet been set by the previous visit(enumerated_spec_init_c) method!
       
   549    */
       
   550   if (NULL == symbol->datatype) {
       
   551     if (symbol->candidate_datatypes.size() != 1) ERROR;
       
   552     symbol->datatype = symbol->candidate_datatypes[0];
       
   553   }
       
   554   set_datatype(symbol->datatype, symbol->enumerated_specification);
       
   555   if (NULL != symbol->enumerated_value) 
       
   556     set_datatype(symbol->datatype, symbol->enumerated_value);
       
   557 
       
   558   symbol->enumerated_specification->accept(*this); /* calls enumerated_value_list_c (or identifier_c, which we ignore!) visit method */
       
   559   return NULL;
       
   560 }
       
   561 
   564 
   562 /* helper symbol for enumerated_specification->enumerated_spec_init */
   565 /* helper symbol for enumerated_specification->enumerated_spec_init */
   563 /* enumerated_value_list ',' enumerated_value */
   566 /* enumerated_value_list ',' enumerated_value */
   564 // SYM_LIST(enumerated_value_list_c)
   567 // SYM_LIST(enumerated_value_list_c)
   565 void *narrow_candidate_datatypes_c::visit(enumerated_value_list_c *symbol) {
   568 void *narrow_candidate_datatypes_c::visit(enumerated_value_list_c *symbol) {
   566   if (NULL == symbol->datatype) ERROR;
   569 //if (NULL == symbol->datatype) ERROR;  // Comented out-> Reserve this check for the print_datatypes_error_c ???  
   567   
   570   for(int i = 0; i < symbol->n; i++) set_datatype(symbol->datatype, symbol->elements[i]);
   568   for(int i = 0; i < symbol->n; i++) {
   571 //for(int i = 0; i < symbol->n; i++) if (NULL == symbol->elements[i]->datatype) ERROR; // Comented out-> Reserve this check for the print_datatypes_error_c ???  
   569     /* the enumerated_value_c objects to which this list points to has both the datatype and the candidate_datatype_list filled in, so we 
       
   570      * call set_datatype() instead of setting the datatype directly!
       
   571      */
       
   572     set_datatype(symbol->datatype, symbol->elements[i]);
       
   573     if (NULL == symbol->elements[i]->datatype) ERROR;
       
   574   }
       
   575   return NULL;  
   572   return NULL;  
   576 }
   573 }
   577 
   574 
   578 
   575 
   579 /* enumerated_type_name '#' identifier */
   576 /* enumerated_type_name '#' identifier */
   581 // void *narrow_candidate_datatypes_c::visit(enumerated_value_c *symbol) {/* do nothing! */ return NULL;}
   578 // void *narrow_candidate_datatypes_c::visit(enumerated_value_c *symbol) {/* do nothing! */ return NULL;}
   582 
   579 
   583 
   580 
   584 /*  identifier ':' array_spec_init */
   581 /*  identifier ':' array_spec_init */
   585 // SYM_REF2(array_type_declaration_c, identifier, array_spec_init)
   582 // SYM_REF2(array_type_declaration_c, identifier, array_spec_init)
       
   583 void *narrow_candidate_datatypes_c::visit(array_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->identifier, symbol->array_spec_init);}
   586 
   584 
   587 /* array_specification [ASSIGN array_initialization} */
   585 /* array_specification [ASSIGN array_initialization} */
   588 /* array_initialization may be NULL ! */
   586 /* array_initialization may be NULL ! */
   589 // SYM_REF2(array_spec_init_c, array_specification, array_initialization)
   587 // SYM_REF2(array_spec_init_c, array_specification, array_initialization)
       
   588 void *narrow_candidate_datatypes_c::visit(array_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->array_specification, symbol->array_initialization);}
   590 
   589 
   591 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
   590 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
   592 // SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name)
   591 // SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name)
       
   592 // Not needed!!
   593 
   593 
   594 /* helper symbol for array_specification */
   594 /* helper symbol for array_specification */
   595 /* array_subrange_list ',' subrange */
   595 /* array_subrange_list ',' subrange */
   596 // SYM_LIST(array_subrange_list_c)
   596 // SYM_LIST(array_subrange_list_c)
       
   597 // Not needed ??
   597 
   598 
   598 /* array_initialization:  '[' array_initial_elements_list ']' */
   599 /* array_initialization:  '[' array_initial_elements_list ']' */
   599 /* helper symbol for array_initialization */
   600 /* helper symbol for array_initialization */
   600 /* array_initial_elements_list ',' array_initial_elements */
   601 /* array_initial_elements_list ',' array_initial_elements */
   601 // SYM_LIST(array_initial_elements_list_c)
   602 // SYM_LIST(array_initial_elements_list_c)
       
   603 // Not needed ???
   602 
   604 
   603 /* integer '(' [array_initial_element] ')' */
   605 /* integer '(' [array_initial_element] ')' */
   604 /* array_initial_element may be NULL ! */
   606 /* array_initial_element may be NULL ! */
   605 // SYM_REF2(array_initial_elements_c, integer, array_initial_element)
   607 // SYM_REF2(array_initial_elements_c, integer, array_initial_element)
       
   608 // Not needed ???
   606 
   609 
   607 /*  structure_type_name ':' structure_specification */
   610 /*  structure_type_name ':' structure_specification */
   608 // SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
   611 // SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
       
   612 void *narrow_candidate_datatypes_c::visit(structure_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->structure_type_name, symbol->structure_specification);}
   609 
   613 
   610 /* structure_type_name ASSIGN structure_initialization */
   614 /* structure_type_name ASSIGN structure_initialization */
   611 /* structure_initialization may be NULL ! */
   615 /* structure_initialization may be NULL ! */
   612 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
   616 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
       
   617 void *narrow_candidate_datatypes_c::visit(initialized_structure_c *symbol) {return narrow_spec_init(symbol, symbol->structure_type_name, symbol->structure_initialization);}
   613 
   618 
   614 /* helper symbol for structure_declaration */
   619 /* helper symbol for structure_declaration */
   615 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
   620 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
   616 /* structure_element_declaration_list structure_element_declaration ';' */
   621 /* structure_element_declaration_list structure_element_declaration ';' */
   617 // SYM_LIST(structure_element_declaration_list_c)
   622 // SYM_LIST(structure_element_declaration_list_c)
       
   623 // Not needed ???
   618 
   624 
   619 /*  structure_element_name ':' *_spec_init */
   625 /*  structure_element_name ':' *_spec_init */
   620 // SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init)
   626 // SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init)
       
   627 // Not needed ???
   621 
   628 
   622 /* helper symbol for structure_initialization */
   629 /* helper symbol for structure_initialization */
   623 /* structure_initialization: '(' structure_element_initialization_list ')' */
   630 /* structure_initialization: '(' structure_element_initialization_list ')' */
   624 /* structure_element_initialization_list ',' structure_element_initialization */
   631 /* structure_element_initialization_list ',' structure_element_initialization */
   625 // SYM_LIST(structure_element_initialization_list_c)
   632 // SYM_LIST(structure_element_initialization_list_c)
       
   633 // Not needed ???
   626 
   634 
   627 /*  structure_element_name ASSIGN value */
   635 /*  structure_element_name ASSIGN value */
   628 // SYM_REF2(structure_element_initialization_c, structure_element_name, value)
   636 // SYM_REF2(structure_element_initialization_c, structure_element_name, value)
       
   637 // Not needed ???
   629 
   638 
   630 /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
   639 /*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
   631 // SYM_REF4(string_type_declaration_c, string_type_name, elementary_string_type_name, string_type_declaration_size, string_type_declaration_init/* may be == NULL! */) 
   640 // SYM_REF4(string_type_declaration_c, string_type_name, elementary_string_type_name, string_type_declaration_size, string_type_declaration_init/* may be == NULL! */) 
   632 
   641 
   633 
   642