Changeset 39d8950 for src/ResolvExpr
- Timestamp:
- Mar 16, 2022, 4:41:01 PM (2 years ago)
- Branches:
- ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
- Children:
- f7496c5
- Parents:
- 9d8124f
- Location:
- src/ResolvExpr
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/CandidateFinder.cpp
r9d8124f r39d8950 10 10 // Created On : Wed Jun 5 14:30:00 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Oct 1 14:55:00 201913 // Update Count : 212 // Last Modified On : Wed Mar 16 11:58:00 2022 13 // Update Count : 3 14 14 // 15 15 … … 595 595 /// Actually visits expressions to find their candidate interpretations 596 596 class Finder final : public ast::WithShortCircuiting { 597 const ResolveContext & context; 597 598 const ast::SymbolTable & symtab; 598 599 public: … … 618 619 619 620 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 ) {} 622 623 623 624 void previsit( const ast::Node * ) { visit_children = false; } … … 872 873 Tuples::handleTupleAssignment( selfFinder, untypedExpr, argCandidates ); 873 874 874 CandidateFinder funcFinder { symtab, tenv };875 CandidateFinder funcFinder( context, tenv ); 875 876 if (auto nameExpr = untypedExpr->func.as<ast::NameExpr>()) { 876 877 auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name); … … 918 919 // find function operators 919 920 ast::ptr< ast::Expr > opExpr = new ast::NameExpr{ untypedExpr->location, "?()" }; 920 CandidateFinder opFinder { symtab, tenv };921 CandidateFinder opFinder( context, tenv ); 921 922 // okay if there aren't any function operations 922 923 opFinder.find( opExpr, ResolvMode::withoutFailFast() ); … … 1059 1060 1060 1061 void postvisit( const ast::AddressExpr * addressExpr ) { 1061 CandidateFinder finder { symtab, tenv };1062 CandidateFinder finder( context, tenv ); 1062 1063 finder.find( addressExpr->arg ); 1063 1064 … … 1079 1080 ast::ptr< ast::Type > toType = castExpr->result; 1080 1081 assert( toType ); 1081 toType = resolveTypeof( toType, symtab);1082 toType = resolveTypeof( toType, context ); 1082 1083 // toType = SymTab::validateType( castExpr->location, toType, symtab ); 1083 1084 toType = adjustExprType( toType, tenv, symtab ); 1084 1085 1085 CandidateFinder finder { symtab, tenv, toType };1086 CandidateFinder finder( context, tenv, toType ); 1086 1087 finder.find( castExpr->arg, ResolvMode::withAdjustment() ); 1087 1088 … … 1136 1137 void postvisit( const ast::VirtualCastExpr * castExpr ) { 1137 1138 assertf( castExpr->result, "Implicit virtual cast targets not yet supported." ); 1138 CandidateFinder finder { symtab, tenv };1139 CandidateFinder finder( context, tenv ); 1139 1140 // don't prune here, all alternatives guaranteed to have same type 1140 1141 finder.find( castExpr->arg, ResolvMode::withoutPrune() ); … … 1153 1154 auto target = inst->base.get(); 1154 1155 1155 CandidateFinder finder { symtab, tenv };1156 CandidateFinder finder( context, tenv ); 1156 1157 1157 1158 auto pick_alternatives = [target, this](CandidateList & found, bool expect_ref) { … … 1202 1203 1203 1204 void postvisit( const ast::UntypedMemberExpr * memberExpr ) { 1204 CandidateFinder aggFinder { symtab, tenv };1205 CandidateFinder aggFinder( context, tenv ); 1205 1206 aggFinder.find( memberExpr->aggregate, ResolvMode::withAdjustment() ); 1206 1207 for ( CandidateRef & agg : aggFinder.candidates ) { … … 1287 1288 addCandidate( 1288 1289 new ast::SizeofExpr{ 1289 sizeofExpr->location, resolveTypeof( sizeofExpr->type, symtab) },1290 sizeofExpr->location, resolveTypeof( sizeofExpr->type, context ) }, 1290 1291 tenv ); 1291 1292 } else { 1292 1293 // find all candidates for the argument to sizeof 1293 CandidateFinder finder { symtab, tenv };1294 CandidateFinder finder( context, tenv ); 1294 1295 finder.find( sizeofExpr->expr ); 1295 1296 // find the lowest-cost candidate, otherwise ambiguous … … 1311 1312 addCandidate( 1312 1313 new ast::AlignofExpr{ 1313 alignofExpr->location, resolveTypeof( alignofExpr->type, symtab) },1314 alignofExpr->location, resolveTypeof( alignofExpr->type, context ) }, 1314 1315 tenv ); 1315 1316 } else { 1316 1317 // find all candidates for the argument to alignof 1317 CandidateFinder finder { symtab, tenv };1318 CandidateFinder finder( context, tenv ); 1318 1319 finder.find( alignofExpr->expr ); 1319 1320 // find the lowest-cost candidate, otherwise ambiguous … … 1354 1355 1355 1356 void postvisit( const ast::LogicalExpr * logicalExpr ) { 1356 CandidateFinder finder1 { symtab, tenv };1357 CandidateFinder finder1( context, tenv ); 1357 1358 finder1.find( logicalExpr->arg1, ResolvMode::withAdjustment() ); 1358 1359 if ( finder1.candidates.empty() ) return; 1359 1360 1360 CandidateFinder finder2 { symtab, tenv };1361 CandidateFinder finder2( context, tenv ); 1361 1362 finder2.find( logicalExpr->arg2, ResolvMode::withAdjustment() ); 1362 1363 if ( finder2.candidates.empty() ) return; … … 1384 1385 void postvisit( const ast::ConditionalExpr * conditionalExpr ) { 1385 1386 // candidates for condition 1386 CandidateFinder finder1 { symtab, tenv };1387 CandidateFinder finder1( context, tenv ); 1387 1388 finder1.find( conditionalExpr->arg1, ResolvMode::withAdjustment() ); 1388 1389 if ( finder1.candidates.empty() ) return; 1389 1390 1390 1391 // candidates for true result 1391 CandidateFinder finder2 { symtab, tenv };1392 CandidateFinder finder2( context, tenv ); 1392 1393 finder2.find( conditionalExpr->arg2, ResolvMode::withAdjustment() ); 1393 1394 if ( finder2.candidates.empty() ) return; 1394 1395 1395 1396 // candidates for false result 1396 CandidateFinder finder3 { symtab, tenv };1397 CandidateFinder finder3( context, tenv ); 1397 1398 finder3.find( conditionalExpr->arg3, ResolvMode::withAdjustment() ); 1398 1399 if ( finder3.candidates.empty() ) return; … … 1445 1446 void postvisit( const ast::CommaExpr * commaExpr ) { 1446 1447 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 ); 1450 1451 finder2.find( commaExpr->arg2, ResolvMode::withAdjustment() ); 1451 1452 … … 1460 1461 1461 1462 void postvisit( const ast::ConstructorExpr * ctorExpr ) { 1462 CandidateFinder finder { symtab, tenv };1463 CandidateFinder finder( context, tenv ); 1463 1464 finder.find( ctorExpr->callExpr, ResolvMode::withoutPrune() ); 1464 1465 for ( CandidateRef & r : finder.candidates ) { … … 1469 1470 void postvisit( const ast::RangeExpr * rangeExpr ) { 1470 1471 // resolve low and high, accept candidates where low and high types unify 1471 CandidateFinder finder1 { symtab, tenv };1472 CandidateFinder finder1( context, tenv ); 1472 1473 finder1.find( rangeExpr->low, ResolvMode::withAdjustment() ); 1473 1474 if ( finder1.candidates.empty() ) return; 1474 1475 1475 CandidateFinder finder2 { symtab, tenv };1476 CandidateFinder finder2( context, tenv ); 1476 1477 finder2.find( rangeExpr->high, ResolvMode::withAdjustment() ); 1477 1478 if ( finder2.candidates.empty() ) return; … … 1549 1550 1550 1551 void postvisit( const ast::UniqueExpr * unqExpr ) { 1551 CandidateFinder finder { symtab, tenv };1552 CandidateFinder finder( context, tenv ); 1552 1553 finder.find( unqExpr->expr, ResolvMode::withAdjustment() ); 1553 1554 for ( CandidateRef & r : finder.candidates ) { … … 1558 1559 1559 1560 void postvisit( const ast::StmtExpr * stmtExpr ) { 1560 addCandidate( resolveStmtExpr( stmtExpr, symtab), tenv );1561 addCandidate( resolveStmtExpr( stmtExpr, context ), tenv ); 1561 1562 } 1562 1563 … … 1570 1571 for ( const ast::InitAlternative & initAlt : initExpr->initAlts ) { 1571 1572 // calculate target type 1572 const ast::Type * toType = resolveTypeof( initAlt.type, symtab);1573 const ast::Type * toType = resolveTypeof( initAlt.type, context ); 1573 1574 // toType = SymTab::validateType( initExpr->location, toType, symtab ); 1574 1575 toType = adjustExprType( toType, tenv, symtab ); … … 1576 1577 // types are not bound to the initialization type, since return type variables are 1577 1578 // only open for the duration of resolving the UntypedExpr. 1578 CandidateFinder finder { symtab, tenv, toType };1579 CandidateFinder finder( context, tenv, toType ); 1579 1580 finder.find( initExpr->expr, ResolvMode::withAdjustment() ); 1580 1581 for ( CandidateRef & cand : finder.candidates ) { … … 1693 1694 } 1694 1695 else { 1695 satisfyAssertions(candidate, localSyms, satisfied, errors);1696 satisfyAssertions(candidate, context.symtab, satisfied, errors); 1696 1697 needRecomputeKey = true; 1697 1698 } … … 1855 1856 r->expr = ast::mutate_field( 1856 1857 r->expr.get(), &ast::Expr::result, 1857 adjustExprType( r->expr->result, r->env, localSyms) );1858 adjustExprType( r->expr->result, r->env, context.symtab ) ); 1858 1859 } 1859 1860 } … … 1873 1874 1874 1875 for ( const auto & x : xs ) { 1875 out.emplace_back( localSyms, env );1876 out.emplace_back( context, env ); 1876 1877 out.back().find( x, ResolvMode::withAdjustment() ); 1877 1878 -
src/ResolvExpr/CandidateFinder.hpp
r9d8124f r39d8950 10 10 // Created On : Wed Jun 5 14:30:00 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Oct 1 9:51:00 201913 // Update Count : 212 // Last Modified On : Wed Mar 16 15:22:00 2022 13 // Update Count : 3 14 14 // 15 15 … … 25 25 namespace ResolvExpr { 26 26 27 struct ResolveContext; 28 27 29 /// Data to perform expression resolution 28 30 struct CandidateFinder { 29 31 CandidateList candidates; ///< List of candidate resolutions 30 const ast::SymbolTable & localSyms; ///< Symbol table to lookup candidates32 const ResolveContext & context; ///< Information about where the canditates are being found. 31 33 const ast::TypeEnvironment & env; ///< Substitutions performed in this resolution 32 34 ast::ptr< ast::Type > targetType; ///< Target type for resolution … … 34 36 35 37 CandidateFinder( 36 const ast::SymbolTable & syms, const ast::TypeEnvironment & env,38 const ResolveContext & context, const ast::TypeEnvironment & env, 37 39 const ast::Type * tt = nullptr ) 38 : candidates(), localSyms( syms), env( env ), targetType( tt ) {}40 : candidates(), context( context ), env( env ), targetType( tt ) {} 39 41 40 42 /// Fill candidates with feasible resolutions for `expr` -
src/ResolvExpr/CandidatePrinter.cpp
r9d8124f r39d8950 10 10 // Created On : Tue Nov 9 9:54:00 2021 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Nov 9 15:47:00 202113 // Update Count : 012 // Last Modified On : Wed Mar 16 13:56:00 2022 13 // Update Count : 1 14 14 // 15 15 … … 22 22 #include "AST/TranslationUnit.hpp" 23 23 #include "ResolvExpr/CandidateFinder.hpp" 24 #include "ResolvExpr/Resolver.h" 24 25 25 26 #include <iostream> … … 29 30 namespace { 30 31 31 class CandidatePrintCore : public ast::WithSymbolTable { 32 class CandidatePrintCore : public ast::WithSymbolTable, 33 public ast::WithConstTranslationUnit { 32 34 std::ostream & os; 33 35 public: … … 36 38 void postvisit( const ast::ExprStmt * stmt ) { 37 39 ast::TypeEnvironment env; 38 CandidateFinder finder( symtab, env );40 CandidateFinder finder( { symtab, transUnit().global }, env ); 39 41 finder.find( stmt->expr, ResolvMode::withAdjustment() ); 40 42 int count = 1; -
src/ResolvExpr/ResolveTypeof.cc
r9d8124f r39d8950 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:12:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue May 19 16:49:04 201513 // Update Count : 311 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Mar 16 16:09:00 2022 13 // Update Count : 4 14 14 // 15 15 … … 22 22 #include "AST/Node.hpp" 23 23 #include "AST/Pass.hpp" 24 #include "AST/TranslationUnit.hpp" 24 25 #include "AST/Type.hpp" 25 26 #include "AST/TypeEnvironment.hpp" … … 119 120 namespace { 120 121 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 ) {} 124 126 125 127 void previsit( const ast::TypeofType * ) { visit_children = false; } … … 137 139 ast::TypeEnvironment dummy; 138 140 ast::ptr< ast::Expr > newExpr = 139 resolveInVoidContext( typeofType->expr, localSymtab, dummy );141 resolveInVoidContext( typeofType->expr, context, dummy ); 140 142 assert( newExpr->result && ! newExpr->result->isVoid() ); 141 143 newType = newExpr->result; … … 161 163 } // anonymous namespace 162 164 163 const ast::Type * resolveTypeof( const ast::Type * type , const ast::SymbolTable & symtab) {164 ast::Pass< ResolveTypeof_new > mutator { symtab };165 const ast::Type * resolveTypeof( const ast::Type * type , const ResolveContext & context ) { 166 ast::Pass< ResolveTypeof_new > mutator( context ); 165 167 return type->accept( mutator ); 166 168 } … … 168 170 struct FixArrayDimension { 169 171 // 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 ) {} 172 174 173 175 const ast::ArrayType * previsit (const ast::ArrayType * arrayType) { 174 176 if (!arrayType->dimension) return arrayType; 175 177 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 ); 178 181 179 182 if (InitTweak::isConstExpr(mutType->dimension)) { … … 187 190 }; 188 191 189 const ast::Type * fixArrayType( const ast::Type * type, const ast::SymbolTable & symtab) {190 ast::Pass<FixArrayDimension> visitor {symtab};192 const ast::Type * fixArrayType( const ast::Type * type, const ResolveContext & context ) { 193 ast::Pass<FixArrayDimension> visitor(context); 191 194 return type->accept(visitor); 192 195 } 193 196 194 const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ast::SymbolTable & symtab) {195 if (!decl->isTypeFixed) { 197 const ast::ObjectDecl * fixObjectType( const ast::ObjectDecl * decl , const ResolveContext & context ) { 198 if (!decl->isTypeFixed) { 196 199 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); 199 202 mutDecl->type = resolvedType; 200 203 -
src/ResolvExpr/ResolveTypeof.h
r9d8124f r39d8950 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:14:53 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sat Jul 22 09:38:35 201713 // Update Count : 311 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Mar 16 11:33:00 2022 13 // Update Count : 4 14 14 // 15 15 … … 22 22 namespace ast { 23 23 class Type; 24 class SymbolTable;25 24 class ObjectDecl; 26 25 } 27 26 28 27 namespace ResolvExpr { 28 struct ResolveContext; 29 29 30 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 & ); 32 33 } // namespace ResolvExpr 33 34 -
src/ResolvExpr/Resolver.cc
r9d8124f r39d8950 9 9 // Author : Aaron B. Moss 10 10 // Created On : Sun May 17 12:17:01 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Feb 1 16:27:14202213 // Update Count : 24 511 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Mar 16 16:07:00 2022 13 // Update Count : 246 14 14 // 15 15 … … 997 997 /// Calls the CandidateFinder and finds the single best candidate 998 998 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, 1000 1000 std::function<bool(const Candidate &)> pred = anyCandidate, ResolvMode mode = {} 1001 1001 ) { … … 1007 1007 ++recursion_level; 1008 1008 ast::TypeEnvironment env; 1009 CandidateFinder finder { symtab, env };1009 CandidateFinder finder( context, env ); 1010 1010 finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); 1011 1011 --recursion_level; … … 1129 1129 1130 1130 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 1132 1133 ) { 1133 1134 assertf( expr, "expected a non-null expression" ); … … 1136 1137 ast::ptr< ast::CastExpr > untyped = new ast::CastExpr{ expr }; 1137 1138 CandidateRef choice = findUnfinishedKindExpression( 1138 untyped, symtab, "", anyCandidate, ResolvMode::withAdjustment() );1139 untyped, context, "", anyCandidate, ResolvMode::withAdjustment() ); 1139 1140 1140 1141 // a cast expression has either 0 or 1 interpretations (by language rules); … … 1149 1150 /// context. 1150 1151 ast::ptr< ast::Expr > findVoidExpression( 1151 const ast::Expr * untyped, const ast::SymbolTable & symtab1152 const ast::Expr * untyped, const ResolveContext & context 1152 1153 ) { 1153 1154 ast::TypeEnvironment env; 1154 ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, symtab, env );1155 ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, context, env ); 1155 1156 finishExpr( newExpr, env, untyped->env ); 1156 1157 return newExpr; … … 1163 1164 /// lowest cost, returning the resolved version 1164 1165 ast::ptr< ast::Expr > findKindExpression( 1165 const ast::Expr * untyped, const ast::SymbolTable & symtab,1166 const ast::Expr * untyped, const ResolveContext & context, 1166 1167 std::function<bool(const Candidate &)> pred = anyCandidate, 1167 1168 const std::string & kind = "", ResolvMode mode = {} … … 1169 1170 if ( ! untyped ) return {}; 1170 1171 CandidateRef choice = 1171 findUnfinishedKindExpression( untyped, symtab, kind, pred, mode );1172 findUnfinishedKindExpression( untyped, context, kind, pred, mode ); 1172 1173 ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env ); 1173 1174 return std::move( choice->expr ); … … 1176 1177 /// Resolve `untyped` to the single expression whose candidate is the best match 1177 1178 ast::ptr< ast::Expr > findSingleExpression( 1178 const ast::Expr * untyped, const ast::SymbolTable & symtab1179 const ast::Expr * untyped, const ResolveContext & context 1179 1180 ) { 1180 1181 Stats::ResolveTime::start( untyped ); 1181 auto res = findKindExpression( untyped, symtab);1182 auto res = findKindExpression( untyped, context ); 1182 1183 Stats::ResolveTime::stop(); 1183 1184 return res; … … 1186 1187 1187 1188 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 1189 1191 ) { 1190 1192 assert( untyped && type ); 1191 1193 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 ); 1194 1196 return newExpr; 1195 1197 } … … 1217 1219 /// Resolve `untyped` as an integral expression, returning the resolved version 1218 1220 ast::ptr< ast::Expr > findIntegralExpression( 1219 const ast::Expr * untyped, const ast::SymbolTable & symtab1221 const ast::Expr * untyped, const ResolveContext & context 1220 1222 ) { 1221 return findKindExpression( untyped, symtab, hasIntegralType, "condition" );1223 return findKindExpression( untyped, context, hasIntegralType, "condition" ); 1222 1224 } 1223 1225 … … 1243 1245 : public ast::WithSymbolTable, public ast::WithGuards, 1244 1246 public ast::WithVisitorRef<Resolver_new>, public ast::WithShortCircuiting, 1245 public ast::WithStmtsToAdd<> {1247 public ast::WithStmtsToAdd<>, public ast::WithConstTranslationUnit { 1246 1248 1247 1249 ast::ptr< ast::Type > functionReturn = nullptr; … … 1251 1253 1252 1254 bool inEnumDecl = false; 1255 1256 ResolveContext getContext() const { 1257 return ResolveContext{ symtab, transUnit().global }; 1258 } 1253 1259 1254 1260 public: … … 1303 1309 1304 1310 ast::ptr< ast::Init > resolveCtorInit( 1305 const ast::ConstructorInit * ctorInit, const ast::SymbolTable & symtab1311 const ast::ConstructorInit * ctorInit, const ResolveContext & context 1306 1312 ) { 1307 1313 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; 1309 1318 return ctorInit->accept( resolver ); 1310 1319 } 1311 1320 1312 1321 const ast::Expr * resolveStmtExpr( 1313 const ast::StmtExpr * stmtExpr, const ast::SymbolTable & symtab1322 const ast::StmtExpr * stmtExpr, const ResolveContext & context 1314 1323 ) { 1315 1324 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; 1317 1329 auto ret = mutate(stmtExpr->accept(resolver)); 1318 1330 strict_dynamic_cast< ast::StmtExpr * >( ret )->computeResult(); … … 1321 1333 1322 1334 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) { 1324 1336 std::string name = attr->normalizedName(); 1325 1337 if (name == "constructor" || name == "destructor") { 1326 1338 if (attr->params.size() == 1) { 1327 1339 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 ); 1329 1341 auto result = eval(arg); 1330 1342 … … 1369 1381 1370 1382 for (auto & attr: mutDecl->attributes) { 1371 attr = handleAttribute(mutDecl->location, attr, symtab);1383 attr = handleAttribute(mutDecl->location, attr, getContext() ); 1372 1384 } 1373 1385 … … 1382 1394 } 1383 1395 for (auto & asst : mutDecl->assertions) { 1384 asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), symtab);1396 asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), getContext()); 1385 1397 symtab.addId(asst); 1386 1398 mutType->assertions.emplace_back(new ast::VariableExpr(functionDecl->location, asst)); … … 1394 1406 1395 1407 for (auto & param : mutDecl->params) { 1396 param = fixObjectType(param.strict_as<ast::ObjectDecl>(), symtab);1408 param = fixObjectType(param.strict_as<ast::ObjectDecl>(), getContext()); 1397 1409 symtab.addId(param); 1398 1410 paramTypes.emplace_back(param->get_type()); 1399 1411 } 1400 1412 for (auto & ret : mutDecl->returns) { 1401 ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), symtab);1413 ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), getContext()); 1402 1414 returnTypes.emplace_back(ret->get_type()); 1403 1415 } … … 1470 1482 // enumerator initializers should not use the enum type to initialize, since the 1471 1483 // enum type is still incomplete at this point. Use `int` instead. 1472 objectDecl = fixObjectType(objectDecl, symtab);1484 objectDecl = fixObjectType(objectDecl, getContext()); 1473 1485 currentObject = ast::CurrentObject{ 1474 1486 objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } }; … … 1476 1488 else { 1477 1489 if (!objectDecl->isTypeFixed) { 1478 auto newDecl = fixObjectType(objectDecl, symtab);1490 auto newDecl = fixObjectType(objectDecl, getContext()); 1479 1491 auto mutDecl = mutate(newDecl); 1480 1492 … … 1507 1519 // nested type decls are hoisted already. no need to do anything 1508 1520 if (auto obj = member.as<ast::ObjectDecl>()) { 1509 member = fixObjectType(obj, symtab);1521 member = fixObjectType(obj, getContext()); 1510 1522 } 1511 1523 } … … 1530 1542 return ast::mutate_field( 1531 1543 assertDecl, &ast::StaticAssertDecl::cond, 1532 findIntegralExpression( assertDecl->cond, symtab) );1544 findIntegralExpression( assertDecl->cond, getContext() ) ); 1533 1545 } 1534 1546 1535 1547 template< typename PtrType > 1536 const PtrType * handlePtrType( const PtrType * type, const ast::SymbolTable & symtab) {1548 const PtrType * handlePtrType( const PtrType * type, const ResolveContext & context ) { 1537 1549 if ( type->dimension ) { 1538 ast::ptr< ast::Type > sizeType = ast::sizeType;1550 ast::ptr< ast::Type > sizeType = context.global.sizeType; 1539 1551 ast::mutate_field( 1540 1552 type, &PtrType::dimension, 1541 findSingleExpression( type->dimension, sizeType, symtab) );1553 findSingleExpression( type->dimension, sizeType, context ) ); 1542 1554 } 1543 1555 return type; … … 1545 1557 1546 1558 const ast::ArrayType * Resolver_new::previsit( const ast::ArrayType * at ) { 1547 return handlePtrType( at, symtab);1559 return handlePtrType( at, getContext() ); 1548 1560 } 1549 1561 1550 1562 const ast::PointerType * Resolver_new::previsit( const ast::PointerType * pt ) { 1551 return handlePtrType( pt, symtab);1563 return handlePtrType( pt, getContext() ); 1552 1564 } 1553 1565 … … 1557 1569 1558 1570 return ast::mutate_field( 1559 exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, symtab) );1571 exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, getContext() ) ); 1560 1572 } 1561 1573 … … 1564 1576 1565 1577 asmExpr = ast::mutate_field( 1566 asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, symtab) );1578 asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, getContext() ) ); 1567 1579 1568 1580 return asmExpr; … … 1578 1590 const ast::IfStmt * Resolver_new::previsit( const ast::IfStmt * ifStmt ) { 1579 1591 return ast::mutate_field( 1580 ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, symtab) );1592 ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, getContext() ) ); 1581 1593 } 1582 1594 1583 1595 const ast::WhileDoStmt * Resolver_new::previsit( const ast::WhileDoStmt * whileDoStmt ) { 1584 1596 return ast::mutate_field( 1585 whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, symtab) );1597 whileDoStmt, &ast::WhileDoStmt::cond, findIntegralExpression( whileDoStmt->cond, getContext() ) ); 1586 1598 } 1587 1599 … … 1589 1601 if ( forStmt->cond ) { 1590 1602 forStmt = ast::mutate_field( 1591 forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, symtab) );1603 forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, getContext() ) ); 1592 1604 } 1593 1605 1594 1606 if ( forStmt->inc ) { 1595 1607 forStmt = ast::mutate_field( 1596 forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, symtab) );1608 forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, getContext() ) ); 1597 1609 } 1598 1610 … … 1604 1616 switchStmt = ast::mutate_field( 1605 1617 switchStmt, &ast::SwitchStmt::cond, 1606 findIntegralExpression( switchStmt->cond, symtab) );1618 findIntegralExpression( switchStmt->cond, getContext() ) ); 1607 1619 currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result }; 1608 1620 return switchStmt; … … 1617 1629 ast::ptr< ast::Expr > untyped = 1618 1630 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() ); 1620 1632 1621 1633 // case condition cannot have a cast in C, so it must be removed here, regardless of … … 1638 1650 branchStmt = ast::mutate_field( 1639 1651 branchStmt, &ast::BranchStmt::computedTarget, 1640 findSingleExpression( branchStmt->computedTarget, target, symtab) );1652 findSingleExpression( branchStmt->computedTarget, target, getContext() ) ); 1641 1653 } 1642 1654 return branchStmt; … … 1648 1660 returnStmt = ast::mutate_field( 1649 1661 returnStmt, &ast::ReturnStmt::expr, 1650 findSingleExpression( returnStmt->expr, functionReturn, symtab) );1662 findSingleExpression( returnStmt->expr, functionReturn, getContext() ) ); 1651 1663 } 1652 1664 return returnStmt; … … 1663 1675 throwStmt = ast::mutate_field( 1664 1676 throwStmt, &ast::ThrowStmt::expr, 1665 findSingleExpression( throwStmt->expr, exceptType, symtab) );1677 findSingleExpression( throwStmt->expr, exceptType, getContext() ) ); 1666 1678 } 1667 1679 return throwStmt; … … 1707 1719 1708 1720 ast::TypeEnvironment env; 1709 CandidateFinder funcFinder { symtab, env };1721 CandidateFinder funcFinder( getContext(), env ); 1710 1722 1711 1723 // Find all candidates for a function in canonical form … … 1921 1933 ); 1922 1934 1923 clause2.target.args.emplace_back( findSingleExpression( init, symtab) );1935 clause2.target.args.emplace_back( findSingleExpression( init, getContext() ) ); 1924 1936 } 1925 1937 1926 1938 // 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() ); 1928 1940 clause2.stmt = clause.stmt->accept( *visitor ); 1929 1941 … … 1940 1952 ast::ptr< ast::Type > target = 1941 1953 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() ); 1944 1956 timeout2.stmt = stmt->timeout.stmt->accept( *visitor ); 1945 1957 … … 1954 1966 ast::WaitForStmt::OrElse orElse2; 1955 1967 1956 orElse2.cond = findSingleExpression( stmt->orElse.cond, symtab);1968 orElse2.cond = findSingleExpression( stmt->orElse.cond, getContext() ); 1957 1969 orElse2.stmt = stmt->orElse.stmt->accept( *visitor ); 1958 1970 … … 1975 1987 for (auto & expr : exprs) { 1976 1988 // 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" ); 1978 1990 1979 1991 // if with expression might be impure, create a temporary so that it is evaluated once … … 2001 2013 ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{ 2002 2014 singleInit->location, singleInit->value, currentObject.getOptions() }; 2003 ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, symtab);2015 ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, getContext() ); 2004 2016 const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >(); 2005 2017 -
src/ResolvExpr/Resolver.h
r9d8124f r39d8950 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:18:34 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Mon Feb 18 20:40:38 201913 // Update Count : 411 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Mar 16 11:32:00 2022 13 // Update Count : 5 14 14 // 15 15 … … 23 23 class Declaration; 24 24 class Expression; 25 class DeletedExpr; 25 26 class StmtExpr; 27 class Type; 26 28 namespace SymTab { 27 29 class Indexer; … … 35 37 class StmtExpr; 36 38 class SymbolTable; 39 class TranslationGlobal; 37 40 class TranslationUnit; 38 41 class Type; … … 55 58 void resolveWithExprs( std::list< Declaration * > & translationUnit ); 56 59 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 57 66 /// Checks types and binds syntactic constructs to typed representations 58 67 void resolve( ast::TranslationUnit& translationUnit ); … … 62 71 /// context. 63 72 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 ); 65 74 /// Resolve `untyped` to the single expression whose candidate is the best match for the 66 75 /// given type. 67 76 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 & ); 69 78 ast::ptr< ast::Expr > findVoidExpression( 70 const ast::Expr * untyped, const ast::SymbolTable & symtab);79 const ast::Expr * untyped, const ResolveContext & ); 71 80 /// Resolves a constructor init expression 72 81 ast::ptr< ast::Init > resolveCtorInit( 73 const ast::ConstructorInit * ctorInit, const ast::SymbolTable & symtab);82 const ast::ConstructorInit * ctorInit, const ResolveContext & context ); 74 83 /// Resolves a statement expression 75 84 const ast::Expr * resolveStmtExpr( 76 const ast::StmtExpr * stmtExpr, const ast::SymbolTable & symtab);85 const ast::StmtExpr * stmtExpr, const ResolveContext & context ); 77 86 } // namespace ResolvExpr 78 87
Note: See TracChangeset
for help on using the changeset viewer.