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 <stddef.h> // for size_t |
---|
17 | #include <cassert> // for assertf, assert, safe_dynamic_... |
---|
18 | #include <deque> |
---|
19 | #include <iostream> // for ostream, operator<<, basic_ost... |
---|
20 | #include <stack> // for stack |
---|
21 | #include <string> // for string, operator<<, allocator |
---|
22 | |
---|
23 | #include "AST/Expr.hpp" // for InitAlternative |
---|
24 | #include "AST/GenericSubstitution.hpp" // for genericSubstitution |
---|
25 | #include "AST/Init.hpp" // for Designation |
---|
26 | #include "AST/Node.hpp" // for readonly |
---|
27 | #include "AST/Type.hpp" |
---|
28 | #include "Common/Indenter.h" // for Indenter, operator<< |
---|
29 | #include "Common/SemanticError.h" // for SemanticError |
---|
30 | #include "Common/utility.h" // for toString |
---|
31 | #include "CurrentObject.h" |
---|
32 | #include "SynTree/Constant.h" // for Constant |
---|
33 | #include "SynTree/Declaration.h" // for ObjectDecl, Declaration, Struc... |
---|
34 | #include "SynTree/Expression.h" // for InitAlternative, VariableExpr |
---|
35 | #include "SynTree/Initializer.h" // for Designation, operator<< |
---|
36 | #include "SynTree/Type.h" // for Type, StructInstType, UnionIns... |
---|
37 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
38 | |
---|
39 | #if 0 |
---|
40 | #define PRINT(x) x |
---|
41 | #else |
---|
42 | #define PRINT(x) |
---|
43 | #endif |
---|
44 | |
---|
45 | namespace ResolvExpr { |
---|
46 | template< typename AggrInst > |
---|
47 | TypeSubstitution makeGenericSubstitution( AggrInst * inst ) { |
---|
48 | assert( inst ); |
---|
49 | assert( inst->get_baseParameters() ); |
---|
50 | std::list< TypeDecl * > baseParams = *inst->get_baseParameters(); |
---|
51 | std::list< Expression * > typeSubs = inst->get_parameters(); |
---|
52 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); |
---|
53 | return subs; |
---|
54 | } |
---|
55 | |
---|
56 | TypeSubstitution makeGenericSubstitution( Type * type ) { |
---|
57 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { |
---|
58 | return makeGenericSubstitution( inst ); |
---|
59 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { |
---|
60 | return makeGenericSubstitution( inst ); |
---|
61 | } else { |
---|
62 | return TypeSubstitution(); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | class MemberIterator { |
---|
67 | public: |
---|
68 | virtual ~MemberIterator() {} |
---|
69 | |
---|
70 | /// walks the current object using the given designators as a guide |
---|
71 | virtual void setPosition( std::list< Expression * > & designators ) = 0; |
---|
72 | |
---|
73 | /// retrieve the list of possible Type/Designaton pairs for the current position in the currect object |
---|
74 | virtual std::list<InitAlternative> operator*() const = 0; |
---|
75 | |
---|
76 | /// true if the iterator is not currently at the end |
---|
77 | virtual operator bool() const = 0; |
---|
78 | |
---|
79 | /// moves the iterator by one member in the current object |
---|
80 | virtual MemberIterator & bigStep() = 0; |
---|
81 | |
---|
82 | /// moves the iterator by one member in the current subobject |
---|
83 | virtual MemberIterator & smallStep() = 0; |
---|
84 | |
---|
85 | /// the type of the current object |
---|
86 | virtual Type * getType() = 0; |
---|
87 | |
---|
88 | /// the type of the current subobject |
---|
89 | virtual Type * getNext() = 0; |
---|
90 | |
---|
91 | /// printing for debug |
---|
92 | virtual void print( std::ostream & out, Indenter indent ) const = 0; |
---|
93 | |
---|
94 | /// helper for operator*; aggregates must add designator to each init alternative, but |
---|
95 | /// adding designators in operator* creates duplicates. |
---|
96 | virtual std::list<InitAlternative> first() const = 0; // should be protected |
---|
97 | }; |
---|
98 | |
---|
99 | std::ostream & operator<<(std::ostream & out, const MemberIterator & it) { |
---|
100 | Indenter indenter; |
---|
101 | it.print( out, indenter ); |
---|
102 | return out; |
---|
103 | } |
---|
104 | |
---|
105 | /// create a new MemberIterator that traverses a type correctly |
---|
106 | MemberIterator * createMemberIterator( Type * type ); |
---|
107 | |
---|
108 | /// iterates "other" types, e.g. basic types, pointer types, etc. which do not change at list initializer entry |
---|
109 | class SimpleIterator : public MemberIterator { |
---|
110 | public: |
---|
111 | SimpleIterator( Type * type ) : type( type ) {} |
---|
112 | |
---|
113 | virtual void setPosition( std::list< Expression * > & designators ) { |
---|
114 | assertf( designators.empty(), "simple iterator given non-empty designator..." ); // xxx - might be semantic error |
---|
115 | } |
---|
116 | |
---|
117 | virtual std::list<InitAlternative> operator*() const { return first(); } |
---|
118 | virtual operator bool() const { return type; } |
---|
119 | |
---|
120 | // big step is the same as small step |
---|
121 | virtual MemberIterator & bigStep() { return smallStep(); } |
---|
122 | virtual MemberIterator & smallStep() { |
---|
123 | type = nullptr; // type is nullified on increment since SimpleIterators do not have members |
---|
124 | return *this; |
---|
125 | } |
---|
126 | |
---|
127 | virtual void print( std::ostream & out, __attribute__((unused)) Indenter indent ) const { |
---|
128 | out << "SimpleIterator(" << type << ")"; |
---|
129 | } |
---|
130 | |
---|
131 | virtual Type * getType() { return type; } |
---|
132 | virtual Type * getNext() { return type; } |
---|
133 | |
---|
134 | protected: |
---|
135 | virtual std::list<InitAlternative> first() const { |
---|
136 | if ( type ) return std::list<InitAlternative>{ { type->clone(), new Designation( {} ) } }; |
---|
137 | else return std::list<InitAlternative>{}; |
---|
138 | } |
---|
139 | private: |
---|
140 | Type * type = nullptr; |
---|
141 | }; |
---|
142 | |
---|
143 | class ArrayIterator : public MemberIterator { |
---|
144 | public: |
---|
145 | ArrayIterator( ArrayType * at ) : array( at ) { |
---|
146 | PRINT( std::cerr << "Creating array iterator: " << at << std::endl; ) |
---|
147 | base = at->base; |
---|
148 | memberIter = createMemberIterator( base ); |
---|
149 | if ( at->isVarLen ) SemanticError( at, "VLA initialization does not support @=: " ); |
---|
150 | setSize( at->dimension ); |
---|
151 | } |
---|
152 | |
---|
153 | ~ArrayIterator() { |
---|
154 | delete memberIter; |
---|
155 | } |
---|
156 | |
---|
157 | private: |
---|
158 | void setSize( Expression * expr ) { // replace this logic with an eval call |
---|
159 | auto res = eval(expr); |
---|
160 | if (res.second) { |
---|
161 | size = res.first; |
---|
162 | } else { |
---|
163 | SemanticError( expr->location, toString("Array designator must be a constant expression: ", expr) ); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | public: |
---|
168 | void setPosition( Expression * expr ) { |
---|
169 | // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions |
---|
170 | if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { |
---|
171 | try { |
---|
172 | index = constExpr->intValue(); |
---|
173 | } catch( SemanticErrorException & ) { |
---|
174 | SemanticError( expr, "Constant expression of non-integral type in array designator: " ); |
---|
175 | } |
---|
176 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
177 | setPosition( castExpr->get_arg() ); |
---|
178 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
179 | EnumInstType * inst = dynamic_cast<EnumInstType *>( varExpr->get_result() ); |
---|
180 | assertf( inst, "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() ); |
---|
181 | long long int value; |
---|
182 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { |
---|
183 | index = value; |
---|
184 | } |
---|
185 | } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) { |
---|
186 | index = 0; // xxx - get actual sizeof/alignof value? |
---|
187 | } else { |
---|
188 | assertf( false, "bad designator given to ArrayIterator: %s", toString( expr ).c_str() ); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | virtual void setPosition( std::list< Expression * > & designators ) { |
---|
193 | if ( ! designators.empty() ) { |
---|
194 | setPosition( designators.front() ); |
---|
195 | designators.pop_front(); |
---|
196 | memberIter->setPosition( designators ); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | virtual std::list<InitAlternative> operator*() const { |
---|
201 | return first(); |
---|
202 | } |
---|
203 | |
---|
204 | virtual operator bool() const { return index < size; } |
---|
205 | |
---|
206 | virtual MemberIterator & bigStep() { |
---|
207 | PRINT( std::cerr << "bigStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
208 | ++index; |
---|
209 | delete memberIter; |
---|
210 | if ( index < size ) memberIter = createMemberIterator( base ); |
---|
211 | else memberIter = nullptr; |
---|
212 | return *this; |
---|
213 | } |
---|
214 | |
---|
215 | virtual MemberIterator & smallStep() { |
---|
216 | PRINT( std::cerr << "smallStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
217 | if ( memberIter ) { |
---|
218 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; ) |
---|
219 | memberIter->smallStep(); |
---|
220 | if ( *memberIter ) { |
---|
221 | PRINT( std::cerr << "has valid member iter" << std::endl; ) |
---|
222 | return *this; |
---|
223 | } |
---|
224 | } |
---|
225 | return bigStep(); |
---|
226 | } |
---|
227 | |
---|
228 | virtual Type * getType() { return array; } |
---|
229 | virtual Type * getNext() { return base; } |
---|
230 | |
---|
231 | virtual std::list<InitAlternative> first() const { |
---|
232 | PRINT( std::cerr << "first in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
233 | if ( memberIter && *memberIter ) { |
---|
234 | std::list<InitAlternative> ret = memberIter->first(); |
---|
235 | for ( InitAlternative & alt : ret ) { |
---|
236 | alt.designation->get_designators().push_front( new ConstantExpr( Constant::from_ulong( index ) ) ); |
---|
237 | } |
---|
238 | return ret; |
---|
239 | } |
---|
240 | return std::list<InitAlternative>(); |
---|
241 | } |
---|
242 | |
---|
243 | virtual void print( std::ostream & out, Indenter indent ) const { |
---|
244 | out << "ArrayIterator(Array of " << base << ")"; |
---|
245 | if ( memberIter ) { |
---|
246 | Indenter childIndent = indent+1; |
---|
247 | out << std::endl << childIndent; |
---|
248 | memberIter->print( out, childIndent ); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | private: |
---|
253 | ArrayType * array = nullptr; |
---|
254 | Type * base = nullptr; |
---|
255 | size_t index = 0; |
---|
256 | size_t size = 0; |
---|
257 | MemberIterator * memberIter = nullptr; |
---|
258 | }; |
---|
259 | |
---|
260 | class AggregateIterator : public MemberIterator { |
---|
261 | public: |
---|
262 | typedef std::list<Declaration *> MemberList; |
---|
263 | typedef MemberList::const_iterator iterator; |
---|
264 | std::string kind = ""; // for debug |
---|
265 | std::string name; |
---|
266 | Type * inst = nullptr; |
---|
267 | const MemberList & members; |
---|
268 | iterator curMember; |
---|
269 | bool atbegin = true; // false at first {small,big}Step -- this aggr type is only added to the possibilities at the beginning |
---|
270 | Type * curType = nullptr; |
---|
271 | MemberIterator * memberIter = nullptr; |
---|
272 | mutable TypeSubstitution sub; |
---|
273 | |
---|
274 | 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 ) ) { |
---|
275 | PRINT( std::cerr << "Creating " << kind << "(" << name << ")"; ) |
---|
276 | init(); |
---|
277 | } |
---|
278 | |
---|
279 | virtual ~AggregateIterator() { |
---|
280 | delete memberIter; |
---|
281 | } |
---|
282 | |
---|
283 | bool init() { |
---|
284 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) |
---|
285 | if ( curMember != members.end() ) { |
---|
286 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( *curMember ) ) { |
---|
287 | PRINT( std::cerr << "incremented to field: " << field << std::endl; ) |
---|
288 | curType = field->get_type(); |
---|
289 | memberIter = createMemberIterator( curType ); |
---|
290 | return true; |
---|
291 | } |
---|
292 | } |
---|
293 | return false; |
---|
294 | } |
---|
295 | |
---|
296 | virtual std::list<InitAlternative> operator*() const { |
---|
297 | if (memberIter && *memberIter) { |
---|
298 | std::list<InitAlternative> ret = memberIter->first(); |
---|
299 | PRINT( std::cerr << "sub: " << sub << std::endl; ) |
---|
300 | for ( InitAlternative & alt : ret ) { |
---|
301 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
302 | alt.designation->get_designators().push_front( new VariableExpr( strict_dynamic_cast< ObjectDecl * >( *curMember ) ) ); |
---|
303 | // need to substitute for generic types, so that casts are to concrete types |
---|
304 | PRINT( std::cerr << " type is: " << alt.type; ) |
---|
305 | sub.apply( alt.type ); // also apply to designation?? |
---|
306 | PRINT( std::cerr << " ==> " << alt.type << std::endl; ) |
---|
307 | } |
---|
308 | return ret; |
---|
309 | } |
---|
310 | return std::list<InitAlternative>(); |
---|
311 | } |
---|
312 | |
---|
313 | virtual void setPosition( std::list< Expression * > & designators ) { |
---|
314 | if ( ! designators.empty() ) { |
---|
315 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( designators.front() ) ) { |
---|
316 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { |
---|
317 | if ( *curMember == varExpr->get_var() ) { |
---|
318 | designators.pop_front(); |
---|
319 | delete memberIter; |
---|
320 | memberIter = createMemberIterator( varExpr->get_result() ); |
---|
321 | curType = varExpr->get_result(); |
---|
322 | atbegin = curMember == members.begin() && designators.empty(); // xxx - is this the right condition for atbegin?? |
---|
323 | memberIter->setPosition( designators ); |
---|
324 | return; |
---|
325 | } // if |
---|
326 | } // for |
---|
327 | assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); |
---|
328 | } else { |
---|
329 | assertf( false, "bad designator given to %s: %s", kind.c_str(), toString( designators.front() ).c_str() ); |
---|
330 | } // if |
---|
331 | } // if |
---|
332 | } |
---|
333 | |
---|
334 | virtual MemberIterator & smallStep() { |
---|
335 | PRINT( std::cerr << "smallStep in " << kind << std::endl; ) |
---|
336 | atbegin = false; |
---|
337 | if ( memberIter ) { |
---|
338 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; ) |
---|
339 | memberIter->smallStep(); |
---|
340 | if ( *memberIter ) { |
---|
341 | PRINT( std::cerr << "success!" << std::endl; ) |
---|
342 | return *this; |
---|
343 | } |
---|
344 | } |
---|
345 | return bigStep(); |
---|
346 | } |
---|
347 | |
---|
348 | virtual Type * getType() { return inst; } |
---|
349 | virtual Type * getNext() { |
---|
350 | if ( memberIter && *memberIter ) return memberIter->getType(); // xxx - ??? recursive call??? |
---|
351 | return nullptr; |
---|
352 | } |
---|
353 | |
---|
354 | virtual std::list<InitAlternative> first() const { |
---|
355 | std::list<InitAlternative> ret; |
---|
356 | PRINT( std::cerr << "first " << kind << std::endl; ) |
---|
357 | if ( memberIter && *memberIter ) { // might not need *memberIter?? |
---|
358 | PRINT( std::cerr << "adding children" << std::endl; ) |
---|
359 | ret = memberIter->first(); |
---|
360 | for ( InitAlternative & alt : ret ) { |
---|
361 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
362 | alt.designation->get_designators().push_front( new VariableExpr( strict_dynamic_cast< ObjectDecl * >( *curMember ) ) ); |
---|
363 | } |
---|
364 | } |
---|
365 | if ( atbegin ) { |
---|
366 | // xxx - what about case of empty struct?? |
---|
367 | // only add self if at the very beginning of the structure |
---|
368 | PRINT( std::cerr << "adding self" << std::endl; ) |
---|
369 | ret.push_front( { inst->clone(), new Designation( {} ) } ); |
---|
370 | } |
---|
371 | return ret; |
---|
372 | } |
---|
373 | |
---|
374 | virtual void print( std::ostream & out, Indenter indent ) const { |
---|
375 | out << kind << "(" << name << ")"; |
---|
376 | if ( memberIter ) { |
---|
377 | Indenter childIndent = indent+1; |
---|
378 | out << std::endl << childIndent; |
---|
379 | memberIter->print( out, childIndent ); |
---|
380 | } |
---|
381 | } |
---|
382 | }; |
---|
383 | |
---|
384 | class UnionIterator : public AggregateIterator { |
---|
385 | public: |
---|
386 | UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst->get_name(), inst, inst->get_baseUnion()->get_members() ) {} |
---|
387 | |
---|
388 | virtual operator bool() const { return (memberIter && *memberIter); } |
---|
389 | virtual MemberIterator & bigStep() { |
---|
390 | // unions only initialize one member |
---|
391 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
392 | atbegin = false; |
---|
393 | delete memberIter; |
---|
394 | memberIter = nullptr; |
---|
395 | curType = nullptr; |
---|
396 | curMember = members.end(); |
---|
397 | return *this; |
---|
398 | } |
---|
399 | }; |
---|
400 | |
---|
401 | class StructIterator : public AggregateIterator { |
---|
402 | public: |
---|
403 | StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst->get_name(), inst, inst->get_baseStruct()->get_members() ) {} |
---|
404 | |
---|
405 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } |
---|
406 | |
---|
407 | virtual MemberIterator & bigStep() { |
---|
408 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
409 | atbegin = false; |
---|
410 | delete memberIter; |
---|
411 | memberIter = nullptr; |
---|
412 | curType = nullptr; |
---|
413 | for ( ; curMember != members.end(); ) { |
---|
414 | ++curMember; |
---|
415 | if ( init() ) { |
---|
416 | return *this; |
---|
417 | } |
---|
418 | } |
---|
419 | return *this; |
---|
420 | } |
---|
421 | }; |
---|
422 | |
---|
423 | class TupleIterator : public AggregateIterator { |
---|
424 | public: |
---|
425 | TupleIterator( TupleType * inst ) : AggregateIterator( "TupleIterator", toString("Tuple", inst->size()), inst, inst->get_members() ) {} |
---|
426 | |
---|
427 | virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } |
---|
428 | |
---|
429 | virtual MemberIterator & bigStep() { |
---|
430 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
431 | atbegin = false; |
---|
432 | delete memberIter; |
---|
433 | memberIter = nullptr; |
---|
434 | curType = nullptr; |
---|
435 | for ( ; curMember != members.end(); ) { |
---|
436 | ++curMember; |
---|
437 | if ( init() ) { |
---|
438 | return *this; |
---|
439 | } |
---|
440 | } |
---|
441 | return *this; |
---|
442 | } |
---|
443 | }; |
---|
444 | |
---|
445 | MemberIterator * createMemberIterator( Type * type ) { |
---|
446 | if ( ReferenceToType * aggr = dynamic_cast< ReferenceToType * >( type ) ) { |
---|
447 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( aggr ) ) { |
---|
448 | return new StructIterator( sit ); |
---|
449 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( aggr ) ) { |
---|
450 | return new UnionIterator( uit ); |
---|
451 | } else { |
---|
452 | assertf( dynamic_cast< EnumInstType * >( type ) || dynamic_cast< TypeInstType * >( type ), "Encountered unhandled ReferenceToType in createMemberIterator: %s", toString( type ).c_str() ); |
---|
453 | return new SimpleIterator( type ); |
---|
454 | } |
---|
455 | } else if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
456 | return new ArrayIterator( at ); |
---|
457 | } else if ( TupleType * tt = dynamic_cast< TupleType * >( type ) ) { |
---|
458 | return new TupleIterator( tt ); |
---|
459 | } else { |
---|
460 | return new SimpleIterator( type ); |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | CurrentObject::CurrentObject() {} |
---|
465 | CurrentObject::CurrentObject( Type * type ) { |
---|
466 | objStack.push( new SimpleIterator( type ) ); |
---|
467 | } |
---|
468 | |
---|
469 | |
---|
470 | void CurrentObject::setNext( Designation * designation ) { |
---|
471 | assertf( ! objStack.empty(), "obj stack empty in setNext" ); |
---|
472 | PRINT( std::cerr << "____setNext" << designation << std::endl; ) |
---|
473 | objStack.top()->setPosition( designation->get_designators() ); |
---|
474 | } |
---|
475 | |
---|
476 | Designation * CurrentObject::findNext( Designation * designation ) { |
---|
477 | typedef std::list< Expression * > DesignatorChain; |
---|
478 | PRINT( std::cerr << "___findNext" << std::endl; ) |
---|
479 | // find all the d's |
---|
480 | std::list<DesignatorChain> desigAlts{ { } }, newDesigAlts; |
---|
481 | std::list<Type *> curTypes { (objStack.top())->getType() }, newTypes; |
---|
482 | for ( Expression * expr : designation->get_designators() ) { |
---|
483 | PRINT( std::cerr << "____untyped: " << expr << std::endl; ) |
---|
484 | std::list<DesignatorChain>::iterator dit = desigAlts.begin(); |
---|
485 | if ( NameExpr * nexpr = dynamic_cast<NameExpr *>(expr) ) { |
---|
486 | for ( Type * t : curTypes ) { |
---|
487 | assert( dit != desigAlts.end() ); |
---|
488 | DesignatorChain & d = *dit; |
---|
489 | PRINT( std::cerr << "____actual: " << t << std::endl; ) |
---|
490 | ReferenceToType * refType = dynamic_cast<ReferenceToType *>(t); |
---|
491 | std::list<Declaration *> members; |
---|
492 | if ( refType ) { |
---|
493 | refType->lookup( nexpr->get_name(), members ); // concatenate identical field name |
---|
494 | // xxx - need to also include anonymous members in this somehow... |
---|
495 | for ( Declaration * mem: members ) { |
---|
496 | if ( ObjectDecl * field = dynamic_cast<ObjectDecl *>(mem) ) { |
---|
497 | PRINT( std::cerr << "____alt: " << field->get_type() << std::endl; ) |
---|
498 | DesignatorChain newD = d; |
---|
499 | newD.push_back( new VariableExpr( field ) ); |
---|
500 | newDesigAlts.push_back( newD ); |
---|
501 | newTypes.push_back( field->get_type() ); |
---|
502 | } // if |
---|
503 | } // for |
---|
504 | } // if |
---|
505 | ++dit; |
---|
506 | } // for |
---|
507 | } else { |
---|
508 | for ( Type * t : curTypes ) { |
---|
509 | assert( dit != desigAlts.end() ); |
---|
510 | DesignatorChain & d = *dit; |
---|
511 | if ( ArrayType * at = dynamic_cast< ArrayType * > ( t ) ) { |
---|
512 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; ) |
---|
513 | d.push_back( expr ); |
---|
514 | newDesigAlts.push_back( d ); |
---|
515 | newTypes.push_back( at->get_base() ); |
---|
516 | } |
---|
517 | ++dit; |
---|
518 | } // for |
---|
519 | } // if |
---|
520 | desigAlts = newDesigAlts; |
---|
521 | newDesigAlts.clear(); |
---|
522 | curTypes = newTypes; |
---|
523 | newTypes.clear(); |
---|
524 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%zu) and current types (%zu) out of sync", desigAlts.size(), curTypes.size() ); |
---|
525 | } // for |
---|
526 | if ( desigAlts.size() > 1 ) { |
---|
527 | SemanticError( designation, toString("Too many alternatives (", desigAlts.size(), ") for designation: ") ); |
---|
528 | } else if ( desigAlts.size() == 0 ) { |
---|
529 | SemanticError( designation, "No reasonable alternatives for designation: " ); |
---|
530 | } |
---|
531 | DesignatorChain & d = desigAlts.back(); |
---|
532 | PRINT( for ( Expression * expr : d ) { |
---|
533 | std::cerr << "____desig: " << expr << std::endl; |
---|
534 | } ) // for |
---|
535 | assertf( ! curTypes.empty(), "empty designator chosen"); |
---|
536 | |
---|
537 | // set new designators |
---|
538 | assertf( ! objStack.empty(), "empty object stack when setting designation" ); |
---|
539 | Designation * actualDesignation = new Designation( d ); |
---|
540 | objStack.top()->setPosition( d ); // destroys d |
---|
541 | return actualDesignation; |
---|
542 | } |
---|
543 | |
---|
544 | void CurrentObject::increment() { |
---|
545 | PRINT( std::cerr << "____increment" << std::endl; ) |
---|
546 | if ( ! objStack.empty() ) { |
---|
547 | PRINT( std::cerr << *objStack.top() << std::endl; ) |
---|
548 | objStack.top()->smallStep(); |
---|
549 | } |
---|
550 | } |
---|
551 | |
---|
552 | void CurrentObject::enterListInit() { |
---|
553 | PRINT( std::cerr << "____entering list init" << std::endl; ) |
---|
554 | assertf( ! objStack.empty(), "empty obj stack entering list init" ); |
---|
555 | Type * type = objStack.top()->getNext(); |
---|
556 | if ( type ) { |
---|
557 | objStack.push( createMemberIterator( type ) ); |
---|
558 | } else { |
---|
559 | assertf( false, "not sure about this case..." ); |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | void CurrentObject::exitListInit() { |
---|
564 | PRINT( std::cerr << "____exiting list init" << std::endl; ) |
---|
565 | assertf( ! objStack.empty(), "objstack empty" ); |
---|
566 | delete objStack.top(); |
---|
567 | objStack.pop(); |
---|
568 | if ( ! objStack.empty() ) { |
---|
569 | PRINT( std::cerr << *objStack.top() << std::endl; ) |
---|
570 | objStack.top()->bigStep(); |
---|
571 | } |
---|
572 | } |
---|
573 | |
---|
574 | std::list< InitAlternative > CurrentObject::getOptions() { |
---|
575 | PRINT( std::cerr << "____getting current options" << std::endl; ) |
---|
576 | assertf( ! objStack.empty(), "objstack empty in getOptions" ); |
---|
577 | return **objStack.top(); |
---|
578 | } |
---|
579 | |
---|
580 | Type * CurrentObject::getCurrentType() { |
---|
581 | PRINT( std::cerr << "____getting current type" << std::endl; ) |
---|
582 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); |
---|
583 | return objStack.top()->getNext(); |
---|
584 | } |
---|
585 | } // namespace ResolvExpr |
---|
586 | |
---|
587 | namespace ast { |
---|
588 | /// create a new MemberIterator that traverses a type correctly |
---|
589 | MemberIterator * createMemberIterator( const CodeLocation & loc, const Type * type ); |
---|
590 | |
---|
591 | /// Iterates "other" types (e.g. basic, pointer) which do not change at list initializer entry |
---|
592 | class SimpleIterator final : public MemberIterator { |
---|
593 | CodeLocation location; |
---|
594 | readonly< Type > type = nullptr; |
---|
595 | public: |
---|
596 | SimpleIterator( const CodeLocation & loc, const Type * t ) : location( loc ), type( t ) {} |
---|
597 | |
---|
598 | void setPosition( |
---|
599 | std::deque< ptr< Expr > >::const_iterator begin, |
---|
600 | std::deque< ptr< Expr > >::const_iterator end |
---|
601 | ) override { |
---|
602 | if ( begin != end ) { |
---|
603 | SemanticError( location, "Un-designated initializer given non-empty designator" ); |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | std::deque< InitAlternative > operator* () const override { return first(); } |
---|
608 | |
---|
609 | operator bool() const override { return type; } |
---|
610 | |
---|
611 | SimpleIterator & bigStep() override { return smallStep(); } |
---|
612 | SimpleIterator & smallStep() override { |
---|
613 | type = nullptr; // empty on increment because no members |
---|
614 | return *this; |
---|
615 | } |
---|
616 | |
---|
617 | const Type * getType() override { return type; } |
---|
618 | |
---|
619 | const Type * getNext() override { return type; } |
---|
620 | |
---|
621 | std::deque< InitAlternative > first() const override { |
---|
622 | if ( type ) return { InitAlternative{ type, new Designation{ location } } }; |
---|
623 | return {}; |
---|
624 | } |
---|
625 | }; |
---|
626 | |
---|
627 | /// Iterates array types |
---|
628 | class ArrayIterator final : public MemberIterator { |
---|
629 | CodeLocation location; |
---|
630 | readonly< ArrayType > array = nullptr; |
---|
631 | readonly< Type > base = nullptr; |
---|
632 | size_t index = 0; |
---|
633 | size_t size = 0; |
---|
634 | std::unique_ptr< MemberIterator > memberIter; |
---|
635 | |
---|
636 | void setSize( const Expr * expr ) { |
---|
637 | auto res = eval(expr); |
---|
638 | if ( ! res.second ) { |
---|
639 | SemanticError( location, |
---|
640 | toString("Array designator must be a constant expression: ", expr ) ); |
---|
641 | } |
---|
642 | size = res.first; |
---|
643 | } |
---|
644 | |
---|
645 | public: |
---|
646 | ArrayIterator( const CodeLocation & loc, const ArrayType * at ) |
---|
647 | : location( loc ), array( at ), base( at->base ) { |
---|
648 | PRINT( std::cerr << "Creating array iterator: " << at << std::endl; ) |
---|
649 | memberIter.reset( createMemberIterator( loc, base ) ); |
---|
650 | if ( at->isVarLen ) { |
---|
651 | SemanticError( location, at, "VLA initialization does not support @=: " ); |
---|
652 | } |
---|
653 | setSize( at->dimension ); |
---|
654 | } |
---|
655 | |
---|
656 | void setPosition( const Expr * expr ) { |
---|
657 | // need to permit integer-constant-expressions, including: integer constants, |
---|
658 | // enumeration constants, character constants, sizeof expressions, alignof expressions, |
---|
659 | // cast expressions |
---|
660 | if ( auto constExpr = dynamic_cast< const ConstantExpr * >( expr ) ) { |
---|
661 | try { |
---|
662 | index = constExpr->intValue(); |
---|
663 | } catch ( SemanticErrorException & ) { |
---|
664 | SemanticError( expr, |
---|
665 | "Constant expression of non-integral type in array designator: " ); |
---|
666 | } |
---|
667 | } else if ( auto castExpr = dynamic_cast< const CastExpr * >( expr ) ) { |
---|
668 | setPosition( castExpr->arg ); |
---|
669 | } else if ( |
---|
670 | dynamic_cast< const SizeofExpr * >( expr ) |
---|
671 | || dynamic_cast< const AlignofExpr * >( expr ) |
---|
672 | ) { |
---|
673 | index = 0; |
---|
674 | } else { |
---|
675 | assertf( false, |
---|
676 | "bad designator given to ArrayIterator: %s", toString( expr ).c_str() ); |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | void setPosition( |
---|
681 | std::deque< ptr< Expr > >::const_iterator begin, |
---|
682 | std::deque< ptr< Expr > >::const_iterator end |
---|
683 | ) override { |
---|
684 | if ( begin == end ) return; |
---|
685 | |
---|
686 | setPosition( *begin ); |
---|
687 | memberIter->setPosition( ++begin, end ); |
---|
688 | } |
---|
689 | |
---|
690 | std::deque< InitAlternative > operator* () const override { return first(); } |
---|
691 | |
---|
692 | operator bool() const override { return index < size; } |
---|
693 | |
---|
694 | ArrayIterator & bigStep() override { |
---|
695 | PRINT( std::cerr << "bigStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
696 | ++index; |
---|
697 | memberIter.reset( index < size ? createMemberIterator( location, base ) : nullptr ); |
---|
698 | return *this; |
---|
699 | } |
---|
700 | |
---|
701 | ArrayIterator & smallStep() override { |
---|
702 | PRINT( std::cerr << "smallStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
703 | if ( memberIter ) { |
---|
704 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; ) |
---|
705 | memberIter->smallStep(); |
---|
706 | if ( *memberIter ) { |
---|
707 | PRINT( std::cerr << "has valid member iter" << std::endl; ) |
---|
708 | return *this; |
---|
709 | } |
---|
710 | } |
---|
711 | return bigStep(); |
---|
712 | } |
---|
713 | |
---|
714 | const Type * getType() override { return array; } |
---|
715 | |
---|
716 | const Type * getNext() override { return base; } |
---|
717 | |
---|
718 | std::deque< InitAlternative > first() const override { |
---|
719 | PRINT( std::cerr << "first in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
720 | if ( memberIter && *memberIter ) { |
---|
721 | std::deque< InitAlternative > ret = memberIter->first(); |
---|
722 | for ( InitAlternative & alt : ret ) { |
---|
723 | alt.designation.get_and_mutate()->designators.emplace_front( |
---|
724 | ConstantExpr::from_ulong( location, index ) ); |
---|
725 | } |
---|
726 | return ret; |
---|
727 | } |
---|
728 | return {}; |
---|
729 | } |
---|
730 | }; |
---|
731 | |
---|
732 | class AggregateIterator : public MemberIterator { |
---|
733 | protected: |
---|
734 | using MemberList = std::vector< ptr< Decl > >; |
---|
735 | |
---|
736 | CodeLocation location; |
---|
737 | std::string kind; // for debug |
---|
738 | std::string name; |
---|
739 | const Type * inst; |
---|
740 | const MemberList & members; |
---|
741 | MemberList::const_iterator curMember; |
---|
742 | bool atbegin = true; // false at first {small,big}Step |
---|
743 | const Type * curType = nullptr; |
---|
744 | std::unique_ptr< MemberIterator > memberIter = nullptr; |
---|
745 | TypeSubstitution sub; |
---|
746 | |
---|
747 | bool init() { |
---|
748 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) |
---|
749 | if ( curMember != members.end() ) { |
---|
750 | if ( auto field = curMember->as< ObjectDecl >() ) { |
---|
751 | PRINT( std::cerr << "incremented to field: " << field << std::endl; ) |
---|
752 | curType = field->get_type(); |
---|
753 | memberIter.reset( createMemberIterator( location, curType ) ); |
---|
754 | return true; |
---|
755 | } |
---|
756 | } |
---|
757 | return false; |
---|
758 | } |
---|
759 | |
---|
760 | AggregateIterator( |
---|
761 | const CodeLocation & loc, const std::string k, const std::string & n, const Type * i, |
---|
762 | const MemberList & ms ) |
---|
763 | : location( loc ), kind( k ), name( n ), inst( i ), members( ms ), curMember( ms.begin() ), |
---|
764 | sub( genericSubstitution( i ) ) { |
---|
765 | PRINT( std::cerr << "Creating " << kind << "(" << name << ")"; ) |
---|
766 | init(); |
---|
767 | } |
---|
768 | |
---|
769 | public: |
---|
770 | void setPosition( |
---|
771 | std::deque< ptr< Expr > >::const_iterator begin, |
---|
772 | std::deque< ptr< Expr > >::const_iterator end |
---|
773 | ) final { |
---|
774 | if ( begin == end ) return; |
---|
775 | |
---|
776 | if ( auto varExpr = begin->as< VariableExpr >() ) { |
---|
777 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { |
---|
778 | if ( *curMember != varExpr->var ) continue; |
---|
779 | |
---|
780 | ++begin; |
---|
781 | |
---|
782 | memberIter.reset( createMemberIterator( location, varExpr->result ) ); |
---|
783 | curType = varExpr->result; |
---|
784 | atbegin = curMember == members.begin() && begin == end; |
---|
785 | memberIter->setPosition( begin, end ); |
---|
786 | return; |
---|
787 | } |
---|
788 | assertf( false, |
---|
789 | "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); |
---|
790 | } else { |
---|
791 | assertf( false, |
---|
792 | "bad designator given to %s: %s", kind.c_str(), toString( *begin ).c_str() ); |
---|
793 | } |
---|
794 | } |
---|
795 | |
---|
796 | std::deque< InitAlternative > operator* () const final { |
---|
797 | if ( memberIter && *memberIter ) { |
---|
798 | std::deque< InitAlternative > ret = memberIter->first(); |
---|
799 | PRINT( std::cerr << "sub: " << sub << std::endl; ) |
---|
800 | for ( InitAlternative & alt : ret ) { |
---|
801 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
802 | alt.designation.get_and_mutate()->designators.emplace_front( |
---|
803 | new VariableExpr{ location, curMember->strict_as< ObjectDecl >() } ); |
---|
804 | // need to substitute for generic types so that casts are to concrete types |
---|
805 | PRINT( std::cerr << " type is: " << alt.type; ) |
---|
806 | sub.apply( alt.type ); // also apply to designation?? |
---|
807 | PRINT( std::cerr << " ==> " << alt.type << std::endl; ) |
---|
808 | } |
---|
809 | return ret; |
---|
810 | } |
---|
811 | return {}; |
---|
812 | } |
---|
813 | |
---|
814 | AggregateIterator & smallStep() final { |
---|
815 | PRINT( std::cerr << "smallStep in " << kind << std::endl; ) |
---|
816 | atbegin = false; |
---|
817 | if ( memberIter ) { |
---|
818 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; ) |
---|
819 | memberIter->smallStep(); |
---|
820 | if ( *memberIter ) { |
---|
821 | PRINT( std::cerr << "success!" << std::endl; ) |
---|
822 | return *this; |
---|
823 | } |
---|
824 | } |
---|
825 | return bigStep(); |
---|
826 | } |
---|
827 | |
---|
828 | AggregateIterator & bigStep() override = 0; |
---|
829 | |
---|
830 | const Type * getType() final { return inst; } |
---|
831 | |
---|
832 | const Type * getNext() final { |
---|
833 | return ( memberIter && *memberIter ) ? memberIter->getType() : nullptr; |
---|
834 | } |
---|
835 | |
---|
836 | std::deque< InitAlternative > first() const final { |
---|
837 | std::deque< InitAlternative > ret; |
---|
838 | PRINT( std::cerr << "first " << kind << std::endl; ) |
---|
839 | if ( memberIter && *memberIter ) { |
---|
840 | PRINT( std::cerr << "adding children" << std::endl; ) |
---|
841 | ret = memberIter->first(); |
---|
842 | for ( InitAlternative & alt : ret ) { |
---|
843 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
844 | alt.designation.get_and_mutate()->designators.emplace_front( |
---|
845 | new VariableExpr{ location, curMember->strict_as< ObjectDecl >() } ); |
---|
846 | } |
---|
847 | } |
---|
848 | if ( atbegin ) { |
---|
849 | // only add self if at the very beginning of the structure |
---|
850 | PRINT( std::cerr << "adding self" << std::endl; ) |
---|
851 | ret.emplace_front( inst, new Designation{ location } ); |
---|
852 | } |
---|
853 | return ret; |
---|
854 | } |
---|
855 | }; |
---|
856 | |
---|
857 | class StructIterator final : public AggregateIterator { |
---|
858 | public: |
---|
859 | StructIterator( const CodeLocation & loc, const StructInstType * inst ) |
---|
860 | : AggregateIterator( loc, "StructIterator", inst->name, inst, inst->base->members ) {} |
---|
861 | |
---|
862 | operator bool() const override { |
---|
863 | return curMember != members.end() || (memberIter && *memberIter); |
---|
864 | } |
---|
865 | |
---|
866 | StructIterator & bigStep() override { |
---|
867 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
868 | atbegin = false; |
---|
869 | memberIter = nullptr; |
---|
870 | curType = nullptr; |
---|
871 | while ( curMember != members.end() ) { |
---|
872 | ++curMember; |
---|
873 | if ( init() ) return *this; |
---|
874 | } |
---|
875 | return *this; |
---|
876 | } |
---|
877 | }; |
---|
878 | |
---|
879 | class UnionIterator final : public AggregateIterator { |
---|
880 | public: |
---|
881 | UnionIterator( const CodeLocation & loc, const UnionInstType * inst ) |
---|
882 | : AggregateIterator( loc, "UnionIterator", inst->name, inst, inst->base->members ) {} |
---|
883 | |
---|
884 | operator bool() const override { return memberIter && *memberIter; } |
---|
885 | |
---|
886 | UnionIterator & bigStep() override { |
---|
887 | // unions only initialize one member |
---|
888 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
889 | atbegin = false; |
---|
890 | memberIter = nullptr; |
---|
891 | curType = nullptr; |
---|
892 | curMember = members.end(); |
---|
893 | return *this; |
---|
894 | } |
---|
895 | }; |
---|
896 | |
---|
897 | class TupleIterator final : public AggregateIterator { |
---|
898 | public: |
---|
899 | TupleIterator( const CodeLocation & loc, const TupleType * inst ) |
---|
900 | : AggregateIterator( |
---|
901 | loc, "TupleIterator", toString("Tuple", inst->size()), inst, inst->members |
---|
902 | ) {} |
---|
903 | |
---|
904 | operator bool() const override { |
---|
905 | return curMember != members.end() || (memberIter && *memberIter); |
---|
906 | } |
---|
907 | |
---|
908 | TupleIterator & bigStep() override { |
---|
909 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
910 | atbegin = false; |
---|
911 | memberIter = nullptr; |
---|
912 | curType = nullptr; |
---|
913 | while ( curMember != members.end() ) { |
---|
914 | ++curMember; |
---|
915 | if ( init() ) return *this; |
---|
916 | } |
---|
917 | return *this; |
---|
918 | } |
---|
919 | }; |
---|
920 | |
---|
921 | MemberIterator * createMemberIterator( const CodeLocation & loc, const Type * type ) { |
---|
922 | if ( auto aggr = dynamic_cast< const ReferenceToType * >( type ) ) { |
---|
923 | if ( auto sit = dynamic_cast< const StructInstType * >( aggr ) ) { |
---|
924 | return new StructIterator{ loc, sit }; |
---|
925 | } else if ( auto uit = dynamic_cast< const UnionInstType * >( aggr ) ) { |
---|
926 | return new UnionIterator{ loc, uit }; |
---|
927 | } else { |
---|
928 | assertf( |
---|
929 | dynamic_cast< const EnumInstType * >( aggr ) |
---|
930 | || dynamic_cast< const TypeInstType * >( aggr ), |
---|
931 | "Encountered unhandled ReferenceToType in createMemberIterator: %s", |
---|
932 | toString( type ).c_str() ); |
---|
933 | return new SimpleIterator{ loc, type }; |
---|
934 | } |
---|
935 | } else if ( auto at = dynamic_cast< const ArrayType * >( type ) ) { |
---|
936 | return new ArrayIterator{ loc, at }; |
---|
937 | } else if ( auto tt = dynamic_cast< const TupleType * >( type ) ) { |
---|
938 | return new TupleIterator{ loc, tt }; |
---|
939 | } else { |
---|
940 | return new SimpleIterator{ loc, type }; |
---|
941 | } |
---|
942 | } |
---|
943 | |
---|
944 | CurrentObject::CurrentObject( const CodeLocation & loc, const Type * type ) : objStack() { |
---|
945 | objStack.emplace_back( new SimpleIterator{ loc, type } ); |
---|
946 | } |
---|
947 | |
---|
948 | const Designation * CurrentObject::findNext( const Designation * designation ) { |
---|
949 | using DesignatorChain = std::deque< ptr< Expr > >; |
---|
950 | PRINT( std::cerr << "___findNext" << std::endl; ) |
---|
951 | |
---|
952 | // find all the d's |
---|
953 | std::vector< DesignatorChain > desigAlts{ {} }, newDesigAlts; |
---|
954 | std::deque< const Type * > curTypes{ objStack.back()->getType() }, newTypes; |
---|
955 | for ( const Expr * expr : designation->designators ) { |
---|
956 | PRINT( std::cerr << "____untyped: " << expr << std::endl; ) |
---|
957 | auto dit = desigAlts.begin(); |
---|
958 | if ( auto nexpr = dynamic_cast< const NameExpr * >( expr ) ) { |
---|
959 | for ( const Type * t : curTypes ) { |
---|
960 | assert( dit != desigAlts.end() ); |
---|
961 | |
---|
962 | DesignatorChain & d = *dit; |
---|
963 | PRINT( std::cerr << "____actual: " << t << std::endl; ) |
---|
964 | if ( auto refType = dynamic_cast< const ReferenceToType * >( t ) ) { |
---|
965 | // concatenate identical field names |
---|
966 | for ( const Decl * mem : refType->lookup( nexpr->name ) ) { |
---|
967 | if ( auto field = dynamic_cast< const ObjectDecl * >( mem ) ) { |
---|
968 | PRINT( std::cerr << "____alt: " << field->type << std::endl; ) |
---|
969 | DesignatorChain d2 = d; |
---|
970 | d2.emplace_back( new VariableExpr{ expr->location, field } ); |
---|
971 | newDesigAlts.emplace_back( std::move( d2 ) ); |
---|
972 | newTypes.emplace_back( field->type ); |
---|
973 | } |
---|
974 | } |
---|
975 | } |
---|
976 | |
---|
977 | ++dit; |
---|
978 | } |
---|
979 | } else { |
---|
980 | for ( const Type * t : curTypes ) { |
---|
981 | assert( dit != desigAlts.end() ); |
---|
982 | |
---|
983 | DesignatorChain & d = *dit; |
---|
984 | if ( auto at = dynamic_cast< const ArrayType * >( t ) ) { |
---|
985 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; ) |
---|
986 | d.emplace_back( expr ); |
---|
987 | newDesigAlts.emplace_back( d ); |
---|
988 | newTypes.emplace_back( at->base ); |
---|
989 | } |
---|
990 | } |
---|
991 | } |
---|
992 | |
---|
993 | // reset queue |
---|
994 | desigAlts = std::move( newDesigAlts ); |
---|
995 | newDesigAlts.clear(); |
---|
996 | curTypes = std::move( newTypes ); |
---|
997 | newTypes.clear(); |
---|
998 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%zu) and current types (%zu) out of sync", desigAlts.size(), curTypes.size() ); |
---|
999 | } |
---|
1000 | |
---|
1001 | if ( desigAlts.size() > 1 ) { |
---|
1002 | SemanticError( designation, toString("Too many alternatives (", desigAlts.size(), ") for designation: ") ); |
---|
1003 | } else if ( desigAlts.empty() ) { |
---|
1004 | SemanticError( designation, "No reasonable alternatives for designation: " ); |
---|
1005 | } |
---|
1006 | |
---|
1007 | DesignatorChain & d = desigAlts.back(); |
---|
1008 | PRINT( for ( Expression * expr : d ) { |
---|
1009 | std::cerr << "____desig: " << expr << std::endl; |
---|
1010 | } ) // for |
---|
1011 | assertf( ! curTypes.empty(), "empty designator chosen"); |
---|
1012 | |
---|
1013 | // set new designators |
---|
1014 | assertf( ! objStack.empty(), "empty object stack when setting designation" ); |
---|
1015 | Designation * actualDesignation = |
---|
1016 | new Designation{ designation->location, DesignatorChain{d} }; |
---|
1017 | objStack.back()->setPosition( d ); // destroys d |
---|
1018 | return actualDesignation; |
---|
1019 | } |
---|
1020 | |
---|
1021 | void CurrentObject::setNext( const Designation * designation ) { |
---|
1022 | PRINT( std::cerr << "____setNext" << designation << std::endl; ) |
---|
1023 | assertf( ! objStack.empty(), "obj stack empty in setNext" ); |
---|
1024 | objStack.back()->setPosition( designation->designators ); |
---|
1025 | } |
---|
1026 | |
---|
1027 | void CurrentObject::increment() { |
---|
1028 | PRINT( std::cerr << "____increment" << std::endl; ) |
---|
1029 | if ( objStack.empty() ) return; |
---|
1030 | PRINT( std::cerr << *objStack.back() << std::endl; ) |
---|
1031 | objStack.back()->smallStep(); |
---|
1032 | } |
---|
1033 | |
---|
1034 | void CurrentObject::enterListInit( const CodeLocation & loc ) { |
---|
1035 | PRINT( std::cerr << "____entering list init" << std::endl; ) |
---|
1036 | assertf( ! objStack.empty(), "empty obj stack entering list init" ); |
---|
1037 | const ast::Type * type = objStack.back()->getNext(); |
---|
1038 | assert( type ); |
---|
1039 | objStack.emplace_back( createMemberIterator( loc, type ) ); |
---|
1040 | } |
---|
1041 | |
---|
1042 | void CurrentObject::exitListInit() { |
---|
1043 | PRINT( std::cerr << "____exiting list init" << std::endl; ) |
---|
1044 | assertf( ! objStack.empty(), "objstack empty" ); |
---|
1045 | objStack.pop_back(); |
---|
1046 | if ( ! objStack.empty() ) { |
---|
1047 | PRINT( std::cerr << *objStack.back() << std::endl; ) |
---|
1048 | objStack.back()->bigStep(); |
---|
1049 | } |
---|
1050 | } |
---|
1051 | |
---|
1052 | std::deque< InitAlternative > CurrentObject::getOptions() { |
---|
1053 | PRINT( std::cerr << "____getting current options" << std::endl; ) |
---|
1054 | assertf( ! objStack.empty(), "objstack empty in getOptions" ); |
---|
1055 | return **objStack.back(); |
---|
1056 | } |
---|
1057 | |
---|
1058 | const Type * CurrentObject::getCurrentType() { |
---|
1059 | PRINT( std::cerr << "____getting current type" << std::endl; ) |
---|
1060 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); |
---|
1061 | return objStack.back()->getNext(); |
---|
1062 | } |
---|
1063 | } |
---|
1064 | |
---|
1065 | // Local Variables: // |
---|
1066 | // tab-width: 4 // |
---|
1067 | // mode: c++ // |
---|
1068 | // compile-command: "make install" // |
---|
1069 | // End: // |
---|