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