Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    r2065609 rbe9288a  
    1919#include <string>                  // for string, operator==
    2020
    21 #include "Common/PassVisitor.h"    // for PassVisitor
    2221#include "Common/SemanticError.h"  // for SemanticError
    2322#include "Common/utility.h"        // for deleteAll, map_range
    24 #include "CodeGen/OperatorTable.h" // for isConstructor
    25 #include "InitTweak/InitTweak.h"   // for getPointerBase
     23#include "InitTweak/InitTweak.h"   // for isConstructor
    2624#include "Parser/LinkageSpec.h"    // for Cforall
    2725#include "SymTab/AddVisit.h"       // for acceptAndAdd
     
    4745
    4846        //=============================================================================================
    49         // Pass declarations
     47        // Visitors declaration
    5048        //=============================================================================================
    5149
     
    5957        //                                           static inline NewField_t * getter_name( MyType * this ) { return &this->newField; }
    6058        //
    61         class ConcurrentSueKeyword : public WithDeclsToAdd {
     59        class ConcurrentSueKeyword : public Visitor {
     60          protected:
     61            template< typename Visitor >
     62            friend void SymTab::acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor );
    6263          public:
    6364
     
    6768                virtual ~ConcurrentSueKeyword() {}
    6869
    69                 void postvisit( StructDecl * decl );
     70                using Visitor::visit;
     71                virtual void visit( StructDecl * decl ) override final;
    7072
    7173                void handle( StructDecl * );
     
    8385                bool needs_main;
    8486
     87                std::list< Declaration * > declsToAdd, declsToAddAfter;
    8588                StructDecl* type_decl = nullptr;
    8689        };
     
    113116
    114117                static void implement( std::list< Declaration * > & translationUnit ) {
    115                         PassVisitor< ThreadKeyword > impl;
    116                         acceptAll( translationUnit, impl );
     118                        ThreadKeyword impl;
     119                        SymTab::acceptAndAdd( translationUnit, impl );
    117120                }
    118121        };
     
    144147
    145148                static void implement( std::list< Declaration * > & translationUnit ) {
    146                         PassVisitor< CoroutineKeyword > impl;
    147                         acceptAll( translationUnit, impl );
     149                        CoroutineKeyword impl;
     150                        SymTab::acceptAndAdd( translationUnit, impl );
    148151                }
    149152        };
     
    175178
    176179                static void implement( std::list< Declaration * > & translationUnit ) {
    177                         PassVisitor< MonitorKeyword > impl;
    178                         acceptAll( translationUnit, impl );
     180                        MonitorKeyword impl;
     181                        SymTab::acceptAndAdd( translationUnit, impl );
    179182                }
    180183        };
     
    188191        // }                                                               }
    189192        //
    190         class MutexKeyword final {
     193        class MutexKeyword final : public Visitor {
    191194          public:
    192195
    193                 void postvisit( FunctionDecl * decl );
    194                 void postvisit(   StructDecl * decl );
     196                using Visitor::visit;
     197                virtual void visit( FunctionDecl * decl ) override final;
     198                virtual void visit(   StructDecl * decl ) override final;
    195199
    196200                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* );
    197201                void validate( DeclarationWithType * );
    198                 void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
     202                void addStatments( CompoundStmt *, const std::list<DeclarationWithType * > &);
    199203
    200204                static void implement( std::list< Declaration * > & translationUnit ) {
    201                         PassVisitor< MutexKeyword > impl;
     205                        MutexKeyword impl;
    202206                        acceptAll( translationUnit, impl );
    203207                }
     
    206210                StructDecl* monitor_decl = nullptr;
    207211                StructDecl* guard_decl = nullptr;
    208 
    209                 static std::unique_ptr< Type > generic_func;
    210212        };
    211 
    212         std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
    213                 new FunctionType(
    214                         noQualifiers,
    215                         true
    216                 )
    217         );
    218213
    219214        //-----------------------------------------------------------------------------
     
    225220        // }                                                               }
    226221        //
    227         class ThreadStarter final {
     222        class ThreadStarter final : public Visitor {
    228223          public:
    229224
    230                 void postvisit( FunctionDecl * decl );
     225                using Visitor::visit;
     226                virtual void visit( FunctionDecl * decl ) override final;
    231227
    232228                void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
    233229
    234230                static void implement( std::list< Declaration * > & translationUnit ) {
    235                         PassVisitor< ThreadStarter > impl;
     231                        ThreadStarter impl;
    236232                        acceptAll( translationUnit, impl );
    237233                }
     
    258254        // Generic keyword implementation
    259255        //=============================================================================================
    260         void ConcurrentSueKeyword::postvisit(StructDecl * decl) {
     256        void ConcurrentSueKeyword::visit(StructDecl * decl) {
     257                Visitor::visit(decl);
    261258                if( decl->get_name() == type_name && decl->has_body() ) {
    262259                        assert( !type_decl );
     
    292289                        LinkageSpec::Cforall,
    293290                        nullptr,
    294                         new ReferenceType(
     291                        new PointerType(
    295292                                noQualifiers,
    296293                                new StructInstType(
     
    346343                }
    347344
    348                 declsToAddBefore.push_back( forward );
    349                 if( needs_main ) declsToAddBefore.push_back( main_decl );
    350                 declsToAddBefore.push_back( get_decl );
     345                declsToAdd.push_back( forward );
     346                if( needs_main ) declsToAdd.push_back( main_decl );
     347                declsToAdd.push_back( get_decl );
    351348
    352349                return get_decl;
     
    397394        // Mutex keyword implementation
    398395        //=============================================================================================
    399 
    400         void MutexKeyword::postvisit(FunctionDecl* decl) {
     396        void MutexKeyword::visit(FunctionDecl* decl) {
     397                Visitor::visit(decl);
    401398
    402399                std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl );
     
    413410                if( !guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
    414411
    415                 addStatments( decl, body, mutexArgs );
    416         }
    417 
    418         void MutexKeyword::postvisit(StructDecl* decl) {
     412                addStatments( body, mutexArgs );
     413        }
     414
     415        void MutexKeyword::visit(StructDecl* decl) {
     416                Visitor::visit(decl);
    419417
    420418                if( decl->get_name() == "monitor_desc" ) {
     
    447445
    448446                //Makes sure it's not a copy
    449                 ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
    450                 if( ! rty ) throw SemanticError( "Mutex argument must be of reference type ", arg );
     447                PointerType* pty = dynamic_cast< PointerType * >( ty );
     448                if( ! pty ) throw SemanticError( "Mutex argument must be of pointer/reference type ", arg );
    451449
    452450                //Make sure the we are pointing directly to a type
    453                 Type* base = rty->get_base();
    454                 if( dynamic_cast< ReferenceType * >( base ) ) throw SemanticError( "Mutex argument have exactly one level of indirection ", arg );
    455                 if( dynamic_cast< PointerType * >( base ) ) throw SemanticError( "Mutex argument have exactly one level of indirection ", arg );
     451                Type* base = pty->get_base();
     452                if(  dynamic_cast< PointerType * >( base ) ) throw SemanticError( "Mutex argument have exactly one level of indirection ", arg );
    456453
    457454                //Make sure that typed isn't mutex
     
    459456        }
    460457
    461         void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
     458        void MutexKeyword::addStatments( CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
    462459                ObjectDecl * monitors = new ObjectDecl(
    463460                        "__monitors",
     
    490487                );
    491488
    492                 assert(generic_func);
    493 
    494489                //in reverse order :
    495                 // monitor_guard_t __guard = { __monitors, #, func };
     490                // monitor_guard_t __guard = { __monitors, # };
    496491                body->push_front(
    497492                        new DeclStmt( noLabels, new ObjectDecl(
     
    507502                                        {
    508503                                                new SingleInit( new VariableExpr( monitors ) ),
    509                                                 new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
    510                                                 new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
     504                                                new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) )
    511505                                        },
    512506                                        noDesignators,
     
    523517        // General entry routine
    524518        //=============================================================================================
    525         void ThreadStarter::postvisit(FunctionDecl * decl) {
    526                 if( ! CodeGen::isConstructor(decl->get_name()) ) return;
     519        void ThreadStarter::visit(FunctionDecl * decl) {
     520                Visitor::visit(decl);
     521
     522                if( ! InitTweak::isConstructor(decl->get_name()) ) return;
    527523
    528524                DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
    529                 auto type  = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
     525                auto ptr = dynamic_cast< PointerType * >( param->get_type() );
     526                // if( ptr ) std::cerr << "FRED1" << std::endl;
     527                auto type  = dynamic_cast< StructInstType * >( ptr->get_base() );
    530528                // if( type ) std::cerr << "FRED2" << std::endl;
    531529                if( type && type->get_baseStruct()->is_thread() ) {
Note: See TracChangeset for help on using the changeset viewer.