summaryrefslogtreecommitdiff
path: root/modules/gdscript/gdscript_parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdscript/gdscript_parser.cpp')
-rw-r--r--modules/gdscript/gdscript_parser.cpp101
1 files changed, 59 insertions, 42 deletions
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp
index 112569a8d6..5c2e7137bf 100644
--- a/modules/gdscript/gdscript_parser.cpp
+++ b/modules/gdscript/gdscript_parser.cpp
@@ -2689,6 +2689,7 @@ void GDScriptParser::_transform_match_statment(MatchNode *p_match_statement) {
op->op = OperatorNode::OP_ASSIGN;
op->arguments.push_back(id2);
op->arguments.push_back(local_var->assign);
+ local_var->assign_op = op;
branch->body->statements.push_front(op);
branch->body->statements.push_front(local_var);
@@ -2866,7 +2867,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
assigned = _get_default_value_for_type(lv->datatype, var_line);
}
- lv->assign = assigned;
//must be added later, to avoid self-referencing.
p_block->variables.insert(n, lv);
@@ -4741,10 +4741,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
member.line = tokenizer->get_token_line();
member.usages = 0;
member.rpc_mode = rpc_mode;
-#ifdef TOOLS_ENABLED
- Variant::CallError ce;
- member.default_value = Variant::construct(member._export.type, NULL, 0, ce);
-#endif
if (current_class->constant_expressions.has(member.identifier)) {
_set_error("A constant named \"" + String(member.identifier) + "\" already exists in this class (at line: " +
@@ -4797,6 +4793,32 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
}
+ if (autoexport && member.data_type.has_type) {
+ if (member.data_type.kind == DataType::BUILTIN) {
+ member._export.type = member.data_type.builtin_type;
+ } else if (member.data_type.kind == DataType::NATIVE) {
+ if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) {
+ member._export.type = Variant::OBJECT;
+ member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
+ member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
+ member._export.hint_string = member.data_type.native_type;
+ member._export.class_name = member.data_type.native_type;
+ } else {
+ _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
+ return;
+ }
+
+ } else {
+ _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
+ return;
+ }
+ }
+
+#ifdef TOOLS_ENABLED
+ Variant::CallError ce;
+ member.default_value = Variant::construct(member._export.type, NULL, 0, ce);
+#endif
+
if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
#ifdef DEBUG_ENABLED
@@ -4930,27 +4952,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
member.initial_assignment = op;
}
- if (autoexport && member.data_type.has_type) {
- if (member.data_type.kind == DataType::BUILTIN) {
- member._export.type = member.data_type.builtin_type;
- } else if (member.data_type.kind == DataType::NATIVE) {
- if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) {
- member._export.type = Variant::OBJECT;
- member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
- member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
- member._export.hint_string = member.data_type.native_type;
- member._export.class_name = member.data_type.native_type;
- } else {
- _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
- return;
- }
-
- } else {
- _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
- return;
- }
- }
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
tokenizer->advance();
@@ -6965,6 +6966,17 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat
if (error_set) return DataType();
+ // Special case: check copy constructor. Those are defined implicitly in Variant.
+ if (par_types.size() == 1) {
+ if (!par_types[0].has_type || (par_types[0].kind == DataType::BUILTIN && par_types[0].builtin_type == tn->vtype)) {
+ DataType result;
+ result.has_type = true;
+ result.kind = DataType::BUILTIN;
+ result.builtin_type = tn->vtype;
+ return result;
+ }
+ }
+
bool match = false;
List<MethodInfo> constructors;
Variant::get_constructor_list(tn->vtype, &constructors);
@@ -7035,12 +7047,10 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat
return_type = _type_from_property(mi.return_val, false);
-#ifdef DEBUG_ENABLED
// Check all arguments beforehand to solve warnings
for (int i = 1; i < p_call->arguments.size(); i++) {
_reduce_node_type(p_call->arguments[i]);
}
-#endif // DEBUG_ENABLED
// Check arguments
@@ -7068,12 +7078,10 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat
ERR_FAIL_V(DataType());
}
-#ifdef DEBUG_ENABLED
// Check all arguments beforehand to solve warnings
for (int i = arg_id + 1; i < p_call->arguments.size(); i++) {
_reduce_node_type(p_call->arguments[i]);
}
-#endif // DEBUG_ENABLED
IdentifierNode *func_id = static_cast<IdentifierNode *>(p_call->arguments[arg_id]);
callee_name = func_id->name;
@@ -7637,6 +7645,11 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType
void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
+ // Names of internal object properties that we check to avoid overriding them.
+ // "__meta__" could also be in here, but since it doesn't really affect object metadata,
+ // it is okay to override it on script.
+ StringName script_name = CoreStringNames::get_singleton()->_script;
+
_mark_line_as_safe(p_class->line);
// Constants
@@ -7657,8 +7670,9 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
c.expression->set_datatype(expr);
DataType tmp;
- if (_get_member_type(p_class->base_type, E->key(), tmp)) {
- _set_error("The member \"" + String(E->key()) + "\" already exists in a parent class.", c.expression->line);
+ const StringName &constant_name = E->key();
+ if (constant_name == script_name || _get_member_type(p_class->base_type, constant_name, tmp)) {
+ _set_error("The member \"" + String(constant_name) + "\" already exists in a parent class.", c.expression->line);
return;
}
}
@@ -7679,7 +7693,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
ClassNode::Member &v = p_class->variables.write[i];
DataType tmp;
- if (_get_member_type(p_class->base_type, v.identifier, tmp)) {
+ if (v.identifier == script_name || _get_member_type(p_class->base_type, v.identifier, tmp)) {
_set_error("The member \"" + String(v.identifier) + "\" already exists in a parent class.", v.line);
return;
}
@@ -7856,12 +7870,12 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) {
def_type.is_constant = false;
p_function->argument_types.write[i] = def_type;
} else {
- p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
+ p_function->argument_types.write[i] = _resolve_type(p_function->argument_types[i], p_function->line);
if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) {
String arg_name = p_function->arguments[i];
_set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" +
- arg_name + "' (" + p_function->arguments[i] + ").",
+ arg_name + "' (" + p_function->argument_types[i].to_string() + ").",
p_function->line);
}
}
@@ -8047,6 +8061,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
last_var_assign = lv->assign;
if (lv->assign) {
+ lv->assign_op->arguments[0]->set_datatype(lv->datatype);
DataType assign_type = _reduce_node_type(lv->assign);
#ifdef DEBUG_ENABLED
if (assign_type.has_type && assign_type.kind == DataType::BUILTIN && assign_type.builtin_type == Variant::NIL) {
@@ -8191,11 +8206,12 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
if (lh_type.has_type && rh_type.may_yield && op->arguments[1]->type == Node::TYPE_OPERATOR) {
_add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, op->line, _find_function_name(static_cast<OperatorNode *>(op->arguments[1])));
}
-#endif // DEBUG_ENABLED
- bool type_match = check_types;
+#endif // DEBUG_ENABLED
+ bool type_match = lh_type.has_type && rh_type.has_type;
if (check_types && !_is_type_compatible(lh_type, rh_type)) {
type_match = false;
+
// Try supertype test
if (_is_type_compatible(rh_type, lh_type)) {
_mark_line_as_unsafe(op->line);
@@ -8266,7 +8282,11 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
_mark_line_as_safe(op->line);
_reduce_node_type(op); // Test for safety anyway
#ifdef DEBUG_ENABLED
- _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
+ if (op->op == OperatorNode::OP_TERNARY_IF) {
+ _add_warning(GDScriptWarning::STANDALONE_TERNARY, statement->line);
+ } else {
+ _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line);
+ }
#endif // DEBUG_ENABLED
}
}
@@ -8483,11 +8503,8 @@ Error GDScriptParser::_parse(const String &p_base_path) {
current_class = main_class;
current_function = NULL;
current_block = NULL;
-#ifdef DEBUG_ENABLED
+
if (for_completion) check_types = false;
-#else
- check_types = false;
-#endif
// Resolve all class-level stuff before getting into function blocks
_check_class_level_types(main_class);