Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/ImplementEnumFunc.cpp

    r85855b0 r822332e  
    1010        const ast::EnumDecl* decl;
    1111        unsigned int functionNesting;
    12         const ast::StructDecl* quasi_void_decl;
    1312        ast::Linkage::Spec proto_linkage;
    1413
     
    2524                : decl(decl),
    2625                  functionNesting{functionNesting},
    27                   quasi_void_decl(new ast::StructDecl(decl->location,
    28                         "quasi_void", ast::AggregateDecl::Struct,
    29                         {}, ast::Linkage::AutoGen)),
    3026                  proto_linkage{ast::Linkage::Cforall} {}
    3127
     
    5652        void genSuccPredBody(ast::FunctionDecl *, const char *) const;
    5753
    58         void genTypeNameFunc();
    59 
    6054        // Implement TypedEnum trait
    6155        void genTypedEnumFuncs();
     
    6458        ast::FunctionDecl* genLabelProto() const;
    6559        ast::FunctionDecl* genValueProto() const;
    66         ast::FunctionDecl* genQuasiValueProto() const;
    67         ast::FunctionDecl* genTypeNameProto() const;
    68 
    6960        void genValueOrLabelBody(
    7061                ast::FunctionDecl* func, ast::ObjectDecl* arrDecl) const;
    7162        void genPosnBody(ast::FunctionDecl* func) const;
    72         void genQuasiValueBody(ast::FunctionDecl* func) const;
    73         void genTypeNameBody(ast::FunctionDecl* func) const;
     63
     64        ////////////////
     65
     66        // ---------------------------------------------------
     67        // ast::FunctionDecl* genAttrCtorProto() const;
     68        /// Changes the node inside a pointer so that it has the unused attribute.
     69        void addUnusedAttribute(ast::ptr<ast::DeclWithType>& declPtr) {
     70                ast::DeclWithType* decl = declPtr.get_and_mutate();
     71                decl->attributes.push_back(new ast::Attribute("unused"));
     72        }
    7473
    7574        // ----------------------------------------------------
     
    118117        return inits;
    119118}
    120 
    121119const ast::Init* EnumAttrFuncGenerator::getAutoInit(
    122120        const ast::Init* prev) const {
     
    191189
    192190ast::FunctionDecl* EnumAttrFuncGenerator::genValueProto() const {
    193         if (decl->base)
    194                 return genProto(
    195                         "valueE",
    196                         {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
    197                         {new ast::ObjectDecl(getLocation(), "_ret",
    198                                                                 ast::deepCopy(decl->base))});
    199         else
    200                 return genQuasiValueProto();
    201 }
    202 
    203 ast::FunctionDecl* EnumAttrFuncGenerator::genQuasiValueProto() const {
    204191        return genProto(
    205192                "valueE",
    206193                {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
    207194                {new ast::ObjectDecl(getLocation(), "_ret",
    208                                                 new ast::StructInstType(quasi_void_decl))});
     195                                     ast::deepCopy(decl->base))});
    209196}
    210197
     
    223210                {new ast::ObjectDecl(getLocation(), "_ret", new ast::BasicType(ast::BasicKind::UnsignedInt))}
    224211        );
    225 }
    226 
    227 ast::FunctionDecl* EnumAttrFuncGenerator::genTypeNameProto() const {
    228         return genProto(
    229                 "type_name",
    230                 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
    231                 {new ast::ObjectDecl(
    232                         getLocation(), "_ret",
    233                         new ast::PointerType(new ast::BasicType{ast::BasicKind::Char}))});
    234212}
    235213
     
    290268        );
    291269}
     270
    292271
    293272void EnumAttrFuncGenerator::genSerialTraitFuncs() {
     
    323302        const CodeLocation & loc = func->location;
    324303        auto mem = func->name=="lowerBound"?  decl->members.front() : decl->members.back();
    325         // auto expr = new ast::NameExpr( loc, mem->name );
    326         auto expr = new ast::QualifiedNameExpr( loc, decl->name, mem->name );
     304        auto expr = new ast::NameExpr( loc, mem->name );
    327305        func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)});
    328306}
     
    371349}
    372350
    373 void EnumAttrFuncGenerator::genQuasiValueBody(ast::FunctionDecl* func) const {
    374         auto location = func->location;
    375         const ast::ObjectDecl * objDecl = new ast::ObjectDecl(
    376                 location, "_out", new ast::StructInstType( quasi_void_decl ));
    377         const ast::DeclStmt * declStmt = new ast::DeclStmt(location, objDecl);
    378         const ast::VariableExpr * varExpr = new ast::VariableExpr(location, objDecl);
    379         const ast::ReturnStmt * retStmt = new ast::ReturnStmt(location, varExpr);
    380 
    381         func->stmts = new ast::CompoundStmt(
    382                 location, {declStmt, retStmt}
    383         );
    384 }
    385 
    386351void EnumAttrFuncGenerator::genPosnBody(ast::FunctionDecl* func) const {
    387352        auto castExpr = new ast::CastExpr(
     
    394359}
    395360
    396 void EnumAttrFuncGenerator::genTypeNameBody(ast::FunctionDecl* func) const {
    397         const ast::Expr * type_name = ast::ConstantExpr::from_string(func->location, decl->name);
    398         func->stmts = new ast::CompoundStmt(
    399                 func->location, {new ast::ReturnStmt(func->location, type_name)}
    400         );
    401 }
    402 
    403361void EnumAttrFuncGenerator::genTypedEnumFunction(const ast::EnumAttribute attr) {
    404         if (attr == ast::EnumAttribute::Value) {
    405                 if (decl->base) {
    406                         // TypedEnum's backing arrays
    407                         std::vector<ast::ptr<ast::Init>> inits = genValueInit();
    408                         ast::ObjectDecl* arrayProto =
    409                                 genAttrArrayProto(attr, getLocation(), inits);
    410                         forwards.push_back(arrayProto);
    411 
    412                         ast::FunctionDecl* funcProto = genValueProto();
    413                         produceForwardDecl(funcProto);
    414                         genValueOrLabelBody(funcProto, arrayProto);
    415                         produceDecl(funcProto);
    416                 }  else {
    417                         ast::FunctionDecl* funcProto = genQuasiValueProto();
    418                         produceForwardDecl(funcProto);
    419                         genQuasiValueBody(funcProto);
    420                         produceDecl(funcProto);
    421                 }
    422         } else if (attr == ast::EnumAttribute::Label) {
    423                 std::vector<ast::ptr<ast::Init>> inits = genLabelInit();
     362        if (attr == ast::EnumAttribute::Value ||
     363                attr == ast::EnumAttribute::Label) {
     364                // TypedEnum's backing arrays
     365                std::vector<ast::ptr<ast::Init>> inits =
     366                        attr == ast::EnumAttribute::Value ? genValueInit() : genLabelInit();
    424367                ast::ObjectDecl* arrayProto =
    425368                        genAttrArrayProto(attr, getLocation(), inits);
    426369                forwards.push_back(arrayProto);
    427                 ast::FunctionDecl* funcProto = genLabelProto();
     370
     371                ast::FunctionDecl* funcProto = ( attr == ast::EnumAttribute::Value )
     372                                               ? genValueProto()
     373                                               : genLabelProto();
    428374                produceForwardDecl(funcProto);
    429375                genValueOrLabelBody(funcProto, arrayProto);
     
    438384
    439385void EnumAttrFuncGenerator::genTypedEnumFuncs() {
    440         genTypedEnumFunction(ast::EnumAttribute::Value);
     386        if (decl->base) genTypedEnumFunction(ast::EnumAttribute::Value);
    441387        genTypedEnumFunction(ast::EnumAttribute::Label);
    442388        genTypedEnumFunction(ast::EnumAttribute::Posn);
    443 }
    444 
    445 void EnumAttrFuncGenerator::genTypeNameFunc() {
    446         ast::FunctionDecl* funcProto = genTypeNameProto();
    447         produceForwardDecl(funcProto);
    448         genTypeNameBody(funcProto);
    449         produceDecl(funcProto);
    450389}
    451390
     
    453392        std::list<ast::ptr<ast::Decl>>& decls) {
    454393        // Generate the functions (they go into forwards and definitions).
    455         genTypeNameFunc();
    456394        genTypedEnumFuncs();
    457395        genSerialTraitFuncs();
Note: See TracChangeset for help on using the changeset viewer.