CppComprehensionEngine.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2021-2022, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. #include <DevTools/HackStudio/AutoCompleteResponse.h>
  11. #include <DevTools/HackStudio/LanguageServers/FileDB.h>
  12. #include <LibCpp/AST.h>
  13. #include <LibCpp/Parser.h>
  14. #include <LibCpp/Preprocessor.h>
  15. #include <LibGUI/TextPosition.h>
  16. #include <Libraries/LibCodeComprehension/CodeComprehensionEngine.h>
  17. namespace CodeComprehension::Cpp {
  18. using namespace ::Cpp;
  19. class CppComprehensionEngine : public CodeComprehensionEngine {
  20. public:
  21. CppComprehensionEngine(FileDB const& filedb);
  22. virtual Vector<CodeComprehension::AutocompleteResultEntry> get_suggestions(String const& file, GUI::TextPosition const& autocomplete_position) override;
  23. virtual void on_edit(String const& file) override;
  24. virtual void file_opened([[maybe_unused]] String const& file) override;
  25. virtual Optional<CodeComprehension::ProjectLocation> find_declaration_of(String const& filename, GUI::TextPosition const& identifier_position) override;
  26. virtual Optional<FunctionParamsHint> get_function_params_hint(String const&, GUI::TextPosition const&) override;
  27. virtual Vector<CodeComprehension::TokenInfo> get_tokens_info(String const& filename) override;
  28. private:
  29. struct SymbolName {
  30. StringView name;
  31. Vector<StringView> scope;
  32. static SymbolName create(StringView, Vector<StringView>&&);
  33. static SymbolName create(StringView);
  34. String scope_as_string() const;
  35. String to_string() const;
  36. bool operator==(SymbolName const&) const = default;
  37. };
  38. struct Symbol {
  39. SymbolName name;
  40. NonnullRefPtr<Cpp::Declaration> declaration;
  41. // Local symbols are symbols that should not appear in a global symbol search.
  42. // For example, a variable that is declared inside a function will have is_local = true.
  43. bool is_local { false };
  44. enum class IsLocal {
  45. No,
  46. Yes
  47. };
  48. static Symbol create(StringView name, Vector<StringView> const& scope, NonnullRefPtr<Cpp::Declaration>, IsLocal is_local);
  49. };
  50. friend Traits<SymbolName>;
  51. struct DocumentData {
  52. String const& filename() const { return m_filename; }
  53. String const& text() const { return m_text; }
  54. Preprocessor const& preprocessor() const
  55. {
  56. VERIFY(m_preprocessor);
  57. return *m_preprocessor;
  58. }
  59. Preprocessor& preprocessor()
  60. {
  61. VERIFY(m_preprocessor);
  62. return *m_preprocessor;
  63. }
  64. Parser const& parser() const
  65. {
  66. VERIFY(m_parser);
  67. return *m_parser;
  68. }
  69. Parser& parser()
  70. {
  71. VERIFY(m_parser);
  72. return *m_parser;
  73. }
  74. String m_filename;
  75. String m_text;
  76. OwnPtr<Preprocessor> m_preprocessor;
  77. OwnPtr<Parser> m_parser;
  78. HashMap<SymbolName, Symbol> m_symbols;
  79. HashTable<String> m_available_headers;
  80. };
  81. Vector<CodeComprehension::AutocompleteResultEntry> autocomplete_property(DocumentData const&, MemberExpression const&, const String partial_text) const;
  82. Vector<AutocompleteResultEntry> autocomplete_name(DocumentData const&, ASTNode const&, String const& partial_text) const;
  83. String type_of(DocumentData const&, Expression const&) const;
  84. String type_of_property(DocumentData const&, Identifier const&) const;
  85. String type_of_variable(Identifier const&) const;
  86. bool is_property(ASTNode const&) const;
  87. RefPtr<Cpp::Declaration> find_declaration_of(DocumentData const&, ASTNode const&) const;
  88. RefPtr<Cpp::Declaration> find_declaration_of(DocumentData const&, SymbolName const&) const;
  89. RefPtr<Cpp::Declaration> find_declaration_of(DocumentData const&, const GUI::TextPosition& identifier_position);
  90. enum class RecurseIntoScopes {
  91. No,
  92. Yes
  93. };
  94. Vector<Symbol> properties_of_type(DocumentData const& document, String const& type) const;
  95. Vector<Symbol> get_child_symbols(ASTNode const&) const;
  96. Vector<Symbol> get_child_symbols(ASTNode const&, Vector<StringView> const& scope, Symbol::IsLocal) const;
  97. DocumentData const* get_document_data(String const& file) const;
  98. DocumentData const* get_or_create_document_data(String const& file);
  99. void set_document_data(String const& file, OwnPtr<DocumentData>&& data);
  100. OwnPtr<DocumentData> create_document_data_for(String const& file);
  101. String document_path_from_include_path(StringView include_path) const;
  102. void update_declared_symbols(DocumentData&);
  103. void update_todo_entries(DocumentData&);
  104. CodeComprehension::DeclarationType type_of_declaration(Cpp::Declaration const&);
  105. Vector<StringView> scope_of_node(ASTNode const&) const;
  106. Vector<StringView> scope_of_reference_to_symbol(ASTNode const&) const;
  107. Optional<CodeComprehension::ProjectLocation> find_preprocessor_definition(DocumentData const&, const GUI::TextPosition&);
  108. Optional<Cpp::Preprocessor::Substitution> find_preprocessor_substitution(DocumentData const&, Cpp::Position const&);
  109. OwnPtr<DocumentData> create_document_data(String text, String const& filename);
  110. Optional<Vector<CodeComprehension::AutocompleteResultEntry>> try_autocomplete_property(DocumentData const&, ASTNode const&, Optional<Token> containing_token) const;
  111. Optional<Vector<CodeComprehension::AutocompleteResultEntry>> try_autocomplete_name(DocumentData const&, ASTNode const&, Optional<Token> containing_token) const;
  112. Optional<Vector<CodeComprehension::AutocompleteResultEntry>> try_autocomplete_include(DocumentData const&, Token include_path_token, Cpp::Position const& cursor_position) const;
  113. static bool is_symbol_available(Symbol const&, Vector<StringView> const& current_scope, Vector<StringView> const& reference_scope);
  114. Optional<FunctionParamsHint> get_function_params_hint(DocumentData const&, FunctionCall&, size_t argument_index);
  115. template<typename Func>
  116. void for_each_available_symbol(DocumentData const&, Func) const;
  117. template<typename Func>
  118. void for_each_included_document_recursive(DocumentData const&, Func) const;
  119. CodeComprehension::TokenInfo::SemanticType get_token_semantic_type(DocumentData const&, Token const&);
  120. CodeComprehension::TokenInfo::SemanticType get_semantic_type_for_identifier(DocumentData const&, Position);
  121. HashMap<String, OwnPtr<DocumentData>> m_documents;
  122. // A document's path will be in this set if we're currently processing it.
  123. // A document is added to this set when we start processing it (e.g because it was #included) and removed when we're done.
  124. // We use this to prevent circular #includes from looping indefinitely.
  125. HashTable<String> m_unfinished_documents;
  126. };
  127. template<typename Func>
  128. void CppComprehensionEngine::for_each_available_symbol(DocumentData const& document, Func func) const
  129. {
  130. for (auto& item : document.m_symbols) {
  131. auto decision = func(item.value);
  132. if (decision == IterationDecision::Break)
  133. return;
  134. }
  135. for_each_included_document_recursive(document, [&](DocumentData const& document) {
  136. for (auto& item : document.m_symbols) {
  137. auto decision = func(item.value);
  138. if (decision == IterationDecision::Break)
  139. return IterationDecision::Break;
  140. }
  141. return IterationDecision::Continue;
  142. });
  143. }
  144. template<typename Func>
  145. void CppComprehensionEngine::for_each_included_document_recursive(DocumentData const& document, Func func) const
  146. {
  147. for (auto& included_path : document.m_available_headers) {
  148. auto* included_document = get_document_data(included_path);
  149. if (!included_document)
  150. continue;
  151. auto decision = func(*included_document);
  152. if (decision == IterationDecision::Break)
  153. continue;
  154. }
  155. }
  156. }
  157. namespace AK {
  158. template<>
  159. struct Traits<CodeComprehension::Cpp::CppComprehensionEngine::SymbolName> : public GenericTraits<CodeComprehension::Cpp::CppComprehensionEngine::SymbolName> {
  160. static unsigned hash(CodeComprehension::Cpp::CppComprehensionEngine::SymbolName const& key)
  161. {
  162. unsigned hash = 0;
  163. hash = pair_int_hash(hash, string_hash(key.name.characters_without_null_termination(), key.name.length()));
  164. for (auto& scope_part : key.scope) {
  165. hash = pair_int_hash(hash, string_hash(scope_part.characters_without_null_termination(), scope_part.length()));
  166. }
  167. return hash;
  168. }
  169. };
  170. }