Debian Codegraph @

modules/gdscript/gdscript_compiler.h

raw symbols SCIP
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.kind = GDScriptDataType::BUILTIN;
105 type.builtin_type = p_constant.get_type();
106 if (type.builtin_type == Variant::OBJECT) {
107 Object *obj = p_constant;
108 if (obj) {
109 type.kind = GDScriptDataType::NATIVE;
110 type.native_type = obj->get_class_name();
111
112 Ref<Script> scr = obj->get_script();
113 if (scr.is_valid()) {
114 type.script_type = scr.ptr();
115 Ref<GDScript> gdscript = scr;
116 if (gdscript.is_valid()) {
117 type.kind = GDScriptDataType::GDSCRIPT;
118 } else {
119 type.kind = GDScriptDataType::SCRIPT;
120 }
121 }
122 } else {
123 type.builtin_type = Variant::NIL;
124 }
125 }
126
127 uint32_t addr = generator->add_or_get_constant(p_constant);
128 return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr, type);
129 }
130
131 void start_block() {
132 HashMap<StringName, GDScriptCodeGenerator::Address> old_locals = locals;
133 locals_stack.push_back(old_locals);
134 generator->start_block();
135 }
136
137 void end_block() {
138 locals = locals_stack.back()->get();
139 locals_stack.pop_back();
140 generator->end_block();
141 }
142 };
143
144 bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
145 bool _is_class_member_property(GDScript *owner, const StringName &p_name);
146 bool _is_local_or_parameter(CodeGen &codegen, const StringName &p_name);
147
148 void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
149
150 GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner, bool p_handle_metatype = true);
151
152 GDScriptCodeGenerator::Address _parse_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::ExpressionNode *p_expression, bool p_root = false, bool p_initializer = false);
153 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);
154 List<GDScriptCodeGenerator::Address> _add_block_locals(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block);
155 void _clear_block_locals(CodeGen &codegen, const List<GDScriptCodeGenerator::Address> &p_locals);
156 Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals = true, bool p_clear_locals = true);
157 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);
158 GDScriptFunction *_make_static_initializer(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class);
159 Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
160 Error _prepare_compilation(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
161 Error _compile_class(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
162 FunctionLambdaInfo _get_function_replacement_info(GDScriptFunction *p_func, int p_index = -1, int p_depth = 0, GDScriptFunction *p_parent_func = nullptr);
163 Vector<FunctionLambdaInfo> _get_function_lambda_replacement_info(GDScriptFunction *p_func, int p_depth = 0, GDScriptFunction *p_parent_func = nullptr);
164 ScriptLambdaInfo _get_script_lambda_replacement_info(GDScript *p_script);
165 bool _do_function_infos_match(const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info);
166 void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info);
167 void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const Vector<FunctionLambdaInfo> &p_old_infos, const Vector<FunctionLambdaInfo> *p_new_infos);
168 void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const ScriptLambdaInfo &p_old_info, const ScriptLambdaInfo *p_new_info);
169 int err_line = 0;
170 int err_column = 0;
171 StringName source;
172 String error;
173 GDScriptParser::ExpressionNode *awaited_node = nullptr;
174 bool has_static_data = false;
175
176 public:
177 static void convert_to_initializer_type(Variant &p_variant, const GDScriptParser::VariableNode *p_node);
178 static void make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
179 Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
180
181 String get_error() const;
182 int get_error_line() const;
183 int get_error_column() const;
184
185 GDScriptCompiler();
186 };