Changeset 9a705dc8


Ignore:
Timestamp:
Apr 19, 2018, 5:18:46 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
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, with_gc
Children:
da9d79b
Parents:
60ba456
Message:

Implement concurrency keyword casts

Location:
src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    r60ba456 r9a705dc8  
    9292        virtual void visit( NameExpr * nameExpr ) override final;
    9393        virtual void visit( CastExpr * castExpr ) override final;
     94        virtual void visit( KeywordCastExpr * castExpr ) override final;
    9495        virtual void visit( VirtualCastExpr * castExpr ) override final;
    9596        virtual void visit( AddressExpr * addressExpr ) override final;
     
    187188        virtual Expression * mutate( UntypedExpr * untypedExpr ) override final;
    188189        virtual Expression * mutate( NameExpr * nameExpr ) override final;
    189         virtual Expression * mutate( AddressExpr * castExpr ) override final;
     190        virtual Expression * mutate( AddressExpr * addrExpr ) override final;
    190191        virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) override final;
    191192        virtual Expression * mutate( CastExpr * castExpr ) override final;
     193        virtual Expression * mutate( KeywordCastExpr * castExpr ) override final;
    192194        virtual Expression * mutate( VirtualCastExpr * castExpr ) override final;
    193195        virtual Expression * mutate( UntypedMemberExpr * memberExpr ) override final;
  • src/Common/PassVisitor.impl.h

    r60ba456 r9a705dc8  
    12591259
    12601260//--------------------------------------------------------------------------
     1261// KeywordCastExpr
     1262template< typename pass_type >
     1263void PassVisitor< pass_type >::visit( KeywordCastExpr * node ) {
     1264        VISIT_START( node );
     1265
     1266        indexerScopedAccept( node->result, *this );
     1267        maybeAccept_impl        ( node->arg   , *this );
     1268
     1269        VISIT_END( node );
     1270}
     1271
     1272template< typename pass_type >
     1273Expression * PassVisitor< pass_type >::mutate( KeywordCastExpr * node ) {
     1274        MUTATE_START( node );
     1275
     1276        indexerScopedMutate( node->env   , *this );
     1277        indexerScopedMutate( node->result, *this );
     1278        maybeMutate_impl   ( node->arg   , *this );
     1279
     1280        MUTATE_END( Expression, node );
     1281}
     1282
     1283//--------------------------------------------------------------------------
    12611284// VirtualCastExpr
    12621285template< typename pass_type >
  • src/Concurrency/Keywords.cc

    r60ba456 r9a705dc8  
    5353          public:
    5454
    55                 ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main ) :
    56                   type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ) {}
     55                ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main, KeywordCastExpr::Target cast_target ) :
     56                  type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ), cast_target( cast_target ) {}
    5757
    5858                virtual ~ConcurrentSueKeyword() {}
    5959
    60                 void postvisit( StructDecl * decl );
     60                Declaration * postmutate( StructDecl * decl );
    6161
    6262                void handle( StructDecl * );
     
    6666
    6767                virtual bool is_target( StructDecl * decl ) = 0;
     68
     69                Expression * postmutate( KeywordCastExpr * cast );
    6870
    6971          private:
     
    7375                const std::string context_error;
    7476                bool needs_main;
     77                KeywordCastExpr::Target cast_target;
    7578
    7679                StructDecl* type_decl = nullptr;
     
    9598                        "get_thread",
    9699                        "thread keyword requires threads to be in scope, add #include <thread>",
    97                         true
     100                        true,
     101                        KeywordCastExpr::Thread
    98102                )
    99103                {}
     
    105109                static void implement( std::list< Declaration * > & translationUnit ) {
    106110                        PassVisitor< ThreadKeyword > impl;
    107                         acceptAll( translationUnit, impl );
     111                        mutateAll( translationUnit, impl );
    108112                }
    109113        };
     
    126130                        "get_coroutine",
    127131                        "coroutine keyword requires coroutines to be in scope, add #include <coroutine>",
    128                         true
     132                        true,
     133                        KeywordCastExpr::Coroutine
    129134                )
    130135                {}
     
    136141                static void implement( std::list< Declaration * > & translationUnit ) {
    137142                        PassVisitor< CoroutineKeyword > impl;
    138                         acceptAll( translationUnit, impl );
     143                        mutateAll( translationUnit, impl );
    139144                }
    140145        };
     
    157162                        "get_monitor",
    158163                        "monitor keyword requires monitors to be in scope, add #include <monitor>",
    159                         false
     164                        false,
     165                        KeywordCastExpr::Monitor
    160166                )
    161167                {}
     
    167173                static void implement( std::list< Declaration * > & translationUnit ) {
    168174                        PassVisitor< MonitorKeyword > impl;
    169                         acceptAll( translationUnit, impl );
     175                        mutateAll( translationUnit, impl );
    170176                }
    171177        };
     
    267273        }
    268274
    269         void ConcurrentSueKeyword::postvisit(StructDecl * decl) {
     275        Declaration * ConcurrentSueKeyword::postmutate(StructDecl * decl) {
    270276                if( decl->name == type_name && decl->body ) {
    271277                        assert( !type_decl );
     
    275281                        handle( decl );
    276282                }
    277         }
     283                return decl;
     284        }
     285
     286        Expression * ConcurrentSueKeyword::postmutate( KeywordCastExpr * cast ) {
     287                if ( cast_target == cast->target ) {
     288                        // convert (thread &)t to (thread_desc &)*get_thread(t), etc.
     289                        if( !type_decl ) SemanticError( cast, context_error );
     290                        Expression * arg = cast->arg;
     291                        cast->arg = nullptr;
     292                        delete cast;
     293                        return new CastExpr(
     294                                UntypedExpr::createDeref(
     295                                        new UntypedExpr( new NameExpr( getter_name ), { arg } )
     296                                ),
     297                                new ReferenceType(
     298                                        noQualifiers,
     299                                        new StructInstType( noQualifiers, type_decl ) )
     300                                );
     301                }
     302                return cast;
     303        }
     304
    278305
    279306        void ConcurrentSueKeyword::handle( StructDecl * decl ) {
  • src/Parser/ExpressionNode.cc

    r60ba456 r9a705dc8  
    414414} // build_cast
    415415
     416Expression * build_keyword_cast( KeywordCastExpr::Target target, ExpressionNode * expr_node ) {
     417        return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target );
     418}
     419
    416420Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
    417421        return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) );
  • src/Parser/ParseNode.h

    r60ba456 r9a705dc8  
    179179
    180180Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node );
     181Expression * build_keyword_cast( KeywordCastExpr::Target target, ExpressionNode * expr_node );
    181182Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node );
    182183Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member );
  • src/Parser/parser.yy

    r60ba456 r9a705dc8  
    688688                { $$ = new ExpressionNode( build_cast( $2, $4 ) ); }
    689689        | '(' COROUTINE '&' ')' cast_expression                         // CFA
    690                 { SemanticError( yylloc, "coroutine cast is currently unimplemented." ); $$ = nullptr; }
     690                { $$ = new ExpressionNode( build_keyword_cast( KeywordCastExpr::Coroutine, $5 ) ); }
    691691        | '(' THREAD '&' ')' cast_expression                            // CFA
    692                 { SemanticError( yylloc, "monitor cast is currently unimplemented." ); $$ = nullptr; }
     692                { $$ = new ExpressionNode( build_keyword_cast( KeywordCastExpr::Thread, $5 ) ); }
    693693        | '(' MONITOR '&' ')' cast_expression                           // CFA
    694                 { SemanticError( yylloc, "thread cast is currently unimplemented." ); $$ = nullptr; }
     694                { $$ = new ExpressionNode( build_keyword_cast( KeywordCastExpr::Monitor, $5 ) ); }
    695695                // VIRTUAL cannot be opt because of look ahead issues
    696696        | '(' VIRTUAL ')' cast_expression                                       // CFA
  • src/SynTree/Expression.cc

    r60ba456 r9a705dc8  
    299299}
    300300
     301KeywordCastExpr::KeywordCastExpr( Expression *arg, Target target ) : Expression(), arg(arg), target( target ) {
     302}
     303
     304KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {
     305}
     306
     307KeywordCastExpr::~KeywordCastExpr() {
     308        delete arg;
     309}
     310
     311const std::string & KeywordCastExpr::targetString() const {
     312        static const std::string targetStrs[] = {
     313                "coroutine", "thread", "monitor"
     314        };
     315        static_assert(
     316                (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS),
     317                "Each KeywordCastExpr::Target should have a corresponding string representation"
     318        );
     319        return targetStrs[(unsigned long)target];
     320}
     321
     322void KeywordCastExpr::print( std::ostream &os, Indenter indent ) const {
     323        os << "Keyword Cast of:" << std::endl << indent+1;
     324        arg->print(os, indent+1);
     325        os << std::endl << indent << "... to: ";
     326        os << targetString();
     327        Expression::print( os, indent );
     328}
     329
    301330VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
    302331        set_result(toType);
  • src/SynTree/Expression.h

    r60ba456 r9a705dc8  
    192192        CastExpr( Expression * arg, bool isGenerated = true );
    193193        CastExpr( Expression * arg, Type * toType, bool isGenerated = true );
     194        CastExpr( Expression * arg, void * ) = delete; // prevent accidentally passing pointers for isGenerated in the first constructor
    194195        CastExpr( const CastExpr & other );
    195196        virtual ~CastExpr();
     
    199200
    200201        virtual CastExpr * clone() const { return new CastExpr( * this ); }
     202        virtual void accept( Visitor & v ) { v.visit( this ); }
     203        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
     204        virtual void print( std::ostream & os, Indenter indent = {} ) const;
     205};
     206
     207/// KeywordCastExpr represents a cast to 'keyword types', e.g. (thread &)t
     208class KeywordCastExpr : public Expression {
     209public:
     210        Expression * arg;
     211        enum Target {
     212                Coroutine, Thread, Monitor, NUMBER_OF_TARGETS
     213        } target;
     214
     215        KeywordCastExpr( Expression * arg, Target target );
     216        KeywordCastExpr( const KeywordCastExpr & other );
     217        virtual ~KeywordCastExpr();
     218
     219        const std::string & targetString() const;
     220
     221        virtual KeywordCastExpr * clone() const { return new KeywordCastExpr( * this ); }
    201222        virtual void accept( Visitor & v ) { v.visit( this ); }
    202223        virtual Expression * acceptMutator( Mutator & m ) { return m.mutate( this ); }
  • src/SynTree/Mutator.h

    r60ba456 r9a705dc8  
    5959        virtual Expression * mutate( UntypedExpr * untypedExpr ) = 0;
    6060        virtual Expression * mutate( NameExpr * nameExpr ) = 0;
    61         virtual Expression * mutate( AddressExpr * castExpr ) = 0;
     61        virtual Expression * mutate( AddressExpr * addrExpr ) = 0;
    6262        virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) = 0;
    6363        virtual Expression * mutate( CastExpr * castExpr ) = 0;
     64        virtual Expression * mutate( KeywordCastExpr * castExpr ) = 0;
    6465        virtual Expression * mutate( VirtualCastExpr * castExpr ) = 0;
    6566        virtual Expression * mutate( UntypedMemberExpr * memberExpr ) = 0;
  • src/SynTree/SynTree.h

    r60ba456 r9a705dc8  
    6969class LabelAddressExpr;
    7070class CastExpr;
     71class KeywordCastExpr;
    7172class VirtualCastExpr;
    7273class MemberExpr;
  • src/SynTree/Visitor.h

    r60ba456 r9a705dc8  
    6262        virtual void visit( NameExpr * nameExpr ) = 0;
    6363        virtual void visit( CastExpr * castExpr ) = 0;
     64        virtual void visit( KeywordCastExpr * castExpr ) = 0;
    6465        virtual void visit( VirtualCastExpr * castExpr ) = 0;
    6566        virtual void visit( AddressExpr * addressExpr ) = 0;
Note: See TracChangeset for help on using the changeset viewer.