source: translator/Parser/InitializerNode.cc @ 134b86a

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 134b86a was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

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