Skip to content

Commit 9e2771f

Browse files
Enforce p_ prefixes for arguments in binds
1 parent ee9acbc commit 9e2771f

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

binding_generator.py

+43-35
Original file line numberDiff line numberDiff line change
@@ -661,17 +661,17 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
661661
result.append(method_signature)
662662

663663
# Move constructor.
664-
result.append(f"\t{class_name}({class_name} &&other);")
664+
result.append(f"\t{class_name}({class_name} &&p_other);")
665665

666666
# Special cases.
667667
if class_name == "String" or class_name == "StringName" or class_name == "NodePath":
668668
if class_name == "StringName":
669-
result.append(f"\t{class_name}(const char *from, bool p_static = false);")
669+
result.append(f"\t{class_name}(const char *p_from, bool p_static = false);")
670670
else:
671-
result.append(f"\t{class_name}(const char *from);")
672-
result.append(f"\t{class_name}(const wchar_t *from);")
673-
result.append(f"\t{class_name}(const char16_t *from);")
674-
result.append(f"\t{class_name}(const char32_t *from);")
671+
result.append(f"\t{class_name}(const char *p_from);")
672+
result.append(f"\t{class_name}(const wchar_t *p_from);")
673+
result.append(f"\t{class_name}(const char16_t *p_from);")
674+
result.append(f"\t{class_name}(const char32_t *p_from);")
675675
if class_name == "Callable":
676676
result.append("\tCallable(CallableCustom *p_custom);")
677677
result.append("\tCallableCustom *get_custom() const;")
@@ -732,10 +732,12 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
732732

733733
# Special cases.
734734
if class_name == "String":
735-
result.append("\tstatic String utf8(const char *from, int64_t len = -1);")
736-
result.append("\tError parse_utf8(const char *from, int64_t len = -1);")
737-
result.append("\tstatic String utf16(const char16_t *from, int64_t len = -1);")
738-
result.append("\tError parse_utf16(const char16_t *from, int64_t len = -1, bool default_little_endian = true);")
735+
result.append("\tstatic String utf8(const char *p_from, int64_t p_len = -1);")
736+
result.append("\tError parse_utf8(const char *p_from, int64_t p_len = -1);")
737+
result.append("\tstatic String utf16(const char16_t *p_from, int64_t p_len = -1);")
738+
result.append(
739+
"\tError parse_utf16(const char16_t *p_from, int64_t p_len = -1, bool p_default_little_endian = true);"
740+
)
739741
result.append("\tCharString utf8() const;")
740742
result.append("\tCharString ascii() const;")
741743
result.append("\tChar16String utf16() const;")
@@ -756,7 +758,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
756758
if operator["name"] not in ["in", "xor"]:
757759
if "right_type" in operator:
758760
result.append(
759-
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const;'
761+
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const;'
760762
)
761763
else:
762764
result.append(
@@ -765,10 +767,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
765767

766768
# Copy assignment.
767769
if copy_constructor_index >= 0:
768-
result.append(f"\t{class_name} &operator=(const {class_name} &other);")
770+
result.append(f"\t{class_name} &operator=(const {class_name} &p_other);")
769771

770772
# Move assignment.
771-
result.append(f"\t{class_name} &operator=({class_name} &&other);")
773+
result.append(f"\t{class_name} &operator=({class_name} &&p_other);")
772774

773775
# Special cases.
774776
if class_name == "String":
@@ -802,8 +804,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
802804

803805
if class_name == "Array":
804806
result.append("\ttemplate <typename... Args>")
805-
result.append("\tstatic Array make(Args... args) {")
806-
result.append("\t\treturn helpers::append_all(Array(), args...);")
807+
result.append("\tstatic Array make(Args... p_args) {")
808+
result.append("\t\treturn helpers::append_all(Array(), p_args...);")
807809
result.append("\t}")
808810

809811
if is_packed_array(class_name):
@@ -1098,13 +1100,13 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
10981100
result.append("")
10991101

11001102
# Move constructor.
1101-
result.append(f"{class_name}::{class_name}({class_name} &&other) {{")
1103+
result.append(f"{class_name}::{class_name}({class_name} &&p_other) {{")
11021104
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
11031105
result.append(
1104-
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);"
1106+
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
11051107
)
11061108
else:
1107-
result.append("\tstd::swap(opaque, other.opaque);")
1109+
result.append("\tstd::swap(opaque, p_other.opaque);")
11081110
result.append("}")
11091111
result.append("")
11101112

