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