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 | // BasicInit.h --
|
---|
8 | //
|
---|
9 | // Author : Rodolfo G. Esteves
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Tue May 19 16:32:21 2015
|
---|
13 | // Update Count : 3
|
---|
14 | //
|
---|
15 |
|
---|
16 | #ifndef _BASINIT_H_
|
---|
17 | #define _BASINIT_H_
|
---|
18 |
|
---|
19 | #include <list>
|
---|
20 |
|
---|
21 | #include "SynTree/Visitor.h"
|
---|
22 | #include "SymTab/Indexer.h"
|
---|
23 |
|
---|
24 | #include "SynTree/Type.h"
|
---|
25 | #include "SynTree/Initializer.h"
|
---|
26 | #include "SynTree/Expression.h"
|
---|
27 | #include "NameInCollection.h"
|
---|
28 | #include "NameAssociation.h"
|
---|
29 |
|
---|
30 | namespace InitTweak {
|
---|
31 | bool isDeclStmtP(Statement *stmt);
|
---|
32 |
|
---|
33 | class BreakInitializer;
|
---|
34 | class BreakDesignator;
|
---|
35 |
|
---|
36 | class BasicInit: public Mutator {
|
---|
37 | public:
|
---|
38 | BasicInit() : bindings( 0 ) {}
|
---|
39 | BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {}
|
---|
40 | BasicInit( const BasicInit &other ) {
|
---|
41 | bindings = other.get_bindings();
|
---|
42 | index = other.index;
|
---|
43 | }
|
---|
44 |
|
---|
45 | ~BasicInit() { /* delete bindings; bindings = 0; */ }
|
---|
46 |
|
---|
47 | NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; }
|
---|
48 | void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) {
|
---|
49 | bindings = newValue;
|
---|
50 | }
|
---|
51 |
|
---|
52 | bool has_bindings() {
|
---|
53 | return ( get_bindings() != 0 || ! stmts.empty() );
|
---|
54 | }
|
---|
55 |
|
---|
56 | virtual ObjectDecl *mutate( ObjectDecl *objectDecl )
|
---|
57 | { index.visit( objectDecl ); return objectDecl; }
|
---|
58 | virtual TypeDecl *mutate( TypeDecl *typeDecl )
|
---|
59 | { index.visit( typeDecl ); return typeDecl; }
|
---|
60 | virtual TypedefDecl *mutate( TypedefDecl *typeDecl )
|
---|
61 | { index.visit( typeDecl ); return typeDecl; }
|
---|
62 | virtual StructDecl *mutate( StructDecl *aggregateDecl )
|
---|
63 | { index.visit( aggregateDecl ); return aggregateDecl; }
|
---|
64 | virtual UnionDecl *mutate( UnionDecl *aggregateDecl )
|
---|
65 | { index.visit( aggregateDecl ); return aggregateDecl; }
|
---|
66 | virtual EnumDecl *mutate( EnumDecl *aggregateDecl )
|
---|
67 | { index.visit( aggregateDecl ); return aggregateDecl; }
|
---|
68 |
|
---|
69 | virtual Type *mutate( StructInstType *aggrInst )
|
---|
70 | { index.visit( aggrInst ); return aggrInst; }
|
---|
71 | virtual Type *mutate( UnionInstType *aggrInst )
|
---|
72 | { index.visit( aggrInst ); return aggrInst; }
|
---|
73 |
|
---|
74 | virtual CompoundStmt *mutate(CompoundStmt *compoundStmt);
|
---|
75 | virtual Statement *mutate(DeclStmt *declStmt);
|
---|
76 |
|
---|
77 | std::list< Statement *> get_statements() const { return stmts; }
|
---|
78 |
|
---|
79 | static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, std::string aggName, std::list< Statement *> &stmts );
|
---|
80 | private:
|
---|
81 | NameAssociation< Expression *, BreakInitializer > *bindings;
|
---|
82 | Statement *assignFromDecl( DeclStmt *declStmt );
|
---|
83 | SymTab::Indexer index;
|
---|
84 | std::list< Statement *> stmts;
|
---|
85 |
|
---|
86 | class Classify {
|
---|
87 | public:
|
---|
88 | enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T };
|
---|
89 | enum InitKind { NULL_I, SINGLE_I, COMPOUND_I };
|
---|
90 |
|
---|
91 | static TypeKind type( Type * );
|
---|
92 | static InitKind initializer( Initializer *);
|
---|
93 |
|
---|
94 | static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index );
|
---|
95 | static std::list< Statement * >
|
---|
96 | matchInit( NameInCollection *, ObjectDecl *, Initializer * );
|
---|
97 | static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit );
|
---|
98 |
|
---|
99 | // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc );
|
---|
100 | };
|
---|
101 | };
|
---|
102 |
|
---|
103 | class BreakInitializer {
|
---|
104 | enum InitKind { EMPTY, SINGLE, COMPOUND };
|
---|
105 |
|
---|
106 | class BreakDesignator;
|
---|
107 | typedef BreakDesignator NameSplitter;
|
---|
108 |
|
---|
109 | public:
|
---|
110 | typedef std::list<Initializer *>::iterator element_iterator;
|
---|
111 | typedef std::list< NameSplitter >::iterator name_iterator;
|
---|
112 |
|
---|
113 | BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) {
|
---|
114 | std::list<Expression *> temp;
|
---|
115 |
|
---|
116 | if ( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) {
|
---|
117 | kind = SINGLE;
|
---|
118 | temp = sinit->get_designators();
|
---|
119 | } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) {
|
---|
120 | kind = COMPOUND;
|
---|
121 | temp = cinit->get_designators();
|
---|
122 | } // if
|
---|
123 |
|
---|
124 | std::transform( temp.begin(), temp.end(), std::back_inserter( designators ), ctor_noptr<NameSplitter, Expression *> );
|
---|
125 | }
|
---|
126 |
|
---|
127 | //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; }
|
---|
128 | ~BreakInitializer () {}
|
---|
129 |
|
---|
130 | BreakInitializer set_name( NameSplitter &name ) {
|
---|
131 | designators.clear();
|
---|
132 | designators.push_back( name );
|
---|
133 |
|
---|
134 | return *this;
|
---|
135 | }
|
---|
136 |
|
---|
137 | element_iterator element_begin() {
|
---|
138 | assert( cinit != 0 );
|
---|
139 | return cinit->begin_initializers();
|
---|
140 | }
|
---|
141 | element_iterator element_end() {
|
---|
142 | assert( cinit != 0 );
|
---|
143 | return cinit->end_initializers();
|
---|
144 | }
|
---|
145 |
|
---|
146 | name_iterator names_begin() { return designators.begin(); }
|
---|
147 | name_iterator names_end() { return designators.end(); }
|
---|
148 |
|
---|
149 | int names_size() const { return designators.size(); }
|
---|
150 |
|
---|
151 | bool has_index() const { return ! designators.empty(); }
|
---|
152 | bool is_single() const { return kind == SINGLE; }
|
---|
153 | bool is_composite() const { return kind == COMPOUND; }
|
---|
154 |
|
---|
155 | Expression *get_value() {
|
---|
156 | switch ( kind ) {
|
---|
157 | case EMPTY:
|
---|
158 | return 0;
|
---|
159 | break;
|
---|
160 | case SINGLE:
|
---|
161 | return sinit->get_value();
|
---|
162 | break;
|
---|
163 | case COMPOUND:
|
---|
164 | assert(false);
|
---|
165 | break;
|
---|
166 | default:
|
---|
167 | assert(false);
|
---|
168 | } // switch
|
---|
169 | return 0;
|
---|
170 | }
|
---|
171 | // attributes
|
---|
172 | private:
|
---|
173 | InitKind kind;
|
---|
174 | SingleInit *sinit;
|
---|
175 | ListInit *cinit;
|
---|
176 | std::list< BreakDesignator > designators;
|
---|
177 | // helper classes
|
---|
178 | public:
|
---|
179 | class BreakDesignator {
|
---|
180 | public:
|
---|
181 | BreakDesignator( Expression *exp ) {
|
---|
182 | Expression *prfx = exp;
|
---|
183 | UntypedMemberExpr *me = 0;
|
---|
184 |
|
---|
185 | do {
|
---|
186 | if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break;
|
---|
187 | blown_struct.push_front( me->get_member() );
|
---|
188 | prfx = me->get_aggregate();
|
---|
189 | } while ( prfx != 0 );
|
---|
190 |
|
---|
191 | NameExpr *ne;
|
---|
192 | if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 )
|
---|
193 | blown_struct.push_front( ne->get_name() );
|
---|
194 | }
|
---|
195 |
|
---|
196 | BreakDesignator( std::string name ) {
|
---|
197 | blown_struct.push_front( name );
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool is_flat() const { return blown_struct.size() == 1; }
|
---|
201 | bool is_nested() const { return blown_struct.size() > 1; }
|
---|
202 |
|
---|
203 | std::string get_name() { return blown_struct.front(); }
|
---|
204 |
|
---|
205 | BreakDesignator &name_remainder() {
|
---|
206 | blown_struct.pop_front();
|
---|
207 | return *this;
|
---|
208 | }
|
---|
209 |
|
---|
210 | private:
|
---|
211 | std::list< std::string > blown_struct;
|
---|
212 | };
|
---|
213 | };
|
---|
214 | } // namespace InitTweak
|
---|
215 |
|
---|
216 | #endif // _BASINIT_H_
|
---|
217 |
|
---|
218 | // Local Variables: //
|
---|
219 | // tab-width: 4 //
|
---|
220 | // mode: c++ //
|
---|
221 | // compile-command: "make install" //
|
---|
222 | // End: //
|
---|