Changeset 39d8950 for src/ResolvExpr


Ignore:
Timestamp:
Mar 16, 2022, 4:41:01 PM (2 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
f7496c5
Parents:
9d8124f
Message:

Thread global information through resolution. Non-top-level calls to the resolver have a bit of a hack but improvements would require changes to the Pass helpers.

Location:
src/ResolvExpr
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CandidateFinder.cpp

    r9d8124f r39d8950  
    1010// Created On       : Wed Jun 5 14:30:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Oct  1 14:55:00 2019
    13 // Update Count     : 2
     12// Last Modified On : Wed Mar 16 11:58:00 2022
     13// Update Count     : 3
    1414//
    1515
     
    595595        /// Actually visits expressions to find their candidate interpretations
    596596        class Finder final : public ast::WithShortCircuiting {
     597                const ResolveContext & context;
    597598                const ast::SymbolTable & symtab;
    598599        public:
     
    618619
    619620                Finder( CandidateFinder & f )
    620                 : symtab( f.localSyms ), selfFinder( f ), candidates( f.candidates ), tenv( f.env ),
    621                   targetType( f.targetType ) {}
     621                : context( f.context ), symtab( context.symtab ), selfFinder( f ),
     622                  candidates( f.candidates ), tenv( f.env ), targetType( f.targetType ) {}
    622623
    623624                void previsit( const ast::Node * ) { visit_children = false; }
     
    872873                        Tuples::handleTupleAssignment( selfFinder, untypedExpr, argCandidates );
    873874
    874                         CandidateFinder funcFinder{ symtab, tenv };
     875                        CandidateFinder funcFinder( context, tenv );
    875876                        if (auto nameExpr = untypedExpr->func.as<ast::NameExpr>()) {
    876877                                auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name);
     
    918919                        // find function operators
    919920                        ast::ptr< ast::Expr > opExpr = new ast::NameExpr{ untypedExpr->location, "?()" };
    920                         CandidateFinder opFinder{ symtab, tenv };
     921                        CandidateFinder opFinder( context, tenv );
    921922                        // okay if there aren't any function operations
    922923                        opFinder.find( opExpr, ResolvMode::withoutFailFast() );
     
    10591060
    10601061                void postvisit( const ast::AddressExpr * addressExpr ) {
    1061                         CandidateFinder finder{ symtab, tenv };
     1062                        CandidateFinder finder( context, tenv );
    10621063                        finder.find( addressExpr->arg );
    10631064
     
    10791080                        ast::ptr< ast::Type > toType = castExpr->result;
    10801081                        assert( toType );
    1081                         toType = resolveTypeof( toType, symtab );
     1082                        toType = resolveTypeof( toType, context );
    10821083                        // toType = SymTab::validateType( castExpr->location, toType, symtab );
    10831084                        toType = adjustExprType( toType, tenv, symtab );
    10841085
    1085                         CandidateFinder finder{ symtab, tenv, toType };
     1086                        CandidateFinder finder( context, tenv, toType );
    10861087                        finder.find( castExpr->arg, ResolvMode::withAdjustment() );
    10871088
     
    11361137                void postvisit( const ast::VirtualCastExpr * castExpr ) {
    11371138                        assertf( castExpr->result, "Implicit virtual cast targets not yet supported." );
    1138                         CandidateFinder finder{ symtab, tenv };
     1139                        CandidateFinder finder( context, tenv );
    11391140                        // don't prune here, all alternatives guaranteed to have same type
    11401141                        finder.find( castExpr->arg, ResolvMode::withoutPrune() );
     
    11531154                        auto target = inst->base.get();
    11541155
    1155                         CandidateFinder finder{ symtab, tenv };
     1156                        CandidateFinder finder( context, tenv );
    11561157
    11571158                        auto pick_alternatives = [target, this](CandidateList & found, bool expect_ref) {
     
    12021203
    12031204                void postvisit( const ast::UntypedMemberExpr * memberExpr ) {
    1204                         CandidateFinder aggFinder{ symtab, tenv };
     1205                        CandidateFinder aggFinder( context, tenv );
    12051206                        aggFinder.find( memberExpr->aggregate, ResolvMode::withAdjustment() );
    12061207                        for ( CandidateRef & agg : aggFinder.candidates ) {
     
    12871288                                addCandidate(
    12881289                                        new ast::SizeofExpr{
    1289                                                 sizeofExpr->location, resolveTypeof( sizeofExpr->type, symtab ) },
     1290                                                sizeofExpr->location, resolveTypeof( sizeofExpr->type, context ) },
    12901291                                        tenv );
    12911292                        } else {
    12921293                                // find all candidates for the argument to sizeof
    1293                                 CandidateFinder finder{ symtab, tenv };
     1294                                CandidateFinder finder( context, tenv );
    12941295                                finder.find( sizeofExpr->expr );
    12951296                                // find the lowest-cost candidate, otherwise ambiguous
     
    13111312                                addCandidate(
    13121313                                        new ast::AlignofExpr{
    1313                                                 alignofExpr->location, resolveTypeof( alignofExpr->type, symtab ) },
     1314                                                alignofExpr->location, resolveTypeof( alignofExpr->type, context ) },
    13141315                                        tenv );
    13151316                        } else {
    13161317                                // find all candidates for the argument to alignof
    1317                                 CandidateFinder finder{ symtab, tenv };
     1318                                CandidateFinder finder( context, tenv );
    13181319                                finder.find( alignofExpr->expr );
    13191320                                // find the lowest-cost candidate, otherwise ambiguous
     
    13541355
    13551356                void postvisit( const ast::LogicalExpr * logicalExpr ) {
    1356                         CandidateFinder finder1{ symtab, tenv };
     1357                        CandidateFinder finder1( context, tenv );
    13571358                        finder1.find( logicalExpr->arg1, ResolvMode::withAdjustment() );
    13581359                        if ( finder1.candidates.empty() ) return;
    13591360
    1360                         CandidateFinder finder2{ symtab, tenv };
     1361                        CandidateFinder finder2( context, tenv );
    13611362                        finder2.find( logicalExpr->arg2, ResolvMode::withAdjustment() );
    13621363                        if ( finder2.candidates.empty() ) return;
     
    13841385                void postvisit( const ast::ConditionalExpr * conditionalExpr ) {
    13851386                        // candidates for condition
    1386                         CandidateFinder finder1{ symtab, tenv };
     1387                        CandidateFinder finder1( context, tenv );
    13871388                        finder1.find( conditionalExpr->arg1, ResolvMode::withAdjustment() );
    13881389                        if ( finder1.candidates.empty() ) return;
    13891390
    13901391                        // candidates for true result
    1391                         CandidateFinder finder2{ symtab, tenv };
     1392                        CandidateFinder finder2( context, tenv );
    13921393                        finder2.find( conditionalExpr->arg2, ResolvMode::withAdjustment() );
    13931394                        if ( finder2.candidates.empty() ) return;
    13941395
    13951396                        // candidates for false result
    1396                         CandidateFinder finder3{ symtab, tenv };
     1397                        CandidateFinder finder3( context, tenv );
    13971398                        finder3.find( conditionalExpr->arg3, ResolvMode::withAdjustment() );
    13981399                        if ( finder3.candidates.empty() ) return;
     
    14451446                void postvisit( const ast::CommaExpr * commaExpr ) {
    14461447                        ast::TypeEnvironment env{ tenv };
    1447                         ast::ptr< ast::Expr > arg1 = resolveInVoidContext( commaExpr->arg1, symtab, env );
    1448 
    1449                         CandidateFinder finder2{ symtab, env };
     1448                        ast::ptr< ast::Expr > arg1 = resolveInVoidContext( commaExpr->arg1, context, env );
     1449
     1450                        CandidateFinder finder2( context, env );
    14501451                        finder2.find( commaExpr->arg2, ResolvMode::withAdjustment() );
    14511452
     
    14601461
    14611462                void postvisit( const ast::ConstructorExpr * ctorExpr ) {
    1462                         CandidateFinder finder{ symtab, tenv };
     1463                        CandidateFinder finder( context, tenv );
    14631464                        finder.find( ctorExpr->callExpr, ResolvMode::withoutPrune() );
    14641465                        for ( CandidateRef & r : finder.candidates ) {
     
    14691470                void postvisit( const ast::RangeExpr * rangeExpr ) {
    14701471                        // resolve low and high, accept candidates where low and high types unify
    1471                         CandidateFinder finder1{ symtab, tenv };
     1472                        CandidateFinder finder1( context, tenv );
    14721473                        finder1.find( rangeExpr->low, ResolvMode::withAdjustment() );
    14731474                        if ( finder1.candidates.empty() ) return;
    14741475
    1475                         CandidateFinder finder2{ symtab, tenv };
     1476                        CandidateFinder finder2( context, tenv );
    14761477                        finder2.find( rangeExpr->high, ResolvMode::withAdjustment() );
    14771478                        if ( finder2.candidates.empty() ) return;
     
    15491550
    15501551                void postvisit( const ast::UniqueExpr * unqExpr ) {
    1551                         CandidateFinder finder{ symtab, tenv };
     1552                        CandidateFinder finder( context, tenv );
    15521553                        finder.find( unqExpr->expr, ResolvMode::withAdjustment() );
    15531554                        for ( CandidateRef & r : finder.candidates ) {
     
    15581559
    15591560                void postvisit( const ast::StmtExpr * stmtExpr ) {
    1560                         addCandidate( resolveStmtExpr( stmtExpr, symtab ), tenv );
     1561                        addCandidate( resolveStmtExpr( stmtExpr, context ), tenv );
    15611562                }
    15621563
     
    15701571                        for ( const ast::InitAlternative & initAlt : initExpr->initAlts ) {
    15711572                                // calculate target type
    1572                                 const ast::Type * toType = resolveTypeof( initAlt.type, symtab );
     1573                                const ast::Type * toType = resolveTypeof( initAlt.type, context );
    15731574                                // toType = SymTab::validateType( initExpr->location, toType, symtab );
    15741575                                toType = adjustExprType( toType, tenv, symtab );
     
    15761577                                // types are not bound to the initialization type, since return type variables are
    15771578                                // only open for the duration of resolving the UntypedExpr.
    1578                                 CandidateFinder finder{ symtab, tenv, toType };
     1579                                CandidateFinder finder( context, tenv, toType );
    15791580                                finder.find( initExpr->expr, ResolvMode::withAdjustment() );
    15801581                                for ( CandidateRef & cand : finder.candidates ) {
     
    16931694                }
    16941695                else {
    1695                         satisfyAssertions(candidate, localSyms, satisfied, errors);
     1696                        satisfyAssertions(candidate, context.symtab, satisfied, errors);
    16961697                        needRecomputeKey = true;
    16971698                }
     
    18551856                        r->expr = ast::mutate_field(
    18561857                                r->expr.get(), &ast::Expr::result,
    1857                                 adjustExprType( r->expr->result, r->env, localSyms ) );
     1858                                adjustExprType( r->expr->result, r->env, context.symtab ) );
    18581859                }
    18591860        }
     
    18731874
    18741875        for ( const auto & x : xs ) {
    1875                 out.emplace_back( localSyms, env );
     1876                out.emplace_back( context, env );
    18761877                out.back().find( x, ResolvMode::withAdjustment() );
    18771878
  • src/ResolvExpr/CandidateFinder.hpp

    r9d8124f r39d8950  
    1010// Created On       : Wed Jun 5 14:30:00 2019
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Oct  1  9:51:00 2019
    13 // Update Count     : 2
     12// Last Modified On : Wed Mar 16 15:22:00 2022
     13// Update Count     : 3
    1414//
    1515
     
    2525namespace ResolvExpr {
    2626
     27struct ResolveContext;
     28
    2729/// Data to perform expression resolution
    2830struct CandidateFinder {
    2931        CandidateList candidates;          ///< List of candidate resolutions
    30         const ast::SymbolTable & localSyms;   ///< Symbol table to lookup candidates
     32        const ResolveContext & context;  ///< Information about where the canditates are being found.
    3133        const ast::TypeEnvironment & env;  ///< Substitutions performed in this resolution
    3234        ast::ptr< ast::Type > targetType;  ///< Target type for resolution
     
    3436
    3537        CandidateFinder(
    36                 const ast::SymbolTable & syms, const ast::TypeEnvironment & env,
     38                const ResolveContext & context, const ast::TypeEnvironment & env,
    3739                const ast::Type * tt = nullptr )
    38         : candidates(), localSyms( syms ), env( env ), targetType( tt ) {}
     40        : candidates(), context( context ), env( env ), targetType( tt ) {}
    3941
    4042        /// Fill candidates with feasible resolutions for `expr`
  • src/ResolvExpr/CandidatePrinter.cpp

    r9d8124f r39d8950  
    1010// Created On       : Tue Nov  9  9:54:00 2021
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Nov  9 15:47:00 2021
    13 // Update Count     : 0
     12// Last Modified On : Wed Mar 16 13:56:00 2022
     13// Update Count     : 1
    1414//
    1515
     
    2222#include "AST/TranslationUnit.hpp"
    2323#include "ResolvExpr/CandidateFinder.hpp"
     24#include "ResolvExpr/Resolver.h"
    2425
    2526#include <iostream>
     
    2930namespace {
    3031
    31 class CandidatePrintCore : public ast::WithSymbolTable {
     32class CandidatePrintCore : public ast::WithSymbolTable,
     33                public ast::WithConstTranslationUnit {
    3234        std::ostream & os;
    3335public:
     
    3638        void postvisit( const ast::ExprStmt * stmt ) {
    3739                ast::TypeEnvironment env;
    38                 CandidateFinder finder( symtab, env );
     40                CandidateFinder finder( { symtab, transUnit().global }, env );
    3941                finder.find( stmt->expr, ResolvMode::withAdjustment() );
    4042                int count = 1;
  • src/ResolvExpr/ResolveTypeof.cc

    r9d8124f r39d8950  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 12:12:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 19 16:49:04 2015
    13 // Update Count     : 3
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed Mar 16 16:09:00 2022
     13// Update Count     : 4
    1414//
    1515
     
    2222#include "AST/Node.hpp"
    2323#include "AST/Pass.hpp"
     24#include "AST/TranslationUnit.hpp"
    2425#include "AST/Type.hpp"
    2526#include "AST/TypeEnvironment.hpp"
     
    119120namespace {
    120121        struct ResolveTypeof_new : public ast::WithShortCircuiting {
    121                 const ast::SymbolTable & localSymtab;
    122 
    123                 ResolveTypeof_new( const ast::SymbolTable & syms ) : localSymtab( syms ) {}
     122                const ResolveContext & context;
     123
     124                ResolveTypeof_new( const ResolveContext & context ) :
     125                        context( context ) {}
    124126
    125127                void previsit( const ast::TypeofType * ) { visit_children = false; }
     
    137139                                ast::TypeEnvironment dummy;
    138140                                ast::ptr< ast::Expr > newExpr =
    139                                         resolveInVoidContext( typeofType->expr, localSymtab, dummy );
     141                                        resolveInVoidContext( typeofType->expr, context, dummy );
    140142                                assert( newExpr->result && ! newExpr->result->isVoid() );
    141143                                newType = newExpr->result;
     
    161163} // anonymous namespace
    162164
    163 const ast::Type * resolveTypeof( const ast::Type * type , const ast::SymbolTable & symtab ) {
    164         ast::Pass< ResolveTypeof_new > mutator{ symtab };
     165const ast::Type * resolveTypeof( const ast::Type * type , const ResolveContext & context ) {
     166        ast::Pass< ResolveTypeof_new > mutator( context );
    165167        return type->accept( mutator );
    166168}
     
    168170struct FixArrayDimension {
    169171        // should not require a mutable symbol table - prevent pass template instantiation
    170         const ast::SymbolTable & _symtab;
    171         FixArrayDimension(const ast::SymbolTable & symtab): _symtab(symtab) {}
     172        const ResolveContext & context;
     173        FixArrayDimension(const ResolveContext & context) : context( context ) {}
    172174
    173175        const ast::ArrayType * previsit (const ast::ArrayType * arrayType) {
    174176                if (!arrayType->dimension) return arrayType;
    175177                auto mutType = mutate(arrayType);
    176                 ast::ptr<ast::Type> sizetype = ast::sizeType ? ast::sizeType : new ast::BasicType(ast::BasicType::LongUnsignedInt);
    177                 mutType->dimension = findSingleExpression(arrayType->dimension, sizetype, _symtab);
     178                auto globalSizeType = context.global.sizeType;
     179                ast::ptr<ast::Type> sizetype = globalSizeType ? globalSizeType : new ast::BasicType(ast::BasicType::LongUnsignedInt);
     180                mutType->dimension = findSingleExpression(arrayType->dimension, sizetype, context );
    178181
    179182                if (InitTweak::isConstExpr(mutType->dimension)) {
     
    187190};
    188191
    189 const ast::Type * fixArrayType( const ast::Type * type, const ast::SymbolTable & symtab) {
    190         ast::Pass<FixArrayDimension> visitor {symtab};
     192const ast::Type * fixArrayType( const ast::Type * type, const ResolveContext & context ) {
     193        ast::Pass<FixArrayDimension> visitor(context);
    191194        return type->accept(visitor);
    192195}
    193196
    194 const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ast::SymbolTable & symtab ) {
    195         if (!decl->isTypeFixed) { 
     197const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ResolveContext & context ) {
     198        if (!decl->isTypeFixed) {
    196199                auto mutDecl = mutate(decl);
    197                 auto resolvedType = resolveTypeof(decl->type, symtab);
    198                 resolvedType = fixArrayType(resolvedType, symtab);
     200                auto resolvedType = resolveTypeof(decl->type, context);
     201                resolvedType = fixArrayType(resolvedType, context);
    199202                mutDecl->type = resolvedType;
    200203
  • src/ResolvExpr/ResolveTypeof.h

    r9d8124f r39d8950  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 12:14:53 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jul 22 09:38:35 2017
    13 // Update Count     : 3
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed Mar 16 11:33:00 2022
     13// Update Count     : 4
    1414//
    1515
     
    2222namespace ast {
    2323        class Type;
    24         class SymbolTable;
    2524        class ObjectDecl;
    2625}
    2726
    2827namespace ResolvExpr {
     28        struct ResolveContext;
     29
    2930        Type *resolveTypeof( Type*, const SymTab::Indexer &indexer );
    30         const ast::Type * resolveTypeof( const ast::Type *, const ast::SymbolTable & );
    31         const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ast::SymbolTable & symtab );
     31        const ast::Type * resolveTypeof( const ast::Type *, const ResolveContext & );
     32        const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ResolveContext & );
    3233} // namespace ResolvExpr
    3334
  • src/ResolvExpr/Resolver.cc

    r9d8124f r39d8950  
    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 : Wed Mar 16 16:07:00 2022
     13// Update Count     : 246
    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
     
    12431245        : public ast::WithSymbolTable, public ast::WithGuards,
    12441246          public ast::WithVisitorRef<Resolver_new>, public ast::WithShortCircuiting,
    1245           public ast::WithStmtsToAdd<> {
     1247          public ast::WithStmtsToAdd<>, public ast::WithConstTranslationUnit {
    12461248
    12471249                ast::ptr< ast::Type > functionReturn = nullptr;
     
    12511253
    12521254                bool inEnumDecl = false;
     1255
     1256                ResolveContext getContext() const {
     1257                        return ResolveContext{ symtab, transUnit().global };
     1258                }
    12531259
    12541260        public:
     
    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.symtab };
     1315                ast::TranslationUnit transUnit;
     1316                transUnit.global = context.global;
     1317                resolver.core.translationUnit = &transUnit;
    13091318                return ctorInit->accept( resolver );
    13101319        }
    13111320
    13121321        const ast::Expr * resolveStmtExpr(
    1313                 const ast::StmtExpr * stmtExpr, const ast::SymbolTable & symtab
     1322                const ast::StmtExpr * stmtExpr, const ResolveContext & context
    13141323        ) {
    13151324                assert( stmtExpr );
    1316                 ast::Pass< Resolver_new > resolver{ symtab };
     1325                ast::Pass< Resolver_new > resolver{ context.symtab };
     1326                ast::TranslationUnit transUnit;
     1327                transUnit.global = context.global;
     1328                resolver.core.translationUnit = &transUnit;
    13171329                auto ret = mutate(stmtExpr->accept(resolver));
    13181330                strict_dynamic_cast< ast::StmtExpr * >( ret )->computeResult();
     
    13211333
    13221334        namespace {
    1323                 const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ast::SymbolTable & symtab) {
     1335                const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ResolveContext & context) {
    13241336                        std::string name = attr->normalizedName();
    13251337                        if (name == "constructor" || name == "destructor") {
    13261338                                if (attr->params.size() == 1) {
    13271339                                        auto arg = attr->params.front();
    1328                                         auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicType::LongLongSignedInt ), symtab );
     1340                                        auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicType::LongLongSignedInt ), context );
    13291341                                        auto result = eval(arg);
    13301342
     
    13691381
    13701382                        for (auto & attr: mutDecl->attributes) {
    1371                                 attr = handleAttribute(mutDecl->location, attr, symtab);
     1383                                attr = handleAttribute(mutDecl->location, attr, getContext() );
    13721384                        }
    13731385
     
    13821394                        }
    13831395                        for (auto & asst : mutDecl->assertions) {
    1384                                 asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), symtab);
     1396                                asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), getContext());
    13851397                                symtab.addId(asst);
    13861398                                mutType->assertions.emplace_back(new ast::VariableExpr(functionDecl->location, asst));
     
    13941406
    13951407                        for (auto & param : mutDecl->params) {
    1396                                 param = fixObjectType(param.strict_as<ast::ObjectDecl>(), symtab);
     1408                                param = fixObjectType(param.strict_as<ast::ObjectDecl>(), getContext());
    13971409                                symtab.addId(param);
    13981410                                paramTypes.emplace_back(param->get_type());
    13991411                        }
    14001412                        for (auto & ret : mutDecl->returns) {
    1401                                 ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), symtab);
     1413                                ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), getContext());
    14021414                                returnTypes.emplace_back(ret->get_type());
    14031415                        }
     
    14701482                        // enumerator initializers should not use the enum type to initialize, since the
    14711483                        // enum type is still incomplete at this point. Use `int` instead.
    1472                         objectDecl = fixObjectType(objectDecl, symtab);
     1484                        objectDecl = fixObjectType(objectDecl, getContext());
    14731485                        currentObject = ast::CurrentObject{
    14741486                                objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } };
     
    14761488                else {
    14771489                        if (!objectDecl->isTypeFixed) {
    1478                                 auto newDecl = fixObjectType(objectDecl, symtab);
     1490                                auto newDecl = fixObjectType(objectDecl, getContext());
    14791491                                auto mutDecl = mutate(newDecl);
    14801492
     
    15071519                        // nested type decls are hoisted already. no need to do anything
    15081520                        if (auto obj = member.as<ast::ObjectDecl>()) {
    1509                                 member = fixObjectType(obj, symtab);
     1521                                member = fixObjectType(obj, getContext());
    15101522                        }
    15111523                }
     
    15301542                return ast::mutate_field(
    15311543                        assertDecl, &ast::StaticAssertDecl::cond,
    1532                         findIntegralExpression( assertDecl->cond, symtab ) );
     1544                        findIntegralExpression( assertDecl->cond, getContext() ) );
    15331545        }
    15341546
    15351547        template< typename PtrType >
    1536         const PtrType * handlePtrType( const PtrType * type, const ast::SymbolTable & symtab ) {
     1548        const PtrType * handlePtrType( const PtrType * type, const ResolveContext & context ) {
    15371549                if ( type->dimension ) {
    1538                         ast::ptr< ast::Type > sizeType = ast::sizeType;
     1550                        ast::ptr< ast::Type > sizeType = context.global.sizeType;
    15391551                        ast::mutate_field(
    15401552                                type, &PtrType::dimension,
    1541                                 findSingleExpression( type->dimension, sizeType, symtab ) );
     1553                                findSingleExpression( type->dimension, sizeType, context ) );
    15421554                }
    15431555                return type;
     
    15451557
    15461558        const ast::ArrayType * Resolver_new::previsit( const ast::ArrayType * at ) {
    1547                 return handlePtrType( at, symtab );
     1559                return handlePtrType( at, getContext() );
    15481560        }
    15491561
    15501562        const ast::PointerType * Resolver_new::previsit( const ast::PointerType * pt ) {
    1551                 return handlePtrType( pt, symtab );
     1563                return handlePtrType( pt, getContext() );
    15521564        }
    15531565
     
    15571569
    15581570                return ast::mutate_field(
    1559                         exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, symtab ) );
     1571                        exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, getContext() ) );
    15601572        }
    15611573
     
    15641576
    15651577                asmExpr = ast::mutate_field(
    1566                         asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, symtab ) );
     1578                        asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, getContext() ) );
    15671579
    15681580                return asmExpr;
     
    15781590        const ast::IfStmt * Resolver_new::previsit( const ast::IfStmt * ifStmt ) {
    15791591                return ast::mutate_field(
    1580                         ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, symtab ) );
     1592                        ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, getContext() ) );
    15811593        }
    15821594
    15831595        const ast::WhileDoStmt * Resolver_new::previsit( const ast::WhileDoStmt * whileDoStmt ) {
    15841596                return ast::mutate_field(
    1585                         whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, symtab ) );
     1597                        whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, getContext() ) );
    15861598        }
    15871599
     
    15891601                if ( forStmt->cond ) {
    15901602                        forStmt = ast::mutate_field(
    1591                                 forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, symtab ) );
     1603                                forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, getContext() ) );
    15921604                }
    15931605
    15941606                if ( forStmt->inc ) {
    15951607                        forStmt = ast::mutate_field(
    1596                                 forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, symtab ) );
     1608                                forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, getContext() ) );
    15971609                }
    15981610
     
    16041616                switchStmt = ast::mutate_field(
    16051617                        switchStmt, &ast::SwitchStmt::cond,
    1606                         findIntegralExpression( switchStmt->cond, symtab ) );
     1618                        findIntegralExpression( switchStmt->cond, getContext() ) );
    16071619                currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result };
    16081620                return switchStmt;
     
    16171629                        ast::ptr< ast::Expr > untyped =
    16181630                                new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type };
    1619                         ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, symtab );
     1631                        ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, getContext() );
    16201632
    16211633                        // case condition cannot have a cast in C, so it must be removed here, regardless of
     
    16381650                        branchStmt = ast::mutate_field(
    16391651                                branchStmt, &ast::BranchStmt::computedTarget,
    1640                                 findSingleExpression( branchStmt->computedTarget, target, symtab ) );
     1652                                findSingleExpression( branchStmt->computedTarget, target, getContext() ) );
    16411653                }
    16421654                return branchStmt;
     
    16481660                        returnStmt = ast::mutate_field(
    16491661                                returnStmt, &ast::ReturnStmt::expr,
    1650                                 findSingleExpression( returnStmt->expr, functionReturn, symtab ) );
     1662                                findSingleExpression( returnStmt->expr, functionReturn, getContext() ) );
    16511663                }
    16521664                return returnStmt;
     
    16631675                        throwStmt = ast::mutate_field(
    16641676                                throwStmt, &ast::ThrowStmt::expr,
    1665                                 findSingleExpression( throwStmt->expr, exceptType, symtab ) );
     1677                                findSingleExpression( throwStmt->expr, exceptType, getContext() ) );
    16661678                }
    16671679                return throwStmt;
     
    17071719
    17081720                        ast::TypeEnvironment env;
    1709                         CandidateFinder funcFinder{ symtab, env };
     1721                        CandidateFinder funcFinder( getContext(), env );
    17101722
    17111723                        // Find all candidates for a function in canonical form
     
    19211933                                );
    19221934
    1923                                 clause2.target.args.emplace_back( findSingleExpression( init, symtab ) );
     1935                                clause2.target.args.emplace_back( findSingleExpression( init, getContext() ) );
    19241936                        }
    19251937
    19261938                        // Resolve the conditions as if it were an IfStmt, statements normally
    1927                         clause2.cond = findSingleExpression( clause.cond, symtab );
     1939                        clause2.cond = findSingleExpression( clause.cond, getContext() );
    19281940                        clause2.stmt = clause.stmt->accept( *visitor );
    19291941
     
    19401952                        ast::ptr< ast::Type > target =
    19411953                                new ast::BasicType{ ast::BasicType::LongLongUnsignedInt };
    1942                         timeout2.time = findSingleExpression( stmt->timeout.time, target, symtab );
    1943                         timeout2.cond = findSingleExpression( stmt->timeout.cond, symtab );
     1954                        timeout2.time = findSingleExpression( stmt->timeout.time, target, getContext() );
     1955                        timeout2.cond = findSingleExpression( stmt->timeout.cond, getContext() );
    19441956                        timeout2.stmt = stmt->timeout.stmt->accept( *visitor );
    19451957
     
    19541966                        ast::WaitForStmt::OrElse orElse2;
    19551967
    1956                         orElse2.cond = findSingleExpression( stmt->orElse.cond, symtab );
     1968                        orElse2.cond = findSingleExpression( stmt->orElse.cond, getContext() );
    19571969                        orElse2.stmt = stmt->orElse.stmt->accept( *visitor );
    19581970
     
    19751987                for (auto & expr : exprs) {
    19761988                        // only struct- and union-typed expressions are viable candidates
    1977                         expr = findKindExpression( expr, symtab, structOrUnion, "with expression" );
     1989                        expr = findKindExpression( expr, getContext(), structOrUnion, "with expression" );
    19781990
    19791991                        // if with expression might be impure, create a temporary so that it is evaluated once
     
    20012013                ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{
    20022014                        singleInit->location, singleInit->value, currentObject.getOptions() };
    2003                 ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, symtab );
     2015                ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, getContext() );
    20042016                const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >();
    20052017
  • src/ResolvExpr/Resolver.h

    r9d8124f r39d8950  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 12:18:34 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Feb 18 20:40:38 2019
    13 // Update Count     : 4
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed Mar 16 11:32:00 2022
     13// Update Count     : 5
    1414//
    1515
     
    2323class Declaration;
    2424class Expression;
     25class DeletedExpr;
    2526class StmtExpr;
     27class Type;
    2628namespace SymTab {
    2729        class Indexer;
     
    3537        class StmtExpr;
    3638        class SymbolTable;
     39        class TranslationGlobal;
    3740        class TranslationUnit;
    3841        class Type;
     
    5558        void resolveWithExprs( std::list< Declaration * > & translationUnit );
    5659
     60        /// Helper Type: Passes around information between various sub-calls.
     61        struct ResolveContext {
     62                const ast::SymbolTable & symtab;
     63                const ast::TranslationGlobal & global;
     64        };
     65
    5766        /// Checks types and binds syntactic constructs to typed representations
    5867        void resolve( ast::TranslationUnit& translationUnit );
     
    6271        /// context.
    6372        ast::ptr< ast::Expr > resolveInVoidContext(
    64                 const ast::Expr * expr, const ast::SymbolTable & symtab, ast::TypeEnvironment & env );
     73                const ast::Expr * expr, const ResolveContext &, ast::TypeEnvironment & env );
    6574        /// Resolve `untyped` to the single expression whose candidate is the best match for the
    6675        /// given type.
    6776        ast::ptr< ast::Expr > findSingleExpression(
    68                 const ast::Expr * untyped, const ast::Type * type, const ast::SymbolTable & symtab );
     77                const ast::Expr * untyped, const ast::Type * type, const ResolveContext & );
    6978        ast::ptr< ast::Expr > findVoidExpression(
    70                 const ast::Expr * untyped, const ast::SymbolTable & symtab);
     79                const ast::Expr * untyped, const ResolveContext & );
    7180        /// Resolves a constructor init expression
    7281        ast::ptr< ast::Init > resolveCtorInit(
    73                 const ast::ConstructorInit * ctorInit, const ast::SymbolTable & symtab );
     82                const ast::ConstructorInit * ctorInit, const ResolveContext & context );
    7483        /// Resolves a statement expression
    7584        const ast::Expr * resolveStmtExpr(
    76                 const ast::StmtExpr * stmtExpr, const ast::SymbolTable & symtab );
     85                const ast::StmtExpr * stmtExpr, const ResolveContext & context );
    7786} // namespace ResolvExpr
    7887
Note: See TracChangeset for help on using the changeset viewer.