- File:
-
- 1 edited
-
src/Validate/ReplacePseudoFunc.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Validate/ReplacePseudoFunc.cpp
r76fe046 rbbf2cb1 344 344 } 345 345 346 struct ReplaceEnumInst final { 347 const ast::Expr* postvisit(const ast::ApplicationExpr* expr) { 348 auto fname = ast::getFunctionName(expr); 349 if (fname == "?[?]") { 350 if (expr->args.size() != 2) return expr; 351 352 auto arg1AsVar = 353 reduceCastExpr(expr->args.front()).as<ast::VariableExpr>(); 354 auto arg2AsVar = 355 reduceCastExpr(expr->args.back()).as<ast::VariableExpr>(); 356 357 if (!arg1AsVar || !arg2AsVar) return expr; 358 359 auto arg1AsDecl = arg1AsVar->var.as<ast::ObjectDecl>(); 360 auto arg2AsDecl = arg2AsVar->var.as<ast::ObjectDecl>(); 361 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; 383 } 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 }; 409 346 410 struct ReplaceSuccAndPred final : public ast::WithSymbolTable, 347 411 public ast::WithConstTranslationUnit { … … 352 416 if (expr->args.size() != 1) return expr; 353 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 // } 354 450 auto param = expr->args.front(); 355 451 if (auto argAsVar = reduceCastExpr(param).as<ast::VariableExpr>()) { … … 429 525 ast::Pass<PseudoFuncGenerateRoutine>::run(translationUnit); 430 526 ast::Pass<ReplacePseudoFuncCore>::run(translationUnit); 527 ast::Pass<ReplaceEnumInst>::run(translationUnit); 431 528 432 529 ast::Pass<ReplaceSuccAndPred>::run(translationUnit);
Note:
See TracChangeset
for help on using the changeset viewer.