source: translator/Parser.old/InitializerNode.cc @ 51b7345

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 51b7345 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1#include "ParseNode.h"
2#include "SynTree/Expression.h"
3#include "SynTree/Initializer.h"
4#include "utility.h"
5#include "SemanticError.h"
6// #include <cstdlib> // for strtol
7#include <cassert>
8
9InitializerNode::InitializerNode( ExpressionNode *_expr, bool aggrp, ExpressionNode *des )
10  : ParseNode( "", _expr->get_filename(), _expr->get_lineno() ), expr( _expr ), aggregate( aggrp ), designator( des ), kids( 0 )
11{
12  if( des && des->get_lineno() < lineno ) {
13    lineno = des->get_lineno();
14  }
15
16  if ( aggrp )
17    kids = dynamic_cast< InitializerNode *>( get_link() );
18
19  if ( kids != 0 )
20    set_link( 0 );
21}
22
23InitializerNode::InitializerNode( InitializerNode *init, bool aggrp, ExpressionNode *des )
24  : ParseNode( "", init->get_filename(), init->get_lineno() ), aggregate( aggrp ), designator( des ), kids( 0 )
25{
26  if( des && des->get_lineno() < lineno ) {
27    lineno = des->get_lineno();
28  }
29
30  if (init != 0)
31    set_link(init);
32
33  if ( aggrp )
34      kids = dynamic_cast< InitializerNode *>( get_link() );
35
36  if ( kids != 0 )
37    set_next( 0 );
38}
39
40InitializerNode::~InitializerNode() {
41  delete expr;
42}
43
44void InitializerNode::print( std::ostream &os, int indent ) const {
45  os << std::string(indent, ' ') << "Initializer expression" << std::endl;
46}
47
48void InitializerNode::printOneLine( std::ostream &os ) const {
49  if (!aggregate)
50    {
51
52      if ( designator != 0 )
53        {
54          os << "designated by: (";
55          ExpressionNode  *curdes = designator;
56          while( curdes != 0){
57            curdes->printOneLine(os);
58            curdes = (ExpressionNode *)(curdes->get_link());
59            if(curdes) os << ", ";
60          }
61          os << ")";
62        }
63
64      if (expr) expr->printOneLine(os);
65    }
66  else  // It's an aggregate
67    {
68      os << "[";
69      if( next_init() != 0 ) 
70        next_init()->printOneLine(os);
71
72      if (aggregate) os << "]";
73    }
74 
75  InitializerNode * moreInit;
76  if  ( get_link() != 0 && ((moreInit = dynamic_cast< InitializerNode * >( get_link() ) ) != 0) ) 
77    moreInit->printOneLine( os );
78}
79
80Initializer *InitializerNode::build() const {
81  if ( aggregate )
82    {
83      assert( next_init() != 0 );
84
85      std::list< Initializer *> initlist;
86      buildList<Initializer, InitializerNode>( next_init(), initlist );
87
88      std::list< Expression *> designlist;
89      if ( designator != 0 )
90        buildList<Expression, ExpressionNode>( designator, designlist );
91
92      return new ListInit( initlist, designlist );
93    }
94  else
95    {
96      std::list< Expression *> designators;
97
98      if ( designator != 0 ) 
99        buildList<Expression, ExpressionNode>( designator, designators );
100
101      return new SingleInit( get_expression()->build(), designators );
102    }
103
104  return 0;  // shouldn't be here
105}
106
107
Note: See TracBrowser for help on using the repository browser.