| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | /*  gdscript_compiler.h                                                  */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-03 21:27:34 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03: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-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | #ifndef GDSCRIPT_COMPILER_H
 | 
					
						
							|  |  |  | #define GDSCRIPT_COMPILER_H
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-19 17:00:06 +02:00
										 |  |  | #include "core/templates/hash_set.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | #include "gdscript.h"
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | #include "gdscript_codegen.h"
 | 
					
						
							| 
									
										
										
										
											2020-05-01 19:14:56 -03:00
										 |  |  | #include "gdscript_function.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | #include "gdscript_parser.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | class GDScriptCompiler { | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 	const GDScriptParser *parser = nullptr; | 
					
						
							| 
									
										
										
										
											2022-05-19 17:00:06 +02:00
										 |  |  | 	HashSet<GDScript *> parsed_classes; | 
					
						
							|  |  |  | 	HashSet<GDScript *> parsing_classes; | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 	GDScript *main_script = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	struct CodeGen { | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 		GDScript *script = nullptr; | 
					
						
							|  |  |  | 		const GDScriptParser::ClassNode *class_node = nullptr; | 
					
						
							|  |  |  | 		const GDScriptParser::FunctionNode *function_node = nullptr; | 
					
						
							| 
									
										
										
										
											2020-06-01 16:41:05 -03:00
										 |  |  | 		StringName function_name; | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 		GDScriptCodeGenerator *generator = nullptr; | 
					
						
							| 
									
										
										
										
											2022-05-13 15:04:37 +02:00
										 |  |  | 		HashMap<StringName, GDScriptCodeGenerator::Address> parameters; | 
					
						
							|  |  |  | 		HashMap<StringName, GDScriptCodeGenerator::Address> locals; | 
					
						
							|  |  |  | 		List<HashMap<StringName, GDScriptCodeGenerator::Address>> locals_stack; | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) { | 
					
						
							|  |  |  | 			uint32_t addr = generator->add_local(p_name, p_type); | 
					
						
							|  |  |  | 			locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type); | 
					
						
							|  |  |  | 			return locals[p_name]; | 
					
						
							| 
									
										
										
										
											2015-01-14 16:37:24 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 		GDScriptCodeGenerator::Address add_local_constant(const StringName &p_name, const Variant &p_value) { | 
					
						
							|  |  |  | 			uint32_t addr = generator->add_local_constant(p_name, p_value); | 
					
						
							| 
									
										
										
										
											2021-04-08 11:55:24 -03:00
										 |  |  | 			locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr); | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 			return locals[p_name]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 		GDScriptCodeGenerator::Address add_temporary(const GDScriptDataType &p_type = GDScriptDataType()) { | 
					
						
							| 
									
										
										
										
											2021-04-14 14:35:51 -03:00
										 |  |  | 			uint32_t addr = generator->add_temporary(p_type); | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 			return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::TEMPORARY, addr, p_type); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-14 16:37:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 		GDScriptCodeGenerator::Address add_constant(const Variant &p_constant) { | 
					
						
							|  |  |  | 			GDScriptDataType type; | 
					
						
							|  |  |  | 			type.has_type = true; | 
					
						
							|  |  |  | 			type.kind = GDScriptDataType::BUILTIN; | 
					
						
							|  |  |  | 			type.builtin_type = p_constant.get_type(); | 
					
						
							|  |  |  | 			if (type.builtin_type == Variant::OBJECT) { | 
					
						
							|  |  |  | 				Object *obj = p_constant; | 
					
						
							|  |  |  | 				if (obj) { | 
					
						
							|  |  |  | 					type.kind = GDScriptDataType::NATIVE; | 
					
						
							|  |  |  | 					type.native_type = obj->get_class_name(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 12:53:28 +03:00
										 |  |  | 					Ref<Script> scr = obj->get_script(); | 
					
						
							|  |  |  | 					if (scr.is_valid()) { | 
					
						
							|  |  |  | 						type.script_type = scr.ptr(); | 
					
						
							|  |  |  | 						Ref<GDScript> gdscript = scr; | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 						if (gdscript.is_valid()) { | 
					
						
							|  |  |  | 							type.kind = GDScriptDataType::GDSCRIPT; | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							type.kind = GDScriptDataType::SCRIPT; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					type.builtin_type = Variant::NIL; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 			uint32_t addr = generator->add_or_get_constant(p_constant); | 
					
						
							|  |  |  | 			return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr, type); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 		void start_block() { | 
					
						
							| 
									
										
										
										
											2022-05-13 15:04:37 +02:00
										 |  |  | 			HashMap<StringName, GDScriptCodeGenerator::Address> old_locals = locals; | 
					
						
							| 
									
										
										
										
											2020-10-20 14:34:14 -03:00
										 |  |  | 			locals_stack.push_back(old_locals); | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 			generator->start_block(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		void end_block() { | 
					
						
							| 
									
										
										
										
											2020-10-20 14:34:14 -03:00
										 |  |  | 			locals = locals_stack.back()->get(); | 
					
						
							|  |  |  | 			locals_stack.pop_back(); | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 			generator->end_block(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 17:37:45 -03:00
										 |  |  | 	bool _is_class_member_property(CodeGen &codegen, const StringName &p_name); | 
					
						
							|  |  |  | 	bool _is_class_member_property(GDScript *owner, const StringName &p_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | 	void _set_error(const String &p_error, const GDScriptParser::Node *p_node); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 	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()); | 
					
						
							|  |  |  | 	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()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-10 01:26:07 +02:00
										 |  |  | 	GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner = nullptr) const; | 
					
						
							| 
									
										
										
										
											2018-05-29 23:16:57 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 14:51:56 -03:00
										 |  |  | 	GDScriptCodeGenerator::Address _parse_assign_right_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::AssignmentNode *p_assignmentint, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address()); | 
					
						
							|  |  |  | 	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()); | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2021-03-28 11:03:13 -03:00
										 |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2020-06-01 16:41:05 -03: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 18:49:29 +02:00
										 |  |  | 	Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); | 
					
						
							| 
									
										
										
										
											2018-11-24 23:46:13 +01:00
										 |  |  | 	Error _parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); | 
					
						
							| 
									
										
										
										
											2019-03-16 18:49:29 +02:00
										 |  |  | 	void _make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); | 
					
						
							| 
									
										
										
										
											2021-02-08 10:57:18 +01:00
										 |  |  | 	int err_line = 0; | 
					
						
							|  |  |  | 	int err_column = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	StringName source; | 
					
						
							|  |  |  | 	String error; | 
					
						
							| 
									
										
										
										
											2020-05-01 19:14:56 -03:00
										 |  |  | 	bool within_await = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2018-11-24 23:46:13 +01:00
										 |  |  | 	Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	String get_error() const; | 
					
						
							|  |  |  | 	int get_error_line() const; | 
					
						
							|  |  |  | 	int get_error_column() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | 	GDScriptCompiler(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 18:38:18 +01:00
										 |  |  | #endif // GDSCRIPT_COMPILER_H
 |