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 | // CurrentObject.h -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Tue Jun 13 15:28:32 2017 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Tue Jun 13 15:28:44 2017 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #include <stack> |
---|
17 | #include <iostream> |
---|
18 | |
---|
19 | #include "CurrentObject.h" |
---|
20 | |
---|
21 | #include "Common/Indenter.h" |
---|
22 | |
---|
23 | #include "SynTree/Declaration.h" |
---|
24 | #include "SynTree/Initializer.h" |
---|
25 | #include "SynTree/Type.h" |
---|
26 | #include "SynTree/TypeSubstitution.h" |
---|
27 | |
---|
28 | #if 0 |
---|
29 | #define PRINT(x) x |
---|
30 | #else |
---|
31 | #define PRINT(x) |
---|
32 | #endif |
---|
33 | |
---|
34 | namespace ResolvExpr { |
---|
35 | long long int getConstValue( ConstantExpr * constExpr ) { |
---|
36 | if ( BasicType * basicType = dynamic_cast< BasicType * >( constExpr->get_result() ) ) { |
---|
37 | if ( basicType->isInteger() ) { |
---|
38 | return constExpr->get_constant()->get_ival(); |
---|
39 | } else { |
---|
40 | assertf( false, "Non-integer constant expression in getConstValue", toString( constExpr ).c_str() ); // xxx - might be semantic error |
---|
41 | } |
---|
42 | } else if ( dynamic_cast< OneType * >( constExpr->get_result() ) ) { |
---|
43 | return 1; |
---|
44 | } else if ( dynamic_cast< ZeroType * >( constExpr->get_result() ) ) { |
---|
45 | return 0; |
---|
46 | } else { |
---|
47 | assertf( false, "unhandled type on getConstValue %s", toString( constExpr->get_result() ).c_str() ); // xxx - might be semantic error |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | template< typename AggrInst > |
---|
52 | TypeSubstitution makeGenericSubstitution( AggrInst * inst ) { |
---|
53 | assert( inst ); |
---|
54 | assert( inst->get_baseParameters() ); |
---|
55 | std::list< TypeDecl * > baseParams = *inst->get_baseParameters(); |
---|
56 | std::list< Expression * > typeSubs = inst->get_parameters(); |
---|
57 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); |
---|
58 | return subs; |
---|
59 | } |
---|
60 | |
---|
61 | TypeSubstitution makeGenericSubstitution( Type * type ) { |
---|
62 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { |
---|
63 | return makeGenericSubstitution( inst ); |
---|
64 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { |
---|
65 | return makeGenericSubstitution( inst ); |
---|
66 | } else { |
---|
67 | return TypeSubstitution(); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | class MemberIterator { |
---|
72 | public: |
---|
73 | virtual ~MemberIterator() {} |
---|
74 | |
---|
75 | virtual void setPosition( std::list< Expression * > & designators ) = 0; |
---|
76 | virtual std::list<InitAlternative> operator*() const = 0; |
---|
77 | virtual operator bool() const = 0; |
---|
78 | virtual MemberIterator & bigStep() = 0; |
---|
79 | virtual MemberIterator & smallStep() = 0; |
---|
80 | virtual Type * getType() = 0; |
---|
81 | virtual Type * getNext() = 0; |
---|
82 | |
---|
83 | virtual void print( std::ostream & out, Indenter indent ) const = 0; |
---|
84 | |
---|
85 | virtual std::list<InitAlternative> first() const = 0; // should be protected |
---|
86 | }; |
---|
87 | |
---|
88 | std::ostream & operator<<(std::ostream & out, const MemberIterator & it) { |
---|
89 | Indenter indenter; |
---|
90 | it.print( out, indenter ); |
---|
91 | return out; |
---|
92 | } |
---|
93 | |
---|
94 | /// create a new MemberIterator that traverses a type correctly |
---|
95 | MemberIterator * createMemberIterator( Type * type ); |
---|
96 | |
---|
97 | /// iterates "other" types, e.g. basic types, pointer types, etc. which do not change at list initializer entry |
---|
98 | class SimpleIterator : public MemberIterator { |
---|
99 | public: |
---|
100 | SimpleIterator( Type * type ) : type( type ) {} |
---|
101 | |
---|
102 | virtual void setPosition( std::list< Expression * > & designators ) { |
---|
103 | assertf( designators.empty(), "simple iterator given non-empty designator..." ); // xxx - might be semantic error |
---|
104 | } |
---|
105 | |
---|
106 | virtual std::list<InitAlternative> operator*() const { return first(); } |
---|
107 | virtual operator bool() const { return type; } |
---|
108 | |
---|
109 | // big step is the same as small step |
---|
110 | virtual MemberIterator & bigStep() { return smallStep(); } |
---|
111 | virtual MemberIterator & smallStep() { |
---|
112 | type = nullptr; // type is nullified on increment since SimpleIterators do not have members |
---|
113 | return *this; |
---|
114 | } |
---|
115 | |
---|
116 | virtual void print( std::ostream & out, __attribute__((unused)) Indenter indent ) const { |
---|
117 | out << "SimpleIterator(" << type << ")"; |
---|
118 | } |
---|
119 | |
---|
120 | virtual Type * getType() { return type; } |
---|
121 | virtual Type * getNext() { return type; } |
---|
122 | |
---|
123 | protected: |
---|
124 | virtual std::list<InitAlternative> first() const { |
---|
125 | if ( type ) return std::list<InitAlternative>{ { type->clone(), new Designation( {} ) } }; |
---|
126 | else return std::list<InitAlternative>{}; |
---|
127 | } |
---|
128 | private: |
---|
129 | Type * type = nullptr; |
---|
130 | }; |
---|
131 | |
---|
132 | class ArrayIterator : public MemberIterator { |
---|
133 | public: |
---|
134 | ArrayIterator( ArrayType * at ) : array( at ) { |
---|
135 | PRINT( std::cerr << "Creating array iterator: " << at << std::endl; ) |
---|
136 | base = at->get_base(); |
---|
137 | memberIter = createMemberIterator( base ); |
---|
138 | setSize( at->get_dimension() ); |
---|
139 | } |
---|
140 | |
---|
141 | ~ArrayIterator() { |
---|
142 | delete memberIter; |
---|
143 | } |
---|
144 | |
---|
145 | private: |
---|
146 | void setSize( Expression * expr ) { |
---|
147 | if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { |
---|
148 | size = getConstValue( constExpr ); |
---|
149 | PRINT( std::cerr << "array type with size: " << size << std::endl; ) |
---|
150 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
151 | setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast |
---|
152 | } else { |
---|
153 | assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | public: |
---|
158 | void setPosition( Expression * expr ) { |
---|
159 | // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions |
---|
160 | if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { |
---|
161 | index = getConstValue( constExpr ); |
---|
162 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
163 | setPosition( castExpr->get_arg() ); |
---|
164 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
165 | assertf( dynamic_cast<EnumInstType *> ( varExpr->get_result() ), "ArrayIterator given variable that isn't an enum constant", toString( expr ).c_str() ); |
---|
166 | index = 0; // xxx - get actual value of enum constant |
---|
167 | } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) { |
---|
168 | index = 0; // xxx - get actual sizeof/alignof value? |
---|
169 | } else { |
---|
170 | assertf( false, "bad designator given to ArrayIterator: %s", toString( expr ).c_str() ); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | virtual void setPosition( std::list< Expression * > & designators ) { |
---|
175 | if ( ! designators.empty() ) { |
---|
176 | setPosition( designators.front() ); |
---|
177 | designators.pop_front(); |
---|
178 | memberIter->setPosition( designators ); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | virtual std::list<InitAlternative> operator*() const { |
---|
183 | return first(); |
---|
184 | } |
---|
185 | |
---|
186 | virtual operator bool() const { return index < size; } |
---|
187 | |
---|
188 | virtual MemberIterator & bigStep() { |
---|
189 | PRINT( std::cerr << "bigStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
190 | ++index; |
---|
191 | delete memberIter; |
---|
192 | if ( index < size ) memberIter = createMemberIterator( base ); |
---|
193 | else memberIter = nullptr; |
---|
194 | return *this; |
---|
195 | } |
---|
196 | |
---|
197 | virtual MemberIterator & smallStep() { |
---|
198 | PRINT( std::cerr << "smallStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
199 | if ( memberIter ) { |
---|
200 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; ) |
---|
201 | memberIter->smallStep(); |
---|
202 | if ( *memberIter ) { |
---|
203 | PRINT( std::cerr << "has valid member iter" << std::endl; ) |
---|
204 | return *this; |
---|
205 | } |
---|
206 | } |
---|
207 | return bigStep(); |
---|
208 | } |
---|
209 | |
---|
210 | virtual Type * getType() { return array; } |
---|
211 | virtual Type * getNext() { return base; } |
---|
212 | |
---|
213 | virtual std::list<InitAlternative> first() const { |
---|
214 | PRINT( std::cerr << "first in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
215 | if ( memberIter && *memberIter ) { |
---|
216 | std::list<InitAlternative> ret = memberIter->first(); |
---|
217 | for ( InitAlternative & alt : ret ) { |
---|
218 | alt.designation->get_designators().push_front( new ConstantExpr( Constant::from_ulong( index ) ) ); |
---|
219 | } |
---|
220 | return ret; |
---|
221 | } |
---|
222 | return std::list<InitAlternative>(); |
---|
223 | } |
---|
224 | |
---|
225 | virtual void print( std::ostream & out, Indenter indent ) const { |
---|
226 | out << "ArrayIterator(Array of " << base << ")"; |
---|
227 | if ( memberIter ) { |
---|
228 | Indenter childIndent = indent+1; |
---|
229 | out << std::endl << childIndent; |
---|
230 | memberIter->print( out, childIndent ); |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | private: |
---|
235 | ArrayType * array = nullptr; |
---|
236 | Type * base = nullptr; |
---|
237 | size_t index = 0; |
---|
238 | size_t size = 0; |
---|
239 | MemberIterator * memberIter = nullptr; |
---|
240 | }; |
---|
241 | |
---|
242 | class AggregateIterator : public MemberIterator { |
---|
243 | public: |
---|
244 | typedef std::list<Declaration *> MemberList; |
---|
245 | typedef MemberList::const_iterator iterator; |
---|
246 | std::string kind = ""; // for debug |
---|
247 | std::string name; |
---|
248 | Type * inst = nullptr; |
---|
249 | const MemberList & members; |
---|
250 | iterator curMember; |
---|
251 | bool atbegin = true; // false at first {small,big}Step -- this aggr type is only added to the possibilities at the beginning |
---|
252 | Type * curType = nullptr; |
---|
253 | MemberIterator * memberIter = nullptr; |
---|
254 | mutable TypeSubstitution sub; |
---|
255 | |
---|
256 | AggregateIterator( const std::string & kind, const std::string & name, Type * inst, const MemberList & members ) : kind( kind ), name( name ), inst( inst ), members( members ), curMember( members.begin() ), sub( makeGenericSubstitution( inst ) ) { |
---|
257 | init(); |
---|
258 | } |
---|
259 | |
---|
260 | virtual ~AggregateIterator() { |
---|
261 | delete memberIter; |
---|
262 | } |
---|
263 | |
---|
264 | bool init() { |
---|
265 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) |
---|
266 | if ( curMember != members.end() ) { |
---|
267 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( *curMember ) ) { |
---|
268 | PRINT( std::cerr << "incremented to field: " << field << std::endl; ) |
---|
269 | curType = field->get_type(); |
---|
270 | memberIter = createMemberIterator( curType ); |
---|
271 | return true; |
---|
272 | } |
---|
273 | } |
---|
274 | return false; |
---|
275 | } |
---|
276 | |
---|
277 | virtual std::list<InitAlternative> operator*() const { |
---|
278 | if (memberIter && *memberIter) { |
---|
279 | std::list<InitAlternative> ret = memberIter->first(); |
---|
280 | PRINT( std::cerr << "sub: " << sub << std::endl; ) |
---|
281 | for ( InitAlternative & alt : ret ) { |
---|
282 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
283 | alt.designation->get_designators().push_front( new VariableExpr( safe_dynamic_cast< ObjectDecl * >( *curMember ) ) ); |
---|
284 | // need to substitute for generic types, so that casts are to concrete types |
---|
285 | PRINT( std::cerr << " type is: " << alt.type; ) |
---|
286 | sub.apply( alt.type ); // also apply to designation?? |
---|
287 | PRINT( std::cerr << " ==> " << alt.type << std::endl; ) |
---|
288 | } |
---|
289 | return ret; |
---|
290 | } |
---|
291 | return std::list<InitAlternative>(); |
---|
292 | } |
---|
293 | |
---|
294 | virtual void setPosition( std::list< Expression * > & designators ) { |
---|
295 | if ( ! designators.empty() ) { |
---|
296 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( designators.front() ) ) { |
---|
297 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { |
---|
298 | if ( *curMember == varExpr->get_var() ) { |
---|
299 | designators.pop_front(); |
---|
300 | delete memberIter; |
---|
301 | memberIter = createMemberIterator( varExpr->get_result() ); |
---|
302 | curType = varExpr->get_result(); |
---|
303 | atbegin = curMember == members.begin() && designators.empty(); // xxx - is this the right condition for atbegin?? |
---|
304 | memberIter->setPosition( designators ); |
---|
305 | return; |
---|
306 | } // if |
---|
307 | } // for |
---|
308 | assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); |
---|
309 | } else { |
---|
310 | assertf( false, "bad designator given to %s: %s", kind.c_str(), toString( designators.front() ).c_str() ); |
---|
311 | } // if |
---|
312 | } // if |
---|
313 | } |
---|
314 | |
---|
315 | virtual MemberIterator & smallStep() { |
---|
316 | PRINT( std::cerr << "smallStep in " << kind << std::endl; ) |
---|
317 | atbegin = false; |
---|
318 | if ( memberIter ) { |
---|
319 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; ) |
---|
320 | memberIter->smallStep(); |
---|
321 | if ( *memberIter ) { |
---|
322 | PRINT( std::cerr << "success!" << std::endl; ) |
---|
323 | return *this; |
---|
324 | } |
---|
325 | } |
---|
326 | return bigStep(); |
---|
327 | } |
---|
328 | |
---|
329 | virtual Type * getType() { return inst; } |
---|
330 | virtual Type * getNext() { |
---|
331 | if ( memberIter && *memberIter ) return memberIter->getType(); // xxx - ??? recursive call??? |
---|
332 | return nullptr; |
---|
333 | } |
---|
334 | |
---|
335 | virtual std::list<InitAlternative> first() const { |
---|
336 | std::list<InitAlternative> ret; |
---|
337 | PRINT( std::cerr << "first " << kind << std::endl; ) |
---|
338 | if ( memberIter && *memberIter ) { // might not need *memberIter?? |
---|
339 | PRINT( std::cerr << "adding children" << std::endl; ) |
---|
340 | ret = memberIter->first(); |
---|
341 | for ( InitAlternative & alt : ret ) { |
---|
342 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
343 | alt.designation->get_designators().push_front( new VariableExpr( safe_dynamic_cast< ObjectDecl * >( *curMember ) ) ); |
---|
344 | } |
---|
345 | } |
---|
346 | // if ( curMember == std::next( decl->get_members().begin(), 1 ) ) { // xxx - this never triggers because curMember is incremented immediately on construction |
---|
347 | if ( atbegin ) { // xxx - this never triggers because curMember is incremented immediately on construction |
---|
348 | // xxx - what about case of empty struct?? |
---|
349 | // only add self if at the very beginning of the structure |
---|
350 | PRINT( std::cerr << "adding self" << std::endl; ) |
---|
351 | ret.push_front( { inst->clone(), new Designation( {} ) } ); |
---|
352 | } |
---|
353 | return ret; |
---|
354 | } |
---|
355 | |
---|
356 | virtual void print( std::ostream & out, Indenter indent ) const { |
---|
357 | out << kind << "(" << name << ")"; |
---|
358 | if ( memberIter ) { |
---|
359 | Indenter childIndent = indent+1; |
---|
360 | out << std::endl << childIndent; |
---|
361 | memberIter->print( out, childIndent ); |
---|
362 | } |
---|
363 | } |
---|
364 | }; |
---|
365 | |
---|
366 | class UnionIterator : public AggregateIterator { |
---|
367 | public: |
---|
368 | UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst->get_name(), inst, inst->get_baseUnion()->get_members() ) {} |
---|
369 | |
---|
370 | virtual operator bool() const { return (memberIter && *memberIter); } |
---|
371 | virtual MemberIterator & bigStep() { |
---|
372 | // unions only initialize one member |
---|
373 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
374 | atbegin = false; |
---|
375 | delete memberIter; |
---|
376 | memberIter = nullptr; |
---|
377 | curType = nullptr; |
---|
378 | curMember = members.end(); |
---|
379 | return *this; |
---|
380 | } |
---|
381 | virtual std::list<InitAlternative> first() const { return std::list<InitAlternative>{}; } |
---|
382 | }; |
---|
383 | |
---|
384 | class StructIterator : public AggregateIterator { |
---|
385 | public: |
---|
386 | StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst->get_name(), inst, inst->get_baseStruct()->get_members() ) {} |
---|
387 | |
---|
388 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } |
---|
389 | |
---|
390 | virtual MemberIterator & bigStep() { |
---|
391 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
392 | atbegin = false; |
---|
393 | delete memberIter; |
---|
394 | memberIter = nullptr; |
---|
395 | curType = nullptr; |
---|
396 | for ( ; curMember != members.end(); ) { |
---|
397 | ++curMember; |
---|
398 | if ( init() ) { |
---|
399 | return *this; |
---|
400 | } |
---|
401 | } |
---|
402 | return *this; |
---|
403 | } |
---|
404 | }; |
---|
405 | |
---|
406 | class TupleIterator : public AggregateIterator { |
---|
407 | public: |
---|
408 | TupleIterator( TupleType * inst ) : AggregateIterator( "TupleIterator", toString("Tuple", inst->size()), inst, inst->get_members() ) {} |
---|
409 | |
---|
410 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } |
---|
411 | |
---|
412 | virtual MemberIterator & bigStep() { |
---|
413 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
414 | atbegin = false; |
---|
415 | delete memberIter; |
---|
416 | memberIter = nullptr; |
---|
417 | curType = nullptr; |
---|
418 | for ( ; curMember != members.end(); ) { |
---|
419 | ++curMember; |
---|
420 | if ( init() ) { |
---|
421 | return *this; |
---|
422 | } |
---|
423 | } |
---|
424 | return *this; |
---|
425 | } |
---|
426 | }; |
---|
427 | |
---|
428 | MemberIterator * createMemberIterator( Type * type ) { |
---|
429 | if ( ReferenceToType * aggr = dynamic_cast< ReferenceToType * >( type ) ) { |
---|
430 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( aggr ) ) { |
---|
431 | return new StructIterator( sit ); |
---|
432 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( aggr ) ) { |
---|
433 | return new UnionIterator( uit ); |
---|
434 | } else { |
---|
435 | assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" ); |
---|
436 | return new SimpleIterator( type ); |
---|
437 | } |
---|
438 | } else if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
439 | return new ArrayIterator( at ); |
---|
440 | } else if ( TupleType * tt = dynamic_cast< TupleType * >( type ) ) { |
---|
441 | return new TupleIterator( tt ); |
---|
442 | } else { |
---|
443 | return new SimpleIterator( type ); |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | CurrentObject::CurrentObject() {} |
---|
448 | CurrentObject::CurrentObject( Type * type ) { |
---|
449 | objStack.push( new SimpleIterator( type ) ); |
---|
450 | } |
---|
451 | |
---|
452 | |
---|
453 | void CurrentObject::setNext( Designation * designation ) { |
---|
454 | assertf( ! objStack.empty(), "obj stack empty in setNext" ); |
---|
455 | PRINT( std::cerr << "____setNext" << designation << std::endl; ) |
---|
456 | objStack.top()->setPosition( designation->get_designators() ); |
---|
457 | } |
---|
458 | |
---|
459 | Designation * CurrentObject::findNext( Designation * designation ) { |
---|
460 | typedef std::list< Expression * > DesignatorChain; |
---|
461 | PRINT( std::cerr << "___findNext" << std::endl; ) |
---|
462 | // find all the d's |
---|
463 | std::list<DesignatorChain> desigAlts{ { } }, newDesigAlts; |
---|
464 | std::list<Type *> curTypes { (objStack.top())->getType() }, newTypes; |
---|
465 | for ( Expression * expr : designation->get_designators() ) { |
---|
466 | PRINT( std::cerr << "____untyped: " << expr << std::endl; ) |
---|
467 | std::list<DesignatorChain>::iterator dit = desigAlts.begin(); |
---|
468 | if ( NameExpr * nexpr = dynamic_cast<NameExpr *>(expr) ) { |
---|
469 | for ( Type * t : curTypes ) { |
---|
470 | assert( dit != desigAlts.end() ); |
---|
471 | DesignatorChain & d = *dit; |
---|
472 | PRINT( std::cerr << "____actual: " << t << std::endl; ) |
---|
473 | ReferenceToType * refType = dynamic_cast<ReferenceToType *>(t); |
---|
474 | std::list<Declaration *> members; |
---|
475 | if ( refType ) { |
---|
476 | refType->lookup( nexpr->get_name(), members ); // concatenate identical field name |
---|
477 | // xxx - need to also include anonymous members in this somehow... |
---|
478 | for ( Declaration * mem: members ) { |
---|
479 | if ( ObjectDecl * field = dynamic_cast<ObjectDecl *>(mem) ) { |
---|
480 | PRINT( std::cerr << "____alt: " << field->get_type() << std::endl; ) |
---|
481 | DesignatorChain newD = d; |
---|
482 | newD.push_back( new VariableExpr( field ) ); |
---|
483 | newDesigAlts.push_back( newD ); |
---|
484 | newTypes.push_back( field->get_type() ); |
---|
485 | } // if |
---|
486 | } // for |
---|
487 | } // if |
---|
488 | ++dit; |
---|
489 | } // for |
---|
490 | } else { |
---|
491 | for ( Type * t : curTypes ) { |
---|
492 | assert( dit != desigAlts.end() ); |
---|
493 | DesignatorChain & d = *dit; |
---|
494 | if ( ArrayType * at = dynamic_cast< ArrayType * > ( t ) ) { |
---|
495 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; ) |
---|
496 | d.push_back( expr ); |
---|
497 | newDesigAlts.push_back( d ); |
---|
498 | newTypes.push_back( at->get_base() ); |
---|
499 | } |
---|
500 | ++dit; |
---|
501 | } // for |
---|
502 | } // if |
---|
503 | desigAlts = newDesigAlts; |
---|
504 | newDesigAlts.clear(); |
---|
505 | curTypes = newTypes; |
---|
506 | newTypes.clear(); |
---|
507 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%d) and current types (%d) out of sync", desigAlts.size(), curTypes.size() ); |
---|
508 | } // for |
---|
509 | if ( desigAlts.size() > 1 ) { |
---|
510 | throw SemanticError( toString("Too many alternatives (", desigAlts.size(), ") for designation: "), designation ); |
---|
511 | } else if ( desigAlts.size() == 0 ) { |
---|
512 | throw SemanticError( "No reasonable alternatives for designation: ", designation ); |
---|
513 | } |
---|
514 | DesignatorChain & d = desigAlts.back(); |
---|
515 | PRINT( for ( Expression * expr : d ) { |
---|
516 | std::cerr << "____desig: " << expr << std::endl; |
---|
517 | } ) // for |
---|
518 | assertf( ! curTypes.empty(), "empty designator chosen"); |
---|
519 | |
---|
520 | // set new designators |
---|
521 | assertf( ! objStack.empty(), "empty object stack when setting designation" ); |
---|
522 | Designation * actualDesignation = new Designation( d ); |
---|
523 | objStack.top()->setPosition( d ); // destroys d |
---|
524 | return actualDesignation; |
---|
525 | } |
---|
526 | |
---|
527 | void CurrentObject::increment() { |
---|
528 | PRINT( std::cerr << "____increment" << std::endl; ) |
---|
529 | if ( ! objStack.empty() ) { |
---|
530 | PRINT( std::cerr << *objStack.top() << std::endl; ) |
---|
531 | objStack.top()->smallStep(); |
---|
532 | } |
---|
533 | } |
---|
534 | |
---|
535 | void CurrentObject::enterListInit() { |
---|
536 | PRINT( std::cerr << "____entering list init" << std::endl; ) |
---|
537 | assertf( ! objStack.empty(), "empty obj stack entering list init" ); |
---|
538 | Type * type = objStack.top()->getNext(); |
---|
539 | if ( type ) { |
---|
540 | objStack.push( createMemberIterator( type ) ); |
---|
541 | } else { |
---|
542 | assertf( false, "not sure about this case..." ); |
---|
543 | } |
---|
544 | } |
---|
545 | |
---|
546 | void CurrentObject::exitListInit() { |
---|
547 | PRINT( std::cerr << "____exiting list init" << std::endl; ) |
---|
548 | assertf( ! objStack.empty(), "objstack empty" ); |
---|
549 | delete objStack.top(); |
---|
550 | objStack.pop(); |
---|
551 | if ( ! objStack.empty() ) { |
---|
552 | PRINT( std::cerr << *objStack.top() << std::endl; ) |
---|
553 | objStack.top()->bigStep(); |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | std::list< InitAlternative > CurrentObject::getOptions() { |
---|
558 | PRINT( std::cerr << "____getting current options" << std::endl; ) |
---|
559 | assertf( ! objStack.empty(), "objstack empty in getOptions" ); |
---|
560 | return **objStack.top(); |
---|
561 | } |
---|
562 | |
---|
563 | Type * CurrentObject::getCurrentType() { |
---|
564 | PRINT( std::cerr << "____getting current type" << std::endl; ) |
---|
565 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); |
---|
566 | return objStack.top()->getNext(); |
---|
567 | } |
---|
568 | } // namespace ResolvExpr |
---|
569 | |
---|
570 | // Local Variables: // |
---|
571 | // tab-width: 4 // |
---|
572 | // mode: c++ // |
---|
573 | // compile-command: "make install" // |
---|
574 | // End: // |
---|