- File:
-
- 1 edited
-
src/Validate/ImplementEnumFunc.cpp (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Validate/ImplementEnumFunc.cpp
r85855b0 r822332e 10 10 const ast::EnumDecl* decl; 11 11 unsigned int functionNesting; 12 const ast::StructDecl* quasi_void_decl;13 12 ast::Linkage::Spec proto_linkage; 14 13 … … 25 24 : decl(decl), 26 25 functionNesting{functionNesting}, 27 quasi_void_decl(new ast::StructDecl(decl->location,28 "quasi_void", ast::AggregateDecl::Struct,29 {}, ast::Linkage::AutoGen)),30 26 proto_linkage{ast::Linkage::Cforall} {} 31 27 … … 56 52 void genSuccPredBody(ast::FunctionDecl *, const char *) const; 57 53 58 void genTypeNameFunc();59 60 54 // Implement TypedEnum trait 61 55 void genTypedEnumFuncs(); … … 64 58 ast::FunctionDecl* genLabelProto() const; 65 59 ast::FunctionDecl* genValueProto() const; 66 ast::FunctionDecl* genQuasiValueProto() const;67 ast::FunctionDecl* genTypeNameProto() const;68 69 60 void genValueOrLabelBody( 70 61 ast::FunctionDecl* func, ast::ObjectDecl* arrDecl) const; 71 62 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 } 74 73 75 74 // ---------------------------------------------------- … … 118 117 return inits; 119 118 } 120 121 119 const ast::Init* EnumAttrFuncGenerator::getAutoInit( 122 120 const ast::Init* prev) const { … … 191 189 192 190 ast::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 else200 return genQuasiValueProto();201 }202 203 ast::FunctionDecl* EnumAttrFuncGenerator::genQuasiValueProto() const {204 191 return genProto( 205 192 "valueE", 206 193 {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, 207 194 {new ast::ObjectDecl(getLocation(), "_ret", 208 new ast::StructInstType(quasi_void_decl))});195 ast::deepCopy(decl->base))}); 209 196 } 210 197 … … 223 210 {new ast::ObjectDecl(getLocation(), "_ret", new ast::BasicType(ast::BasicKind::UnsignedInt))} 224 211 ); 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}))});234 212 } 235 213 … … 290 268 ); 291 269 } 270 292 271 293 272 void EnumAttrFuncGenerator::genSerialTraitFuncs() { … … 323 302 const CodeLocation & loc = func->location; 324 303 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 ); 327 305 func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)}); 328 306 } … … 371 349 } 372 350 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 386 351 void EnumAttrFuncGenerator::genPosnBody(ast::FunctionDecl* func) const { 387 352 auto castExpr = new ast::CastExpr( … … 394 359 } 395 360 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 403 361 void 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(); 424 367 ast::ObjectDecl* arrayProto = 425 368 genAttrArrayProto(attr, getLocation(), inits); 426 369 forwards.push_back(arrayProto); 427 ast::FunctionDecl* funcProto = genLabelProto(); 370 371 ast::FunctionDecl* funcProto = ( attr == ast::EnumAttribute::Value ) 372 ? genValueProto() 373 : genLabelProto(); 428 374 produceForwardDecl(funcProto); 429 375 genValueOrLabelBody(funcProto, arrayProto); … … 438 384 439 385 void EnumAttrFuncGenerator::genTypedEnumFuncs() { 440 genTypedEnumFunction(ast::EnumAttribute::Value);386 if (decl->base) genTypedEnumFunction(ast::EnumAttribute::Value); 441 387 genTypedEnumFunction(ast::EnumAttribute::Label); 442 388 genTypedEnumFunction(ast::EnumAttribute::Posn); 443 }444 445 void EnumAttrFuncGenerator::genTypeNameFunc() {446 ast::FunctionDecl* funcProto = genTypeNameProto();447 produceForwardDecl(funcProto);448 genTypeNameBody(funcProto);449 produceDecl(funcProto);450 389 } 451 390 … … 453 392 std::list<ast::ptr<ast::Decl>>& decls) { 454 393 // Generate the functions (they go into forwards and definitions). 455 genTypeNameFunc();456 394 genTypedEnumFuncs(); 457 395 genSerialTraitFuncs();
Note:
See TracChangeset
for help on using the changeset viewer.