Skip to content

Commit 131beb1

Browse files
vitautfacebook-github-bot
authored andcommitted
Add enum:self for direct AST access in templates
Summary: Add `enum:self` for direct AST access in templates. To this end pass the prototype database via `mstch_context` to mstch object ctors. Demonstrate usage by updating `declare_enums.mustache`. Before: ``` return "{{enum:name}}"; ``` After: ``` return "{{enum:self.name}}"; ``` Once we fully migrate templates, we'll be able to drop the `self` part which will give the same ergonomics as before but without the legacy mstch object layer. To simplify the review `self` is only added to a single mstch object type in this diff, the rest will be added in a follow-up diff. Reviewed By: praihan Differential Revision: D71205072 fbshipit-source-id: 6db90dc5442e34bf7382264abe6a5c90f0ec3545
1 parent e3d7de6 commit 131beb1

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

third-party/thrift/src/thrift/compiler/generate/mstch_objects.h

+7
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ struct mstch_context : mstch_factories {
237237

238238
node_metadata_cache metadata_cache;
239239

240+
const whisker::prototype_database* prototypes = nullptr;
241+
240242
/**
241243
* Sets or erases the option with the given `key` depending on the
242244
* `condition`.
@@ -1196,6 +1198,7 @@ class mstch_enum : public mstch_base {
11961198
register_methods(
11971199
this,
11981200
{
1201+
{"enum:self", &mstch_enum::self},
11991202
{"enum:name", &mstch_enum::name},
12001203
{"enum:values", &mstch_enum::values},
12011204
{"enum:structured_annotations?",
@@ -1210,6 +1213,10 @@ class mstch_enum : public mstch_base {
12101213
});
12111214
}
12121215

1216+
whisker::object self() {
1217+
return whisker::object(whisker::native_handle<t_enum>(
1218+
whisker::manage_as_static(*enum_), context_.prototypes->of<t_enum>()));
1219+
}
12131220
mstch::node name() { return enum_->get_name(); }
12141221
mstch::node values();
12151222
mstch::node unused_value() { return enum_->unused(); }

third-party/thrift/src/thrift/compiler/generate/t_mstch_generator.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class t_mstch_generator : public t_whisker_generator {
4040
const std::map<std::string, std::string>& options) override {
4141
t_whisker_generator::process_options(options);
4242
mstch_context_.options = compiler_options();
43+
mstch_context_.prototypes = render_state().prototypes.get();
4344
}
4445

4546
protected:

third-party/thrift/src/thrift/compiler/generate/templates/cpp2/module_types_h/declare_enums.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ template <> struct TEnumTraits<::{{program:qualified_namespace}}::{{enum:cpp_nam
5959
static bool findValue(std::string_view name, type* out) noexcept;
6060

6161
FOLLY_ERASE static std::string_view typeName() noexcept {
62-
return "{{enum:name}}";
62+
return "{{enum:self.name}}";
6363
}
6464

6565
FOLLY_ERASE static constexpr std::string_view moduleName() noexcept {

0 commit comments

Comments
 (0)