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