Skip to content

Commit 4ae6842

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Migrate cpp_mstch_function properties to Whisker and delete class
Summary: Migrate all 6 properties from `cpp_mstch_function` to the `make_prototype_for_function` Whisker prototype, then delete the `cpp_mstch_function` class entirely: - `eb` → `event_based?` (renamed for clarity) - `sync_returns_by_outparam?` - `prefixed_name` - `has_deprecated_header_client_methods` - `virtual_client_methods?` - `legacy_client_methods?` The `interface()` mstch helper is replaced by `context().get_function_parent()` in the Whisker properties. Template changes: - Renamed `function:eb` → `function:event_based?` across all cpp2 templates - Converted mstch `{{#function:event_based?}}`/`{{^function:event_based?}}` conditional pairs to Whisker `{{#if}}`/`{{#else}}`/`{{/if}}` syntax - Simplified nested negation conditions to `(not (or ...))` in `service_null.mustache` and `service_null_impl.mustache` Reviewed By: praihan Differential Revision: D95436828 fbshipit-source-id: d7e9ae825cffaacb3f53c69675dc17681f4cc5f9
1 parent 50970b4 commit 4ae6842

File tree

7 files changed

+77
-98
lines changed

7 files changed

+77
-98
lines changed

third-party/thrift/src/thrift/compiler/generate/t_mstch_cpp2_generator.cc

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,57 @@ class t_mstch_cpp2_generator : public t_mstch_generator {
735735
return cpp2::is_stack_arguments(compiler_options(), function);
736736
});
737737

738+
def.property("event_based?", [this](const t_function& f) {
739+
const t_interface* parent = context().get_function_parent(&f);
740+
assert(parent != nullptr);
741+
return f.get_unstructured_annotation("thread") == "eb" ||
742+
f.has_structured_annotation(kCppProcessInEbThreadUri) ||
743+
parent->has_unstructured_annotation("process_in_event_base") ||
744+
parent->has_structured_annotation(kCppProcessInEbThreadUri);
745+
});
746+
747+
def.property("sync_returns_by_outparam?", [](const t_function& f) {
748+
return is_complex_return(f.return_type()->get_true_type()) &&
749+
!f.interaction() && !f.sink_or_stream();
750+
});
751+
752+
def.property("prefixed_name", [this](const t_function& f) {
753+
const t_interface* parent = context().get_function_parent(&f);
754+
assert(parent != nullptr);
755+
const std::string& name = cpp2::get_name(&f);
756+
return parent->is<t_interaction>()
757+
? fmt::format("{}_{}", parent->name(), name)
758+
: name;
759+
});
760+
761+
def.property(
762+
"has_deprecated_header_client_methods", [this](const t_function& f) {
763+
const t_interface* parent = context().get_function_parent(&f);
764+
assert(parent != nullptr);
765+
return f.has_structured_annotation(
766+
kCppGenerateDeprecatedHeaderClientMethodsUri) ||
767+
f.has_unstructured_annotation(
768+
"cpp.generate_deprecated_header_client_methods") ||
769+
parent->has_structured_annotation(
770+
kCppGenerateDeprecatedHeaderClientMethodsUri) ||
771+
parent->has_unstructured_annotation(
772+
"cpp.generate_deprecated_header_client_methods");
773+
});
774+
775+
def.property("virtual_client_methods?", [this](const t_function& f) {
776+
const t_interface* parent = context().get_function_parent(&f);
777+
assert(parent != nullptr);
778+
return !generate_reduced_client(*parent) && !f.interaction() &&
779+
!f.is_bidirectional_stream();
780+
});
781+
782+
def.property("legacy_client_methods?", [this](const t_function& f) {
783+
const t_interface* parent = context().get_function_parent(&f);
784+
assert(parent != nullptr);
785+
return !generate_reduced_client(*parent) && !f.interaction() &&
786+
!f.is_bidirectional_stream();
787+
});
788+
738789
return std::move(def).make();
739790
}
740791

