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