[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 | // |
---|
[c92bdcc] | 7 | // CurrentObject.cpp -- |
---|
[e4d829b] | 8 | // |
---|
| 9 | // Author : Rob Schluntz |
---|
| 10 | // Created On : Tue Jun 13 15:28:32 2017 |
---|
[b1f2007] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sat Dec 9 17:49:51 2023 |
---|
| 13 | // Update Count : 20 |
---|
[e4d829b] | 14 | // |
---|
| 15 | |
---|
[c92bdcc] | 16 | #include "CurrentObject.hpp" |
---|
| 17 | |
---|
[ea6332d] | 18 | #include <stddef.h> // for size_t |
---|
| 19 | #include <cassert> // for assertf, assert, safe_dynamic_... |
---|
[60aaa51d] | 20 | #include <deque> |
---|
[ea6332d] | 21 | #include <iostream> // for ostream, operator<<, basic_ost... |
---|
| 22 | #include <stack> // for stack |
---|
| 23 | #include <string> // for string, operator<<, allocator |
---|
[e4d829b] | 24 | |
---|
[8e1467d] | 25 | #include "AST/Copy.hpp" // for shallowCopy |
---|
[2b59f55] | 26 | #include "AST/Expr.hpp" // for InitAlternative |
---|
[60aaa51d] | 27 | #include "AST/GenericSubstitution.hpp" // for genericSubstitution |
---|
[2b59f55] | 28 | #include "AST/Init.hpp" // for Designation |
---|
| 29 | #include "AST/Node.hpp" // for readonly |
---|
[8f06277] | 30 | #include "AST/Print.hpp" // for readonly |
---|
[60aaa51d] | 31 | #include "AST/Type.hpp" |
---|
[c92bdcc] | 32 | #include "Common/Eval.hpp" // for eval |
---|
| 33 | #include "Common/Indenter.hpp" // for Indenter, operator<< |
---|
| 34 | #include "Common/SemanticError.hpp" // for SemanticError |
---|
| 35 | #include "Common/Utility.hpp" // for toString |
---|
[e4d829b] | 36 | |
---|
| 37 | #if 0 |
---|
| 38 | #define PRINT(x) x |
---|
| 39 | #else |
---|
| 40 | #define PRINT(x) |
---|
| 41 | #endif |
---|
| 42 | |
---|
[b7d92b96] | 43 | namespace ast { |
---|
[485393c] | 44 | |
---|
[2908f08] | 45 | /// Iterates members of a type by initializer. |
---|
| 46 | class MemberIterator { |
---|
| 47 | public: |
---|
| 48 | virtual ~MemberIterator() {} |
---|
[485393c] | 49 | |
---|
[2908f08] | 50 | /// Internal set position based on iterator ranges. |
---|
| 51 | virtual void setPosition( |
---|
| 52 | std::deque< ptr< Expr > >::const_iterator it, |
---|
| 53 | std::deque< ptr< Expr > >::const_iterator end ) = 0; |
---|
[485393c] | 54 | |
---|
[2908f08] | 55 | /// Walks the current object using the given designators as a guide. |
---|
| 56 | void setPosition( const std::deque< ptr< Expr > > & designators ) { |
---|
| 57 | setPosition( designators.begin(), designators.end() ); |
---|
| 58 | } |
---|
[485393c] | 59 | |
---|
[2908f08] | 60 | /// Retrieve the list of possible (Type,Designation) pairs for the |
---|
| 61 | /// current position in the current object. |
---|
| 62 | virtual std::deque< InitAlternative > operator* () const = 0; |
---|
[485393c] | 63 | |
---|
[2908f08] | 64 | /// True if the iterator is not currently at the end. |
---|
| 65 | virtual operator bool() const = 0; |
---|
[485393c] | 66 | |
---|
[2908f08] | 67 | /// Moves the iterator by one member in the current object. |
---|
| 68 | virtual MemberIterator & bigStep() = 0; |
---|
[485393c] | 69 | |
---|
[2908f08] | 70 | /// Moves the iterator by one member in the current subobject. |
---|
| 71 | virtual MemberIterator & smallStep() = 0; |
---|
[485393c] | 72 | |
---|
[2908f08] | 73 | /// The type of the current object. |
---|
| 74 | virtual const Type * getType() = 0; |
---|
[b7d92b96] | 75 | |
---|
[2908f08] | 76 | /// The type of the current subobject. |
---|
| 77 | virtual const Type * getNext() = 0; |
---|
[2b59f55] | 78 | |
---|
[2908f08] | 79 | /// Helper for operator*; aggregates must add designator to each init |
---|
| 80 | /// alternative, but adding designators in operator* creates duplicates. |
---|
| 81 | virtual std::deque< InitAlternative > first() const = 0; |
---|
| 82 | }; |
---|
[2b59f55] | 83 | |
---|
[2908f08] | 84 | namespace { |
---|
[60aaa51d] | 85 | |
---|
[2908f08] | 86 | /// create a new MemberIterator that traverses a type correctly |
---|
| 87 | MemberIterator * createMemberIterator( const CodeLocation & loc, const Type * type ); |
---|
[60aaa51d] | 88 | |
---|
[2908f08] | 89 | /// Iterates "other" types (e.g. basic, pointer) which do not change at list initializer entry |
---|
| 90 | class SimpleIterator final : public MemberIterator { |
---|
| 91 | CodeLocation location; |
---|
| 92 | const Type * type = nullptr; |
---|
| 93 | public: |
---|
| 94 | SimpleIterator( const CodeLocation & loc, const Type * t ) : location( loc ), type( t ) {} |
---|
| 95 | |
---|
| 96 | void setPosition( |
---|
| 97 | std::deque< ptr< Expr > >::const_iterator begin, |
---|
| 98 | std::deque< ptr< Expr > >::const_iterator end |
---|
| 99 | ) override { |
---|
| 100 | if ( begin != end ) { |
---|
| 101 | SemanticError( location, "Un-designated initializer given non-empty designator" ); |
---|
[60aaa51d] | 102 | } |
---|
[2908f08] | 103 | } |
---|
[60aaa51d] | 104 | |
---|
[2908f08] | 105 | std::deque< InitAlternative > operator* () const override { return first(); } |
---|
[60aaa51d] | 106 | |
---|
[2908f08] | 107 | operator bool() const override { return type; } |
---|
[60aaa51d] | 108 | |
---|
[2908f08] | 109 | SimpleIterator & bigStep() override { return smallStep(); } |
---|
| 110 | SimpleIterator & smallStep() override { |
---|
| 111 | type = nullptr; // empty on increment because no members |
---|
| 112 | return *this; |
---|
| 113 | } |
---|
[60aaa51d] | 114 | |
---|
[2908f08] | 115 | const Type * getType() override { return type; } |
---|
[60aaa51d] | 116 | |
---|
[2908f08] | 117 | const Type * getNext() override { return type; } |
---|
[60aaa51d] | 118 | |
---|
[2908f08] | 119 | std::deque< InitAlternative > first() const override { |
---|
| 120 | if ( type ) return { InitAlternative{ type, new Designation{ location } } }; |
---|
| 121 | return {}; |
---|
| 122 | } |
---|
| 123 | }; |
---|
| 124 | |
---|
| 125 | /// Iterates over an indexed type: |
---|
| 126 | class IndexIterator : public MemberIterator { |
---|
| 127 | protected: |
---|
| 128 | CodeLocation location; |
---|
| 129 | size_t index = 0; |
---|
| 130 | size_t size = 0; |
---|
| 131 | std::unique_ptr<MemberIterator> memberIter; |
---|
| 132 | public: |
---|
| 133 | IndexIterator( const CodeLocation & loc, size_t size ) : |
---|
| 134 | location( loc ), size( size ) |
---|
| 135 | {} |
---|
| 136 | |
---|
| 137 | void setPosition( const Expr * expr ) { |
---|
| 138 | // need to permit integer-constant-expressions, including: integer constants, |
---|
| 139 | // enumeration constants, character constants, sizeof expressions, alignof expressions, |
---|
| 140 | // cast expressions |
---|
| 141 | |
---|
| 142 | auto arg = eval( expr ); |
---|
| 143 | assertf( arg.hasKnownValue, "Non-evaluable expression made it to IndexIterator" ); |
---|
| 144 | index = arg.knownValue; |
---|
| 145 | |
---|
| 146 | // if ( auto constExpr = dynamic_cast< const ConstantExpr * >( expr ) ) { |
---|
| 147 | // try { |
---|
| 148 | // index = constExpr->intValue(); |
---|
| 149 | // } catch ( SemanticErrorException & ) { |
---|
| 150 | // SemanticError( expr, "Constant expression of non-integral type in array designator: " ); |
---|
| 151 | // } |
---|
| 152 | // } else if ( auto castExpr = dynamic_cast< const CastExpr * >( expr ) ) { |
---|
| 153 | // setPosition( castExpr->arg ); |
---|
| 154 | // } else if ( dynamic_cast< const SizeofExpr * >( expr ) || dynamic_cast< const AlignofExpr * >( expr ) ) { |
---|
| 155 | // index = 0; |
---|
| 156 | // } else { |
---|
| 157 | // assertf( false, "2 bad designator given to ArrayIterator: %s", toString( expr ).c_str() ); |
---|
| 158 | // } |
---|
| 159 | } |
---|
[60aaa51d] | 160 | |
---|
[2908f08] | 161 | void setPosition( |
---|
| 162 | std::deque<ast::ptr<ast::Expr>>::const_iterator begin, |
---|
| 163 | std::deque<ast::ptr<ast::Expr>>::const_iterator end |
---|
| 164 | ) override { |
---|
| 165 | if ( begin == end ) return; |
---|
[699a97d] | 166 | |
---|
[2908f08] | 167 | setPosition( *begin ); |
---|
| 168 | memberIter->setPosition( ++begin, end ); |
---|
| 169 | } |
---|
[699a97d] | 170 | |
---|
[2908f08] | 171 | std::deque< InitAlternative > operator* () const override { return first(); } |
---|
[699a97d] | 172 | |
---|
[2908f08] | 173 | operator bool() const override { return index < size; } |
---|
| 174 | }; |
---|
| 175 | |
---|
| 176 | /// Iterates over the members of array types: |
---|
| 177 | class ArrayIterator final : public IndexIterator { |
---|
| 178 | const ArrayType * array = nullptr; |
---|
| 179 | const Type * base = nullptr; |
---|
| 180 | |
---|
| 181 | size_t getSize( const Expr * expr ) { |
---|
| 182 | auto res = eval( expr ); |
---|
| 183 | if ( !res.hasKnownValue ) { |
---|
[b1f2007] | 184 | SemanticError( location, "Array designator must be a constant expression %s", toString( expr ).c_str() ); |
---|
[699a97d] | 185 | } |
---|
[2908f08] | 186 | return res.knownValue; |
---|
| 187 | } |
---|
[60aaa51d] | 188 | |
---|
[2908f08] | 189 | public: |
---|
| 190 | ArrayIterator( const CodeLocation & loc, const ArrayType * at ) : |
---|
| 191 | IndexIterator( loc, getSize( at->dimension) ), |
---|
| 192 | array( at ), base( at->base ) { |
---|
| 193 | PRINT( std::cerr << "Creating array iterator: " << at << std::endl; ) |
---|
| 194 | memberIter.reset( createMemberIterator( loc, base ) ); |
---|
| 195 | if ( at->isVarLen ) { |
---|
| 196 | SemanticError( location, at, "VLA initialization does not support @=: " ); |
---|
[60aaa51d] | 197 | } |
---|
[2908f08] | 198 | } |
---|
[60aaa51d] | 199 | |
---|
[2908f08] | 200 | ArrayIterator & bigStep() override { |
---|
| 201 | PRINT( std::cerr << "bigStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
| 202 | ++index; |
---|
| 203 | memberIter.reset( index < size ? createMemberIterator( location, base ) : nullptr ); |
---|
| 204 | return *this; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | ArrayIterator & smallStep() override { |
---|
| 208 | PRINT( std::cerr << "smallStep in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
| 209 | if ( memberIter ) { |
---|
| 210 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; ) |
---|
| 211 | memberIter->smallStep(); |
---|
| 212 | if ( *memberIter ) { |
---|
| 213 | PRINT( std::cerr << "has valid member iter" << std::endl; ) |
---|
| 214 | return *this; |
---|
[60aaa51d] | 215 | } |
---|
| 216 | } |
---|
[2908f08] | 217 | return bigStep(); |
---|
| 218 | } |
---|
[60aaa51d] | 219 | |
---|
[2908f08] | 220 | const Type * getType() override { return array; } |
---|
[60aaa51d] | 221 | |
---|
[2908f08] | 222 | const Type * getNext() override { return base; } |
---|
[60aaa51d] | 223 | |
---|
[2908f08] | 224 | std::deque< InitAlternative > first() const override { |
---|
| 225 | PRINT( std::cerr << "first in ArrayIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
| 226 | if ( memberIter && *memberIter ) { |
---|
| 227 | std::deque< InitAlternative > ret = memberIter->first(); |
---|
| 228 | for ( InitAlternative & alt : ret ) { |
---|
| 229 | alt.designation.get_and_mutate()->designators.emplace_front( ConstantExpr::from_ulong( location, index ) ); |
---|
[60aaa51d] | 230 | } |
---|
[2908f08] | 231 | return ret; |
---|
[60aaa51d] | 232 | } |
---|
[2908f08] | 233 | return {}; |
---|
| 234 | } |
---|
| 235 | }; |
---|
| 236 | |
---|
| 237 | class AggregateIterator : public MemberIterator { |
---|
| 238 | protected: |
---|
| 239 | using MemberList = std::vector< ptr< Decl > >; |
---|
| 240 | |
---|
| 241 | CodeLocation location; |
---|
| 242 | std::string kind; // for debug |
---|
| 243 | std::string name; |
---|
| 244 | const Type * inst; |
---|
| 245 | const MemberList & members; |
---|
| 246 | MemberList::const_iterator curMember; |
---|
| 247 | bool atbegin = true; // false at first {small,big}Step |
---|
| 248 | const Type * curType = nullptr; |
---|
| 249 | std::unique_ptr< MemberIterator > memberIter = nullptr; |
---|
| 250 | TypeSubstitution sub; |
---|
| 251 | |
---|
| 252 | bool init() { |
---|
| 253 | PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) |
---|
| 254 | if ( curMember != members.end() ) { |
---|
| 255 | if ( auto field = curMember->as< ObjectDecl >() ) { |
---|
| 256 | PRINT( std::cerr << "incremented to field: " << field << std::endl; ) |
---|
| 257 | curType = field->get_type(); |
---|
| 258 | memberIter.reset( createMemberIterator( location, curType ) ); |
---|
| 259 | return true; |
---|
[60aaa51d] | 260 | } |
---|
| 261 | } |
---|
[2908f08] | 262 | return false; |
---|
| 263 | } |
---|
[60aaa51d] | 264 | |
---|
[2908f08] | 265 | AggregateIterator( |
---|
| 266 | const CodeLocation & loc, const std::string k, const std::string & n, const Type * i, |
---|
| 267 | const MemberList & ms ) |
---|
| 268 | : location( loc ), kind( k ), name( n ), inst( i ), members( ms ), curMember( ms.begin() ), |
---|
| 269 | sub( genericSubstitution( i ) ) { |
---|
| 270 | PRINT( std::cerr << "Creating " << kind << "(" << name << ")"; ) |
---|
| 271 | init(); |
---|
| 272 | } |
---|
[60aaa51d] | 273 | |
---|
[2908f08] | 274 | public: |
---|
| 275 | void setPosition( |
---|
| 276 | std::deque< ptr< Expr > >::const_iterator begin, |
---|
| 277 | std::deque< ptr< Expr > >::const_iterator end |
---|
| 278 | ) final { |
---|
| 279 | if ( begin == end ) return; |
---|
[60aaa51d] | 280 | |
---|
[2908f08] | 281 | if ( auto varExpr = begin->as< VariableExpr >() ) { |
---|
| 282 | for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { |
---|
| 283 | if ( *curMember != varExpr->var ) continue; |
---|
[60aaa51d] | 284 | |
---|
[2908f08] | 285 | ++begin; |
---|
[60aaa51d] | 286 | |
---|
[2908f08] | 287 | memberIter.reset( createMemberIterator( location, varExpr->result ) ); |
---|
| 288 | curType = varExpr->result; |
---|
| 289 | atbegin = curMember == members.begin() && begin == end; |
---|
| 290 | memberIter->setPosition( begin, end ); |
---|
| 291 | return; |
---|
[60aaa51d] | 292 | } |
---|
[2908f08] | 293 | assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); |
---|
| 294 | } else { |
---|
| 295 | assertf( false, "1 bad designator given to %s: %s", kind.c_str(), toString( *begin ).c_str() ); |
---|
[60aaa51d] | 296 | } |
---|
[2908f08] | 297 | } |
---|
[60aaa51d] | 298 | |
---|
[2908f08] | 299 | std::deque< InitAlternative > operator* () const final { |
---|
| 300 | if ( memberIter && *memberIter ) { |
---|
| 301 | std::deque< InitAlternative > ret = memberIter->first(); |
---|
| 302 | PRINT( std::cerr << "sub: " << sub << std::endl; ) |
---|
| 303 | for ( InitAlternative & alt : ret ) { |
---|
| 304 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
| 305 | alt.designation.get_and_mutate()->designators.emplace_front( |
---|
| 306 | new VariableExpr{ location, curMember->strict_as< ObjectDecl >() } ); |
---|
| 307 | // need to substitute for generic types so that casts are to concrete types |
---|
| 308 | alt.type = shallowCopy(alt.type.get()); |
---|
| 309 | PRINT( std::cerr << " type is: " << alt.type; ) |
---|
| 310 | sub.apply( alt.type ); // also apply to designation?? |
---|
| 311 | PRINT( std::cerr << " ==> " << alt.type << std::endl; ) |
---|
[60aaa51d] | 312 | } |
---|
[2908f08] | 313 | return ret; |
---|
[60aaa51d] | 314 | } |
---|
[2908f08] | 315 | return {}; |
---|
| 316 | } |
---|
[60aaa51d] | 317 | |
---|
[2908f08] | 318 | AggregateIterator & smallStep() final { |
---|
| 319 | PRINT( std::cerr << "smallStep in " << kind << std::endl; ) |
---|
| 320 | atbegin = false; |
---|
| 321 | if ( memberIter ) { |
---|
| 322 | PRINT( std::cerr << "has member iter, incrementing..." << std::endl; ) |
---|
| 323 | memberIter->smallStep(); |
---|
| 324 | if ( *memberIter ) { |
---|
| 325 | PRINT( std::cerr << "success!" << std::endl; ) |
---|
| 326 | return *this; |
---|
[60aaa51d] | 327 | } |
---|
| 328 | } |
---|
[2908f08] | 329 | return bigStep(); |
---|
| 330 | } |
---|
[60aaa51d] | 331 | |
---|
[2908f08] | 332 | AggregateIterator & bigStep() override = 0; |
---|
[60aaa51d] | 333 | |
---|
[2908f08] | 334 | const Type * getType() final { return inst; } |
---|
[60aaa51d] | 335 | |
---|
[2908f08] | 336 | const Type * getNext() final { |
---|
| 337 | bool hasMember = memberIter && *memberIter; |
---|
| 338 | return hasMember ? memberIter->getType() : nullptr; |
---|
| 339 | } |
---|
[60aaa51d] | 340 | |
---|
[2908f08] | 341 | std::deque< InitAlternative > first() const final { |
---|
| 342 | std::deque< InitAlternative > ret; |
---|
| 343 | PRINT( std::cerr << "first " << kind << std::endl; ) |
---|
| 344 | if ( memberIter && *memberIter ) { |
---|
| 345 | PRINT( std::cerr << "adding children" << std::endl; ) |
---|
| 346 | ret = memberIter->first(); |
---|
| 347 | for ( InitAlternative & alt : ret ) { |
---|
| 348 | PRINT( std::cerr << "iterating and adding designators" << std::endl; ) |
---|
| 349 | alt.designation.get_and_mutate()->designators.emplace_front( |
---|
| 350 | new VariableExpr{ location, curMember->strict_as< ObjectDecl >() } ); |
---|
[60aaa51d] | 351 | } |
---|
| 352 | } |
---|
[2908f08] | 353 | if ( atbegin ) { |
---|
| 354 | // only add self if at the very beginning of the structure |
---|
| 355 | PRINT( std::cerr << "adding self" << std::endl; ) |
---|
| 356 | ret.emplace_front( inst, new Designation{ location } ); |
---|
| 357 | } |
---|
| 358 | return ret; |
---|
| 359 | } |
---|
| 360 | }; |
---|
[60aaa51d] | 361 | |
---|
[2908f08] | 362 | class StructIterator final : public AggregateIterator { |
---|
| 363 | public: |
---|
| 364 | StructIterator( const CodeLocation & loc, const StructInstType * inst ) |
---|
| 365 | : AggregateIterator( loc, "StructIterator", inst->name, inst, inst->base->members ) {} |
---|
[60aaa51d] | 366 | |
---|
[2908f08] | 367 | operator bool() const override { |
---|
| 368 | return curMember != members.end() || (memberIter && *memberIter); |
---|
| 369 | } |
---|
[60aaa51d] | 370 | |
---|
[2908f08] | 371 | StructIterator & bigStep() override { |
---|
| 372 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
| 373 | atbegin = false; |
---|
| 374 | memberIter = nullptr; |
---|
| 375 | curType = nullptr; |
---|
| 376 | while ( curMember != members.end() ) { |
---|
| 377 | ++curMember; |
---|
| 378 | if ( init() ) return *this; |
---|
[60aaa51d] | 379 | } |
---|
[2908f08] | 380 | return *this; |
---|
| 381 | } |
---|
| 382 | }; |
---|
| 383 | |
---|
| 384 | class UnionIterator final : public AggregateIterator { |
---|
| 385 | public: |
---|
| 386 | UnionIterator( const CodeLocation & loc, const UnionInstType * inst ) |
---|
| 387 | : AggregateIterator( loc, "UnionIterator", inst->name, inst, inst->base->members ) {} |
---|
| 388 | |
---|
| 389 | operator bool() const override { return memberIter && *memberIter; } |
---|
| 390 | |
---|
| 391 | UnionIterator & bigStep() override { |
---|
| 392 | // unions only initialize one member |
---|
| 393 | PRINT( std::cerr << "bigStep in " << kind << std::endl; ) |
---|
| 394 | atbegin = false; |
---|
| 395 | memberIter = nullptr; |
---|
| 396 | curType = nullptr; |
---|
| 397 | curMember = members.end(); |
---|
| 398 | return *this; |
---|
| 399 | } |
---|
| 400 | }; |
---|
[60aaa51d] | 401 | |
---|
[2908f08] | 402 | /// Iterates across the positions in a tuple: |
---|
| 403 | class TupleIterator final : public IndexIterator { |
---|
| 404 | ast::TupleType const * const tuple; |
---|
[ea2759b] | 405 | |
---|
[2908f08] | 406 | const ast::Type * typeAtIndex() const { |
---|
| 407 | assert( index < size ); |
---|
| 408 | return tuple->types[ index ].get(); |
---|
| 409 | } |
---|
[fac05b3] | 410 | |
---|
[2908f08] | 411 | public: |
---|
| 412 | TupleIterator( const CodeLocation & loc, const TupleType * type ) |
---|
| 413 | : IndexIterator( loc, type->size() ), tuple( type ) { |
---|
| 414 | PRINT( std::cerr << "Creating tuple iterator: " << type << std::endl; ) |
---|
| 415 | memberIter.reset( createMemberIterator( loc, typeAtIndex() ) ); |
---|
| 416 | } |
---|
[60aaa51d] | 417 | |
---|
[2908f08] | 418 | TupleIterator & bigStep() override { |
---|
| 419 | ++index; |
---|
| 420 | memberIter.reset( index < size ? |
---|
| 421 | createMemberIterator( location, typeAtIndex() ) : nullptr ); |
---|
| 422 | return *this; |
---|
| 423 | } |
---|
[ea2759b] | 424 | |
---|
[2908f08] | 425 | TupleIterator & smallStep() override { |
---|
| 426 | if ( memberIter ) { |
---|
| 427 | PRINT( std::cerr << "has member iter: " << *memberIter << std::endl; ) |
---|
| 428 | memberIter->smallStep(); |
---|
| 429 | if ( !memberIter ) { |
---|
| 430 | PRINT( std::cerr << "has valid member iter" << std::endl; ) |
---|
| 431 | return *this; |
---|
[ea2759b] | 432 | } |
---|
| 433 | } |
---|
[2908f08] | 434 | return bigStep(); |
---|
| 435 | } |
---|
[ea2759b] | 436 | |
---|
[2908f08] | 437 | const ast::Type * getType() override { |
---|
| 438 | return tuple; |
---|
| 439 | } |
---|
[ea2759b] | 440 | |
---|
[2908f08] | 441 | const ast::Type * getNext() override { |
---|
| 442 | bool hasMember = memberIter && *memberIter; |
---|
| 443 | return hasMember ? memberIter->getType() : nullptr; |
---|
| 444 | } |
---|
[ea2759b] | 445 | |
---|
[2908f08] | 446 | std::deque< InitAlternative > first() const override { |
---|
| 447 | PRINT( std::cerr << "first in TupleIterator (" << index << "/" << size << ")" << std::endl; ) |
---|
| 448 | if ( memberIter && *memberIter ) { |
---|
| 449 | std::deque< InitAlternative > ret = memberIter->first(); |
---|
| 450 | for ( InitAlternative & alt : ret ) { |
---|
| 451 | alt.designation.get_and_mutate()->designators.emplace_front( |
---|
| 452 | ConstantExpr::from_ulong( location, index ) ); |
---|
[ea2759b] | 453 | } |
---|
[2908f08] | 454 | return ret; |
---|
[ea2759b] | 455 | } |
---|
[2908f08] | 456 | return {}; |
---|
| 457 | } |
---|
| 458 | }; |
---|
| 459 | |
---|
| 460 | MemberIterator * createMemberIterator( const CodeLocation & loc, const Type * type ) { |
---|
| 461 | if ( auto aggr = dynamic_cast< const BaseInstType * >( type ) ) { |
---|
| 462 | if ( auto sit = dynamic_cast< const StructInstType * >( aggr ) ) { |
---|
| 463 | assert( sit->base ); |
---|
| 464 | return new StructIterator{ loc, sit }; |
---|
| 465 | } else if ( auto uit = dynamic_cast< const UnionInstType * >( aggr ) ) { |
---|
| 466 | assert( uit->base ); |
---|
| 467 | return new UnionIterator{ loc, uit }; |
---|
[60aaa51d] | 468 | } else { |
---|
[2908f08] | 469 | assertf( |
---|
| 470 | dynamic_cast< const EnumInstType * >( type ) |
---|
| 471 | || dynamic_cast< const TypeInstType * >( type ), |
---|
| 472 | "Encountered unhandled BaseInstType in createMemberIterator: %s", |
---|
| 473 | toString( type ).c_str() ); |
---|
[60aaa51d] | 474 | return new SimpleIterator{ loc, type }; |
---|
| 475 | } |
---|
[2908f08] | 476 | } else if ( auto at = dynamic_cast< const ArrayType * >( type ) ) { |
---|
| 477 | return new ArrayIterator{ loc, at }; |
---|
| 478 | } else if ( auto tt = dynamic_cast< const TupleType * >( type ) ) { |
---|
| 479 | return new TupleIterator{ loc, tt }; |
---|
| 480 | } else { |
---|
| 481 | return new SimpleIterator{ loc, type }; |
---|
[60aaa51d] | 482 | } |
---|
[2908f08] | 483 | } |
---|
| 484 | |
---|
| 485 | } // namespace |
---|
| 486 | |
---|
| 487 | CurrentObject::CurrentObject( const CodeLocation & loc, const Type * type ) : objStack() { |
---|
| 488 | objStack.emplace_back( new SimpleIterator{ loc, type } ); |
---|
| 489 | } |
---|
[60aaa51d] | 490 | |
---|
[2908f08] | 491 | const Designation * CurrentObject::findNext( const Designation * designation ) { |
---|
| 492 | using DesignatorChain = std::deque< ptr< Expr > >; |
---|
| 493 | PRINT( std::cerr << "___findNext" << std::endl; ) |
---|
| 494 | |
---|
| 495 | // find all the d's |
---|
| 496 | std::vector< DesignatorChain > desigAlts{ {} }, newDesigAlts; |
---|
| 497 | std::deque< const Type * > curTypes{ objStack.back()->getType() }, newTypes; |
---|
| 498 | for ( const Expr * expr : designation->designators ) { |
---|
| 499 | PRINT( std::cerr << "____untyped: " << expr << std::endl; ) |
---|
| 500 | auto dit = desigAlts.begin(); |
---|
[da5ac2e] | 501 | auto nexpr = dynamic_cast< const NameExpr * >( expr ); |
---|
[2908f08] | 502 | |
---|
| 503 | for ( const Type * t : curTypes ) { |
---|
| 504 | assert( dit != desigAlts.end() ); |
---|
| 505 | DesignatorChain & d = *dit; |
---|
[da5ac2e] | 506 | // Name Designation: |
---|
| 507 | if ( nexpr ) { |
---|
[2908f08] | 508 | PRINT( std::cerr << "____actual: " << t << std::endl; ) |
---|
| 509 | if ( auto refType = dynamic_cast< const BaseInstType * >( t ) ) { |
---|
| 510 | // concatenate identical field names |
---|
| 511 | for ( const Decl * mem : refType->lookup( nexpr->name ) ) { |
---|
| 512 | if ( auto field = dynamic_cast< const ObjectDecl * >( mem ) ) { |
---|
| 513 | PRINT( std::cerr << "____alt: " << field->type << std::endl; ) |
---|
| 514 | DesignatorChain d2 = d; |
---|
| 515 | d2.emplace_back( new VariableExpr{ expr->location, field } ); |
---|
| 516 | newDesigAlts.emplace_back( std::move( d2 ) ); |
---|
| 517 | newTypes.emplace_back( field->type ); |
---|
[2d11663] | 518 | } |
---|
[2908f08] | 519 | } |
---|
| 520 | } |
---|
[2d11663] | 521 | |
---|
[2908f08] | 522 | ++dit; |
---|
[da5ac2e] | 523 | // Index Designation: |
---|
[2908f08] | 524 | } else { |
---|
| 525 | if ( auto at = dynamic_cast< const ArrayType * >( t ) ) { |
---|
| 526 | PRINT( std::cerr << "____alt: " << at->get_base() << std::endl; ) |
---|
| 527 | d.emplace_back( expr ); |
---|
| 528 | newDesigAlts.emplace_back( d ); |
---|
| 529 | newTypes.emplace_back( at->base ); |
---|
[2d11663] | 530 | } |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | |
---|
[2908f08] | 534 | // reset queue |
---|
| 535 | desigAlts = std::move( newDesigAlts ); |
---|
| 536 | newDesigAlts.clear(); |
---|
| 537 | curTypes = std::move( newTypes ); |
---|
| 538 | newTypes.clear(); |
---|
| 539 | assertf( desigAlts.size() == curTypes.size(), "Designator alternatives (%zu) and current types (%zu) out of sync", desigAlts.size(), curTypes.size() ); |
---|
[2d11663] | 540 | } |
---|
| 541 | |
---|
[2908f08] | 542 | if ( desigAlts.size() > 1 ) { |
---|
| 543 | SemanticError( designation, toString("Too many alternatives (", desigAlts.size(), ") for designation: ") ); |
---|
| 544 | } else if ( desigAlts.empty() ) { |
---|
| 545 | SemanticError( designation, "No reasonable alternatives for designation: " ); |
---|
[60aaa51d] | 546 | } |
---|
| 547 | |
---|
[2908f08] | 548 | DesignatorChain & d = desigAlts.back(); |
---|
| 549 | PRINT( for ( Expression * expr : d ) { |
---|
| 550 | std::cerr << "____desig: " << expr << std::endl; |
---|
| 551 | } ) // for |
---|
| 552 | assertf( ! curTypes.empty(), "empty designator chosen"); |
---|
| 553 | |
---|
| 554 | // set new designators |
---|
| 555 | assertf( ! objStack.empty(), "empty object stack when setting designation" ); |
---|
| 556 | Designation * actualDesignation = |
---|
| 557 | new Designation{ designation->location, DesignatorChain{d} }; |
---|
| 558 | objStack.back()->setPosition( d ); // destroys d |
---|
| 559 | return actualDesignation; |
---|
| 560 | } |
---|
[60aaa51d] | 561 | |
---|
[2908f08] | 562 | void CurrentObject::setNext( const Designation * designation ) { |
---|
| 563 | PRINT( std::cerr << "____setNext" << designation << std::endl; ) |
---|
| 564 | assertf( ! objStack.empty(), "obj stack empty in setNext" ); |
---|
| 565 | objStack.back()->setPosition( designation->designators ); |
---|
| 566 | } |
---|
[60aaa51d] | 567 | |
---|
[2908f08] | 568 | void CurrentObject::increment() { |
---|
| 569 | PRINT( std::cerr << "____increment" << std::endl; ) |
---|
| 570 | if ( objStack.empty() ) return; |
---|
| 571 | PRINT( std::cerr << *objStack.back() << std::endl; ) |
---|
| 572 | objStack.back()->smallStep(); |
---|
| 573 | } |
---|
[60aaa51d] | 574 | |
---|
[2908f08] | 575 | void CurrentObject::enterListInit( const CodeLocation & loc ) { |
---|
| 576 | PRINT( std::cerr << "____entering list init" << std::endl; ) |
---|
| 577 | assertf( ! objStack.empty(), "empty obj stack entering list init" ); |
---|
| 578 | const ast::Type * type = objStack.back()->getNext(); |
---|
| 579 | assert( type ); |
---|
| 580 | objStack.emplace_back( createMemberIterator( loc, type ) ); |
---|
| 581 | } |
---|
[60aaa51d] | 582 | |
---|
[2908f08] | 583 | void CurrentObject::exitListInit() { |
---|
| 584 | PRINT( std::cerr << "____exiting list init" << std::endl; ) |
---|
| 585 | assertf( ! objStack.empty(), "objstack empty" ); |
---|
| 586 | objStack.pop_back(); |
---|
| 587 | if ( ! objStack.empty() ) { |
---|
| 588 | PRINT( std::cerr << *objStack.back() << std::endl; ) |
---|
| 589 | objStack.back()->bigStep(); |
---|
[60aaa51d] | 590 | } |
---|
[b7d92b96] | 591 | } |
---|
| 592 | |
---|
[2908f08] | 593 | std::deque< InitAlternative > CurrentObject::getOptions() { |
---|
| 594 | PRINT( std::cerr << "____getting current options" << std::endl; ) |
---|
| 595 | assertf( ! objStack.empty(), "objstack empty in getOptions" ); |
---|
| 596 | return **objStack.back(); |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | const Type * CurrentObject::getCurrentType() { |
---|
| 600 | PRINT( std::cerr << "____getting current type" << std::endl; ) |
---|
| 601 | assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); |
---|
| 602 | return objStack.back()->getNext(); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | } // namespace ast |
---|
| 606 | |
---|
[e4d829b] | 607 | // Local Variables: // |
---|
| 608 | // tab-width: 4 // |
---|
| 609 | // mode: c++ // |
---|
| 610 | // compile-command: "make install" // |
---|
| 611 | // End: // |
---|