Skip to content

Commit ed1e963

Browse files
authored
Merge pull request #1490 from AThousandShips/arg_name_fix
Enforce `p_` prefixes for arguments in binds
2 parents 068e930 + 9e2771f commit ed1e963

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

binding_generator.py

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -756,17 +756,17 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
756756
result.append(method_signature)
757757

758758
# Move constructor.
759-
result.append(f"\t{class_name}({class_name} &&other);")
759+
result.append(f"\t{class_name}({class_name} &&p_other);")
760760

761761
# Special cases.
762762
if class_name == "String" or class_name == "StringName" or class_name == "NodePath":
763763
if class_name == "StringName":
764-
result.append(f"\t{class_name}(const char *from, bool p_static = false);")
764+
result.append(f"\t{class_name}(const char *p_from, bool p_static = false);")
765765
else:
766-
result.append(f"\t{class_name}(const char *from);")
767-
result.append(f"\t{class_name}(const wchar_t *from);")
768-
result.append(f"\t{class_name}(const char16_t *from);")
769-
result.append(f"\t{class_name}(const char32_t *from);")
766+
result.append(f"\t{class_name}(const char *p_from);")
767+
result.append(f"\t{class_name}(const wchar_t *p_from);")
768+
result.append(f"\t{class_name}(const char16_t *p_from);")
769+
result.append(f"\t{class_name}(const char32_t *p_from);")
770770
if class_name == "Callable":
771771
result.append("\tCallable(CallableCustom *p_custom);")
772772
result.append("\tCallableCustom *get_custom() const;")
@@ -827,10 +827,12 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
827827

828828
# Special cases.
829829
if class_name == "String":
830-
result.append("\tstatic String utf8(const char *from, int64_t len = -1);")
831-
result.append("\tError parse_utf8(const char *from, int64_t len = -1);")
832-
result.append("\tstatic String utf16(const char16_t *from, int64_t len = -1);")
833-
result.append("\tError parse_utf16(const char16_t *from, int64_t len = -1, bool default_little_endian = true);")
830+
result.append("\tstatic String utf8(const char *p_from, int64_t p_len = -1);")
831+
result.append("\tError parse_utf8(const char *p_from, int64_t p_len = -1);")
832+
result.append("\tstatic String utf16(const char16_t *p_from, int64_t p_len = -1);")
833+
result.append(
834+
"\tError parse_utf16(const char16_t *p_from, int64_t p_len = -1, bool p_default_little_endian = true);"
835+
)
834836
result.append("\tCharString utf8() const;")
835837
result.append("\tCharString ascii() const;")
836838
result.append("\tChar16String utf16() const;")
@@ -851,7 +853,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
851853
if operator["name"] not in ["in", "xor"]:
852854
if "right_type" in operator:
853855
result.append(
854-
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const;'
856+
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const;'
855857
)
856858
else:
857859
result.append(
@@ -860,10 +862,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
860862

861863
# Copy assignment.
862864
if copy_constructor_index >= 0:
863-
result.append(f"\t{class_name} &operator=(const {class_name} &other);")
865+
result.append(f"\t{class_name} &operator=(const {class_name} &p_other);")
864866

865867
# Move assignment.
866-
result.append(f"\t{class_name} &operator=({class_name} &&other);")
868+
result.append(f"\t{class_name} &operator=({class_name} &&p_other);")
867869

868870
# Special cases.
869871
if class_name == "String":
@@ -897,8 +899,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
897899

898900
if class_name == "Array":
899901
result.append("\ttemplate <typename... Args>")
900-
result.append("\tstatic Array make(Args... args) {")
901-
result.append("\t\treturn helpers::append_all(Array(), args...);")
902+
result.append("\tstatic Array make(Args... p_args) {")
903+
result.append("\t\treturn helpers::append_all(Array(), p_args...);")
902904
result.append("\t}")
903905

904906
if is_packed_array(class_name):
@@ -1193,13 +1195,13 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
11931195
result.append("")
11941196

11951197
# Move constructor.
1196-
result.append(f"{class_name}::{class_name}({class_name} &&other) {{")
1198+
result.append(f"{class_name}::{class_name}({class_name} &&p_other) {{")
11971199
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
11981200
result.append(
1199-
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);"
1201+
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
12001202
)
12011203
else:
1202-
result.append("\tstd::swap(opaque, other.opaque);")
1204+
result.append("\tstd::swap(opaque, p_other.opaque);")
12031205
result.append("}")
12041206
result.append("")
12051207