@@ -1195,7 +1197,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
11951197
if operator["name"] not in ["in", "xor"]:
11961198
if "right_type" in operator:
11971199
result.append(
1198-
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const {{'
1200+
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const {{'
11991201
)
12001202
(encode, arg_name) = get_encoded_arg("other", operator["right_type"], None)
12011203
result += encode
@@ -1215,7 +1217,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
12151217

12161218
# Copy assignment.
12171219
if copy_constructor_index >= 0:
1218-
result.append(f"{class_name} &{class_name}::operator=(const {class_name} &other) {{")
1220+
result.append(f"{class_name} &{class_name}::operator=(const {class_name} &p_other) {{")
12191221
if builtin_api["has_destructor"]:
12201222
result.append("\t_method_bindings.destructor(&opaque);")
12211223
(encode, arg_name) = get_encoded_arg(
@@ -1232,13 +1234,13 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
12321234
result.append("")
12331235

12341236
# Move assignment.
1235-
result.append(f"{class_name} &{class_name}::operator=({class_name} &&other) {{")
1237+
result.append(f"{class_name} &{class_name}::operator=({class_name} &&p_other) {{")
12361238
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
12371239
result.append(
1238-
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);"
1240+
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
12391241
)
12401242
else:
1241-
result.append("\tstd::swap(opaque, other.opaque);")
1243+
result.append("\tstd::swap(opaque, p_other.opaque);")
12421244
result.append("\treturn *this;")
12431245
result.append("}")
12441246

@@ -1714,9 +1716,9 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
17141716
if "alias_for" in class_api and return_type.startswith(class_api["alias_for"] + "::"):
17151717
method_body += f"({return_type})"
17161718
method_body += f'ClassDBSingleton::get_singleton()->{method["name"]}('
1717-
method_body += ", ".join(map(lambda x: escape_identifier(x["name"]), method_arguments))
1719+
method_body += ", ".join(map(lambda x: escape_argument(x["name"]), method_arguments))
17181720
if vararg:
1719-
method_body += ", args..."
1721+
method_body += ", p_args..."
17201722
method_body += "); \\"
17211723

17221724
result.append(method_body)
@@ -1878,7 +1880,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
18781880
else: # vararg.
18791881
result.append("\tGDExtensionCallError error;")
18801882
result.append("\tVariant ret;")
1881-
method_call += "internal::gdextension_interface_object_method_bind_call(_gde_method_bind, _owner, reinterpret_cast<GDExtensionConstVariantPtr *>(args), arg_count, &ret, &error"
1883+
method_call += "internal::gdextension_interface_object_method_bind_call(_gde_method_bind, _owner, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count, &ret, &error"
18821884

18831885
if is_ref:
18841886
method_call += ")" # Close Ref<> constructor.
@@ -2147,7 +2149,7 @@ def generate_utility_functions(api, output_dir):
21472149
source.append(f'\t{get_gdextension_type(correct_type(function["return_type"]))} ret;')
21482150
else:
21492151
source.append("\tVariant ret;")
2150-
function_call += "_gde_function(&ret, reinterpret_cast<GDExtensionConstVariantPtr *>(args), arg_count"
2152+
function_call += "_gde_function(&ret, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count"
21512153

21522154
function_call += ");"
21532155
source.append(function_call)
@@ -2178,9 +2180,9 @@ def make_function_parameters(parameters, include_default=False, for_builtin=Fals
21782180

21792181
for index, par in enumerate(parameters):
21802182
parameter = type_for_parameter(par["type"], par["meta"] if "meta" in par else None)
2181-
parameter_name = escape_identifier(par["name"])
2183+
parameter_name = escape_argument(par["name"])
21822184
if len(parameter_name) == 0:
2183-
parameter_name = "arg_" + str(index + 1)
2185+
parameter_name = "p_arg_" + str(index + 1)
21842186
parameter += parameter_name
21852187

21862188
if include_default and "default_value" in par and (not for_builtin or par["type"] != "Variant"):
@@ -2194,7 +2196,7 @@ def make_function_parameters(parameters, include_default=False, for_builtin=Fals
21942196
signature.append(parameter)
21952197

21962198
if is_vararg:
2197-
signature.append("const Args&... args")
2199+
signature.append("const Args&... p_args")
21982200

21992201
return ", ".join(signature)
22002202

@@ -2225,7 +2227,7 @@ def get_include_path(type_name):
22252227
def get_encoded_arg(arg_name, type_name, type_meta):
22262228
result = []
22272229

2228-
name = escape_identifier(arg_name)
2230+
name = escape_argument(arg_name)
22292231
arg_type = correct_type(type_name)
22302232
if is_pod_type(arg_type):
22312233
result.append(f"\t{get_gdextension_type(arg_type)} {name}_encoded;")
@@ -2291,7 +2293,7 @@ def make_signature(
22912293
if not is_vararg:
22922294
function_signature += make_function_parameters(arguments, for_header, for_builtin, is_vararg)
22932295
else:
2294-
function_signature += "const Variant **args, GDExtensionInt arg_count"
2296+
function_signature += "const Variant **p_args, GDExtensionInt p_arg_count"
22952297

22962298
function_signature += ")"
22972299

@@ -2364,12 +2366,12 @@ def make_varargs_template(
23642366
args_array = f"\tstd::array<Variant, {len(method_arguments)} + sizeof...(Args)> variant_args {{ "
23652367
for argument in method_arguments:
23662368
if argument["type"] == "Variant":
2367-
args_array += escape_identifier(argument["name"])
2369+
args_array += escape_argument(argument["name"])
23682370
else:
2369-
args_array += f'Variant({escape_identifier(argument["name"])})'
2371+
args_array += f'Variant({escape_argument(argument["name"])})'
23702372
args_array += ", "
23712373

2372-
args_array += "Variant(args)... };"
2374+
args_array += "Variant(p_args)... };"
23732375
result.append(args_array)
23742376
result.append(f"\tstd::array<const Variant *, {len(method_arguments)} + sizeof...(Args)> call_args;")
23752377
result.append("\tfor(size_t i = 0; i < variant_args.size(); i++) {")
@@ -2673,6 +2675,12 @@ def escape_identifier(id):
26732675
return id
26742676

26752677

2678+
def escape_argument(id):
2679+
if id.startswith("p_") or id.startswith("r_"):
2680+
return id
2681+
return "p_" + id
2682+
2683+
26762684
def get_operator_id_name(op):
26772685
op_id_map = {
26782686
"==": "equal",

0 commit comments

Comments
 (0)