Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    rceedde6 r42107b4  
    1919#include <string>                  // for string, operator==
    2020
     21#include "Common/GC.h"             // for new_static_root
    2122#include "Common/PassVisitor.h"    // for PassVisitor
    2223#include "Common/SemanticError.h"  // for SemanticError
     
    191192                void postvisit(   StructDecl * decl );
    192193
    193                 std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
     194                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* );
    194195                void validate( DeclarationWithType * );
    195196                void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
     
    206207                StructDecl* dtor_guard_decl = nullptr;
    207208
    208                 static std::unique_ptr< Type > generic_func;
     209                static Type* generic_func;
    209210        };
    210211
    211         std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
    212                 new FunctionType(
    213                         noQualifiers,
    214                         true
    215                 )
    216         );
     212        Type* MutexKeyword::generic_func = new_static_root<FunctionType>( noQualifiers, true );
    217213
    218214        //-----------------------------------------------------------------------------
     
    288284                        // convert (thread &)t to (thread_desc &)*get_thread(t), etc.
    289285                        if( !type_decl ) SemanticError( cast, context_error );
    290                         Expression * arg = cast->arg;
    291                         cast->arg = nullptr;
    292                         delete cast;
    293286                        return new CastExpr(
    294287                                UntypedExpr::createDeref(
    295                                         new UntypedExpr( new NameExpr( getter_name ), { arg } )
     288                                        new UntypedExpr( new NameExpr( getter_name ), { cast->arg } )
    296289                                ),
    297290                                new ReferenceType(
     
    318311                StructDecl * forward = decl->clone();
    319312                forward->set_body( false );
    320                 deleteAll( forward->get_members() );
    321313                forward->get_members().clear();
    322314
     
    383375                }
    384376
    385                 delete this_decl;
    386 
    387377                declsToAddBefore.push_back( forward );
    388378                if( needs_main ) declsToAddBefore.push_back( main_decl );
     
    441431        void MutexKeyword::postvisit(FunctionDecl* decl) {
    442432
    443                 bool first = false;
    444                 std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
     433                std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl );
     434                if( mutexArgs.empty() ) return;
     435
     436                if( CodeGen::isConstructor(decl->name) ) SemanticError( decl, "constructors cannot have mutex parameters" );
     437
    445438                bool isDtor = CodeGen::isDestructor( decl->name );
    446439
    447                 // Is this function relevant to monitors
    448                 if( mutexArgs.empty() ) {
    449                         // If this is the destructor for a monitor it must be mutex
    450                         if(isDtor) {
    451                                 Type* ty = decl->get_functionType()->get_parameters().front()->get_type();
    452 
    453                                 // If it's a copy, it's not a mutex
    454                                 ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
    455                                 if( ! rty ) return;
    456 
    457                                 // If we are not pointing directly to a type, it's not a mutex
    458                                 Type* base = rty->get_base();
    459                                 if( dynamic_cast< ReferenceType * >( base ) ) return;
    460                                 if( dynamic_cast< PointerType * >( base ) ) return;
    461 
    462                                 // Check if its a struct
    463                                 StructInstType * baseStruct = dynamic_cast< StructInstType * >( base );
    464                                 if( !baseStruct ) return;
    465 
    466                                 // Check if its a monitor
    467                                 if(baseStruct->baseStruct->is_monitor() || baseStruct->baseStruct->is_thread())
    468                                         SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters\n" );
    469                         }
    470                         return;
    471                 }
    472 
    473                 // Monitors can't be constructed with mutual exclusion
    474                 if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" );
    475 
    476                 // It makes no sense to have multiple mutex parameters for the destructor
    477440                if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" );
    478441
    479                 // Make sure all the mutex arguments are monitors
    480442                for(auto arg : mutexArgs) {
    481443                        validate( arg );
    482444                }
    483445
    484                 // Check if we need to instrument the body
    485446                CompoundStmt* body = decl->get_statements();
    486447                if( ! body ) return;
    487448
    488                 // Do we have the required headers
    489449                if( !monitor_decl || !guard_decl || !dtor_guard_decl )
    490                         SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>\n" );
    491 
    492                 // Instrument the body
     450                        SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>" );
     451
    493452                if( isDtor ) {
    494453                        addDtorStatments( decl, body, mutexArgs );
     
    515474        }
    516475
    517         std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) {
     476        std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl ) {
    518477                std::list<DeclarationWithType*> mutexArgs;
    519478
    520                 bool once = true;
    521479                for( auto arg : decl->get_functionType()->get_parameters()) {
    522480                        //Find mutex arguments
    523481                        Type* ty = arg->get_type();
    524482                        if( ! ty->get_mutex() ) continue;
    525 
    526                         if(once) {first = true;}
    527                         once = false;
    528483
    529484                        //Append it to the list
Note: See TracChangeset for help on using the changeset viewer.