CppComprehensionEngine.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2021, 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/CodeComprehensionEngine.h>
  12. #include <DevTools/HackStudio/LanguageServers/FileDB.h>
  13. #include <LibCpp/AST.h>
  14. #include <LibCpp/Parser.h>
  15. #include <LibCpp/Preprocessor.h>
  16. #include <LibGUI/TextPosition.h>
  17. namespace LanguageServers::Cpp {
  18. using namespace ::Cpp;
  19. class CppComprehensionEngine : public CodeComprehensionEngine {
  20. public:
  21. CppComprehensionEngine(const FileDB& filedb);
  22. virtual Vector<GUI::AutocompleteProvider::Entry> get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) override;
  23. virtual void on_edit(const String& file) override;
  24. virtual void file_opened([[maybe_unused]] const String& file) override;
  25. virtual Optional<GUI::AutocompleteProvider::ProjectLocation> find_declaration_of(const String& filename, const GUI::TextPosition& identifier_position) override;
  26. virtual Optional<FunctionParamsHint> get_function_params_hint(const String&, const GUI::TextPosition&) override;
  27. virtual Vector<GUI::AutocompleteProvider::TokenInfo> get_tokens_info(const String& 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==(const SymbolName&) const = default;
  37. };
  38. struct Symbol {
  39. SymbolName name;
  40. NonnullRefPtr<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, const Vector<StringView>& scope, NonnullRefPtr<Declaration>, IsLocal is_local);
  49. };
  50. friend Traits<SymbolName>;
  51. struct DocumentData {
  52. const String& filename() const { return m_filename; }
  53. const String& text() const { return m_text; }
  54. const Preprocessor& 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. const Parser& 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<GUI::AutocompleteProvider::Entry> autocomplete_property(const DocumentData&, const MemberExpression&, const String partial_text) const;
  82. Vector<GUI::AutocompleteProvider::Entry> autocomplete_name(const DocumentData&, const ASTNode&, const String& partial_text) const;
  83. String type_of(const DocumentData&, const Expression&) const;
  84. String type_of_property(const DocumentData&, const Identifier&) const;
  85. String type_of_variable(const Identifier&) const;
  86. bool is_property(const ASTNode&) const;
  87. RefPtr<Declaration> find_declaration_of(const DocumentData&, const ASTNode&) const;
  88. RefPtr<Declaration> find_declaration_of(const DocumentData&, const SymbolName&) const;
  89. RefPtr<Declaration> find_declaration_of(const DocumentData&, const GUI::TextPosition& identifier_position);
  90. enum class RecurseIntoScopes {
  91. No,
  92. Yes
  93. };
  94. Vector<Symbol> properties_of_type(const DocumentData& document, const String& type) const;
  95. Vector<Symbol> get_child_symbols(const ASTNode&) const;
  96. Vector<Symbol> get_child_symbols(const ASTNode&, const Vector<StringView>& scope, Symbol::IsLocal) const;
  97. const DocumentData* get_document_data(const String& file) const;
  98. const DocumentData* get_or_create_document_data(const String& file);
  99. void set_document_data(const String& file, OwnPtr<DocumentData>&& data);
  100. OwnPtr<DocumentData> create_document_data_for(const String& 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. GUI::AutocompleteProvider::DeclarationType type_of_declaration(const Declaration&);
  105. Vector<StringView> scope_of_node(const ASTNode&) const;
  106. Vector<StringView> scope_of_reference_to_symbol(const ASTNode&) const;
  107. Optional<GUI::AutocompleteProvider::ProjectLocation> find_preprocessor_definition(const DocumentData&, const GUI::TextPosition&);
  108. OwnPtr<DocumentData> create_document_data(String&& text, const String& filename);
  109. Optional<Vector<GUI::AutocompleteProvider::Entry>> try_autocomplete_property(const DocumentData&, const ASTNode&, Optional<Token> containing_token) const;
  110. Optional<Vector<GUI::AutocompleteProvider::Entry>> try_autocomplete_name(const DocumentData&, const ASTNode&, Optional<Token> containing_token) const;
  111. Optional<Vector<GUI::AutocompleteProvider::Entry>> try_autocomplete_include(const DocumentData&, Token include_path_token, Cpp::Position const& cursor_position) const;
  112. static bool is_symbol_available(const Symbol&, const Vector<StringView>& current_scope, const Vector<StringView>& reference_scope);
  113. Optional<FunctionParamsHint> get_function_params_hint(DocumentData const&, FunctionCall&, size_t argument_index);
  114. template<typename Func>
  115. void for_each_available_symbol(const DocumentData&, Func) const;
  116. template<typename Func>
  117. void for_each_included_document_recursive(const DocumentData&, Func) const;
  118. GUI::AutocompleteProvider::TokenInfo::SemanticType get_token_semantic_type(DocumentData const&, Token const&);
  119. GUI::AutocompleteProvider::TokenInfo::SemanticType get_semantic_type_for_identifier(DocumentData const&, Position);
  120. HashMap<String, OwnPtr<DocumentData>> m_documents;
  121. // A document's path will be in this set if we're currently processing it.
  122. // A document is added to this set when we start processing it (e.g because it was #included) and removed when we're done.
  123. // We use this to prevent circular #includes from looping indefinitely.
  124. HashTable<String> m_unfinished_documents;
  125. };
  126. template<typename Func>
  127. void CppComprehensionEngine::for_each_available_symbol(const DocumentData& document, Func func) const
  128. {
  129. for (auto& item : document.m_symbols) {
  130. auto decision = func(item.value);
  131. if (decision == IterationDecision::Break)
  132. return;
  133. }
  134. for_each_included_document_recursive(document, [&](const DocumentData& document) {
  135. for (auto& item : document.m_symbols) {
  136. auto decision = func(item.value);
  137. if (decision == IterationDecision::Break)
  138. return IterationDecision::Break;
  139. }
  140. return IterationDecision::Continue;
  141. });
  142. }
  143. template<typename Func>
  144. void CppComprehensionEngine::for_each_included_document_recursive(const DocumentData& document, Func func) const
  145. {
  146. for (auto& included_path : document.m_available_headers) {
  147. auto* included_document = get_document_data(included_path);
  148. if (!included_document)
  149. continue;
  150. auto decision = func(*included_document);
  151. if (decision == IterationDecision::Break)
  152. continue;
  153. }
  154. }
  155. }
  156. namespace AK {
  157. template<>
  158. struct Traits<LanguageServers::Cpp::CppComprehensionEngine::SymbolName> : public GenericTraits<LanguageServers::Cpp::CppComprehensionEngine::SymbolName> {
  159. static unsigned hash(const LanguageServers::Cpp::CppComprehensionEngine::SymbolName& key)
  160. {
  161. unsigned hash = 0;
  162. hash = pair_int_hash(hash, string_hash(key.name.characters_without_null_termination(), key.name.length()));
  163. for (auto& scope_part : key.scope) {
  164. hash = pair_int_hash(hash, string_hash(scope_part.characters_without_null_termination(), scope_part.length()));
  165. }
  166. return hash;
  167. }
  168. };
  169. }