Changeset e5c3811 for src/ResolvExpr


Ignore:
Timestamp:
Nov 25, 2020, 3:12:17 AM (3 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
4702a2c
Parents:
7192145
Message:

create dedicated symbol tables for big 3 operators
note: arbitrary this param type is not supported; it is currently allowed although never used

Location:
src/ResolvExpr
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CandidateFinder.cpp

    r7192145 re5c3811  
    4343#include "SymTab/Validate.h"      // for validateType
    4444#include "Tuples/Tuples.h"        // for handleTupleAssignment
     45#include "InitTweak/InitTweak.h"  // for getPointerBase
     46
     47#include "Common/Stats/Counter.h"
    4548
    4649#define PRINT( text ) if ( resolvep ) { text }
     
    864867
    865868                void postvisit( const ast::UntypedExpr * untypedExpr ) {
    866                         CandidateFinder funcFinder{ symtab, tenv };
    867                         funcFinder.find( untypedExpr->func, ResolvMode::withAdjustment() );
    868                         // short-circuit if no candidates
    869                         if ( funcFinder.candidates.empty() ) return;
    870 
    871                         reason.code = NoMatch;
    872 
    873869                        std::vector< CandidateFinder > argCandidates =
    874870                                selfFinder.findSubExprs( untypedExpr->args );
     
    877873                        // if not tuple assignment, handled as normal function call
    878874                        Tuples::handleTupleAssignment( selfFinder, untypedExpr, argCandidates );
     875
     876                        CandidateFinder funcFinder{ symtab, tenv };
     877                        if (auto nameExpr = untypedExpr->func.as<ast::NameExpr>()) {
     878                                auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name);
     879                                if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) {
     880                                        assertf(!argCandidates.empty(), "special function call without argument");
     881                                        for (auto & firstArgCand: argCandidates[0]) {
     882                                                ast::ptr<ast::Type> argType = firstArgCand->expr->result;
     883                                                firstArgCand->env.apply(argType);
     884                                                // strip references
     885                                                // xxx - is this correct?
     886                                                while (argType.as<ast::ReferenceType>()) argType = argType.as<ast::ReferenceType>()->base;
     887
     888                                                // convert 1-tuple to plain type
     889                                                if (auto tuple = argType.as<ast::TupleType>()) {
     890                                                        if (tuple->size() == 1) {
     891                                                                argType = tuple->types[0];
     892                                                        }
     893                                                }
     894                                               
     895                                                // if argType is an unbound type parameter, all special functions need to be searched.
     896                                                if (isUnboundType(argType)) {
     897                                                        funcFinder.otypeKeys.clear();
     898                                                        break;
     899                                                }
     900
     901                                                if (argType.as<ast::PointerType>()) funcFinder.otypeKeys.insert(Mangle::Encoding::pointer);
     902                                                else funcFinder.otypeKeys.insert(Mangle::mangle(argType, Mangle::NoGenericParams | Mangle::Type));
     903                                        }
     904                                }
     905                        }
     906                        // if candidates are already produced, do not fail
     907                        // xxx - is it possible that handleTupleAssignment and main finder both produce candidates?
     908                        // this means there exists ctor/assign functions with a tuple as first parameter.
     909                        funcFinder.find( untypedExpr->func, selfFinder.candidates.empty() ? ResolvMode::withAdjustment() : ResolvMode::withoutFailFast() );
     910                        // short-circuit if no candidates
     911                        // if ( funcFinder.candidates.empty() ) return;
     912
     913                        reason.code = NoMatch;
    879914
    880915                        // find function operators
     
    11871222
    11881223                void postvisit( const ast::NameExpr * nameExpr ) {
    1189                         std::vector< ast::SymbolTable::IdData > declList = symtab.lookupId( nameExpr->name );
     1224                        std::vector< ast::SymbolTable::IdData > declList;
     1225                        if (!selfFinder.otypeKeys.empty()) {
     1226                                auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name);
     1227                                assertf(kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS, "special lookup with non-special target: %s", nameExpr->name.c_str());
     1228
     1229                                for (auto & otypeKey: selfFinder.otypeKeys) {
     1230                                        auto result = symtab.specialLookupId(kind, otypeKey);
     1231                                        declList.insert(declList.end(), std::make_move_iterator(result.begin()), std::make_move_iterator(result.end()));
     1232                                }
     1233                        }
     1234                        else {
     1235                                declList = symtab.lookupId( nameExpr->name );
     1236                        }
    11901237                        PRINT( std::cerr << "nameExpr is " << nameExpr->name << std::endl; )
     1238
    11911239                        if( declList.empty() ) return;
    11921240
     
    15581606                                        }
    15591607                                }
     1608
    15601609                        }
    15611610
  • src/ResolvExpr/CandidateFinder.hpp

    r7192145 re5c3811  
    3131        const ast::TypeEnvironment & env;  ///< Substitutions performed in this resolution
    3232        ast::ptr< ast::Type > targetType;  ///< Target type for resolution
     33        std::set< std::string > otypeKeys;  /// different type may map to same key
    3334
    3435        CandidateFinder(
  • src/ResolvExpr/SatisfyAssertions.cpp

    r7192145 re5c3811  
    167167                // find candidates that unify with the desired type
    168168                AssnCandidateList matches;
    169                 for ( const ast::SymbolTable::IdData & cdata : sat.symtab.lookupId( assn.first->name ) ) {
     169
     170                std::vector<ast::SymbolTable::IdData> candidates;
     171                auto kind = ast::SymbolTable::getSpecialFunctionKind(assn.first->name);
     172                if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) {
     173                        // prefilter special decls by argument type, if already known
     174                        ast::ptr<ast::Type> thisArgType = strict_dynamic_cast<const ast::PointerType *>(assn.first->get_type())->base
     175                                .strict_as<ast::FunctionType>()->params[0]
     176                                .strict_as<ast::ReferenceType>()->base;
     177                        sat.cand->env.apply(thisArgType);
     178
     179                        std::string otypeKey = "";
     180                        if (thisArgType.as<ast::PointerType>()) otypeKey = Mangle::Encoding::pointer;
     181                        else if (!isUnboundType(thisArgType)) otypeKey = Mangle::mangle(thisArgType, Mangle::Type | Mangle::NoGenericParams);
     182
     183                        candidates = sat.symtab.specialLookupId(kind, otypeKey);
     184                }
     185                else {
     186                        candidates = sat.symtab.lookupId(assn.first->name);
     187                }
     188                for ( const ast::SymbolTable::IdData & cdata : candidates ) {
    170189                        const ast::DeclWithType * candidate = cdata.id;
    171190
Note: See TracChangeset for help on using the changeset viewer.