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 : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Aug 15 18:27:02 2016 |
---|
13 | // Update Count : 20 |
---|
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_next() ); |
---|
28 | |
---|
29 | if ( kids != 0 ) |
---|
30 | set_last( 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_last( init ); |
---|
37 | |
---|
38 | if ( aggrp ) |
---|
39 | kids = dynamic_cast< InitializerNode * >( get_next() ); |
---|
40 | |
---|
41 | if ( kids != 0 ) |
---|
42 | set_next( 0 ); |
---|
43 | } |
---|
44 | |
---|
45 | InitializerNode::~InitializerNode() { |
---|
46 | delete expr; |
---|
47 | delete designator; |
---|
48 | delete kids; |
---|
49 | } |
---|
50 | |
---|
51 | void InitializerNode::print( std::ostream &os, int indent ) const { |
---|
52 | os << std::string( indent, ' ' ) << "Initializer expression" << std::endl; |
---|
53 | } |
---|
54 | |
---|
55 | void InitializerNode::printOneLine( std::ostream &os ) const { |
---|
56 | if ( ! aggregate ) { |
---|
57 | if ( designator != 0 ) { |
---|
58 | os << "designated by: ("; |
---|
59 | ExpressionNode *curdes = designator; |
---|
60 | while ( curdes != 0) { |
---|
61 | curdes->printOneLine(os); |
---|
62 | curdes = (ExpressionNode *)(curdes->get_next()); |
---|
63 | if ( curdes ) os << ", "; |
---|
64 | } // while |
---|
65 | os << ")"; |
---|
66 | } // if |
---|
67 | if ( expr ) expr->printOneLine(os); |
---|
68 | } else { // It's an aggregate |
---|
69 | os << "[--"; |
---|
70 | if ( next_init() != 0 ) |
---|
71 | next_init()->printOneLine(os); |
---|
72 | if (aggregate) os << "--]"; |
---|
73 | } // if |
---|
74 | |
---|
75 | InitializerNode *moreInit; |
---|
76 | if ( get_next() != 0 && ((moreInit = dynamic_cast< InitializerNode * >( get_next() ) ) != 0) ) |
---|
77 | moreInit->printOneLine( os ); |
---|
78 | } |
---|
79 | |
---|
80 | Initializer *InitializerNode::build() const { |
---|
81 | // if ( get_expression() == 0 ) return 0; // XXX (?) |
---|
82 | |
---|
83 | if ( aggregate ) { |
---|
84 | //assert( next_init() != 0 ); |
---|
85 | |
---|
86 | std::list< Initializer * > initlist; |
---|
87 | buildList< Initializer, InitializerNode >( next_init(), initlist ); |
---|
88 | |
---|
89 | std::list< Expression * > designlist; |
---|
90 | |
---|
91 | if ( designator != 0 ) { |
---|
92 | buildList< Expression, ExpressionNode >( designator, designlist ); |
---|
93 | } // if |
---|
94 | |
---|
95 | return new ListInit( initlist, designlist, maybeConstructed ); |
---|
96 | } else { |
---|
97 | std::list< Expression * > designators; |
---|
98 | |
---|
99 | if ( designator != 0 ) |
---|
100 | buildList< Expression, ExpressionNode >( designator, designators ); |
---|
101 | |
---|
102 | if ( get_expression() != 0) |
---|
103 | return new SingleInit( maybeBuild< Expression >( get_expression() ), designators, maybeConstructed ); |
---|
104 | } // if |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | // Local Variables: // |
---|
110 | // tab-width: 4 // |
---|
111 | // mode: c++ // |
---|
112 | // compile-command: "make install" // |
---|
113 | // End: // |
---|