Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_compiler.h
10277 views
1
/**************************************************************************/
2
/* gdscript_compiler.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "gdscript.h"
34
#include "gdscript_codegen.h"
35
#include "gdscript_function.h"
36
#include "gdscript_parser.h"
37
38
#include "core/templates/hash_set.h"
39
40
class GDScriptCompiler {
41
const GDScriptParser *parser = nullptr;
42
HashSet<GDScript *> parsed_classes;
43
HashSet<GDScript *> parsing_classes;
44
GDScript *main_script = nullptr;
45
46
struct FunctionLambdaInfo {
47
GDScriptFunction *function = nullptr;
48
GDScriptFunction *parent = nullptr;
49
GDScript *script = nullptr;
50
StringName name;
51
int line = 0;
52
int index = 0;
53
int depth = 0;
54
//uint64_t code_hash;
55
//int code_size;
56
int capture_count = 0;
57
bool use_self = false;
58
int arg_count = 0;
59
int default_arg_count = 0;
60
//Vector<GDScriptDataType> argument_types;
61
//GDScriptDataType return_type;
62
Vector<FunctionLambdaInfo> sublambdas;
63
};
64
65
struct ScriptLambdaInfo {
66
Vector<FunctionLambdaInfo> implicit_initializer_info;
67
Vector<FunctionLambdaInfo> implicit_ready_info;
68
Vector<FunctionLambdaInfo> static_initializer_info;
69
HashMap<StringName, Vector<FunctionLambdaInfo>> member_function_infos;
70
Vector<FunctionLambdaInfo> other_function_infos;
71
HashMap<StringName, ScriptLambdaInfo> subclass_info;
72
};
73
74
struct CodeGen {
75
GDScript *script = nullptr;
76
const GDScriptParser::ClassNode *class_node = nullptr;
77
const GDScriptParser::FunctionNode *function_node = nullptr;
78
StringName function_name;
79
GDScriptCodeGenerator *generator = nullptr;
80
HashMap<StringName, GDScriptCodeGenerator::Address> parameters;
81
HashMap<StringName, GDScriptCodeGenerator::Address> locals;
82
List<HashMap<StringName, GDScriptCodeGenerator::Address>> locals_stack;
83
bool is_static = false;
84
85
GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) {
86
uint32_t addr = generator->add_local(p_name, p_type);
87
locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type);
88
return locals[p_name];
89
}
90
91
GDScriptCodeGenerator::Address add_local_constant(const StringName &p_name, const Variant &p_value) {
92
uint32_t addr = generator->add_local_constant(p_name, p_value);
93
locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr);
94
return locals[p_name];
95
}
96
97
GDScriptCodeGenerator::Address add_temporary(const GDScriptDataType &p_type = GDScriptDataType()) {
98
uint32_t addr = generator->add_temporary(p_type);
99
return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::TEMPORARY, addr, p_type);
100
}
101
102
GDScriptCodeGenerator::Address add_constant(const Variant &p_constant) {
103
GDScriptDataType type;
104
type.has_type = true;
105
type.kind = GDScriptDataType::BUILTIN;
106
type.builtin_type = p_constant.get_type();
107
if (type.builtin_type == Variant::OBJECT) {
108
Object *obj = p_constant;
109
if (obj) {
110
type.kind = GDScriptDataType::NATIVE;
111
type.native_type = obj->get_class_name();
112
113
Ref<Script> scr = obj->get_script();
114
if (scr.is_valid()) {
115
type.script_type = scr.ptr();
116
Ref<GDScript> gdscript = scr;
117
if (gdscript.is_valid()) {
118
type.kind = GDScriptDataType::GDSCRIPT;
119
} else {
120
type.kind = GDScriptDataType::SCRIPT;
121
}
122
}
123
} else {
124
type.builtin_type = Variant::NIL;
125
}
126
}
127
128
uint32_t addr = generator->add_or_get_constant(p_constant);
129
return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr, type);
130
}
131
132
void start_block() {
133
HashMap<StringName, GDScriptCodeGenerator::Address> old_locals = locals;
134
locals_stack.push_back(old_locals);
135
generator->start_block();
136
}
137
138
void end_block() {
139
locals = locals_stack.back()->get();
140
locals_stack.pop_back();
141
generator->end_block();
142
}
143
};
144
145
bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
146
bool _is_class_member_property(GDScript *owner, const StringName &p_name);
147
bool _is_local_or_parameter(CodeGen &codegen, const StringName &p_name);
148
149
void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
150
151
GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner, bool p_handle_metatype = true);
152
153
GDScriptCodeGenerator::Address _parse_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::ExpressionNode *p_expression, bool p_root = false, bool p_initializer = false);
154
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);
155
List<GDScriptCodeGenerator::Address> _add_block_locals(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block);
156
void _clear_block_locals(CodeGen &codegen, const List<GDScriptCodeGenerator::Address> &p_locals);
157
Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals = true, bool p_clear_locals = true);
158
GDScriptFunction *_parse_function(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false, bool p_for_lambda = false);
159
GDScriptFunction *_make_static_initializer(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class);
160
Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
161
Error _prepare_compilation(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
162
Error _compile_class(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
163
FunctionLambdaInfo _get_function_replacement_info(GDScriptFunction *p_func, int p_index = -1, int p_depth = 0, GDScriptFunction *p_parent_func = nullptr);
164
Vector<FunctionLambdaInfo> _get_function_lambda_replacement_info(GDScriptFunction *p_func, int p_depth = 0, GDScriptFunction *p_parent_func = nullptr);
165
ScriptLambdaInfo _get_script_lambda_replacement_info(GDScript *p_script);
166
bool _do_function_infos_match(const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info);
167
void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info);
168
void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const Vector<FunctionLambdaInfo> &p_old_infos, const Vector<FunctionLambdaInfo> *p_new_infos);
169
void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const ScriptLambdaInfo &p_old_info, const ScriptLambdaInfo *p_new_info);
170
int err_line = 0;
171
int err_column = 0;
172
StringName source;
173
String error;
174
GDScriptParser::ExpressionNode *awaited_node = nullptr;
175
bool has_static_data = false;
176
177
public:
178
static void convert_to_initializer_type(Variant &p_variant, const GDScriptParser::VariableNode *p_node);
179
static void make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
180
Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
181
182
String get_error() const;
183
int get_error_line() const;
184
int get_error_column() const;
185
186
GDScriptCompiler();
187
};
188
189