Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    rb32ada31 r615a096  
    1212// Last Modified By :
    1313// Last Modified On :
    14 // Update Count     : 1
     14// Update Count     : 3
    1515//
    1616
    1717#include "Concurrency/Keywords.h"
    1818
    19 #include "SymTab/AddVisit.h"
    2019#include "SynTree/Declaration.h"
    2120#include "SynTree/Expression.h"
     
    3029        namespace {
    3130                const std::list<Label> noLabels;
    32                 const std::list< Attribute * > noAttributes;
    3331                Type::StorageClasses noStorage;
    3432                Type::Qualifiers noQualifiers;
     
    6563        //                                           void main( MyCoroutine * this );
    6664        //
    67         class CoroutineKeyword final : public Visitor {
    68             template< typename Visitor >
    69             friend void SymTab::acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor );
    70           public:
    71 
    72                 using Visitor::visit;
    73                 virtual void visit( StructDecl * decl ) override final;
    74 
    75                 void handle( StructDecl * );
    76                 Declaration * addField( StructDecl * );
    77                 void addRoutines( StructDecl *, Declaration * );
    78 
    79                 static void implement( std::list< Declaration * > & translationUnit ) {
    80                         CoroutineKeyword impl;
    81                         SymTab::acceptAndAdd( translationUnit, impl );
    82                 }
    83 
    84           private:
    85                 std::list< Declaration * > declsToAdd, declsToAddAfter;
    86                 StructDecl* coroutine_decl = nullptr;
     65        class CoroutineKeyword final : public Mutator {
     66          public:
     67
     68                static void implement( std::list< Declaration * > & translationUnit ) {}
    8769        };
    8870
     
    11597
    11698                using Visitor::visit;
    117                 virtual void visit( FunctionDecl * decl ) override final;
    118                 virtual void visit(   StructDecl * decl ) override final;
     99                virtual void visit( FunctionDecl *functionDecl ) override final;
     100                virtual void visit(   StructDecl *functionDecl ) override final;
    119101
    120102                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* );
     
    129111          private:
    130112                StructDecl* monitor_decl = nullptr;
    131                 StructDecl* guard_decl = nullptr;
    132113        };
    133114
     
    143124
    144125        //=============================================================================================
    145         // Coroutine keyword implementation
    146         //=============================================================================================
    147         void CoroutineKeyword::visit(StructDecl * decl) {
    148                 if( decl->get_name() == "coroutine_desc" ) {
    149                         assert( !coroutine_decl );
    150                         coroutine_decl = decl;
    151                 }
    152                 else if ( false ) {
    153                         handle( decl );
    154                 }
    155 
    156         }
    157 
    158         void CoroutineKeyword::handle( StructDecl * decl ) {
    159                 if( ! decl->has_body() ) return;
    160 
    161                 if( !coroutine_decl ) throw SemanticError( "coroutine keyword requires coroutines to be in scope, add #include <coroutine>", decl );
    162 
    163                 Declaration * field = addField( decl );
    164                 addRoutines( decl, field );
    165         }
    166 
    167         Declaration * CoroutineKeyword::addField( StructDecl * decl ) {
    168                 Declaration * cor = new ObjectDecl(
    169                         "__cor",
    170                         noStorage,
    171                         LinkageSpec::Cforall,
    172                         nullptr,
    173                         new StructInstType(
    174                                 noQualifiers,
    175                                 coroutine_decl
    176                         ),
    177                         nullptr
    178                 );
    179 
    180                 decl->get_members().push_front( cor );
    181 
    182                 return cor;
    183         }
    184 
    185         void CoroutineKeyword::addRoutines( StructDecl * decl, Declaration * field ) {
    186                 FunctionType * type = new FunctionType( noQualifiers, false );
    187                 type->get_parameters().push_back(
    188                         new ObjectDecl(
    189                                 "this",
    190                                 noStorage,
    191                                 LinkageSpec::Cforall,
    192                                 nullptr,
    193                                 new PointerType(
    194                                         noQualifiers,
    195                                         new StructInstType(
    196                                                 noQualifiers,
    197                                                 decl
    198                                         )
    199                                 ),
    200                                 nullptr
    201                         )
    202                 );
    203 
    204                 CompoundStmt * statement = new CompoundStmt( noLabels );
    205                 statement->push_back(
    206                         new ReturnStmt(
    207                                 noLabels,
    208                                 new UntypedMemberExpr(
    209                                         new NameExpr( "__cor" ),
    210                                         new NameExpr( "this" )
    211                                 )
    212                         )
    213                 );
    214 
    215                 declsToAddAfter.push_back(
    216                         new FunctionDecl(
    217                                 "get_coroutine",
    218                                 Type::Static,
    219                                 LinkageSpec::Cforall,
    220                                 type,
    221                                 statement,
    222                                 noAttributes,
    223                                 Type::Inline
    224                         )
    225                 );
    226         }
    227        
    228 
    229         //=============================================================================================
    230126        // Mutex keyword implementation
    231127        //=============================================================================================
     
    241137                if( ! body ) return;
    242138
    243                 if( !monitor_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
    244                 if( !guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
    245 
     139                assert(monitor_decl);
    246140                addStatments( body, mutexArgs );
    247141        }
     
    252146                        monitor_decl = decl;
    253147                }
    254                 else if( decl->get_name() == "monitor_guard_t" ) {
    255                         assert( !guard_decl );
    256                         guard_decl = decl;
    257                 }
    258148        }
    259149
     
    264154                        //Find mutex arguments
    265155                        Type* ty = arg->get_type();
    266                         if( ! ty->get_qualifiers().isMutex ) continue;
     156                        if( ! ty->get_mutex() ) continue;
    267157
    268158                        //Append it to the list
     
    285175
    286176                //Make sure that typed isn't mutex
    287                 if( base->get_qualifiers().isMutex ) throw SemanticError( "mutex keyword may only appear once per argument ", arg );
     177                if( ! base->get_mutex() ) throw SemanticError( "mutex keyword may only appear once per argument ", arg );
    288178        }
    289179
    290180        void MutexKeyword::addStatments( CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
     181
    291182                ObjectDecl * monitors = new ObjectDecl(
    292183                        "__monitors",
     
    327218                                new StructInstType(
    328219                                        noQualifiers,
    329                                         guard_decl
     220                                        "monitor_guard_t"
    330221                                ),
    331222                                new ListInit(
     
    333224                                                new SingleInit( new VariableExpr( monitors ) ),
    334225                                                new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) )
    335                                         },
    336                                         noDesignators,
    337                                         true
     226                                        }
    338227                                )
    339228                        ))
    340229                );
    341230
    342                 //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
     231                //monitor_desc * __monitors[] = { a, b };
    343232                body->push_front( new DeclStmt( noLabels, monitors) );
    344233        }
Note: See TracChangeset for help on using the changeset viewer.