Changeset 69867ad9 for src/Validate


Ignore:
Timestamp:
Apr 30, 2024, 12:08:46 PM (7 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
35897fb
Parents:
caaf424 (diff), 0153dbd (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/Validate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/HoistStruct.cpp

    rcaaf424 r69867ad9  
    2727namespace {
    2828
     29/// Is this a declaration can appear in a struct/union and should be hoisted?
    2930bool shouldHoist( ast::Decl const * decl ) {
    3031        return dynamic_cast< ast::StructDecl const * >( decl )
     
    3435}
    3536
    36 /* This pass also does some renaming and internal field alteration, but the
    37  * complex part is the actual hoisting. Hoisted declarations should always
     37/// Helper that updates an InstType if the base name could be updated.
     38template<typename InstType>
     39InstType const * preInstType( InstType const * type ) {
     40        assert( type->base );
     41        if ( nullptr == type->base->parent ) return type;
     42        auto mut = ast::mutate( type );
     43        mut->name = mut->base->name;
     44        return mut;
     45}
     46
     47/// Update StructInstType and UnionInstType names.
     48struct NameUpdater {
     49        ast::StructInstType const * previsit( ast::StructInstType const * type ) {
     50                return preInstType( type );
     51        }
     52
     53        ast::UnionInstType const * previsit( ast::UnionInstType const * type ) {
     54                return preInstType( type );
     55        }
     56};
     57
     58ast::Decl const * updateNames( ast::Decl const * decl ) {
     59        ast::Pass<NameUpdater> visitor;
     60        return decl->accept( visitor );
     61}
     62
     63/* This pass hoists from structs/unions. Hoisted declarations should always
    3864 * appear before the declaration they are hoisted out of and if two types are
    3965 * nested in the same declaration their order should not change.
     66 * It also sets up parent relationships, does name mangling of hoisted types
     67 * and updates instance types of the hoisted types.
    4068 */
    4169struct HoistStructCore final :
     
    96124                auto mut = ast::mutate( decl );
    97125                mut->parent = parent;
    98                 mut->name = qualifiedName( mut );
    99126                extendParams( mut->params, parent->params );
    100127                decl = mut;
     
    116143                }
    117144        }
     145        // Is this a nested type? Then update the name, after the parent's name
     146        // has been updated (hence the post visit).
     147        if ( mut->parent ) {
     148                mut->name = qualifiedName( mut );
     149        // Top level type that has hoisted? Then do a second pass subpass to make
     150        // sure we update instance type names after the declaration is renamed.
     151        } else if ( !declsToAddBefore.empty() ) {
     152                for ( ast::ptr<ast::Decl> & member : mut->members ) {
     153                        member = updateNames( member.get() );
     154                }
     155                for ( ast::ptr<ast::Decl> & declToAdd : declsToAddBefore ) {
     156                        declToAdd = updateNames( declToAdd );
     157                }
     158        }
    118159        return mut;
    119160}
     
    167208}
    168209
    169 template<typename InstType>
    170 InstType const * preInstType( InstType const * type ) {
    171         assert( type->base );
    172         auto mut = ast::mutate( type );
    173         mut->name = mut->base->name;
    174         return mut;
    175 }
    176 
    177210ast::StructInstType const * HoistStructCore::previsit( ast::StructInstType const * type ) {
    178211        return preInstType( preCollectionInstType( type ) );
  • src/Validate/ImplementEnumFunc.cpp

    rcaaf424 r69867ad9  
    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;
     
    289308
    290309ast::FunctionDecl* EnumAttrFuncGenerator::genPosnProto() const {
    291         return genProto(
    292                 "posE",
    293                 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
    294                 {new ast::ObjectDecl(getLocation(), "_ret",
    295                         new ast::BasicType(ast::BasicKind::UnsignedInt))});
     310    return genProto(
     311        "posE",
     312        {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
     313        {new ast::ObjectDecl(getLocation(), "_ret",
     314            new ast::BasicType(ast::BasicKind::UnsignedInt))});
    296315}
    297316
     
    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);
Note: See TracChangeset for help on using the changeset viewer.