@@ -1266,11 +1317,10 @@ class cpp_mstch_service : public mstch_service {
12661317
const t_service* service,
12671318
mstch_context& ctx,
12681319
mstch_element_position pos,
1269-
source_manager& sm,
12701320
const t_service* containing_service = nullptr,
12711321
int32_t split_id = 0,
12721322
int32_t split_count = 1)
1273-
: mstch_service(service, ctx, pos, containing_service), sm_(sm) {
1323+
: mstch_service(service, ctx, pos, containing_service) {
12741324
register_methods(
12751325
this,
12761326
{
@@ -1324,7 +1374,6 @@ class cpp_mstch_service : public mstch_service {
13241374
}
13251375

13261376
std::vector<const t_function*> split_functions_;
1327-
source_manager& sm_;
13281377
};
13291378

13301379
class cpp_mstch_interaction : public cpp_mstch_service {
@@ -1335,75 +1384,8 @@ class cpp_mstch_interaction : public cpp_mstch_service {
13351384
const t_interaction* interaction,
13361385
mstch_context& ctx,
13371386
mstch_element_position pos,
1338-
const t_service* containing_service,
1339-
source_manager& sm)
1340-
: cpp_mstch_service(interaction, ctx, pos, sm, containing_service) {}
1341-
};
1342-
1343-
class cpp_mstch_function : public mstch_function {
1344-
public:
1345-
cpp_mstch_function(
1346-
const t_function* function,
1347-
mstch_context& ctx,
1348-
mstch_element_position pos,
1349-
std::shared_ptr<cpp2_generator_context> cpp_ctx)
1350-
: mstch_function(function, ctx, pos), cpp_context_(std::move(cpp_ctx)) {
1351-
register_methods(
1352-
this,
1353-
{
1354-
{"function:eb", &cpp_mstch_function::event_based},
1355-
{"function:sync_returns_by_outparam?",
1356-
&cpp_mstch_function::sync_returns_by_outparam},
1357-
{"function:prefixed_name", &cpp_mstch_function::prefixed_name},
1358-
{"function:has_deprecated_header_client_methods",
1359-
&cpp_mstch_function::has_deprecated_header_client_methods},
1360-
{"function:virtual_client_methods?",
1361-
&cpp_mstch_function::virtual_client_methods},
1362-
{"function:legacy_client_methods?",
1363-
&cpp_mstch_function::legacy_client_methods},
1364-
});
1365-
}
1366-
mstch::node event_based() {
1367-
return function_->get_unstructured_annotation("thread") == "eb" ||
1368-
function_->has_structured_annotation(kCppProcessInEbThreadUri) ||
1369-
interface().has_unstructured_annotation("process_in_event_base") ||
1370-
interface().has_structured_annotation(kCppProcessInEbThreadUri);
1371-
}
1372-
mstch::node sync_returns_by_outparam() {
1373-
return is_complex_return(function_->return_type()->get_true_type()) &&
1374-
!function_->interaction() && !function_->sink_or_stream();
1375-
}
1376-
1377-
mstch::node prefixed_name() {
1378-
const std::string& name = cpp2::get_name(function_);
1379-
return interface().is<t_interaction>()
1380-
? fmt::format("{}_{}", interface().name(), name)
1381-
: name;
1382-
}
1383-
1384-
mstch::node has_deprecated_header_client_methods() {
1385-
return function_->has_structured_annotation(
1386-
kCppGenerateDeprecatedHeaderClientMethodsUri) ||
1387-
function_->has_unstructured_annotation(
1388-
"cpp.generate_deprecated_header_client_methods") ||
1389-
interface().has_structured_annotation(
1390-
kCppGenerateDeprecatedHeaderClientMethodsUri) ||
1391-
interface().has_unstructured_annotation(
1392-
"cpp.generate_deprecated_header_client_methods");
1393-
}
1394-
1395-
mstch::node virtual_client_methods() {
1396-
return !generate_reduced_client(interface()) && !function_->interaction() &&
1397-
!function_->is_bidirectional_stream();
1398-
}
1399-
1400-
mstch::node legacy_client_methods() {
1401-
return !generate_reduced_client(interface()) && !function_->interaction() &&
1402-
!function_->is_bidirectional_stream();
1403-
}
1404-
1405-
private:
1406-
std::shared_ptr<cpp2_generator_context> cpp_context_;
1387+
const t_service* containing_service)
1388+
: cpp_mstch_service(interaction, ctx, pos, containing_service) {}
14071389
};
14081390

14091391
bool needs_op_encode(const t_type& type);
@@ -2471,9 +2453,8 @@ void t_mstch_cpp2_generator::generate_program() {
24712453

24722454
void t_mstch_cpp2_generator::set_mstch_factories() {
24732455
mstch_context_.add<cpp_mstch_program>(std::ref(source_mgr_));
2474-
mstch_context_.add<cpp_mstch_service>(std::ref(source_mgr_));
2475-
mstch_context_.add<cpp_mstch_interaction>(std::ref(source_mgr_));
2476-
mstch_context_.add<cpp_mstch_function>(cpp_context_);
2456+
mstch_context_.add<cpp_mstch_service>();
2457+
mstch_context_.add<cpp_mstch_interaction>();
24772458
mstch_context_.add<cpp_mstch_type>(cpp_context_);
24782459
mstch_context_.add<cpp_mstch_struct>(cpp_context_);
24792460
mstch_context_.add<cpp_mstch_field>(cpp_context_);
@@ -2637,7 +2618,6 @@ void t_mstch_cpp2_generator::generate_out_of_line_service(
26372618
service,
26382619
mstch_context_,
26392620
mstch_element_position(),
2640-
source_mgr_,
26412621
nullptr,
26422622
split_id,
26432623
split_count);

third-party/thrift/src/thrift/compiler/generate/templates/cpp2/service_cpp/processmap.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ apache::thrift::ServiceRequestInfoMap {{service:cpp_name}}ServiceInfoHolder::sta
4949
apache::thrift::ServiceRequestInfoMap requestInfoMap = {
5050
{{#service:functions}}{{#function:return_type}}{{^function:starts_interaction?}}
5151
{"{{function:name}}",
52-
{ {{#function:eb}}true{{/function:eb}}{{^function:eb}}false{{/function:eb}},
52+
{ {{#if function:event_based?}}true{{#else}}false{{/if}},
5353
{{> types/function_kind}},
5454
"{{service:parent_service_name}}.{{> common/function_name}}",
5555
std::nullopt,
@@ -59,7 +59,7 @@ apache::thrift::ServiceRequestInfoMap {{service:cpp_name}}ServiceInfoHolder::sta
5959
{{/function:starts_interaction?}}{{/function:return_type}}{{/service:functions}}
6060
{{#service:interactions}}{{#service:functions}}{{#function:return_type}}
6161
{"{{service:name}}.{{function:name}}",
62-
{ {{#function:eb}}true{{/function:eb}}{{^function:eb}}false{{/function:eb}},
62+
{ {{#if function:event_based?}}true{{#else}}false{{/if}},
6363
{{> types/function_kind}},
6464
"{{service:parent_service_name}}.{{> common/function_name}}",
6565
"{{service:name}}",

third-party/thrift/src/thrift/compiler/generate/templates/cpp2/service_cpp/service_interface_impl.mustache

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//
2828
// Method '{{function:name}}'
2929
//
30-
{{^function:eb}}
30+
{{#if (not function:event_based?)}}
3131

3232
{{> service_common/sync_function_return_type_server}} {{> service_common/service_class_name}}::{{function:cpp_name}}({{> service_common/sync_function_return_param_server}}{{> service_common/function_param_list_server_commented_out}}) {
3333
apache::thrift::detail::si::throw_app_exn_unimplemented("{{function:name}}");
@@ -229,8 +229,7 @@ determineInvocationType:
229229
callback->exception(std::current_exception());
230230
}
231231
}
232-
{{/function:eb}}
233-
{{#function:eb}}{{!Event base codegen}}
232+
{{#else}}
234233
{{#function:oneway?}}
235234

236235
void {{> service_common/service_class_name}}::async_eb_{{function:cpp_name}}(
@@ -246,7 +245,7 @@ void {{> service_common/service_class_name}}::async_eb_{{function:cpp_name}}(
246245
apache::thrift::detail::si::create_app_exn_unimplemented("{{function:name}}"));
247246
}
248247
{{/function:oneway?}}
249-
{{/function:eb}}
248+
{{/if (not function:event_based?)}}
250249
//
251250
// End of Method '{{function:name}}'
252251
//

third-party/thrift/src/thrift/compiler/generate/templates/cpp2/service_cpp/service_null_impl.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
17-
}}{{#service:functions}}{{#function:return_type}}{{^function:starts_interaction?}}{{^function:creates_interaction?}}{{^function:eb}}{{^function:stream?}}
17+
}}{{#service:functions}}{{#function:return_type}}{{#if (not (or function:starts_interaction? function:creates_interaction? function:event_based? function:stream?))}}
1818
{{> service_common/sync_function_return_type_server}} {{service:cpp_name}}SvNull::{{function:cpp_name}}({{> service_common/sync_function_return_param_server}}{{> service_common/function_param_list_server_commented_out}}) { {{!
1919
}}{{#type:resolves_to_complex_return?}} }{{/type:resolves_to_complex_return?}}
2020
{{^type:resolves_to_complex_return?}}
@@ -33,4 +33,4 @@
3333
}
3434
{{/type:resolves_to_complex_return?}}
3535

36-
{{/function:stream?}}{{/function:eb}}{{/function:creates_interaction?}}{{/function:starts_interaction?}}{{/function:return_type}}{{/service:functions}}
36+
{{/if (not (or function:starts_interaction? function:creates_interaction? function:event_based? function:stream?))}}{{/function:return_type}}{{/service:functions}}

third-party/thrift/src/thrift/compiler/generate/templates/cpp2/service_h/service_interface_functions.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}}{{#let function_comma = (if function:params.fields? ", " null)}}
2020
{{#if (not function:starts_interaction?)}}
2121
{{#function:return_type}}
22-
{{^function:eb}}
22+
{{#if (not function:event_based?)}}
2323
virtual {{> service_common/sync_function_return_type_server}} sync_{{function:cpp_name}}({{> service_common/sync_function_return_param_server}}{{> service_common/function_param_list_server_commented_out}});
2424
[[deprecated("Use sync_{{function:cpp_name}} instead")]] virtual {{> service_common/sync_function_return_type_server}} {{function:cpp_name}}({{> service_common/sync_function_return_param_server}}{{> service_common/function_param_list_server_commented_out}});
2525
{{^service:interaction?}}
@@ -30,12 +30,12 @@
3030
virtual folly::coro::Task<{{> types/return_type_server}}> co_{{function:cpp_name}}({{> service_common/function_param_list_server}});
3131
virtual folly::coro::Task<{{> types/return_type_server}}> co_{{function:cpp_name}}(apache::thrift::RequestParams params{{function_comma}}{{> service_common/function_param_list_server}});
3232
#endif
33-
{{/function:eb}}
33+
{{/if (not function:event_based?)}}
3434
{{#function:oneway?}}
35-
virtual void async_{{#function:eb}}eb{{/function:eb}}{{^function:eb}}tm{{/function:eb}}_{{function:cpp_name}}(apache::thrift::HandlerCallbackOneWay::Ptr callback{{function_comma}}{{> service_common/function_param_list_server}});
35+
virtual void async_{{#if function:event_based?}}eb{{#else}}tm{{/if}}_{{function:cpp_name}}(apache::thrift::HandlerCallbackOneWay::Ptr callback{{function_comma}}{{> service_common/function_param_list_server}});
3636
{{/function:oneway?}}
3737
{{^function:oneway?}}
38-
virtual void async_{{#function:eb}}eb{{/function:eb}}{{^function:eb}}tm{{/function:eb}}_{{function:cpp_name}}(apache::thrift::HandlerCallbackPtr<{{> types/return_type_server}}> callback{{function_comma}}{{> service_common/function_param_list_server}});
38+
virtual void async_{{#if function:event_based?}}eb{{#else}}tm{{/if}}_{{function:cpp_name}}(apache::thrift::HandlerCallbackPtr<{{> types/return_type_server}}> callback{{function_comma}}{{> service_common/function_param_list_server}});
3939
{{/function:oneway?}}
4040
{{/function:return_type}}
4141
{{#else}}
@@ -47,7 +47,7 @@
4747
static {{service:qualified_name}}ServiceInfoHolder __fbthrift_serviceInfoHolder;
4848
{{/service:interaction?}}
4949
{{#service:functions}}
50-
{{^function:eb}}
50+
{{#if (not function:event_based?)}}
5151
std::atomic<apache::thrift::detail::si::InvocationType> __fbthrift_invocation_{{function:cpp_name}}{apache::thrift::detail::si::InvocationType::AsyncTm};
52-
{{/function:eb}}
52+
{{/if (not function:event_based?)}}
5353
{{/service:functions}}

third-party/thrift/src/thrift/compiler/generate/templates/cpp2/service_h/service_null.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
}}class {{service:cpp_name}}SvNull : public ::apache::thrift::ServiceHandler<{{service:cpp_name}}>{{#service:extends}}, virtual public ::apache::thrift::ServiceHandler<::{{service:cpp_qualified_namespace}}::{{service:cpp_name}}>{{/service:extends}} {
1818
public:
19-
{{#service:functions}}{{#function:return_type}}{{^function:starts_interaction?}}{{^function:creates_interaction?}}{{^function:eb}}{{^function:stream?}}
19+
{{#service:functions}}{{#function:return_type}}{{#if (not (or function:starts_interaction? function:creates_interaction? function:event_based? function:stream?))}}
2020
{{> service_common/sync_function_return_type_server}} {{function:cpp_name}}({{> service_common/sync_function_return_param_server}}{{> service_common/function_param_list_server_commented_out}}) override;
21-
{{/function:stream?}}{{/function:eb}}{{/function:creates_interaction?}}{{/function:starts_interaction?}}{{/function:return_type}}{{/service:functions}}
21+
{{/if (not (or function:starts_interaction? function:creates_interaction? function:event_based? function:stream?))}}{{/function:return_type}}{{/service:functions}}
2222
};

third-party/thrift/src/thrift/compiler/generate/templates/cpp2/service_tcc/process_and_return.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ void {{service:parent_service_cpp_name}}AsyncProcessor::setUpAndProcess_{{functi
3838
folly::EventBase* eb,
3939
[[maybe_unused]] apache::thrift::concurrency::ThreadManager* tm) {
4040
if (!setUpRequestProcessing(
41-
req, ctx, eb, {{#if function:eb}}nullptr{{#else}}tm{{/if}}, {{> types/function_kind}}, iface_{{#if service:interaction?}}, "{{interaction:name}}"{{/if}}{{#if function:creates_interaction?}}, "{{function:created_interaction.cpp_name}}", true{{/if}})) {
41+
req, ctx, eb, {{#if function:event_based?}}nullptr{{#else}}tm{{/if}}, {{> types/function_kind}}, iface_{{#if service:interaction?}}, "{{interaction:name}}"{{/if}}{{#if function:creates_interaction?}}, "{{function:created_interaction.cpp_name}}", true{{/if}})) {
4242
return;
4343
}
44-
{{^function:eb}}
44+
{{#if (not function:event_based?)}}
4545
auto scope = iface_->getRequestExecutionScope(
4646
ctx, apache::thrift::concurrency::{{function:priority}});
4747
ctx->setRequestExecutionScope(std::move(scope));
@@ -55,7 +55,7 @@ void {{service:parent_service_cpp_name}}AsyncProcessor::setUpAndProcess_{{functi
5555
&{{service:parent_service_cpp_name}}AsyncProcessor::
5656
executeRequest_{{function:prefixed_name}}<ProtocolIn_, ProtocolOut_>,
5757
this);
58-
{{/function:eb}}{{#function:eb}}
58+
{{#else}}
5959
{{#if in_or_creates_interaction?}}
6060
processInThread(
6161
std::move(req),
@@ -86,7 +86,7 @@ void {{service:parent_service_cpp_name}}AsyncProcessor::setUpAndProcess_{{functi
8686
{}};
8787
executeRequest_{{function:prefixed_name}}<ProtocolIn_, ProtocolOut_>(std::move(serverRequest));
8888
{{/if in_or_creates_interaction?}}
89-
{{/function:eb}}
89+
{{/if (not function:event_based?)}}
9090
}
9191

9292
template <typename ProtocolIn_, typename ProtocolOut_>
@@ -253,7 +253,7 @@ void {{service:parent_service_cpp_name}}AsyncProcessor::executeRequest_{{#servic
253253
throw_wrapped_{{#service:interaction?}}{{service:name}}_{{/service:interaction?}}{{function:cpp_name}}<ProtocolIn_, ProtocolOut_>,
254254
serverRequest.requestContext()->getProtoSeqId(),
255255
apache::thrift::detail::ServerRequestHelper::eventBase(serverRequest),
256-
{{^function:eb}}apache::thrift::detail::ServerRequestHelper::executor(serverRequest){{/function:eb}}{{#function:eb}}nullptr{{/function:eb}},
256+
{{#if function:event_based?}}nullptr{{#else}}apache::thrift::detail::ServerRequestHelper::executor(serverRequest){{/if}},
257257
serverRequest.requestContext(),
258258
requestPileNotification,
259259
concurrencyControllerNotification,
@@ -278,7 +278,7 @@ void {{service:parent_service_cpp_name}}AsyncProcessor::executeRequest_{{#servic
278278
return;
279279
}
280280
{{/if service:self.interaction?}}
281-
ifacePtr->{{#function:eb}}async_eb{{/function:eb}}{{^function:eb}}async_tm{{/function:eb}}_{{function:cpp_name}}(std::move(cb){{> service_tcc/get_args_ref}});
281+
ifacePtr->{{#if function:event_based?}}async_eb{{#else}}async_tm{{/if}}_{{function:cpp_name}}(std::move(cb){{> service_tcc/get_args_ref}});
282282
};
283283
};
284284
#if FOLLY_HAS_COROUTINES

0 commit comments

Comments
 (0)