Ignore:
Timestamp:
Mar 21, 2022, 1:44:06 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
a76202d
Parents:
ef3c383 (diff), dbe2533 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    ref3c383 rd672350  
    99// Author           : Aaron B. Moss
    1010// Created On       : Sun May 17 12:17:01 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Feb  1 16:27:14 2022
    13 // Update Count     : 245
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Mar 18 10:41:00 2022
     13// Update Count     : 247
    1414//
    1515
     
    997997                /// Calls the CandidateFinder and finds the single best candidate
    998998                CandidateRef findUnfinishedKindExpression(
    999                         const ast::Expr * untyped, const ast::SymbolTable & symtab, const std::string & kind,
     999                        const ast::Expr * untyped, const ResolveContext & context, const std::string & kind,
    10001000                        std::function<bool(const Candidate &)> pred = anyCandidate, ResolvMode mode = {}
    10011001                ) {
     
    10071007                        ++recursion_level;
    10081008                        ast::TypeEnvironment env;
    1009                         CandidateFinder finder{ symtab, env };
     1009                        CandidateFinder finder( context, env );
    10101010                        finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode );
    10111011                        --recursion_level;
     
    11291129
    11301130        ast::ptr< ast::Expr > resolveInVoidContext(
    1131                 const ast::Expr * expr, const ast::SymbolTable & symtab, ast::TypeEnvironment & env
     1131                const ast::Expr * expr, const ResolveContext & context,
     1132                ast::TypeEnvironment & env
    11321133        ) {
    11331134                assertf( expr, "expected a non-null expression" );
     
    11361137                ast::ptr< ast::CastExpr > untyped = new ast::CastExpr{ expr };
    11371138                CandidateRef choice = findUnfinishedKindExpression(
    1138                         untyped, symtab, "", anyCandidate, ResolvMode::withAdjustment() );
     1139                        untyped, context, "", anyCandidate, ResolvMode::withAdjustment() );
    11391140
    11401141                // a cast expression has either 0 or 1 interpretations (by language rules);
     
    11491150                /// context.
    11501151                ast::ptr< ast::Expr > findVoidExpression(
    1151                         const ast::Expr * untyped, const ast::SymbolTable & symtab
     1152                        const ast::Expr * untyped, const ResolveContext & context
    11521153                ) {
    11531154                        ast::TypeEnvironment env;
    1154                         ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, symtab, env );
     1155                        ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, context, env );
    11551156                        finishExpr( newExpr, env, untyped->env );
    11561157                        return newExpr;
     
    11631164                /// lowest cost, returning the resolved version
    11641165                ast::ptr< ast::Expr > findKindExpression(
    1165                         const ast::Expr * untyped, const ast::SymbolTable & symtab,
     1166                        const ast::Expr * untyped, const ResolveContext & context,
    11661167                        std::function<bool(const Candidate &)> pred = anyCandidate,
    11671168                        const std::string & kind = "", ResolvMode mode = {}
     
    11691170                        if ( ! untyped ) return {};
    11701171                        CandidateRef choice =
    1171                                 findUnfinishedKindExpression( untyped, symtab, kind, pred, mode );
     1172                                findUnfinishedKindExpression( untyped, context, kind, pred, mode );
    11721173                        ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env );
    11731174                        return std::move( choice->expr );
     
    11761177                /// Resolve `untyped` to the single expression whose candidate is the best match
    11771178                ast::ptr< ast::Expr > findSingleExpression(
    1178                         const ast::Expr * untyped, const ast::SymbolTable & symtab
     1179                        const ast::Expr * untyped, const ResolveContext & context
    11791180                ) {
    11801181                        Stats::ResolveTime::start( untyped );
    1181                         auto res = findKindExpression( untyped, symtab );
     1182                        auto res = findKindExpression( untyped, context );
    11821183                        Stats::ResolveTime::stop();
    11831184                        return res;
     
    11861187
    11871188        ast::ptr< ast::Expr > findSingleExpression(
    1188                 const ast::Expr * untyped, const ast::Type * type, const ast::SymbolTable & symtab
     1189                const ast::Expr * untyped, const ast::Type * type,
     1190                const ResolveContext & context
    11891191        ) {
    11901192                assert( untyped && type );
    11911193                ast::ptr< ast::Expr > castExpr = new ast::CastExpr{ untyped, type };
    1192                 ast::ptr< ast::Expr > newExpr = findSingleExpression( castExpr, symtab );
    1193                 removeExtraneousCast( newExpr, symtab );
     1194                ast::ptr< ast::Expr > newExpr = findSingleExpression( castExpr, context );
     1195                removeExtraneousCast( newExpr, context.symtab );
    11941196                return newExpr;
    11951197        }
     
    12171219                /// Resolve `untyped` as an integral expression, returning the resolved version
    12181220                ast::ptr< ast::Expr > findIntegralExpression(
    1219                         const ast::Expr * untyped, const ast::SymbolTable & symtab
     1221                        const ast::Expr * untyped, const ResolveContext & context
    12201222                ) {
    1221                         return findKindExpression( untyped, symtab, hasIntegralType, "condition" );
     1223                        return findKindExpression( untyped, context, hasIntegralType, "condition" );
    12221224                }
    12231225
     
    12491251                // for work previously in GenInit
    12501252                static InitTweak::ManagedTypes_new managedTypes;
     1253                ResolveContext context;
    12511254
    12521255                bool inEnumDecl = false;
     
    12541257        public:
    12551258                static size_t traceId;
    1256                 Resolver_new() = default;
    1257                 Resolver_new( const ast::SymbolTable & syms ) { symtab = syms; }
     1259                Resolver_new( const ast::TranslationGlobal & global ) :
     1260                        context{ symtab, global } {}
     1261                Resolver_new( const ResolveContext & context ) :
     1262                        ast::WithSymbolTable{ context.symtab },
     1263                        context{ symtab, context.global } {}
    12581264
    12591265                const ast::FunctionDecl * previsit( const ast::FunctionDecl * );
     
    12721278                const ast::AsmStmt *         previsit( const ast::AsmStmt * );
    12731279                const ast::IfStmt *          previsit( const ast::IfStmt * );
    1274                 const ast::WhileDoStmt *       previsit( const ast::WhileDoStmt * );
     1280                const ast::WhileDoStmt *     previsit( const ast::WhileDoStmt * );
    12751281                const ast::ForStmt *         previsit( const ast::ForStmt * );
    12761282                const ast::SwitchStmt *      previsit( const ast::SwitchStmt * );
     
    12991305
    13001306        void resolve( ast::TranslationUnit& translationUnit ) {
    1301                 ast::Pass< Resolver_new >::run( translationUnit );
     1307                ast::Pass< Resolver_new >::run( translationUnit, translationUnit.global );
    13021308        }
    13031309
    13041310        ast::ptr< ast::Init > resolveCtorInit(
    1305                 const ast::ConstructorInit * ctorInit, const ast::SymbolTable & symtab
     1311                const ast::ConstructorInit * ctorInit, const ResolveContext & context
    13061312        ) {
    13071313                assert( ctorInit );
    1308                 ast::Pass< Resolver_new > resolver{ symtab };
     1314                ast::Pass< Resolver_new > resolver( context );
    13091315                return ctorInit->accept( resolver );
    13101316        }
    13111317
    13121318        const ast::Expr * resolveStmtExpr(
    1313                 const ast::StmtExpr * stmtExpr, const ast::SymbolTable & symtab
     1319                const ast::StmtExpr * stmtExpr, const ResolveContext & context
    13141320        ) {
    13151321                assert( stmtExpr );
    1316                 ast::Pass< Resolver_new > resolver{ symtab };
     1322                ast::Pass< Resolver_new > resolver( context );
    13171323                auto ret = mutate(stmtExpr->accept(resolver));
    13181324                strict_dynamic_cast< ast::StmtExpr * >( ret )->computeResult();
     
    13211327
    13221328        namespace {
    1323                 const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ast::SymbolTable & symtab) {
     1329                const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ResolveContext & context) {
    13241330                        std::string name = attr->normalizedName();
    13251331                        if (name == "constructor" || name == "destructor") {
    13261332                                if (attr->params.size() == 1) {
    13271333                                        auto arg = attr->params.front();
    1328                                         auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicType::LongLongSignedInt ), symtab );
     1334                                        auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicType::LongLongSignedInt ), context );
    13291335                                        auto result = eval(arg);
    13301336
     
    13691375
    13701376                        for (auto & attr: mutDecl->attributes) {
    1371                                 attr = handleAttribute(mutDecl->location, attr, symtab);
     1377                                attr = handleAttribute(mutDecl->location, attr, context );
    13721378                        }
    13731379
     
    13821388                        }
    13831389                        for (auto & asst : mutDecl->assertions) {
    1384                                 asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), symtab);
     1390                                asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), context);
    13851391                                symtab.addId(asst);
    13861392                                mutType->assertions.emplace_back(new ast::VariableExpr(functionDecl->location, asst));
     
    13941400
    13951401                        for (auto & param : mutDecl->params) {
    1396                                 param = fixObjectType(param.strict_as<ast::ObjectDecl>(), symtab);
     1402                                param = fixObjectType(param.strict_as<ast::ObjectDecl>(), context);
    13971403                                symtab.addId(param);
    13981404                                paramTypes.emplace_back(param->get_type());
    13991405                        }
    14001406                        for (auto & ret : mutDecl->returns) {
    1401                                 ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), symtab);
     1407                                ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), context);
    14021408                                returnTypes.emplace_back(ret->get_type());
    14031409                        }
     
    14701476                        // enumerator initializers should not use the enum type to initialize, since the
    14711477                        // enum type is still incomplete at this point. Use `int` instead.
    1472                         objectDecl = fixObjectType(objectDecl, symtab);
     1478                        objectDecl = fixObjectType(objectDecl, context);
    14731479                        currentObject = ast::CurrentObject{
    14741480                                objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } };
     
    14761482                else {
    14771483                        if (!objectDecl->isTypeFixed) {
    1478                                 auto newDecl = fixObjectType(objectDecl, symtab);
     1484                                auto newDecl = fixObjectType(objectDecl, context);
    14791485                                auto mutDecl = mutate(newDecl);
    14801486
     
    15071513                        // nested type decls are hoisted already. no need to do anything
    15081514                        if (auto obj = member.as<ast::ObjectDecl>()) {
    1509                                 member = fixObjectType(obj, symtab);
     1515                                member = fixObjectType(obj, context);
    15101516                        }
    15111517                }
     
    15301536                return ast::mutate_field(
    15311537                        assertDecl, &ast::StaticAssertDecl::cond,
    1532                         findIntegralExpression( assertDecl->cond, symtab ) );
     1538                        findIntegralExpression( assertDecl->cond, context ) );
    15331539        }
    15341540
    15351541        template< typename PtrType >
    1536         const PtrType * handlePtrType( const PtrType * type, const ast::SymbolTable & symtab ) {
     1542        const PtrType * handlePtrType( const PtrType * type, const ResolveContext & context ) {
    15371543                if ( type->dimension ) {
    1538                         ast::ptr< ast::Type > sizeType = ast::sizeType;
     1544                        ast::ptr< ast::Type > sizeType = context.global.sizeType;
    15391545                        ast::mutate_field(
    15401546                                type, &PtrType::dimension,
    1541                                 findSingleExpression( type->dimension, sizeType, symtab ) );
     1547                                findSingleExpression( type->dimension, sizeType, context ) );
    15421548                }
    15431549                return type;
     
    15451551
    15461552        const ast::ArrayType * Resolver_new::previsit( const ast::ArrayType * at ) {
    1547                 return handlePtrType( at, symtab );
     1553                return handlePtrType( at, context );
    15481554        }
    15491555
    15501556        const ast::PointerType * Resolver_new::previsit( const ast::PointerType * pt ) {
    1551                 return handlePtrType( pt, symtab );
     1557                return handlePtrType( pt, context );
    15521558        }
    15531559
     
    15571563
    15581564                return ast::mutate_field(
    1559                         exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, symtab ) );
     1565                        exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, context ) );
    15601566        }
    15611567
     
    15641570
    15651571                asmExpr = ast::mutate_field(
    1566                         asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, symtab ) );
     1572                        asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, context ) );
    15671573
    15681574                return asmExpr;
     
    15781584        const ast::IfStmt * Resolver_new::previsit( const ast::IfStmt * ifStmt ) {
    15791585                return ast::mutate_field(
    1580                         ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, symtab ) );
     1586                        ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, context ) );
    15811587        }
    15821588
    15831589        const ast::WhileDoStmt * Resolver_new::previsit( const ast::WhileDoStmt * whileDoStmt ) {
    15841590                return ast::mutate_field(
    1585                         whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, symtab ) );
     1591                        whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, context ) );
    15861592        }
    15871593
     
    15891595                if ( forStmt->cond ) {
    15901596                        forStmt = ast::mutate_field(
    1591                                 forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, symtab ) );
     1597                                forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, context ) );
    15921598                }
    15931599
    15941600                if ( forStmt->inc ) {
    15951601                        forStmt = ast::mutate_field(
    1596                                 forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, symtab ) );
     1602                                forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, context ) );
    15971603                }
    15981604
     
    16041610                switchStmt = ast::mutate_field(
    16051611                        switchStmt, &ast::SwitchStmt::cond,
    1606                         findIntegralExpression( switchStmt->cond, symtab ) );
     1612                        findIntegralExpression( switchStmt->cond, context ) );
    16071613                currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result };
    16081614                return switchStmt;
     
    16171623                        ast::ptr< ast::Expr > untyped =
    16181624                                new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type };
    1619                         ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, symtab );
     1625                        ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, context );
    16201626
    16211627                        // case condition cannot have a cast in C, so it must be removed here, regardless of
     
    16381644                        branchStmt = ast::mutate_field(
    16391645                                branchStmt, &ast::BranchStmt::computedTarget,
    1640                                 findSingleExpression( branchStmt->computedTarget, target, symtab ) );
     1646                                findSingleExpression( branchStmt->computedTarget, target, context ) );
    16411647                }
    16421648                return branchStmt;
     
    16481654                        returnStmt = ast::mutate_field(
    16491655                                returnStmt, &ast::ReturnStmt::expr,
    1650                                 findSingleExpression( returnStmt->expr, functionReturn, symtab ) );
     1656                                findSingleExpression( returnStmt->expr, functionReturn, context ) );
    16511657                }
    16521658                return returnStmt;
     
    16631669                        throwStmt = ast::mutate_field(
    16641670                                throwStmt, &ast::ThrowStmt::expr,
    1665                                 findSingleExpression( throwStmt->expr, exceptType, symtab ) );
     1671                                findSingleExpression( throwStmt->expr, exceptType, context ) );
    16661672                }
    16671673                return throwStmt;
     
    17071713
    17081714                        ast::TypeEnvironment env;
    1709                         CandidateFinder funcFinder{ symtab, env };
     1715                        CandidateFinder funcFinder( context, env );
    17101716
    17111717                        // Find all candidates for a function in canonical form
     
    19211927                                );
    19221928
    1923                                 clause2.target.args.emplace_back( findSingleExpression( init, symtab ) );
     1929                                clause2.target.args.emplace_back( findSingleExpression( init, context ) );
    19241930                        }
    19251931
    19261932                        // Resolve the conditions as if it were an IfStmt, statements normally
    1927                         clause2.cond = findSingleExpression( clause.cond, symtab );
     1933                        clause2.cond = findSingleExpression( clause.cond, context );
    19281934                        clause2.stmt = clause.stmt->accept( *visitor );
    19291935
     
    19401946                        ast::ptr< ast::Type > target =
    19411947                                new ast::BasicType{ ast::BasicType::LongLongUnsignedInt };
    1942                         timeout2.time = findSingleExpression( stmt->timeout.time, target, symtab );
    1943                         timeout2.cond = findSingleExpression( stmt->timeout.cond, symtab );
     1948                        timeout2.time = findSingleExpression( stmt->timeout.time, target, context );
     1949                        timeout2.cond = findSingleExpression( stmt->timeout.cond, context );
    19441950                        timeout2.stmt = stmt->timeout.stmt->accept( *visitor );
    19451951
     
    19541960                        ast::WaitForStmt::OrElse orElse2;
    19551961
    1956                         orElse2.cond = findSingleExpression( stmt->orElse.cond, symtab );
     1962                        orElse2.cond = findSingleExpression( stmt->orElse.cond, context );
    19571963                        orElse2.stmt = stmt->orElse.stmt->accept( *visitor );
    19581964
     
    19751981                for (auto & expr : exprs) {
    19761982                        // only struct- and union-typed expressions are viable candidates
    1977                         expr = findKindExpression( expr, symtab, structOrUnion, "with expression" );
     1983                        expr = findKindExpression( expr, context, structOrUnion, "with expression" );
    19781984
    19791985                        // if with expression might be impure, create a temporary so that it is evaluated once
     
    20012007                ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{
    20022008                        singleInit->location, singleInit->value, currentObject.getOptions() };
    2003                 ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, symtab );
     2009                ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, context );
    20042010                const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >();
    20052011
Note: See TracChangeset for help on using the changeset viewer.