You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-05 12:10:55 +00:00
Change GDScript compiler to use codegen abstraction
This commit is contained in:
@@ -505,6 +505,9 @@ void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_clas
|
|||||||
member.variable->set_datatype(datatype); // Allow recursive usage.
|
member.variable->set_datatype(datatype); // Allow recursive usage.
|
||||||
reduce_expression(member.variable->initializer);
|
reduce_expression(member.variable->initializer);
|
||||||
datatype = member.variable->initializer->get_datatype();
|
datatype = member.variable->initializer->get_datatype();
|
||||||
|
if (datatype.type_source != GDScriptParser::DataType::UNDETECTED) {
|
||||||
|
datatype.type_source = GDScriptParser::DataType::INFERRED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (member.variable->datatype_specifier != nullptr) {
|
if (member.variable->datatype_specifier != nullptr) {
|
||||||
@@ -540,6 +543,7 @@ void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_clas
|
|||||||
} else if (datatype.builtin_type == Variant::NIL) {
|
} else if (datatype.builtin_type == Variant::NIL) {
|
||||||
push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer);
|
push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer);
|
||||||
}
|
}
|
||||||
|
datatype.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
|
||||||
}
|
}
|
||||||
|
|
||||||
datatype.is_constant = false;
|
datatype.is_constant = false;
|
||||||
@@ -914,6 +918,7 @@ void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScript
|
|||||||
p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
|
p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
|
||||||
} else {
|
} else {
|
||||||
p_suite->set_datatype(p_statement->get_datatype());
|
p_suite->set_datatype(p_statement->get_datatype());
|
||||||
|
p_suite->datatype.type_source = GDScriptParser::DataType::INFERRED;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -33,109 +33,88 @@
|
|||||||
|
|
||||||
#include "core/set.h"
|
#include "core/set.h"
|
||||||
#include "gdscript.h"
|
#include "gdscript.h"
|
||||||
|
#include "gdscript_codegen.h"
|
||||||
#include "gdscript_function.h"
|
#include "gdscript_function.h"
|
||||||
#include "gdscript_parser.h"
|
#include "gdscript_parser.h"
|
||||||
|
|
||||||
class GDScriptCompiler {
|
class GDScriptCompiler {
|
||||||
const GDScriptParser *parser;
|
const GDScriptParser *parser = nullptr;
|
||||||
Set<GDScript *> parsed_classes;
|
Set<GDScript *> parsed_classes;
|
||||||
Set<GDScript *> parsing_classes;
|
Set<GDScript *> parsing_classes;
|
||||||
GDScript *main_script;
|
GDScript *main_script = nullptr;
|
||||||
|
|
||||||
struct CodeGen {
|
struct CodeGen {
|
||||||
GDScript *script;
|
GDScript *script = nullptr;
|
||||||
const GDScriptParser::ClassNode *class_node;
|
const GDScriptParser::ClassNode *class_node = nullptr;
|
||||||
const GDScriptParser::FunctionNode *function_node;
|
const GDScriptParser::FunctionNode *function_node = nullptr;
|
||||||
StringName function_name;
|
StringName function_name;
|
||||||
bool debug_stack;
|
GDScriptCodeGenerator *generator = nullptr;
|
||||||
|
Map<StringName, GDScriptCodeGenerator::Address> parameters;
|
||||||
|
Map<StringName, GDScriptCodeGenerator::Address> locals;
|
||||||
|
List<Set<StringName>> locals_in_scope;
|
||||||
|
|
||||||
List<Map<StringName, int>> stack_id_stack;
|
GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) {
|
||||||
Map<StringName, int> stack_identifiers;
|
uint32_t addr = generator->add_local(p_name, p_type);
|
||||||
|
locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type);
|
||||||
List<GDScriptFunction::StackDebug> stack_debug;
|
locals_in_scope.back()->get().insert(p_name);
|
||||||
List<Map<StringName, int>> block_identifier_stack;
|
return locals[p_name];
|
||||||
Map<StringName, int> block_identifiers;
|
|
||||||
Map<StringName, int> local_named_constants;
|
|
||||||
|
|
||||||
void add_stack_identifier(const StringName &p_id, int p_stackpos) {
|
|
||||||
stack_identifiers[p_id] = p_stackpos;
|
|
||||||
if (debug_stack) {
|
|
||||||
block_identifiers[p_id] = p_stackpos;
|
|
||||||
GDScriptFunction::StackDebug sd;
|
|
||||||
sd.added = true;
|
|
||||||
sd.line = current_line;
|
|
||||||
sd.identifier = p_id;
|
|
||||||
sd.pos = p_stackpos;
|
|
||||||
stack_debug.push_back(sd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_stack_identifiers() {
|
GDScriptCodeGenerator::Address add_local_constant(const StringName &p_name, const Variant &p_value) {
|
||||||
stack_id_stack.push_back(stack_identifiers);
|
uint32_t addr = generator->add_local_constant(p_name, p_value);
|
||||||
if (debug_stack) {
|
locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_CONSTANT, addr);
|
||||||
block_identifier_stack.push_back(block_identifiers);
|
return locals[p_name];
|
||||||
block_identifiers.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop_stack_identifiers() {
|
GDScriptCodeGenerator::Address add_temporary(const GDScriptDataType &p_type = GDScriptDataType()) {
|
||||||
stack_identifiers = stack_id_stack.back()->get();
|
uint32_t addr = generator->add_temporary();
|
||||||
stack_id_stack.pop_back();
|
return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::TEMPORARY, addr, p_type);
|
||||||
|
}
|
||||||
|
|
||||||
if (debug_stack) {
|
GDScriptCodeGenerator::Address add_constant(const Variant &p_constant) {
|
||||||
for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
|
GDScriptDataType type;
|
||||||
GDScriptFunction::StackDebug sd;
|
type.has_type = true;
|
||||||
sd.added = false;
|
type.kind = GDScriptDataType::BUILTIN;
|
||||||
sd.identifier = E->key();
|
type.builtin_type = p_constant.get_type();
|
||||||
sd.line = current_line;
|
if (type.builtin_type == Variant::OBJECT) {
|
||||||
sd.pos = E->get();
|
Object *obj = p_constant;
|
||||||
stack_debug.push_back(sd);
|
if (obj) {
|
||||||
|
type.kind = GDScriptDataType::NATIVE;
|
||||||
|
type.native_type = obj->get_class_name();
|
||||||
|
|
||||||
|
Ref<Script> script = obj->get_script();
|
||||||
|
if (script.is_valid()) {
|
||||||
|
type.script_type = script;
|
||||||
|
Ref<GDScript> gdscript = script;
|
||||||
|
if (gdscript.is_valid()) {
|
||||||
|
type.kind = GDScriptDataType::GDSCRIPT;
|
||||||
|
} else {
|
||||||
|
type.kind = GDScriptDataType::SCRIPT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
type.builtin_type = Variant::NIL;
|
||||||
}
|
}
|
||||||
block_identifiers = block_identifier_stack.back()->get();
|
|
||||||
block_identifier_stack.pop_back();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t addr = generator->add_or_get_constant(p_constant);
|
||||||
|
return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
|
void start_block() {
|
||||||
Map<StringName, int> name_map;
|
Set<StringName> scope;
|
||||||
#ifdef TOOLS_ENABLED
|
locals_in_scope.push_back(scope);
|
||||||
Vector<StringName> named_globals;
|
generator->start_block();
|
||||||
#endif
|
|
||||||
|
|
||||||
int get_name_map_pos(const StringName &p_identifier) {
|
|
||||||
int ret;
|
|
||||||
if (!name_map.has(p_identifier)) {
|
|
||||||
ret = name_map.size();
|
|
||||||
name_map[p_identifier] = ret;
|
|
||||||
} else {
|
|
||||||
ret = name_map[p_identifier];
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_constant_pos(const Variant &p_constant) {
|
void end_block() {
|
||||||
if (constant_map.has(p_constant)) {
|
Set<StringName> &scope = locals_in_scope.back()->get();
|
||||||
return constant_map[p_constant] | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
|
for (Set<StringName>::Element *E = scope.front(); E; E = E->next()) {
|
||||||
|
locals.erase(E->get());
|
||||||
}
|
}
|
||||||
int pos = constant_map.size();
|
locals_in_scope.pop_back();
|
||||||
constant_map[p_constant] = pos;
|
generator->end_block();
|
||||||
return pos | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<int> opcodes;
|
|
||||||
void alloc_stack(int p_level) {
|
|
||||||
if (p_level >= stack_max) {
|
|
||||||
stack_max = p_level + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void alloc_call(int p_params) {
|
|
||||||
if (p_params >= call_max) {
|
|
||||||
call_max = p_params;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int current_line;
|
|
||||||
int stack_max;
|
|
||||||
int call_max;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
|
bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
|
||||||
@@ -143,17 +122,16 @@ class GDScriptCompiler {
|
|||||||
|
|
||||||
void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
|
void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
|
||||||
|
|
||||||
bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::UnaryOpNode *on, Variant::Operator op, int p_stack_level);
|
Error _create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||||
bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0);
|
Error _create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||||
bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0);
|
|
||||||
bool _generate_typed_assign(CodeGen &codegen, int p_src_address, int p_dst_address, const GDScriptDataType &p_datatype, const GDScriptParser::DataType &p_value_type);
|
|
||||||
|
|
||||||
GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const;
|
GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const;
|
||||||
|
|
||||||
int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::AssignmentNode *p_assignment, int p_stack_level, int p_index_addr = 0);
|
GDScriptCodeGenerator::Address _parse_assign_right_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::AssignmentNode *p_assignmentint, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||||
int _parse_expression(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false, int p_index_addr = 0);
|
GDScriptCodeGenerator::Address _parse_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::ExpressionNode *p_expression, bool p_root = false, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
|
||||||
Error _parse_match_pattern(CodeGen &codegen, const GDScriptParser::PatternNode *p_pattern, int p_stack_level, int p_value_addr, int p_type_addr, int &r_bound_variables, Vector<int> &r_patch_addresses, Vector<int> &r_block_patch_address);
|
GDScriptCodeGenerator::Address _parse_match_pattern(CodeGen &codegen, Error &r_error, const GDScriptParser::PatternNode *p_pattern, const GDScriptCodeGenerator::Address &p_value_addr, const GDScriptCodeGenerator::Address &p_type_addr, const GDScriptCodeGenerator::Address &p_previous_test, bool p_is_first, bool p_is_nested);
|
||||||
Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1);
|
void _add_locals_in_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block);
|
||||||
|
Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals = true);
|
||||||
Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false);
|
Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false);
|
||||||
Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
|
Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
|
||||||
Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
|
Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
|
||||||
|
|||||||
@@ -1476,7 +1476,9 @@ GDScriptParser::ContinueNode *GDScriptParser::parse_continue() {
|
|||||||
}
|
}
|
||||||
current_suite->has_continue = true;
|
current_suite->has_continue = true;
|
||||||
end_statement(R"("continue")");
|
end_statement(R"("continue")");
|
||||||
return alloc_node<ContinueNode>();
|
ContinueNode *cont = alloc_node<ContinueNode>();
|
||||||
|
cont->is_for_match = is_continue_match;
|
||||||
|
return cont;
|
||||||
}
|
}
|
||||||
|
|
||||||
GDScriptParser::ForNode *GDScriptParser::parse_for() {
|
GDScriptParser::ForNode *GDScriptParser::parse_for() {
|
||||||
@@ -1495,10 +1497,12 @@ GDScriptParser::ForNode *GDScriptParser::parse_for() {
|
|||||||
// Save break/continue state.
|
// Save break/continue state.
|
||||||
bool could_break = can_break;
|
bool could_break = can_break;
|
||||||
bool could_continue = can_continue;
|
bool could_continue = can_continue;
|
||||||
|
bool was_continue_match = is_continue_match;
|
||||||
|
|
||||||
// Allow break/continue.
|
// Allow break/continue.
|
||||||
can_break = true;
|
can_break = true;
|
||||||
can_continue = true;
|
can_continue = true;
|
||||||
|
is_continue_match = false;
|
||||||
|
|
||||||
SuiteNode *suite = alloc_node<SuiteNode>();
|
SuiteNode *suite = alloc_node<SuiteNode>();
|
||||||
if (n_for->variable) {
|
if (n_for->variable) {
|
||||||
@@ -1511,6 +1515,7 @@ GDScriptParser::ForNode *GDScriptParser::parse_for() {
|
|||||||
// Reset break/continue state.
|
// Reset break/continue state.
|
||||||
can_break = could_break;
|
can_break = could_break;
|
||||||
can_continue = could_continue;
|
can_continue = could_continue;
|
||||||
|
is_continue_match = was_continue_match;
|
||||||
|
|
||||||
return n_for;
|
return n_for;
|
||||||
}
|
}
|
||||||
@@ -1645,8 +1650,10 @@ GDScriptParser::MatchBranchNode *GDScriptParser::parse_match_branch() {
|
|||||||
|
|
||||||
// Save continue state.
|
// Save continue state.
|
||||||
bool could_continue = can_continue;
|
bool could_continue = can_continue;
|
||||||
|
bool was_continue_match = is_continue_match;
|
||||||
// Allow continue for match.
|
// Allow continue for match.
|
||||||
can_continue = true;
|
can_continue = true;
|
||||||
|
is_continue_match = true;
|
||||||
|
|
||||||
SuiteNode *suite = alloc_node<SuiteNode>();
|
SuiteNode *suite = alloc_node<SuiteNode>();
|
||||||
if (branch->patterns.size() > 0) {
|
if (branch->patterns.size() > 0) {
|
||||||
@@ -1663,6 +1670,7 @@ GDScriptParser::MatchBranchNode *GDScriptParser::parse_match_branch() {
|
|||||||
|
|
||||||
// Restore continue state.
|
// Restore continue state.
|
||||||
can_continue = could_continue;
|
can_continue = could_continue;
|
||||||
|
is_continue_match = was_continue_match;
|
||||||
|
|
||||||
return branch;
|
return branch;
|
||||||
}
|
}
|
||||||
@@ -1820,16 +1828,19 @@ GDScriptParser::WhileNode *GDScriptParser::parse_while() {
|
|||||||
// Save break/continue state.
|
// Save break/continue state.
|
||||||
bool could_break = can_break;
|
bool could_break = can_break;
|
||||||
bool could_continue = can_continue;
|
bool could_continue = can_continue;
|
||||||
|
bool was_continue_match = is_continue_match;
|
||||||
|
|
||||||
// Allow break/continue.
|
// Allow break/continue.
|
||||||
can_break = true;
|
can_break = true;
|
||||||
can_continue = true;
|
can_continue = true;
|
||||||
|
is_continue_match = false;
|
||||||
|
|
||||||
n_while->loop = parse_suite(R"("while" block)");
|
n_while->loop = parse_suite(R"("while" block)");
|
||||||
|
|
||||||
// Reset break/continue state.
|
// Reset break/continue state.
|
||||||
can_break = could_break;
|
can_break = could_break;
|
||||||
can_continue = could_continue;
|
can_continue = could_continue;
|
||||||
|
is_continue_match = was_continue_match;
|
||||||
|
|
||||||
return n_while;
|
return n_while;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -609,6 +609,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct ContinueNode : public Node {
|
struct ContinueNode : public Node {
|
||||||
|
bool is_for_match = false;
|
||||||
ContinueNode() {
|
ContinueNode() {
|
||||||
type = CONTINUE;
|
type = CONTINUE;
|
||||||
}
|
}
|
||||||
@@ -1079,6 +1080,7 @@ private:
|
|||||||
bool panic_mode = false;
|
bool panic_mode = false;
|
||||||
bool can_break = false;
|
bool can_break = false;
|
||||||
bool can_continue = false;
|
bool can_continue = false;
|
||||||
|
bool is_continue_match = false; // Whether a `continue` will act on a `match`.
|
||||||
bool is_ignoring_warnings = false;
|
bool is_ignoring_warnings = false;
|
||||||
List<bool> multiline_stack;
|
List<bool> multiline_stack;
|
||||||
|
|
||||||
|
|||||||
@@ -1039,7 +1039,7 @@ void GDScriptTokenizer::check_indent() {
|
|||||||
// First time indenting, choose character now.
|
// First time indenting, choose character now.
|
||||||
indent_char = current_indent_char;
|
indent_char = current_indent_char;
|
||||||
} else if (current_indent_char != indent_char) {
|
} else if (current_indent_char != indent_char) {
|
||||||
Token error = make_error(vformat("Used \"%c\" for indentation instead \"%c\" as used before in the file.", String(¤t_indent_char, 1).c_escape(), String(&indent_char, 1).c_escape()));
|
Token error = make_error(vformat("Used \"%s\" for indentation instead \"%s\" as used before in the file.", String(¤t_indent_char, 1).c_escape(), String(&indent_char, 1).c_escape()));
|
||||||
error.start_line = line;
|
error.start_line = line;
|
||||||
error.start_column = 1;
|
error.start_column = 1;
|
||||||
error.leftmost_column = 1;
|
error.leftmost_column = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user