Changes in src/Concurrency/Keywords.cc [77a2994:b81fd95]
- File:
-
- 1 edited
-
src/Concurrency/Keywords.cc (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Concurrency/Keywords.cc
r77a2994 rb81fd95 19 19 #include <string> // for string, operator== 20 20 21 #include <iostream>22 23 #include "Common/Examine.h" // for isMainFor24 21 #include "Common/PassVisitor.h" // for PassVisitor 25 22 #include "Common/SemanticError.h" // for SemanticError … … 37 34 #include "SynTree/Type.h" // for StructInstType, Type, PointerType 38 35 #include "SynTree/Visitor.h" // for Visitor, acceptAll 39 #include "Virtual/Tables.h"40 36 41 37 class Attribute; 42 38 43 39 namespace Concurrency { 44 inline static std::string getVTableName( std::string const & exception_name ) {45 return exception_name.empty() ? std::string() : Virtual::vtableTypeName(exception_name);46 }47 48 40 //============================================================================================= 49 41 // Pass declarations … … 62 54 public: 63 55 64 ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, 65 std::string&& getter_name, std::string&& context_error, std::string&& exception_name, 66 bool needs_main, AggregateDecl::Aggregate cast_target ) : 67 type_name( type_name ), field_name( field_name ), getter_name( getter_name ), 68 context_error( context_error ), vtable_name( getVTableName( exception_name ) ), 69 needs_main( needs_main ), cast_target( cast_target ) {} 56 ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main, AggregateDecl::Aggregate cast_target ) : 57 type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ), cast_target( cast_target ) {} 70 58 71 59 virtual ~ConcurrentSueKeyword() {} … … 75 63 76 64 void handle( StructDecl * ); 77 void addVtableForward( StructDecl * );78 65 FunctionDecl * forwardDeclare( StructDecl * ); 79 66 ObjectDecl * addField( StructDecl * ); … … 89 76 const std::string getter_name; 90 77 const std::string context_error; 91 const std::string vtable_name;92 78 bool needs_main; 93 79 AggregateDecl::Aggregate cast_target; … … 95 81 StructDecl * type_decl = nullptr; 96 82 FunctionDecl * dtor_decl = nullptr; 97 StructDecl * vtable_decl = nullptr;98 83 }; 99 84 … … 116 101 "get_thread", 117 102 "thread keyword requires threads to be in scope, add #include <thread.hfa>\n", 118 "",119 103 true, 120 104 AggregateDecl::Thread … … 149 133 "get_coroutine", 150 134 "coroutine keyword requires coroutines to be in scope, add #include <coroutine.hfa>\n", 151 "CoroutineCancelled",152 135 true, 153 136 AggregateDecl::Coroutine … … 184 167 "get_monitor", 185 168 "monitor keyword requires monitors to be in scope, add #include <monitor.hfa>\n", 186 "",187 169 false, 188 170 AggregateDecl::Monitor … … 216 198 "get_generator", 217 199 "Unable to find builtin type $generator\n", 218 "",219 200 true, 220 201 AggregateDecl::Generator … … 250 231 251 232 private: 233 DeclarationWithType * is_main( FunctionDecl * ); 252 234 bool is_real_suspend( FunctionDecl * ); 253 235 … … 377 359 handle( decl ); 378 360 } 379 else if ( !vtable_decl && vtable_name == decl->name && decl->body ) {380 vtable_decl = decl;381 }382 // Might be able to get ride of is target.383 assert( is_target(decl) == (cast_target == decl->kind) );384 361 return decl; 385 362 } 386 363 387 364 DeclarationWithType * ConcurrentSueKeyword::postmutate( FunctionDecl * decl ) { 388 if ( type_decl && isDestructorFor( decl, type_decl ) ) 389 dtor_decl = decl; 390 else if ( vtable_name.empty() ) 391 ; 392 else if ( auto param = isMainFor( decl, cast_target ) ) { 393 // This should never trigger. 394 assert( vtable_decl ); 395 // Should be safe because of isMainFor. 396 StructInstType * struct_type = static_cast<StructInstType *>( 397 static_cast<ReferenceType *>( param->get_type() )->base ); 398 assert( struct_type ); 399 400 declsToAddAfter.push_back( Virtual::makeVtableInstance( vtable_decl, { 401 new TypeExpr( struct_type->clone() ), 402 }, struct_type, nullptr ) ); 403 } 404 365 if( !type_decl ) return decl; 366 if( !CodeGen::isDestructor( decl->name ) ) return decl; 367 368 auto params = decl->type->parameters; 369 if( params.size() != 1 ) return decl; 370 371 auto type = dynamic_cast<ReferenceType*>( params.front()->get_type() ); 372 if( !type ) return decl; 373 374 auto stype = dynamic_cast<StructInstType*>( type->base ); 375 if( !stype ) return decl; 376 if( stype->baseStruct != type_decl ) return decl; 377 378 if( !dtor_decl ) dtor_decl = decl; 405 379 return decl; 406 380 } … … 426 400 if( !dtor_decl ) SemanticError( decl, context_error ); 427 401 428 addVtableForward( decl );429 402 FunctionDecl * func = forwardDeclare( decl ); 430 403 ObjectDecl * field = addField( decl ); 431 404 addRoutines( field, func ); 432 }433 434 void ConcurrentSueKeyword::addVtableForward( StructDecl * decl ) {435 if ( vtable_decl ) {436 declsToAddBefore.push_back( Virtual::makeVtableForward( vtable_decl, {437 new TypeExpr( new StructInstType( noQualifiers, decl ) ),438 } ) );439 // Its only an error if we want a vtable and don't have one.440 } else if ( ! vtable_name.empty() ) {441 SemanticError( decl, context_error );442 }443 405 } 444 406 … … 566 528 // Suspend keyword implementation 567 529 //============================================================================================= 530 DeclarationWithType * SuspendKeyword::is_main( FunctionDecl * func) { 531 if(func->name != "main") return nullptr; 532 if(func->type->parameters.size() != 1) return nullptr; 533 534 auto param = func->type->parameters.front(); 535 536 auto type = dynamic_cast<ReferenceType * >(param->get_type()); 537 if(!type) return nullptr; 538 539 auto obj = dynamic_cast<StructInstType *>(type->base); 540 if(!obj) return nullptr; 541 542 if(!obj->baseStruct->is_generator()) return nullptr; 543 544 return param; 545 } 546 568 547 bool SuspendKeyword::is_real_suspend( FunctionDecl * func ) { 569 548 if(isMangled(func->linkage)) return false; // the real suspend isn't mangled … … 586 565 587 566 // Is this the main of a generator? 588 auto param = is MainFor( func, AggregateDecl::Aggregate::Generator);567 auto param = is_main( func ); 589 568 if(!param) return; 590 569 … … 931 910 { 932 911 new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ), 933 new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) ), 934 new SingleInit( new ConstantExpr( Constant::from_bool( false ) ) ) 912 new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) ) 935 913 }, 936 914 noDesignators, … … 1055 1033 // tab-width: 4 // 1056 1034 // End: // 1057
Note:
See TracChangeset
for help on using the changeset viewer.