Changeset eb7586e for src


Ignore:
Timestamp:
Apr 28, 2024, 7:50:11 PM (3 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
c5c123f
Parents:
7042c60
Message:
  1. Change return value of typed Enum in null context: they now return the position. Therefore, printf with enumeration value will no longer be supported. 2. sout now will return the enumeration value. So sout | enumValue will print what we expect. 3. Provide enum.hfa, which contains traits that related to enum. 4. Implement functions declare in enum.hfa for enum types, so enum type fulfill the traits. Known defeat: error if we use the enum traits on enum types. They work but c compiler gives an warning
Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CandidateFinder.cpp

    r7042c60 reb7586e  
    906906                                }
    907907                                CandidateRef & choice = winners.front();
    908                                 choice->cost.incVar();
     908                                choice->cost.incSafe();
    909909                                candidates.emplace_back( std::move(choice) );
    910910                        }
     
    13761376                        ast::Expr * newExpr = data.combine( nameExpr->location, cost );
    13771377
    1378                         bool bentConversion = false;
    1379                         if ( auto inst = newExpr->result.as<ast::EnumInstType>() ) {
    1380                                 if ( inst->base && inst->base->base ) {
    1381                                         bentConversion = true;
    1382                                 }
    1383                         }
    1384 
     1378                        // bool bentConversion = false;
     1379                        // if ( auto inst = newExpr->result.as<ast::EnumInstType>() ) {
     1380                        //      if ( inst->base && inst->base->base ) {
     1381                        //              bentConversion = true;
     1382                        //      }
     1383                        // }
     1384
     1385                        // CandidateRef newCand = std::make_shared<Candidate>(
     1386                        //      newExpr, copy( tenv ), ast::OpenVarSet{}, ast::AssertionSet{}, bentConversion? Cost::safe: Cost::zero,
     1387                        //      cost );
    13851388                        CandidateRef newCand = std::make_shared<Candidate>(
    1386                                 newExpr, copy( tenv ), ast::OpenVarSet{}, ast::AssertionSet{}, bentConversion? Cost::safe: Cost::zero,
     1389                                newExpr, copy( tenv ), ast::OpenVarSet{}, ast::AssertionSet{}, Cost::zero,
    13871390                                cost );
    1388 
    13891391                        if (newCand->expr->env) {
    13901392                                newCand->env.add(*newCand->expr->env);
     
    18291831                                        Cost cost = Cost::zero;
    18301832                                        ast::Expr * newExpr = data.combine( expr->location, cost );
     1833                                        // CandidateRef newCand =
     1834                                        //      std::make_shared<Candidate>(
     1835                                        //              newExpr, copy( tenv ), ast::OpenVarSet{},
     1836                                        //              ast::AssertionSet{}, Cost::safe, cost
     1837                                        //      );
    18311838                                        CandidateRef newCand =
    18321839                                                std::make_shared<Candidate>(
    18331840                                                        newExpr, copy( tenv ), ast::OpenVarSet{},
    1834                                                         ast::AssertionSet{}, Cost::safe, cost
     1841                                                        ast::AssertionSet{}, Cost::zero, cost
    18351842                                                );
    1836 
    18371843                                        if (newCand->expr->env) {
    18381844                                                newCand->env.add(*newCand->expr->env);
  • src/ResolvExpr/CastCost.cc

    r7042c60 reb7586e  
    5353                        } else {
    5454                                cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env );
     55                                if ( Cost::unsafe < cost ) {
     56                                        if (auto enumInst =  dynamic_cast<const ast::EnumInstType *>(dst)) {
     57                                                assert(enumInst->base->base);
     58                                                cost = Cost::unsafe;
     59                                        }
     60                                }
    5561                        }
     62                }
     63
     64                void postvisit( const ast::ZeroType * zero ) {
     65                        // auto ptr = dynamic_cast< const ast::PointerType * >( dst );
     66                        // if ( ptr && basicType->isInteger() ) {
     67                        //      // needed for, e.g. unsigned long => void *
     68                        //      cost = Cost::unsafe;
     69                        // } else {
     70                        cost = conversionCost( zero, dst, srcIsLvalue, symtab, env );
     71                        if ( Cost::unsafe < cost ) {
     72                                if (auto enumInst =  dynamic_cast<const ast::EnumInstType *>(dst)) {
     73                                        assert(enumInst->base->base);
     74                                        cost = Cost::unsafe;
     75                                }
     76                        }
     77                        // }
     78                }
     79
     80                void postvisit( const ast::OneType * one ) {
     81                        // auto ptr = dynamic_cast< const ast::PointerType * >( dst );
     82                        // if ( ptr && basicType->isInteger() ) {
     83                        //      // needed for, e.g. unsigned long => void *
     84                        //      cost = Cost::unsafe;
     85                        // } else {
     86                        cost = conversionCost( one, dst, srcIsLvalue, symtab, env );
     87                        if ( Cost::unsafe < cost ) {
     88                                if (auto enumInst =  dynamic_cast<const ast::EnumInstType *>(dst)) {
     89                                        assert(enumInst->base->base);
     90                                        cost = Cost::unsafe;
     91                                }
     92                        }
     93                        // }
    5694                }
    5795
     
    80118                                        cost = Cost::unsafe;
    81119                                }
     120                        }
     121                }
     122
     123                void postvist( const ast::EnumInstType * ) {
     124                        if ( auto basic = dynamic_cast< const ast::BasicType * >(dst) ) {
     125                                if ( basic->isInteger() ) cost = Cost::unsafe;
    82126                        }
    83127                }
  • src/ResolvExpr/ConversionCost.cc

    r7042c60 reb7586e  
    284284        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    285285                if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) {
    286                         cost = Cost::zero;
    287                         cost.incUnsafe();
     286                        cost = Cost::unsafe;
    288287                }
    289288        }
     
    482481        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    483482                if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) {
    484                         cost = Cost::zero;
    485                         cost.incUnsafe();
     483                        cost = Cost::unsafe;
    486484                }
    487485        }
     
    504502        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    505503                if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) {
    506                         cost = Cost::zero;
    507                         cost.incUnsafe();
     504                        cost = Cost::unsafe;
    508505                }
    509506        }
  • src/Validate/ImplementEnumFunc.cpp

    r7042c60 reb7586e  
    3030        void genAttrFunctions();
    3131        void genSuccPredPosn();
    32         void genSuccPredDecl();
     32        // void genSuccPredDecl();
    3333
    3434        void appendReturnThis(ast::FunctionDecl* decl) {
     
    7373        const ast::Decl* getDecl() const { return decl; }
    7474
     75        // Implement Bounded trait for enum
     76    void genBoundedFunctions();
     77        // Implement Serial trait for enum
     78        void genSerialTraitFuncs();
     79
     80        // Bounded trait
     81        ast::FunctionDecl* genLowerBoundProto() const;
     82        ast::FunctionDecl* genUpperBoundProto() const;
     83        void genLowerBoundBody(ast::FunctionDecl* func) const;
     84        void genUpperBoundBody(ast::FunctionDecl* func) const;
     85
    7586        ast::FunctionDecl* genPosnProto() const;
    7687        ast::FunctionDecl* genLabelProto() const;
    7788        ast::FunctionDecl* genValueProto() const;
     89
     90        // Serial trait
     91        ast::FunctionDecl* genFromIntProto() const;
     92        ast::FunctionDecl* genFromInstanceProto() const;
    7893        ast::FunctionDecl* genSuccProto() const;
    7994        ast::FunctionDecl* genPredProto() const;
     95
     96        void genFromIntBody(ast::FunctionDecl *) const;
     97        void genFromInstanceBody(ast::FunctionDecl *) const;
     98        ////////////////
    8099
    81100        ast::FunctionDecl* genSuccPosProto() const;
     
    313332}
    314333
     334ast::FunctionDecl* EnumAttrFuncGenerator::genFromIntProto() const {
     335        return genProto(
     336                "fromInt",
     337                {new ast::ObjectDecl(getLocation(), "_i", new ast::BasicType(ast::BasicKind::UnsignedInt))},
     338                {new ast::ObjectDecl(getLocation(), "_ret", new ast::EnumInstType(decl))}
     339        );
     340}
     341
     342ast::FunctionDecl* EnumAttrFuncGenerator::genFromInstanceProto() const {
     343        return genProto(
     344                "fromInstance",
     345                {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
     346                {new ast::ObjectDecl(getLocation(), "_ret", new ast::BasicType(ast::BasicKind::UnsignedInt))}
     347        );
     348}
     349
     350void EnumAttrFuncGenerator::genFromIntBody(ast::FunctionDecl* func) const {
     351        auto params = func->params;
     352        assert( params.size() == 1 );
     353        auto param = params.front();
     354        auto castExpr = new ast::CastExpr(
     355                func->location,
     356                new ast::VariableExpr(func->location, param),
     357                new ast::EnumInstType(decl),
     358                ast::GeneratedFlag::ExplicitCast
     359        );
     360        func->stmts = new ast::CompoundStmt(
     361                func->location, {new ast::ReturnStmt(func->location, castExpr)}
     362        );
     363}
     364
     365void EnumAttrFuncGenerator::genFromInstanceBody(ast::FunctionDecl* func) const {
     366        auto params = func->params;
     367        assert( params.size() == 1 );
     368        auto param = params.front();
     369        ast::UntypedExpr* untyped = ast::UntypedExpr::createCall(
     370                func->location, "posE", { new ast::VariableExpr(func->location, param) });
     371        func->stmts = new ast::CompoundStmt(
     372                func->location, {new ast::ReturnStmt(func->location, untyped)}
     373        );
     374}
     375
     376void EnumAttrFuncGenerator::genSerialTraitFuncs() {
     377        auto fromIntProto = genFromIntProto();
     378        produceForwardDecl(fromIntProto);
     379        genFromIntBody(fromIntProto);
     380        produceDecl(fromIntProto);
     381
     382        auto fromInstanceProto = genFromInstanceProto();
     383        produceForwardDecl(fromInstanceProto);
     384        genFromInstanceBody(fromInstanceProto);
     385        produceDecl(fromInstanceProto);
     386
     387        auto succProto = genSuccProto();
     388        auto predProto = genPredProto();
     389        produceForwardDecl(succProto);
     390        produceForwardDecl(predProto);
     391}
     392
    315393ast::FunctionDecl* EnumAttrFuncGenerator::genSuccProto() const {
    316394        return genProto(
     
    327405                {new ast::ObjectDecl(getLocation(), "_ret",
    328406                                     new ast::EnumInstType(decl))});
     407}
     408
     409ast::FunctionDecl* EnumAttrFuncGenerator::genLowerBoundProto() const {
     410    return genProto("lowerBound", {}, {
     411        new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))
     412    });
     413}
     414
     415ast::FunctionDecl* EnumAttrFuncGenerator::genUpperBoundProto() const {
     416    return genProto("upperBound", {}, {
     417        new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))
     418    });
     419}
     420
     421void EnumAttrFuncGenerator::genLowerBoundBody(ast::FunctionDecl* func) const {
     422        const CodeLocation & loc = func->location;
     423        auto mem = decl->members.front();
     424        // auto expr = new ast::QualifiedNameExpr( loc, decl, mem->name );
     425        // expr->result = new ast::EnumInstType( decl );
     426        auto expr = new ast::NameExpr( loc, mem->name );
     427        func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)});
     428}
     429
     430void EnumAttrFuncGenerator::genUpperBoundBody(ast::FunctionDecl* func) const {
     431        const CodeLocation & loc = func->location;
     432        auto mem = decl->members.back();
     433        auto expr = new ast::NameExpr( loc, mem->name );
     434        // expr->result = new ast::EnumInstType( decl );
     435        func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)});   
     436}
     437
     438void EnumAttrFuncGenerator::genBoundedFunctions() {
     439        ast::FunctionDecl * upperDecl = genUpperBoundProto();
     440        produceForwardDecl(upperDecl);
     441        genUpperBoundBody(upperDecl);
     442        produceDecl(upperDecl);
     443
     444        ast::FunctionDecl * lowerDecl = genLowerBoundProto();
     445        produceForwardDecl(lowerDecl);
     446        genLowerBoundBody(lowerDecl);
     447        produceDecl(lowerDecl);
    329448}
    330449
     
    373492                func->location, "?[?]",
    374493                {new ast::NameExpr(func->location, arrDecl->name),
    375                         new ast::CastExpr(
    376                                 func->location,
    377                                 new ast::VariableExpr( func->location, func->params.front() ),
    378                                 new ast::EnumAttrType( new ast::EnumInstType(decl),
    379                                         ast::EnumAttribute::Posn))});
     494                new ast::CastExpr(
     495                        func->location,
     496                        new ast::VariableExpr( func->location, func->params.front() ),
     497                        new ast::EnumAttrType( new ast::EnumInstType(decl),
     498                                ast::EnumAttribute::Posn))});
    380499        func->stmts = new ast::CompoundStmt(
    381500                func->location, {new ast::ReturnStmt(func->location, untyped)});
     
    450569
    451570void EnumAttrFuncGenerator::genAttrFunctions() {
    452         if (decl->base) {
    453                 genAttributesDecls(ast::EnumAttribute::Value);
    454                 genAttributesDecls(ast::EnumAttribute::Label);
    455                 genAttributesDecls(ast::EnumAttribute::Posn);
    456         }
    457 }
    458 
    459 void EnumAttrFuncGenerator::genSuccPredDecl() {
    460         if (decl->base) {
    461                 auto succProto = genSuccProto();
    462                 auto predProto = genPredProto();
    463 
    464                 produceForwardDecl(succProto);
    465                 produceForwardDecl(predProto);
    466         }
    467 }
     571        genAttributesDecls(ast::EnumAttribute::Value);
     572        genAttributesDecls(ast::EnumAttribute::Label);
     573        genAttributesDecls(ast::EnumAttribute::Posn);   
     574}
     575
     576// void EnumAttrFuncGenerator::genSuccPredDecl() {
     577//      auto succProto = genSuccProto();
     578//      auto predProto = genPredProto();
     579
     580//      produceForwardDecl(succProto);
     581//      produceForwardDecl(predProto);
     582// }
    468583
    469584void EnumAttrFuncGenerator::genSuccPredPosn() {
    470         if (decl->base) {
    471                 ast::FunctionDecl* succ = genSuccPredFunc(true);
    472                 ast::FunctionDecl* pred = genSuccPredFunc(false);
    473 
    474                 produceDecl(succ);
    475                 produceDecl(pred);
    476         }
     585        ast::FunctionDecl* succ = genSuccPredFunc(true);
     586        ast::FunctionDecl* pred = genSuccPredFunc(false);
     587
     588        produceDecl(succ);
     589        produceDecl(pred);
    477590}
    478591
     
    482595        genAttrStandardFuncs();
    483596        genAttrFunctions();
    484         genSuccPredDecl();
    485         genSuccPredPosn(); // Posn
     597        genSerialTraitFuncs();
     598        genSuccPredPosn();
     599        // problematic
     600        genBoundedFunctions();
    486601        // Now export the lists contents.
    487602        decls.splice(decls.end(), forwards);
  • src/main.cc

    r7042c60 reb7586e  
    325325                PASS( "Validate Generic Parameters", Validate::fillGenericParameters, transUnit );
    326326                PASS( "Translate Dimensions", Validate::translateDimensionParameters, transUnit );
     327                // Need to happen before fixing returns because implementEnumFunc has ReturnStmt
     328               
     329                PASS( "Generate Enum Attributes Functions", Validate::implementEnumFunc, transUnit );
    327330                PASS( "Check Function Returns", Validate::checkReturnStatements, transUnit );
    328331                PASS( "Fix Return Statements", InitTweak::fixReturnStatements, transUnit );
     
    333336
    334337                PASS( "Generate Autogen Routines", Validate::autogenerateRoutines, transUnit );
    335                 PASS( "Generate Enum Attributes Functions", Validate::implementEnumFunc, transUnit );
    336 
     338               
    337339                PASS( "Implement Actors", Concurrency::implementActors, transUnit );
    338340                PASS( "Implement Virtual Destructors", Virtual::implementVirtDtors, transUnit );
Note: See TracChangeset for help on using the changeset viewer.