Skip to content

Commit 5c8a486

Browse files
vitautfacebook-github-bot
authored andcommitted
Merge t_whisker_base_generator and t_whisker_generator
Summary: Merge `t_whisker_base_generator` and `t_whisker_generator`, making `t_mstch_generator` inherit from `t_whisker_generator` and merge globals from the latter. This is done to make `t_<node>` functions available in mstch generators for the incremental migration to Whisker and getting rid of the intermediate `mstch_<node>` layer. Currently this is a noop because no functions are registered yet. Reviewed By: praihan Differential Revision: D70255300 fbshipit-source-id: 97f01723dd91622074e903fb04d4d893bc9d7745
1 parent 4c6a6fc commit 5c8a486

File tree

4 files changed

+22
-37
lines changed

4 files changed

+22
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ mstch::map t_mstch_generator::prepend_prefix(
415415

416416
whisker::map t_mstch_generator::globals() const {
417417
auto options = render_options();
418-
whisker::map result;
418+
whisker::map result = t_whisker_generator::globals();
419419
for (const auto& undefined_name : options.allowed_undefined_variables) {
420420
result.insert({undefined_name, whisker::make::null});
421421
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131

3232
namespace apache::thrift::compiler {
3333

34-
class t_mstch_generator : public t_whisker_base_generator {
34+
class t_mstch_generator : public t_whisker_generator {
3535
public:
36-
using t_whisker_base_generator::t_whisker_base_generator;
36+
using t_whisker_generator::t_whisker_generator;
3737

3838
void process_options(
3939
const std::map<std::string, std::string>& options) override {
@@ -70,7 +70,7 @@ class t_mstch_generator : public t_whisker_base_generator {
7070
*/
7171
const std::string& get_template(const std::string& template_name);
7272

73-
using t_whisker_base_generator::render;
73+
using t_whisker_generator::render;
7474
/**
7575
* Render the mstch template with name `template_name` in the given context.
7676
*/
@@ -219,7 +219,7 @@ class t_mstch_generator : public t_whisker_base_generator {
219219
*/
220220
std::map<std::string, std::string> options_;
221221

222-
whisker::map globals() const final;
222+
whisker::map globals() const final override;
223223
strictness_options strictness() const final;
224224

225225
/**

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ class whisker_source_parser : public whisker::source_resolver {
273273

274274
} // namespace
275275

276-
/* static */ const t_whisker_base_generator::templates_map&
277-
t_whisker_base_generator::templates_by_path() {
276+
/* static */ const t_whisker_generator::templates_map&
277+
t_whisker_generator::templates_by_path() {
278278
static const auto cached_result = [] {
279279
templates_map result;
280280
for (std::size_t i = 0; i < templates_size; ++i) {
@@ -297,8 +297,7 @@ t_whisker_base_generator::templates_by_path() {
297297
return cached_result;
298298
}
299299

300-
t_whisker_base_generator::cached_render_state&
301-
t_whisker_base_generator::render_state() {
300+
t_whisker_generator::cached_render_state& t_whisker_generator::render_state() {
302301
if (!cached_render_state_) {
303302
whisker::render_options options;
304303

@@ -331,7 +330,7 @@ t_whisker_base_generator::render_state() {
331330
return *cached_render_state_;
332331
}
333332

334-
std::string t_whisker_base_generator::render(
333+
std::string t_whisker_generator::render(
335334
std::string_view template_file, const whisker::object& context) {
336335
cached_render_state& state = render_state();
337336
const whisker::ast::root* ast = state.source_resolver->resolve_import(
@@ -350,7 +349,7 @@ std::string t_whisker_base_generator::render(
350349
return out.str();
351350
}
352351

353-
void t_whisker_base_generator::write_to_file(
352+
void t_whisker_generator::write_to_file(
354353
const std::filesystem::path& output_file, std::string_view data) {
355354
auto abs_path = detail::make_abs_path(fs_path(get_out_dir()), output_file);
356355
std::filesystem::create_directories(abs_path.parent_path());
@@ -370,7 +369,7 @@ void t_whisker_base_generator::write_to_file(
370369
record_genfile(abs_path.string());
371370
}
372371

373-
void t_whisker_base_generator::render_to_file(
372+
void t_whisker_generator::render_to_file(
374373
const std::filesystem::path& output_file,
375374
std::string_view template_file,
376375
const whisker::object& context) {

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,9 @@
6060
namespace apache::thrift::compiler {
6161

6262
/**
63-
* Base class for all template-based code generators using Whisker as the
64-
* templating engine.
65-
*
66-
* This class serves as the basis for both:
67-
* - t_mstch_generator, the legacy mstch-based generator
68-
* - t_whisker_generator, the pure Whisker-based generator
63+
* A template-based code generator that uses Whisker as the templating engine.
6964
*/
70-
class t_whisker_base_generator : public t_generator {
65+
class t_whisker_generator : public t_generator {
7166
public:
7267
using t_generator::t_generator;
7368

@@ -82,7 +77,7 @@ class t_whisker_base_generator : public t_generator {
8277
* The global context used for whisker rendering. This function can be used,
8378
* for example, to add globally available helper functions.
8479
*/
85-
virtual whisker::map globals() const { return {}; }
80+
virtual whisker::map globals() const;
8681

8782
// See whisker::render_options
8883
struct strictness_options {
@@ -145,21 +140,6 @@ class t_whisker_base_generator : public t_generator {
145140
std::string_view template_file,
146141
const whisker::object& context);
147142

148-
private:
149-
struct cached_render_state {
150-
whisker::diagnostics_engine diagnostic_engine;
151-
std::shared_ptr<whisker::source_resolver> source_resolver;
152-
whisker::render_options render_options;
153-
};
154-
std::optional<cached_render_state> cached_render_state_;
155-
cached_render_state& render_state();
156-
};
157-
158-
class t_whisker_generator : public t_whisker_base_generator {
159-
public:
160-
using t_whisker_base_generator::t_whisker_base_generator;
161-
162-
protected:
163143
// Whisker-based code generators are designed to work directly against the
164144
// Thrift AST node types (t_<node_type> classes). This is different from the
165145
// mstch-based code generators which have an additional conversion layer from
@@ -286,8 +266,14 @@ class t_whisker_generator : public t_whisker_base_generator {
286266
h_program>;
287267
using h_node = make_handle<t_node, h_named>;
288268

289-
public:
290-
whisker::map globals() const override;
269+
private:
270+
struct cached_render_state {
271+
whisker::diagnostics_engine diagnostic_engine;
272+
std::shared_ptr<whisker::source_resolver> source_resolver;
273+
whisker::render_options render_options;
274+
};
275+
std::optional<cached_render_state> cached_render_state_;
276+
cached_render_state& render_state();
291277
};
292278

293279
} // namespace apache::thrift::compiler

0 commit comments

Comments
 (0)