gdscript_analyzer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*************************************************************************/
  2. /* gdscript_analyzer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef GDSCRIPT_ANALYZER_H
  31. #define GDSCRIPT_ANALYZER_H
  32. #include "core/object/object.h"
  33. #include "core/object/reference.h"
  34. #include "core/templates/set.h"
  35. #include "gdscript_cache.h"
  36. #include "gdscript_parser.h"
  37. class GDScriptAnalyzer {
  38. GDScriptParser *parser = nullptr;
  39. HashMap<String, Ref<GDScriptParserRef>> depended_parsers;
  40. const GDScriptParser::EnumNode *current_enum = nullptr;
  41. Error resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive = true);
  42. GDScriptParser::DataType resolve_datatype(GDScriptParser::TypeNode *p_type);
  43. void decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement);
  44. // This traverses the tree to resolve all TypeNodes.
  45. Error resolve_program();
  46. void resolve_annotation(GDScriptParser::AnnotationNode *p_annotation);
  47. void resolve_class_interface(GDScriptParser::ClassNode *p_class);
  48. void resolve_class_body(GDScriptParser::ClassNode *p_class);
  49. void resolve_function_signature(GDScriptParser::FunctionNode *p_function);
  50. void resolve_function_body(GDScriptParser::FunctionNode *p_function);
  51. void resolve_node(GDScriptParser::Node *p_node);
  52. void resolve_suite(GDScriptParser::SuiteNode *p_suite);
  53. void resolve_if(GDScriptParser::IfNode *p_if);
  54. void resolve_for(GDScriptParser::ForNode *p_for);
  55. void resolve_while(GDScriptParser::WhileNode *p_while);
  56. void resolve_variable(GDScriptParser::VariableNode *p_variable);
  57. void resolve_constant(GDScriptParser::ConstantNode *p_constant);
  58. void resolve_assert(GDScriptParser::AssertNode *p_assert);
  59. void resolve_match(GDScriptParser::MatchNode *p_match);
  60. void resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test);
  61. void resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test);
  62. void resolve_parameter(GDScriptParser::ParameterNode *p_parameter);
  63. void resolve_return(GDScriptParser::ReturnNode *p_return);
  64. // Reduction functions.
  65. void reduce_expression(GDScriptParser::ExpressionNode *p_expression);
  66. void reduce_array(GDScriptParser::ArrayNode *p_array);
  67. void reduce_assignment(GDScriptParser::AssignmentNode *p_assignment);
  68. void reduce_await(GDScriptParser::AwaitNode *p_await);
  69. void reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op);
  70. void reduce_call(GDScriptParser::CallNode *p_call, bool is_await = false);
  71. void reduce_cast(GDScriptParser::CastNode *p_cast);
  72. void reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary);
  73. void reduce_get_node(GDScriptParser::GetNodeNode *p_get_node);
  74. void reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin = false);
  75. void reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base = nullptr);
  76. void reduce_literal(GDScriptParser::LiteralNode *p_literal);
  77. void reduce_preload(GDScriptParser::PreloadNode *p_preload);
  78. void reduce_self(GDScriptParser::SelfNode *p_self);
  79. void reduce_subscript(GDScriptParser::SubscriptNode *p_subscript);
  80. void reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op);
  81. void reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op);
  82. void const_fold_array(GDScriptParser::ArrayNode *p_array);
  83. void const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary);
  84. // Helpers.
  85. GDScriptParser::DataType type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source);
  86. GDScriptParser::DataType type_from_metatype(const GDScriptParser::DataType &p_meta_type) const;
  87. GDScriptParser::DataType type_from_property(const PropertyInfo &p_property) const;
  88. GDScriptParser::DataType make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source);
  89. bool get_function_signature(GDScriptParser::Node *p_source, GDScriptParser::DataType base_type, const StringName &p_function, GDScriptParser::DataType &r_return_type, List<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg);
  90. 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, bool &r_static, bool &r_vararg);
  91. bool validate_call_arg(const List<GDScriptParser::DataType> &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call);
  92. bool validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call);
  93. 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);
  94. GDScriptParser::DataType get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source);
  95. void update_array_literal_element_type(const GDScriptParser::DataType &p_base_type, GDScriptParser::ArrayNode *p_array_literal);
  96. bool is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false) const;
  97. void push_error(const String &p_message, const GDScriptParser::Node *p_origin);
  98. void mark_node_unsafe(const GDScriptParser::Node *p_node);
  99. bool class_exists(const StringName &p_class) const;
  100. Ref<GDScriptParserRef> get_parser_for(const String &p_path);
  101. #ifdef DEBUG_ENABLED
  102. bool is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context);
  103. #endif
  104. public:
  105. Error resolve_inheritance();
  106. Error resolve_interface();
  107. Error resolve_body();
  108. Error analyze();
  109. GDScriptAnalyzer(GDScriptParser *p_parser);
  110. };
  111. #endif // GDSCRIPT_ANALYZER_H