[544deb9] | 1 | #include "ReplacePseudoFunc.hpp" |
---|
| 2 | |
---|
| 3 | #include <set> |
---|
| 4 | |
---|
[ac939461] | 5 | #include "AST/Decl.hpp" |
---|
[544deb9] | 6 | #include "AST/Inspect.hpp" |
---|
[ac939461] | 7 | #include "AST/Pass.hpp" |
---|
[0522ebe] | 8 | #include "AST/Print.hpp" |
---|
[ac939461] | 9 | #include "AST/Stmt.hpp" |
---|
| 10 | #include "Common/utility.h" |
---|
[0522ebe] | 11 | #include "ResolvExpr/CandidateFinder.hpp" |
---|
[544deb9] | 12 | #include "ResolvExpr/Resolver.h" |
---|
[a55ebcc] | 13 | #include "SymTab/Mangler.h" |
---|
| 14 | |
---|
[ac939461] | 15 | namespace Validate { |
---|
| 16 | |
---|
| 17 | namespace { |
---|
| 18 | |
---|
[544deb9] | 19 | std::set<std::string> queryLabels; |
---|
| 20 | std::set<std::string> queryValues; |
---|
| 21 | |
---|
[0522ebe] | 22 | struct ReplaceEnumInstWithPos final : public ast::WithShortCircuiting { |
---|
| 23 | const ast::ObjectDecl* postvisit(const ast::ObjectDecl* decl) { |
---|
| 24 | auto enumInst = decl->type.strict_as<ast::EnumInstType>(); |
---|
| 25 | auto enumPos = new ast::EnumPosType(enumInst); |
---|
| 26 | auto ret = ast::mutate_field(decl, &ast::ObjectDecl::type, enumPos); |
---|
| 27 | ret = ast::mutate_field(ret, &ast::ObjectDecl::mangleName, |
---|
| 28 | Mangle::mangle(ret)); |
---|
| 29 | return ret; |
---|
| 30 | } |
---|
| 31 | }; |
---|
| 32 | |
---|
| 33 | const inline std::string getValueArrayName(std::string enumName) { |
---|
| 34 | return "values_" + enumName; |
---|
| 35 | } |
---|
| 36 | |
---|
[a55ebcc] | 37 | // struct AutoInit { |
---|
| 38 | // ast::EnumDecl const* postvisit( const ast::EnumDecl* expr ); |
---|
| 39 | // }; |
---|
| 40 | |
---|
[c75b30a] | 41 | struct WrapEnumValueExpr final : public ast::WithShortCircuiting, |
---|
| 42 | public ast::WithSymbolTable, |
---|
| 43 | public ast::WithConstTranslationUnit { |
---|
| 44 | void previsit(const ast::DeclStmt* expr); |
---|
| 45 | void previsit(const ast::ApplicationExpr* expr); |
---|
| 46 | void previsit(const ast::CastExpr* expr); |
---|
[0522ebe] | 47 | void previsit(const ast::VariableExpr*) { visit_children = false; } |
---|
[c75b30a] | 48 | |
---|
| 49 | ast::Expr const* postvisit(const ast::VariableExpr* expr); |
---|
| 50 | }; |
---|
| 51 | |
---|
[544deb9] | 52 | struct FindGenEnumArray final : public ast::WithShortCircuiting { |
---|
| 53 | void previsit(const ast::ApplicationExpr* enumDecl); |
---|
[ac939461] | 54 | }; |
---|
[544deb9] | 55 | |
---|
[c75b30a] | 56 | struct PseudoFuncGenerateRoutine final : public ast::WithDeclsToAdd<>, |
---|
| 57 | public ast::WithSymbolTable, |
---|
[a55ebcc] | 58 | public ast::WithShortCircuiting, |
---|
| 59 | public ast::WithConstTranslationUnit { |
---|
[c75b30a] | 60 | void previsit(const ast::EnumDecl* enumDecl); |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | struct ReplacePseudoFuncCore : public ast::WithShortCircuiting, |
---|
| 64 | public ast::WithSymbolTable, |
---|
| 65 | public ast::WithConstTranslationUnit { |
---|
| 66 | ast::Expr const* postvisit(ast::ApplicationExpr const* decl); |
---|
| 67 | }; |
---|
| 68 | |
---|
[a55ebcc] | 69 | // ast::EnumDecl const * AutoInit::postvisit( const ast::EnumDecl * expr ) { |
---|
| 70 | // for ( size_t i = 0; i < expr->members.size(); i++ ) { |
---|
| 71 | // auto mem = expr->members[i].as<ast::ObjectDecl>(); |
---|
| 72 | // assert( mem ); |
---|
| 73 | // if ( mem->init ) |
---|
| 74 | // } |
---|
| 75 | // return expr; |
---|
| 76 | // } |
---|
[c75b30a] | 77 | |
---|
[a55ebcc] | 78 | void WrapEnumValueExpr::previsit(const ast::ApplicationExpr* expr) { |
---|
[c75b30a] | 79 | auto varExpr = expr->func.as<ast::VariableExpr>(); |
---|
| 80 | auto fname = ast::getFunctionName(expr); |
---|
[0522ebe] | 81 | if (!varExpr || varExpr->var->linkage == ast::Linkage::Intrinsic) { |
---|
| 82 | if (fname == "?{}" || fname == "?=?") visit_children = false; |
---|
| 83 | } |
---|
[c75b30a] | 84 | |
---|
[bbf2cb1] | 85 | if (fname == "labelE" || fname == "valueE" || fname == "posE" || |
---|
| 86 | fname == "pred" || fname == "succ") { |
---|
[c75b30a] | 87 | visit_children = false; |
---|
[a55ebcc] | 88 | } |
---|
[c75b30a] | 89 | } |
---|
| 90 | |
---|
| 91 | void WrapEnumValueExpr::previsit(const ast::DeclStmt*) { |
---|
| 92 | visit_children = false; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void WrapEnumValueExpr::previsit(const ast::CastExpr* expr) { |
---|
| 96 | if (expr->result && expr->result.as<ast::ReferenceType>()) { |
---|
| 97 | visit_children = false; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | ast::Expr const* WrapEnumValueExpr::postvisit(const ast::VariableExpr* expr) { |
---|
| 102 | if (!expr->result) { |
---|
| 103 | return expr; |
---|
| 104 | } |
---|
| 105 | if (auto enumInst = expr->result.as<ast::EnumInstType>()) { |
---|
| 106 | if (enumInst->base && enumInst->base->base) { |
---|
| 107 | auto untyped = new ast::UntypedExpr( |
---|
| 108 | expr->location, new ast::NameExpr(expr->location, "valueE"), |
---|
[0522ebe] | 109 | {std::move(expr)}); |
---|
[c75b30a] | 110 | ResolvExpr::ResolveContext context{symtab, transUnit().global}; |
---|
| 111 | auto result = ResolvExpr::findVoidExpression(untyped, context); |
---|
[a55ebcc] | 112 | ast::ptr<ast::ApplicationExpr> ret = |
---|
[0522ebe] | 113 | result.strict_as<ast::ApplicationExpr>(); |
---|
| 114 | return ast::deepCopy(ret); |
---|
[c75b30a] | 115 | } |
---|
| 116 | } |
---|
| 117 | return expr; |
---|
| 118 | } |
---|
| 119 | |
---|
[544deb9] | 120 | void FindGenEnumArray::previsit(const ast::ApplicationExpr* expr) { |
---|
| 121 | auto fname = ast::getFunctionName(expr); |
---|
| 122 | if (fname == "labelE" || fname == "valueE") { |
---|
| 123 | if (expr->args.size() != 1) { |
---|
| 124 | SemanticError(expr, "Position Expression only take one parameter"); |
---|
| 125 | } |
---|
| 126 | const ast::VariableExpr* arg = |
---|
| 127 | expr->args.front().as<const ast::VariableExpr>(); |
---|
| 128 | if (!arg) { |
---|
| 129 | SemanticError(expr, "Unimplement Pseudo Function Cases"); |
---|
| 130 | } |
---|
| 131 | const ast::ObjectDecl* argAsVar = arg->var.as<const ast::ObjectDecl>(); |
---|
| 132 | const std::string referredName = argAsVar->name; |
---|
| 133 | const ast::EnumInstType* argType = |
---|
| 134 | argAsVar->type.as<const ast::EnumInstType>(); |
---|
| 135 | if (!argType) { |
---|
| 136 | SemanticError( |
---|
| 137 | argAsVar, |
---|
| 138 | "Position can only be used on an enumeration instance"); |
---|
| 139 | } |
---|
| 140 | ast::ptr<ast::EnumDecl> base = argType->base; |
---|
| 141 | assert(base); |
---|
| 142 | if (fname == "labelE") queryLabels.insert(base->name); |
---|
| 143 | if (fname == "valueE") queryValues.insert(base->name); |
---|
| 144 | } |
---|
[ac939461] | 145 | } |
---|
| 146 | |
---|
[0522ebe] | 147 | const ast::Init* getAutoInit(const CodeLocation& location, |
---|
| 148 | const ast::Type* type, |
---|
| 149 | ResolvExpr::ResolveContext context, |
---|
| 150 | const ast::Init* prev) { |
---|
| 151 | if (auto prevInit = dynamic_cast<const ast::SingleInit*>(prev)) { |
---|
[a55ebcc] | 152 | auto prevInitExpr = prevInit->value; |
---|
[0522ebe] | 153 | if (auto constInit = prevInitExpr.as<ast::ConstantExpr>()) { |
---|
[a55ebcc] | 154 | // Assume no string literal for now |
---|
| 155 | return new ast::SingleInit( |
---|
[0522ebe] | 156 | location, ast::ConstantExpr::from_int( |
---|
| 157 | location, constInit->intValue() + 1)); |
---|
[a55ebcc] | 158 | } else { |
---|
| 159 | auto untypedThisInit = new ast::UntypedExpr( |
---|
[0522ebe] | 160 | location, new ast::NameExpr(location, "?++"), {prevInitExpr}); |
---|
| 161 | auto typedInit = ResolvExpr::findSingleExpression(untypedThisInit, |
---|
| 162 | type, context); |
---|
| 163 | return new ast::SingleInit(location, typedInit); |
---|
[a55ebcc] | 164 | } |
---|
| 165 | } |
---|
[0522ebe] | 166 | SemanticError(prev, "Auto Init a List is not implemented"); |
---|
[a55ebcc] | 167 | return prev; |
---|
| 168 | } |
---|
| 169 | |
---|
[544deb9] | 170 | void PseudoFuncGenerateRoutine::previsit(const ast::EnumDecl* enumDecl) { |
---|
| 171 | visit_children = false; |
---|
| 172 | const CodeLocation& location = enumDecl->location; |
---|
| 173 | if (enumDecl->members.size() == 0 || !enumDecl->base) return; |
---|
| 174 | |
---|
| 175 | std::vector<ast::ptr<ast::Init>> inits; |
---|
| 176 | std::vector<ast::ptr<ast::Init>> labels; |
---|
[a55ebcc] | 177 | auto type = enumDecl->base; |
---|
| 178 | |
---|
[0522ebe] | 179 | for (size_t i = 0; i < enumDecl->members.size(); i++) { |
---|
| 180 | ast::ptr<ast::Decl> mem = enumDecl->members.at(i); |
---|
| 181 | auto memAsObjectDecl = mem.as<ast::ObjectDecl>(); |
---|
| 182 | assert(memAsObjectDecl); |
---|
| 183 | if (memAsObjectDecl->init) { |
---|
| 184 | inits.emplace_back(memAsObjectDecl->init); |
---|
[a55ebcc] | 185 | } else { |
---|
[0522ebe] | 186 | const CodeLocation& location = mem->location; |
---|
| 187 | if (i == 0) { |
---|
| 188 | inits.emplace_back(new ast::SingleInit( |
---|
| 189 | location, ast::ConstantExpr::from_int(mem->location, 0))); |
---|
[a55ebcc] | 190 | } else { |
---|
[0522ebe] | 191 | inits.emplace_back(getAutoInit( |
---|
| 192 | location, enumDecl->base, |
---|
| 193 | ResolvExpr::ResolveContext{symtab, transUnit().global}, |
---|
| 194 | inits.at(i - 1).as<ast::SingleInit>())); |
---|
[a55ebcc] | 195 | } |
---|
| 196 | } |
---|
[544deb9] | 197 | labels.emplace_back(new ast::SingleInit( |
---|
[0522ebe] | 198 | location, ast::ConstantExpr::from_string(location, mem->name))); |
---|
| 199 | } |
---|
[544deb9] | 200 | if (queryValues.count(enumDecl->name)) { |
---|
| 201 | auto init = new ast::ListInit(location, std::move(inits)); |
---|
[c75b30a] | 202 | const ast::ArrayType* arrT = new ast::ArrayType( |
---|
| 203 | enumDecl->base, |
---|
| 204 | ast::ConstantExpr::from_int(location, enumDecl->members.size()), |
---|
| 205 | ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim); |
---|
| 206 | ast::ObjectDecl* values = new ast::ObjectDecl( |
---|
| 207 | location, "values_" + enumDecl->name, arrT, init, |
---|
| 208 | ast::Storage::Static, ast::Linkage::AutoGen); |
---|
[544deb9] | 209 | symtab.addId(values); |
---|
| 210 | values->mangleName = Mangle::mangle(values); |
---|
| 211 | declsToAddAfter.push_back(values); |
---|
| 212 | } |
---|
| 213 | if (queryLabels.count(enumDecl->name)) { |
---|
| 214 | auto label_strings = new ast::ListInit(location, std::move(labels)); |
---|
[c75b30a] | 215 | auto labels = new ast::ObjectDecl( |
---|
[544deb9] | 216 | location, "labels_" + enumDecl->name, |
---|
| 217 | new ast::ArrayType( |
---|
| 218 | new ast::PointerType(new ast::BasicType{ast::BasicType::Char}), |
---|
| 219 | ast::ConstantExpr::from_int(location, enumDecl->members.size()), |
---|
| 220 | ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim), |
---|
| 221 | label_strings, ast::Storage::Static, ast::Linkage::AutoGen); |
---|
[c75b30a] | 222 | symtab.addId(labels); |
---|
| 223 | labels->mangleName = Mangle::mangle(labels); |
---|
| 224 | declsToAddAfter.push_back(labels); |
---|
[544deb9] | 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
[bbf2cb1] | 228 | ast::ApplicationExpr const* resolveAttributeFunctions( |
---|
[c75b30a] | 229 | const CodeLocation location, ResolvExpr::ResolveContext context, |
---|
[0522ebe] | 230 | const ast::VariableExpr* arg, const ast::EnumDecl* base, |
---|
| 231 | const std::string& name) { |
---|
[c75b30a] | 232 | ast::Expr* toResolve = new ast::NameExpr(location, name + base->name); |
---|
[0522ebe] | 233 | // Find the request arrary |
---|
| 234 | auto arr = ResolvExpr::findVoidExpression(toResolve, context); |
---|
| 235 | assert(arr.get()); |
---|
| 236 | auto arrAsVar = arr.strict_as<ast::VariableExpr>(); |
---|
| 237 | // change EnumInstType to EnumPosType to avoid recursive resolution |
---|
| 238 | auto argAsDecl = arg->var.as<ast::ObjectDecl>(); |
---|
| 239 | if (argAsDecl->type.as<ast::EnumInstType>()) { |
---|
| 240 | ast::Pass<ReplaceEnumInstWithPos> replacer; |
---|
| 241 | auto rep = argAsDecl->accept(replacer); |
---|
| 242 | auto mutatedArg = ast::mutate_field(arg, &ast::VariableExpr::var, rep); |
---|
[bbf2cb1] | 243 | mutatedArg = ast::mutate_field(mutatedArg, &ast::VariableExpr::result, |
---|
| 244 | mutatedArg->var->get_type()); |
---|
[0522ebe] | 245 | auto untyped = |
---|
| 246 | new ast::UntypedExpr(location, new ast::NameExpr(location, "?[?]"), |
---|
| 247 | {std::move(arrAsVar), mutatedArg}); |
---|
| 248 | auto typedResult = ResolvExpr::findVoidExpression(untyped, context); |
---|
| 249 | ast::ptr<ast::ApplicationExpr> ret = |
---|
| 250 | typedResult.strict_as<ast::ApplicationExpr>(); |
---|
| 251 | return ast::deepCopy(ret); |
---|
| 252 | } else { |
---|
| 253 | auto untyped = |
---|
| 254 | new ast::UntypedExpr(location, new ast::NameExpr(location, "?[?]"), |
---|
| 255 | {std::move(arrAsVar), arg}); |
---|
| 256 | auto typedResult = ResolvExpr::findVoidExpression(untyped, context); |
---|
| 257 | |
---|
| 258 | ast::ptr<ast::ApplicationExpr> ret = |
---|
| 259 | typedResult.strict_as<ast::ApplicationExpr>(); |
---|
| 260 | return ast::deepCopy(ret); |
---|
| 261 | } |
---|
[c75b30a] | 262 | } |
---|
[544deb9] | 263 | |
---|
| 264 | ast::Expr const* ReplacePseudoFuncCore::postvisit( |
---|
| 265 | ast::ApplicationExpr const* expr) { |
---|
| 266 | auto fname = ast::getFunctionName(expr); |
---|
| 267 | auto location = expr->location; |
---|
| 268 | if (fname == "posE" || fname == "valueE" || fname == "labelE") { |
---|
| 269 | if (expr->args.size() != 1) { |
---|
| 270 | SemanticError(expr, |
---|
| 271 | "Pseudo Enum Expression only take one parameter"); |
---|
[ac939461] | 272 | } |
---|
[544deb9] | 273 | ast::ptr<ast::VariableExpr> arg = |
---|
| 274 | expr->args.front().as<const ast::VariableExpr>(); |
---|
| 275 | if (!arg) { |
---|
| 276 | SemanticError(expr, "Unimplement Pseudo Function Cases"); |
---|
[ac939461] | 277 | } |
---|
[544deb9] | 278 | const ast::ObjectDecl* argAsVar = arg->var.as<const ast::ObjectDecl>(); |
---|
[ac939461] | 279 | const std::string referredName = argAsVar->name; |
---|
[a55ebcc] | 280 | |
---|
[0522ebe] | 281 | if (const ast::EnumInstType* argTypeAsEnumInst = |
---|
| 282 | argAsVar->type.as<const ast::EnumInstType>()) { |
---|
| 283 | const ast::EnumDecl* base = argTypeAsEnumInst->base; |
---|
| 284 | ResolvExpr::ResolveContext context{symtab, transUnit().global}; |
---|
| 285 | // If resolvable as constant |
---|
| 286 | for (size_t i = 0; i < base->members.size(); i++) { |
---|
| 287 | if (base->members[i]->name == referredName) { |
---|
| 288 | if (fname == "posE") |
---|
| 289 | return ast::ConstantExpr::from_int(expr->location, i); |
---|
| 290 | else if (fname == "labelE") |
---|
| 291 | return ast::ConstantExpr::from_string(expr->location, |
---|
| 292 | referredName); |
---|
| 293 | else { |
---|
[bbf2cb1] | 294 | return resolveAttributeFunctions( |
---|
[0522ebe] | 295 | location, context, arg.get(), base, "values_"); |
---|
| 296 | } |
---|
| 297 | } |
---|
[544deb9] | 298 | } |
---|
| 299 | |
---|
[0522ebe] | 300 | if (fname == "labelE") { |
---|
[bbf2cb1] | 301 | if (auto labelExpr = resolveAttributeFunctions( |
---|
[0522ebe] | 302 | location, context, arg.get(), base, "labels_")) { |
---|
| 303 | return labelExpr; |
---|
| 304 | } |
---|
| 305 | } else if (fname == "valueE") { |
---|
[bbf2cb1] | 306 | if (auto valueExpr = resolveAttributeFunctions( |
---|
[0522ebe] | 307 | location, context, arg.get(), base, "values_")) { |
---|
| 308 | return valueExpr; |
---|
| 309 | } |
---|
| 310 | } else { // it is position; replace itself |
---|
| 311 | return std::move(arg.get()); |
---|
[544deb9] | 312 | } |
---|
[0522ebe] | 313 | } else if (const ast::EnumPosType* argTypeAsPos = |
---|
| 314 | argAsVar->type.as<const ast::EnumPosType>()) { |
---|
| 315 | const ast::EnumDecl* base = argTypeAsPos->instance->base; |
---|
| 316 | ResolvExpr::ResolveContext context{symtab, transUnit().global}; |
---|
| 317 | if (fname == "labelE") { |
---|
[bbf2cb1] | 318 | if (auto labelExpr = resolveAttributeFunctions( |
---|
[0522ebe] | 319 | location, context, arg.get(), base, "labels_")) { |
---|
| 320 | return labelExpr; |
---|
| 321 | } |
---|
| 322 | } else if (fname == "valueE") { |
---|
[bbf2cb1] | 323 | if (auto valueExpr = resolveAttributeFunctions( |
---|
[0522ebe] | 324 | location, context, arg.get(), base, "values_")) { |
---|
| 325 | return valueExpr; |
---|
| 326 | } |
---|
| 327 | } else { // it is position; replace itself |
---|
| 328 | return std::move(arg.get()); |
---|
[ac939461] | 329 | } |
---|
[0522ebe] | 330 | } else { |
---|
| 331 | SemanticError(argAsVar, |
---|
| 332 | "Pseudo Enum Expression can only be used on an " |
---|
| 333 | "enumeration instance"); |
---|
[ac939461] | 334 | } |
---|
| 335 | } |
---|
| 336 | return expr; |
---|
| 337 | } |
---|
| 338 | |
---|
[0522ebe] | 339 | ast::ptr<ast::Expr> reduceCastExpr(ast::ptr<ast::Expr> expr) { |
---|
| 340 | if (auto castExpr = expr.as<ast::CastExpr>()) { |
---|
| 341 | return reduceCastExpr(castExpr->arg); |
---|
| 342 | } |
---|
| 343 | return expr; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | struct ReplaceEnumInst final { |
---|
| 347 | const ast::Expr* postvisit(const ast::ApplicationExpr* expr) { |
---|
| 348 | auto fname = ast::getFunctionName(expr); |
---|
[bbf2cb1] | 349 | if (fname == "?[?]") { |
---|
| 350 | if (expr->args.size() != 2) return expr; |
---|
[0522ebe] | 351 | |
---|
[bbf2cb1] | 352 | auto arg1AsVar = |
---|
| 353 | reduceCastExpr(expr->args.front()).as<ast::VariableExpr>(); |
---|
| 354 | auto arg2AsVar = |
---|
| 355 | reduceCastExpr(expr->args.back()).as<ast::VariableExpr>(); |
---|
[0522ebe] | 356 | |
---|
[bbf2cb1] | 357 | if (!arg1AsVar || !arg2AsVar) return expr; |
---|
[0522ebe] | 358 | |
---|
[bbf2cb1] | 359 | auto arg1AsDecl = arg1AsVar->var.as<ast::ObjectDecl>(); |
---|
| 360 | auto arg2AsDecl = arg2AsVar->var.as<ast::ObjectDecl>(); |
---|
[0522ebe] | 361 | |
---|
[bbf2cb1] | 362 | if (!arg1AsDecl || !arg2AsDecl) return expr; |
---|
| 363 | auto arrInst = arg1AsDecl->type.as<ast::ArrayType>(); |
---|
| 364 | auto pointerInst = arg1AsDecl->type.as<ast::PointerType>(); |
---|
| 365 | if (!arrInst && !pointerInst) { |
---|
| 366 | return expr; |
---|
| 367 | } |
---|
| 368 | auto enumInst = arg2AsDecl->type.as<ast::EnumInstType>(); |
---|
| 369 | if (!enumInst) return expr; |
---|
| 370 | |
---|
| 371 | const std::string arrName = arg1AsDecl->name; |
---|
| 372 | if (arrName != getValueArrayName(enumInst->base->name)) return expr; |
---|
| 373 | ast::Pass<ReplaceEnumInstWithPos> replacer; |
---|
| 374 | auto rep = arg2AsDecl->accept(replacer); |
---|
| 375 | if (!rep) return expr; |
---|
| 376 | auto mutObj = |
---|
| 377 | ast::mutate_field(arg2AsVar, &ast::VariableExpr::var, rep); |
---|
| 378 | mutObj = ast::mutate_field(mutObj, &ast::VariableExpr::result, |
---|
| 379 | mutObj->var->get_type()); |
---|
| 380 | auto mut = ast::mutate_field_index( |
---|
| 381 | expr, &ast::ApplicationExpr::args, 1, mutObj); |
---|
| 382 | return mut; |
---|
[0522ebe] | 383 | } |
---|
[bbf2cb1] | 384 | // else if (fname == "succ" || fname == "pred") { |
---|
| 385 | // if (expr->args.size() != 1) return expr; |
---|
| 386 | // auto argExpr = expr->args.front(); |
---|
| 387 | // auto argAsVar = reduceCastExpr(argExpr).as<ast::VariableExpr>(); |
---|
| 388 | |
---|
| 389 | // if (auto argAsDecl = argAsVar->var.as<ast::ObjectDecl>()) { |
---|
| 390 | // if (auto enumInst = argAsDecl->type.as<ast::EnumInstType>()) |
---|
| 391 | // { |
---|
| 392 | // auto enumPos = new ast::EnumPosType(enumInst); |
---|
| 393 | // auto castExpr = |
---|
| 394 | // new ast::CastExpr(argExpr->location, argExpr, |
---|
| 395 | // enumPos); |
---|
| 396 | // auto mut = ast::mutate_field_index( |
---|
| 397 | // expr, &ast::ApplicationExpr::args, 0, castExpr); |
---|
| 398 | // return mut; |
---|
| 399 | // } else if (auto enumPos = |
---|
| 400 | // argAsDecl->type.as<ast::EnumPosType>()) { |
---|
| 401 | // // std::cout << "pos" << std::endl; |
---|
| 402 | // return expr; |
---|
| 403 | // } |
---|
| 404 | // } |
---|
| 405 | // } |
---|
| 406 | return expr; |
---|
| 407 | } |
---|
| 408 | }; |
---|
[0522ebe] | 409 | |
---|
[bbf2cb1] | 410 | struct ReplaceSuccAndPred final : public ast::WithSymbolTable, |
---|
| 411 | public ast::WithConstTranslationUnit { |
---|
| 412 | const ast::Expr* postvisit(const ast::ApplicationExpr* expr) { |
---|
| 413 | auto fname = ast::getFunctionName(expr); |
---|
| 414 | if (fname == "succ" || fname == "pred") { |
---|
| 415 | const CodeLocation& location = expr->location; |
---|
| 416 | if (expr->args.size() != 1) return expr; |
---|
| 417 | |
---|
| 418 | // if (auto argAsVar = reduceCastExpr(expr->args.front()) |
---|
| 419 | // .as<ast::VariableExpr>()) { |
---|
| 420 | // if (auto argAsDecl = argAsVar->var.as<ast::ObjectDecl>()) { |
---|
| 421 | // auto enumPos = argAsDecl->type.as<ast::EnumPosType>(); |
---|
| 422 | // if (!enumPos) return expr; |
---|
| 423 | // // ast::Pass<ReplaceEnumInstWithPos> replacer; |
---|
| 424 | // // auto posObj = argAsDecl->accept(replacer); |
---|
| 425 | // // if (!posObj) return expr; |
---|
| 426 | |
---|
| 427 | // // auto newParam = new ast::VariableExpr( location, |
---|
| 428 | // posObj |
---|
| 429 | // // ); |
---|
| 430 | |
---|
| 431 | // auto untyped = new ast::UntypedExpr( |
---|
| 432 | // location, |
---|
| 433 | // new ast::NameExpr(location, fname == "succ" |
---|
| 434 | // ? "_successor_" |
---|
| 435 | // : "_predessor_"), |
---|
| 436 | // {argAsVar}); |
---|
| 437 | |
---|
| 438 | // ResolvExpr::ResolveContext context{symtab, |
---|
| 439 | // transUnit().global}; |
---|
| 440 | |
---|
| 441 | // auto typedResult = |
---|
| 442 | // ResolvExpr::findVoidExpression(untyped, context); |
---|
| 443 | |
---|
| 444 | // ast::ptr<ast::ApplicationExpr> ret = |
---|
| 445 | // typedResult.strict_as<ast::ApplicationExpr>(); |
---|
| 446 | |
---|
| 447 | // return ast::deepCopy(ret); |
---|
| 448 | // } |
---|
| 449 | // } |
---|
| 450 | auto param = expr->args.front(); |
---|
| 451 | if (auto argAsVar = reduceCastExpr(param).as<ast::VariableExpr>()) { |
---|
| 452 | if (auto argAsDecl = argAsVar->var.as<ast::ObjectDecl>()) { |
---|
| 453 | if (auto enumInst = |
---|
| 454 | argAsDecl->type.as<ast::EnumInstType>()) { |
---|
| 455 | auto castTo = new ast::EnumPosType(enumInst); |
---|
| 456 | auto castExpr = |
---|
| 457 | new ast::CastExpr(param->location, param, castTo); |
---|
| 458 | |
---|
| 459 | auto untyped = new ast::UntypedExpr( |
---|
| 460 | expr->location, |
---|
| 461 | new ast::NameExpr(location, fname == "succ" |
---|
| 462 | ? "_successor_" |
---|
| 463 | : "_predessor_"), |
---|
| 464 | {castExpr}); |
---|
| 465 | ResolvExpr::ResolveContext context{symtab, |
---|
| 466 | transUnit().global}; |
---|
| 467 | auto typedResult = |
---|
| 468 | ResolvExpr::findVoidExpression(untyped, context); |
---|
| 469 | ast::ptr<ast::ApplicationExpr> ret = |
---|
| 470 | typedResult.strict_as<ast::ApplicationExpr>(); |
---|
| 471 | return ast::deepCopy(ret); |
---|
| 472 | } else if (auto posType = |
---|
| 473 | argAsDecl->type.as<ast::EnumPosType>()) { |
---|
| 474 | // Very nasty fix. Must be revisit |
---|
| 475 | if (auto paramAsVar = param.as<ast::VariableExpr>()) { |
---|
| 476 | if (paramAsVar->result.as<ast::EnumInstType>()) { |
---|
| 477 | auto paramToUse = ast::mutate_field( |
---|
| 478 | paramAsVar, &ast::VariableExpr::result, |
---|
| 479 | posType); |
---|
| 480 | auto untyped = new ast::UntypedExpr( |
---|
| 481 | expr->location, |
---|
| 482 | new ast::NameExpr(location, |
---|
| 483 | fname == "succ" |
---|
| 484 | ? "_successor_" |
---|
| 485 | : "_predessor_"), |
---|
| 486 | {paramToUse}); |
---|
| 487 | ResolvExpr::ResolveContext context{ |
---|
| 488 | symtab, transUnit().global}; |
---|
| 489 | auto typedResult = |
---|
| 490 | ResolvExpr::findVoidExpression(untyped, |
---|
| 491 | context); |
---|
| 492 | ast::ptr<ast::ApplicationExpr> ret = |
---|
| 493 | typedResult |
---|
| 494 | .strict_as<ast::ApplicationExpr>(); |
---|
| 495 | return ast::deepCopy(ret); |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | auto untyped = new ast::UntypedExpr( |
---|
| 499 | expr->location, |
---|
| 500 | new ast::NameExpr(location, fname == "succ" |
---|
| 501 | ? "_successor_" |
---|
| 502 | : "_predessor_"), |
---|
| 503 | {param}); |
---|
| 504 | ResolvExpr::ResolveContext context{symtab, |
---|
| 505 | transUnit().global}; |
---|
| 506 | auto typedResult = |
---|
| 507 | ResolvExpr::findVoidExpression(untyped, context); |
---|
| 508 | ast::ptr<ast::ApplicationExpr> ret = |
---|
| 509 | typedResult.strict_as<ast::ApplicationExpr>(); |
---|
| 510 | return ast::deepCopy(ret); |
---|
| 511 | } |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | } |
---|
| 515 | return expr; |
---|
[0522ebe] | 516 | } |
---|
| 517 | }; |
---|
| 518 | |
---|
[544deb9] | 519 | } // namespace |
---|
[ac939461] | 520 | |
---|
[544deb9] | 521 | void replacePseudoFunc(ast::TranslationUnit& translationUnit) { |
---|
[c75b30a] | 522 | ast::Pass<WrapEnumValueExpr>::run(translationUnit); |
---|
[544deb9] | 523 | ast::Pass<FindGenEnumArray>::run(translationUnit); |
---|
[bbf2cb1] | 524 | |
---|
[544deb9] | 525 | ast::Pass<PseudoFuncGenerateRoutine>::run(translationUnit); |
---|
| 526 | ast::Pass<ReplacePseudoFuncCore>::run(translationUnit); |
---|
[0522ebe] | 527 | ast::Pass<ReplaceEnumInst>::run(translationUnit); |
---|
[bbf2cb1] | 528 | |
---|
| 529 | ast::Pass<ReplaceSuccAndPred>::run(translationUnit); |
---|
[ac939461] | 530 | } |
---|
[544deb9] | 531 | } // namespace Validate |
---|