| 1 | /**************************************************************************/ |
| 2 | /* gdscript_analyzer.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 | #ifndef GDSCRIPT_ANALYZER_H |
| 32 | #define GDSCRIPT_ANALYZER_H |
| 33 | |
| 34 | #include "gdscript_cache.h" |
| 35 | #include "gdscript_parser.h" |
| 36 | |
| 37 | #include "core/object/object.h" |
| 38 | #include "core/object/ref_counted.h" |
| 39 | #include "core/templates/hash_set.h" |
| 40 | |
| 41 | class GDScriptAnalyzer { |
| 42 | GDScriptParser *parser = nullptr; |
| 43 | HashMap<String, Ref<GDScriptParserRef>> depended_parsers; |
| 44 | |
| 45 | const GDScriptParser::EnumNode *current_enum = nullptr; |
| 46 | GDScriptParser::LambdaNode *current_lambda = nullptr; |
| 47 | List<GDScriptParser::LambdaNode *> pending_body_resolution_lambdas; |
| 48 | bool static_context = false; |
| 49 | |
| 50 | // Tests for detecting invalid overloading of script members |
| 51 | static _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); |
| 52 | static _FORCE_INLINE_ bool has_member_name_conflict_in_native_type(const StringName &p_name, const StringName &p_native_type_string); |
| 53 | Error check_native_member_name_conflict(const StringName &p_member_name, const GDScriptParser::Node *p_member_node, const StringName &p_native_type_string); |
| 54 | Error check_class_member_name_conflict(const GDScriptParser::ClassNode *p_class_node, const StringName &p_member_name, const GDScriptParser::Node *p_member_node); |
| 55 | |
| 56 | void get_class_node_current_scope_classes(GDScriptParser::ClassNode *p_node, List<GDScriptParser::ClassNode *> *p_list); |
| 57 | |
| 58 | Error resolve_class_inheritance(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source = nullptr); |
| 59 | Error resolve_class_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive); |
| 60 | GDScriptParser::DataType resolve_datatype(GDScriptParser::TypeNode *p_type); |
| 61 | |
| 62 | void decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement); |
| 63 | |
| 64 | void resolve_annotation(GDScriptParser::AnnotationNode *p_annotation); |
| 65 | void resolve_class_member(GDScriptParser::ClassNode *p_class, StringName p_name, const GDScriptParser::Node *p_source = nullptr); |
| 66 | void resolve_class_member(GDScriptParser::ClassNode *p_class, int p_index, const GDScriptParser::Node *p_source = nullptr); |
| 67 | void resolve_class_interface(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source = nullptr); |
| 68 | void resolve_class_interface(GDScriptParser::ClassNode *p_class, bool p_recursive); |
| 69 | void resolve_class_body(GDScriptParser::ClassNode *p_class, const GDScriptParser::Node *p_source = nullptr); |
| 70 | void resolve_class_body(GDScriptParser::ClassNode *p_class, bool p_recursive); |
| 71 | void resolve_function_signature(GDScriptParser::FunctionNode *p_function, const GDScriptParser::Node *p_source = nullptr, bool p_is_lambda = false); |
| 72 | void resolve_function_body(GDScriptParser::FunctionNode *p_function, bool p_is_lambda = false); |
| 73 | void resolve_node(GDScriptParser::Node *p_node, bool p_is_root = true); |
| 74 | void resolve_suite(GDScriptParser::SuiteNode *p_suite); |
| 75 | void resolve_assignable(GDScriptParser::AssignableNode *p_assignable, const char *p_kind); |
| 76 | void resolve_variable(GDScriptParser::VariableNode *p_variable, bool p_is_local); |
| 77 | void resolve_constant(GDScriptParser::ConstantNode *p_constant, bool p_is_local); |
| 78 | void resolve_parameter(GDScriptParser::ParameterNode *p_parameter); |
| 79 | void resolve_if(GDScriptParser::IfNode *p_if); |
| 80 | void resolve_for(GDScriptParser::ForNode *p_for); |
| 81 | void resolve_while(GDScriptParser::WhileNode *p_while); |
| 82 | void resolve_assert(GDScriptParser::AssertNode *p_assert); |
| 83 | void resolve_match(GDScriptParser::MatchNode *p_match); |
| 84 | void resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test); |
| 85 | void resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test); |
| 86 | void resolve_return(GDScriptParser::ReturnNode *p_return); |
| 87 | |
| 88 | // Reduction functions. |
| 89 | void reduce_expression(GDScriptParser::ExpressionNode *p_expression, bool p_is_root = false); |
| 90 | void reduce_array(GDScriptParser::ArrayNode *p_array); |
| 91 | void reduce_assignment(GDScriptParser::AssignmentNode *p_assignment); |
| 92 | void reduce_await(GDScriptParser::AwaitNode *p_await); |
| 93 | void reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op); |
| 94 | void reduce_call(GDScriptParser::CallNode *p_call, bool p_is_await = false, bool p_is_root = false); |
| 95 | void reduce_cast(GDScriptParser::CastNode *p_cast); |
| 96 | void reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary); |
| 97 | void reduce_get_node(GDScriptParser::GetNodeNode *p_get_node); |
| 98 | void reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin = false); |
| 99 | void reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base = nullptr); |
| 100 | void reduce_lambda(GDScriptParser::LambdaNode *p_lambda); |
| 101 | void reduce_literal(GDScriptParser::LiteralNode *p_literal); |
| 102 | void reduce_preload(GDScriptParser::PreloadNode *p_preload); |
| 103 | void reduce_self(GDScriptParser::SelfNode *p_self); |
| 104 | void reduce_subscript(GDScriptParser::SubscriptNode *p_subscript, bool p_can_be_pseudo_type = false); |
| 105 | void reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op, bool p_is_root = false); |
| 106 | void reduce_type_test(GDScriptParser::TypeTestNode *p_type_test); |
| 107 | void reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op); |
| 108 | |
| 109 | Variant make_expression_reduced_value(GDScriptParser::ExpressionNode *p_expression, bool &is_reduced); |
| 110 | Variant make_array_reduced_value(GDScriptParser::ArrayNode *p_array, bool &is_reduced); |
| 111 | Variant make_dictionary_reduced_value(GDScriptParser::DictionaryNode *p_dictionary, bool &is_reduced); |
| 112 | Variant make_subscript_reduced_value(GDScriptParser::SubscriptNode *p_subscript, bool &is_reduced); |
| 113 | |
| 114 | // Helpers. |
| 115 | Array make_array_from_element_datatype(const GDScriptParser::DataType &p_element_datatype, const GDScriptParser::Node *p_source_node = nullptr); |
| 116 | GDScriptParser::DataType type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source); |
| 117 | static GDScriptParser::DataType type_from_metatype(const GDScriptParser::DataType &p_meta_type); |
| 118 | GDScriptParser::DataType type_from_property(const PropertyInfo &p_property, bool p_is_arg = false, bool p_is_readonly = false) const; |
| 119 | GDScriptParser::DataType make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source); |
| 120 | bool 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); |
| 121 | bool 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); |
| 122 | void validate_call_arg(const List<GDScriptParser::DataType> &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call); |
| 123 | void validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call); |
| 124 | GDScriptParser::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); |
| 125 | GDScriptParser::DataType get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source); |
| 126 | void update_const_expression_builtin_type(GDScriptParser::ExpressionNode *p_expression, const GDScriptParser::DataType &p_type, const char *p_usage, bool p_is_cast = false); |
| 127 | void update_array_literal_element_type(GDScriptParser::ArrayNode *p_array, const GDScriptParser::DataType &p_element_type); |
| 128 | bool 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); |
| 129 | void push_error(const String &p_message, const GDScriptParser::Node *p_origin = nullptr); |
| 130 | void mark_node_unsafe(const GDScriptParser::Node *p_node); |
| 131 | void downgrade_node_type_source(GDScriptParser::Node *p_node); |
| 132 | void mark_lambda_use_self(); |
| 133 | void resolve_pending_lambda_bodies(); |
| 134 | bool class_exists(const StringName &p_class) const; |
| 135 | Ref<GDScriptParserRef> get_parser_for(const String &p_path); |
| 136 | void reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype); |
| 137 | #ifdef DEBUG_ENABLED |
| 138 | void is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope); |
| 139 | #endif |
| 140 | |
| 141 | public: |
| 142 | Error resolve_inheritance(); |
| 143 | Error resolve_interface(); |
| 144 | Error resolve_body(); |
| 145 | Error resolve_dependencies(); |
| 146 | Error analyze(); |
| 147 | |
| 148 | Variant make_variable_default_value(GDScriptParser::VariableNode *p_variable); |
| 149 | const HashMap<String, Ref<GDScriptParserRef>> &get_depended_parsers(); |
| 150 | |
| 151 | GDScriptAnalyzer(GDScriptParser *p_parser); |
| 152 | }; |
| 153 | |
| 154 | #endif // GDSCRIPT_ANALYZER_H |
| 155 | |