summaryrefslogtreecommitdiff
path: root/modules/gdscript/gdscript_compiler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdscript/gdscript_compiler.cpp')
-rw-r--r--modules/gdscript/gdscript_compiler.cpp464
1 files changed, 248 insertions, 216 deletions
diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp
index 2bbec29043..bc095ae1f9 100644
--- a/modules/gdscript/gdscript_compiler.cpp
+++ b/modules/gdscript/gdscript_compiler.cpp
@@ -33,24 +33,24 @@
#include "gdscript.h"
bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) {
-
- if (codegen.function_node && codegen.function_node->_static)
+ if (codegen.function_node && codegen.function_node->_static) {
return false;
+ }
- if (codegen.stack_identifiers.has(p_name))
+ if (codegen.stack_identifiers.has(p_name)) {
return false; //shadowed
+ }
return _is_class_member_property(codegen.script, p_name);
}
bool GDScriptCompiler::_is_class_member_property(GDScript *owner, const StringName &p_name) {
-
GDScript *scr = owner;
GDScriptNativeClass *nc = nullptr;
while (scr) {
-
- if (scr->native.is_valid())
+ if (scr->native.is_valid()) {
nc = scr->native.ptr();
+ }
scr = scr->_base;
}
@@ -60,9 +60,9 @@ bool GDScriptCompiler::_is_class_member_property(GDScript *owner, const StringNa
}
void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::Node *p_node) {
-
- if (error != "")
+ if (error != "") {
return;
+ }
error = p_error;
if (p_node) {
@@ -75,12 +75,12 @@ void GDScriptCompiler::_set_error(const String &p_error, const GDScriptParser::N
}
bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level) {
-
ERR_FAIL_COND_V(on->arguments.size() != 1, false);
int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level);
- if (src_address_a < 0)
+ if (src_address_a < 0) {
return false;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); // perform operator
codegen.opcodes.push_back(op); //which operator
@@ -91,18 +91,20 @@ bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptPa
}
bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) {
-
ERR_FAIL_COND_V(on->arguments.size() != 2, false);
int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level, false, p_initializer, p_index_addr);
- if (src_address_a < 0)
+ if (src_address_a < 0) {
return false;
- if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)
+ }
+ if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
p_stack_level++; //uses stack for return, increase stack
+ }
int src_address_b = _parse_expression(codegen, on->arguments[1], p_stack_level, false, p_initializer);
- if (src_address_b < 0)
+ if (src_address_b < 0) {
return false;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_OPERATOR); // perform operator
codegen.opcodes.push_back(op); //which operator
@@ -172,28 +174,44 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D
}
int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr) {
-
Variant::Operator var_op = Variant::OP_MAX;
switch (p_expression->op) {
-
- case GDScriptParser::OperatorNode::OP_ASSIGN_ADD: var_op = Variant::OP_ADD; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_SUB: var_op = Variant::OP_SUBTRACT; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_MUL: var_op = Variant::OP_MULTIPLY; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_DIV: var_op = Variant::OP_DIVIDE; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_MOD: var_op = Variant::OP_MODULE; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: var_op = Variant::OP_SHIFT_LEFT; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: var_op = Variant::OP_SHIFT_RIGHT; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND: var_op = Variant::OP_BIT_AND; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR: var_op = Variant::OP_BIT_OR; break;
- case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR: var_op = Variant::OP_BIT_XOR; break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_ADD:
+ var_op = Variant::OP_ADD;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_SUB:
+ var_op = Variant::OP_SUBTRACT;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_MUL:
+ var_op = Variant::OP_MULTIPLY;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_DIV:
+ var_op = Variant::OP_DIVIDE;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_MOD:
+ var_op = Variant::OP_MODULE;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
+ var_op = Variant::OP_SHIFT_LEFT;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
+ var_op = Variant::OP_SHIFT_RIGHT;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND:
+ var_op = Variant::OP_BIT_AND;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR:
+ var_op = Variant::OP_BIT_OR;
+ break;
+ case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR:
+ var_op = Variant::OP_BIT_XOR;
+ break;
case GDScriptParser::OperatorNode::OP_INIT_ASSIGN:
case GDScriptParser::OperatorNode::OP_ASSIGN: {
-
//none
} break;
default: {
-
ERR_FAIL_V(-1);
}
}
@@ -201,12 +219,12 @@ int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDS
bool initializer = p_expression->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN;
if (var_op == Variant::OP_MAX) {
-
return _parse_expression(codegen, p_expression->arguments[1], p_stack_level, false, initializer);
}
- if (!_create_binary_operator(codegen, p_expression, var_op, p_stack_level, initializer, p_index_addr))
+ if (!_create_binary_operator(codegen, p_expression, var_op, p_stack_level, initializer, p_index_addr)) {
return -1;
+ }
int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
@@ -215,7 +233,6 @@ int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDS
}
int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root, bool p_initializer, int p_index_addr) {
-
switch (p_expression->type) {
//should parse variable declaration and adjust stack accordingly...
case GDScriptParser::Node::TYPE_IDENTIFIER: {
@@ -231,7 +248,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
// TRY STACK!
if (!p_initializer && codegen.stack_identifiers.has(identifier)) {
-
int pos = codegen.stack_identifiers[identifier];
return pos | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS);
}
@@ -249,11 +265,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
//TRY MEMBERS!
if (!codegen.function_node || !codegen.function_node->_static) {
-
// TRY MEMBER VARIABLES!
//static function
if (codegen.script->member_indices.has(identifier)) {
-
int idx = codegen.script->member_indices[identifier].index;
return idx | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS); //argument (stack root)
}
@@ -263,26 +277,23 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
GDScript *owner = codegen.script;
while (owner) {
-
GDScript *scr = owner;
GDScriptNativeClass *nc = nullptr;
while (scr) {
-
if (scr->constants.has(identifier)) {
-
//int idx=scr->constants[identifier];
int idx = codegen.get_name_map_pos(identifier);
return idx | (GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT << GDScriptFunction::ADDR_BITS); //argument (stack root)
}
- if (scr->native.is_valid())
+ if (scr->native.is_valid()) {
nc = scr->native.ptr();
+ }
scr = scr->_base;
}
// CLASS C++ Integer Constant
if (nc) {
-
bool success = false;
int constant = ClassDB::get_integer_constant(nc->get_name(), identifier, &success);
if (success) {
@@ -290,7 +301,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
int idx;
if (!codegen.constant_map.has(key)) {
-
idx = codegen.constant_map.size();
codegen.constant_map[key] = idx;
@@ -306,7 +316,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
}
if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
-
int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier];
return idx | (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
}
@@ -314,7 +323,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
/* TRY GLOBAL CLASSES */
if (ScriptServer::is_global_class(identifier)) {
-
const GDScriptParser::ClassNode *class_node = codegen.class_node;
while (class_node->owner) {
class_node = class_node->owner;
@@ -335,7 +343,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
int idx;
if (!codegen.constant_map.has(key)) {
-
idx = codegen.constant_map.size();
codegen.constant_map[key] = idx;
@@ -348,7 +355,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
#ifdef TOOLS_ENABLED
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
-
int idx = codegen.named_globals.find(identifier);
if (idx == -1) {
idx = codegen.named_globals.size();
@@ -372,7 +378,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
int idx;
if (!codegen.constant_map.has(cn->value)) {
-
idx = codegen.constant_map.size();
codegen.constant_map[cn->value] = idx;
@@ -392,17 +397,16 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
return (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
} break;
case GDScriptParser::Node::TYPE_ARRAY: {
-
const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
Vector<int> values;
int slevel = p_stack_level;
for (int i = 0; i < an->elements.size(); i++) {
-
int ret = _parse_expression(codegen, an->elements[i], slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
codegen.alloc_stack(slevel);
@@ -413,8 +417,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY);
codegen.opcodes.push_back(values.size());
- for (int i = 0; i < values.size(); i++)
+ for (int i = 0; i < values.size(); i++) {
codegen.opcodes.push_back(values[i]);
+ }
int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
@@ -423,17 +428,16 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
case GDScriptParser::Node::TYPE_DICTIONARY: {
-
const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
Vector<int> values;
int slevel = p_stack_level;
for (int i = 0; i < dn->elements.size(); i++) {
-
int ret = _parse_expression(codegen, dn->elements[i].key, slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
codegen.alloc_stack(slevel);
@@ -442,8 +446,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
values.push_back(ret);
ret = _parse_expression(codegen, dn->elements[i].value, slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
codegen.alloc_stack(slevel);
@@ -454,8 +459,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY);
codegen.opcodes.push_back(dn->elements.size());
- for (int i = 0; i < values.size(); i++)
+ for (int i = 0; i < values.size(); i++) {
codegen.opcodes.push_back(values[i]);
+ }
int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
@@ -468,8 +474,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
int slevel = p_stack_level;
int src_addr = _parse_expression(codegen, cn->source_node, slevel);
- if (src_addr < 0)
+ if (src_addr < 0) {
return src_addr;
+ }
if (src_addr & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
slevel++;
codegen.alloc_stack(slevel);
@@ -485,7 +492,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
case GDScriptDataType::NATIVE: {
int class_idx;
if (GDScriptLanguage::get_singleton()->get_global_map().has(cast_type.native_type)) {
-
class_idx = GDScriptLanguage::get_singleton()->get_global_map()[cast_type.native_type];
class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
} else {
@@ -497,7 +503,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
case GDScriptDataType::SCRIPT:
case GDScriptDataType::GDSCRIPT: {
-
Variant script = cast_type.script_type;
int idx = codegen.get_constant_pos(script);
idx |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; //make it a local constant (faster access)
@@ -523,10 +528,8 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
const GDScriptParser::OperatorNode *on = static_cast<const GDScriptParser::OperatorNode *>(p_expression);
switch (on->op) {
-
//call/constructor operator
case GDScriptParser::OperatorNode::OP_PARENT_CALL: {
-
ERR_FAIL_COND_V(on->arguments.size() < 1, -1);
const GDScriptParser::IdentifierNode *in = (const GDScriptParser::IdentifierNode *)on->arguments[0];
@@ -534,10 +537,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
Vector<int> arguments;
int slevel = p_stack_level;
for (int i = 1; i < on->arguments.size(); i++) {
-
int ret = _parse_expression(codegen, on->arguments[i], slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
codegen.alloc_stack(slevel);
@@ -551,12 +554,12 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back(codegen.get_name_map_pos(in->name)); //instance
codegen.opcodes.push_back(arguments.size()); //argument count
codegen.alloc_call(arguments.size());
- for (int i = 0; i < arguments.size(); i++)
+ for (int i = 0; i < arguments.size(); i++) {
codegen.opcodes.push_back(arguments[i]); //arguments
+ }
} break;
case GDScriptParser::OperatorNode::OP_CALL: {
-
if (on->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
//construct a basic type
ERR_FAIL_COND_V(on->arguments.size() < 1, -1);
@@ -567,10 +570,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
Vector<int> arguments;
int slevel = p_stack_level;
for (int i = 1; i < on->arguments.size(); i++) {
-
int ret = _parse_expression(codegen, on->arguments[i], slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
codegen.alloc_stack(slevel);
@@ -583,8 +586,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back(vtype); //instance
codegen.opcodes.push_back(arguments.size()); //argument count
codegen.alloc_call(arguments.size());
- for (int i = 0; i < arguments.size(); i++)
+ for (int i = 0; i < arguments.size(); i++) {
codegen.opcodes.push_back(arguments[i]); //arguments
+ }
} else if (on->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
//built in function
@@ -594,10 +598,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
Vector<int> arguments;
int slevel = p_stack_level;
for (int i = 1; i < on->arguments.size(); i++) {
-
int ret = _parse_expression(codegen, on->arguments[i], slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
@@ -611,8 +615,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back(static_cast<const GDScriptParser::BuiltInFunctionNode *>(on->arguments[0])->function);
codegen.opcodes.push_back(on->arguments.size() - 1);
codegen.alloc_call(on->arguments.size() - 1);
- for (int i = 0; i < arguments.size(); i++)
+ for (int i = 0; i < arguments.size(); i++) {
codegen.opcodes.push_back(arguments[i]);
+ }
} else {
//regular function
@@ -628,14 +633,12 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
int slevel = p_stack_level;
for (int i = 0; i < on->arguments.size(); i++) {
-
int ret;
if (i == 0 && on->arguments[i]->type == GDScriptParser::Node::TYPE_SELF && codegen.function_node && codegen.function_node->_static) {
//static call to self
ret = (GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS);
} else if (i == 1) {
-
if (on->arguments[i]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
_set_error("Attempt to call a non-identifier.", on);
return -1;
@@ -644,10 +647,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
ret = codegen.get_name_map_pos(id->name);
} else {
-
ret = _parse_expression(codegen, on->arguments[i], slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
codegen.alloc_stack(slevel);
@@ -659,21 +662,21 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back(p_root ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN); // perform operator
codegen.opcodes.push_back(on->arguments.size() - 2);
codegen.alloc_call(on->arguments.size() - 2);
- for (int i = 0; i < arguments.size(); i++)
+ for (int i = 0; i < arguments.size(); i++) {
codegen.opcodes.push_back(arguments[i]);
+ }
}
} break;
case GDScriptParser::OperatorNode::OP_YIELD: {
-
ERR_FAIL_COND_V(on->arguments.size() && on->arguments.size() != 2, -1);
Vector<int> arguments;
int slevel = p_stack_level;
for (int i = 0; i < on->arguments.size(); i++) {
-
int ret = _parse_expression(codegen, on->arguments[i], slevel);
- if (ret < 0)
+ if (ret < 0) {
return ret;
+ }
if ((ret >> GDScriptFunction::ADDR_BITS & GDScriptFunction::ADDR_TYPE_STACK) == GDScriptFunction::ADDR_TYPE_STACK) {
slevel++;
codegen.alloc_stack(slevel);
@@ -683,8 +686,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
//push call bytecode
codegen.opcodes.push_back(arguments.size() == 0 ? GDScriptFunction::OPCODE_YIELD : GDScriptFunction::OPCODE_YIELD_SIGNAL); // basic type constructor
- for (int i = 0; i < arguments.size(); i++)
+ for (int i = 0; i < arguments.size(); i++) {
codegen.opcodes.push_back(arguments[i]); //arguments
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_YIELD_RESUME);
//next will be where to place the result :)
@@ -693,22 +697,21 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
//indexing operator
case GDScriptParser::OperatorNode::OP_INDEX:
case GDScriptParser::OperatorNode::OP_INDEX_NAMED: {
-
ERR_FAIL_COND_V(on->arguments.size() != 2, -1);
int slevel = p_stack_level;
bool named = (on->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED);
int from = _parse_expression(codegen, on->arguments[0], slevel);
- if (from < 0)
+ if (from < 0) {
return from;
+ }
int index;
if (p_index_addr != 0) {
index = p_index_addr;
} else if (named) {
if (on->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
-
GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1]);
const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(identifier->name);
@@ -729,7 +732,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
index = codegen.get_name_map_pos(static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1])->name);
} else {
-
if (on->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT && static_cast<const GDScriptParser::ConstantNode *>(on->arguments[1])->value.get_type() == Variant::STRING) {
//also, somehow, named (speed up anyway)
StringName name = static_cast<const GDScriptParser::ConstantNode *>(on->arguments[1])->value;
@@ -744,8 +746,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
}
index = _parse_expression(codegen, on->arguments[1], slevel);
- if (index < 0)
+ if (index < 0) {
return index;
+ }
}
}
@@ -755,20 +758,21 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
case GDScriptParser::OperatorNode::OP_AND: {
-
// AND operator with early out on failure
int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
- if (res < 0)
+ if (res < 0) {
return res;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
codegen.opcodes.push_back(res);
int jump_fail_pos = codegen.opcodes.size();
codegen.opcodes.push_back(0);
res = _parse_expression(codegen, on->arguments[1], p_stack_level);
- if (res < 0)
+ if (res < 0) {
return res;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
codegen.opcodes.push_back(res);
@@ -788,20 +792,21 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
case GDScriptParser::OperatorNode::OP_OR: {
-
// OR operator with early out on success
int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
- if (res < 0)
+ if (res < 0) {
return res;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
codegen.opcodes.push_back(res);
int jump_success_pos = codegen.opcodes.size();
codegen.opcodes.push_back(0);
res = _parse_expression(codegen, on->arguments[1], p_stack_level);
- if (res < 0)
+ if (res < 0) {
return res;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF);
codegen.opcodes.push_back(res);
@@ -822,20 +827,21 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
// ternary operators
case GDScriptParser::OperatorNode::OP_TERNARY_IF: {
-
// x IF a ELSE y operator with early out on failure
int res = _parse_expression(codegen, on->arguments[0], p_stack_level);
- if (res < 0)
+ if (res < 0) {
return res;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
codegen.opcodes.push_back(res);
int jump_fail_pos = codegen.opcodes.size();
codegen.opcodes.push_back(0);
res = _parse_expression(codegen, on->arguments[1], p_stack_level);
- if (res < 0)
+ if (res < 0) {
return res;
+ }
codegen.alloc_stack(p_stack_level); //it will be used..
codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
@@ -847,8 +853,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.write[jump_fail_pos] = codegen.opcodes.size();
res = _parse_expression(codegen, on->arguments[2], p_stack_level);
- if (res < 0)
+ if (res < 0) {
return res;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
codegen.opcodes.push_back(p_stack_level | GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
@@ -861,71 +868,113 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
//unary operators
case GDScriptParser::OperatorNode::OP_NEG: {
- if (!_create_unary_operator(codegen, on, Variant::OP_NEGATE, p_stack_level)) return -1;
+ if (!_create_unary_operator(codegen, on, Variant::OP_NEGATE, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_POS: {
- if (!_create_unary_operator(codegen, on, Variant::OP_POSITIVE, p_stack_level)) return -1;
+ if (!_create_unary_operator(codegen, on, Variant::OP_POSITIVE, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_NOT: {
- if (!_create_unary_operator(codegen, on, Variant::OP_NOT, p_stack_level)) return -1;
+ if (!_create_unary_operator(codegen, on, Variant::OP_NOT, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_BIT_INVERT: {
- if (!_create_unary_operator(codegen, on, Variant::OP_BIT_NEGATE, p_stack_level)) return -1;
+ if (!_create_unary_operator(codegen, on, Variant::OP_BIT_NEGATE, p_stack_level)) {
+ return -1;
+ }
} break;
//binary operators (in precedence order)
case GDScriptParser::OperatorNode::OP_IN: {
- if (!_create_binary_operator(codegen, on, Variant::OP_IN, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_IN, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_EQUAL: {
- if (!_create_binary_operator(codegen, on, Variant::OP_EQUAL, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_EQUAL, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_NOT_EQUAL: {
- if (!_create_binary_operator(codegen, on, Variant::OP_NOT_EQUAL, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_NOT_EQUAL, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_LESS: {
- if (!_create_binary_operator(codegen, on, Variant::OP_LESS, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_LESS, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_LESS_EQUAL: {
- if (!_create_binary_operator(codegen, on, Variant::OP_LESS_EQUAL, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_LESS_EQUAL, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_GREATER: {
- if (!_create_binary_operator(codegen, on, Variant::OP_GREATER, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_GREATER, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: {
- if (!_create_binary_operator(codegen, on, Variant::OP_GREATER_EQUAL, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_GREATER_EQUAL, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_ADD: {
- if (!_create_binary_operator(codegen, on, Variant::OP_ADD, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_ADD, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_SUB: {
- if (!_create_binary_operator(codegen, on, Variant::OP_SUBTRACT, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_SUBTRACT, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_MUL: {
- if (!_create_binary_operator(codegen, on, Variant::OP_MULTIPLY, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_MULTIPLY, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_DIV: {
- if (!_create_binary_operator(codegen, on, Variant::OP_DIVIDE, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_DIVIDE, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_MOD: {
- if (!_create_binary_operator(codegen, on, Variant::OP_MODULE, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_MODULE, p_stack_level)) {
+ return -1;
+ }
} break;
//case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
//case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
case GDScriptParser::OperatorNode::OP_BIT_AND: {
- if (!_create_binary_operator(codegen, on, Variant::OP_BIT_AND, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_BIT_AND, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_BIT_OR: {
- if (!_create_binary_operator(codegen, on, Variant::OP_BIT_OR, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_BIT_OR, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_BIT_XOR: {
- if (!_create_binary_operator(codegen, on, Variant::OP_BIT_XOR, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_BIT_XOR, p_stack_level)) {
+ return -1;
+ }
} break;
//shift
case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: {
- if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_LEFT, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_LEFT, p_stack_level)) {
+ return -1;
+ }
} break;
case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: {
- if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_RIGHT, p_stack_level)) return -1;
+ if (!_create_binary_operator(codegen, on, Variant::OP_SHIFT_RIGHT, p_stack_level)) {
+ return -1;
+ }
} break;
//assignment operators
case GDScriptParser::OperatorNode::OP_ASSIGN_ADD:
@@ -940,18 +989,15 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR:
case GDScriptParser::OperatorNode::OP_INIT_ASSIGN:
case GDScriptParser::OperatorNode::OP_ASSIGN: {
-
ERR_FAIL_COND_V(on->arguments.size() != 2, -1);
if (on->arguments[0]->type == GDScriptParser::Node::TYPE_OPERATOR && (static_cast<GDScriptParser::OperatorNode *>(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX || static_cast<GDScriptParser::OperatorNode *>(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED)) {
-
// SET (chained) MODE!
#ifdef DEBUG_ENABLED
if (static_cast<GDScriptParser::OperatorNode *>(on->arguments[0])->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
const GDScriptParser::OperatorNode *inon = static_cast<GDScriptParser::OperatorNode *>(on->arguments[0]);
if (inon->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
-
const Map<StringName, GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(static_cast<GDScriptParser::IdentifierNode *>(inon->arguments[1])->name);
if (MI && MI->get().setter == codegen.function_node->name) {
String n = static_cast<GDScriptParser::IdentifierNode *>(inon->arguments[1])->name;
@@ -976,13 +1022,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
//create get/set chain
GDScriptParser::OperatorNode *n = op;
while (true) {
-
chain.push_back(n);
if (n->arguments[0]->type != GDScriptParser::Node::TYPE_OPERATOR) {
-
//check for a built-in property
if (n->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
-
GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(n->arguments[0]);
if (_is_class_member_property(codegen, identifier->name)) {
assign_property = identifier->name;
@@ -991,8 +1034,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
break;
}
n = static_cast<GDScriptParser::OperatorNode *>(n->arguments[0]);
- if (n->op != GDScriptParser::OperatorNode::OP_INDEX && n->op != GDScriptParser::OperatorNode::OP_INDEX_NAMED)
+ if (n->op != GDScriptParser::OperatorNode::OP_INDEX && n->op != GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
break;
+ }
}
}
@@ -1000,8 +1044,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
//get at (potential) root stack pos, so it can be returned
int prev_pos = _parse_expression(codegen, chain.back()->get()->arguments[0], slevel);
- if (prev_pos < 0)
+ if (prev_pos < 0) {
return prev_pos;
+ }
int retval = prev_pos;
if (retval & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
@@ -1012,7 +1057,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
Vector<int> setchain;
if (assign_property != StringName()) {
-
// recover and assign at the end, this allows stuff like
// position.x+=2.0
// in Node2D
@@ -1022,20 +1066,18 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
}
for (List<GDScriptParser::OperatorNode *>::Element *E = chain.back(); E; E = E->prev()) {
-
- if (E == chain.front()) //ignore first
+ if (E == chain.front()) { //ignore first
break;
+ }
bool named = E->get()->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED;
int key_idx;
if (named) {
-
key_idx = codegen.get_name_map_pos(static_cast<const GDScriptParser::IdentifierNode *>(E->get()->arguments[1])->name);
//printf("named key %x\n",key_idx);
} else {
-
if (prev_pos & (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)) {
slevel++;
codegen.alloc_stack(slevel);
@@ -1048,8 +1090,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
//stack was raised here if retval was stack but..
}
- if (key_idx < 0) //error
+ if (key_idx < 0) { //error
return key_idx;
+ }
codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_GET_NAMED : GDScriptFunction::OPCODE_GET);
codegen.opcodes.push_back(prev_pos);
@@ -1076,17 +1119,16 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
bool named = false;
if (op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
-
set_index = codegen.get_name_map_pos(static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name);
named = true;
} else {
-
set_index = _parse_expression(codegen, op->arguments[1], slevel + 1);
named = false;
}
- if (set_index < 0) //error
+ if (set_index < 0) { //error
return set_index;
+ }
if (set_index & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
slevel++;
@@ -1094,8 +1136,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
}
int set_value = _parse_assign_right_expression(codegen, on, slevel + 1, named ? 0 : set_index);
- if (set_value < 0) //error
+ if (set_value < 0) { //error
return set_value;
+ }
codegen.opcodes.push_back(named ? GDScriptFunction::OPCODE_SET_NAMED : GDScriptFunction::OPCODE_SET);
codegen.opcodes.push_back(prev_pos);
@@ -1103,7 +1146,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back(set_value);
for (int i = 0; i < setchain.size(); i++) {
-
codegen.opcodes.push_back(setchain[i]);
}
@@ -1115,8 +1157,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
int slevel = p_stack_level;
int src_address = _parse_assign_right_expression(codegen, on, slevel);
- if (src_address < 0)
+ if (src_address < 0) {
return -1;
+ }
StringName name = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[0])->name;
@@ -1126,14 +1169,14 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
} else {
-
//REGULAR ASSIGNMENT MODE!!
int slevel = p_stack_level;
int dst_address_a = _parse_expression(codegen, on->arguments[0], slevel, false, on->op == GDScriptParser::OperatorNode::OP_INIT_ASSIGN);
- if (dst_address_a < 0)
+ if (dst_address_a < 0) {
return -1;
+ }
if (dst_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
slevel++;
@@ -1141,8 +1184,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
}
int src_address_b = _parse_assign_right_expression(codegen, on, slevel);
- if (src_address_b < 0)
+ if (src_address_b < 0) {
return -1;
+ }
GDScriptDataType assign_type = _gdtype_from_datatype(on->arguments[0]->get_datatype());
@@ -1158,7 +1202,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
case GDScriptDataType::NATIVE: {
int class_idx;
if (GDScriptLanguage::get_singleton()->get_global_map().has(assign_type.native_type)) {
-
class_idx = GDScriptLanguage::get_singleton()->get_global_map()[assign_type.native_type];
class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
} else {
@@ -1172,7 +1215,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
case GDScriptDataType::SCRIPT:
case GDScriptDataType::GDSCRIPT: {
-
Variant script = assign_type.script_type;
int idx = codegen.get_constant_pos(script);
idx |= GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS; //make it a local constant (faster access)
@@ -1201,21 +1243,23 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
}
} break;
case GDScriptParser::OperatorNode::OP_IS: {
-
ERR_FAIL_COND_V(on->arguments.size() != 2, false);
int slevel = p_stack_level;
int src_address_a = _parse_expression(codegen, on->arguments[0], slevel);
- if (src_address_a < 0)
+ if (src_address_a < 0) {
return -1;
+ }
- if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)
+ if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
slevel++; //uses stack for return, increase stack
+ }
int src_address_b = _parse_expression(codegen, on->arguments[1], slevel);
- if (src_address_b < 0)
+ if (src_address_b < 0) {
return -1;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_EXTENDS_TEST); // perform operator
codegen.opcodes.push_back(src_address_a); // argument 1
@@ -1229,11 +1273,13 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
int slevel = p_stack_level;
int src_address_a = _parse_expression(codegen, on->arguments[0], slevel);
- if (src_address_a < 0)
+ if (src_address_a < 0) {
return -1;
+ }
- if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS)
+ if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) {
slevel++; //uses stack for return, increase stack
+ }
const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(on->arguments[1]);
@@ -1242,7 +1288,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
codegen.opcodes.push_back((int)tn->vtype); // argument 2 (unary only takes one parameter)
} break;
default: {
-
ERR_FAIL_V_MSG(0, "Bug in bytecode compiler, unexpected operator #" + itos(on->op) + " in parse tree while parsing expression."); //unreachable code
} break;
@@ -1255,20 +1300,17 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
} break;
//TYPE_TYPE,
default: {
-
ERR_FAIL_V_MSG(-1, "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); //unreachable code
} break;
}
}
Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level, int p_break_addr, int p_continue_addr) {
-
codegen.push_stack_identifiers();
int new_identifiers = 0;
codegen.current_line = p_block->line;
for (int i = 0; i < p_block->statements.size(); i++) {
-
const GDScriptParser::Node *s = p_block->statements[i];
switch (s->type) {
@@ -1286,7 +1328,6 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(s);
switch (cf->cf_type) {
-
case GDScriptParser::ControlFlowNode::CF_MATCH: {
GDScriptParser::MatchNode *match = cf->match;
@@ -1360,10 +1401,10 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
} break;
case GDScriptParser::ControlFlowNode::CF_IF: {
-
int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
- if (ret2 < 0)
+ if (ret2 < 0) {
return ERR_PARSE_ERROR;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
codegen.opcodes.push_back(ret2);
@@ -1371,19 +1412,20 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
codegen.opcodes.push_back(0); //temporary
Error err = _parse_block(codegen, cf->body, p_stack_level, p_break_addr, p_continue_addr);
- if (err)
+ if (err) {
return err;
+ }
if (cf->body_else) {
-
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
int end_addr = codegen.opcodes.size();
codegen.opcodes.push_back(0);
codegen.opcodes.write[else_addr] = codegen.opcodes.size();
Error err2 = _parse_block(codegen, cf->body_else, p_stack_level, p_break_addr, p_continue_addr);
- if (err2)
+ if (err2) {
return err2;
+ }
codegen.opcodes.write[end_addr] = codegen.opcodes.size();
} else {
@@ -1393,7 +1435,6 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
} break;
case GDScriptParser::ControlFlowNode::CF_FOR: {
-
int slevel = p_stack_level;
int iter_stack_pos = slevel;
int iterator_pos = (slevel++) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
@@ -1405,8 +1446,9 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
codegen.add_stack_identifier(static_cast<const GDScriptParser::IdentifierNode *>(cf->arguments[0])->name, iter_stack_pos);
int ret2 = _parse_expression(codegen, cf->arguments[1], slevel, false);
- if (ret2 < 0)
+ if (ret2 < 0) {
return ERR_COMPILATION_FAILED;
+ }
//assign container
codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSIGN);
@@ -1434,8 +1476,9 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
codegen.opcodes.push_back(iterator_pos);
Error err = _parse_block(codegen, cf->body, slevel, break_pos, continue_pos);
- if (err)
+ if (err) {
return err;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
codegen.opcodes.push_back(continue_pos);
@@ -1445,7 +1488,6 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
} break;
case GDScriptParser::ControlFlowNode::CF_WHILE: {
-
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
codegen.opcodes.push_back(codegen.opcodes.size() + 3);
int break_addr = codegen.opcodes.size();
@@ -1454,14 +1496,16 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
int continue_addr = codegen.opcodes.size();
int ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
- if (ret2 < 0)
+ if (ret2 < 0) {
return ERR_PARSE_ERROR;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_IF_NOT);
codegen.opcodes.push_back(ret2);
codegen.opcodes.push_back(break_addr);
Error err = _parse_block(codegen, cf->body, p_stack_level, break_addr, continue_addr);
- if (err)
+ if (err) {
return err;
+ }
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP);
codegen.opcodes.push_back(continue_addr);
@@ -1469,9 +1513,7 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
} break;
case GDScriptParser::ControlFlowNode::CF_BREAK: {
-
if (p_break_addr < 0) {
-
_set_error("'break'' not within loop", cf);
return ERR_COMPILATION_FAILED;
}
@@ -1480,9 +1522,7 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
} break;
case GDScriptParser::ControlFlowNode::CF_CONTINUE: {
-
if (p_continue_addr < 0) {
-
_set_error("'continue' not within loop", cf);
return ERR_COMPILATION_FAILED;
}
@@ -1492,17 +1532,15 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
} break;
case GDScriptParser::ControlFlowNode::CF_RETURN: {
-
int ret2;
if (cf->arguments.size()) {
-
ret2 = _parse_expression(codegen, cf->arguments[0], p_stack_level, false);
- if (ret2 < 0)
+ if (ret2 < 0) {
return ERR_PARSE_ERROR;
+ }
} else {
-
ret2 = GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
}
@@ -1519,14 +1557,16 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
const GDScriptParser::AssertNode *as = static_cast<const GDScriptParser::AssertNode *>(s);
int ret2 = _parse_expression(codegen, as->condition, p_stack_level, false);
- if (ret2 < 0)
+ if (ret2 < 0) {
return ERR_PARSE_ERROR;
+ }
int message_ret = 0;
if (as->message) {
message_ret = _parse_expression(codegen, as->message, p_stack_level + 1, false);
- if (message_ret < 0)
+ if (message_ret < 0) {
return ERR_PARSE_ERROR;
+ }
}
codegen.opcodes.push_back(GDScriptFunction::OPCODE_ASSERT);
@@ -1541,7 +1581,6 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
#endif
} break;
case GDScriptParser::Node::TYPE_LOCAL_VAR: {
-
const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(s);
// since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
@@ -1559,8 +1598,9 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
default: {
//expression
int ret2 = _parse_expression(codegen, s, p_stack_level, true);
- if (ret2 < 0)
+ if (ret2 < 0) {
return ERR_PARSE_ERROR;
+ }
} break;
}
}
@@ -1569,7 +1609,6 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
}
Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready) {
-
Vector<int> bytecode;
CodeGen codegen;
@@ -1610,7 +1649,6 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
if (is_initializer || (p_func && String(p_func->name) == "_init")) {
//parse initializer for class members
if (!p_func && p_class->extends_used && p_script->native.is_null()) {
-
//call implicit parent constructor
codegen.opcodes.push_back(GDScriptFunction::OPCODE_CALL_SELF_BASE);
codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
@@ -1618,8 +1656,9 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
codegen.opcodes.push_back((GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) | 0);
}
Error err = _parse_block(codegen, p_class->initializer, stack_level);
- if (err)
+ if (err) {
return err;
+ }
is_initializer = true;
}
@@ -1627,8 +1666,9 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
//parse initializer for class members
if (p_class->ready->statements.size()) {
Error err = _parse_block(codegen, p_class->ready, stack_level);
- if (err)
+ if (err) {
return err;
+ }
}
}
@@ -1638,13 +1678,10 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
StringName func_name;
if (p_func) {
-
if (p_func->default_values.size()) {
-
codegen.opcodes.push_back(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
defarg_addr.push_back(codegen.opcodes.size());
for (int i = 0; i < p_func->default_values.size(); i++) {
-
_parse_expression(codegen, p_func->default_values[i], stack_level, true);
defarg_addr.push_back(codegen.opcodes.size());
}
@@ -1653,15 +1690,17 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
}
Error err = _parse_block(codegen, p_func->body, stack_level);
- if (err)
+ if (err) {
return err;
+ }
func_name = p_func->name;
} else {
- if (p_for_ready)
+ if (p_for_ready) {
func_name = "_ready";
- else
+ } else {
func_name = "_init";
+ }
}
codegen.opcodes.push_back(GDScriptFunction::OPCODE_END);
@@ -1706,17 +1745,14 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
gdfunc->constants.write[idx] = *K;
}
} else {
-
gdfunc->_constants_ptr = nullptr;
gdfunc->_constant_count = 0;
}
//global names
if (codegen.name_map.size()) {
-
gdfunc->global_names.resize(codegen.name_map.size());
gdfunc->_global_names_ptr = &gdfunc->global_names[0];
for (Map<StringName, int>::Element *E = codegen.name_map.front(); E; E = E->next()) {
-
gdfunc->global_names.write[E->get()] = E->key();
}
gdfunc->_global_names_count = gdfunc->global_names.size();
@@ -1739,19 +1775,16 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
#endif
if (codegen.opcodes.size()) {
-
gdfunc->code = codegen.opcodes;
gdfunc->_code_ptr = &gdfunc->code[0];
gdfunc->_code_size = codegen.opcodes.size();
} else {
-
gdfunc->_code_ptr = nullptr;
gdfunc->_code_size = 0;
}
if (defarg_addr.size()) {
-
gdfunc->default_arguments = defarg_addr;
gdfunc->_default_arg_count = defarg_addr.size() - 1;
gdfunc->_default_arg_ptr = &gdfunc->default_arguments[0];
@@ -1768,8 +1801,9 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
if (EngineDebugger::is_active()) {
String signature;
//path
- if (p_script->get_path() != String())
+ if (p_script->get_path() != String()) {
signature += p_script->get_path();
+ }
//loc
if (p_func) {
signature += "::" + itos(p_func->body->line);
@@ -1809,17 +1843,18 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
gdfunc->_initial_line = 0;
}
- if (codegen.debug_stack)
+ if (codegen.debug_stack) {
gdfunc->stack_debug = codegen.stack_debug;
+ }
- if (is_initializer)
+ if (is_initializer) {
p_script->initializer = gdfunc;
+ }
return OK;
}
Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
-
parsing_classes.insert(p_script);
if (p_class->owner && p_class->owner->owner) {
@@ -1890,7 +1925,6 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar
}
for (int i = 0; i < p_class->variables.size(); i++) {
-
StringName name = p_class->variables[i].identifier;
GDScript::MemberInfo minfo;
@@ -1905,7 +1939,6 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar
PropertyInfo export_info = p_class->variables[i]._export;
if (export_info.type != Variant::NIL) {
-
if (!minfo.data_type.has_type) {
prop_info.type = export_info.type;
prop_info.class_name = export_info.class_name;
@@ -1932,7 +1965,6 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar
}
for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
-
StringName name = E->key();
ERR_CONTINUE(E->get().expression->type != GDScriptParser::Node::TYPE_CONSTANT);
@@ -1947,13 +1979,11 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar
}
for (int i = 0; i < p_class->_signals.size(); i++) {
-
StringName name = p_class->_signals[i].name;
GDScript *c = p_script;
while (c) {
-
if (c->_signals.has(name)) {
_set_error("Signal '" + name + "' redefined (in current or parent class)", p_class);
return ERR_ALREADY_EXISTS;
@@ -1989,8 +2019,9 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar
// Subclass might still be parsing, just skip it
if (!parsed_classes.has(subclass_ptr) && !parsing_classes.has(subclass_ptr)) {
Error err = _parse_class_level(subclass_ptr, p_class->subclasses[i], p_keep_state);
- if (err)
+ if (err) {
return err;
+ }
}
#ifdef TOOLS_ENABLED
@@ -2011,37 +2042,41 @@ Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptPa
bool has_ready = false;
for (int i = 0; i < p_class->functions.size(); i++) {
-
- if (!has_initializer && p_class->functions[i]->name == "_init")
+ if (!has_initializer && p_class->functions[i]->name == "_init") {
has_initializer = true;
- if (!has_ready && p_class->functions[i]->name == "_ready")
+ }
+ if (!has_ready && p_class->functions[i]->name == "_ready") {
has_ready = true;
+ }
Error err = _parse_function(p_script, p_class, p_class->functions[i]);
- if (err)
+ if (err) {
return err;
+ }
}
//parse static methods
for (int i = 0; i < p_class->static_functions.size(); i++) {
-
Error err = _parse_function(p_script, p_class, p_class->static_functions[i]);
- if (err)
+ if (err) {
return err;
+ }
}
if (!has_initializer) {
//create a constructor
Error err = _parse_function(p_script, p_class, nullptr);
- if (err)
+ if (err) {
return err;
+ }
}
if (!has_ready && p_class->ready->statements.size()) {
//create a constructor
Error err = _parse_function(p_script, p_class, nullptr, true);
- if (err)
+ if (err) {
return err;
+ }
}
#ifdef DEBUG_ENABLED
@@ -2050,7 +2085,6 @@ Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptPa
if (p_keep_state) {
for (Set<Object *>::Element *E = p_script->instances.front(); E;) {
-
Set<Object *>::Element *N = E->next();
ScriptInstance *si = E->get()->get_script_instance();
@@ -2085,7 +2119,6 @@ Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptPa
}
#endif
} else {
-
GDScriptInstance *gi = static_cast<GDScriptInstance *>(si);
gi->reload_members();
}
@@ -2110,7 +2143,6 @@ Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptPa
}
void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
-
Map<StringName, Ref<GDScript>> old_subclasses;
if (p_keep_state) {
@@ -2145,7 +2177,6 @@ void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::C
}
Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) {
-
err_line = -1;
err_column = -1;
error = "";
@@ -2165,27 +2196,28 @@ Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_scri
p_script->_owner = nullptr;
Error err = _parse_class_level(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
- if (err)
+ if (err) {
return err;
+ }
err = _parse_class_blocks(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state);
- if (err)
+ if (err) {
return err;
+ }
return OK;
}
String GDScriptCompiler::get_error() const {
-
return error;
}
-int GDScriptCompiler::get_error_line() const {
+int GDScriptCompiler::get_error_line() const {
return err_line;
}
-int GDScriptCompiler::get_error_column() const {
+int GDScriptCompiler::get_error_column() const {
return err_column;
}