Changeset a55ebcc
- Timestamp:
- Feb 5, 2024, 2:17:33 AM (3 months ago)
- Branches:
- master
- Children:
- 47bd204
- Parents:
- 020fa10
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CompilationState.cc
r020fa10 ra55ebcc 43 43 codegenp = false, 44 44 prettycodegenp = false, 45 linemarks = false; 45 linemarks = false, 46 reppseu = false; 46 47 47 48 // Local Variables: // -
src/CompilationState.h
r020fa10 ra55ebcc 42 42 codegenp, 43 43 prettycodegenp, 44 linemarks; 44 linemarks, 45 reppseu; 45 46 46 47 // is the compiler building prelude or libcfa? -
src/ResolvExpr/CandidateFinder.cpp
r020fa10 ra55ebcc 672 672 void postvisit( const ast::StmtExpr * stmtExpr ); 673 673 void postvisit( const ast::UntypedInitExpr * initExpr ); 674 void postvisit( const ast::QualifiedNameExpr * qualifiedExpr ); 674 675 675 676 void postvisit( const ast::InitExpr * ) { … … 890 891 } else if ( auto unionInst = aggrExpr->result.as< ast::UnionInstType >() ) { 891 892 addAggMembers( unionInst, aggrExpr, *cand, Cost::unsafe, "" ); 892 } else if ( auto enumInst = aggrExpr->result.as< ast::EnumInstType >() ) { 893 } 894 else if ( auto enumInst = aggrExpr->result.as< ast::EnumInstType >() ) { 893 895 // The Attribute Arrays are not yet generated, need to proxy them 894 896 // as attribute function call … … 1401 1403 void Finder::postvisit( const ast::VariableExpr * variableExpr ) { 1402 1404 // not sufficient to just pass `variableExpr` here, type might have changed since 1403 // creation 1404 if ( auto obj = dynamic_cast<const ast::ObjectDecl *>( variableExpr->var.get() )) { 1405 if ( auto enumInstType = dynamic_cast<const ast::EnumInstType *>( obj->type.get() ) ) { 1406 if ( enumInstType->base && enumInstType->base->base ) { 1407 const CodeLocation & location = variableExpr->location; 1408 auto ids = symtab.lookupId( "valueE" ); 1409 for ( ast::SymbolTable::IdData & id : ids ) { 1410 if ( auto func = id.id.as<ast::FunctionDecl>() ) { 1411 if ( func->params.size() == 1 ) { 1412 ast::ptr<ast::DeclWithType> valueEParam = func->params.front(); 1413 auto valueEParamType = valueEParam->get_type(); 1414 ast::OpenVarSet funcOpen; 1415 ast::AssertionSet funcNeed, funcHave; 1416 ast::TypeEnvironment funcEnv{ tenv }; 1417 ast::ptr<ast::Type> common; 1418 if ( unifyInexact( valueEParamType, enumInstType, funcEnv, funcNeed, funcHave, funcOpen, WidenMode{ true, true }, common ) ) { 1419 auto appl = new ast::ApplicationExpr( location, 1420 ast::VariableExpr::functionPointer( location, func), { variableExpr } ); 1421 // addCandidate( appl, copy( tenv ), ); 1422 Candidate cand {appl, copy( tenv )}; 1423 addCandidate( cand, appl, Cost::safe ); 1424 } 1425 } 1426 } 1427 } 1428 } 1429 1430 } 1431 } 1432 addCandidate( variableExpr, tenv ); 1433 1405 addCandidate( variableExpr, tenv ); 1434 1406 } 1435 1407 … … 1807 1779 } 1808 1780 1781 void Finder::postvisit( const ast::QualifiedNameExpr * expr ) { 1782 std::vector< ast::SymbolTable::IdData > declList = symtab.lookupId( expr->name ); 1783 if ( declList.empty() ) return; 1784 1785 for ( ast::SymbolTable::IdData & data: declList ) { 1786 const ast::Type * t = data.id->get_type()->stripReferences(); 1787 if ( const ast::EnumInstType * enumInstType = 1788 dynamic_cast<const ast::EnumInstType *>( t ) ) { 1789 if ( enumInstType->base->name == expr->type_decl->name ) { 1790 Cost cost = Cost::zero; 1791 ast::Expr * newExpr = data.combine( expr->location, cost ); 1792 CandidateRef newCand = 1793 std::make_shared<Candidate>( 1794 newExpr, copy( tenv ), ast::OpenVarSet{}, 1795 ast::AssertionSet{}, Cost::zero, cost 1796 ); 1797 1798 if (newCand->expr->env) { 1799 newCand->env.add(*newCand->expr->env); 1800 auto mutExpr = newCand->expr.get_and_mutate(); 1801 mutExpr->env = nullptr; 1802 newCand->expr = mutExpr; 1803 } 1804 1805 newCand->expr = ast::mutate_field( 1806 newCand->expr.get(), &ast::Expr::result, 1807 renameTyVars( newCand->expr->result ) ); 1808 addAnonConversions( newCand ); 1809 candidates.emplace_back( std::move( newCand ) ); 1810 } 1811 } 1812 } 1813 } 1809 1814 // size_t Finder::traceId = Stats::Heap::new_stacktrace_id("Finder"); 1810 1815 /// Prunes a list of candidates down to those that have the minimum conversion cost for a given -
src/Validate/FixQualifiedTypes.cpp
r020fa10 ra55ebcc 89 89 } 90 90 91 ast::Expr const * postvisit( ast::QualifiedNameExpr const * t ) {92 assert( location );93 if ( !t->type_decl ) return t;94 95 auto enumName = t->type_decl->name;96 const ast::EnumDecl * enumDecl = symtab.lookupEnum( enumName );97 for ( ast::ptr<ast::Decl> const & member : enumDecl->members ) {98 if ( auto memberAsObj = member.as<ast::ObjectDecl>() ) {99 if ( memberAsObj->name == t->name ) {100 return new ast::VariableExpr( t->location, memberAsObj );101 }102 } else {103 assertf( false, "unhandled qualified child type" );104 }105 }106 107 auto var = new ast::ObjectDecl( t->location, t->name,108 new ast::EnumInstType( enumDecl, ast::CV::Const ),109 nullptr, {}, ast::Linkage::Cforall );110 var->mangleName = Mangle::mangle( var );111 return new ast::VariableExpr( t->location, var );112 }113 114 91 }; 115 92 -
src/Validate/ReplacePseudoFunc.cpp
r020fa10 ra55ebcc 9 9 #include "Common/utility.h" 10 10 #include "ResolvExpr/Resolver.h" 11 #include "SymTab/Mangler.h" // for Mangler 11 #include "SymTab/Mangler.h" 12 13 #include "AST/Print.hpp" 14 15 #include "ResolvExpr/CandidateFinder.hpp" 16 12 17 namespace Validate { 13 18 … … 16 21 std::set<std::string> queryLabels; 17 22 std::set<std::string> queryValues; 23 24 // struct AutoInit { 25 // ast::EnumDecl const* postvisit( const ast::EnumDecl* expr ); 26 // }; 18 27 19 28 struct WrapEnumValueExpr final : public ast::WithShortCircuiting, … … 23 32 void previsit(const ast::ApplicationExpr* expr); 24 33 void previsit(const ast::CastExpr* expr); 34 void previsit(const ast::VariableExpr* ) { visit_children = false; } 25 35 26 36 ast::Expr const* postvisit(const ast::VariableExpr* expr); … … 33 43 struct PseudoFuncGenerateRoutine final : public ast::WithDeclsToAdd<>, 34 44 public ast::WithSymbolTable, 35 public ast::WithShortCircuiting { 45 public ast::WithShortCircuiting, 46 public ast::WithConstTranslationUnit { 36 47 void previsit(const ast::EnumDecl* enumDecl); 37 48 }; … … 43 54 }; 44 55 56 // ast::EnumDecl const * AutoInit::postvisit( const ast::EnumDecl * expr ) { 57 // for ( size_t i = 0; i < expr->members.size(); i++ ) { 58 // auto mem = expr->members[i].as<ast::ObjectDecl>(); 59 // assert( mem ); 60 // if ( mem->init ) 61 // } 62 // return expr; 63 // } 64 45 65 void WrapEnumValueExpr::previsit(const ast::ApplicationExpr* expr) { 46 47 66 auto varExpr = expr->func.as<ast::VariableExpr>(); 48 67 auto fname = ast::getFunctionName(expr); … … 52 71 } 53 72 54 if (fname == "labelE" || fname == "valueE" || fname == "posE") 73 if (fname == "labelE" || fname == "valueE" || fname == "posE") { 55 74 visit_children = false; 75 } 56 76 } 57 77 … … 67 87 68 88 ast::Expr const* WrapEnumValueExpr::postvisit(const ast::VariableExpr* expr) { 69 visit_children = false;70 89 if (!expr->result) { 71 90 return expr; … … 75 94 auto untyped = new ast::UntypedExpr( 76 95 expr->location, new ast::NameExpr(expr->location, "valueE"), 77 { new ast::VariableExpr(*expr)});96 { std::move( expr ) }); 78 97 ResolvExpr::ResolveContext context{symtab, transUnit().global}; 79 98 auto result = ResolvExpr::findVoidExpression(untyped, context); 80 if (result.get()) { 81 ast::ptr<ast::ApplicationExpr> ret = 99 ast::ptr<ast::ApplicationExpr> ret = 82 100 result.strict_as<ast::ApplicationExpr>(); 83 return new ast::ApplicationExpr(*ret); 84 } 101 return ast::deepCopy( ret ); 85 102 } 86 103 } … … 115 132 } 116 133 134 const ast::Init * getAutoInit( const CodeLocation & location, 135 const ast::Type * type, ResolvExpr::ResolveContext context, const ast::Init * prev ) { 136 if ( auto prevInit = dynamic_cast< const ast::SingleInit * >( prev ) ) { 137 auto prevInitExpr = prevInit->value; 138 if ( auto constInit = prevInitExpr.as< ast::ConstantExpr >() ) { 139 // Assume no string literal for now 140 return new ast::SingleInit( 141 location, 142 ast::ConstantExpr::from_int( 143 location, constInit->intValue() + 1 ) 144 ); 145 } else { 146 auto untypedThisInit = new ast::UntypedExpr( 147 location, 148 new ast::NameExpr( location, "?++" ), 149 { prevInitExpr } 150 ); 151 auto typedInit = ResolvExpr::findSingleExpression(untypedThisInit, type, 152 context ); 153 return new ast::SingleInit( location, typedInit ); 154 } 155 } 156 SemanticError( prev, "Auto Init a List is not implemented" ); 157 return prev; 158 } 159 117 160 void PseudoFuncGenerateRoutine::previsit(const ast::EnumDecl* enumDecl) { 118 161 visit_children = false; … … 122 165 std::vector<ast::ptr<ast::Init>> inits; 123 166 std::vector<ast::ptr<ast::Init>> labels; 124 for (const ast::Decl* mem : enumDecl->members) { 125 auto memAsObjectDecl = dynamic_cast<const ast::ObjectDecl*>(mem); 126 inits.emplace_back(memAsObjectDecl->init); 167 auto type = enumDecl->base; 168 169 for ( size_t i = 0; i < enumDecl->members.size(); i++ ) { 170 ast::ptr<ast::Decl> mem = enumDecl->members.at( i ); 171 auto memAsObjectDecl = mem.as< ast::ObjectDecl >(); 172 assert( memAsObjectDecl ); 173 if ( memAsObjectDecl->init ) { 174 inits.emplace_back( memAsObjectDecl->init ); 175 } else { 176 const CodeLocation & location = mem->location; 177 if ( i == 0 ) { 178 inits.emplace_back( new ast::SingleInit( 179 location, 180 ast::ConstantExpr::from_int( mem->location, 0 ) 181 ) ); 182 } else { 183 inits.emplace_back( getAutoInit( location, enumDecl->base, 184 ResolvExpr::ResolveContext{symtab, transUnit().global}, 185 inits.at( i - 1 ).as<ast::SingleInit>()) ); 186 } 187 } 127 188 labels.emplace_back(new ast::SingleInit( 128 129 } 189 location, ast::ConstantExpr::from_string(location, mem->name))); 190 } 130 191 if (queryValues.count(enumDecl->name)) { 131 192 auto init = new ast::ListInit(location, std::move(inits)); … … 206 267 return ast::ConstantExpr::from_string(expr->location, 207 268 referredName); 208 else 269 else { 209 270 return getPseudoFuncApplication(location, context, arg.get(), 210 base, "values_"); 271 base, "values_"); 272 } 273 211 274 } 212 275 } -
src/main.cc
r020fa10 ra55ebcc 383 383 DUMP( exprp, std::move( transUnit ) ); 384 384 PASS( "Replace Pseudo Func", Validate::replacePseudoFunc, transUnit ); 385 385 DUMP( reppseu, std::move( transUnit ) ); 386 386 PASS( "Fix Init", InitTweak::fix, transUnit, buildingLibrary() ); 387 387 PASS( "Erase With", ResolvExpr::eraseWith, transUnit ); … … 536 536 { "bbox", bboxp, true, "print AST before box pass" }, 537 537 { "bcodegen", bcodegenp, true, "print AST before code generation" }, 538 { "reppseu", reppseu, true, "print AST after replacing pseudo functions" } 538 539 }; 539 540 enum { printoptsSize = sizeof( printopts ) / sizeof( printopts[0] ) }; -
tests/enum_tests/structEnum.cfa
r020fa10 ra55ebcc 17 17 }; 18 18 19 PointEnum foo(PointEnum in) {20 return in;21 }19 // PointEnum foo(PointEnum in) { 20 // return in; 21 // } 22 22 23 23 // The only valid usage
Note: See TracChangeset
for help on using the changeset viewer.