• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_KYTHE_DATA_H_
6 #define V8_TORQUE_KYTHE_DATA_H_
7 
8 #include "src/torque/ast.h"
9 #include "src/torque/contextual.h"
10 #include "src/torque/global-context.h"
11 #include "src/torque/implementation-visitor.h"
12 
13 namespace v8 {
14 namespace internal {
15 namespace torque {
16 
17 struct KythePosition {
18   std::string file_path;
19   uint64_t start_offset;
20   uint64_t end_offset;
21 };
22 
23 using kythe_entity_t = uint64_t;
24 
25 class KytheConsumer {
26  public:
27   enum class Kind {
28     Unspecified,
29     Constant,
30     Function,
31     ClassField,
32     Variable,
33     Type,
34   };
35 
36   virtual ~KytheConsumer() = 0;
37 
38   virtual kythe_entity_t AddDefinition(Kind kind, std::string name,
39                                        KythePosition pos) = 0;
40 
41   virtual void AddUse(Kind kind, kythe_entity_t entity,
42                       KythePosition use_pos) = 0;
43   virtual void AddCall(Kind kind, kythe_entity_t caller_entity,
44                        KythePosition call_pos,
45                        kythe_entity_t callee_entity) = 0;
46 };
47 inline KytheConsumer::~KytheConsumer() = default;
48 
49 class KytheData : public ContextualClass<KytheData> {
50  public:
51   KytheData() = default;
52 
SetConsumer(KytheConsumer * consumer)53   static void SetConsumer(KytheConsumer* consumer) {
54     Get().consumer_ = consumer;
55   }
56 
57   // Constants
58   V8_EXPORT_PRIVATE static kythe_entity_t AddConstantDefinition(
59       const Value* constant);
60   V8_EXPORT_PRIVATE static void AddConstantUse(SourcePosition use_position,
61                                                const Value* constant);
62   // Callables
63   V8_EXPORT_PRIVATE static kythe_entity_t AddFunctionDefinition(
64       Callable* callable);
65   V8_EXPORT_PRIVATE static void AddCall(Callable* caller,
66                                         SourcePosition call_position,
67                                         Callable* callee);
68   // Class fields
69   V8_EXPORT_PRIVATE static kythe_entity_t AddClassFieldDefinition(
70       const Field* field);
71   V8_EXPORT_PRIVATE static void AddClassFieldUse(SourcePosition use_position,
72                                                  const Field* field);
73   // Bindings
74   V8_EXPORT_PRIVATE static kythe_entity_t AddBindingDefinition(
75       Binding<LocalValue>* binding);
76   V8_EXPORT_PRIVATE static kythe_entity_t AddBindingDefinition(
77       Binding<LocalLabel>* binding);
78   V8_EXPORT_PRIVATE static void AddBindingUse(SourcePosition use_position,
79                                               Binding<LocalValue>* binding);
80   V8_EXPORT_PRIVATE static void AddBindingUse(SourcePosition use_position,
81                                               Binding<LocalLabel>* binding);
82 
83   // Types
84   V8_EXPORT_PRIVATE static kythe_entity_t AddTypeDefinition(
85       const Declarable* type_decl);
86   V8_EXPORT_PRIVATE static void AddTypeUse(SourcePosition use_position,
87                                            const Declarable* type_decl);
88 
89  private:
90   static kythe_entity_t AddBindingDefinitionImpl(
91       uint64_t binding_index, const std::string& name,
92       const SourcePosition& ident_pos);
93 
94   KytheConsumer* consumer_;
95   std::unordered_map<const Value*, kythe_entity_t> constants_;
96   std::unordered_map<Callable*, kythe_entity_t> callables_;
97 
98   std::unordered_map<const Field*, std::set<SourcePosition>> field_uses_;
99   std::unordered_map<uint64_t, kythe_entity_t> local_bindings_;
100   std::unordered_map<const Declarable*, kythe_entity_t> types_;
101   std::unordered_map<const Field*, kythe_entity_t> class_fields_;
102 };
103 
104 }  // namespace torque
105 }  // namespace internal
106 }  // namespace v8
107 
108 #endif  // V8_TORQUE_KYTHE_DATA_H_
109