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.cpp624
1 files changed, 231 insertions, 393 deletions
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp
index b03d041aaf..fbb5f91139 100644
--- a/modules/gdscript/gdscript_parser.cpp
+++ b/modules/gdscript/gdscript_parser.cpp
@@ -42,14 +42,14 @@
template <class T>
T *GDScriptParser::alloc_node() {
-
T *t = memnew(T);
t->next = list;
list = t;
- if (!head)
+ if (!head) {
head = t;
+ }
t->line = tokenizer->get_token_line();
t->column = tokenizer->get_token_column();
@@ -61,7 +61,6 @@ static String _find_function_name(const GDScriptParser::OperatorNode *p_call);
#endif // DEBUG_ENABLED
bool GDScriptParser::_end_statement() {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
tokenizer->advance();
return true; //handle next
@@ -83,7 +82,6 @@ void GDScriptParser::_set_end_statement_error(String p_name) {
}
bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
-
if (tokenizer->get_token() != GDScriptTokenizer::TK_COLON) {
// report location at the previous token (on the previous line)
int error_line = tokenizer->get_token_line(-1);
@@ -108,12 +106,10 @@ bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
while (true) {
if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
-
return false; //wtf
} else if (tokenizer->get_token(1) == GDScriptTokenizer::TK_EOF) {
return false;
} else if (tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
-
int indent = tokenizer->get_token_line_indent();
int tabs = tokenizer->get_token_line_tab_indent();
IndentLevel current_level = indent_level.back()->get();
@@ -132,7 +128,6 @@ bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
return true;
} else if (p_block) {
-
NewLineNode *nl = alloc_node<NewLineNode>();
nl->line = tokenizer->get_token_line();
p_block->statements.push_back(nl);
@@ -143,16 +138,13 @@ bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
}
bool GDScriptParser::_parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete, bool p_parsing_constant) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
tokenizer->advance();
} else {
-
parenthesis++;
int argidx = 0;
while (true) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
_make_completable_call(argidx);
completion_node = p_parent;
@@ -178,9 +170,7 @@ bool GDScriptParser::_parse_arguments(Node *p_parent, Vector<Node *> &p_args, bo
break;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
-
if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
-
_set_error("Expression expected");
return false;
}
@@ -200,7 +190,6 @@ bool GDScriptParser::_parse_arguments(Node *p_parent, Vector<Node *> &p_args, bo
}
void GDScriptParser::_make_completable_call(int p_arg) {
-
completion_cursor = StringName();
completion_type = COMPLETION_CALL_ARGUMENTS;
completion_class = current_class;
@@ -213,14 +202,12 @@ void GDScriptParser::_make_completable_call(int p_arg) {
}
bool GDScriptParser::_get_completable_identifier(CompletionType p_type, StringName &identifier) {
-
identifier = StringName();
if (tokenizer->is_token_literal()) {
identifier = tokenizer->get_token_literal();
tokenizer->advance();
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
-
completion_cursor = identifier;
completion_type = p_type;
completion_class = current_class;
@@ -246,7 +233,6 @@ bool GDScriptParser::_get_completable_identifier(CompletionType p_type, StringNa
}
GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign, bool p_parsing_constant) {
-
//Vector<Node*> expressions;
//Vector<OperatorNode::Operator> operators;
@@ -257,7 +243,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found)
while (true) {
-
/*****************/
/* Parse Operand */
/*****************/
@@ -285,11 +270,11 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
parenthesis++;
Node *subexpr = _parse_expression(p_parent, p_static, p_allow_assign, p_parsing_constant);
parenthesis--;
- if (!subexpr)
+ if (!subexpr) {
return nullptr;
+ }
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
-
_set_error("Expected ')' in expression");
return nullptr;
}
@@ -306,7 +291,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
int line = tokenizer->get_token_line();
while (!done) {
-
switch (tokenizer->get_token()) {
case GDScriptTokenizer::TK_CURSOR: {
completion_type = COMPLETION_GET_NODE;
@@ -320,7 +304,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
} break;
case GDScriptTokenizer::TK_CONSTANT: {
-
if (!need_identifier) {
done = true;
break;
@@ -337,7 +320,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
} break;
case GDScriptTokenizer::TK_OP_DIV: {
-
if (need_identifier) {
done = true;
break;
@@ -392,7 +374,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
continue; //no point in cursor in the middle of expression
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
-
//constant defined by tokenizer
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = tokenizer->get_token_constant();
@@ -400,7 +381,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
expr = constant;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_PI) {
-
//constant defined by tokenizer
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = Math_PI;
@@ -408,7 +388,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
expr = constant;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_TAU) {
-
//constant defined by tokenizer
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = Math_TAU;
@@ -416,7 +395,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
expr = constant;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_INF) {
-
//constant defined by tokenizer
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = Math_INF;
@@ -424,7 +402,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
expr = constant;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_NAN) {
-
//constant defined by tokenizer
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = Math_NAN;
@@ -432,7 +409,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
expr = constant;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_PRELOAD) {
-
//constant defined by tokenizer
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
@@ -489,11 +465,11 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
return nullptr;
}
- if (!path.is_abs_path() && base_path != "")
+ if (!path.is_abs_path() && base_path != "") {
path = base_path.plus_file(path);
+ }
path = path.replace("///", "//").simplify_path();
if (path == self_path) {
-
_set_error("Can't preload itself (use 'get_script()').");
return nullptr;
}
@@ -502,7 +478,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
dependencies.push_back(path);
if (!dependencies_only) {
if (!validating) {
-
//this can be too slow for just validating code
if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
@@ -510,7 +485,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
res = ResourceLoader::load(path);
}
} else {
-
if (!FileAccess::exists(path)) {
_set_error("Can't preload resource at path: " + path);
return nullptr;
@@ -543,7 +517,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
expr = constant;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_YIELD) {
-
if (!current_function) {
_set_error("\"yield()\" can only be used inside function blocks.");
return nullptr;
@@ -570,12 +543,12 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
expr = yield;
tokenizer->advance();
} else {
-
parenthesis++;
Node *object = _parse_and_reduce_expression(p_parent, p_static);
- if (!object)
+ if (!object) {
return nullptr;
+ }
yield->arguments.push_back(object);
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
@@ -586,7 +559,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
-
completion_cursor = StringName();
completion_node = object;
completion_type = COMPLETION_YIELD;
@@ -600,8 +572,9 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
Node *signal = _parse_and_reduce_expression(p_parent, p_static);
- if (!signal)
+ if (!signal) {
return nullptr;
+ }
yield->arguments.push_back(signal);
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
@@ -617,7 +590,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_SELF) {
-
if (p_static) {
_set_error("\"self\" isn't allowed in a static function or constant expression.");
return nullptr;
@@ -627,28 +599,23 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
expr = self;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
-
Variant::Type bi_type = tokenizer->get_token_type();
tokenizer->advance(2);
StringName identifier;
if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT, identifier)) {
-
completion_built_in_constant = bi_type;
}
if (identifier == StringName()) {
-
_set_error("Built-in type constant or static function expected after \".\".");
return nullptr;
}
if (!Variant::has_constant(bi_type, identifier)) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN &&
Variant::is_method_const(bi_type, identifier) &&
Variant::get_method_return_type(bi_type, identifier) == bi_type) {
-
tokenizer->advance();
OperatorNode *construct = alloc_node<OperatorNode>();
@@ -666,8 +633,9 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
id->name = identifier;
op->arguments.push_back(id);
- if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant))
+ if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant)) {
return nullptr;
+ }
expr = op;
} else {
@@ -688,7 +656,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
}
} else {
-
ConstantNode *cn = alloc_node<ConstantNode>();
cn->value = Variant::get_constant_value(bi_type, identifier);
cn->datatype = _type_from_variant(cn->value);
@@ -733,13 +700,11 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance(2);
}
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) {
-
BuiltInFunctionNode *bn = alloc_node<BuiltInFunctionNode>();
bn->function = tokenizer->get_token_built_in_func();
op->arguments.push_back(bn);
tokenizer->advance(2);
} else {
-
SelfNode *self = alloc_node<SelfNode>();
op->arguments.push_back(self);
@@ -765,8 +730,9 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
}
if (!replaced) {
- if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant))
+ if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant)) {
return nullptr;
+ }
expr = op;
}
} else if (tokenizer->is_token_literal(0, true)) {
@@ -913,7 +879,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ADD || tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB || tokenizer->get_token() == GDScriptTokenizer::TK_OP_NOT || tokenizer->get_token() == GDScriptTokenizer::TK_OP_BIT_INVERT) {
-
//single prefix operators like !expr +expr -expr ++expr --expr
alloc_node<OperatorNode>();
Expression e;
@@ -979,9 +944,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
bool expecting_comma = false;
while (true) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
-
_set_error("Unterminated array");
return nullptr;
@@ -989,7 +952,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
break;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
-
tokenizer->advance(); //ignore newline
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
if (!expecting_comma) {
@@ -1006,8 +968,9 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
return nullptr;
}
Node *n = _parse_expression(arr, p_static, p_allow_assign, p_parsing_constant);
- if (!n)
+ if (!n) {
return nullptr;
+ }
arr->elements.push_back(n);
expecting_comma = true;
}
@@ -1035,14 +998,11 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
DictExpect expecting = DICT_EXPECT_KEY;
while (true) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
-
_set_error("Unterminated dictionary");
return nullptr;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
-
if (expecting == DICT_EXPECT_COLON) {
_set_error("':' expected");
return nullptr;
@@ -1054,10 +1014,8 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance();
break;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
-
tokenizer->advance(); //ignore newline
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
-
if (expecting == DICT_EXPECT_KEY) {
_set_error("key or '}' expected");
return nullptr;
@@ -1075,7 +1033,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance(); //ignore newline
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
-
if (expecting == DICT_EXPECT_KEY) {
_set_error("key or '}' expected");
return nullptr;
@@ -1092,7 +1049,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
expecting = DICT_EXPECT_VALUE;
tokenizer->advance(); //ignore newline
} else {
-
if (expecting == DICT_EXPECT_COMMA) {
_set_error("',' or '}' expected");
return nullptr;
@@ -1103,7 +1059,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
if (expecting == DICT_EXPECT_KEY) {
-
if (tokenizer->is_token_literal() && tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
// We check with is_token_literal, as this allows us to use match/sync/etc. as a name
//lua style identifier, easier to write
@@ -1116,16 +1071,18 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
} else {
//python/js style more flexible
key = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
- if (!key)
+ if (!key) {
return nullptr;
+ }
expecting = DICT_EXPECT_COLON;
}
}
if (expecting == DICT_EXPECT_VALUE) {
Node *value = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
- if (!value)
+ if (!value) {
return nullptr;
+ }
expecting = DICT_EXPECT_COMMA;
if (key->type == GDScriptParser::Node::TYPE_CONSTANT) {
@@ -1191,7 +1148,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
expr = tn;
tokenizer->advance();
} else {
-
//find list [ or find dictionary {
_set_error("Error parsing expression, misplaced: " + String(tokenizer->get_token_name(tokenizer->get_token())));
return nullptr; //nothing
@@ -1204,11 +1160,9 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
/******************/
while (true) {
-
//expressions can be indexed any number of times
if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
-
//indexing using "."
if (tokenizer->get_token(1) != GDScriptTokenizer::TK_CURSOR && !tokenizer->is_token_literal(1)) {
@@ -1239,8 +1193,9 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
_make_completable_call(0);
completion_node = op;
}
- if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant))
+ if (!_parse_arguments(op, op->arguments, p_static, true, p_parsing_constant)) {
return nullptr;
+ }
expr = op;
} else {
@@ -1252,7 +1207,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
StringName identifier;
if (_get_completable_identifier(COMPLETION_INDEX, identifier)) {
-
if (identifier == StringName()) {
identifier = "@temp"; //so it parses alright
}
@@ -1292,8 +1246,9 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
tokenizer->advance(1);
expr = op;
- } else
+ } else {
break;
+ }
}
/*****************/
@@ -1478,16 +1433,13 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
/* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
while (expression.size() > 1) {
-
int next_op = -1;
int min_priority = 0xFFFFF;
bool is_unary = false;
bool is_ternary = false;
for (int i = 0; i < expression.size(); i++) {
-
if (!expression[i].is_op) {
-
continue;
}
@@ -1499,7 +1451,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
bool right_to_left = false;
switch (expression[i].op) {
-
case OperatorNode::OP_IS:
case OperatorNode::OP_IS_BUILTIN:
priority = -1;
@@ -1650,17 +1601,14 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
if (next_op == -1) {
-
_set_error("Yet another parser bug....");
ERR_FAIL_V(nullptr);
}
// OK! create operator..
if (is_unary) {
-
int expr_pos = next_op;
while (expression[expr_pos].is_op) {
-
expr_pos++;
if (expr_pos == expression.size()) {
//can happen..
@@ -1671,7 +1619,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
//consecutively do unary operators
for (int i = expr_pos - 1; i >= next_op; i--) {
-
OperatorNode *op = alloc_node<OperatorNode>();
op->op = expression[i].op;
op->arguments.push_back(expression[i + 1].node);
@@ -1701,7 +1648,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
op->line = op_line; //line might have been changed from a \n
if (expression[next_op - 1].is_op) {
-
_set_error("Parser bug...");
ERR_FAIL_V(nullptr);
}
@@ -1737,7 +1683,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
expression.remove(next_op);
expression.remove(next_op);
} else {
-
if (next_op < 1 || next_op >= (expression.size() - 1)) {
_set_error("Parser bug...");
ERR_FAIL_V(nullptr);
@@ -1748,7 +1693,6 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
op->line = op_line; //line might have been changed from a \n
if (expression[next_op - 1].is_op) {
-
_set_error("Parser bug...");
ERR_FAIL_V(nullptr);
}
@@ -1777,23 +1721,20 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to_const) {
-
switch (p_node->type) {
-
case Node::TYPE_BUILT_IN_FUNCTION: {
//many may probably be optimizable
return p_node;
} break;
case Node::TYPE_ARRAY: {
-
ArrayNode *an = static_cast<ArrayNode *>(p_node);
bool all_constants = true;
for (int i = 0; i < an->elements.size(); i++) {
-
an->elements.write[i] = _reduce_expression(an->elements[i], p_to_const);
- if (an->elements[i]->type != Node::TYPE_CONSTANT)
+ if (an->elements[i]->type != Node::TYPE_CONSTANT) {
all_constants = false;
+ }
}
if (all_constants && p_to_const) {
@@ -1815,18 +1756,18 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
} break;
case Node::TYPE_DICTIONARY: {
-
DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
bool all_constants = true;
for (int i = 0; i < dn->elements.size(); i++) {
-
dn->elements.write[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
- if (dn->elements[i].key->type != Node::TYPE_CONSTANT)
+ if (dn->elements[i].key->type != Node::TYPE_CONSTANT) {
all_constants = false;
+ }
dn->elements.write[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
- if (dn->elements[i].value->type != Node::TYPE_CONSTANT)
+ if (dn->elements[i].value->type != Node::TYPE_CONSTANT) {
all_constants = false;
+ }
}
if (all_constants && p_to_const) {
@@ -1849,14 +1790,12 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
} break;
case Node::TYPE_OPERATOR: {
-
OperatorNode *op = static_cast<OperatorNode *>(p_node);
bool all_constants = true;
int last_not_constant = -1;
for (int i = 0; i < op->arguments.size(); i++) {
-
op->arguments.write[i] = _reduce_expression(op->arguments[i], p_to_const);
if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
all_constants = false;
@@ -1875,15 +1814,12 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
} else if (op->op == OperatorNode::OP_CALL) {
//can reduce base type constructors
if ((op->arguments[0]->type == Node::TYPE_TYPE || (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && GDScriptFunctions::is_deterministic(static_cast<BuiltInFunctionNode *>(op->arguments[0])->function))) && last_not_constant == 0) {
-
//native type constructor or intrinsic function
const Variant **vptr = nullptr;
Vector<Variant *> ptrs;
if (op->arguments.size() > 1) {
-
ptrs.resize(op->arguments.size() - 1);
for (int i = 0; i < ptrs.size(); i++) {
-
ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
ptrs.write[i] = &cn->value;
}
@@ -1904,7 +1840,6 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
}
if (ce.error != Callable::CallError::CALL_OK) {
-
String errwhere;
if (op->arguments[0]->type == Node::TYPE_TYPE) {
TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
@@ -1916,18 +1851,14 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
}
switch (ce.error) {
-
case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
-
_set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
} break;
case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
-
_set_error("Too many arguments for " + errwhere + ".");
} break;
case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
-
_set_error("Too few arguments for " + errwhere + ".");
} break;
default: {
@@ -1956,7 +1887,6 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
//can reduce indices into constant arrays or dictionaries
if (all_constants) {
-
ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
ConstantNode *cb = static_cast<ConstantNode *>(op->arguments[1]);
@@ -1978,9 +1908,7 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
return op;
} else if (op->op == OperatorNode::OP_INDEX_NAMED) {
-
if (op->arguments[0]->type == Node::TYPE_CONSTANT && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
-
ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
IdentifierNode *ib = static_cast<IdentifierNode *>(op->arguments[1]);
@@ -2003,7 +1931,6 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
//validate assignment (don't assign to constant expression
switch (op->op) {
-
case OperatorNode::OP_ASSIGN:
case OperatorNode::OP_ASSIGN_ADD:
case OperatorNode::OP_ASSIGN_SUB:
@@ -2015,7 +1942,6 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
case OperatorNode::OP_ASSIGN_BIT_AND:
case OperatorNode::OP_ASSIGN_BIT_OR:
case OperatorNode::OP_ASSIGN_BIT_XOR: {
-
if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
_set_error("Can't assign to constant", tokenizer->get_token_line() - 1);
error_line = op->line;
@@ -2041,8 +1967,9 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
}
}
//now se if all are constants
- if (!all_constants)
+ if (!all_constants) {
return op; //nothing to reduce from here on
+ }
#define _REDUCE_UNARY(m_vop) \
bool valid = false; \
Variant res; \
@@ -2072,7 +1999,6 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
return cn;
switch (op->op) {
-
//unary operators
case OperatorNode::OP_NEG: {
_REDUCE_UNARY(Variant::OP_NEGATE);
@@ -2164,23 +2090,24 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to
}
GDScriptParser::Node *GDScriptParser::_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const, bool p_allow_assign) {
-
Node *expr = _parse_expression(p_parent, p_static, p_allow_assign, p_reduce_const);
- if (!expr || error_set)
+ if (!expr || error_set) {
return nullptr;
+ }
expr = _reduce_expression(expr, p_reduce_const);
- if (!expr || error_set)
+ if (!expr || error_set) {
return nullptr;
+ }
return expr;
}
bool GDScriptParser::_reduce_export_var_type(Variant &p_value, int p_line) {
-
if (p_value.get_type() == Variant::ARRAY) {
Array arr = p_value;
for (int i = 0; i < arr.size(); i++) {
- if (!_reduce_export_var_type(arr[i], p_line))
+ if (!_reduce_export_var_type(arr[i], p_line)) {
return false;
+ }
}
return true;
}
@@ -2189,8 +2116,9 @@ bool GDScriptParser::_reduce_export_var_type(Variant &p_value, int p_line) {
Dictionary dict = p_value;
for (int i = 0; i < dict.size(); i++) {
Variant value = dict.get_value_at_index(i);
- if (!_reduce_export_var_type(value, p_line))
+ if (!_reduce_export_var_type(value, p_line)) {
return false;
+ }
}
return true;
}
@@ -2209,7 +2137,6 @@ bool GDScriptParser::_reduce_export_var_type(Variant &p_value, int p_line) {
}
bool GDScriptParser::_recover_from_completion() {
-
if (!completion_found) {
return false; //can't recover if no completion
}
@@ -2227,12 +2154,12 @@ bool GDScriptParser::_recover_from_completion() {
}
GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
-
PatternNode *pattern = alloc_node<PatternNode>();
GDScriptTokenizer::Token token = tokenizer->get_token();
- if (error_set)
+ if (error_set) {
return nullptr;
+ }
if (token == GDScriptTokenizer::TK_EOF) {
return nullptr;
@@ -2244,7 +2171,6 @@ GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
tokenizer->advance();
pattern->pt_type = GDScriptParser::PatternNode::PT_ARRAY;
while (true) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
tokenizer->advance();
break;
@@ -2316,7 +2242,6 @@ GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
tokenizer->advance();
pattern->pt_type = GDScriptParser::PatternNode::PT_DICTIONARY;
while (true) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
tokenizer->advance();
break;
@@ -2429,13 +2354,14 @@ void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBran
bool catch_all_appeared = false;
while (true) {
-
- while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
+ while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline()) {
;
+ }
// GDScriptTokenizer::Token token = tokenizer->get_token();
- if (error_set)
+ if (error_set) {
return;
+ }
if (current_level.indent > indent_level.back()->get().indent) {
break; // go back a level
@@ -2507,12 +2433,10 @@ void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBran
}
void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings) {
-
const DataType &to_match_type = p_node_to_match->get_datatype();
switch (p_pattern->pt_type) {
case PatternNode::PT_CONSTANT: {
-
DataType pattern_type = _reduce_node_type(p_pattern->constant);
if (error_set) {
return;
@@ -2576,7 +2500,6 @@ void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_m
p_resulting_node = true_value;
} break;
case PatternNode::PT_ARRAY: {
-
bool open_ended = false;
if (p_pattern->array.size() > 0) {
@@ -2673,7 +2596,6 @@ void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_m
} break;
case PatternNode::PT_DICTIONARY: {
-
bool open_ended = false;
if (p_pattern->array.size() > 0) {
@@ -2742,7 +2664,6 @@ void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_m
}
for (Map<ConstantNode *, PatternNode *>::Element *e = p_pattern->dictionary.front(); e; e = e->next()) {
-
Node *condition = nullptr;
// check for has, then for pattern
@@ -2757,7 +2678,6 @@ void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_m
has_call->arguments.push_back(e->key());
if (e->value()) {
-
OperatorNode *indexed_value = alloc_node<OperatorNode>();
indexed_value->op = OperatorNode::OP_INDEX;
indexed_value->arguments.push_back(p_node_to_match);
@@ -2795,7 +2715,6 @@ void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_m
p_resulting_node = true_value;
} break;
default: {
-
} break;
}
}
@@ -2816,7 +2735,6 @@ void GDScriptParser::_transform_match_statment(MatchNode *p_match_statement) {
}
for (int i = 0; i < p_match_statement->branches.size(); i++) {
-
PatternBranchNode *branch = p_match_statement->branches[i];
MatchNode::CompiledPatternBranch compiled_branch;
@@ -2899,7 +2817,6 @@ void GDScriptParser::_transform_match_statment(MatchNode *p_match_statement) {
}
void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
-
IndentLevel current_level = indent_level.back()->get();
#ifdef DEBUG_ENABLED
@@ -2926,8 +2843,9 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
is_first_line = false;
GDScriptTokenizer::Token token = tokenizer->get_token();
- if (error_set)
+ if (error_set) {
return;
+ }
if (current_level.indent > indent_level.back()->get().indent) {
p_block->end_line = tokenizer->get_token_line();
@@ -2935,7 +2853,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
if (pending_newline != -1) {
-
NewLineNode *nl2 = alloc_node<NewLineNode>();
nl2->line = pending_newline;
p_block->statements.push_back(nl2);
@@ -2967,7 +2884,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
return;
} break;
case GDScriptTokenizer::TK_NEWLINE: {
-
int line = tokenizer->get_token_line();
if (!_parse_newline()) {
@@ -2986,7 +2902,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
} break;
case GDScriptTokenizer::TK_CF_PASS: {
if (tokenizer->get_token(1) != GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF) {
-
_set_error("Expected \";\" or a line break.");
return;
}
@@ -3003,7 +2918,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
tokenizer->advance();
int var_line = tokenizer->get_token_line();
if (!tokenizer->is_token_literal(0, true)) {
-
_set_error("Expected an identifier for the local variable name.");
return;
}
@@ -3048,7 +2962,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
-
tokenizer->advance();
Node *subexpr = _parse_and_reduce_expression(p_block, p_static);
if (!subexpr) {
@@ -3061,7 +2974,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
lv->assignments++;
assigned = subexpr;
} else {
-
assigned = _get_default_value_for_type(lv->datatype, var_line);
}
//must be added later, to avoid self-referencing.
@@ -3088,7 +3000,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
} break;
case GDScriptTokenizer::TK_CF_IF: {
-
tokenizer->advance();
Node *condition = _parse_and_reduce_expression(p_block, p_static);
@@ -3120,17 +3031,18 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
_parse_block(cf_if->body, p_static);
current_block = p_block;
- if (error_set)
+ if (error_set) {
return;
+ }
p_block->statements.push_back(cf_if);
bool all_have_return = cf_if->body->has_return;
bool have_else = false;
while (true) {
-
- while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
+ while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline()) {
;
+ }
if (indent_level.back()->get().indent < current_level.indent) { //not at current indent level
p_block->end_line = tokenizer->get_token_line();
@@ -3138,9 +3050,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELIF) {
-
if (indent_level.back()->get().indent > current_level.indent) {
-
_set_error("Invalid indentation.");
return;
}
@@ -3180,13 +3090,13 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
current_block = cf_else->body;
_parse_block(cf_else->body, p_static);
current_block = p_block;
- if (error_set)
+ if (error_set) {
return;
+ }
all_have_return = all_have_return && cf_else->body->has_return;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELSE) {
-
if (indent_level.back()->get().indent > current_level.indent) {
_set_error("Invalid indentation.");
return;
@@ -3205,16 +3115,18 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
current_block = cf_if->body_else;
_parse_block(cf_if->body_else, p_static);
current_block = p_block;
- if (error_set)
+ if (error_set) {
return;
+ }
all_have_return = all_have_return && cf_if->body_else->has_return;
have_else = true;
break; //after else, exit
- } else
+ } else {
break;
+ }
}
cf_if->body->has_return = all_have_return;
@@ -3223,7 +3135,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
} break;
case GDScriptTokenizer::TK_CF_WHILE: {
-
tokenizer->advance();
Node *condition2 = _parse_and_reduce_expression(p_block, p_static);
if (!condition2) {
@@ -3240,6 +3151,8 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
cf_while->body = alloc_node<BlockNode>();
cf_while->body->parent_block = p_block;
+ cf_while->body->can_break = true;
+ cf_while->body->can_continue = true;
p_block->sub_blocks.push_back(cf_while->body);
if (!_enter_indent_block(cf_while->body)) {
@@ -3251,17 +3164,16 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
current_block = cf_while->body;
_parse_block(cf_while->body, p_static);
current_block = p_block;
- if (error_set)
+ if (error_set) {
return;
+ }
p_block->has_return = cf_while->body->has_return;
p_block->statements.push_back(cf_while);
} break;
case GDScriptTokenizer::TK_CF_FOR: {
-
tokenizer->advance();
if (!tokenizer->is_token_literal(0, true)) {
-
_set_error("Identifier expected after \"for\".");
}
@@ -3288,7 +3200,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
DataType iter_type;
if (container->type == Node::TYPE_OPERATOR) {
-
OperatorNode *op = static_cast<OperatorNode *>(container);
if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && static_cast<BuiltInFunctionNode *>(op->arguments[0])->function == GDScriptFunctions::GEN_RANGE) {
//iterating a range, so see if range() can be optimized without allocating memory, by replacing it by vectors (which can work as iterable too!)
@@ -3304,6 +3215,8 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
ConstantNode *c = static_cast<ConstantNode *>(op->arguments[i]);
if (c->value.get_type() == Variant::FLOAT || c->value.get_type() == Variant::INT) {
constants.push_back(c->value);
+ } else {
+ constant = false;
}
} else {
constant = false;
@@ -3311,9 +3224,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
if (args.size() > 0 && args.size() < 4) {
-
if (constant) {
-
ConstantNode *cn = alloc_node<ConstantNode>();
switch (args.size()) {
case 1:
@@ -3369,6 +3280,8 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
cf_for->body = alloc_node<BlockNode>();
cf_for->body->parent_block = p_block;
+ cf_for->body->can_break = true;
+ cf_for->body->can_continue = true;
p_block->sub_blocks.push_back(cf_for->body);
if (!_enter_indent_block(cf_for->body)) {
@@ -3392,12 +3305,27 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
_parse_block(cf_for->body, p_static);
current_block = p_block;
- if (error_set)
+ if (error_set) {
return;
+ }
p_block->has_return = cf_for->body->has_return;
p_block->statements.push_back(cf_for);
} break;
case GDScriptTokenizer::TK_CF_CONTINUE: {
+ BlockNode *upper_block = p_block;
+ bool is_continue_valid = false;
+ while (upper_block) {
+ if (upper_block->can_continue) {
+ is_continue_valid = true;
+ break;
+ }
+ upper_block = upper_block->parent_block;
+ }
+
+ if (!is_continue_valid) {
+ _set_error("Unexpected keyword \"continue\" outside a loop.");
+ return;
+ }
_mark_line_as_safe(tokenizer->get_token_line());
tokenizer->advance();
@@ -3410,6 +3338,20 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
} break;
case GDScriptTokenizer::TK_CF_BREAK: {
+ BlockNode *upper_block = p_block;
+ bool is_break_valid = false;
+ while (upper_block) {
+ if (upper_block->can_break) {
+ is_break_valid = true;
+ break;
+ }
+ upper_block = upper_block->parent_block;
+ }
+
+ if (!is_break_valid) {
+ _set_error("Unexpected keyword \"break\" outside a loop.");
+ return;
+ }
_mark_line_as_safe(tokenizer->get_token_line());
tokenizer->advance();
@@ -3422,7 +3364,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
} break;
case GDScriptTokenizer::TK_CF_RETURN: {
-
tokenizer->advance();
ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
cf_return->cf_type = ControlFlowNode::CF_RETURN;
@@ -3454,7 +3395,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
} break;
case GDScriptTokenizer::TK_CF_MATCH: {
-
tokenizer->advance();
MatchNode *match_node = alloc_node<MatchNode>();
@@ -3478,13 +3418,15 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
BlockNode *compiled_branches = alloc_node<BlockNode>();
compiled_branches->parent_block = p_block;
compiled_branches->parent_class = p_block->parent_class;
+ compiled_branches->can_continue = true;
p_block->sub_blocks.push_back(compiled_branches);
_parse_pattern_block(compiled_branches, match_node->branches, p_static);
- if (error_set)
+ if (error_set) {
return;
+ }
ControlFlowNode *match_cf_node = alloc_node<ControlFlowNode>();
match_cf_node->cf_type = ControlFlowNode::CF_MATCH;
@@ -3497,7 +3439,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
_end_statement();
} break;
case GDScriptTokenizer::TK_PR_ASSERT: {
-
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
@@ -3541,7 +3482,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
} break;
case GDScriptTokenizer::TK_PR_BREAKPOINT: {
-
tokenizer->advance();
BreakpointNode *bn = alloc_node<BreakpointNode>();
p_block->statements.push_back(bn);
@@ -3552,7 +3492,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
} break;
default: {
-
Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
if (!expression) {
if (_recover_from_completion()) {
@@ -3577,9 +3516,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
}
bool GDScriptParser::_parse_newline() {
-
if (tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
-
IndentLevel current_level = indent_level.back()->get();
int indent = tokenizer->get_token_line_indent();
int tabs = tokenizer->get_token_line_tab_indent();
@@ -3596,9 +3533,7 @@ bool GDScriptParser::_parse_newline() {
}
if (indent < current_level.indent) {
-
while (indent < current_level.indent) {
-
//exit block
if (indent_level.size() == 1) {
_set_error("Invalid indentation. Bug?");
@@ -3608,7 +3543,6 @@ bool GDScriptParser::_parse_newline() {
indent_level.pop_back();
if (indent_level.back()->get().indent < indent) {
-
_set_error("Unindent does not match any outer indentation level.");
return false;
}
@@ -3631,15 +3565,12 @@ bool GDScriptParser::_parse_newline() {
}
void GDScriptParser::_parse_extends(ClassNode *p_class) {
-
if (p_class->extends_used) {
-
_set_error("\"extends\" can only be present once per script.");
return;
}
if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
-
_set_error("\"extends\" must be used before anything else.");
return;
}
@@ -3656,10 +3587,8 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) {
// see if inheritance happens from a file
if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
-
Variant constant = tokenizer->get_token_constant();
if (constant.get_type() != Variant::STRING) {
-
_set_error("\"extends\" constant must be a string.");
return;
}
@@ -3676,16 +3605,14 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) {
if (tokenizer->get_token() != GDScriptTokenizer::TK_PERIOD) {
return;
- } else
+ } else {
tokenizer->advance();
+ }
}
while (true) {
-
switch (tokenizer->get_token()) {
-
case GDScriptTokenizer::TK_IDENTIFIER: {
-
StringName identifier = tokenizer->get_token_identifier();
p_class->extends_class.push_back(identifier);
} break;
@@ -3694,7 +3621,6 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) {
break;
default: {
-
_set_error("Invalid \"extends\" syntax, expected string constant (path) and/or identifier (parent class).");
return;
}
@@ -3703,7 +3629,6 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) {
tokenizer->advance(1);
switch (tokenizer->get_token()) {
-
case GDScriptTokenizer::TK_IDENTIFIER:
case GDScriptTokenizer::TK_PERIOD:
continue;
@@ -3715,14 +3640,13 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) {
}
void GDScriptParser::_parse_class(ClassNode *p_class) {
-
IndentLevel current_level = indent_level.back()->get();
while (true) {
-
GDScriptTokenizer::Token token = tokenizer->get_token();
- if (error_set)
+ if (error_set) {
return;
+ }
if (current_level.indent > indent_level.back()->get().indent) {
p_class->end_line = tokenizer->get_token_line();
@@ -3730,7 +3654,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
switch (token) {
-
case GDScriptTokenizer::TK_CURSOR: {
tokenizer->advance();
} break;
@@ -3750,11 +3673,11 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
} break;
case GDScriptTokenizer::TK_PR_EXTENDS: {
-
_mark_line_as_safe(tokenizer->get_token_line());
_parse_extends(p_class);
- if (error_set)
+ if (error_set) {
return;
+ }
if (!_end_statement()) {
_set_end_statement_error("extends");
return;
@@ -3762,7 +3685,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
} break;
case GDScriptTokenizer::TK_PR_CLASS_NAME: {
-
_mark_line_as_safe(tokenizer->get_token_line());
if (p_class->owner) {
_set_error("\"class_name\" is only valid for the main class namespace.");
@@ -3773,7 +3695,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
return;
}
if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
-
_set_error("\"class_name\" syntax: \"class_name <UniqueName>\"");
return;
}
@@ -3839,9 +3760,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
} break;
case GDScriptTokenizer::TK_PR_TOOL: {
-
if (p_class->tool) {
-
_set_error("The \"tool\" keyword can only be present once per script.");
return;
}
@@ -3856,7 +3775,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
StringName name;
if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
-
_set_error("\"class\" syntax: \"class <Name>:\" or \"class <Name> extends <BaseClass>:\"");
return;
}
@@ -3905,14 +3823,13 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
p_class->subclasses.push_back(newclass);
if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_EXTENDS) {
-
_parse_extends(newclass);
- if (error_set)
+ if (error_set) {
return;
+ }
}
if (!_enter_indent_block()) {
-
_set_error("Indented block expected.");
return;
}
@@ -3930,7 +3847,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
case GDScriptTokenizer::TK_PR_STATIC: {
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
-
_set_error("Expected \"func\".");
return;
}
@@ -3938,12 +3854,10 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
[[fallthrough]];
}
case GDScriptTokenizer::TK_PR_FUNCTION: {
-
bool _static = false;
pending_newline = -1;
if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_STATIC) {
-
_static = true;
}
@@ -3954,7 +3868,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (name == StringName()) {
-
_set_error("Expected an identifier after \"func\" (syntax: \"func <identifier>([arguments]):\").");
return;
}
@@ -3987,7 +3900,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
#endif // DEBUG_ENABLED
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
-
_set_error("Expected \"(\" after the identifier (syntax: \"func <identifier>([arguments]):\" ).");
return;
}
@@ -4007,19 +3919,16 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
//has arguments
bool defaulting = false;
while (true) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
tokenizer->advance();
continue;
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_VAR) {
-
tokenizer->advance(); //var before the identifier is allowed
}
if (!tokenizer->is_token_literal(0, true)) {
-
_set_error("Expected an identifier for an argument.");
return;
}
@@ -4051,7 +3960,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
argument_types.push_back(argtype);
if (defaulting && tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
-
_set_error("Default parameter expected.");
return;
}
@@ -4062,8 +3970,9 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
defaulting = true;
tokenizer->advance(1);
Node *defval = _parse_and_reduce_expression(p_class, _static);
- if (!defval || error_set)
+ if (!defval || error_set) {
return;
+ }
OperatorNode *on = alloc_node<OperatorNode>();
on->op = OperatorNode::OP_ASSIGN;
@@ -4092,7 +4001,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
tokenizer->advance();
continue;
} else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
-
_set_error("Expected \",\" or \")\".");
return;
}
@@ -4120,14 +4028,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
if (name == "_init") {
-
if (_static) {
_set_error("The constructor cannot be static.");
return;
}
if (p_class->extends_used) {
-
OperatorNode *cparent = alloc_node<OperatorNode>();
cparent->op = OperatorNode::OP_PARENT_CALL;
block->statements.push_back(cparent);
@@ -4148,7 +4054,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
//has arguments
parenthesis++;
while (true) {
-
current_function = function;
Node *arg = _parse_and_reduce_expression(p_class, _static);
current_function = nullptr;
@@ -4158,7 +4063,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
tokenizer->advance();
continue;
} else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
-
_set_error("Expected \",\" or \")\".");
return;
}
@@ -4171,9 +4075,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
tokenizer->advance();
}
} else {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
-
_set_error("Parent constructor call found for a class without inheritance.");
return;
}
@@ -4182,7 +4084,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
DataType return_type;
if (tokenizer->get_token() == GDScriptTokenizer::TK_FORWARD_ARROW) {
-
if (!_parse_type(return_type, true)) {
_set_error("Expected a return type for the function.");
return;
@@ -4190,17 +4091,17 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (!_enter_indent_block(block)) {
-
_set_error(vformat("Indented block expected after declaration of \"%s\" function.", function->name));
return;
}
function->return_type = return_type;
- if (_static)
+ if (_static) {
p_class->static_functions.push_back(function);
- else
+ } else {
p_class->functions.push_back(function);
+ }
current_function = function;
function->body = block;
@@ -4266,11 +4167,9 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
} break;
case GDScriptTokenizer::TK_PR_EXPORT: {
-
tokenizer->advance();
if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
-
#define _ADVANCE_AND_CONSUME_NEWLINES \
do { \
tokenizer->advance(); \
@@ -4295,7 +4194,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
-
Variant::Type type = tokenizer->get_token_type();
if (type == Variant::NIL) {
_set_error("Can't export null type.");
@@ -4314,11 +4212,8 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
_ADVANCE_AND_CONSUME_NEWLINES;
switch (type) {
-
case Variant::INT: {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
-
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
@@ -4331,7 +4226,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
bool first = true;
while (true) {
-
if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
current_export = PropertyInfo();
_set_error("Expected a string constant in the named bit flags hint.");
@@ -4339,16 +4233,18 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
String c = tokenizer->get_token_constant();
- if (!first)
+ if (!first) {
current_export.hint_string += ",";
- else
+ } else {
first = false;
+ }
current_export.hint_string += c.xml_escape();
_ADVANCE_AND_CONSUME_NEWLINES;
- if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
+ if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
break;
+ }
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
current_export = PropertyInfo();
@@ -4362,7 +4258,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_2D_RENDER") {
-
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
_set_error("Expected \")\" in the layers 2D render hint.");
@@ -4373,7 +4268,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_2D_PHYSICS") {
-
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
_set_error("Expected \")\" in the layers 2D physics hint.");
@@ -4384,7 +4278,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_3D_RENDER") {
-
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
_set_error("Expected \")\" in the layers 3D render hint.");
@@ -4395,7 +4288,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "LAYERS_3D_PHYSICS") {
-
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
_set_error("Expected \")\" in the layers 3D physics hint.");
@@ -4410,25 +4302,25 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
current_export.hint = PROPERTY_HINT_ENUM;
bool first = true;
while (true) {
-
if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
-
current_export = PropertyInfo();
_set_error("Expected a string constant in the enumeration hint.");
return;
}
String c = tokenizer->get_token_constant();
- if (!first)
+ if (!first) {
current_export.hint_string += ",";
- else
+ } else {
first = false;
+ }
current_export.hint_string += c.xml_escape();
_ADVANCE_AND_CONSUME_NEWLINES;
- if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
+ if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
break;
+ }
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
current_export = PropertyInfo();
@@ -4445,7 +4337,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
[[fallthrough]];
}
case Variant::FLOAT: {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
current_export.hint = PROPERTY_HINT_EXP_EASING;
_ADVANCE_AND_CONSUME_NEWLINES;
@@ -4458,19 +4349,19 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
// range
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
-
current_export.hint = PROPERTY_HINT_EXP_RANGE;
_ADVANCE_AND_CONSUME_NEWLINES;
- if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
+ if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
break;
- else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
+ } else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
_set_error("Expected \")\" or \",\" in the exponential range hint.");
return;
}
_ADVANCE_AND_CONSUME_NEWLINES;
- } else
+ } else {
current_export.hint = PROPERTY_HINT_RANGE;
+ }
float sign = 1.0;
@@ -4479,7 +4370,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
_ADVANCE_AND_CONSUME_NEWLINES;
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
-
current_export = PropertyInfo();
_set_error("Expected a range in the numeric hint.");
return;
@@ -4494,7 +4384,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
-
current_export = PropertyInfo();
_set_error("Expected \",\" or \")\" in the numeric range hint.");
return;
@@ -4509,7 +4398,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
-
current_export = PropertyInfo();
_set_error("Expected a number as upper bound in the numeric range hint.");
return;
@@ -4518,11 +4406,11 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
_ADVANCE_AND_CONSUME_NEWLINES;
- if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
+ if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
break;
+ }
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
-
current_export = PropertyInfo();
_set_error("Expected \",\" or \")\" in the numeric range hint.");
return;
@@ -4536,7 +4424,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
-
current_export = PropertyInfo();
_set_error("Expected a number as step in the numeric range hint.");
return;
@@ -4547,30 +4434,29 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
} break;
case Variant::STRING: {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
//enumeration
current_export.hint = PROPERTY_HINT_ENUM;
bool first = true;
while (true) {
-
if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
-
current_export = PropertyInfo();
_set_error("Expected a string constant in the enumeration hint.");
return;
}
String c = tokenizer->get_token_constant();
- if (!first)
+ if (!first) {
current_export.hint_string += ",";
- else
+ } else {
first = false;
+ }
current_export.hint_string += c.xml_escape();
_ADVANCE_AND_CONSUME_NEWLINES;
- if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
+ if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
break;
+ }
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
current_export = PropertyInfo();
@@ -4584,13 +4470,11 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
-
_ADVANCE_AND_CONSUME_NEWLINES;
- if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
+ if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
current_export.hint = PROPERTY_HINT_DIR;
- else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
-
+ } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
@@ -4616,16 +4500,13 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
-
current_export.hint = PROPERTY_HINT_FILE;
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
-
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
-
if (!p_class->tool) {
_set_error("Global filesystem hints may only be used in tool scripts.");
return;
@@ -4633,22 +4514,22 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
_ADVANCE_AND_CONSUME_NEWLINES;
- if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
+ if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
break;
- else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA)
+ } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
_ADVANCE_AND_CONSUME_NEWLINES;
- else {
+ } else {
_set_error("Expected \")\" or \",\" in the hint.");
return;
}
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
-
- if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
+ if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE) {
_set_error("Expected string constant with filter.");
- else
+ } else {
_set_error("Expected \"GLOBAL\" or string constant with filter.");
+ }
return;
}
current_export.hint_string = tokenizer->get_token_constant();
@@ -4663,7 +4544,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
-
current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
_ADVANCE_AND_CONSUME_NEWLINES;
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
@@ -4674,9 +4554,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
} break;
case Variant::COLOR: {
-
if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) {
-
current_export = PropertyInfo();
_set_error("Color type hint expects RGB or RGBA as hints.");
return;
@@ -4696,7 +4574,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
} break;
default: {
-
current_export = PropertyInfo();
_set_error("Type \"" + Variant::get_type_name(type) + "\" can't take hints.");
return;
@@ -4705,7 +4582,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
} else {
-
parenthesis++;
Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
if (!subexpr) {
@@ -4765,10 +4641,11 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
bool first = true;
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
if (enum_values[E->get()].get_type() == Variant::INT) {
- if (!first)
+ if (!first) {
current_export.hint_string += ",";
- else
+ } else {
first = false;
+ }
current_export.hint_string += E->get().operator String().camelcase_to_underscore(true).capitalize().xml_escape();
if (!is_flags) {
@@ -4785,7 +4662,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
-
current_export = PropertyInfo();
_set_error("Expected \")\" or \",\" after the export hint.");
return;
@@ -4807,7 +4683,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_ONREADY && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTE && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTER && tokenizer->get_token() != GDScriptTokenizer::TK_PR_PUPPET && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTESYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTERSYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_PUPPETSYNC) {
-
current_export = PropertyInfo();
_set_error("Expected \"var\", \"onready\", \"remote\", \"master\", \"puppet\", \"remotesync\", \"mastersync\", \"puppetsync\".");
return;
@@ -4816,7 +4691,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_ONREADY: {
-
//may be fallthrough from export, ignore if so
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
@@ -4827,7 +4701,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_REMOTE: {
-
//may be fallthrough from export, ignore if so
tokenizer->advance();
if (current_export.type) {
@@ -4847,7 +4720,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_MASTER: {
-
//may be fallthrough from export, ignore if so
tokenizer->advance();
if (current_export.type) {
@@ -4867,7 +4739,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_PUPPET: {
-
//may be fallthrough from export, ignore if so
tokenizer->advance();
if (current_export.type) {
@@ -4887,14 +4758,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_REMOTESYNC: {
-
//may be fallthrough from export, ignore if so
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
- if (current_export.type)
+ if (current_export.type) {
_set_error("Expected \"var\".");
- else
+ } else {
_set_error("Expected \"var\" or \"func\".");
+ }
return;
}
@@ -4902,14 +4773,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_MASTERSYNC: {
-
//may be fallthrough from export, ignore if so
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
- if (current_export.type)
+ if (current_export.type) {
_set_error("Expected \"var\".");
- else
+ } else {
_set_error("Expected \"var\" or \"func\".");
+ }
return;
}
@@ -4917,14 +4788,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
continue;
} break;
case GDScriptTokenizer::TK_PR_PUPPETSYNC: {
-
//may be fallthrough from export, ignore if so
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
- if (current_export.type)
+ if (current_export.type) {
_set_error("Expected \"var\".");
- else
+ } else {
_set_error("Expected \"var\" or \"func\".");
+ }
return;
}
@@ -4946,7 +4817,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
tokenizer->advance();
if (!tokenizer->is_token_literal(0, true)) {
-
_set_error("Expected an identifier for the member variable name.");
return;
}
@@ -5036,7 +4906,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
#endif
if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
-
#ifdef DEBUG_ENABLED
int line = tokenizer->get_token_line();
#endif
@@ -5052,12 +4921,10 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
//discourage common error
if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
-
OperatorNode *op = static_cast<OperatorNode *>(subexpr);
if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
if (id->name == "get_node") {
-
_set_error("Use \"onready var " + String(member.identifier) + " = get_node(...)\" instead.");
return;
}
@@ -5067,22 +4934,20 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
member.expression = subexpr;
if (autoexport && !member.data_type.has_type) {
-
if (subexpr->type != Node::TYPE_CONSTANT) {
-
_set_error("Type-less export needs a constant expression assigned to infer type.");
return;
}
ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
if (cn->value.get_type() == Variant::NIL) {
-
_set_error("Can't accept a null constant expression for inferring export type.");
return;
}
- if (!_reduce_export_var_type(cn->value, member.line))
+ if (!_reduce_export_var_type(cn->value, member.line)) {
return;
+ }
member._export.type = cn->value.get_type();
member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
@@ -5099,7 +4964,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
#ifdef TOOLS_ENABLED
if (subexpr->type == Node::TYPE_CONSTANT && (member._export.type != Variant::NIL || member.data_type.has_type)) {
-
ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
if (cn->value.get_type() != Variant::NIL) {
if (member._export.type != Variant::NIL && cn->value.get_type() != member._export.type) {
@@ -5129,20 +4993,21 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
#ifdef DEBUG_ENABLED
NewLineNode *nl2 = alloc_node<NewLineNode>();
nl2->line = line;
- if (onready)
+ if (onready) {
p_class->ready->statements.push_back(nl2);
- else
+ } else {
p_class->initializer->statements.push_back(nl2);
+ }
#endif
- if (onready)
+ if (onready) {
p_class->ready->statements.push_back(op);
- else
+ } else {
p_class->initializer->statements.push_back(op);
+ }
member.initial_assignment = op;
} else {
-
if (autoexport && !member.data_type.has_type) {
_set_error("Type-less export needs a constant expression assigned to infer type.");
return;
@@ -5175,7 +5040,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
-
tokenizer->advance();
if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
@@ -5216,7 +5080,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
tokenizer->advance();
if (!tokenizer->is_token_literal(0, true)) {
-
_set_error("Expected an identifier for the constant.");
return;
}
@@ -5332,14 +5195,11 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
while (true) {
if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
-
tokenizer->advance(); // Ignore newlines
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
-
tokenizer->advance();
break; // End of enum
} else if (!tokenizer->is_token_literal(0, true)) {
-
if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
_set_error("Unexpected end of file.");
} else {
@@ -5460,7 +5320,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
} break;
default: {
-
_set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
return;
@@ -5470,7 +5329,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
}
void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive) {
-
if (p_class->base_type.has_type) {
// Already determined
} else if (p_class->extends_used) {
@@ -5485,7 +5343,6 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
//path (and optionally subclasses)
if (path.is_rel_path()) {
-
String base = base_path;
if (base == "" || base.is_rel_path()) {
@@ -5500,22 +5357,17 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
return;
}
if (!script->is_valid()) {
-
_set_error("Script isn't fully loaded (cyclic preload?): " + path, p_class->line);
return;
}
if (p_class->extends_class.size()) {
-
for (int i = 0; i < p_class->extends_class.size(); i++) {
-
String sub = p_class->extends_class[i];
if (script->get_subclasses().has(sub)) {
-
Ref<Script> subclass = script->get_subclasses()[sub]; //avoid reference from disappearing
script = subclass;
} else {
-
_set_error("Couldn't find the subclass: " + sub, p_class->line);
return;
}
@@ -5523,7 +5375,6 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
}
} else {
-
if (p_class->extends_class.size() == 0) {
_set_error("Parser bug: undecidable inheritance.", p_class->line);
ERR_FAIL();
@@ -5570,7 +5421,6 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
}
while (p) {
-
bool found = false;
for (int i = 0; i < p->subclasses.size(); i++) {
@@ -5599,10 +5449,12 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
}
}
- if (base_class)
+ if (base_class) {
break;
- if (found)
+ }
+ if (found) {
continue;
+ }
if (p->constant_expressions.has(base)) {
if (p->constant_expressions[base].expression->type != Node::TYPE_CONSTANT) {
@@ -5622,21 +5474,17 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
}
if (base_script.is_valid()) {
-
String ident = base;
Ref<GDScript> find_subclass = base_script;
for (int i = extend_iter; i < p_class->extends_class.size(); i++) {
-
String subclass = p_class->extends_class[i];
ident += ("." + subclass);
if (find_subclass->get_subclasses().has(subclass)) {
-
find_subclass = find_subclass->get_subclasses()[subclass];
} else if (find_subclass->get_constants().has(subclass)) {
-
Ref<GDScript> new_base_class = find_subclass->get_constants()[subclass];
if (new_base_class.is_null()) {
_set_error("Constant isn't a class: " + ident, p_class->line);
@@ -5644,7 +5492,6 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
}
find_subclass = new_base_class;
} else {
-
_set_error("Couldn't find the subclass: " + ident, p_class->line);
return;
}
@@ -5653,15 +5500,12 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
script = find_subclass;
} else if (!base_class) {
-
if (p_class->extends_class.size() > 1) {
-
_set_error("Invalid inheritance (unknown class + subclasses).", p_class->line);
return;
}
//if not found, try engine classes
if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
-
_set_error("Unknown class: \"" + base + "\"", p_class->line);
return;
}
@@ -5704,12 +5548,14 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive
}
String GDScriptParser::DataType::to_string() const {
- if (!has_type)
+ if (!has_type) {
return "var";
+ }
switch (kind) {
case BUILTIN: {
- if (builtin_type == Variant::NIL)
+ if (builtin_type == Variant::NIL) {
return "null";
+ }
return Variant::get_type_name(builtin_type);
} break;
case NATIVE: {
@@ -5873,10 +5719,12 @@ bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
}
GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, int p_line) {
- if (!p_source.has_type)
+ if (!p_source.has_type) {
return p_source;
- if (p_source.kind != DataType::UNRESOLVED)
+ }
+ if (p_source.kind != DataType::UNRESOLVED) {
return p_source;
+ }
Vector<String> full_name = p_source.native_type.operator String().split(".", false);
int name_part = 0;
@@ -5885,7 +5733,6 @@ GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source,
result.has_type = true;
while (name_part < full_name.size()) {
-
bool found = false;
StringName id = full_name[name_part];
DataType base_type = result;
@@ -6547,7 +6394,6 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
DataType source_type = _reduce_node_type(cn->source_node);
cn->cast_type = _resolve_type(cn->cast_type, cn->line);
if (source_type.has_type) {
-
bool valid = false;
if (check_types) {
if (cn->cast_type.kind == DataType::BUILTIN && source_type.kind == DataType::BUILTIN) {
@@ -6601,7 +6447,6 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
} break;
case OperatorNode::OP_IS:
case OperatorNode::OP_IS_BUILTIN: {
-
if (op->arguments.size() != 2) {
_set_error("Parser bug: binary operation without 2 arguments.", op->line);
ERR_FAIL_V(DataType());
@@ -6637,7 +6482,6 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
case OperatorNode::OP_POS:
case OperatorNode::OP_NOT:
case OperatorNode::OP_BIT_INVERT: {
-
DataType argument_type = _reduce_node_type(op->arguments[0]);
if (!argument_type.has_type) {
break;
@@ -6675,7 +6519,6 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
case OperatorNode::OP_BIT_AND:
case OperatorNode::OP_BIT_OR:
case OperatorNode::OP_BIT_XOR: {
-
if (op->arguments.size() != 2) {
_set_error("Parser bug: binary operation without 2 arguments.", op->line);
ERR_FAIL_V(DataType());
@@ -6743,7 +6586,6 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
case OperatorNode::OP_ASSIGN_BIT_OR:
case OperatorNode::OP_ASSIGN_BIT_XOR:
case OperatorNode::OP_INIT_ASSIGN: {
-
_set_error("Assignment inside an expression isn't allowed (parser bug?).", op->line);
return DataType();
@@ -6806,7 +6648,6 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
}
} break;
case OperatorNode::OP_INDEX: {
-
if (op->arguments[1]->type == Node::TYPE_CONSTANT) {
ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
if (cn->value.get_type() == Variant::STRING) {
@@ -7005,7 +6846,6 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
}
bool GDScriptParser::_get_function_signature(DataType &p_base_type, const StringName &p_function, DataType &r_return_type, List<DataType> &r_arg_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) const {
-
r_static = false;
r_default_arg_count = 0;
@@ -7116,8 +6956,9 @@ bool GDScriptParser::_get_function_signature(DataType &p_base_type, const String
native = "_" + native.operator String();
}
if (!ClassDB::class_exists(native)) {
- if (!check_types)
+ if (!check_types) {
return false;
+ }
ERR_FAIL_V_MSG(false, "Parser bug: Class '" + String(native) + "' not found.");
}
@@ -7208,8 +7049,9 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat
par_types.write[i - 1] = _reduce_node_type(p_call->arguments[i]);
}
- if (error_set)
+ if (error_set) {
return DataType();
+ }
// Special case: check copy constructor. Those are defined implicitly in Variant.
if (par_types.size() == 1) {
@@ -7277,8 +7119,9 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat
err += "' matches the signature '";
err += Variant::get_type_name(tn->vtype) + "(";
for (int i = 0; i < par_types.size(); i++) {
- if (i > 0)
+ if (i > 0) {
err += ", ";
+ }
err += par_types[i].to_string();
}
err += ")'.";
@@ -7507,7 +7350,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat
return return_type;
}
-bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const {
+bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type, bool *r_is_const) const {
DataType base_type = p_base_type;
// Check classes in current file
@@ -7518,6 +7361,9 @@ bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringN
while (base) {
if (base->constant_expressions.has(p_member)) {
+ if (r_is_const) {
+ *r_is_const = true;
+ }
r_member_type = base->constant_expressions[p_member].expression->get_datatype();
return true;
}
@@ -7636,8 +7482,9 @@ bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringN
native = "_" + native.operator String();
}
if (!ClassDB::class_exists(native)) {
- if (!check_types)
+ if (!check_types) {
return false;
+ }
ERR_FAIL_V_MSG(false, "Parser bug: Class \"" + String(native) + "\" not found.");
}
@@ -7725,7 +7572,6 @@ bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringN
}
GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line, bool p_is_indexing) {
-
if (p_base_type && !p_base_type->has_type) {
return DataType();
}
@@ -7742,8 +7588,9 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType
base_type = DataType(*p_base_type);
}
- if (_get_member_type(base_type, p_identifier, member_type)) {
- if (!p_base_type && current_function && current_function->_static) {
+ bool is_const = false;
+ if (_get_member_type(base_type, p_identifier, member_type, &is_const)) {
+ if (!p_base_type && current_function && current_function->_static && !is_const) {
_set_error("Can't access member variable (\"" + p_identifier.operator String() + "\") from a static function.", p_line);
return DataType();
}
@@ -7898,7 +7745,6 @@ 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.
@@ -7934,14 +7780,16 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
// Function declarations
for (int i = 0; i < p_class->static_functions.size(); i++) {
_check_function_types(p_class->static_functions[i]);
- if (error_set)
+ if (error_set) {
return;
+ }
}
for (int i = 0; i < p_class->functions.size(); i++) {
_check_function_types(p_class->functions[i]);
- if (error_set)
+ if (error_set) {
return;
+ }
}
// Class variables
@@ -7956,6 +7804,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
_mark_line_as_safe(v.line);
v.data_type = _resolve_type(v.data_type, v.line);
+ v.initial_assignment->arguments[0]->set_datatype(v.data_type);
if (v.expression) {
DataType expr_type = _reduce_node_type(v.expression);
@@ -7999,6 +7848,10 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
_set_error("The assigned value doesn't have a set type; the variable type can't be inferred.", v.line);
return;
}
+ if (expr_type.kind == DataType::BUILTIN && expr_type.builtin_type == Variant::NIL) {
+ _set_error("The variable type cannot be inferred because its value is \"null\".", v.line);
+ return;
+ }
v.data_type = expr_type;
v.data_type.is_constant = false;
}
@@ -8016,8 +7869,9 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
}
// Setter and getter
- if (v.setter == StringName() && v.getter == StringName())
+ if (v.setter == StringName() && v.getter == StringName()) {
continue;
+ }
bool found_getter = false;
bool found_setter = false;
@@ -8060,12 +7914,14 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
return;
}
}
- if (found_getter && found_setter)
+ if (found_getter && found_setter) {
break;
+ }
}
- if ((found_getter || v.getter == StringName()) && (found_setter || v.setter == StringName()))
+ if ((found_getter || v.getter == StringName()) && (found_setter || v.setter == StringName())) {
continue;
+ }
// Check for static functions
for (int j = 0; j < p_class->static_functions.size(); j++) {
@@ -8096,14 +7952,14 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
for (int i = 0; i < p_class->subclasses.size(); i++) {
current_class = p_class->subclasses[i];
_check_class_level_types(current_class);
- if (error_set)
+ if (error_set) {
return;
+ }
current_class = p_class;
}
}
void GDScriptParser::_check_function_types(FunctionNode *p_function) {
-
p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
// Arguments
@@ -8222,21 +8078,9 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) {
p_function->return_type.has_type = false;
p_function->return_type.may_yield = true;
}
-
-#ifdef DEBUG_ENABLED
- for (Map<StringName, LocalVarNode *>::Element *E = p_function->body->variables.front(); E; E = E->next()) {
- LocalVarNode *lv = E->get();
- for (int i = 0; i < current_class->variables.size(); i++) {
- if (current_class->variables[i].identifier == lv->name) {
- _add_warning(GDScriptWarning::SHADOWED_VARIABLE, lv->line, lv->name, itos(current_class->variables[i].line));
- }
- }
- }
-#endif // DEBUG_ENABLED
}
void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
-
// Function blocks
for (int i = 0; i < p_class->static_functions.size(); i++) {
current_function = p_class->static_functions[i];
@@ -8245,8 +8089,9 @@ void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
_check_block_types(current_block);
current_block = nullptr;
current_function = nullptr;
- if (error_set)
+ if (error_set) {
return;
+ }
}
for (int i = 0; i < p_class->functions.size(); i++) {
@@ -8256,8 +8101,9 @@ void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
_check_block_types(current_block);
current_block = nullptr;
current_function = nullptr;
- if (error_set)
+ if (error_set) {
return;
+ }
}
#ifdef DEBUG_ENABLED
@@ -8278,8 +8124,9 @@ void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
for (int i = 0; i < p_class->subclasses.size(); i++) {
current_class = p_class->subclasses[i];
_check_class_blocks_types(current_class);
- if (error_set)
+ if (error_set) {
return;
+ }
current_class = p_class;
}
}
@@ -8305,7 +8152,6 @@ static String _find_function_name(const GDScriptParser::OperatorNode *p_call) {
#endif // DEBUG_ENABLED
void GDScriptParser::_check_block_types(BlockNode *p_block) {
-
Node *last_var_assign = nullptr;
// Check each statement
@@ -8343,6 +8189,11 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
if (lv->datatype.has_type && assign_type.may_yield && lv->assign->type == Node::TYPE_OPERATOR) {
_add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, lv->line, _find_function_name(static_cast<OperatorNode *>(lv->assign)));
}
+ for (int i = 0; i < current_class->variables.size(); i++) {
+ if (current_class->variables[i].identifier == lv->name) {
+ _add_warning(GDScriptWarning::SHADOWED_VARIABLE, lv->line, lv->name, itos(current_class->variables[i].line));
+ }
+ }
#endif // DEBUG_ENABLED
if (!_is_type_compatible(lv->datatype, assign_type)) {
@@ -8388,6 +8239,10 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
_set_error("The assigned value doesn't have a set type; the variable type can't be inferred.", lv->line);
return;
}
+ if (assign_type.kind == DataType::BUILTIN && assign_type.builtin_type == Variant::NIL) {
+ _set_error("The variable type cannot be inferred because its value is \"null\".", lv->line);
+ return;
+ }
lv->datatype = assign_type;
lv->datatype.is_constant = false;
}
@@ -8542,8 +8397,9 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
_add_warning(GDScriptWarning::RETURN_VALUE_DISCARDED, op->line, func_name);
}
#endif // DEBUG_ENABLED
- if (error_set)
+ if (error_set) {
return;
+ }
} break;
case OperatorNode::OP_YIELD: {
_mark_line_as_safe(op->line);
@@ -8578,8 +8434,9 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
}
}
- if (!function_type.has_type)
+ if (!function_type.has_type) {
break;
+ }
if (function_type.kind == DataType::BUILTIN && function_type.builtin_type == Variant::NIL) {
// Return void, should not have arguments
@@ -8639,8 +8496,9 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
current_block = p_block->sub_blocks[i];
_check_block_types(current_block);
current_block = p_block;
- if (error_set)
+ if (error_set) {
return;
+ }
}
#ifdef DEBUG_ENABLED
@@ -8659,9 +8517,9 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
}
void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
-
- if (error_set)
+ if (error_set) {
return; //allow no further errors
+ }
error = p_error;
error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
@@ -8723,16 +8581,14 @@ void GDScriptParser::_add_warning(int p_code, int p_line, const Vector<String> &
#endif // DEBUG_ENABLED
String GDScriptParser::get_error() const {
-
return error;
}
int GDScriptParser::get_error_line() const {
-
return error_line;
}
-int GDScriptParser::get_error_column() const {
+int GDScriptParser::get_error_column() const {
return error_column;
}
@@ -8741,7 +8597,6 @@ bool GDScriptParser::has_error() const {
}
Error GDScriptParser::_parse(const String &p_base_path) {
-
base_path = p_base_path;
//assume class
@@ -8783,8 +8638,9 @@ Error GDScriptParser::_parse(const String &p_base_path) {
current_function = nullptr;
current_block = nullptr;
- if (for_completion)
+ if (for_completion) {
check_types = false;
+ }
// Resolve all class-level stuff before getting into function blocks
_check_class_level_types(main_class);
@@ -8840,7 +8696,6 @@ Error GDScriptParser::_parse(const String &p_base_path) {
}
Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
-
clear();
self_path = p_self_path;
@@ -8854,7 +8709,6 @@ Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const St
}
Error GDScriptParser::parse(const String &p_code, const String &p_base_path, bool p_just_validate, const String &p_self_path, bool p_for_completion, Set<int> *r_safe_lines, bool p_dependencies_only) {
-
clear();
self_path = p_self_path;
@@ -8875,19 +8729,15 @@ Error GDScriptParser::parse(const String &p_code, const String &p_base_path, boo
}
bool GDScriptParser::is_tool_script() const {
-
return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
}
const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
-
return head;
}
void GDScriptParser::clear() {
-
while (list) {
-
Node *l = list;
list = list->next;
memdelete(l);
@@ -8929,57 +8779,46 @@ void GDScriptParser::clear() {
}
GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
-
return completion_type;
}
StringName GDScriptParser::get_completion_cursor() {
-
return completion_cursor;
}
int GDScriptParser::get_completion_line() {
-
return completion_line;
}
Variant::Type GDScriptParser::get_completion_built_in_constant() {
-
return completion_built_in_constant;
}
GDScriptParser::Node *GDScriptParser::get_completion_node() {
-
return completion_node;
}
GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
-
return completion_block;
}
GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
-
return completion_class;
}
GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
-
return completion_function;
}
int GDScriptParser::get_completion_argument_index() {
-
return completion_argument;
}
bool GDScriptParser::get_completion_identifier_is_function() {
-
return completion_ident_is_call;
}
GDScriptParser::GDScriptParser() {
-
head = nullptr;
list = nullptr;
tokenizer = nullptr;
@@ -8988,6 +8827,5 @@ GDScriptParser::GDScriptParser() {
}
GDScriptParser::~GDScriptParser() {
-
clear();
}