Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce p_ prefixes for arguments in binds #1490

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 43 additions & 35 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,17 +661,17 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append(method_signature)

# Move constructor.
result.append(f"\t{class_name}({class_name} &&other);")
result.append(f"\t{class_name}({class_name} &&p_other);")
AThousandShips marked this conversation as resolved.
Show resolved Hide resolved

# Special cases.
if class_name == "String" or class_name == "StringName" or class_name == "NodePath":
if class_name == "StringName":
result.append(f"\t{class_name}(const char *from, bool p_static = false);")
result.append(f"\t{class_name}(const char *p_from, bool p_static = false);")
else:
result.append(f"\t{class_name}(const char *from);")
result.append(f"\t{class_name}(const wchar_t *from);")
result.append(f"\t{class_name}(const char16_t *from);")
result.append(f"\t{class_name}(const char32_t *from);")
result.append(f"\t{class_name}(const char *p_from);")
result.append(f"\t{class_name}(const wchar_t *p_from);")
result.append(f"\t{class_name}(const char16_t *p_from);")
result.append(f"\t{class_name}(const char32_t *p_from);")
if class_name == "Callable":
result.append("\tCallable(CallableCustom *p_custom);")
result.append("\tCallableCustom *get_custom() const;")
Expand Down Expand Up @@ -732,10 +732,12 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl

# Special cases.
if class_name == "String":
result.append("\tstatic String utf8(const char *from, int64_t len = -1);")
result.append("\tError parse_utf8(const char *from, int64_t len = -1);")
result.append("\tstatic String utf16(const char16_t *from, int64_t len = -1);")
result.append("\tError parse_utf16(const char16_t *from, int64_t len = -1, bool default_little_endian = true);")
result.append("\tstatic String utf8(const char *p_from, int64_t p_len = -1);")
result.append("\tError parse_utf8(const char *p_from, int64_t p_len = -1);")
result.append("\tstatic String utf16(const char16_t *p_from, int64_t p_len = -1);")
result.append(
"\tError parse_utf16(const char16_t *p_from, int64_t p_len = -1, bool p_default_little_endian = true);"
)
result.append("\tCharString utf8() const;")
result.append("\tCharString ascii() const;")
result.append("\tChar16String utf16() const;")
Expand All @@ -756,7 +758,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
if operator["name"] not in ["in", "xor"]:
if "right_type" in operator:
result.append(
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const;'
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const;'
)
else:
result.append(
Expand All @@ -765,10 +767,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl

# Copy assignment.
if copy_constructor_index >= 0:
result.append(f"\t{class_name} &operator=(const {class_name} &other);")
result.append(f"\t{class_name} &operator=(const {class_name} &p_other);")

# Move assignment.
result.append(f"\t{class_name} &operator=({class_name} &&other);")
result.append(f"\t{class_name} &operator=({class_name} &&p_other);")

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

if class_name == "Array":
result.append("\ttemplate <typename... Args>")
result.append("\tstatic Array make(Args... args) {")
result.append("\t\treturn helpers::append_all(Array(), args...);")
result.append("\tstatic Array make(Args... p_args) {")
result.append("\t\treturn helpers::append_all(Array(), p_args...);")
result.append("\t}")

if is_packed_array(class_name):
Expand Down Expand Up @@ -1098,13 +1100,13 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
result.append("")

# Move constructor.
result.append(f"{class_name}::{class_name}({class_name} &&other) {{")
result.append(f"{class_name}::{class_name}({class_name} &&p_other) {{")
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
result.append(
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);"
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
)
else:
result.append("\tstd::swap(opaque, other.opaque);")
result.append("\tstd::swap(opaque, p_other.opaque);")
result.append("}")
result.append("")

Expand Down Expand Up @@ -1195,7 +1197,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
if operator["name"] not in ["in", "xor"]:
if "right_type" in operator:
result.append(
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}other) const {{'
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const {{'
)
(encode, arg_name) = get_encoded_arg("other", operator["right_type"], None)
result += encode
Expand All @@ -1215,7 +1217,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl

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

# Move assignment.
result.append(f"{class_name} &{class_name}::operator=({class_name} &&other) {{")
result.append(f"{class_name} &{class_name}::operator=({class_name} &&p_other) {{")
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
result.append(
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);"
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
)
else:
result.append("\tstd::swap(opaque, other.opaque);")
result.append("\tstd::swap(opaque, p_other.opaque);")
result.append("\treturn *this;")
result.append("}")

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

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

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

function_call += ");"
source.append(function_call)
Expand Down Expand Up @@ -2178,9 +2180,9 @@ def make_function_parameters(parameters, include_default=False, for_builtin=Fals

for index, par in enumerate(parameters):
parameter = type_for_parameter(par["type"], par["meta"] if "meta" in par else None)
parameter_name = escape_identifier(par["name"])
parameter_name = escape_argument(par["name"])
if len(parameter_name) == 0:
parameter_name = "arg_" + str(index + 1)
parameter_name = "p_arg_" + str(index + 1)
parameter += parameter_name

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

if is_vararg:
signature.append("const Args&... args")
signature.append("const Args&... p_args")

return ", ".join(signature)

Expand Down Expand Up @@ -2225,7 +2227,7 @@ def get_include_path(type_name):
def get_encoded_arg(arg_name, type_name, type_meta):
result = []

name = escape_identifier(arg_name)
name = escape_argument(arg_name)
arg_type = correct_type(type_name)
if is_pod_type(arg_type):
result.append(f"\t{get_gdextension_type(arg_type)} {name}_encoded;")
Expand Down Expand Up @@ -2291,7 +2293,7 @@ def make_signature(
if not is_vararg:
function_signature += make_function_parameters(arguments, for_header, for_builtin, is_vararg)
else:
function_signature += "const Variant **args, GDExtensionInt arg_count"
function_signature += "const Variant **p_args, GDExtensionInt p_arg_count"

function_signature += ")"

Expand Down Expand Up @@ -2364,12 +2366,12 @@ def make_varargs_template(
args_array = f"\tstd::array<Variant, {len(method_arguments)} + sizeof...(Args)> variant_args {{ "
for argument in method_arguments:
if argument["type"] == "Variant":
args_array += escape_identifier(argument["name"])
args_array += escape_argument(argument["name"])
else:
args_array += f'Variant({escape_identifier(argument["name"])})'
args_array += f'Variant({escape_argument(argument["name"])})'
args_array += ", "

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


def escape_argument(id):
if id.startswith("p_") or id.startswith("r_"):
return id
return "p_" + id


def get_operator_id_name(op):
op_id_map = {
"==": "equal",
Expand Down
Loading