[af746cc] | 1 | #include "AST/Create.hpp" |
---|
| 2 | #include "AST/Pass.hpp" |
---|
| 3 | #include "AST/TranslationUnit.hpp" |
---|
| 4 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign |
---|
| 5 | #include "InitTweak/InitTweak.h" // for isAssignment, isCopyConstructor |
---|
| 6 | namespace Validate { |
---|
| 7 | |
---|
| 8 | namespace { |
---|
| 9 | class EnumAttrFuncGenerator { |
---|
[fc1a3e2] | 10 | const ast::EnumDecl* decl; |
---|
| 11 | const ast::EnumInstType* instType; |
---|
| 12 | unsigned int functionNesting; |
---|
| 13 | ast::Linkage::Spec proto_linkage; |
---|
| 14 | |
---|
| 15 | public: |
---|
| 16 | std::list<ast::ptr<ast::Decl>> forwards; |
---|
| 17 | std::list<ast::ptr<ast::Decl>> definitions; |
---|
| 18 | |
---|
| 19 | void generateAndAppendFunctions(std::list<ast::ptr<ast::Decl>>&); |
---|
| 20 | |
---|
| 21 | EnumAttrFuncGenerator( |
---|
| 22 | const ast::EnumDecl* decl, |
---|
| 23 | const ast::EnumInstType* instType, |
---|
| 24 | unsigned int functionNesting ) |
---|
| 25 | : decl(decl), |
---|
| 26 | instType{instType}, |
---|
| 27 | functionNesting{functionNesting}, |
---|
| 28 | proto_linkage{ast::Linkage::Cforall} {} |
---|
| 29 | |
---|
| 30 | private: |
---|
| 31 | const CodeLocation& getLocation() const { return decl->location; } |
---|
| 32 | |
---|
| 33 | ast::FunctionDecl* genProto( |
---|
| 34 | std::string&& name, std::vector<ast::ptr<ast::DeclWithType>>&& params, |
---|
| 35 | std::vector<ast::ptr<ast::DeclWithType>>&& returns) const; |
---|
| 36 | |
---|
| 37 | void produceDecl(const ast::FunctionDecl* decl); |
---|
| 38 | void produceForwardDecl(const ast::FunctionDecl* decl); |
---|
| 39 | |
---|
| 40 | const ast::Decl* getDecl() const { return decl; } |
---|
| 41 | |
---|
[c333ed2] | 42 | // Implement Bounded trait |
---|
| 43 | void genBoundedFunctions(); |
---|
| 44 | ast::FunctionDecl* genBoundedProto(const char *) const; |
---|
| 45 | void genBoundedBody(ast::FunctionDecl* func) const; |
---|
[eb7586e] | 46 | |
---|
[c333ed2] | 47 | // Implement Serial trait |
---|
| 48 | void genSerialTraitFuncs(); |
---|
| 49 | ast::FunctionDecl* genFromIntProto() const; |
---|
| 50 | ast::FunctionDecl* genFromInstanceProto() const; |
---|
| 51 | ast::FunctionDecl* genInstToInstFuncProto(const char* func) const; |
---|
| 52 | void genFromIntBody(ast::FunctionDecl *) const; |
---|
| 53 | void genFromInstanceBody(ast::FunctionDecl *) const; |
---|
| 54 | void genSuccPredBody(ast::FunctionDecl *, const char *) const; |
---|
[eb7586e] | 55 | |
---|
[c333ed2] | 56 | // Implement TypedEnum trait |
---|
| 57 | void genTypedEnumFuncs(); |
---|
| 58 | void genTypedEnumFunction(const ast::EnumAttribute attr); |
---|
[fc1a3e2] | 59 | ast::FunctionDecl* genPosnProto() const; |
---|
| 60 | ast::FunctionDecl* genLabelProto() const; |
---|
| 61 | ast::FunctionDecl* genValueProto() const; |
---|
[c333ed2] | 62 | void genValueOrLabelBody( |
---|
| 63 | ast::FunctionDecl* func, ast::ObjectDecl* arrDecl) const; |
---|
| 64 | void genPosnBody(ast::FunctionDecl* func) const; |
---|
[eb7586e] | 65 | |
---|
| 66 | //////////////// |
---|
| 67 | |
---|
[fc1a3e2] | 68 | // --------------------------------------------------- |
---|
| 69 | // ast::FunctionDecl* genAttrCtorProto() const; |
---|
| 70 | /// Changes the node inside a pointer so that it has the unused attribute. |
---|
| 71 | void addUnusedAttribute(ast::ptr<ast::DeclWithType>& declPtr) { |
---|
| 72 | ast::DeclWithType* decl = declPtr.get_and_mutate(); |
---|
| 73 | decl->attributes.push_back(new ast::Attribute("unused")); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | ast::ObjectDecl* dstParam() const { |
---|
| 77 | return new ast::ObjectDecl(getLocation(), "_dst", |
---|
| 78 | new ast::ReferenceType(new ast::EnumAttrType( |
---|
| 79 | ast::deepCopy(instType)))); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | ast::ObjectDecl* srcParam() const { |
---|
| 83 | return new ast::ObjectDecl( |
---|
| 84 | getLocation(), "_src", |
---|
| 85 | new ast::EnumAttrType(ast::deepCopy(instType))); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // ---------------------------------------------------- |
---|
| 89 | |
---|
| 90 | const ast::Init* getAutoInit(const ast::Init* prev) const; |
---|
| 91 | |
---|
| 92 | std::vector<ast::ptr<ast::Init>> genLabelInit() const; |
---|
| 93 | |
---|
| 94 | std::vector<ast::ptr<ast::Init>> genValueInit() const; |
---|
| 95 | ast::ObjectDecl* genAttrArrayProto( |
---|
| 96 | const ast::EnumAttribute attr, const CodeLocation& location, |
---|
| 97 | std::vector<ast::ptr<ast::Init>>& inits) const; |
---|
[af746cc] | 98 | }; |
---|
| 99 | |
---|
| 100 | std::vector<ast::ptr<ast::Init>> EnumAttrFuncGenerator::genLabelInit() const { |
---|
[fc1a3e2] | 101 | std::vector<ast::ptr<ast::Init>> inits; |
---|
| 102 | for (size_t i = 0; i < decl->members.size(); i++) { |
---|
| 103 | ast::ptr<ast::Decl> mem = decl->members.at(i); |
---|
| 104 | auto memAsObjectDecl = mem.as<ast::ObjectDecl>(); |
---|
| 105 | assert(memAsObjectDecl); |
---|
| 106 | inits.emplace_back(new ast::SingleInit( |
---|
| 107 | mem->location, |
---|
| 108 | ast::ConstantExpr::from_string(mem->location, mem->name))); |
---|
| 109 | } |
---|
| 110 | return inits; |
---|
[af746cc] | 111 | } |
---|
| 112 | |
---|
| 113 | std::vector<ast::ptr<ast::Init>> EnumAttrFuncGenerator::genValueInit() const { |
---|
[fc1a3e2] | 114 | std::vector<ast::ptr<ast::Init>> inits; |
---|
| 115 | for (size_t i = 0; i < decl->members.size(); i++) { |
---|
| 116 | ast::ptr<ast::Decl> mem = decl->members.at(i); |
---|
| 117 | auto memAsObjectDecl = mem.as<ast::ObjectDecl>(); |
---|
| 118 | assert(memAsObjectDecl); |
---|
| 119 | if (memAsObjectDecl->init) { |
---|
| 120 | inits.emplace_back(memAsObjectDecl->init); |
---|
| 121 | } else { |
---|
| 122 | const CodeLocation& location = mem->location; |
---|
| 123 | if (i == 0) { |
---|
| 124 | inits.emplace_back(new ast::SingleInit( |
---|
| 125 | location, ast::ConstantExpr::from_int(mem->location, 0))); |
---|
| 126 | } else { |
---|
| 127 | inits.emplace_back(getAutoInit(inits.at(i - 1))); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | return inits; |
---|
[af746cc] | 132 | } |
---|
| 133 | const ast::Init* EnumAttrFuncGenerator::getAutoInit( |
---|
[fc1a3e2] | 134 | const ast::Init* prev) const { |
---|
| 135 | if (prev == nullptr) { |
---|
| 136 | return new ast::SingleInit( |
---|
| 137 | getLocation(), ast::ConstantExpr::from_int(getLocation(), 0)); |
---|
| 138 | } |
---|
| 139 | auto prevInit = dynamic_cast<const ast::SingleInit*>(prev); |
---|
| 140 | assert(prevInit); |
---|
| 141 | auto prevInitExpr = prevInit->value; |
---|
| 142 | if (auto constInit = prevInitExpr.as<ast::ConstantExpr>()) { |
---|
| 143 | // Assume no string literal for now |
---|
| 144 | return new ast::SingleInit( |
---|
| 145 | getLocation(), ast::ConstantExpr::from_int( |
---|
| 146 | getLocation(), constInit->intValue() + 1)); |
---|
| 147 | } else { |
---|
| 148 | auto untypedThisInit = new ast::UntypedExpr( |
---|
| 149 | getLocation(), new ast::NameExpr(getLocation(), "?++"), |
---|
| 150 | {prevInitExpr}); |
---|
| 151 | return new ast::SingleInit(getLocation(), untypedThisInit); |
---|
| 152 | } |
---|
[af746cc] | 153 | } |
---|
| 154 | |
---|
| 155 | ast::FunctionDecl* EnumAttrFuncGenerator::genProto( |
---|
[fc1a3e2] | 156 | std::string&& name, std::vector<ast::ptr<ast::DeclWithType>>&& params, |
---|
| 157 | std::vector<ast::ptr<ast::DeclWithType>>&& returns) const { |
---|
| 158 | ast::FunctionDecl* decl = new ast::FunctionDecl( |
---|
| 159 | // Auto-generated routines use the type declaration's location. |
---|
| 160 | getLocation(), std::move(name), {}, {}, std::move(params), |
---|
| 161 | std::move(returns), |
---|
| 162 | // Only a prototype, no body. |
---|
| 163 | nullptr, |
---|
| 164 | // Use static storage if we are at the top level. |
---|
| 165 | (0 < functionNesting) ? ast::Storage::Classes() : ast::Storage::Static, |
---|
| 166 | proto_linkage, std::vector<ast::ptr<ast::Attribute>>(), |
---|
| 167 | // Auto-generated routines are inline to avoid conflicts. |
---|
| 168 | ast::Function::Specs(ast::Function::Inline)); |
---|
| 169 | decl->fixUniqueId(); |
---|
| 170 | return decl; |
---|
[af746cc] | 171 | } |
---|
| 172 | |
---|
| 173 | void EnumAttrFuncGenerator::produceDecl(const ast::FunctionDecl* decl) { |
---|
[fc1a3e2] | 174 | assert(nullptr != decl->stmts); |
---|
[af746cc] | 175 | |
---|
[fc1a3e2] | 176 | definitions.push_back(decl); |
---|
[af746cc] | 177 | } |
---|
| 178 | |
---|
| 179 | void EnumAttrFuncGenerator::produceForwardDecl(const ast::FunctionDecl* decl) { |
---|
[fc1a3e2] | 180 | if (0 != functionNesting) return; |
---|
| 181 | ast::FunctionDecl* fwd = |
---|
| 182 | (decl->stmts) ? ast::asForward(decl) : ast::deepCopy(decl); |
---|
| 183 | fwd->fixUniqueId(); |
---|
| 184 | forwards.push_back(fwd); |
---|
[af746cc] | 185 | } |
---|
| 186 | |
---|
| 187 | ast::FunctionDecl* EnumAttrFuncGenerator::genPosnProto() const { |
---|
| 188 | return genProto( |
---|
| 189 | "posE", |
---|
| 190 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, |
---|
| 191 | {new ast::ObjectDecl(getLocation(), "_ret", |
---|
[cf191ac] | 192 | new ast::BasicType(ast::BasicKind::UnsignedInt))}); |
---|
[af746cc] | 193 | } |
---|
| 194 | |
---|
| 195 | ast::FunctionDecl* EnumAttrFuncGenerator::genLabelProto() const { |
---|
[fc1a3e2] | 196 | return genProto( |
---|
| 197 | "labelE", |
---|
| 198 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, |
---|
| 199 | {new ast::ObjectDecl( |
---|
| 200 | getLocation(), "_ret", |
---|
| 201 | new ast::PointerType(new ast::BasicType{ast::BasicKind::Char}))}); |
---|
[af746cc] | 202 | } |
---|
| 203 | |
---|
| 204 | ast::FunctionDecl* EnumAttrFuncGenerator::genValueProto() const { |
---|
[fc1a3e2] | 205 | return genProto( |
---|
| 206 | "valueE", |
---|
| 207 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, |
---|
| 208 | {new ast::ObjectDecl(getLocation(), "_ret", |
---|
| 209 | ast::deepCopy(decl->base))}); |
---|
[af746cc] | 210 | } |
---|
| 211 | |
---|
[eb7586e] | 212 | ast::FunctionDecl* EnumAttrFuncGenerator::genFromIntProto() const { |
---|
| 213 | return genProto( |
---|
| 214 | "fromInt", |
---|
| 215 | {new ast::ObjectDecl(getLocation(), "_i", new ast::BasicType(ast::BasicKind::UnsignedInt))}, |
---|
| 216 | {new ast::ObjectDecl(getLocation(), "_ret", new ast::EnumInstType(decl))} |
---|
| 217 | ); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | ast::FunctionDecl* EnumAttrFuncGenerator::genFromInstanceProto() const { |
---|
| 221 | return genProto( |
---|
| 222 | "fromInstance", |
---|
| 223 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, |
---|
| 224 | {new ast::ObjectDecl(getLocation(), "_ret", new ast::BasicType(ast::BasicKind::UnsignedInt))} |
---|
| 225 | ); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void EnumAttrFuncGenerator::genFromIntBody(ast::FunctionDecl* func) const { |
---|
| 229 | auto params = func->params; |
---|
| 230 | assert( params.size() == 1 ); |
---|
| 231 | auto param = params.front(); |
---|
| 232 | auto castExpr = new ast::CastExpr( |
---|
| 233 | func->location, |
---|
| 234 | new ast::VariableExpr(func->location, param), |
---|
| 235 | new ast::EnumInstType(decl), |
---|
| 236 | ast::GeneratedFlag::ExplicitCast |
---|
| 237 | ); |
---|
| 238 | func->stmts = new ast::CompoundStmt( |
---|
| 239 | func->location, {new ast::ReturnStmt(func->location, castExpr)} |
---|
| 240 | ); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | void EnumAttrFuncGenerator::genFromInstanceBody(ast::FunctionDecl* func) const { |
---|
| 244 | auto params = func->params; |
---|
| 245 | assert( params.size() == 1 ); |
---|
| 246 | auto param = params.front(); |
---|
| 247 | ast::UntypedExpr* untyped = ast::UntypedExpr::createCall( |
---|
| 248 | func->location, "posE", { new ast::VariableExpr(func->location, param) }); |
---|
| 249 | func->stmts = new ast::CompoundStmt( |
---|
| 250 | func->location, {new ast::ReturnStmt(func->location, untyped)} |
---|
| 251 | ); |
---|
| 252 | } |
---|
| 253 | |
---|
[c333ed2] | 254 | void EnumAttrFuncGenerator::genSuccPredBody(ast::FunctionDecl * func, const char* opt) const { |
---|
| 255 | auto params = func->params; |
---|
| 256 | assert( params.size() == 1 ); |
---|
| 257 | auto param = params.front(); |
---|
| 258 | auto enumToInt = new ast::CastExpr( |
---|
| 259 | func->location, |
---|
| 260 | new ast::VariableExpr(func->location, param), |
---|
| 261 | new ast::BasicType(ast::BasicKind::UnsignedInt), |
---|
| 262 | ast::GeneratedFlag::ExplicitCast |
---|
| 263 | ); |
---|
| 264 | ast::UntypedExpr* addOneExpr = ast::UntypedExpr::createCall( func->location, |
---|
| 265 | opt, |
---|
| 266 | {enumToInt, |
---|
| 267 | ast::ConstantExpr::from_int(func->location, 1)} |
---|
| 268 | ); |
---|
| 269 | auto intToEnum = new ast::CastExpr( |
---|
| 270 | func->location, |
---|
| 271 | addOneExpr, |
---|
| 272 | new ast::EnumInstType( decl ), |
---|
| 273 | ast::GeneratedFlag::ExplicitCast |
---|
| 274 | ); |
---|
| 275 | func->stmts = new ast::CompoundStmt( |
---|
| 276 | func->location, { |
---|
| 277 | new ast::ReturnStmt( |
---|
| 278 | func->location, |
---|
| 279 | intToEnum |
---|
| 280 | ) |
---|
| 281 | } |
---|
| 282 | ); |
---|
[eb7586e] | 283 | } |
---|
| 284 | |
---|
[af746cc] | 285 | |
---|
[c333ed2] | 286 | void EnumAttrFuncGenerator::genSerialTraitFuncs() { |
---|
| 287 | ast::FunctionDecl * protos[4] = { |
---|
| 288 | genFromIntProto(), |
---|
| 289 | genFromInstanceProto(), |
---|
| 290 | genInstToInstFuncProto("succ"), |
---|
| 291 | genInstToInstFuncProto("pred") |
---|
| 292 | }; |
---|
| 293 | for (auto& proto: protos) produceForwardDecl(proto); |
---|
| 294 | genFromIntBody(protos[0]); |
---|
| 295 | genFromInstanceBody(protos[1]); |
---|
| 296 | genSuccPredBody(protos[2], "?+?"); |
---|
| 297 | genSuccPredBody(protos[3], "?-?"); |
---|
[6d9aa79] | 298 | for (auto& proto: protos) produceDecl(proto); |
---|
[c333ed2] | 299 | } |
---|
| 300 | |
---|
| 301 | ast::FunctionDecl* EnumAttrFuncGenerator::genInstToInstFuncProto(const char * func) const { |
---|
[fc1a3e2] | 302 | return genProto( |
---|
[c333ed2] | 303 | func, |
---|
[fc1a3e2] | 304 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))}, |
---|
| 305 | {new ast::ObjectDecl(getLocation(), "_ret", |
---|
| 306 | new ast::EnumInstType(decl))}); |
---|
[af746cc] | 307 | } |
---|
| 308 | |
---|
[c333ed2] | 309 | ast::FunctionDecl* EnumAttrFuncGenerator::genBoundedProto(const char * func) const { |
---|
| 310 | return genProto(func, {}, { |
---|
[eb7586e] | 311 | new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl)) |
---|
| 312 | }); |
---|
| 313 | } |
---|
| 314 | |
---|
[c333ed2] | 315 | void EnumAttrFuncGenerator::genBoundedBody(ast::FunctionDecl* func) const { |
---|
[eb7586e] | 316 | const CodeLocation & loc = func->location; |
---|
[c333ed2] | 317 | auto mem = func->name=="lowerBound"? decl->members.front() : decl->members.back(); |
---|
[eb7586e] | 318 | auto expr = new ast::NameExpr( loc, mem->name ); |
---|
| 319 | func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)}); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | void EnumAttrFuncGenerator::genBoundedFunctions() { |
---|
[c333ed2] | 323 | ast::FunctionDecl * boundedProtos[2] = {genBoundedProto("upperBound"), genBoundedProto("lowerBound")}; |
---|
| 324 | for (auto & protos: boundedProtos) { |
---|
| 325 | produceForwardDecl(protos); |
---|
| 326 | genBoundedBody(protos); |
---|
| 327 | produceDecl(protos); |
---|
| 328 | } |
---|
[eb7586e] | 329 | } |
---|
| 330 | |
---|
[af746cc] | 331 | inline ast::EnumAttrType * getPosnType( const ast::EnumDecl * decl ) { |
---|
[fc1a3e2] | 332 | return new ast::EnumAttrType(new ast::EnumInstType(decl), ast::EnumAttribute::Posn); |
---|
[af746cc] | 333 | } |
---|
| 334 | |
---|
| 335 | ast::ObjectDecl* EnumAttrFuncGenerator::genAttrArrayProto( |
---|
[fc1a3e2] | 336 | const ast::EnumAttribute attr, const CodeLocation& location, |
---|
| 337 | std::vector<ast::ptr<ast::Init>>& inits) const { |
---|
| 338 | ast::ArrayType* arrT = new ast::ArrayType( |
---|
| 339 | attr == ast::EnumAttribute::Value |
---|
| 340 | ? decl->base |
---|
| 341 | : new ast::PointerType(new ast::BasicType{ast::BasicKind::Char}), |
---|
| 342 | ast::ConstantExpr::from_int(decl->location, decl->members.size()), |
---|
| 343 | ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim); |
---|
| 344 | |
---|
| 345 | ast::ObjectDecl* objDecl = |
---|
| 346 | new ast::ObjectDecl( |
---|
| 347 | decl->location, decl->getUnmangeldArrayName( attr ), |
---|
| 348 | arrT, new ast::ListInit( location, std::move( inits ) ), |
---|
| 349 | ast::Storage::Static, ast::Linkage::AutoGen ); |
---|
| 350 | |
---|
| 351 | return objDecl; |
---|
[af746cc] | 352 | } |
---|
| 353 | |
---|
| 354 | void EnumAttrFuncGenerator::genValueOrLabelBody( |
---|
[fc1a3e2] | 355 | ast::FunctionDecl* func, ast::ObjectDecl* arrDecl) const { |
---|
| 356 | ast::UntypedExpr* untyped = ast::UntypedExpr::createCall( |
---|
| 357 | func->location, "?[?]", |
---|
| 358 | {new ast::NameExpr(func->location, arrDecl->name), |
---|
[eb7586e] | 359 | new ast::CastExpr( |
---|
| 360 | func->location, |
---|
| 361 | new ast::VariableExpr( func->location, func->params.front() ), |
---|
| 362 | new ast::EnumAttrType( new ast::EnumInstType(decl), |
---|
| 363 | ast::EnumAttribute::Posn))}); |
---|
[fc1a3e2] | 364 | func->stmts = new ast::CompoundStmt( |
---|
| 365 | func->location, {new ast::ReturnStmt(func->location, untyped)}); |
---|
[af746cc] | 366 | } |
---|
| 367 | |
---|
| 368 | void EnumAttrFuncGenerator::genPosnBody(ast::FunctionDecl* func) const { |
---|
[fc1a3e2] | 369 | auto castExpr = new ast::CastExpr( |
---|
| 370 | func->location, |
---|
| 371 | new ast::VariableExpr(func->location, func->params.front()), |
---|
| 372 | new ast::EnumAttrType(new ast::EnumInstType(decl), |
---|
| 373 | ast::EnumAttribute::Posn)); |
---|
| 374 | func->stmts = new ast::CompoundStmt( |
---|
| 375 | func->location, {new ast::ReturnStmt(func->location, castExpr)}); |
---|
[af746cc] | 376 | } |
---|
| 377 | |
---|
[c333ed2] | 378 | void EnumAttrFuncGenerator::genTypedEnumFunction(const ast::EnumAttribute attr) { |
---|
[fc1a3e2] | 379 | if (attr == ast::EnumAttribute::Value || |
---|
| 380 | attr == ast::EnumAttribute::Label) { |
---|
[c333ed2] | 381 | // TypedEnum's backing arrays |
---|
[fc1a3e2] | 382 | std::vector<ast::ptr<ast::Init>> inits = |
---|
| 383 | attr == ast::EnumAttribute::Value ? genValueInit() : genLabelInit(); |
---|
| 384 | ast::ObjectDecl* arrayProto = |
---|
| 385 | genAttrArrayProto(attr, getLocation(), inits); |
---|
| 386 | forwards.push_back(arrayProto); |
---|
| 387 | |
---|
| 388 | ast::FunctionDecl* funcProto = ( attr == ast::EnumAttribute::Value ) |
---|
| 389 | ? genValueProto() |
---|
| 390 | : genLabelProto(); |
---|
| 391 | produceForwardDecl(funcProto); |
---|
| 392 | genValueOrLabelBody(funcProto, arrayProto); |
---|
| 393 | produceDecl(funcProto); |
---|
| 394 | } else { |
---|
| 395 | ast::FunctionDecl* funcProto = genPosnProto(); |
---|
| 396 | produceForwardDecl(funcProto); |
---|
| 397 | genPosnBody(funcProto); |
---|
| 398 | produceDecl(funcProto); |
---|
| 399 | } |
---|
[af746cc] | 400 | } |
---|
| 401 | |
---|
[c333ed2] | 402 | void EnumAttrFuncGenerator::genTypedEnumFuncs() { |
---|
| 403 | if (decl->base) genTypedEnumFunction(ast::EnumAttribute::Value); |
---|
| 404 | genTypedEnumFunction(ast::EnumAttribute::Label); |
---|
| 405 | genTypedEnumFunction(ast::EnumAttribute::Posn); |
---|
[af746cc] | 406 | } |
---|
| 407 | |
---|
| 408 | void EnumAttrFuncGenerator::generateAndAppendFunctions( |
---|
[fc1a3e2] | 409 | std::list<ast::ptr<ast::Decl>>& decls) { |
---|
| 410 | // Generate the functions (they go into forwards and definitions). |
---|
[c333ed2] | 411 | genTypedEnumFuncs(); |
---|
[eb7586e] | 412 | genSerialTraitFuncs(); |
---|
| 413 | genBoundedFunctions(); |
---|
[fc1a3e2] | 414 | // Now export the lists contents. |
---|
| 415 | decls.splice(decls.end(), forwards); |
---|
| 416 | decls.splice(decls.end(), definitions); |
---|
[af746cc] | 417 | } |
---|
| 418 | |
---|
| 419 | // --------------------------------------------------------- |
---|
| 420 | |
---|
[fc1a3e2] | 421 | struct ImplementEnumFunc final : |
---|
| 422 | public ast::WithDeclsToAdd<>, public ast::WithShortCircuiting { |
---|
| 423 | void previsit(const ast::EnumDecl* enumDecl); |
---|
| 424 | void previsit(const ast::FunctionDecl* functionDecl); |
---|
| 425 | void postvisit(const ast::FunctionDecl* functionDecl); |
---|
[af746cc] | 426 | |
---|
[fc1a3e2] | 427 | private: |
---|
| 428 | // Current level of nested functions. |
---|
| 429 | unsigned int functionNesting = 0; |
---|
[af746cc] | 430 | }; |
---|
| 431 | |
---|
| 432 | void ImplementEnumFunc::previsit(const ast::EnumDecl* enumDecl) { |
---|
[c333ed2] | 433 | if (!enumDecl->body || !enumDecl->isTyped) return; |
---|
[fc1a3e2] | 434 | ast::EnumInstType enumInst(enumDecl->name); |
---|
| 435 | enumInst.base = enumDecl; |
---|
| 436 | EnumAttrFuncGenerator gen(enumDecl, &enumInst, functionNesting); |
---|
| 437 | gen.generateAndAppendFunctions(declsToAddAfter); |
---|
[af746cc] | 438 | } |
---|
| 439 | |
---|
| 440 | void ImplementEnumFunc::previsit(const ast::FunctionDecl*) { |
---|
[fc1a3e2] | 441 | functionNesting += 1; |
---|
[af746cc] | 442 | } |
---|
| 443 | |
---|
| 444 | void ImplementEnumFunc::postvisit(const ast::FunctionDecl*) { |
---|
[fc1a3e2] | 445 | functionNesting -= 1; |
---|
[af746cc] | 446 | } |
---|
| 447 | |
---|
[fc1a3e2] | 448 | } // namespace |
---|
[af746cc] | 449 | |
---|
| 450 | void implementEnumFunc(ast::TranslationUnit& translationUnit) { |
---|
[fc1a3e2] | 451 | ast::Pass<ImplementEnumFunc>::run(translationUnit); |
---|
[af746cc] | 452 | } |
---|
[fc1a3e2] | 453 | |
---|
| 454 | } // namespace Validate |
---|