Changeset 62423350 for src/ResolvExpr/CurrentObject.cc
- Timestamp:
- Jun 29, 2017, 5:06:24 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- a12d5aa
- Parents:
- bb1cd95
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/CurrentObject.cc
rbb1cd95 r62423350 22 22 #include "SynTree/Initializer.h" 23 23 #include "SynTree/Type.h" 24 #include "SynTree/TypeSubstitution.h" 24 25 25 26 #if 0 … … 32 33 long long int getConstValue( ConstantExpr * constExpr ) { 33 34 if ( BasicType * basicType = dynamic_cast< BasicType * >( constExpr->get_result() ) ) { 34 if ( basicType->get_kind() == BasicType::Char || basicType->get_kind() == BasicType::SignedChar || basicType->get_kind() == BasicType::UnsignedChar ) { 35 assertf( constExpr->get_constant()->get_value().size() == 3, "constant value with unusual length: %s", constExpr->get_constant()->get_value().c_str() ); 36 return constExpr->get_constant()->get_value()[1]; 35 if ( basicType->isInteger() ) { 36 return constExpr->get_constant()->get_ival(); 37 37 } else { 38 return stoll( constExpr->get_constant()->get_value() ); 39 } 38 assertf( false, "Non-integer constant expression in getConstValue", toString( constExpr ).c_str() ); // xxx - might be semantic error 39 } 40 } else if ( dynamic_cast< OneType * >( constExpr->get_result() ) ) { 41 return 1; 42 } else if ( dynamic_cast< ZeroType * >( constExpr->get_result() ) ) { 43 return 0; 40 44 } else { 41 assertf( false, "unhandled type on getConstValue %s", constExpr->get_result() );45 assertf( false, "unhandled type on getConstValue %s", toString( constExpr->get_result() ).c_str() ); // xxx - might be semantic error 42 46 } 43 47 } … … 56 60 std::ostream & operator<<( std::ostream & out, Indenter & indent ) { 57 61 return out << std::string(indent.indent, ' '); 62 } 63 64 template< typename AggrInst > 65 TypeSubstitution makeGenericSubstitution( AggrInst * inst ) { 66 assert( inst ); 67 assert( inst->get_baseParameters() ); 68 std::list< TypeDecl * > baseParams = *inst->get_baseParameters(); 69 std::list< Expression * > typeSubs = inst->get_parameters(); 70 TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); 71 return subs; 72 } 73 74 TypeSubstitution makeGenericSubstitution( Type * type ) { 75 if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { 76 return makeGenericSubstitution( inst ); 77 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { 78 return makeGenericSubstitution( inst ); 79 } else { 80 return TypeSubstitution(); 81 } 58 82 } 59 83 … … 135 159 void setSize( Expression * expr ) { 136 160 if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { 137 size = getConstValue( constExpr ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this161 size = getConstValue( constExpr ); 138 162 PRINT( std::cerr << "array type with size: " << size << std::endl; ) 139 163 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { 140 setSize( castExpr->get_arg() ); 164 setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast 141 165 } else { 142 assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() ); 166 assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this 143 167 } 144 168 } … … 231 255 class AggregateIterator : public MemberIterator { 232 256 public: 233 typedef std::list<Declaration *>::iterator iterator; 234 const char * kind = ""; // for debug 235 ReferenceToType * inst = nullptr; 236 AggregateDecl * decl = nullptr; 257 typedef std::list<Declaration *> MemberList; 258 typedef MemberList::const_iterator iterator; 259 std::string kind = ""; // for debug 260 std::string name; 261 Type * inst = nullptr; 262 const MemberList & members; 237 263 iterator curMember; 238 bool atbegin = true; // false at first {small,big}Step -- this structtype is only added to the possibilities at the beginning264 bool atbegin = true; // false at first {small,big}Step -- this aggr type is only added to the possibilities at the beginning 239 265 Type * curType = nullptr; 240 266 MemberIterator * memberIter = nullptr; 241 242 AggregateIterator( const char * kind, ReferenceToType * inst, AggregateDecl * decl ) : kind( kind ), inst( inst ), decl( decl ), curMember( decl->get_members().begin() ) { 267 mutable TypeSubstitution sub; 268 269 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 ) ) { 243 270 init(); 244 271 } … … 249 276 250 277 bool init() { 251 PRINT( std::cerr << "--init()--" << std::endl; )252 if ( curMember != decl->get_members().end() ) {278 PRINT( std::cerr << "--init()--" << members.size() << std::endl; ) 279 if ( curMember != members.end() ) { 253 280 if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( *curMember ) ) { 254 281 PRINT( std::cerr << "incremented to field: " << field << std::endl; ) … … 264 291 if (memberIter && *memberIter) { 265 292 std::list<InitAlternative> ret = memberIter->first(); 293 PRINT( std::cerr << "sub: " << sub << std::endl; ) 266 294 for ( InitAlternative & alt : ret ) { 267 295 PRINT( std::cerr << "iterating and adding designators" << std::endl; ) 268 296 alt.designation->get_designators().push_front( new VariableExpr( safe_dynamic_cast< ObjectDecl * >( *curMember ) ) ); 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; ) 269 301 } 270 302 return ret; … … 276 308 if ( ! designators.empty() ) { 277 309 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( designators.front() ) ) { 278 for ( curMember = decl->get_members().begin(); curMember != decl->get_members().end(); ++curMember ) {310 for ( curMember = members.begin(); curMember != members.end(); ++curMember ) { 279 311 if ( *curMember == varExpr->get_var() ) { 280 312 designators.pop_front(); … … 282 314 memberIter = createMemberIterator( varExpr->get_result() ); 283 315 curType = varExpr->get_result(); 284 atbegin = curMember == decl->get_members().begin() && designators.empty(); // xxx - is this right??316 atbegin = curMember == members.begin() && designators.empty(); // xxx - is this the right condition for atbegin?? 285 317 memberIter->setPosition( designators ); 286 318 return; 287 319 } // if 288 320 } // for 289 assertf( false, "could not find member in %s: %s", kind , toString( varExpr ).c_str() );321 assertf( false, "could not find member in %s: %s", kind.c_str(), toString( varExpr ).c_str() ); 290 322 } else { 291 assertf( false, "bad designator given to %s: %s", kind , toString( designators.front() ).c_str() );323 assertf( false, "bad designator given to %s: %s", kind.c_str(), toString( designators.front() ).c_str() ); 292 324 } // if 293 325 } // if … … 336 368 337 369 virtual void print( std::ostream & out, Indenter indent ) const { 338 out << kind << "(" << decl->get_name()<< ")";370 out << kind << "(" << name << ")"; 339 371 if ( memberIter ) { 340 372 Indenter childIndent = indent+1; … … 347 379 class UnionIterator : public AggregateIterator { 348 380 public: 349 UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst , inst->get_baseUnion() ) {}381 UnionIterator( UnionInstType * inst ) : AggregateIterator( "UnionIterator", inst->get_name(), inst, inst->get_baseUnion()->get_members() ) {} 350 382 351 383 virtual operator bool() const { return (memberIter && *memberIter); } … … 357 389 memberIter = nullptr; 358 390 curType = nullptr; 359 curMember = decl->get_members().end();391 curMember = members.end(); 360 392 return *this; 361 393 } … … 365 397 class StructIterator : public AggregateIterator { 366 398 public: 367 StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst , inst->get_baseStruct() ) {}368 369 virtual operator bool() const { return curMember != decl->get_members().end() || (memberIter && *memberIter); }399 StructIterator( StructInstType * inst ) : AggregateIterator( "StructIterator", inst->get_name(), inst, inst->get_baseStruct()->get_members() ) {} 400 401 virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } 370 402 371 403 virtual MemberIterator & bigStep() { … … 375 407 memberIter = nullptr; 376 408 curType = nullptr; 377 for ( ; curMember != decl->get_members().end(); ) { 409 for ( ; curMember != members.end(); ) { 410 ++curMember; 411 if ( init() ) { 412 return *this; 413 } 414 } 415 return *this; 416 } 417 }; 418 419 class TupleIterator : public AggregateIterator { 420 public: 421 TupleIterator( TupleType * inst ) : AggregateIterator( "TupleIterator", toString("Tuple", inst->size()), inst, inst->get_members() ) {} 422 423 virtual operator bool() const { return curMember != members.end() || (memberIter && *memberIter); } 424 425 virtual MemberIterator & bigStep() { 426 PRINT( std::cerr << "bigStep in " << kind << std::endl; ) 427 atbegin = false; 428 delete memberIter; 429 memberIter = nullptr; 430 curType = nullptr; 431 for ( ; curMember != members.end(); ) { 378 432 ++curMember; 379 433 if ( init() ) { … … 392 446 return new UnionIterator( uit ); 393 447 } else { 394 assertf( false, "some other reftotype" ); 448 assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" ); 449 return new SimpleIterator( type ); 395 450 } 396 451 } else if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { 397 452 return new ArrayIterator( at ); 453 } else if ( TupleType * tt = dynamic_cast< TupleType * >( type ) ) { 454 return new TupleIterator( tt ); 398 455 } else { 399 456 return new SimpleIterator( type ); … … 494 551 Type * type = objStack.top()->getNext(); 495 552 if ( type ) { 496 // xxx - record types.front()?497 553 objStack.push( createMemberIterator( type ) ); 498 554 } else { … … 517 573 return **objStack.top(); 518 574 } 575 576 Type * CurrentObject::getCurrentType() { 577 PRINT( std::cerr << "____getting current type" << std::endl; ) 578 assertf( ! objStack.empty(), "objstack empty in getCurrentType" ); 579 return objStack.top()->getNext(); 580 } 519 581 } // namespace ResolvExpr 520 582
Note: See TracChangeset
for help on using the changeset viewer.