[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;
|
---|
[85855b0] | 12 | const ast::StructDecl* quasi_void_decl;
|
---|
[fc1a3e2] | 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,
|
---|
[822332e] | 23 | const ast::EnumInstType*,
|
---|
[fc1a3e2] | 24 | unsigned int functionNesting )
|
---|
| 25 | : decl(decl),
|
---|
| 26 | functionNesting{functionNesting},
|
---|
| 27 | proto_linkage{ast::Linkage::Cforall} {}
|
---|
| 28 |
|
---|
| 29 | private:
|
---|
| 30 | const CodeLocation& getLocation() const { return decl->location; }
|
---|
| 31 |
|
---|
| 32 | ast::FunctionDecl* genProto(
|
---|
| 33 | std::string&& name, std::vector<ast::ptr<ast::DeclWithType>>&& params,
|
---|
| 34 | std::vector<ast::ptr<ast::DeclWithType>>&& returns) const;
|
---|
| 35 |
|
---|
| 36 | void produceDecl(const ast::FunctionDecl* decl);
|
---|
| 37 | void produceForwardDecl(const ast::FunctionDecl* decl);
|
---|
| 38 |
|
---|
| 39 | const ast::Decl* getDecl() const { return decl; }
|
---|
| 40 |
|
---|
[c333ed2] | 41 | // Implement Bounded trait
|
---|
| 42 | void genBoundedFunctions();
|
---|
| 43 | ast::FunctionDecl* genBoundedProto(const char *) const;
|
---|
| 44 | void genBoundedBody(ast::FunctionDecl* func) const;
|
---|
[eb7586e] | 45 |
|
---|
[c333ed2] | 46 | // Implement Serial trait
|
---|
| 47 | void genSerialTraitFuncs();
|
---|
| 48 | ast::FunctionDecl* genFromIntProto() const;
|
---|
| 49 | ast::FunctionDecl* genFromInstanceProto() const;
|
---|
| 50 | ast::FunctionDecl* genInstToInstFuncProto(const char* func) const;
|
---|
[822332e] | 51 | void genFromIntBody(ast::FunctionDecl *) const;
|
---|
[c333ed2] | 52 | void genFromInstanceBody(ast::FunctionDecl *) const;
|
---|
| 53 | void genSuccPredBody(ast::FunctionDecl *, const char *) const;
|
---|
[eb7586e] | 54 |
|
---|
[85855b0] | 55 | void genTypeNameFunc();
|
---|
| 56 |
|
---|
[c333ed2] | 57 | // Implement TypedEnum trait
|
---|
| 58 | void genTypedEnumFuncs();
|
---|
| 59 | void genTypedEnumFunction(const ast::EnumAttribute attr);
|
---|
[fc1a3e2] | 60 | ast::FunctionDecl* genPosnProto() const;
|
---|
| 61 | ast::FunctionDecl* genLabelProto() const;
|
---|
| 62 | ast::FunctionDecl* genValueProto() const;
|
---|
[85855b0] | 63 | ast::FunctionDecl* genQuasiValueProto() const;
|
---|
| 64 | ast::FunctionDecl* genTypeNameProto() const;
|
---|
| 65 |
|
---|
[c333ed2] | 66 | void genValueOrLabelBody(
|
---|
| 67 | ast::FunctionDecl* func, ast::ObjectDecl* arrDecl) const;
|
---|
| 68 | void genPosnBody(ast::FunctionDecl* func) const;
|
---|
[85855b0] | 69 | void genQuasiValueBody(ast::FunctionDecl* func) const;
|
---|
| 70 | void genTypeNameBody(ast::FunctionDecl* func) const;
|
---|
[fc1a3e2] | 71 |
|
---|
| 72 | // ----------------------------------------------------
|
---|
| 73 |
|
---|
| 74 | const ast::Init* getAutoInit(const ast::Init* prev) const;
|
---|
| 75 |
|
---|
| 76 | std::vector<ast::ptr<ast::Init>> genLabelInit() const;
|
---|
| 77 |
|
---|
| 78 | std::vector<ast::ptr<ast::Init>> genValueInit() const;
|
---|
| 79 | ast::ObjectDecl* genAttrArrayProto(
|
---|
[90be0cf] | 80 | const CodeLocation& location, const std::string& prefix,
|
---|
| 81 | const ast::Type * type, std::vector<ast::ptr<ast::Init>>& inits ) const;
|
---|
[af746cc] | 82 | };
|
---|
| 83 |
|
---|
| 84 | std::vector<ast::ptr<ast::Init>> EnumAttrFuncGenerator::genLabelInit() const {
|
---|
[fc1a3e2] | 85 | std::vector<ast::ptr<ast::Init>> inits;
|
---|
| 86 | for (size_t i = 0; i < decl->members.size(); i++) {
|
---|
| 87 | ast::ptr<ast::Decl> mem = decl->members.at(i);
|
---|
| 88 | auto memAsObjectDecl = mem.as<ast::ObjectDecl>();
|
---|
| 89 | assert(memAsObjectDecl);
|
---|
| 90 | inits.emplace_back(new ast::SingleInit(
|
---|
| 91 | mem->location,
|
---|
| 92 | ast::ConstantExpr::from_string(mem->location, mem->name)));
|
---|
| 93 | }
|
---|
| 94 | return inits;
|
---|
[af746cc] | 95 | }
|
---|
| 96 |
|
---|
| 97 | std::vector<ast::ptr<ast::Init>> EnumAttrFuncGenerator::genValueInit() const {
|
---|
[fc1a3e2] | 98 | std::vector<ast::ptr<ast::Init>> inits;
|
---|
| 99 | for (size_t i = 0; i < decl->members.size(); i++) {
|
---|
| 100 | ast::ptr<ast::Decl> mem = decl->members.at(i);
|
---|
| 101 | auto memAsObjectDecl = mem.as<ast::ObjectDecl>();
|
---|
| 102 | assert(memAsObjectDecl);
|
---|
[bb336a6] | 103 | if (auto& init = memAsObjectDecl->init) {
|
---|
[9d5eacb] | 104 | if ( auto singleInit = init.as<ast::SingleInit>() ) {
|
---|
| 105 | if ( auto nameExpr = singleInit->value.as<ast::NameExpr>() ) {
|
---|
| 106 | auto name = nameExpr->name;
|
---|
| 107 | if (auto it = std::find_if(decl->members.begin(), decl->members.end(),
|
---|
| 108 | [name](ast::ptr<ast::Decl> mem_decl) {
|
---|
| 109 | return (mem_decl->name == name);
|
---|
| 110 | }); it != std::end(decl->members)
|
---|
| 111 | ) {
|
---|
| 112 | auto index = std::distance( decl->members.begin(), it );
|
---|
| 113 | auto targetInit = inits.at(index).strict_as<ast::SingleInit>();
|
---|
| 114 | auto targetExpr = targetInit->value;
|
---|
| 115 | inits.push_back( new ast::SingleInit( targetExpr->location, targetExpr ) );
|
---|
| 116 | continue;
|
---|
| 117 | }
|
---|
[bb336a6] | 118 | }
|
---|
| 119 | }
|
---|
[fc1a3e2] | 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 | }
|
---|
[85855b0] | 133 |
|
---|
[af746cc] | 134 | const ast::Init* EnumAttrFuncGenerator::getAutoInit(
|
---|
[fc1a3e2] | 135 | const ast::Init* prev) const {
|
---|
| 136 | if (prev == nullptr) {
|
---|
| 137 | return new ast::SingleInit(
|
---|
| 138 | getLocation(), ast::ConstantExpr::from_int(getLocation(), 0));
|
---|
| 139 | }
|
---|
| 140 | auto prevInit = dynamic_cast<const ast::SingleInit*>(prev);
|
---|
| 141 | assert(prevInit);
|
---|
| 142 | auto prevInitExpr = prevInit->value;
|
---|
| 143 | if (auto constInit = prevInitExpr.as<ast::ConstantExpr>()) {
|
---|
| 144 | // Assume no string literal for now
|
---|
| 145 | return new ast::SingleInit(
|
---|
| 146 | getLocation(), ast::ConstantExpr::from_int(
|
---|
| 147 | getLocation(), constInit->intValue() + 1));
|
---|
| 148 | } else {
|
---|
| 149 | auto untypedThisInit = new ast::UntypedExpr(
|
---|
[830edc6] | 150 | getLocation(), new ast::NameExpr(getLocation(), "?+?"),
|
---|
| 151 | { prevInitExpr,
|
---|
| 152 | new ast::ConstantExpr( getLocation(), new ast::OneType, "1", 1) });
|
---|
[fc1a3e2] | 153 | return new ast::SingleInit(getLocation(), untypedThisInit);
|
---|
| 154 | }
|
---|
[af746cc] | 155 | }
|
---|
| 156 |
|
---|
| 157 | ast::FunctionDecl* EnumAttrFuncGenerator::genProto(
|
---|
[fc1a3e2] | 158 | std::string&& name, std::vector<ast::ptr<ast::DeclWithType>>&& params,
|
---|
| 159 | std::vector<ast::ptr<ast::DeclWithType>>&& returns) const {
|
---|
| 160 | ast::FunctionDecl* decl = new ast::FunctionDecl(
|
---|
| 161 | // Auto-generated routines use the type declaration's location.
|
---|
| 162 | getLocation(), std::move(name), {}, {}, std::move(params),
|
---|
| 163 | std::move(returns),
|
---|
| 164 | // Only a prototype, no body.
|
---|
| 165 | nullptr,
|
---|
| 166 | // Use static storage if we are at the top level.
|
---|
| 167 | (0 < functionNesting) ? ast::Storage::Classes() : ast::Storage::Static,
|
---|
| 168 | proto_linkage, std::vector<ast::ptr<ast::Attribute>>(),
|
---|
| 169 | // Auto-generated routines are inline to avoid conflicts.
|
---|
| 170 | ast::Function::Specs(ast::Function::Inline));
|
---|
| 171 | decl->fixUniqueId();
|
---|
| 172 | return decl;
|
---|
[af746cc] | 173 | }
|
---|
| 174 |
|
---|
| 175 | void EnumAttrFuncGenerator::produceDecl(const ast::FunctionDecl* decl) {
|
---|
[fc1a3e2] | 176 | assert(nullptr != decl->stmts);
|
---|
[af746cc] | 177 |
|
---|
[fc1a3e2] | 178 | definitions.push_back(decl);
|
---|
[af746cc] | 179 | }
|
---|
| 180 |
|
---|
| 181 | void EnumAttrFuncGenerator::produceForwardDecl(const ast::FunctionDecl* decl) {
|
---|
[fc1a3e2] | 182 | if (0 != functionNesting) return;
|
---|
| 183 | ast::FunctionDecl* fwd =
|
---|
| 184 | (decl->stmts) ? ast::asForward(decl) : ast::deepCopy(decl);
|
---|
| 185 | fwd->fixUniqueId();
|
---|
| 186 | forwards.push_back(fwd);
|
---|
[af746cc] | 187 | }
|
---|
| 188 |
|
---|
| 189 | ast::FunctionDecl* EnumAttrFuncGenerator::genPosnProto() const {
|
---|
| 190 | return genProto(
|
---|
[5eb3f65] | 191 | "posn",
|
---|
[af746cc] | 192 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
|
---|
| 193 | {new ast::ObjectDecl(getLocation(), "_ret",
|
---|
[5f210c0] | 194 | new ast::BasicType(ast::BasicKind::SignedInt))});
|
---|
[af746cc] | 195 | }
|
---|
| 196 |
|
---|
| 197 | ast::FunctionDecl* EnumAttrFuncGenerator::genLabelProto() const {
|
---|
[fc1a3e2] | 198 | return genProto(
|
---|
[5eb3f65] | 199 | "label",
|
---|
[fc1a3e2] | 200 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
|
---|
| 201 | {new ast::ObjectDecl(
|
---|
| 202 | getLocation(), "_ret",
|
---|
[c2cf2d0] | 203 | new ast::PointerType(
|
---|
| 204 | new ast::BasicType(ast::BasicKind::Char, ast::CV::Const)))});
|
---|
[af746cc] | 205 | }
|
---|
| 206 |
|
---|
| 207 | ast::FunctionDecl* EnumAttrFuncGenerator::genValueProto() const {
|
---|
[236f133] | 208 | assert (decl->isTyped());
|
---|
[fc1a3e2] | 209 | return genProto(
|
---|
[5eb3f65] | 210 | "value",
|
---|
[fc1a3e2] | 211 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
|
---|
[63d5b9c8] | 212 | {new ast::ObjectDecl(getLocation(), "_ret", decl->base)});
|
---|
[af746cc] | 213 | }
|
---|
| 214 |
|
---|
[236f133] | 215 | // ast::FunctionDecl* EnumAttrFuncGenerator::genQuasiValueProto() const {
|
---|
| 216 | // return genProto(
|
---|
| 217 | // "value",
|
---|
| 218 | // {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
|
---|
| 219 | // {new ast::ObjectDecl(getLocation(), "_ret",
|
---|
| 220 | // new ast::StructInstType(quasi_void_decl))});
|
---|
| 221 | // }
|
---|
| 222 |
|
---|
[eb7586e] | 223 | ast::FunctionDecl* EnumAttrFuncGenerator::genFromIntProto() const {
|
---|
| 224 | return genProto(
|
---|
[0c327ce] | 225 | "fromInt_unsafe",
|
---|
[5f210c0] | 226 | {new ast::ObjectDecl(getLocation(), "_i", new ast::BasicType(ast::BasicKind::SignedInt))},
|
---|
[eb7586e] | 227 | {new ast::ObjectDecl(getLocation(), "_ret", new ast::EnumInstType(decl))}
|
---|
| 228 | );
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | ast::FunctionDecl* EnumAttrFuncGenerator::genFromInstanceProto() const {
|
---|
| 232 | return genProto(
|
---|
| 233 | "fromInstance",
|
---|
| 234 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
|
---|
[5f210c0] | 235 | {new ast::ObjectDecl(getLocation(), "_ret", new ast::BasicType(ast::BasicKind::SignedInt))}
|
---|
[eb7586e] | 236 | );
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[85855b0] | 239 | ast::FunctionDecl* EnumAttrFuncGenerator::genTypeNameProto() const {
|
---|
| 240 | return genProto(
|
---|
| 241 | "type_name",
|
---|
| 242 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
|
---|
| 243 | {new ast::ObjectDecl(
|
---|
| 244 | getLocation(), "_ret",
|
---|
[c2cf2d0] | 245 | new ast::PointerType(
|
---|
| 246 | new ast::BasicType(ast::BasicKind::Char, ast::CV::Const)))});
|
---|
[85855b0] | 247 | }
|
---|
| 248 |
|
---|
[eb7586e] | 249 | void EnumAttrFuncGenerator::genFromIntBody(ast::FunctionDecl* func) const {
|
---|
| 250 | auto params = func->params;
|
---|
| 251 | assert( params.size() == 1 );
|
---|
| 252 | auto param = params.front();
|
---|
| 253 | auto castExpr = new ast::CastExpr(
|
---|
| 254 | func->location,
|
---|
| 255 | new ast::VariableExpr(func->location, param),
|
---|
| 256 | new ast::EnumInstType(decl),
|
---|
| 257 | ast::GeneratedFlag::ExplicitCast
|
---|
| 258 | );
|
---|
| 259 | func->stmts = new ast::CompoundStmt(
|
---|
| 260 | func->location, {new ast::ReturnStmt(func->location, castExpr)}
|
---|
| 261 | );
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | void EnumAttrFuncGenerator::genFromInstanceBody(ast::FunctionDecl* func) const {
|
---|
| 265 | auto params = func->params;
|
---|
| 266 | assert( params.size() == 1 );
|
---|
| 267 | auto param = params.front();
|
---|
| 268 | ast::UntypedExpr* untyped = ast::UntypedExpr::createCall(
|
---|
[5eb3f65] | 269 | func->location, "posn", { new ast::VariableExpr(func->location, param) });
|
---|
[eb7586e] | 270 | func->stmts = new ast::CompoundStmt(
|
---|
| 271 | func->location, {new ast::ReturnStmt(func->location, untyped)}
|
---|
| 272 | );
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[c333ed2] | 275 | void EnumAttrFuncGenerator::genSuccPredBody(ast::FunctionDecl * func, const char* opt) const {
|
---|
| 276 | auto params = func->params;
|
---|
| 277 | assert( params.size() == 1 );
|
---|
| 278 | auto param = params.front();
|
---|
| 279 | auto enumToInt = new ast::CastExpr(
|
---|
| 280 | func->location,
|
---|
| 281 | new ast::VariableExpr(func->location, param),
|
---|
[5f210c0] | 282 | new ast::BasicType(ast::BasicKind::SignedInt),
|
---|
[c333ed2] | 283 | ast::GeneratedFlag::ExplicitCast
|
---|
| 284 | );
|
---|
| 285 | ast::UntypedExpr* addOneExpr = ast::UntypedExpr::createCall( func->location,
|
---|
| 286 | opt,
|
---|
| 287 | {enumToInt,
|
---|
| 288 | ast::ConstantExpr::from_int(func->location, 1)}
|
---|
| 289 | );
|
---|
| 290 | auto intToEnum = new ast::CastExpr(
|
---|
| 291 | func->location,
|
---|
| 292 | addOneExpr,
|
---|
| 293 | new ast::EnumInstType( decl ),
|
---|
| 294 | ast::GeneratedFlag::ExplicitCast
|
---|
| 295 | );
|
---|
| 296 | func->stmts = new ast::CompoundStmt(
|
---|
| 297 | func->location, {
|
---|
| 298 | new ast::ReturnStmt(
|
---|
| 299 | func->location,
|
---|
| 300 | intToEnum
|
---|
| 301 | )
|
---|
| 302 | }
|
---|
| 303 | );
|
---|
[eb7586e] | 304 | }
|
---|
| 305 |
|
---|
[c333ed2] | 306 | void EnumAttrFuncGenerator::genSerialTraitFuncs() {
|
---|
| 307 | ast::FunctionDecl * protos[4] = {
|
---|
| 308 | genFromIntProto(),
|
---|
| 309 | genFromInstanceProto(),
|
---|
[0c327ce] | 310 | genInstToInstFuncProto("succ_unsafe"),
|
---|
| 311 | genInstToInstFuncProto("pred_unsafe")
|
---|
[c333ed2] | 312 | };
|
---|
| 313 | for (auto& proto: protos) produceForwardDecl(proto);
|
---|
| 314 | genFromIntBody(protos[0]);
|
---|
| 315 | genFromInstanceBody(protos[1]);
|
---|
| 316 | genSuccPredBody(protos[2], "?+?");
|
---|
| 317 | genSuccPredBody(protos[3], "?-?");
|
---|
[6d9aa79] | 318 | for (auto& proto: protos) produceDecl(proto);
|
---|
[c333ed2] | 319 | }
|
---|
| 320 |
|
---|
| 321 | ast::FunctionDecl* EnumAttrFuncGenerator::genInstToInstFuncProto(const char * func) const {
|
---|
[fc1a3e2] | 322 | return genProto(
|
---|
[c333ed2] | 323 | func,
|
---|
[fc1a3e2] | 324 | {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
|
---|
| 325 | {new ast::ObjectDecl(getLocation(), "_ret",
|
---|
| 326 | new ast::EnumInstType(decl))});
|
---|
[af746cc] | 327 | }
|
---|
| 328 |
|
---|
[c333ed2] | 329 | ast::FunctionDecl* EnumAttrFuncGenerator::genBoundedProto(const char * func) const {
|
---|
| 330 | return genProto(func, {}, {
|
---|
[eb7586e] | 331 | new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))
|
---|
| 332 | });
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[c333ed2] | 335 | void EnumAttrFuncGenerator::genBoundedBody(ast::FunctionDecl* func) const {
|
---|
[eb7586e] | 336 | const CodeLocation & loc = func->location;
|
---|
[c333ed2] | 337 | auto mem = func->name=="lowerBound"? decl->members.front() : decl->members.back();
|
---|
[85855b0] | 338 | // auto expr = new ast::NameExpr( loc, mem->name );
|
---|
| 339 | auto expr = new ast::QualifiedNameExpr( loc, decl->name, mem->name );
|
---|
[eb7586e] | 340 | func->stmts = new ast::CompoundStmt( loc, {new ast::ReturnStmt(loc, expr)});
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | void EnumAttrFuncGenerator::genBoundedFunctions() {
|
---|
[c333ed2] | 344 | ast::FunctionDecl * boundedProtos[2] = {genBoundedProto("upperBound"), genBoundedProto("lowerBound")};
|
---|
| 345 | for (auto & protos: boundedProtos) {
|
---|
| 346 | produceForwardDecl(protos);
|
---|
| 347 | genBoundedBody(protos);
|
---|
| 348 | produceDecl(protos);
|
---|
| 349 | }
|
---|
[eb7586e] | 350 | }
|
---|
| 351 |
|
---|
[af746cc] | 352 | ast::ObjectDecl* EnumAttrFuncGenerator::genAttrArrayProto(
|
---|
[90be0cf] | 353 | const CodeLocation& location, const std::string& prefix,
|
---|
| 354 | const ast::Type * type, std::vector<ast::ptr<ast::Init>>& inits ) const {
|
---|
[fc1a3e2] | 355 | ast::ArrayType* arrT = new ast::ArrayType(
|
---|
[90be0cf] | 356 | type,
|
---|
[fc1a3e2] | 357 | ast::ConstantExpr::from_int(decl->location, decl->members.size()),
|
---|
| 358 | ast::LengthFlag::FixedLen, ast::DimensionFlag::DynamicDim);
|
---|
| 359 |
|
---|
| 360 | ast::ObjectDecl* objDecl =
|
---|
| 361 | new ast::ObjectDecl(
|
---|
[90be0cf] | 362 | decl->location, prefix + decl->name,
|
---|
[fc1a3e2] | 363 | arrT, new ast::ListInit( location, std::move( inits ) ),
|
---|
| 364 | ast::Storage::Static, ast::Linkage::AutoGen );
|
---|
| 365 |
|
---|
| 366 | return objDecl;
|
---|
[af746cc] | 367 | }
|
---|
| 368 |
|
---|
| 369 | void EnumAttrFuncGenerator::genValueOrLabelBody(
|
---|
[fc1a3e2] | 370 | ast::FunctionDecl* func, ast::ObjectDecl* arrDecl) const {
|
---|
| 371 | ast::UntypedExpr* untyped = ast::UntypedExpr::createCall(
|
---|
| 372 | func->location, "?[?]",
|
---|
| 373 | {new ast::NameExpr(func->location, arrDecl->name),
|
---|
[eb7586e] | 374 | new ast::CastExpr(
|
---|
| 375 | func->location,
|
---|
| 376 | new ast::VariableExpr( func->location, func->params.front() ),
|
---|
[5f210c0] | 377 | new ast::BasicType( ast::BasicKind::SignedInt ),
|
---|
[acb33f15] | 378 | ast::GeneratedFlag::ExplicitCast
|
---|
| 379 | )});
|
---|
[fc1a3e2] | 380 | func->stmts = new ast::CompoundStmt(
|
---|
| 381 | func->location, {new ast::ReturnStmt(func->location, untyped)});
|
---|
[af746cc] | 382 | }
|
---|
| 383 |
|
---|
[236f133] | 384 | // void EnumAttrFuncGenerator::genQuasiValueBody(ast::FunctionDecl* func) const {
|
---|
| 385 | // auto location = func->location;
|
---|
| 386 | // const ast::ObjectDecl * objDecl = new ast::ObjectDecl(
|
---|
| 387 | // location, "_out", new ast::StructInstType( quasi_void_decl ));
|
---|
| 388 | // const ast::DeclStmt * declStmt = new ast::DeclStmt(location, objDecl);
|
---|
| 389 | // const ast::VariableExpr * varExpr = new ast::VariableExpr(location, objDecl);
|
---|
| 390 | // const ast::ReturnStmt * retStmt = new ast::ReturnStmt(location, varExpr);
|
---|
[85855b0] | 391 |
|
---|
[236f133] | 392 | // func->stmts = new ast::CompoundStmt(
|
---|
| 393 | // location, {declStmt, retStmt}
|
---|
| 394 | // );
|
---|
| 395 | // }
|
---|
[85855b0] | 396 |
|
---|
[af746cc] | 397 | void EnumAttrFuncGenerator::genPosnBody(ast::FunctionDecl* func) const {
|
---|
[fc1a3e2] | 398 | auto castExpr = new ast::CastExpr(
|
---|
| 399 | func->location,
|
---|
| 400 | new ast::VariableExpr(func->location, func->params.front()),
|
---|
[5f210c0] | 401 | new ast::BasicType( ast::BasicKind::SignedInt ),
|
---|
[acb33f15] | 402 | ast::GeneratedFlag::ExplicitCast);
|
---|
[fc1a3e2] | 403 | func->stmts = new ast::CompoundStmt(
|
---|
| 404 | func->location, {new ast::ReturnStmt(func->location, castExpr)});
|
---|
[af746cc] | 405 | }
|
---|
| 406 |
|
---|
[85855b0] | 407 | void EnumAttrFuncGenerator::genTypeNameBody(ast::FunctionDecl* func) const {
|
---|
| 408 | const ast::Expr * type_name = ast::ConstantExpr::from_string(func->location, decl->name);
|
---|
| 409 | func->stmts = new ast::CompoundStmt(
|
---|
| 410 | func->location, {new ast::ReturnStmt(func->location, type_name)}
|
---|
| 411 | );
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[c333ed2] | 414 | void EnumAttrFuncGenerator::genTypedEnumFunction(const ast::EnumAttribute attr) {
|
---|
[85855b0] | 415 | if (attr == ast::EnumAttribute::Value) {
|
---|
[90be0cf] | 416 | if ( !decl->isTyped() ) return;
|
---|
| 417 | std::vector<ast::ptr<ast::Init>> inits = genValueInit();
|
---|
| 418 | ast::ObjectDecl* arrayProto =
|
---|
| 419 | genAttrArrayProto( getLocation(), "values_", decl->base, inits );
|
---|
| 420 | forwards.push_back(arrayProto);
|
---|
| 421 |
|
---|
| 422 | ast::FunctionDecl* funcProto = genValueProto();
|
---|
| 423 | produceForwardDecl(funcProto);
|
---|
| 424 | genValueOrLabelBody(funcProto, arrayProto);
|
---|
| 425 | produceDecl(funcProto);
|
---|
[85855b0] | 426 | } else if (attr == ast::EnumAttribute::Label) {
|
---|
| 427 | std::vector<ast::ptr<ast::Init>> inits = genLabelInit();
|
---|
[90be0cf] | 428 | const ast::Type * type = new ast::PointerType(
|
---|
| 429 | new ast::BasicType( ast::BasicKind::Char, ast::CV::Const ) );
|
---|
[fc1a3e2] | 430 | ast::ObjectDecl* arrayProto =
|
---|
[90be0cf] | 431 | genAttrArrayProto( getLocation(), "labels_", type, inits );
|
---|
[fc1a3e2] | 432 | forwards.push_back(arrayProto);
|
---|
[85855b0] | 433 | ast::FunctionDecl* funcProto = genLabelProto();
|
---|
[fc1a3e2] | 434 | produceForwardDecl(funcProto);
|
---|
| 435 | genValueOrLabelBody(funcProto, arrayProto);
|
---|
| 436 | produceDecl(funcProto);
|
---|
| 437 | } else {
|
---|
[90be0cf] | 438 | assert( attr == ast::EnumAttribute::Posn );
|
---|
[fc1a3e2] | 439 | ast::FunctionDecl* funcProto = genPosnProto();
|
---|
| 440 | produceForwardDecl(funcProto);
|
---|
| 441 | genPosnBody(funcProto);
|
---|
| 442 | produceDecl(funcProto);
|
---|
| 443 | }
|
---|
[af746cc] | 444 | }
|
---|
| 445 |
|
---|
[c333ed2] | 446 | void EnumAttrFuncGenerator::genTypedEnumFuncs() {
|
---|
[725f777f] | 447 | genTypedEnumFunction(ast::EnumAttribute::Value);
|
---|
| 448 | genTypedEnumFunction(ast::EnumAttribute::Label);
|
---|
| 449 | genTypedEnumFunction(ast::EnumAttribute::Posn);
|
---|
[af746cc] | 450 | }
|
---|
| 451 |
|
---|
[85855b0] | 452 | void EnumAttrFuncGenerator::genTypeNameFunc() {
|
---|
| 453 | ast::FunctionDecl* funcProto = genTypeNameProto();
|
---|
| 454 | produceForwardDecl(funcProto);
|
---|
| 455 | genTypeNameBody(funcProto);
|
---|
| 456 | produceDecl(funcProto);
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[af746cc] | 459 | void EnumAttrFuncGenerator::generateAndAppendFunctions(
|
---|
[fc1a3e2] | 460 | std::list<ast::ptr<ast::Decl>>& decls) {
|
---|
| 461 | // Generate the functions (they go into forwards and definitions).
|
---|
[725f777f] | 462 | genTypeNameFunc();
|
---|
[c333ed2] | 463 | genTypedEnumFuncs();
|
---|
[725f777f] | 464 | genSerialTraitFuncs();
|
---|
| 465 | genBoundedFunctions();
|
---|
[fc1a3e2] | 466 | // Now export the lists contents.
|
---|
| 467 | decls.splice(decls.end(), forwards);
|
---|
| 468 | decls.splice(decls.end(), definitions);
|
---|
[af746cc] | 469 | }
|
---|
| 470 |
|
---|
| 471 | // ---------------------------------------------------------
|
---|
| 472 |
|
---|
[fc1a3e2] | 473 | struct ImplementEnumFunc final :
|
---|
| 474 | public ast::WithDeclsToAdd<>, public ast::WithShortCircuiting {
|
---|
| 475 | void previsit(const ast::EnumDecl* enumDecl);
|
---|
| 476 | void previsit(const ast::FunctionDecl* functionDecl);
|
---|
| 477 | void postvisit(const ast::FunctionDecl* functionDecl);
|
---|
[af746cc] | 478 |
|
---|
[fc1a3e2] | 479 | private:
|
---|
| 480 | // Current level of nested functions.
|
---|
| 481 | unsigned int functionNesting = 0;
|
---|
[af746cc] | 482 | };
|
---|
| 483 |
|
---|
| 484 | void ImplementEnumFunc::previsit(const ast::EnumDecl* enumDecl) {
|
---|
[5ccc733] | 485 | if (!enumDecl->body || !enumDecl->isCfa) return;
|
---|
[fc1a3e2] | 486 | ast::EnumInstType enumInst(enumDecl->name);
|
---|
| 487 | enumInst.base = enumDecl;
|
---|
| 488 | EnumAttrFuncGenerator gen(enumDecl, &enumInst, functionNesting);
|
---|
[725f777f] | 489 | gen.generateAndAppendFunctions(declsToAddAfter);
|
---|
[af746cc] | 490 | }
|
---|
| 491 |
|
---|
| 492 | void ImplementEnumFunc::previsit(const ast::FunctionDecl*) {
|
---|
[fc1a3e2] | 493 | functionNesting += 1;
|
---|
[af746cc] | 494 | }
|
---|
| 495 |
|
---|
| 496 | void ImplementEnumFunc::postvisit(const ast::FunctionDecl*) {
|
---|
[fc1a3e2] | 497 | functionNesting -= 1;
|
---|
[af746cc] | 498 | }
|
---|
| 499 |
|
---|
[fc1a3e2] | 500 | } // namespace
|
---|
[af746cc] | 501 |
|
---|
| 502 | void implementEnumFunc(ast::TranslationUnit& translationUnit) {
|
---|
[fc1a3e2] | 503 | ast::Pass<ImplementEnumFunc>::run(translationUnit);
|
---|
[af746cc] | 504 | }
|
---|
[fc1a3e2] | 505 |
|
---|
| 506 | } // namespace Validate
|
---|