Changes in / [f6e8c67:af60383]


Ignore:
Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CommonType.cc

    rf6e8c67 raf60383  
    672672
    673673        void postvisit( const ast::EnumInstType * enumInst ) {
    674                 // if ( dynamic_cast<const ast::EnumPosType *>(enumInst) ) {
    675                 //      result = enumInst;
    676                 // } else
    677                 if (!dynamic_cast<const ast::EnumInstType *>(type2)) {
     674                if (!dynamic_cast<const ast::EnumInstType *>(type2))
    678675                        result = commonType( type2, enumInst, tenv, need, have, open, widen);
    679676                }
    680         }
    681 
    682         void postvisit( const ast::EnumPosType * enumPos ) {
    683                 if ( auto type2AsPos = dynamic_cast<const ast::EnumPosType *>(type2) ) {
    684                         // result = commonType( type2AsPos->instance, enumPos->instance, tenv, need, have, open, widen );
    685                         result = enumPos;
    686                 } else if (  auto typeAsBasic = dynamic_cast<const ast::BasicType *>(type2) ) {
    687                         result = type2;
    688                 }
    689         }
    690677
    691678        void postvisit( const ast::TraitInstType * ) {}
  • src/ResolvExpr/ConversionCost.cc

    rf6e8c67 raf60383  
    288288            cost = Cost::unsafe;
    289289                }
    290         } else if ( dynamic_cast< const ast::EnumPosType *>(dst) ) {
    291                 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    292                 cost = costCalc( basicType, integer, srcIsLvalue, symtab, env );
    293290        }
    294291}
     
    366363}
    367364
    368 void ConversionCost::postvisit( const ast::EnumInstType * inst ) {
     365void ConversionCost::postvisit( const ast::EnumInstType * ) {
     366        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
     367        cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     368        // }
     369        if ( cost < Cost::unsafe ) {
     370                cost.incSafe();
     371        }
     372}
     373
     374void ConversionCost::postvisit( const ast::EnumPosType * ) {
    369375        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    370376        cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     
    372378                cost.incSafe();
    373379        }
    374 }
    375 
    376 void ConversionCost::postvisit( const ast::EnumPosType * src ) {
    377         if ( auto dstBase = dynamic_cast<const ast::EnumPosType *>( dst ) ) {
    378                 // cost = costCalc( src->instance, dstBase->instance, srcIsLvalue, symtab, env );
    379                 // if ( cost < Cost::unsafe ) cost.incSafe();
    380                 cost = Cost::zero;
    381         }
    382         // if ( auto dstBase = dynamic_cast<const ast::EnumInstType *>( dst ) ) {
    383         //      cost = costCalc( src->instance, dstBase, srcIsLvalue, symtab, env );
    384         //      if ( cost < Cost::unsafe ) cost.incSafe();
    385         // }
    386         else {
    387                 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    388                 cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    389                 if ( cost < Cost::unsafe ) {
    390                         cost.incSafe();
    391                 }
    392         }
    393 
    394380}
    395381
  • src/ResolvExpr/Unify.cc

    rf6e8c67 raf60383  
    518518
    519519                void postvisit( const ast::EnumPosType * ) {
    520                         // Lazy approach for now
    521                         auto otherPos = dynamic_cast< const ast::EnumPosType *>(type2);
    522                         if (otherPos) this->result = otherPos;
     520                        // Does nothing for now. Handled in ReplacePseudoFunc
     521                        // Might move here in the future
    523522                }
    524523
  • src/Validate/Autogen.cpp

    rf6e8c67 raf60383  
    197197        bool shouldAutogen() const final { return true; }
    198198        void genAttrFuncForward();
    199         void genPosFunctions();
    200199private:
    201200        void genFuncBody( ast::FunctionDecl * decl ) final;
     
    206205        ast::FunctionDecl * genLabelProto() const;
    207206        ast::FunctionDecl * genValueProto() const;
    208         ast::FunctionDecl * genSuccProto() const;
    209         ast::FunctionDecl * genPredProto() const;
    210 
    211         ast::FunctionDecl * genSuccPosProto() const;
    212         ast::FunctionDecl * genPredPosProto() const;
    213 
    214         ast::FunctionDecl * genSuccPredFunc( bool succ );
    215         // ast::FunctionDecl * genPredFunc();
     207        // ast::FunctionDecl * genValueProto2() const;
    216208};
    217209
     
    258250        if ( enumDecl->base ) {
    259251                gen.genAttrFuncForward();
    260                 gen.genPosFunctions();
    261252        }
    262253        gen.generateAndAppendFunctions( declsToAddAfter );
     
    799790}
    800791
    801 ast::FunctionDecl * EnumFuncGenerator::genSuccProto() const {
    802         return genProto( "succ",
    803                 { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumInstType( decl ) )},
    804                 { new ast::ObjectDecl( getLocation(), "_ret", new ast::EnumInstType( decl ))} );
    805 }
    806 
    807 ast::FunctionDecl * EnumFuncGenerator::genPredProto() const {
    808         return genProto( "pred",
    809                 { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumInstType( decl ))},
    810                 { new ast::ObjectDecl( getLocation(), "_ret", new ast::EnumInstType( decl ))} );
    811 }
    812 
    813 ast::FunctionDecl * EnumFuncGenerator::genSuccPosProto() const {
    814         return genProto( "_successor_",
    815                 { new ast::ObjectDecl( getLocation(), "_i",
    816                         new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
    817                 {
    818                         new ast::ObjectDecl( getLocation(), "_ret",
    819                         new ast::EnumPosType( new ast::EnumInstType( decl ) ) )
    820                 } );
    821 }
    822 
    823 ast::FunctionDecl * EnumFuncGenerator::genPredPosProto() const {
    824         return genProto( "_predessor_",
    825                 { new ast::ObjectDecl( getLocation(), "_i",
    826                         new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
    827                 {
    828                         new ast::ObjectDecl( getLocation(), "_ret",
    829                         new ast::EnumPosType( new ast::EnumInstType( decl ) ) )
    830                 } );
    831 }
    832 
    833 ast::FunctionDecl * EnumFuncGenerator::genSuccPredFunc( bool succ ) {
    834         ast::FunctionDecl * decl = succ? genSuccPosProto(): genPredPosProto();
    835         produceForwardDecl( decl );
    836 
    837         const CodeLocation& location = getLocation();
    838 
    839         auto & params = decl->params;
    840         assert( params.size() == 1 );
    841         auto param = params.front().strict_as<ast::ObjectDecl>();
    842 
    843         auto newReturn = new ast::ObjectDecl( location, "_returns",
    844                 new ast::BasicType{ ast::BasicType::SignedInt} );
    845        
    846 
    847         ast::UntypedExpr * addOneExpr = new ast::UntypedExpr( location,
    848                 new ast::NameExpr( location, succ? "?+?": "?-?" )
    849         );
    850         addOneExpr->args.push_back(
    851                 new ast::CastExpr( location,
    852                         new ast::VariableExpr( location, param ),
    853                         new ast::BasicType{ ast::BasicType::SignedInt }
    854                 )
    855         );
    856         addOneExpr->args.push_back(
    857                 ast::ConstantExpr::from_int( location, 1 )
    858         );
    859 
    860         ast::UntypedExpr * assignExpr = new ast::UntypedExpr( location,
    861                 new ast::NameExpr( location, "?=?" )
    862         );
    863         assignExpr->args.push_back(     
    864                 new ast::VariableExpr( location, newReturn )
    865         );
    866         assignExpr->args.push_back(
    867                 addOneExpr
    868         );
    869 
    870         decl->stmts = new ast::CompoundStmt( location,
    871                 {
    872                         new ast::DeclStmt( location, newReturn ),
    873                         new ast::ExprStmt( location, assignExpr ),
    874                         new ast::ReturnStmt( location,
    875                                 new ast::VariableExpr( location, newReturn ))
    876                 } );
    877        
    878         return decl;
    879 }
     792// ast::FunctionDecl * EnumFuncGenerator::genValueProto2() const {
     793//      return genProto( "valueE",
     794//              { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
     795//              { new ast::ObjectDecl( getLocation(), "_ret", ast::deepCopy( decl->base ) ) } );
     796// }
    880797
    881798void EnumFuncGenerator::genAttrFuncForward() { 
    882799        if ( decl->base ) {
    883                 ast::FunctionDecl *(EnumFuncGenerator::*attrProtos[5])() const = {
     800                ast::FunctionDecl *(EnumFuncGenerator::*attrProtos[3])() const = {
    884801                        &EnumFuncGenerator::genPosProto, &EnumFuncGenerator::genLabelProto,
    885                         &EnumFuncGenerator::genValueProto, &EnumFuncGenerator::genSuccProto,
    886                         &EnumFuncGenerator::genPredProto
    887                         // ,&EnumFuncGenerator::genSuccPosProto,
    888                         // &EnumFuncGenerator::genPredPosProto
    889                 };
     802                        &EnumFuncGenerator::genValueProto
     803                        // , &EnumFuncGenerator::genValueProto2
     804                        };
    890805                for ( auto & generator : attrProtos ) {
    891806                        produceForwardDecl( (this->*generator)() );
    892807                }
    893808        }
    894 }
    895 
    896 void EnumFuncGenerator::genPosFunctions() {
    897         if ( decl->base ) {
    898                 ast::FunctionDecl * succ = genSuccPredFunc( true );
    899                 ast::FunctionDecl * pred = genSuccPredFunc( false );
    900                 produceDecl( succ );
    901                 produceDecl( pred );
    902         }
    903 
    904809}
    905810
  • src/Validate/ReplacePseudoFunc.cpp

    rf6e8c67 raf60383  
    2121
    2222struct ReplaceEnumInstWithPos final : public ast::WithShortCircuiting {
     23    void previsit(const ast::ObjectDecl*) { visit_children = false; }
    2324    const ast::ObjectDecl* postvisit(const ast::ObjectDecl* decl) {
    2425        auto enumInst = decl->type.strict_as<ast::EnumInstType>();
     
    8384    }
    8485
    85     if (fname == "labelE" || fname == "valueE" || fname == "posE" ||
    86         fname == "pred" || fname == "succ") {
     86    if (fname == "labelE" || fname == "valueE" || fname == "posE") {
    8787        visit_children = false;
    8888    }
     
    226226}
    227227
    228 ast::ApplicationExpr const* resolveAttributeFunctions(
     228ast::ApplicationExpr const* getPseudoFuncApplication(
    229229    const CodeLocation location, ResolvExpr::ResolveContext context,
    230230    const ast::VariableExpr* arg, const ast::EnumDecl* base,
     
    241241        auto rep = argAsDecl->accept(replacer);
    242242        auto mutatedArg = ast::mutate_field(arg, &ast::VariableExpr::var, rep);
    243         mutatedArg = ast::mutate_field(mutatedArg, &ast::VariableExpr::result,
    244                                        mutatedArg->var->get_type());
    245243        auto untyped =
    246244            new ast::UntypedExpr(location, new ast::NameExpr(location, "?[?]"),
     
    292290                                                              referredName);
    293291                    else {
    294                         return resolveAttributeFunctions(
     292                        return getPseudoFuncApplication(
    295293                            location, context, arg.get(), base, "values_");
    296294                    }
     
    299297
    300298            if (fname == "labelE") {
    301                 if (auto labelExpr = resolveAttributeFunctions(
     299                if (auto labelExpr = getPseudoFuncApplication(
    302300                        location, context, arg.get(), base, "labels_")) {
    303301                    return labelExpr;
    304302                }
    305303            } else if (fname == "valueE") {
    306                 if (auto valueExpr = resolveAttributeFunctions(
     304                if (auto valueExpr = getPseudoFuncApplication(
    307305                        location, context, arg.get(), base, "values_")) {
    308306                    return valueExpr;
     
    316314            ResolvExpr::ResolveContext context{symtab, transUnit().global};
    317315            if (fname == "labelE") {
    318                 if (auto labelExpr = resolveAttributeFunctions(
     316                if (auto labelExpr = getPseudoFuncApplication(
    319317                        location, context, arg.get(), base, "labels_")) {
    320318                    return labelExpr;
    321319                }
    322320            } else if (fname == "valueE") {
    323                 if (auto valueExpr = resolveAttributeFunctions(
     321                if (auto valueExpr = getPseudoFuncApplication(
    324322                        location, context, arg.get(), base, "values_")) {
    325323                    return valueExpr;
     
    347345    const ast::Expr* postvisit(const ast::ApplicationExpr* expr) {
    348346        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 
    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;
     347        if (fname != "?[?]") return expr;
     348        if (expr->args.size() != 2) return expr;
     349
     350        auto arg1AsVar =
     351            reduceCastExpr(expr->args.front()).as<ast::VariableExpr>();
     352        auto arg2AsVar =
     353            reduceCastExpr(expr->args.back()).as<ast::VariableExpr>();
     354
     355        if (!arg1AsVar || !arg2AsVar) return expr;
     356
     357        auto arg1Asecl = arg1AsVar->var.as<ast::ObjectDecl>();
     358        auto arg2Asecl = arg2AsVar->var.as<ast::ObjectDecl>();
     359
     360        if (!arg1Asecl || !arg2Asecl) return expr;
     361        auto arrInst = arg1Asecl->type.as<ast::ArrayType>();
     362        auto pointerInst = arg1Asecl->type.as<ast::PointerType>();
     363        if (!arrInst && !pointerInst) {
     364            return expr;
     365        }
     366        auto enumInst = arg2Asecl->type.as<ast::EnumInstType>();
     367        if (!enumInst) return expr;
     368
     369        const std::string arrName = arg1Asecl->name;
     370        if (arrName != getValueArrayName(enumInst->base->name)) return expr;
     371        ast::Pass<ReplaceEnumInstWithPos> replacer;
     372        auto rep = arg2Asecl->accept(replacer);
     373        if (!rep) return expr;
     374        auto mutObj =
     375            ast::mutate_field(arg2AsVar, &ast::VariableExpr::var, rep);
     376        auto mut = ast::mutate_field_index(expr, &ast::ApplicationExpr::args, 1,
     377                                           mutObj);
     378        return mut;
    516379    }
    517380};
     
    522385    ast::Pass<WrapEnumValueExpr>::run(translationUnit);
    523386    ast::Pass<FindGenEnumArray>::run(translationUnit);
    524 
    525387    ast::Pass<PseudoFuncGenerateRoutine>::run(translationUnit);
    526388    ast::Pass<ReplacePseudoFuncCore>::run(translationUnit);
    527389    ast::Pass<ReplaceEnumInst>::run(translationUnit);
    528 
    529     ast::Pass<ReplaceSuccAndPred>::run(translationUnit);
    530390}
    531391}  // namespace Validate
Note: See TracChangeset for help on using the changeset viewer.