1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // InitializerNode.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Sat May 16 13:20:24 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Thu Jan 07 13:32:57 2016 |
---|
13 | // Update Count : 13 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> |
---|
17 | #include <iostream> |
---|
18 | using namespace std; |
---|
19 | |
---|
20 | #include "ParseNode.h" |
---|
21 | #include "SynTree/Expression.h" |
---|
22 | #include "SynTree/Initializer.h" |
---|
23 | |
---|
24 | InitializerNode::InitializerNode( ExpressionNode *_expr, bool aggrp, ExpressionNode *des ) |
---|
25 | : expr( _expr ), aggregate( aggrp ), designator( des ), kids( 0 ), maybeConstructed( true ) { |
---|
26 | if ( aggrp ) |
---|
27 | kids = dynamic_cast< InitializerNode *>( get_link() ); |
---|
28 | |
---|
29 | if ( kids != 0 ) |
---|
30 | set_link( 0 ); |
---|
31 | } |
---|
32 | |
---|
33 | InitializerNode::InitializerNode( InitializerNode *init, bool aggrp, ExpressionNode *des ) |
---|
34 | : expr( 0 ), aggregate( aggrp ), designator( des ), kids( 0 ), maybeConstructed( true ) { |
---|
35 | if ( init != 0 ) |
---|
36 | set_link(init); |
---|
37 | |
---|
38 | if ( aggrp ) |
---|
39 | kids = dynamic_cast< InitializerNode *>( get_link() ); |
---|
40 | |
---|
41 | if ( kids != 0 ) |
---|
42 | set_next( 0 ); |
---|
43 | } |
---|
44 | |
---|
45 | InitializerNode::~InitializerNode() { |
---|
46 | delete expr; |
---|
47 | } |
---|
48 | |
---|
49 | void InitializerNode::print( std::ostream &os, int indent ) const { |
---|
50 | os << std::string( indent, ' ' ) << "Initializer expression" << std::endl; |
---|
51 | } |
---|
52 | |
---|
53 | void InitializerNode::printOneLine( std::ostream &os ) const { |
---|
54 | if ( ! aggregate ) { |
---|
55 | if ( designator != 0 ) { |
---|
56 | os << "designated by: ("; |
---|
57 | ExpressionNode *curdes = designator; |
---|
58 | while ( curdes != 0) { |
---|
59 | curdes->printOneLine(os); |
---|
60 | curdes = (ExpressionNode *)(curdes->get_link()); |
---|
61 | if ( curdes ) os << ", "; |
---|
62 | } // while |
---|
63 | os << ")"; |
---|
64 | } // if |
---|
65 | if ( expr ) expr->printOneLine(os); |
---|
66 | } else { // It's an aggregate |
---|
67 | os << "[--"; |
---|
68 | if ( next_init() != 0 ) |
---|
69 | next_init()->printOneLine(os); |
---|
70 | if (aggregate) os << "--]"; |
---|
71 | } // if |
---|
72 | |
---|
73 | InitializerNode *moreInit; |
---|
74 | if ( get_link() != 0 && ((moreInit = dynamic_cast< InitializerNode * >( get_link() ) ) != 0) ) |
---|
75 | moreInit->printOneLine( os ); |
---|
76 | } |
---|
77 | |
---|
78 | Initializer *InitializerNode::build() const { |
---|
79 | // if ( get_expression() == 0 ) return 0; // XXX (?) |
---|
80 | |
---|
81 | if ( aggregate ) { |
---|
82 | //assert( next_init() != 0 ); |
---|
83 | |
---|
84 | std::list< Initializer *> initlist; |
---|
85 | buildList<Initializer, InitializerNode>( next_init(), initlist ); |
---|
86 | |
---|
87 | std::list< Expression *> designlist; |
---|
88 | |
---|
89 | if ( designator != 0 ) { |
---|
90 | buildList<Expression, ExpressionNode>( designator, designlist ); |
---|
91 | } // if |
---|
92 | |
---|
93 | return new ListInit( initlist, designlist, maybeConstructed ); |
---|
94 | } else { |
---|
95 | std::list< Expression *> designators; |
---|
96 | |
---|
97 | if ( designator != 0 ) |
---|
98 | buildList<Expression, ExpressionNode>( designator, designators ); |
---|
99 | |
---|
100 | if ( get_expression() != 0) |
---|
101 | return new SingleInit( get_expression()->build(), designators, maybeConstructed ); |
---|
102 | } // if |
---|
103 | |
---|
104 | return 0; |
---|
105 | } |
---|
106 | |
---|
107 | // Local Variables: // |
---|
108 | // tab-width: 4 // |
---|
109 | // mode: c++ // |
---|
110 | // compile-command: "make install" // |
---|
111 | // End: // |
---|