-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
bindings_generator.cpp
4905 lines (4060 loc) · 177 KB
/
bindings_generator.cpp
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* bindings_generator.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "bindings_generator.h"
#if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
#include "../godotsharp_defs.h"
#include "../utils/naming_utils.h"
#include "../utils/path_utils.h"
#include "../utils/string_utils.h"
#include "core/config/engine.h"
#include "core/core_constants.h"
#include "core/io/compression.h"
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
#include "core/os/os.h"
#include "main/main.h"
StringBuilder &operator<<(StringBuilder &r_sb, const String &p_string) {
r_sb.append(p_string);
return r_sb;
}
StringBuilder &operator<<(StringBuilder &r_sb, const char *p_cstring) {
r_sb.append(p_cstring);
return r_sb;
}
#define CS_INDENT " " // 4 whitespaces
#define INDENT1 CS_INDENT
#define INDENT2 INDENT1 INDENT1
#define INDENT3 INDENT2 INDENT1
#define INDENT4 INDENT3 INDENT1
#define MEMBER_BEGIN "\n" INDENT1
#define OPEN_BLOCK "{\n"
#define CLOSE_BLOCK "}\n"
#define OPEN_BLOCK_L1 INDENT1 OPEN_BLOCK
#define OPEN_BLOCK_L2 INDENT2 OPEN_BLOCK
#define OPEN_BLOCK_L3 INDENT3 OPEN_BLOCK
#define CLOSE_BLOCK_L1 INDENT1 CLOSE_BLOCK
#define CLOSE_BLOCK_L2 INDENT2 CLOSE_BLOCK
#define CLOSE_BLOCK_L3 INDENT3 CLOSE_BLOCK
#define BINDINGS_GLOBAL_SCOPE_CLASS "GD"
#define BINDINGS_NATIVE_NAME_FIELD "NativeName"
#define CS_PARAM_MEMORYOWN "memoryOwn"
#define CS_PARAM_METHODBIND "method"
#define CS_PARAM_INSTANCE "ptr"
#define CS_STATIC_METHOD_GETINSTANCE "GetPtr"
#define CS_METHOD_CALL "Call"
#define CS_PROPERTY_SINGLETON "Singleton"
#define CS_SINGLETON_INSTANCE_SUFFIX "Instance"
#define CS_METHOD_INVOKE_GODOT_CLASS_METHOD "InvokeGodotClassMethod"
#define CS_METHOD_HAS_GODOT_CLASS_METHOD "HasGodotClassMethod"
#define CS_METHOD_HAS_GODOT_CLASS_SIGNAL "HasGodotClassSignal"
#define CS_STATIC_FIELD_NATIVE_CTOR "NativeCtor"
#define CS_STATIC_FIELD_METHOD_BIND_PREFIX "MethodBind"
#define CS_STATIC_FIELD_METHOD_PROXY_NAME_PREFIX "MethodProxyName_"
#define CS_STATIC_FIELD_SIGNAL_PROXY_NAME_PREFIX "SignalProxyName_"
#define ICALL_PREFIX "godot_icall_"
#define ICALL_CLASSDB_GET_METHOD "ClassDB_get_method"
#define ICALL_CLASSDB_GET_METHOD_WITH_COMPATIBILITY "ClassDB_get_method_with_compatibility"
#define ICALL_CLASSDB_GET_CONSTRUCTOR "ClassDB_get_constructor"
#define C_LOCAL_RET "ret"
#define C_LOCAL_VARARG_RET "vararg_ret"
#define C_LOCAL_PTRCALL_ARGS "call_args"
#define C_CLASS_NATIVE_FUNCS "NativeFuncs"
#define C_NS_MONOUTILS "InteropUtils"
#define C_METHOD_UNMANAGED_GET_MANAGED C_NS_MONOUTILS ".UnmanagedGetManaged"
#define C_METHOD_ENGINE_GET_SINGLETON C_NS_MONOUTILS ".EngineGetSingleton"
#define C_NS_MONOMARSHAL "Marshaling"
#define C_METHOD_MONOSTR_TO_GODOT C_NS_MONOMARSHAL ".ConvertStringToNative"
#define C_METHOD_MONOSTR_FROM_GODOT C_NS_MONOMARSHAL ".ConvertStringToManaged"
#define C_METHOD_MONOARRAY_TO(m_type) C_NS_MONOMARSHAL ".ConvertSystemArrayToNative" #m_type
#define C_METHOD_MONOARRAY_FROM(m_type) C_NS_MONOMARSHAL ".ConvertNative" #m_type "ToSystemArray"
#define C_METHOD_MANAGED_TO_CALLABLE C_NS_MONOMARSHAL ".ConvertCallableToNative"
#define C_METHOD_MANAGED_FROM_CALLABLE C_NS_MONOMARSHAL ".ConvertCallableToManaged"
#define C_METHOD_MANAGED_TO_SIGNAL C_NS_MONOMARSHAL ".ConvertSignalToNative"
#define C_METHOD_MANAGED_FROM_SIGNAL C_NS_MONOMARSHAL ".ConvertSignalToManaged"
// Types that will be ignored by the generator and won't be available in C#.
// This must be kept in sync with `ignored_types` in csharp_script.cpp
const Vector<String> ignored_types = {};
// Special [code] keywords to wrap with <see langword="code"/> instead of <c>code</c>.
// Don't check against all C# reserved words, as many cases are GDScript-specific.
const Vector<String> langword_check = { "true", "false", "null" };
// The following properties currently need to be defined with `new` to avoid warnings. We treat
// them as a special case instead of silencing the warnings altogether, to be warned if more
// shadowing appears.
const Vector<String> prop_allowed_inherited_member_hiding = {
"ArrayMesh.BlendShapeMode",
"Button.TextDirection",
"Label.TextDirection",
"LineEdit.TextDirection",
"LinkButton.TextDirection",
"MenuBar.TextDirection",
"RichTextLabel.TextDirection",
"TextEdit.TextDirection",
};
void BindingsGenerator::TypeInterface::postsetup_enum_type(BindingsGenerator::TypeInterface &r_enum_itype) {
// C interface for enums is the same as that of 'uint32_t'. Remember to apply
// any of the changes done here to the 'uint32_t' type interface as well.
r_enum_itype.cs_type = r_enum_itype.proxy_name;
r_enum_itype.cs_in_expr = "(int)%0";
r_enum_itype.cs_out = "%5return (%2)%0(%1);";
{
// The expected types for parameters and return value in ptrcall are 'int64_t' or 'uint64_t'.
r_enum_itype.c_in = "%5%0 %1_in = %1;\n";
r_enum_itype.c_out = "%5return (%0)(%1);\n";
r_enum_itype.c_type = "long";
r_enum_itype.c_arg_in = "&%s_in";
}
r_enum_itype.c_type_in = "int";
r_enum_itype.c_type_out = r_enum_itype.c_type_in;
r_enum_itype.class_doc = &EditorHelp::get_doc_data()->class_list[r_enum_itype.proxy_name];
}
static String fix_doc_description(const String &p_bbcode) {
// This seems to be the correct way to do this. It's the same EditorHelp does.
return p_bbcode.dedent()
.replace("\t", "")
.replace("\r", "")
.strip_edges();
}
String BindingsGenerator::bbcode_to_text(const String &p_bbcode, const TypeInterface *p_itype) {
// Based on the version in EditorHelp.
if (p_bbcode.is_empty()) {
return String();
}
DocTools *doc = EditorHelp::get_doc_data();
String bbcode = p_bbcode;
StringBuilder output;
List<String> tag_stack;
bool code_tag = false;
int pos = 0;
while (pos < bbcode.length()) {
int brk_pos = bbcode.find("[", pos);
if (brk_pos < 0) {
brk_pos = bbcode.length();
}
if (brk_pos > pos) {
String text = bbcode.substr(pos, brk_pos - pos);
if (code_tag || tag_stack.size() > 0) {
output.append("'" + text + "'");
} else {
output.append(text);
}
}
if (brk_pos == bbcode.length()) {
// Nothing else to add.
break;
}
int brk_end = bbcode.find("]", brk_pos + 1);
if (brk_end == -1) {
String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
if (code_tag || tag_stack.size() > 0) {
output.append("'" + text + "'");
}
break;
}
String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
if (tag.begins_with("/")) {
bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
if (!tag_ok) {
output.append("]");
pos = brk_pos + 1;
continue;
}
tag_stack.pop_front();
pos = brk_end + 1;
code_tag = false;
} else if (code_tag) {
output.append("[");
pos = brk_pos + 1;
} else if (tag.begins_with("method ") || tag.begins_with("constructor ") || tag.begins_with("operator ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ") || tag.begins_with("theme_item ") || tag.begins_with("param ")) {
const int tag_end = tag.find(" ");
const String link_tag = tag.substr(0, tag_end);
const String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
const Vector<String> link_target_parts = link_target.split(".");
if (link_target_parts.size() <= 0 || link_target_parts.size() > 2) {
ERR_PRINT("Invalid reference format: '" + tag + "'.");
output.append(tag);
pos = brk_end + 1;
continue;
}
const TypeInterface *target_itype;
StringName target_cname;
if (link_target_parts.size() == 2) {
target_itype = _get_type_or_null(TypeReference(link_target_parts[0]));
if (!target_itype) {
target_itype = _get_type_or_null(TypeReference("_" + link_target_parts[0]));
}
target_cname = link_target_parts[1];
} else {
target_itype = p_itype;
target_cname = link_target_parts[0];
}
if (link_tag == "method") {
_append_text_method(output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "constructor") {
// TODO: Support constructors?
_append_text_undeclared(output, link_target);
} else if (link_tag == "operator") {
// TODO: Support operators?
_append_text_undeclared(output, link_target);
} else if (link_tag == "member") {
_append_text_member(output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "signal") {
_append_text_signal(output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "enum") {
_append_text_enum(output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "constant") {
_append_text_constant(output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "param") {
_append_text_param(output, link_target);
} else if (link_tag == "theme_item") {
// We do not declare theme_items in any way in C#, so there is nothing to reference.
_append_text_undeclared(output, link_target);
}
pos = brk_end + 1;
} else if (doc->class_list.has(tag)) {
if (tag == "Array" || tag == "Dictionary") {
output.append("'" BINDINGS_NAMESPACE_COLLECTIONS ".");
output.append(tag);
output.append("'");
} else if (tag == "bool" || tag == "int") {
output.append(tag);
} else if (tag == "float") {
output.append(
#ifdef REAL_T_IS_DOUBLE
"double"
#else
"float"
#endif
);
} else if (tag == "Variant") {
output.append("'Godot.Variant'");
} else if (tag == "String") {
output.append("string");
} else if (tag == "Nil") {
output.append("null");
} else if (tag.begins_with("@")) {
// @GlobalScope, @GDScript, etc.
output.append("'" + tag + "'");
} else if (tag == "PackedByteArray") {
output.append("byte[]");
} else if (tag == "PackedInt32Array") {
output.append("int[]");
} else if (tag == "PackedInt64Array") {
output.append("long[]");
} else if (tag == "PackedFloat32Array") {
output.append("float[]");
} else if (tag == "PackedFloat64Array") {
output.append("double[]");
} else if (tag == "PackedStringArray") {
output.append("string[]");
} else if (tag == "PackedVector2Array") {
output.append("'" BINDINGS_NAMESPACE ".Vector2[]'");
} else if (tag == "PackedVector3Array") {
output.append("'" BINDINGS_NAMESPACE ".Vector3[]'");
} else if (tag == "PackedColorArray") {
output.append("'" BINDINGS_NAMESPACE ".Color[]'");
} else if (tag == "PackedVector4Array") {
output.append("'" BINDINGS_NAMESPACE ".Vector4[]'");
} else {
const TypeInterface *target_itype = _get_type_or_null(TypeReference(tag));
if (!target_itype) {
target_itype = _get_type_or_null(TypeReference("_" + tag));
}
if (target_itype) {
output.append("'" + target_itype->proxy_name + "'");
} else {
ERR_PRINT("Cannot resolve type reference in documentation: '" + tag + "'.");
output.append("'" + tag + "'");
}
}
pos = brk_end + 1;
} else if (tag == "b") {
// Bold is not supported.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "i") {
// Italic is not supported.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "code" || tag.begins_with("code ")) {
code_tag = true;
pos = brk_end + 1;
tag_stack.push_front("code");
} else if (tag == "kbd") {
// Keyboard combinations are not supported.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "center") {
// Center alignment is not supported.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "br") {
// Break is not supported.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "u") {
// Underline is not supported.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "s") {
// Strikethrough is not supported.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "url") {
int end = bbcode.find("[", brk_end);
if (end == -1) {
end = bbcode.length();
}
String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
// Not supported. Just append the url.
output.append(url);
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag.begins_with("url=")) {
String url = tag.substr(4, tag.length());
// Not supported. Just append the url.
output.append(url);
pos = brk_end + 1;
tag_stack.push_front("url");
} else if (tag == "img") {
int end = bbcode.find("[", brk_end);
if (end == -1) {
end = bbcode.length();
}
String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
// Not supported. Just append the bbcode.
output.append("[img]");
output.append(image);
output.append("[/img]");
pos = end;
tag_stack.push_front(tag);
} else if (tag.begins_with("color=")) {
// Not supported.
pos = brk_end + 1;
tag_stack.push_front("color");
} else if (tag.begins_with("font=")) {
// Not supported.
pos = brk_end + 1;
tag_stack.push_front("font");
} else {
// Ignore unrecognized tag.
output.append("[");
pos = brk_pos + 1;
}
}
return output.as_string();
}
String BindingsGenerator::bbcode_to_xml(const String &p_bbcode, const TypeInterface *p_itype, bool p_is_signal) {
// Based on the version in EditorHelp.
if (p_bbcode.is_empty()) {
return String();
}
DocTools *doc = EditorHelp::get_doc_data();
String bbcode = p_bbcode;
StringBuilder xml_output;
xml_output.append("<para>");
List<String> tag_stack;
bool code_tag = false;
bool line_del = false;
int pos = 0;
while (pos < bbcode.length()) {
int brk_pos = bbcode.find("[", pos);
if (brk_pos < 0) {
brk_pos = bbcode.length();
}
if (brk_pos > pos) {
if (!line_del) {
String text = bbcode.substr(pos, brk_pos - pos);
if (code_tag || tag_stack.size() > 0) {
xml_output.append(text.xml_escape());
} else {
Vector<String> lines = text.split("\n");
for (int i = 0; i < lines.size(); i++) {
if (i != 0) {
xml_output.append("<para>");
}
xml_output.append(lines[i].xml_escape());
if (i != lines.size() - 1) {
xml_output.append("</para>\n");
}
}
}
}
}
if (brk_pos == bbcode.length()) {
// Nothing else to add.
break;
}
int brk_end = bbcode.find("]", brk_pos + 1);
if (brk_end == -1) {
if (!line_del) {
String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
if (code_tag || tag_stack.size() > 0) {
xml_output.append(text.xml_escape());
} else {
Vector<String> lines = text.split("\n");
for (int i = 0; i < lines.size(); i++) {
if (i != 0) {
xml_output.append("<para>");
}
xml_output.append(lines[i].xml_escape());
if (i != lines.size() - 1) {
xml_output.append("</para>\n");
}
}
}
}
break;
}
String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
if (tag.begins_with("/")) {
bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
if (!tag_ok) {
if (!line_del) {
xml_output.append("[");
}
pos = brk_pos + 1;
continue;
}
tag_stack.pop_front();
pos = brk_end + 1;
code_tag = false;
if (tag == "/url") {
xml_output.append("</a>");
} else if (tag == "/code") {
xml_output.append("</c>");
} else if (tag == "/codeblock") {
xml_output.append("</code>");
} else if (tag == "/b") {
xml_output.append("</b>");
} else if (tag == "/i") {
xml_output.append("</i>");
} else if (tag == "/csharp") {
xml_output.append("</code>");
line_del = true;
} else if (tag == "/codeblocks") {
line_del = false;
}
} else if (code_tag) {
xml_output.append("[");
pos = brk_pos + 1;
} else if (tag.begins_with("method ") || tag.begins_with("constructor ") || tag.begins_with("operator ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ") || tag.begins_with("theme_item ") || tag.begins_with("param ")) {
const int tag_end = tag.find(" ");
const String link_tag = tag.substr(0, tag_end);
const String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
const Vector<String> link_target_parts = link_target.split(".");
if (link_target_parts.size() <= 0 || link_target_parts.size() > 2) {
ERR_PRINT("Invalid reference format: '" + tag + "'.");
xml_output.append("<c>");
xml_output.append(tag);
xml_output.append("</c>");
pos = brk_end + 1;
continue;
}
const TypeInterface *target_itype;
StringName target_cname;
if (link_target_parts.size() == 2) {
target_itype = _get_type_or_null(TypeReference(link_target_parts[0]));
if (!target_itype) {
target_itype = _get_type_or_null(TypeReference("_" + link_target_parts[0]));
}
target_cname = link_target_parts[1];
} else {
target_itype = p_itype;
target_cname = link_target_parts[0];
}
if (link_tag == "method") {
_append_xml_method(xml_output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "constructor") {
// TODO: Support constructors?
_append_xml_undeclared(xml_output, link_target);
} else if (link_tag == "operator") {
// TODO: Support operators?
_append_xml_undeclared(xml_output, link_target);
} else if (link_tag == "member") {
_append_xml_member(xml_output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "signal") {
_append_xml_signal(xml_output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "enum") {
_append_xml_enum(xml_output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "constant") {
_append_xml_constant(xml_output, target_itype, target_cname, link_target, link_target_parts);
} else if (link_tag == "param") {
_append_xml_param(xml_output, link_target, p_is_signal);
} else if (link_tag == "theme_item") {
// We do not declare theme_items in any way in C#, so there is nothing to reference.
_append_xml_undeclared(xml_output, link_target);
}
pos = brk_end + 1;
} else if (doc->class_list.has(tag)) {
if (tag == "Array" || tag == "Dictionary") {
xml_output.append("<see cref=\"" BINDINGS_NAMESPACE_COLLECTIONS ".");
xml_output.append(tag);
xml_output.append("\"/>");
} else if (tag == "bool" || tag == "int") {
xml_output.append("<see cref=\"");
xml_output.append(tag);
xml_output.append("\"/>");
} else if (tag == "float") {
xml_output.append("<see cref=\""
#ifdef REAL_T_IS_DOUBLE
"double"
#else
"float"
#endif
"\"/>");
} else if (tag == "Variant") {
xml_output.append("<see cref=\"Godot.Variant\"/>");
} else if (tag == "String") {
xml_output.append("<see cref=\"string\"/>");
} else if (tag == "Nil") {
xml_output.append("<see langword=\"null\"/>");
} else if (tag.begins_with("@")) {
// @GlobalScope, @GDScript, etc.
xml_output.append("<c>");
xml_output.append(tag);
xml_output.append("</c>");
} else if (tag == "PackedByteArray") {
xml_output.append("<see cref=\"byte\"/>[]");
} else if (tag == "PackedInt32Array") {
xml_output.append("<see cref=\"int\"/>[]");
} else if (tag == "PackedInt64Array") {
xml_output.append("<see cref=\"long\"/>[]");
} else if (tag == "PackedFloat32Array") {
xml_output.append("<see cref=\"float\"/>[]");
} else if (tag == "PackedFloat64Array") {
xml_output.append("<see cref=\"double\"/>[]");
} else if (tag == "PackedStringArray") {
xml_output.append("<see cref=\"string\"/>[]");
} else if (tag == "PackedVector2Array") {
xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".Vector2\"/>[]");
} else if (tag == "PackedVector3Array") {
xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".Vector3\"/>[]");
} else if (tag == "PackedColorArray") {
xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".Color\"/>[]");
} else if (tag == "PackedVector4Array") {
xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".Vector4\"/>[]");
} else {
const TypeInterface *target_itype = _get_type_or_null(TypeReference(tag));
if (!target_itype) {
target_itype = _get_type_or_null(TypeReference("_" + tag));
}
if (target_itype) {
if ((!p_itype || p_itype->api_type == ClassDB::API_CORE) && target_itype->api_type == ClassDB::API_EDITOR) {
// Editor references in core documentation cannot be resolved,
// handle as standard codeblock.
_log("Cannot reference editor type '%s' in documentation for core type '%s'\n",
target_itype->proxy_name.utf8().get_data(), p_itype ? p_itype->proxy_name.utf8().get_data() : "@GlobalScope");
xml_output.append("<c>");
xml_output.append(target_itype->proxy_name);
xml_output.append("</c>");
} else {
xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
xml_output.append(target_itype->proxy_name);
xml_output.append("\"/>");
}
} else {
ERR_PRINT("Cannot resolve type reference in documentation: '" + tag + "'.");
xml_output.append("<c>");
xml_output.append(tag);
xml_output.append("</c>");
}
}
pos = brk_end + 1;
} else if (tag == "b") {
xml_output.append("<b>");
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "i") {
xml_output.append("<i>");
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "code" || tag.begins_with("code ")) {
int end = bbcode.find("[", brk_end);
if (end == -1) {
end = bbcode.length();
}
String code = bbcode.substr(brk_end + 1, end - brk_end - 1);
if (langword_check.has(code)) {
xml_output.append("<see langword=\"");
xml_output.append(code);
xml_output.append("\"/>");
pos = brk_end + code.length() + 8;
} else {
xml_output.append("<c>");
code_tag = true;
pos = brk_end + 1;
tag_stack.push_front("code");
}
} else if (tag == "codeblock" || tag.begins_with("codeblock ")) {
xml_output.append("<code>");
code_tag = true;
pos = brk_end + 1;
tag_stack.push_front("codeblock");
} else if (tag == "codeblocks") {
line_del = true;
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "csharp" || tag.begins_with("csharp ")) {
xml_output.append("<code>");
line_del = false;
code_tag = true;
pos = brk_end + 1;
tag_stack.push_front("csharp");
} else if (tag == "kbd") {
// Keyboard combinations are not supported in xml comments.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "center") {
// Center alignment is not supported in xml comments.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "br") {
xml_output.append("\n"); // FIXME: Should use <para> instead. Luckily this tag isn't used for now.
pos = brk_end + 1;
} else if (tag == "u") {
// Underline is not supported in Rider xml comments.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "s") {
// Strikethrough is not supported in xml comments.
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag == "url") {
int end = bbcode.find("[", brk_end);
if (end == -1) {
end = bbcode.length();
}
String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
xml_output.append("<a href=\"");
xml_output.append(url);
xml_output.append("\">");
xml_output.append(url);
pos = brk_end + 1;
tag_stack.push_front(tag);
} else if (tag.begins_with("url=")) {
String url = tag.substr(4, tag.length());
xml_output.append("<a href=\"");
xml_output.append(url);
xml_output.append("\">");
pos = brk_end + 1;
tag_stack.push_front("url");
} else if (tag == "img") {
int end = bbcode.find("[", brk_end);
if (end == -1) {
end = bbcode.length();
}
String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
// Not supported. Just append the bbcode.
xml_output.append("[img]");
xml_output.append(image);
xml_output.append("[/img]");
pos = end;
tag_stack.push_front(tag);
} else if (tag.begins_with("color=")) {
// Not supported.
pos = brk_end + 1;
tag_stack.push_front("color");
} else if (tag.begins_with("font=")) {
// Not supported.
pos = brk_end + 1;
tag_stack.push_front("font");
} else {
if (!line_del) {
// Ignore unrecognized tag.
xml_output.append("[");
}
pos = brk_pos + 1;
}
}
xml_output.append("</para>");
return xml_output.as_string();
}
void BindingsGenerator::_append_text_method(StringBuilder &p_output, const TypeInterface *p_target_itype, const StringName &p_target_cname, const String &p_link_target, const Vector<String> &p_link_target_parts) {
if (p_link_target_parts[0] == name_cache.type_at_GlobalScope) {
if (OS::get_singleton()->is_stdout_verbose()) {
OS::get_singleton()->print("Cannot resolve @GlobalScope method reference in documentation: %s\n", p_link_target.utf8().get_data());
}
// TODO Map what we can
_append_text_undeclared(p_output, p_link_target);
} else if (!p_target_itype || !p_target_itype->is_object_type) {
if (OS::get_singleton()->is_stdout_verbose()) {
if (p_target_itype) {
OS::get_singleton()->print("Cannot resolve method reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data());
} else {
OS::get_singleton()->print("Cannot resolve type from method reference in documentation: %s\n", p_link_target.utf8().get_data());
}
}
// TODO Map what we can
_append_text_undeclared(p_output, p_link_target);
} else {
if (p_target_cname == "_init") {
// The _init method is not declared in C#, reference the constructor instead
p_output.append("'new " BINDINGS_NAMESPACE ".");
p_output.append(p_target_itype->proxy_name);
p_output.append("()'");
} else {
const MethodInterface *target_imethod = p_target_itype->find_method_by_name(p_target_cname);
if (target_imethod) {
p_output.append("'" BINDINGS_NAMESPACE ".");
p_output.append(p_target_itype->proxy_name);
p_output.append(".");
p_output.append(target_imethod->proxy_name);
p_output.append("(");
bool first_key = true;
for (const ArgumentInterface &iarg : target_imethod->arguments) {
const TypeInterface *arg_type = _get_type_or_null(iarg.type);
if (first_key) {
first_key = false;
} else {
p_output.append(", ");
}
if (!arg_type) {
ERR_PRINT("Cannot resolve argument type in documentation: '" + p_link_target + "'.");
p_output.append(iarg.type.cname);
continue;
}
if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) {
p_output.append("Nullable<");
}
String arg_cs_type = arg_type->cs_type + _get_generic_type_parameters(*arg_type, iarg.type.generic_type_parameters);
p_output.append(arg_cs_type.replacen("params ", ""));
if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) {
p_output.append(">");
}
}
p_output.append(")'");
} else {
if (!p_target_itype->is_intentionally_ignored(p_link_target)) {
ERR_PRINT("Cannot resolve method reference in documentation: '" + p_link_target + "'.");
}
_append_text_undeclared(p_output, p_link_target);
}
}
}
}
void BindingsGenerator::_append_text_member(StringBuilder &p_output, const TypeInterface *p_target_itype, const StringName &p_target_cname, const String &p_link_target, const Vector<String> &p_link_target_parts) {
if (p_link_target.contains("/")) {
// Properties with '/' (slash) in the name are not declared in C#, so there is nothing to reference.
_append_text_undeclared(p_output, p_link_target);
} else if (!p_target_itype || !p_target_itype->is_object_type) {
if (OS::get_singleton()->is_stdout_verbose()) {
if (p_target_itype) {
OS::get_singleton()->print("Cannot resolve member reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data());
} else {
OS::get_singleton()->print("Cannot resolve type from member reference in documentation: %s\n", p_link_target.utf8().get_data());
}
}
// TODO Map what we can
_append_text_undeclared(p_output, p_link_target);
} else {
const TypeInterface *current_itype = p_target_itype;
const PropertyInterface *target_iprop = nullptr;
while (target_iprop == nullptr && current_itype != nullptr) {
target_iprop = current_itype->find_property_by_name(p_target_cname);
if (target_iprop == nullptr) {
current_itype = _get_type_or_null(TypeReference(current_itype->base_name));
}
}
if (target_iprop) {
p_output.append("'" BINDINGS_NAMESPACE ".");
p_output.append(current_itype->proxy_name);
p_output.append(".");
p_output.append(target_iprop->proxy_name);
p_output.append("'");
} else {
if (!p_target_itype->is_intentionally_ignored(p_link_target)) {
ERR_PRINT("Cannot resolve member reference in documentation: '" + p_link_target + "'.");
}
_append_text_undeclared(p_output, p_link_target);
}
}
}
void BindingsGenerator::_append_text_signal(StringBuilder &p_output, const TypeInterface *p_target_itype, const StringName &p_target_cname, const String &p_link_target, const Vector<String> &p_link_target_parts) {
if (!p_target_itype || !p_target_itype->is_object_type) {
if (OS::get_singleton()->is_stdout_verbose()) {
if (p_target_itype) {
OS::get_singleton()->print("Cannot resolve signal reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data());
} else {
OS::get_singleton()->print("Cannot resolve type from signal reference in documentation: %s\n", p_link_target.utf8().get_data());
}
}
// TODO Map what we can
_append_text_undeclared(p_output, p_link_target);
} else {
const SignalInterface *target_isignal = p_target_itype->find_signal_by_name(p_target_cname);
if (target_isignal) {
p_output.append("'" BINDINGS_NAMESPACE ".");
p_output.append(p_target_itype->proxy_name);
p_output.append(".");
p_output.append(target_isignal->proxy_name);
p_output.append("'");
} else {
if (!p_target_itype->is_intentionally_ignored(p_link_target)) {
ERR_PRINT("Cannot resolve signal reference in documentation: '" + p_link_target + "'.");
}
_append_text_undeclared(p_output, p_link_target);
}
}
}
void BindingsGenerator::_append_text_enum(StringBuilder &p_output, const TypeInterface *p_target_itype, const StringName &p_target_cname, const String &p_link_target, const Vector<String> &p_link_target_parts) {
const StringName search_cname = !p_target_itype ? p_target_cname : StringName(p_target_itype->name + "." + (String)p_target_cname);
HashMap<StringName, TypeInterface>::ConstIterator enum_match = enum_types.find(search_cname);
if (!enum_match && search_cname != p_target_cname) {
enum_match = enum_types.find(p_target_cname);
}
if (enum_match) {
const TypeInterface &target_enum_itype = enum_match->value;
p_output.append("'" BINDINGS_NAMESPACE ".");
p_output.append(target_enum_itype.proxy_name); // Includes nesting class if any
p_output.append("'");
} else {
if (!p_target_itype->is_intentionally_ignored(p_link_target)) {
ERR_PRINT("Cannot resolve enum reference in documentation: '" + p_link_target + "'.");
}
_append_text_undeclared(p_output, p_link_target);
}
}
void BindingsGenerator::_append_text_constant(StringBuilder &p_output, const TypeInterface *p_target_itype, const StringName &p_target_cname, const String &p_link_target, const Vector<String> &p_link_target_parts) {
if (p_link_target_parts[0] == name_cache.type_at_GlobalScope) {
_append_text_constant_in_global_scope(p_output, p_target_cname, p_link_target);
} else if (!p_target_itype || !p_target_itype->is_object_type) {
// Search in @GlobalScope as a last resort if no class was specified
if (p_link_target_parts.size() == 1) {
_append_text_constant_in_global_scope(p_output, p_target_cname, p_link_target);
return;
}
if (OS::get_singleton()->is_stdout_verbose()) {
if (p_target_itype) {
OS::get_singleton()->print("Cannot resolve constant reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data());
} else {
OS::get_singleton()->print("Cannot resolve type from constant reference in documentation: %s\n", p_link_target.utf8().get_data());
}
}
// TODO Map what we can
_append_text_undeclared(p_output, p_link_target);
} else {
// Try to find the constant in the current class
if (p_target_itype->is_singleton_instance) {
// Constants and enums are declared in the static singleton class.
p_target_itype = &obj_types[p_target_itype->cname];
}
const ConstantInterface *target_iconst = find_constant_by_name(p_target_cname, p_target_itype->constants);