Skip to content

Commit 265412c

Browse files
authored
Merge pull request godotengine#1557 from mihe/cpp-operators
Fix incorrect generation of some C++ operators
2 parents 6230594 + 9949d09 commit 265412c

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

binding_generator.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -872,14 +872,14 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
872872

873873
if "operators" in builtin_api:
874874
for operator in builtin_api["operators"]:
875-
if operator["name"] not in ["in", "xor"]:
875+
if is_valid_cpp_operator(operator["name"]):
876876
if "right_type" in operator:
877877
result.append(
878-
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const;'
878+
f'\t{correct_type(operator["return_type"])} operator{get_operator_cpp_name(operator["name"])}({type_for_parameter(operator["right_type"])}p_other) const;'
879879
)
880880
else:
881881
result.append(
882-
f'\t{correct_type(operator["return_type"])} operator{operator["name"].replace("unary", "")}() const;'
882+
f'\t{correct_type(operator["return_type"])} operator{get_operator_cpp_name(operator["name"])}() const;'
883883
)
884884

885885
# Copy assignment.
@@ -1316,10 +1316,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
13161316

13171317
if "operators" in builtin_api:
13181318
for operator in builtin_api["operators"]:
1319-
if operator["name"] not in ["in", "xor"]:
1319+
if is_valid_cpp_operator(operator["name"]):
13201320
if "right_type" in operator:
13211321
result.append(
1322-
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const {{'
1322+
f'{correct_type(operator["return_type"])} {class_name}::operator{get_operator_cpp_name(operator["name"])}({type_for_parameter(operator["right_type"])}p_other) const {{'
13231323
)
13241324
(encode, arg_name) = get_encoded_arg("other", operator["right_type"], None)
13251325
result += encode
@@ -1329,7 +1329,7 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
13291329
result.append("}")
13301330
else:
13311331
result.append(
1332-
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"].replace("unary", "")}() const {{'
1332+
f'{correct_type(operator["return_type"])} {class_name}::operator{get_operator_cpp_name(operator["name"])}() const {{'
13331333
)
13341334
result.append(
13351335
f'\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator["return_type"]))}>(_method_bindings.operator_{get_operator_id_name(operator["name"])}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr) nullptr);'
@@ -2881,6 +2881,38 @@ def get_operator_id_name(op):
28812881
return op_id_map[op]
28822882

28832883

2884+
def get_operator_cpp_name(op):
2885+
op_cpp_map = {
2886+
"==": "==",
2887+
"!=": "!=",
2888+
"<": "<",
2889+
"<=": "<=",
2890+
">": ">",
2891+
">=": ">=",
2892+
"+": "+",
2893+
"-": "-",
2894+
"*": "*",
2895+
"/": "/",
2896+
"unary-": "-",
2897+
"unary+": "+",
2898+
"%": "%",
2899+
"<<": "<<",
2900+
">>": ">>",
2901+
"&": "&",
2902+
"|": "|",
2903+
"^": "^",
2904+
"~": "~",
2905+
"and": "&&",
2906+
"or": "||",
2907+
"not": "!",
2908+
}
2909+
return op_cpp_map[op]
2910+
2911+
2912+
def is_valid_cpp_operator(op):
2913+
return op not in ["**", "xor", "in"]
2914+
2915+
28842916
def get_default_value_for_type(type_name):
28852917
if type_name == "int":
28862918
return "0"

0 commit comments

Comments
 (0)