Changeset 4ef08f7


Ignore:
Timestamp:
Aug 31, 2020, 1:54:40 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
eb67b47
Parents:
207c7330
Message:

Implemented KeywordCast? in CandidateFinder? of new AST.

Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r207c7330 r4ef08f7  
    705705                        new KeywordCastExpr(
    706706                                get<Expression>().accept1(node->arg),
    707                                 castTarget
     707                                castTarget,
     708                                {node->concrete_target.field, node->concrete_target.getter}
    708709                        )
    709710                );
     
    20872088                                old->location,
    20882089                                GET_ACCEPT_1(arg, Expr),
    2089                                 castTarget
     2090                                castTarget,
     2091                                {old->concrete_target.field, old->concrete_target.getter}
    20902092                        )
    20912093                );
  • src/AST/Expr.hpp

    r207c7330 r4ef08f7  
    312312public:
    313313        ptr<Expr> arg;
     314        struct Concrete {
     315                std::string field;
     316                std::string getter;
     317
     318                Concrete() = default;
     319                Concrete(const Concrete &) = default;
     320        };
    314321        ast::AggregateDecl::Aggregate target;
     322        Concrete concrete_target;
     323
    315324
    316325        KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t )
    317326        : Expr( loc ), arg( a ), target( t ) {}
     327
     328        KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t, const Concrete & ct )
     329        : Expr( loc ), arg( a ), target( t ), concrete_target( ct ) {}
    318330
    319331        /// Get a name for the target type
  • src/ResolvExpr/CandidateFinder.cpp

    r207c7330 r4ef08f7  
    10891089                }
    10901090
     1091                void postvisit( const ast::KeywordCastExpr * castExpr ) {
     1092                        const auto & loc = castExpr->location;
     1093                        assertf( castExpr->result, "Cast target should have been set in Validate." );
     1094                        auto ref = castExpr->result.strict_as<ast::ReferenceType>();
     1095                        auto inst = ref->base.strict_as<ast::StructInstType>();
     1096                        auto target = inst->base.get();
     1097
     1098                        CandidateFinder finder{ symtab, tenv };
     1099
     1100                        auto pick_alternatives = [target, this](CandidateList & found, bool expect_ref) {
     1101                                for(auto & cand : found) {
     1102                                        const ast::Type * expr = cand->expr->result.get();
     1103                                        if(expect_ref) {
     1104                                                auto res = dynamic_cast<const ast::ReferenceType*>(expr);
     1105                                                if(!res) { continue; }
     1106                                                expr = res->base.get();
     1107                                        }
     1108
     1109                                        if(auto insttype = dynamic_cast<const ast::TypeInstType*>(expr)) {
     1110                                                auto td = cand->env.lookup(insttype->name);
     1111                                                if(!td) { continue; }
     1112                                                expr = td->bound.get();
     1113                                        }
     1114
     1115                                        if(auto base = dynamic_cast<const ast::StructInstType*>(expr)) {
     1116                                                if(base->base == target) {
     1117                                                        candidates.push_back( std::move(cand) );
     1118                                                        reason.code = NoReason;
     1119                                                }
     1120                                        }
     1121                                }
     1122                        };
     1123
     1124                        try {
     1125                                // Attempt 1 : turn (thread&)X into ($thread&)X.__thrd
     1126                                // Clone is purely for memory management
     1127                                std::unique_ptr<const ast::Expr> tech1 { new ast::UntypedMemberExpr(loc, new ast::NameExpr(loc, castExpr->concrete_target.field), castExpr->arg) };
     1128
     1129                                // don't prune here, since it's guaranteed all alternatives will have the same type
     1130                                finder.find( tech1.get(), ResolvMode::withoutPrune() );
     1131                                pick_alternatives(finder.candidates, false);
     1132
     1133                                return;
     1134                        } catch(SemanticErrorException & ) {}
     1135
     1136                        // Fallback : turn (thread&)X into ($thread&)get_thread(X)
     1137                        std::unique_ptr<const ast::Expr> fallback { ast::UntypedExpr::createDeref(loc,  new ast::UntypedExpr(loc, new ast::NameExpr(loc, castExpr->concrete_target.getter), { castExpr->arg })) };
     1138                        // don't prune here, since it's guaranteed all alternatives will have the same type
     1139                        finder.find( fallback.get(), ResolvMode::withoutPrune() );
     1140
     1141                        pick_alternatives(finder.candidates, true);
     1142
     1143                        // Whatever happens here, we have no more fallbacks
     1144                }
     1145
    10911146                void postvisit( const ast::UntypedMemberExpr * memberExpr ) {
    10921147                        CandidateFinder aggFinder{ symtab, tenv };
  • src/SynTree/Expression.cc

    r207c7330 r4ef08f7  
    302302}
    303303
    304 KeywordCastExpr::KeywordCastExpr( Expression * arg, AggregateDecl::Aggregate target ) : Expression(), arg(arg), target( target ) {
    305 }
    306 
    307 KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr & other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {
    308 }
     304KeywordCastExpr::KeywordCastExpr( Expression * arg, AggregateDecl::Aggregate target ) : Expression(), arg(arg), target( target ) {}
     305KeywordCastExpr::KeywordCastExpr( Expression * arg, AggregateDecl::Aggregate target, const KeywordCastExpr::Concrete & concrete_target ) : Expression(), arg(arg), target( target ), concrete_target(concrete_target) {}
     306
     307KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr & other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {}
    309308
    310309KeywordCastExpr::~KeywordCastExpr() {
  • src/SynTree/Expression.h

    r207c7330 r4ef08f7  
    248248
    249249        KeywordCastExpr( Expression * arg, AggregateDecl::Aggregate target );
     250        KeywordCastExpr( Expression * arg, AggregateDecl::Aggregate target, const Concrete & concrete_target );
    250251        KeywordCastExpr( const KeywordCastExpr & other );
    251252        virtual ~KeywordCastExpr();
Note: See TracChangeset for help on using the changeset viewer.