@@ -1290,7 +1292,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
12901292
if operator["name"] not in ["in", "xor"]:
12911293
if "right_type" in operator:
12921294
result.append(
1293-
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const {{'
1295+
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const {{'
12941296
)
12951297
(encode, arg_name) = get_encoded_arg("other", operator["right_type"], None)
12961298
result += encode
@@ -1310,7 +1312,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
13101312

13111313
# Copy assignment.
13121314
if copy_constructor_index >= 0:
1313-
result.append(f"{class_name} &{class_name}::operator=(const {class_name} &other) {{")
1315+
result.append(f"{class_name} &{class_name}::operator=(const {class_name} &p_other) {{")
13141316
if builtin_api["has_destructor"]:
13151317
result.append("\t_method_bindings.destructor(&opaque);")
13161318
(encode, arg_name) = get_encoded_arg(
@@ -1327,13 +1329,13 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
13271329
result.append("")
13281330

13291331
# Move assignment.
1330-
result.append(f"{class_name} &{class_name}::operator=({class_name} &&other) {{")
1332+
result.append(f"{class_name} &{class_name}::operator=({class_name} &&p_other) {{")
13311333
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
13321334
result.append(
1333-
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);"
1335+
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
13341336
)
13351337
else:
1336-
result.append("\tstd::swap(opaque, other.opaque);")
1338+
result.append("\tstd::swap(opaque, p_other.opaque);")
13371339
result.append("\treturn *this;")
13381340
result.append("}")
13391341

@@ -1809,9 +1811,9 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
18091811
if "alias_for" in class_api and return_type.startswith(class_api["alias_for"] + "::"):
18101812
method_body += f"({return_type})"
18111813
method_body += f'ClassDBSingleton::get_singleton()->{method["name"]}('
1812-
method_body += ", ".join(map(lambda x: escape_identifier(x["name"]), method_arguments))
1814+
method_body += ", ".join(map(lambda x: escape_argument(x["name"]), method_arguments))
18131815
if vararg:
1814-
method_body += ", args..."
1816+
method_body += ", p_args..."
18151817
method_body += "); \\"
18161818

18171819
result.append(method_body)
@@ -1973,7 +1975,7 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
19731975
else: # vararg.
19741976
result.append("\tGDExtensionCallError error;")
19751977
result.append("\tVariant ret;")
1976-
method_call += "internal::gdextension_interface_object_method_bind_call(_gde_method_bind, _owner, reinterpret_cast<GDExtensionConstVariantPtr *>(args), arg_count, &ret, &error"
1978+
method_call += "internal::gdextension_interface_object_method_bind_call(_gde_method_bind, _owner, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count, &ret, &error"
19771979

19781980
if is_ref:
19791981
method_call += ")" # Close Ref<> constructor.
@@ -2242,7 +2244,7 @@ def generate_utility_functions(api, output_dir):
22422244
source.append(f'\t{get_gdextension_type(correct_type(function["return_type"]))} ret;')
22432245
else:
22442246
source.append("\tVariant ret;")
2245-
function_call += "_gde_function(&ret, reinterpret_cast<GDExtensionConstVariantPtr *>(args), arg_count"
2247+
function_call += "_gde_function(&ret, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count"
22462248

22472249
function_call += ");"
22482250
source.append(function_call)
@@ -2273,9 +2275,9 @@ def make_function_parameters(parameters, include_default=False, for_builtin=Fals
22732275

22742276
for index, par in enumerate(parameters):
22752277
parameter = type_for_parameter(par["type"], par["meta"] if "meta" in par else None)
2276-
parameter_name = escape_identifier(par["name"])
2278+
parameter_name = escape_argument(par["name"])
22772279
if len(parameter_name) == 0:
2278-
parameter_name = "arg_" + str(index + 1)
2280+
parameter_name = "p_arg_" + str(index + 1)
22792281
parameter += parameter_name
22802282

22812283
if include_default and "default_value" in par and (not for_builtin or par["type"] != "Variant"):
@@ -2289,7 +2291,7 @@ def make_function_parameters(parameters, include_default=False, for_builtin=Fals
22892291
signature.append(parameter)
22902292

22912293
if is_vararg:
2292-
signature.append("const Args&... args")
2294+
signature.append("const Args&... p_args")
22932295

22942296
return ", ".join(signature)
22952297

@@ -2320,7 +2322,7 @@ def get_include_path(type_name):
23202322
def get_encoded_arg(arg_name, type_name, type_meta):
23212323
result = []
23222324

2323-
name = escape_identifier(arg_name)
2325+
name = escape_argument(arg_name)
23242326
arg_type = correct_type(type_name)
23252327
if is_pod_type(arg_type):
23262328
result.append(f"\t{get_gdextension_type(arg_type)} {name}_encoded;")
@@ -2386,7 +2388,7 @@ def make_signature(
23862388
if not is_vararg:
23872389
function_signature += make_function_parameters(arguments, for_header, for_builtin, is_vararg)
23882390
else:
2389-
function_signature += "const Variant **args, GDExtensionInt arg_count"
2391+
function_signature += "const Variant **p_args, GDExtensionInt p_arg_count"
23902392

23912393
function_signature += ")"
23922394

@@ -2459,12 +2461,12 @@ def make_varargs_template(
24592461
args_array = f"\tstd::array<Variant, {len(method_arguments)} + sizeof...(Args)> variant_args {{ "
24602462
for argument in method_arguments:
24612463
if argument["type"] == "Variant":
2462-
args_array += escape_identifier(argument["name"])
2464+
args_array += escape_argument(argument["name"])
24632465
else:
2464-
args_array += f'Variant({escape_identifier(argument["name"])})'
2466+
args_array += f'Variant({escape_argument(argument["name"])})'
24652467
args_array += ", "
24662468

2467-
args_array += "Variant(args)... };"
2469+
args_array += "Variant(p_args)... };"
24682470
result.append(args_array)
24692471
result.append(f"\tstd::array<const Variant *, {len(method_arguments)} + sizeof...(Args)> call_args;")
24702472
result.append("\tfor(size_t i = 0; i < variant_args.size(); i++) {")
@@ -2782,6 +2784,12 @@ def escape_identifier(id):
27822784
return id
27832785

27842786

2787+
def escape_argument(id):
2788+
if id.startswith("p_") or id.startswith("r_"):
2789+
return id
2790+
return "p_" + id
2791+
2792+
27852793
def get_operator_id_name(op):
27862794
op_id_map = {
27872795
"==": "equal",

0 commit comments

Comments
 (0)