-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathutil.cc
More file actions
873 lines (780 loc) · 31.2 KB
/
Copy pathutil.cc
File metadata and controls
873 lines (780 loc) · 31.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cctype>
#include <utility>
#include <boost/algorithm/string.hpp>
#include <fmt/format.h>
#include <thrift/compiler/ast/t_type.h>
#include <thrift/compiler/generate/go/util.h>
namespace apache::thrift::compiler::go {
// Name of the field of the response helper struct where
// the return value is stored (if function call is not void).
const std::string DEFAULT_RETVAL_FIELD_NAME = "success";
// The import path for the supporting metadata library
const std::string THRIFT_METADATA_IMPORT =
"github.com/facebook/fbthrift/thrift/lib/thrift/metadata";
// Keywords
// https://go.dev/ref/spec#Keywords
static const std::set<std::string> go_keywords = {
"break", "case", "chan", "const", "continue", "default", "defer",
"else", "fallthrough", "for", "func", "go", "goto", "if",
"import", "interface", "map", "package", "range", "return", "select",
"struct", "switch", "type", "var",
};
// Predeclared types
// https://go.dev/ref/spec#Predeclared_identifiers
static const std::set<std::string> go_predeclared_types = {
"any", "bool", "byte", "comparable", "complex64", "complex128",
"error", "float32", "float64", "int", "int8", "int16",
"int32", "int64", "rune", "string", "uint", "uint8",
"uint16", "uint32", "uint64", "uintptr",
};
// Predelcared functions
// https://go.dev/ref/spec#Predeclared_identifiers
static const std::set<std::string> go_predeclared_funcs = {
"append",
"cap",
"clear",
"close",
"complex",
"copy",
"delete",
"imag",
"len",
"make",
"max",
"min",
"new",
"panic",
"print",
"println",
"real",
"recover",
};
// Predelcared misc (constants, zero value)
// https://go.dev/ref/spec#Predeclared_identifiers
static const std::set<std::string> go_predeclared_misc = {
"true",
"false",
"iota",
"nil",
};
static const std::set<std::string> go_reserved_words = []() {
std::set<std::string> set;
set.insert(go_keywords.cbegin(), go_keywords.cend());
set.insert(go_predeclared_types.cbegin(), go_predeclared_types.cend());
set.insert(go_predeclared_funcs.cbegin(), go_predeclared_funcs.cend());
set.insert(go_predeclared_misc.cbegin(), go_predeclared_misc.cend());
return set;
}();
// common_initialisms from https://github.com/golang/lint/blob/master/lint.go
static const std::set<std::string> common_initialisms = {
"ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID",
"HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS",
"RAM", "RHS", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP",
"TLS", "TTL", "UDP", "UI", "UID", "URI", "URL", "UTF8",
"UUID", "VM", "XML", "XMPP", "XSRF", "XSS",
};
// To avoid conflict with methods (e.g. Error(), String())
static const std::set<std::string> reserved_field_names = {
"Error",
"String",
};
void codegen_data::set_current_program(const t_program* program) {
current_program_ = program;
// Prevent collisions with *this* program's package name
auto pkg_name = go::get_go_package_base_name(program);
go_package_name_collisions_[pkg_name] = 0;
}
std::string codegen_data::make_go_package_name_unique(const std::string& name) {
// Uses go_package_name_collisions_ map to keep track of name collisions in
// order to uniquify package names. When a collision is detected, i.e. package
// or program with the same name - an incrementing numeric suffix is added.
auto unique_name = name;
if (is_go_reserved_word(name)) {
// Emplaces only if not already in map.
go_package_name_collisions_.try_emplace(name, 0);
}
auto iter = go_package_name_collisions_.find(name);
if (iter == go_package_name_collisions_.end()) {
go_package_name_collisions_[name] = 0;
} else {
auto numSuffix = iter->second;
unique_name = name + std::to_string(numSuffix);
go_package_name_collisions_[name] = numSuffix + 1;
}
return unique_name;
}
void codegen_data::register_visitors(
basic_ast_visitor<true, const_visitor_context&>& visitor) {
// Populate package aliases and package name collisions
visitor.add_program_visitor(
[this](const const_visitor_context&, const t_program& node) {
if (&node != current_program_) {
return;
}
for (const t_program* include : node.get_includes_for_codegen()) {
std::string unique_package_name = make_go_package_name_unique(
go::munge_ident(
go::get_go_package_base_name(include), /*exported=*/false));
go_package_map_.emplace(
go::get_go_package_name(include), std::move(unique_package_name));
}
});
// Populate disambiguated field setter names for all programs (including
// included programs). This is needed because templates may access
// go_setter_name on fields from included structs (e.g. when generating
// default values that reference fields from included programs).
visitor.add_structured_definition_visitor(
[this](const const_visitor_context&, const t_structured& node) {
add_struct_go_field_setter_names(node);
});
// Populate metadata types
visitor.add_enum_visitor(
[this](const const_visitor_context& ctx, const t_enum& node) {
if (&ctx.program() == current_program_) {
add_to_thrift_metadata_types(node);
}
});
visitor.add_typedef_visitor(
[this](const const_visitor_context& ctx, const t_typedef& node) {
if (&ctx.program() == current_program_) {
add_to_thrift_metadata_types(*node.type());
add_to_thrift_metadata_types(node);
}
});
visitor.add_structured_definition_visitor(
[this](const const_visitor_context& ctx, const t_structured& node) {
if (&ctx.program() == current_program_) {
// Iterate fields here instead of using a field visitor so field type
// metadata is ordered before the enclosing structured type.
for (const auto& field : node.fields()) {
add_to_thrift_metadata_types(*field.type());
}
add_to_thrift_metadata_types(node);
}
});
// Populate function metadata for supported functions, including ephemeral
// request/response structs and their metadata.
visitor.add_function_visitor(
[this](const const_visitor_context& ctx, const t_function& node) {
// Process supported service and interaction functions in the current
// program.
if (&ctx.program() != current_program_ ||
(!go::is_func_go_client_supported(&node) &&
!go::is_func_go_server_supported(&node))) {
return;
}
const auto* parent = static_cast<const t_interface*>(ctx.parent());
size_t initial_size = req_resp_structs.size();
make_func_req_resp_structs(
&node, go::munge_ident(parent->name()), req_resp_structs);
// `make_func_req_resp_structs` doesn't always generate a field on the
// response struct for the return type (specifically for void
// responses), but we need to ensure the metadata is always present
add_to_thrift_metadata_types(*node.return_type());
// Populate metadata for newly generated structs. The fields contain
// params, exceptions, etc.
for (size_t i = initial_size; i < req_resp_structs.size(); i++) {
const t_struct* strct = req_resp_structs[i];
for (const auto& field : strct->fields()) {
add_to_thrift_metadata_types(*field.type());
}
add_struct_go_field_setter_names(*strct);
}
});
}
bool codegen_data::is_req_resp_struct(const t_structured& structured) const {
return structured.generated() &&
std::find(
req_resp_structs.begin(), req_resp_structs.end(), &structured) !=
req_resp_structs.end();
}
void codegen_data::add_to_thrift_metadata_types(const t_type& type) {
// Filter out external types (i.e. types defined in other programs).
// Primitive (base) types should be treated as internal and kept.
// For those types - 'program' will be null, so account for that.
if (type.program() != nullptr && !is_current_program(type.program())) {
return;
}
if (!metadata_visited_types_.insert(type.get_full_name()).second) {
// We have already visited this type.
return;
}
// The recursion below is equivalent to post-order tree traversal.
// It ensures that the types are recorded in the dependency order.
if (const t_typedef* typedef_ = type.try_as<t_typedef>()) {
add_to_thrift_metadata_types(*typedef_->type());
} else if (const t_list* list_type = type.try_as<t_list>()) {
add_to_thrift_metadata_types(*list_type->elem_type());
} else if (const t_set* set_type = type.try_as<t_set>()) {
add_to_thrift_metadata_types(*set_type->elem_type());
} else if (const t_map* map_type = type.try_as<t_map>()) {
add_to_thrift_metadata_types(*map_type->key_type());
add_to_thrift_metadata_types(*map_type->val_type());
}
thrift_metadata_types.push_back(&type);
}
bool codegen_data::is_current_program(const t_program* program) const {
return (program == current_program_);
}
std::string_view codegen_data::maybe_munge_ident_and_cache(
const t_named* named, bool exported, bool compact) {
assert(named);
if (auto name = get_go_name_annotation(named)) {
return *name;
}
go_munged_names_cache_key_ key{named->name(), exported, compact};
auto& item = go_munged_names_cache_[key];
if (item.view.data() != nullptr) {
return item.view;
}
return item.view = item.ownership =
munge_ident(named->name(), exported, compact);
}
std::string codegen_data::get_go_package_alias(const t_program* program) const {
if (is_current_program(program)) {
return "";
}
auto package = go::get_go_package_name(program);
auto iter = go_package_map_.find(package);
if (iter != go_package_map_.end()) {
return iter->second;
}
throw std::runtime_error(
fmt::format("unable to determine Go package alias '{}'", package));
}
std::string codegen_data::go_package_alias_prefix(
const t_program* program) const {
auto alias = get_go_package_alias(program);
if (alias == "") {
return "";
} else {
return alias + ".";
}
}
std::string get_go_package_name(const t_program* program) {
std::string real_package = program->get_namespace("go");
if (!real_package.empty()) {
return real_package;
}
return boost::algorithm::to_lower_copy(program->name());
}
std::string get_go_package_dir(const t_program* program) {
auto go_package = get_go_package_name(program);
if (go_package.find('/') != std::string::npos) {
return go_package;
}
return boost::replace_all_copy(go_package, ".", "/");
}
std::string get_go_package_base_name(const t_program* program) {
auto go_package = get_go_package_name(program);
std::vector<std::string> parts;
// The go package name can be separated by slashes or dots.
// Slashes can only be used if it was quoted, for example `namespace go
// 'foo/bar'`. These quotes are already removed, when we get to this function.
// Either way the go package name is always the last part.
// e.g. 'foo/bar' -> bar
// foo -> foo
// foo.bar -> bar
// foo.bar.baz -> baz
if (go_package.find('/') != std::string::npos) {
boost::split(parts, go_package, boost::is_any_of("/"));
} else {
boost::split(parts, go_package, boost::is_any_of("."));
}
auto base_name = go_package;
if (parts.size() > 0) {
base_name = parts.back();
}
// Avoid package base name collisions with reserved words
if (is_go_reserved_word(base_name)) {
base_name += "_";
}
return base_name;
}
// Convert snake_case to UpperCamelCase and captialize common initialisms.
std::string munge_ident(const std::string& ident, bool exported, bool compat) {
assert(!ident.empty());
std::ostringstream out;
size_t word_start = 0;
for (size_t i = 0; i < ident.size(); i++) {
char cur_char = ident.at(i);
bool eow = false;
if (i + 1 == ident.size()) {
eow = true;
} else {
char next_char = ident.at(i + 1);
if ((next_char == '_') ||
(islower(cur_char) && isupper(next_char) && !compat)) {
eow = true;
} else if (cur_char == '_') {
word_start = i + 1;
if (!islower(next_char)) {
// Keep underscores, unless followed by a lowercase word.
out << cur_char;
}
}
}
if (!eow) {
continue;
}
size_t word_len = i - word_start + 1;
std::string word = ident.substr(word_start, word_len);
std::string upper = boost::algorithm::to_upper_copy(word);
bool is_initialism = (common_initialisms.count(upper) > 0);
bool is_first_word = (word_start == 0);
size_t next_underscore_pos = ident.find('_', word_start);
bool is_legacy_substr_bug =
(next_underscore_pos != std::string::npos &&
next_underscore_pos > word_len);
if (is_initialism) {
// Compat: legacy generator does not change initialisms
// at the beginning of the string to uppercase.
// Compat: legacy generator does not change initialisms
// to uppercase if it hits a substring bug.
if (!(compat && is_first_word) && !(compat && is_legacy_substr_bug)) {
boost::algorithm::to_upper(word);
}
}
if (is_first_word) {
if (exported) {
word.at(0) = toupper(word.at(0));
} else {
if (is_initialism) {
// Make the entire initialism lowercase
boost::algorithm::to_lower(word);
} else {
word.at(0) = tolower(word.at(0));
}
}
} else {
word.at(0) = toupper(word.at(0));
}
out << word;
// reset the word
word_start = i + 1;
}
auto result = out.str();
// We add underscores to names starting with New to avoid name collisions with
// constructors. For example, if we are given a NewFoo message and a Foo
// message, the Foo message will generate a NewFoo constructor function, which
// will conflict with the NewFoo struct.
if (result.starts_with("New")) {
result += '_';
}
if (is_go_reserved_word(result)) {
result += '_';
}
return result;
}
std::string quote(const std::string& data) {
std::ostringstream quoted;
quoted << '"';
for (auto ch : data) {
if (ch == '\t') {
quoted << '\\' << 't';
} else if (ch == '\r') {
quoted << '\\' << 'r';
} else if (ch == '\n') {
quoted << '\\' << 'n';
} else if (ch == '\\' || ch == '"') {
quoted << '\\' << ch;
} else if (ch < '\x7f') {
quoted << ch;
} else {
throw std::runtime_error("Non-ASCII string literal not implemented");
}
}
quoted << '"';
return quoted.str();
}
// Convert CamelCase to snake_case.
std::string snakecase(const std::string& name) {
std::ostringstream snake;
char last = '_';
for (auto ch : name) {
if (isupper(ch)) {
if (last != '_') {
// Don't insert '_' after an existing one, such as in `Sample_CalcRs`.
// Also don't put a '_' right at the front.
snake << '_';
}
last = (char)tolower(ch);
} else {
last = ch;
}
snake << last;
}
return snake.str();
}
bool is_func_go_client_supported(const t_function* func) {
// "Interaction constructor" is a legacy API, which we will not support.
return !func->is_interaction_constructor();
}
bool is_func_go_server_supported(const t_function* func) {
// "Interaction constructor" is a legacy API, which we will not support.
return !func->is_interaction_constructor();
}
bool is_go_reserved_word(const std::string& value) {
return go_reserved_words.count(value) > 0;
}
bool is_type_go_struct(const t_type* type) {
// Whether the given Thrift type is represented by a Go struct:
// * Thrift struct - represented by Go struct pointer
// * Thrift union - represented by Go struct pointer
// * Thrift exception - represented by Go struct pointer
return type->is<t_structured>();
}
bool is_type_go_nilable(const t_type* type) {
// Whether the underlying Go type can be set to 'nil':
// * Thrift list - represented by Go slice - nilable
// * Thrift set - represented by Go slice - nilable
// * Thrift binary - represented by Go slice - nilable
// * Thrift map - represented by Go map - nilable
// Go struct backed types (see is_type_go_struct above):
// * Thrift struct - represented by Go struct pointer - nilable
// * Thrift union - represented by Go struct pointer - nilable
// * Thrift exception - represented by Go struct pointer - nilable
return type->is<t_list>() || type->is<t_set>() || type->is<t_map>() ||
type->is_binary() || is_type_go_struct(type);
}
bool is_type_go_comparable(
const t_type* type, std::map<std::string, int> visited_type_names) {
// Whether the underlying Go type is comparable.
// As per: https://go.dev/ref/spec#Comparison_operators
// > Slice, map, and function types are not comparable.
// (By extension - structs with slice or map fields are incomparable.)
// Struct hierarchy can sometime be recursive.
// Check if we have already visited this type.
auto type_name = type->get_full_name();
auto iter = visited_type_names.find(type_name);
if (iter != visited_type_names.end() && iter->second > 1) {
return true;
}
// All of the types below are represented by either slice or a map.
auto real_type = type->get_true_type();
if (real_type->is<t_list>() || real_type->is<t_map>() ||
real_type->is<t_set>() || real_type->is_binary()) {
return false;
}
if (const t_structured* as_struct = real_type->try_as<t_structured>()) {
for (const auto& member : as_struct->fields()) {
auto member_type = &member.type().deref();
auto member_name = member_type->get_full_name();
// Insert 0 if member_name is not yet in the map.
auto emplace_pair = visited_type_names.emplace(member_name, 0);
emplace_pair.first->second += 1;
if (!is_type_go_comparable(member_type, visited_type_names)) {
return false;
}
}
}
return true;
}
bool is_type_metadata_primitive(const t_type* type) {
// Whether this type is primitive from metadata.thrift perspective.
// i.e. see ThriftPrimitiveType enum in metadata.thrift
return type->is_bool() || type->is_byte() || type->is_i16() ||
type->is_i32() || type->is_i64() || type->is_float() ||
type->is_double() || type->is_binary() || type->is_string() ||
type->is_void();
}
std::string go_name(const t_named& named) {
if (const std::string* name_override = go::get_go_name_annotation(&named)) {
return *name_override;
}
return go::munge_ident(named.name());
}
std::string get_go_func_name(const t_function* func) {
auto name_override = get_go_name_annotation(func);
if (name_override != nullptr) {
return *name_override;
}
return munge_ident(func->name());
}
std::string get_go_field_name(const t_field* field) {
auto name_override = get_go_name_annotation(field);
if (name_override != nullptr) {
return *name_override;
}
auto name = munge_ident(field->name());
if (reserved_field_names.count(name) > 0) {
name += "_";
}
return name;
}
std::string get_go_type_sanitized_full_name(const t_type& type) {
std::string full_name = type.get_full_name();
boost::replace_all(full_name, " ", "");
boost::replace_all(full_name, ".", "_");
boost::replace_all(full_name, ",", "_");
boost::replace_all(full_name, "<", "_");
boost::replace_all(full_name, ">", "");
return full_name;
}
std::string get_go_type_metadata_name(const t_type& type) {
return fmt::format(
"premadeThriftType_{}", get_go_type_sanitized_full_name(type));
}
std::string get_go_type_codec_type_spec_name(const t_type& type) {
return fmt::format(
"premadeCodecTypeSpec_{}", get_go_type_sanitized_full_name(type));
}
void codegen_data::add_struct_go_field_setter_names(
const t_structured& tstruct) {
std::set<std::string> collisions;
for (const t_field& field : tstruct.fields()) {
// Pre-populate all field names, as they are definite collisions
collisions.insert(go::get_go_field_name(&field));
}
for (const t_field& field : tstruct.fields()) {
// Determine unique setter names for each field, disambiguating any
// collisions with fields or other setters
std::string setter_name =
fmt::format("Set{}", go::get_go_field_name(&field));
// Keep adding `_` to the end until the name is unique. If the name is
// already unique, the first insert succeeds and aborts the loop before any
// appends
while (!collisions.insert(setter_name).second) {
setter_name += "_";
}
field_setter_names[&field] = setter_name;
}
}
std::string get_go_func_unique_arg_name(
const t_function* func, std::string const& desired_arg_name) {
const auto& members = func->params().fields();
std::set<std::string> arg_names;
for (auto& member : members) {
arg_names.insert(go::munge_ident(member.name(), /*exported*/ false));
}
std::string unique_name = desired_arg_name;
auto current_num = 0;
while (arg_names.count(unique_name) > 0) {
unique_name = desired_arg_name + std::to_string(++current_num);
}
return unique_name;
}
void make_func_req_resp_structs(
const t_function* func,
const std::string& prefix,
std::vector<const t_struct*>& req_resp_structs) {
auto funcGoName = go::get_go_func_name(func);
auto req_struct_name = go::munge_ident("req" + prefix + funcGoName, false);
// TODO(T244354071): This is a pre-existing memory leak. See explanation on
// go::codegen_data::req_resp_structs
auto req_struct = new t_struct(func->program(), req_struct_name);
req_struct->set_generated();
for (const auto& member : func->params().fields()) {
// TODO(T244354071): Second unique_ptr over the same underlying object. See
// explanation on go::codegen_data::req_resp_structs
req_struct->append_field(
std::unique_ptr<t_field>(const_cast<t_field*>(&member)));
}
req_resp_structs.push_back(req_struct);
auto resp_struct_name = go::munge_ident("resp" + prefix + funcGoName, false);
// TODO(T244354071): This is a pre-existing memory leak. See explanation on
// go::codegen_data::req_resp_structs
auto resp_struct = new t_struct(func->program(), resp_struct_name);
resp_struct->set_generated();
if (!func->return_type()->is_void()) {
auto resp_field = std::make_unique<t_field>(
func->return_type(), DEFAULT_RETVAL_FIELD_NAME, 0);
resp_field->set_qualifier(t_field_qualifier::optional);
resp_struct->append_field(std::move(resp_field));
}
if (func->exceptions() != nullptr) {
for (const auto& xs : func->exceptions()->fields()) {
// TODO(T244354071): Second unique_ptr over the same underlying object.
// See explanation on go::codegen_data::req_resp_structs
auto xc_ptr = std::unique_ptr<t_field>(const_cast<t_field*>(&xs));
// TODO(T244354071): This is a mutation of `xs` (which is const, from a
// const t_function), since the mutable unique_ptr xc_ptr is pointing to
// the same object. It mutates the original field to force it to be
// optional, and the code-generator relies on this behaviour. The
// template/code-gen should be refactored to generate optional fields for
// exceptions rather than mutating the AST.
xc_ptr->set_qualifier(t_field_qualifier::optional);
resp_struct->append_field(std::move(xc_ptr));
}
}
req_resp_structs.push_back(resp_struct);
if (func->stream()) {
auto stream_struct_name =
go::munge_ident("stream" + prefix + funcGoName, false);
// TODO(T244354071): This is a pre-existing memory leak. See explanation on
// go::codegen_data::req_resp_structs
auto stream_struct = new t_struct(func->program(), stream_struct_name);
stream_struct->set_generated();
auto elem_field = std::make_unique<t_field>(
func->stream()->elem_type(), DEFAULT_RETVAL_FIELD_NAME, 0);
elem_field->set_qualifier(t_field_qualifier::optional);
stream_struct->append_field(std::move(elem_field));
if (func->stream()->exceptions() != nullptr) {
for (const auto& xs : func->stream()->exceptions()->fields()) {
// TODO(T244354071): Second unique_ptr over the same underlying object.
// See explanation on go::codegen_data::req_resp_structs
auto xc_ptr = std::unique_ptr<t_field>(const_cast<t_field*>(&xs));
// TODO(T244354071): This is a mutation of `xs` (which is const, from a
// const t_function), since the mutable unique_ptr xc_ptr is pointing to
// the same object. It mutates the original field to force it to be
// optional, and the code-generator relies on this behaviour. The
// template/code-gen should be refactored to generate optional fields
// for exceptions rather than mutating the AST.
xc_ptr->set_qualifier(t_field_qualifier::optional);
stream_struct->append_field(std::move(xc_ptr));
}
}
req_resp_structs.push_back(stream_struct);
}
if (func->sink()) {
auto sink_struct_name =
go::munge_ident("sink" + prefix + funcGoName, false);
auto sink_struct = new t_struct(func->program(), sink_struct_name);
sink_struct->set_generated();
auto elem_field = std::make_unique<t_field>(
func->sink()->elem_type(), DEFAULT_RETVAL_FIELD_NAME, 0);
elem_field->set_qualifier(t_field_qualifier::optional);
sink_struct->append_field(std::move(elem_field));
if (func->sink()->sink_exceptions() != nullptr) {
for (const auto& xs : func->sink()->sink_exceptions()->fields()) {
// TODO(T244354071): Second unique_ptr over the same underlying object.
// See explanation on go::codegen_data::req_resp_structs
auto xc_ptr = std::unique_ptr<t_field>(const_cast<t_field*>(&xs));
// TODO(T244354071): This is a mutation of `xs` (which is const, from a
// const t_function), since the mutable unique_ptr xc_ptr is pointing to
// the same object. It mutates the original field to force it to be
// optional, and the code-generator relies on this behaviour. The
// template/code-gen should be refactored to generate optional fields
// for exceptions rather than mutating the AST.
xc_ptr->set_qualifier(t_field_qualifier::optional);
sink_struct->append_field(std::move(xc_ptr));
}
}
req_resp_structs.push_back(sink_struct);
// Bidi functions have no final_response_type on the sink
// (the response comes as a stream instead).
if (!func->is_bidirectional_stream()) {
auto final_response_struct_name =
go::munge_ident("respFinal" + prefix + funcGoName, false);
auto final_response_struct =
new t_struct(func->program(), final_response_struct_name);
final_response_struct->set_generated();
auto final_response_field = std::make_unique<t_field>(
func->sink()->final_response_type(), DEFAULT_RETVAL_FIELD_NAME, 0);
final_response_field->set_qualifier(t_field_qualifier::optional);
final_response_struct->append_field(std::move(final_response_field));
if (func->sink()->final_response_exceptions() != nullptr) {
for (const auto& xs :
func->sink()->final_response_exceptions()->fields()) {
// TODO(T244354071): Second unique_ptr over the same underlying
// object. See explanation on go::codegen_data::req_resp_structs
auto xc_ptr = std::unique_ptr<t_field>(const_cast<t_field*>(&xs));
// TODO(T244354071): This is a mutation of `xs` (which is const, from
// a const t_function), since the mutable unique_ptr xc_ptr is
// pointing to the same object. It mutates the original field to force
// it to be optional, and the code-generator relies on this behaviour.
// The template/code-gen should be refactored to generate optional
// fields for exceptions rather than mutating the AST.
xc_ptr->set_qualifier(t_field_qualifier::optional);
final_response_struct->append_field(std::move(xc_ptr));
}
}
req_resp_structs.push_back(final_response_struct);
}
}
}
const std::string* get_go_name_annotation(const t_named* node) {
auto name_annotation = node->find_structured_annotation_or_null(kGoNameUri);
if (name_annotation != nullptr) {
return &(name_annotation->get_value_from_structured_annotation("name")
.get_string());
}
return nullptr;
}
const std::string* get_go_tag_annotation(const t_named* node) {
auto tag_annotation = node->find_structured_annotation_or_null(kGoTagUri);
if (tag_annotation != nullptr) {
return &(tag_annotation->get_value_from_structured_annotation("tag")
.get_string());
}
return nullptr;
}
int get_field_size(const t_field* field, bool is_inside_union) {
// Assume 64-bit architecture
auto real_type = field->type()->get_true_type();
auto qualifier = field->qualifier();
if ((qualifier == t_field_qualifier::optional || is_inside_union) &&
!is_type_go_nilable(real_type)) {
return 8; // pointer
}
if (const auto* primitive = real_type->try_as<t_primitive_type>()) {
switch (primitive->primitive_type()) {
case t_primitive_type::type::t_bool:
return 1;
case t_primitive_type::type::t_byte:
return 1;
case t_primitive_type::type::t_i16:
return 2;
case t_primitive_type::type::t_i32:
return 4;
case t_primitive_type::type::t_i64:
return 8;
case t_primitive_type::type::t_float:
return 4;
case t_primitive_type::type::t_double:
return 8;
case t_primitive_type::type::t_string:
return 16; // Golang: unsafe.Sizeof("")
case t_primitive_type::type::t_binary:
return 24; // Golang: unsafe.Sizeof([]byte{})
case t_primitive_type::type::t_void:
return 0;
}
} else if (real_type->is<t_enum>()) {
return 4; // Backed by int32
} else if (real_type->is<t_list>()) {
return 24; // Golang: unsafe.Sizeof([]int{})
} else if (real_type->is<t_set>()) {
return 24; // Golang: unsafe.Sizeof([]int{})
} else if (real_type->is<t_map>()) {
return 8; // Golang: unsafe.Sizeof(map[string]string{})
} else if (real_type->is<t_structured>()) {
return 8; // pointer
}
return 0;
}
void optimize_fields_layout(
std::vector<const t_field*>& fields, bool is_union) {
std::stable_sort(
fields.begin(),
fields.end(),
[is_union](const auto* lhs, const auto* rhs) {
// Sort by field size, descending
return get_field_size(lhs, is_union) > get_field_size(rhs, is_union);
});
}
std::string doc_comment(const t_named* named) {
std::istringstream in(named->doc());
std::string line;
std::ostringstream out;
while (std::getline(in, line)) {
out << "// " << line << std::endl;
}
return out.str();
}
} // namespace apache::thrift::compiler::go