Changeset a55ebcc for src/Validate
- Timestamp:
- Feb 5, 2024, 2:17:33 AM (4 months ago)
- Branches:
- master
- Children:
- 47bd204
- Parents:
- 020fa10
- Location:
- src/Validate
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.