2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-11-16 17:38:18 +00:00
|
|
|
/* gdscript_compiler.h */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
#ifndef GDSCRIPT_COMPILER_H
|
|
|
|
#define GDSCRIPT_COMPILER_H
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-05-30 02:16:52 +00:00
|
|
|
#include "core/set.h"
|
2017-11-16 17:38:18 +00:00
|
|
|
#include "gdscript.h"
|
2020-05-01 22:14:56 +00:00
|
|
|
#include "gdscript_function.h"
|
2017-11-16 17:38:18 +00:00
|
|
|
#include "gdscript_parser.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
class GDScriptCompiler {
|
|
|
|
const GDScriptParser *parser;
|
2019-03-16 16:49:29 +00:00
|
|
|
Set<GDScript *> parsed_classes;
|
|
|
|
Set<GDScript *> parsing_classes;
|
2018-05-30 02:16:57 +00:00
|
|
|
GDScript *main_script;
|
2014-02-10 01:10:30 +00:00
|
|
|
struct CodeGen {
|
|
|
|
GDScript *script;
|
2017-11-16 17:38:18 +00:00
|
|
|
const GDScriptParser::ClassNode *class_node;
|
|
|
|
const GDScriptParser::FunctionNode *function_node;
|
2020-06-01 19:41:05 +00:00
|
|
|
StringName function_name;
|
2015-01-14 15:37:24 +00:00
|
|
|
bool debug_stack;
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
List<Map<StringName, int>> stack_id_stack;
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<StringName, int> stack_identifiers;
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
List<GDScriptFunction::StackDebug> stack_debug;
|
2020-03-17 06:33:00 +00:00
|
|
|
List<Map<StringName, int>> block_identifier_stack;
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<StringName, int> block_identifiers;
|
2020-05-01 22:14:56 +00:00
|
|
|
Map<StringName, int> local_named_constants;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
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;
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptFunction::StackDebug sd;
|
2017-03-05 15:44:50 +00:00
|
|
|
sd.added = true;
|
|
|
|
sd.line = current_line;
|
|
|
|
sd.identifier = p_id;
|
|
|
|
sd.pos = p_stackpos;
|
|
|
|
stack_debug.push_back(sd);
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 15:37:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void push_stack_identifiers() {
|
|
|
|
stack_id_stack.push_back(stack_identifiers);
|
|
|
|
if (debug_stack) {
|
|
|
|
block_identifier_stack.push_back(block_identifiers);
|
|
|
|
block_identifiers.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_stack_identifiers() {
|
|
|
|
stack_identifiers = stack_id_stack.back()->get();
|
|
|
|
stack_id_stack.pop_back();
|
|
|
|
|
|
|
|
if (debug_stack) {
|
|
|
|
for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptFunction::StackDebug sd;
|
2017-03-05 15:44:50 +00:00
|
|
|
sd.added = false;
|
|
|
|
sd.identifier = E->key();
|
|
|
|
sd.line = current_line;
|
|
|
|
sd.pos = E->get();
|
|
|
|
stack_debug.push_back(sd);
|
|
|
|
}
|
|
|
|
block_identifiers = block_identifier_stack.back()->get();
|
|
|
|
block_identifier_stack.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 15:37:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
|
|
|
|
Map<StringName, int> name_map;
|
2018-05-01 14:06:23 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
Vector<StringName> named_globals;
|
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int get_name_map_pos(const StringName &p_identifier) {
|
2014-02-10 01:10:30 +00:00
|
|
|
int ret;
|
|
|
|
if (!name_map.has(p_identifier)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ret = name_map.size();
|
|
|
|
name_map[p_identifier] = ret;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
ret = name_map[p_identifier];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int get_constant_pos(const Variant &p_constant) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (constant_map.has(p_constant)) {
|
2020-05-01 22:14:56 +00:00
|
|
|
return constant_map[p_constant] | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
int pos = constant_map.size();
|
2017-03-05 15:44:50 +00:00
|
|
|
constant_map[p_constant] = pos;
|
2020-05-01 22:14:56 +00:00
|
|
|
return pos | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<int> opcodes;
|
2017-03-05 15:44:50 +00:00
|
|
|
void alloc_stack(int p_level) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_level >= stack_max) {
|
2020-05-10 10:56:01 +00:00
|
|
|
stack_max = p_level + 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
|
|
|
void alloc_call(int p_params) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_params >= call_max) {
|
2020-05-10 10:56:01 +00:00
|
|
|
call_max = p_params;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int current_line;
|
2014-02-10 01:10:30 +00:00
|
|
|
int stack_max;
|
|
|
|
int call_max;
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
|
|
|
|
bool _is_class_member_property(GDScript *owner, const StringName &p_name);
|
2017-01-04 20:37:45 +00:00
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::UnaryOpNode *on, Variant::Operator op, int p_stack_level);
|
|
|
|
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);
|
|
|
|
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);
|
2020-07-16 01:02:44 +00:00
|
|
|
bool _generate_typed_assign(CodeGen &codegen, int p_src_address, int p_dst_address, const GDScriptDataType &p_datatype, const GDScriptParser::DataType &p_value_type);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-05-30 02:16:57 +00:00
|
|
|
GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const;
|
|
|
|
|
2020-05-01 22:14:56 +00:00
|
|
|
int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::AssignmentNode *p_assignment, int p_stack_level, int p_index_addr = 0);
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
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);
|
2018-11-24 22:46:13 +00:00
|
|
|
Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false);
|
2020-06-01 19:41:05 +00:00
|
|
|
Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
|
2019-03-16 16:49:29 +00:00
|
|
|
Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
|
2018-11-24 22:46:13 +00:00
|
|
|
Error _parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
|
2019-03-16 16:49:29 +00:00
|
|
|
void _make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
|
2014-02-10 01:10:30 +00:00
|
|
|
int err_line;
|
|
|
|
int err_column;
|
|
|
|
StringName source;
|
|
|
|
String error;
|
2020-05-01 22:14:56 +00:00
|
|
|
bool within_await = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
public:
|
2018-11-24 22:46:13 +00:00
|
|
|
Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String get_error() const;
|
|
|
|
int get_error_line() const;
|
|
|
|
int get_error_column() const;
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
GDScriptCompiler();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-11-16 17:38:18 +00:00
|
|
|
#endif // GDSCRIPT_COMPILER_H
|