CppComprehensionEngine.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. private:
  28. struct SymbolName {
  29. StringView name;
  30. Vector<StringView> scope;
  31. static SymbolName create(StringView, Vector<StringView>&&);
  32. static SymbolName create(StringView);
  33. String scope_as_string() const;
  34. String to_string() const;
  35. bool operator==(const SymbolName&) const = default;
  36. };
  37. struct Symbol {
  38. SymbolName name;
  39. NonnullRefPtr<Declaration> declaration;
  40. // Local symbols are symbols that should not appear in a global symbol search.
  41. // For example, a variable that is declared inside a function will have is_local = true.
  42. bool is_local { false };
  43. enum class IsLocal {
  44. No,
  45. Yes
  46. };
  47. static Symbol create(StringView name, const Vector<StringView>& scope, NonnullRefPtr<Declaration>, IsLocal is_local);
  48. };
  49. friend Traits<SymbolName>;
  50. struct DocumentData {
  51. const String& filename() const { return m_filename; }
  52. const String& text() const { return m_text; }
  53. const Preprocessor& preprocessor() const
  54. {
  55. VERIFY(m_preprocessor);
  56. return *m_preprocessor;
  57. }
  58. Preprocessor& preprocessor()
  59. {
  60. VERIFY(m_preprocessor);
  61. return *m_preprocessor;
  62. }
  63. const Parser& parser() const
  64. {
  65. VERIFY(m_parser);
  66. return *m_parser;
  67. }
  68. Parser& parser()
  69. {
  70. VERIFY(m_parser);
  71. return *m_parser;
  72. }
  73. String m_filename;
  74. String m_text;
  75. OwnPtr<Preprocessor> m_preprocessor;
  76. OwnPtr<Parser> m_parser;
  77. HashMap<SymbolName, Symbol> m_symbols;
  78. HashTable<String> m_available_headers;
  79. };
  80. Vector<GUI::AutocompleteProvider::Entry> autocomplete_property(const DocumentData&, const MemberExpression&, const String partial_text) const;
  81. Vector<GUI::AutocompleteProvider::Entry> autocomplete_name(const DocumentData&, const ASTNode&, const String& partial_text) const;
  82. String type_of(const DocumentData&, const Expression&) const;
  83. String type_of_property(const DocumentData&, const Identifier&) const;
  84. String type_of_variable(const Identifier&) const;
  85. bool is_property(const ASTNode&) const;
  86. RefPtr<Declaration> find_declaration_of(const DocumentData&, const ASTNode&) const;
  87. RefPtr<Declaration> find_declaration_of(const DocumentData&, const SymbolName&) const;
  88. enum class RecurseIntoScopes {
  89. No,
  90. Yes
  91. };
  92. Vector<Symbol> properties_of_type(const DocumentData& document, const String& type) const;
  93. Vector<Symbol> get_child_symbols(const ASTNode&) const;
  94. Vector<Symbol> get_child_symbols(const ASTNode&, const Vector<StringView>& scope, Symbol::IsLocal) const;
  95. const DocumentData* get_document_data(const String& file) const;
  96. const DocumentData* get_or_create_document_data(const String& file);
  97. void set_document_data(const String& file, OwnPtr<DocumentData>&& data);
  98. OwnPtr<DocumentData> create_document_data_for(const String& file);
  99. String document_path_from_include_path(const StringView& include_path) const;
  100. void update_declared_symbols(DocumentData&);
  101. void update_todo_entries(DocumentData&);
  102. GUI::AutocompleteProvider::DeclarationType type_of_declaration(const Declaration&);
  103. Vector<StringView> scope_of_node(const ASTNode&) const;
  104. Vector<StringView> scope_of_reference_to_symbol(const ASTNode&) const;
  105. Optional<GUI::AutocompleteProvider::ProjectLocation> find_preprocessor_definition(const DocumentData&, const GUI::TextPosition&);
  106. OwnPtr<DocumentData> create_document_data(String&& text, const String& filename);
  107. Optional<Vector<GUI::AutocompleteProvider::Entry>> try_autocomplete_property(const DocumentData&, const ASTNode&, Optional<Token> containing_token) const;
  108. Optional<Vector<GUI::AutocompleteProvider::Entry>> try_autocomplete_name(const DocumentData&, const ASTNode&, Optional<Token> containing_token) const;
  109. Optional<Vector<GUI::AutocompleteProvider::Entry>> try_autocomplete_include(const DocumentData&, Token include_path_token);
  110. static bool is_symbol_available(const Symbol&, const Vector<StringView>& current_scope, const Vector<StringView>& reference_scope);
  111. Optional<FunctionParamsHint> get_function_params_hint(DocumentData const&, FunctionCall&, size_t argument_index);
  112. template<typename Func>
  113. void for_each_available_symbol(const DocumentData&, Func) const;
  114. template<typename Func>
  115. void for_each_included_document_recursive(const DocumentData&, Func) const;
  116. HashMap<String, OwnPtr<DocumentData>> m_documents;
  117. };
  118. template<typename Func>
  119. void CppComprehensionEngine::for_each_available_symbol(const DocumentData& document, Func func) const
  120. {
  121. for (auto& item : document.m_symbols) {
  122. auto decision = func(item.value);
  123. if (decision == IterationDecision::Break)
  124. return;
  125. }
  126. for_each_included_document_recursive(document, [&](const DocumentData& document) {
  127. for (auto& item : document.m_symbols) {
  128. auto decision = func(item.value);
  129. if (decision == IterationDecision::Break)
  130. return IterationDecision::Break;
  131. }
  132. return IterationDecision::Continue;
  133. });
  134. }
  135. template<typename Func>
  136. void CppComprehensionEngine::for_each_included_document_recursive(const DocumentData& document, Func func) const
  137. {
  138. for (auto& included_path : document.m_available_headers) {
  139. auto* included_document = get_document_data(included_path);
  140. if (!included_document)
  141. continue;
  142. auto decision = func(*included_document);
  143. if (decision == IterationDecision::Break)
  144. continue;
  145. }
  146. }
  147. }
  148. namespace AK {
  149. template<>
  150. struct Traits<LanguageServers::Cpp::CppComprehensionEngine::SymbolName> : public GenericTraits<LanguageServers::Cpp::CppComprehensionEngine::SymbolName> {
  151. static unsigned hash(const LanguageServers::Cpp::CppComprehensionEngine::SymbolName& key)
  152. {
  153. unsigned hash = 0;
  154. hash = pair_int_hash(hash, string_hash(key.name.characters_without_null_termination(), key.name.length()));
  155. for (auto& scope_part : key.scope) {
  156. hash = pair_int_hash(hash, string_hash(scope_part.characters_without_null_termination(), scope_part.length()));
  157. }
  158. return hash;
  159. }
  160. };
  161. }