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