Skip to content

Commit

Permalink
mstch::object::register_methods cleanup
Browse files Browse the repository at this point in the history
Summary:
All `register_*` methods (including volatile variants) have been collapsed into one `register_methods`.

Whisker properties don't support volatile. For volatility, `native_function` can be used instead.

Reviewed By: vitaut

Differential Revision: D68127188

fbshipit-source-id: 99cce68ce99768a3be99d4cd676d3adf4e7e2392
  • Loading branch information
praihan authored and facebook-github-bot committed Jan 16, 2025
1 parent daca825 commit f9af23e
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 235 deletions.
10 changes: 6 additions & 4 deletions thrift/compiler/generate/mstch_objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ std::string mstch_base::get_option(const std::string& option) const {
}

void mstch_base::register_has_option(std::string key, std::string option) {
register_cached_method(
std::move(key), [this, option = std::move(option)]() -> mstch::node {
return has_option(option);
});
register_method(
std::move(key),
std::function<mstch::node()>(
[this, option = std::move(option)]() -> mstch::node {
return has_option(option);
}));
}

mstch::node mstch_base::is_struct() {
Expand Down
32 changes: 16 additions & 16 deletions thrift/compiler/generate/mstch_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class mstch_base : public mstch::object {
public:
mstch_base(mstch_context& ctx, mstch_element_position pos)
: context_(ctx), pos_(pos) {
register_cached_methods(
register_methods(
this,
{
{"first?", &mstch_base::first},
Expand Down Expand Up @@ -432,7 +432,7 @@ class mstch_program : public mstch_base {
mstch_program(
const t_program* p, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), program_(p) {
register_cached_methods(
register_methods(
this,
{
{"program:name", &mstch_program::name},
Expand Down Expand Up @@ -510,7 +510,7 @@ class mstch_service : public mstch_base {
containing_service_(containing_service) {
assert(containing_service_ == nullptr || service_->is_interaction());

register_cached_methods(
register_methods(
this,
{
{"service:name", &mstch_service::name},
Expand All @@ -534,7 +534,7 @@ class mstch_service : public mstch_base {
});

if (service_->is_interaction()) {
register_cached_methods(
register_methods(
this,
{
{"interaction:serial?", &mstch_service::is_serial_interaction},
Expand Down Expand Up @@ -630,7 +630,7 @@ class mstch_function : public mstch_base {
mstch_element_position pos,
const t_interface* iface)
: mstch_base(ctx, pos), function_(f), interface_(iface) {
register_cached_methods(
register_methods(
this,
{
{"function:name", &mstch_function::name},
Expand Down Expand Up @@ -809,7 +809,7 @@ class mstch_type : public mstch_base {

mstch_type(const t_type* t, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), type_(t), resolved_type_(t->get_true_type()) {
register_cached_methods(
register_methods(
this,
{
{"type:name", &mstch_type::name},
Expand Down Expand Up @@ -902,7 +902,7 @@ class mstch_typedef : public mstch_base {
mstch_typedef(
const t_typedef* t, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), typedef_(t) {
register_cached_methods(
register_methods(
this,
{
{"typedef:type", &mstch_typedef::type},
Expand Down Expand Up @@ -933,7 +933,7 @@ class mstch_struct : public mstch_base {
mstch_struct(
const t_structured* s, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), struct_(s) {
register_cached_methods(
register_methods(
this,
{
{"struct:name", &mstch_struct::name},
Expand Down Expand Up @@ -1047,7 +1047,7 @@ class mstch_field : public mstch_base {
mstch_element_position pos,
const field_generator_context* field_context)
: mstch_base(ctx, pos), field_(f), field_context_(field_context) {
register_cached_methods(
register_methods(
this,
{
{"field:name", &mstch_field::name},
Expand Down Expand Up @@ -1197,7 +1197,7 @@ class mstch_enum : public mstch_base {

mstch_enum(const t_enum* e, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), enum_(e) {
register_cached_methods(
register_methods(
this,
{
{"enum:name", &mstch_enum::name},
Expand Down Expand Up @@ -1278,7 +1278,7 @@ class mstch_enum_value : public mstch_base {
mstch_enum_value(
const t_enum_value* ev, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), enum_value_(ev) {
register_cached_methods(
register_methods(
this,
{
{"enum_value:name", &mstch_enum_value::name},
Expand Down Expand Up @@ -1308,7 +1308,7 @@ class mstch_const : public mstch_base {
current_const_(current_const),
expected_type_(expected_type),
field_(field) {
register_cached_methods(
register_methods(
this,
{
{"constant:name", &mstch_const::name},
Expand Down Expand Up @@ -1349,7 +1349,7 @@ class mstch_const_value : public mstch_base {
current_const_(current_const),
expected_type_(expected_type),
type_(cv->kind()) {
register_cached_methods(
register_methods(
this,
{
{"value:bool?", &mstch_const_value::is_bool},
Expand Down Expand Up @@ -1453,7 +1453,7 @@ class mstch_const_map_element : public mstch_base {
element_(*e),
current_const_(current_const),
expected_types_(expected_types) {
register_cached_methods(
register_methods(
this,
{
{"element:key", &mstch_const_map_element::element_key},
Expand All @@ -1476,7 +1476,7 @@ class mstch_structured_annotation : public mstch_base {
mstch_structured_annotation(
const t_const* c, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), const_(*c) {
register_cached_methods(
register_methods(
this,
{{"structured_annotation:const",
&mstch_structured_annotation::constant},
Expand Down Expand Up @@ -1508,7 +1508,7 @@ class mstch_deprecated_annotation : public mstch_base {
mstch_deprecated_annotation(
const t_annotation* a, mstch_context& ctx, mstch_element_position pos)
: mstch_base(ctx, pos), key_(a->first), val_(a->second) {
register_cached_methods(
register_methods(
this,
{
{"annotation:key", &mstch_deprecated_annotation::key},
Expand Down
24 changes: 12 additions & 12 deletions thrift/compiler/generate/t_json_experimental_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class json_experimental_program : public mstch_program {
json_experimental_program(
const t_program* p, mstch_context& ctx, mstch_element_position pos)
: mstch_program(p, ctx, pos) {
register_cached_methods(
register_methods(
this,
{
{"program:py_namespace",
Expand All @@ -89,8 +89,8 @@ class json_experimental_program : public mstch_program {

// To allow rendering a brace not surrounded by whitespace, without
// interfering with the `{{` `}}` used by the mustache syntax.
register_cached_method("open_object", [] { return mstch::node("{"); });
register_cached_method("close_object", [] { return mstch::node("}"); });
register_method("open_object", [] { return mstch::node("{"); });
register_method("close_object", [] { return mstch::node("}"); });
}

mstch::node get_py_namespace() { return program_->get_namespace("py"); }
Expand Down Expand Up @@ -168,7 +168,7 @@ class json_experimental_service : public mstch_service {
mstch_element_position pos,
json_codegen_data d)
: mstch_service(s, ctx, pos), data_(d) {
register_cached_methods(
register_methods(
this,
{
{"service:lineno", &json_experimental_service::get_lineno},
Expand Down Expand Up @@ -199,7 +199,7 @@ class json_experimental_function : public mstch_function {
const t_interface* iface,
source_manager* sm)
: mstch_function(f, ctx, pos, iface), source_mgr_(*sm) {
register_cached_methods(
register_methods(
this,
{
{"function:lineno", &json_experimental_function::get_lineno},
Expand All @@ -225,7 +225,7 @@ class json_experimental_struct : public mstch_struct {
mstch_element_position pos,
source_manager* sm)
: mstch_struct(s, ctx, pos), source_mgr_(*sm) {
register_cached_methods(
register_methods(
this,
{
{"struct:lineno", &json_experimental_struct::get_lineno},
Expand All @@ -251,7 +251,7 @@ class json_experimental_type : public mstch_type {
mstch_element_position pos,
json_codegen_data d)
: mstch_type(t, ctx, pos), data_(d) {
register_cached_methods(
register_methods(
this,
{
{"type:lineno", &json_experimental_type::get_lineno},
Expand Down Expand Up @@ -281,7 +281,7 @@ class json_experimental_field : public mstch_field {
const field_generator_context* field_context,
source_manager* sm)
: mstch_field(f, ctx, pos, field_context), source_mgr_(*sm) {
register_cached_methods(
register_methods(
this,
{
{"field:lineno", &json_experimental_field::get_lineno},
Expand All @@ -307,7 +307,7 @@ class json_experimental_typedef : public mstch_typedef {
mstch_element_position pos,
source_manager* sm)
: mstch_typedef(t, ctx, pos), source_mgr_(*sm) {
register_cached_methods(
register_methods(
this,
{
{"typedef:lineno", &json_experimental_typedef::get_lineno},
Expand Down Expand Up @@ -335,7 +335,7 @@ class json_experimental_enum : public mstch_enum {
mstch_element_position pos,
source_manager* sm)
: mstch_enum(e, ctx, pos), source_mgr_(*sm) {
register_cached_methods(
register_methods(
this,
{
{"enum:empty?", &json_experimental_enum::is_empty},
Expand All @@ -361,7 +361,7 @@ class json_experimental_enum_value : public mstch_enum_value {
mstch_element_position pos,
source_manager* sm)
: mstch_enum_value(ev, ctx, pos), source_mgr_(*sm) {
register_cached_methods(
register_methods(
this,
{
{"enum_value:lineno", &json_experimental_enum_value::get_lineno},
Expand Down Expand Up @@ -392,7 +392,7 @@ class json_experimental_const_value : public mstch_const_value {
source_manager* sm)
: mstch_const_value(cv, ctx, pos, current_const, expected_type),
source_mgr_(*sm) {
register_cached_methods(
register_methods(
this,
{
{"value:integer_or_enum?",
Expand Down
24 changes: 12 additions & 12 deletions thrift/compiler/generate/t_mstch_cpp2_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class cpp_mstch_program : public mstch_program {
split_id_(split_id),
split_structs_(split_structs),
sm_(sm) {
register_cached_methods(
register_methods(
this,
{{"program:cpp_includes", &cpp_mstch_program::cpp_includes},
{"program:qualified_namespace",
Expand Down Expand Up @@ -797,7 +797,7 @@ class cpp_mstch_service : public mstch_service {
int32_t split_id = 0,
int32_t split_count = 1)
: mstch_service(service, ctx, pos, containing_service), sm_(sm) {
register_cached_methods(
register_methods(
this,
{
{"service:program_name", &cpp_mstch_service::program_name},
Expand Down Expand Up @@ -948,7 +948,7 @@ class cpp_mstch_function : public mstch_function {
std::shared_ptr<cpp2_generator_context> cpp_ctx)
: mstch_function(function, ctx, pos, iface),
cpp_context_(std::move(cpp_ctx)) {
register_cached_methods(
register_methods(
this,
{
{"function:eb", &cpp_mstch_function::event_based},
Expand Down Expand Up @@ -1062,7 +1062,7 @@ class cpp_mstch_type : public mstch_type {
mstch_element_position pos,
std::shared_ptr<cpp2_generator_context> cpp_ctx)
: mstch_type(type, ctx, pos), cpp_context_(std::move(cpp_ctx)) {
register_cached_methods(
register_methods(
this,
{
{"type:resolves_to_base?", &cpp_mstch_type::resolves_to_base},
Expand Down Expand Up @@ -1241,7 +1241,7 @@ class cpp_mstch_typedef : public mstch_typedef {
mstch_element_position pos,
std::shared_ptr<cpp2_generator_context> cpp_ctx)
: mstch_typedef(t, ctx, pos), cpp_context_(std::move(cpp_ctx)) {
register_cached_methods(
register_methods(
this,
{
{"typedef:cpp_type", &cpp_mstch_typedef::cpp_type},
Expand All @@ -1263,7 +1263,7 @@ class cpp_mstch_struct : public mstch_struct {
mstch_element_position pos,
std::shared_ptr<cpp2_generator_context> cpp_ctx)
: mstch_struct(s, ctx, pos), cpp_context_(std::move(cpp_ctx)) {
register_cached_methods(
register_methods(
this,
{
{"struct:fields_size", &cpp_mstch_struct::fields_size},
Expand Down Expand Up @@ -1819,7 +1819,7 @@ class cpp_mstch_field : public mstch_field {
std::shared_ptr<cpp2_generator_context> cpp_ctx)
: mstch_field(f, ctx, pos, field_context),
cpp_context_(std::move(cpp_ctx)) {
register_cached_methods(
register_methods(
this,
{
{"field:name_hash", &cpp_mstch_field::name_hash},
Expand Down Expand Up @@ -2178,7 +2178,7 @@ class cpp_mstch_enum : public mstch_enum {
cpp_mstch_enum(
const t_enum* e, mstch_context& ctx, mstch_element_position pos)
: mstch_enum(e, ctx, pos) {
register_cached_methods(
register_methods(
this,
{
{"enum:empty?", &cpp_mstch_enum::is_empty},
Expand Down Expand Up @@ -2294,7 +2294,7 @@ class cpp_mstch_enum_value : public mstch_enum_value {
cpp_mstch_enum_value(
const t_enum_value* ev, mstch_context& ctx, mstch_element_position pos)
: mstch_enum_value(ev, ctx, pos) {
register_cached_methods(
register_methods(
this,
{
{"enum_value:name_hash", &cpp_mstch_enum_value::name_hash},
Expand Down Expand Up @@ -2330,7 +2330,7 @@ class cpp_mstch_const : public mstch_const {
std::shared_ptr<cpp2_generator_context> cpp_ctx)
: mstch_const(c, ctx, pos, current_const, expected_type, field),
cpp_context_(std::move(cpp_ctx)) {
register_cached_methods(
register_methods(
this,
{
{"constant:enum_value", &cpp_mstch_const::enum_value},
Expand Down Expand Up @@ -2409,7 +2409,7 @@ class cpp_mstch_const_value : public mstch_const_value {
const t_const* current_const,
const t_type* expected_type)
: mstch_const_value(cv, ctx, pos, current_const, expected_type) {
register_cached_methods(
register_methods(
this,
{
{"value:default_construct?",
Expand Down Expand Up @@ -2446,7 +2446,7 @@ class cpp_mstch_deprecated_annotation : public mstch_deprecated_annotation {
cpp_mstch_deprecated_annotation(
const t_annotation* a, mstch_context& ctx, mstch_element_position pos)
: mstch_deprecated_annotation(a, ctx, pos) {
register_cached_methods(
register_methods(
this,
{
{"annotation:safe_key", &cpp_mstch_deprecated_annotation::safe_key},
Expand Down
Loading

0 comments on commit f9af23e

Please sign in to comment.