• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_TORQUE_SERVER_DATA_H_
6 #define V8_TORQUE_SERVER_DATA_H_
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include "src/base/macros.h"
13 #include "src/base/optional.h"
14 #include "src/torque/declarable.h"
15 #include "src/torque/global-context.h"
16 #include "src/torque/source-positions.h"
17 #include "src/torque/type-oracle.h"
18 
19 namespace v8 {
20 namespace internal {
21 namespace torque {
22 
23 // The definition of the token in the first element, can be found at the second.
24 using DefinitionMapping = std::pair<SourcePosition, SourcePosition>;
25 // TODO(szuend): Support overlapping source positions when we start adding them.
26 using Definitions = std::vector<DefinitionMapping>;
27 using DefinitionsMap = std::map<SourceId, Definitions>;
28 
29 // Symbols are used to answer search queries (either workspace or document
30 // scope). For now, declarables are stored directly without converting them
31 // into a custom format. Symbols are grouped by sourceId to implement document
32 // scoped searches.
33 using Symbols = std::vector<Declarable*>;
34 using SymbolsMap = std::map<SourceId, Symbols>;
35 
36 // This contextual class holds all the necessary data to answer incoming
37 // LSP requests. It is reset for each compilation step and all information
38 // is calculated eagerly during compilation.
39 class LanguageServerData : public ContextualClass<LanguageServerData> {
40  public:
41   LanguageServerData() = default;
42 
43   V8_EXPORT_PRIVATE static void AddDefinition(SourcePosition token,
44                                               SourcePosition definition);
45 
46   V8_EXPORT_PRIVATE static base::Optional<SourcePosition> FindDefinition(
47       SourceId source, LineAndColumn pos);
48 
SetGlobalContext(GlobalContext global_context)49   static void SetGlobalContext(GlobalContext global_context) {
50     Get().global_context_ =
51         std::make_unique<GlobalContext>(std::move(global_context));
52     Get().PrepareAllDeclarableSymbols();
53   }
54 
SetTypeOracle(TypeOracle type_oracle)55   static void SetTypeOracle(TypeOracle type_oracle) {
56     Get().type_oracle_ = std::make_unique<TypeOracle>(std::move(type_oracle));
57   }
58 
SymbolsForSourceId(SourceId id)59   static const Symbols& SymbolsForSourceId(SourceId id) {
60     return Get().symbols_map_[id];
61   }
62 
63  private:
64   // Splits all declarables up by SourceId and filters out auto-generated ones.
65   void PrepareAllDeclarableSymbols();
66 
67   DefinitionsMap definitions_map_;
68   SymbolsMap symbols_map_;
69   std::unique_ptr<GlobalContext> global_context_;
70   std::unique_ptr<TypeOracle> type_oracle_;
71 };
72 
73 }  // namespace torque
74 }  // namespace internal
75 }  // namespace v8
76 
77 #endif  // V8_TORQUE_SERVER_DATA_H_
78