Path: blob/master/modules/gdscript/gdscript_analyzer.h
10277 views
/**************************************************************************/1/* gdscript_analyzer.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_cache.h"33#include "gdscript_parser.h"3435#include "core/object/object.h"36#include "core/object/ref_counted.h"3738class GDScriptAnalyzer {39GDScriptParser *parser = nullptr;4041template <typename Fn>42class Finally {43Fn fn;4445public:46Finally(Fn p_fn) :47fn(p_fn) {}48~Finally() {49fn();50}51};5253const GDScriptParser::EnumNode *current_enum = nullptr;54GDScriptParser::LambdaNode *current_lambda = nullptr;55List<GDScriptParser::LambdaNode *> pending_body_resolution_lambdas;56HashMap<const GDScriptParser::ClassNode *, Ref<GDScriptParserRef>> external_class_parser_cache;57bool static_context = false;5859// Tests for detecting invalid overloading of script members60static _FORCE_INLINE_ bool has_member_name_conflict_in_script_class(const StringName &p_name, const GDScriptParser::ClassNode *p_current_class_node, const GDScriptParser::Node *p_member);61static _FORCE_INLINE_ bool has_member_name_conflict_in_native_type(const StringName &p_name, const StringName &p_native_type_string);62Error check_native_member_name_conflict(const StringName &p_member_name, const GDScriptParser::Node *p_member_node, const StringName &p_native_type_string);63Error check_class_member_name_conflict(const GDScriptParser::ClassNode *p_class_node, const StringName &p_member_name, const GDScriptParser::Node *p_member_node);6465void get_class_node_current_scope_classes(GDScriptParser::ClassNode *p_node, List<GDScriptParser::ClassNode *> *p_list, GDScriptParser::Node *p_source);6667Error resolve_class_inheritance(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source = nullptr);68Error resolve_class_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive);69GDScriptParser::DataType resolve_datatype(GDScriptParser::TypeNode *p_type);7071void decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement);7273void resolve_annotation(GDScriptParser::AnnotationNode *p_annotation);74void resolve_class_member(GDScriptParser::ClassNode *p_class, const StringName &p_name, const GDScriptParser::Node *p_source = nullptr);75void resolve_class_member(GDScriptParser::ClassNode *p_class, int p_index, const GDScriptParser::Node *p_source = nullptr);76void resolve_class_interface(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source = nullptr);77void resolve_class_interface(GDScriptParser::ClassNode *p_class, bool p_recursive);78void resolve_class_body(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source = nullptr);79void resolve_class_body(GDScriptParser::ClassNode *p_class, bool p_recursive);80void resolve_function_signature(GDScriptParser::FunctionNode *p_function, const GDScriptParser::Node *p_source = nullptr, bool p_is_lambda = false);81void resolve_function_body(GDScriptParser::FunctionNode *p_function, bool p_is_lambda = false);82void resolve_node(GDScriptParser::Node *p_node, bool p_is_root = true);83void resolve_suite(GDScriptParser::SuiteNode *p_suite);84void resolve_assignable(GDScriptParser::AssignableNode *p_assignable, const char *p_kind);85void resolve_variable(GDScriptParser::VariableNode *p_variable, bool p_is_local);86void resolve_constant(GDScriptParser::ConstantNode *p_constant, bool p_is_local);87void resolve_parameter(GDScriptParser::ParameterNode *p_parameter);88void resolve_if(GDScriptParser::IfNode *p_if);89void resolve_for(GDScriptParser::ForNode *p_for);90void resolve_while(GDScriptParser::WhileNode *p_while);91void resolve_assert(GDScriptParser::AssertNode *p_assert);92void resolve_match(GDScriptParser::MatchNode *p_match);93void resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test);94void resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test);95void resolve_return(GDScriptParser::ReturnNode *p_return);9697// Reduction functions.98void reduce_expression(GDScriptParser::ExpressionNode *p_expression, bool p_is_root = false);99void reduce_array(GDScriptParser::ArrayNode *p_array);100void reduce_assignment(GDScriptParser::AssignmentNode *p_assignment);101void reduce_await(GDScriptParser::AwaitNode *p_await);102void reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op);103void reduce_call(GDScriptParser::CallNode *p_call, bool p_is_await = false, bool p_is_root = false);104void reduce_cast(GDScriptParser::CastNode *p_cast);105void reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary);106void reduce_get_node(GDScriptParser::GetNodeNode *p_get_node);107void reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin = false);108void reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base = nullptr);109void reduce_lambda(GDScriptParser::LambdaNode *p_lambda);110void reduce_literal(GDScriptParser::LiteralNode *p_literal);111void reduce_preload(GDScriptParser::PreloadNode *p_preload);112void reduce_self(GDScriptParser::SelfNode *p_self);113void reduce_subscript(GDScriptParser::SubscriptNode *p_subscript, bool p_can_be_pseudo_type = false);114void reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op, bool p_is_root = false);115void reduce_type_test(GDScriptParser::TypeTestNode *p_type_test);116void reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op);117118Variant make_expression_reduced_value(GDScriptParser::ExpressionNode *p_expression, bool &is_reduced);119Variant make_array_reduced_value(GDScriptParser::ArrayNode *p_array, bool &is_reduced);120Variant make_dictionary_reduced_value(GDScriptParser::DictionaryNode *p_dictionary, bool &is_reduced);121Variant make_subscript_reduced_value(GDScriptParser::SubscriptNode *p_subscript, bool &is_reduced);122Variant make_call_reduced_value(GDScriptParser::CallNode *p_call, bool &is_reduced);123124// Helpers.125Array make_array_from_element_datatype(const GDScriptParser::DataType &p_element_datatype, const GDScriptParser::Node *p_source_node = nullptr);126Dictionary make_dictionary_from_element_datatype(const GDScriptParser::DataType &p_key_element_datatype, const GDScriptParser::DataType &p_value_element_datatype, const GDScriptParser::Node *p_source_node = nullptr);127GDScriptParser::DataType type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source);128GDScriptParser::DataType type_from_property(const PropertyInfo &p_property, bool p_is_arg = false, bool p_is_readonly = false) const;129GDScriptParser::DataType make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source);130bool get_function_signature(GDScriptParser::Node *p_source, bool p_is_constructor, GDScriptParser::DataType base_type, const StringName &p_function, GDScriptParser::DataType &r_return_type, List<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, BitField<MethodFlags> &r_method_flags, StringName *r_native_class = nullptr);131bool function_signature_from_info(const MethodInfo &p_info, GDScriptParser::DataType &r_return_type, List<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, BitField<MethodFlags> &r_method_flags);132void validate_call_arg(const List<GDScriptParser::DataType> &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call);133void validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call);134GDScriptParser::DataType get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid, const GDScriptParser::Node *p_source);135GDScriptParser::DataType get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source);136void update_const_expression_builtin_type(GDScriptParser::ExpressionNode *p_expression, const GDScriptParser::DataType &p_type, const char *p_usage, bool p_is_cast = false);137void update_array_literal_element_type(GDScriptParser::ArrayNode *p_array, const GDScriptParser::DataType &p_element_type);138void update_dictionary_literal_element_type(GDScriptParser::DictionaryNode *p_dictionary, const GDScriptParser::DataType &p_key_element_type, const GDScriptParser::DataType &p_value_element_type);139bool is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false, const GDScriptParser::Node *p_source_node = nullptr);140void push_error(const String &p_message, const GDScriptParser::Node *p_origin = nullptr);141void mark_node_unsafe(const GDScriptParser::Node *p_node);142void downgrade_node_type_source(GDScriptParser::Node *p_node);143void mark_lambda_use_self();144void resolve_pending_lambda_bodies();145bool class_exists(const StringName &p_class) const;146void reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype);147Ref<GDScriptParserRef> ensure_cached_external_parser_for_class(const GDScriptParser::ClassNode *p_class, const GDScriptParser::ClassNode *p_from_class, const char *p_context, const GDScriptParser::Node *p_source);148Ref<GDScriptParserRef> find_cached_external_parser_for_class(const GDScriptParser::ClassNode *p_class, const Ref<GDScriptParserRef> &p_dependant_parser);149Ref<GDScriptParserRef> find_cached_external_parser_for_class(const GDScriptParser::ClassNode *p_class, GDScriptParser *p_dependant_parser);150Ref<GDScript> get_depended_shallow_script(const String &p_path, Error &r_error);151#ifdef DEBUG_ENABLED152void is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope);153#endif154155public:156Error resolve_inheritance();157Error resolve_interface();158Error resolve_body();159Error resolve_dependencies();160Error analyze();161162Variant make_variable_default_value(GDScriptParser::VariableNode *p_variable);163164static bool check_type_compatibility(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false, const GDScriptParser::Node *p_source_node = nullptr);165static GDScriptParser::DataType type_from_metatype(const GDScriptParser::DataType &p_meta_type);166167GDScriptAnalyzer(GDScriptParser *p_parser);168};169170171