v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
kythe-data.cc
Go to the documentation of this file.
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
6
7namespace v8 {
8namespace internal {
9namespace torque {
10
11namespace {
12
13KythePosition MakeKythePosition(const SourcePosition& pos) {
14 KythePosition p;
15 if (pos.source.IsValid()) {
17 } else {
18 p.file_path = "UNKNOWN";
19 }
20 p.start_offset = pos.start.offset;
21 p.end_offset = pos.end.offset;
22 return p;
23}
24
25} // namespace
26
27// Constants
29 DCHECK(constant->IsNamespaceConstant() || constant->IsExternConstant());
30 KytheData* that = &KytheData::Get();
31 // Check if we know the constant already.
32 auto it = that->constants_.find(constant);
33 if (it != that->constants_.end()) return it->second;
34
35 // Register this constant.
36 KythePosition pos = MakeKythePosition(constant->name()->pos);
37 kythe_entity_t constant_id = that->consumer_->AddDefinition(
38 KytheConsumer::Kind::Constant, constant->name()->value, pos);
39 that->constants_.insert(it, std::make_pair(constant, constant_id));
40 return constant_id;
41}
42
44 const Value* constant) {
45 DCHECK(constant->IsNamespaceConstant() || constant->IsExternConstant());
46 KytheData* that = &Get();
47 kythe_entity_t constant_id = AddConstantDefinition(constant);
48 KythePosition use_pos = MakeKythePosition(use_position);
49 that->consumer_->AddUse(KytheConsumer::Kind::Constant, constant_id, use_pos);
50}
51
52// Callables
54 KytheData* that = &KytheData::Get();
55 // Check if we know the caller already.
56 auto it = that->callables_.find(callable);
57 if (it != that->callables_.end()) return it->second;
58
59 // Register this callable.
60 auto ident_pos = callable->IdentifierPosition();
61 kythe_entity_t callable_id = that->consumer_->AddDefinition(
63 MakeKythePosition(ident_pos));
64 that->callables_.insert(it, std::make_pair(callable, callable_id));
65 return callable_id;
66}
67
68void KytheData::AddCall(Callable* caller, SourcePosition call_position,
69 Callable* callee) {
70 if (!caller) return; // Ignore those for now.
71 DCHECK_NOT_NULL(caller);
72 DCHECK_NOT_NULL(callee);
73 KytheData* that = &Get();
74 if (call_position.source.IsValid()) {
75 kythe_entity_t caller_id = AddFunctionDefinition(caller);
76 kythe_entity_t callee_id = AddFunctionDefinition(callee);
77
78 KythePosition call_pos = MakeKythePosition(call_position);
79 that->consumer_->AddCall(KytheConsumer::Kind::Function, caller_id, call_pos,
80 callee_id);
81 }
82}
83
84// Class fields
86 DCHECK(field);
87 KytheData* that = &KytheData::Get();
88 // Check if we know that field already.
89 auto it = that->class_fields_.find(field);
90 if (it != that->class_fields_.end()) return it->second;
91 // Register this field.
92 KythePosition pos = MakeKythePosition(field->pos);
93 kythe_entity_t field_id = that->consumer_->AddDefinition(
95 that->class_fields_.insert(it, std::make_pair(field, field_id));
96 return field_id;
97}
98
100 const Field* field) {
101 DCHECK(field);
102 KytheData* that = &KytheData::Get();
103 kythe_entity_t field_id = AddClassFieldDefinition(field);
104
105 KythePosition use_pos = MakeKythePosition(use_position);
106 that->consumer_->AddUse(KytheConsumer::Kind::ClassField, field_id, use_pos);
107}
108
109// Bindings
111 CHECK(binding);
112 const uint64_t binding_index = binding->unique_index();
113 return AddBindingDefinitionImpl(binding_index, binding->name(),
114 binding->declaration_position());
115}
116
118 CHECK(binding);
119 const uint64_t binding_index = binding->unique_index();
120 return AddBindingDefinitionImpl(binding_index, binding->name(),
121 binding->declaration_position());
122}
123
125 uint64_t binding_index, const std::string& name,
126 const SourcePosition& ident_pos) {
127 KytheData* that = &KytheData::Get();
128 // Check if we know the binding already.
129 auto it = that->local_bindings_.find(binding_index);
130 if (it != that->local_bindings_.end()) return it->second;
131 // Register this binding.
132 kythe_entity_t binding_id = that->consumer_->AddDefinition(
133 KytheConsumer::Kind::Variable, name, MakeKythePosition(ident_pos));
134 that->local_bindings_.insert(it, std::make_pair(binding_index, binding_id));
135 return binding_id;
136}
137
139 Binding<LocalValue>* binding) {
140 CHECK(binding);
141 KytheData* that = &KytheData::Get();
142 kythe_entity_t binding_id = AddBindingDefinition(binding);
143
144 KythePosition use_pos = MakeKythePosition(use_position);
145 that->consumer_->AddUse(KytheConsumer::Kind::Variable, binding_id, use_pos);
146}
147
149 Binding<LocalLabel>* binding) {
150 CHECK(binding);
151 KytheData* that = &KytheData::Get();
152 kythe_entity_t binding_id = AddBindingDefinition(binding);
153
154 KythePosition use_pos = MakeKythePosition(use_position);
155 that->consumer_->AddUse(KytheConsumer::Kind::Variable, binding_id, use_pos);
156}
157
158// Types
160 CHECK(type_decl);
161 KytheData* that = &KytheData::Get();
162 // Check if we know that type already.
163 auto it = that->types_.find(type_decl);
164 if (it != that->types_.end()) return it->second;
165 // Register this type.
166 KythePosition pos = MakeKythePosition(type_decl->IdentifierPosition());
167 kythe_entity_t type_id = that->consumer_->AddDefinition(
169 that->types_.insert(it, std::make_pair(type_decl, type_id));
170 return type_id;
171}
172
174 const Declarable* type_decl) {
175 CHECK(type_decl);
176 KytheData* that = &KytheData::Get();
177 kythe_entity_t type_id = AddTypeDefinition(type_decl);
178
179 KythePosition use_pos = MakeKythePosition(use_position);
180 that->consumer_->AddUse(KytheConsumer::Kind::Type, type_id, use_pos);
181}
182
183} // namespace torque
184} // namespace internal
185} // namespace v8
SourcePosition pos
static VarType & Get()
Definition contextual.h:64
const std::string & name() const
SourcePosition declaration_position() const
const std::string & ExternalName() const
Definition declarable.h:304
SourcePosition IdentifierPosition() const
Definition declarable.h:103
virtual const char * type_name() const
Definition declarable.h:91
static V8_EXPORT_PRIVATE kythe_entity_t AddConstantDefinition(const Value *constant)
Definition kythe-data.cc:28
static kythe_entity_t AddBindingDefinitionImpl(uint64_t binding_index, const std::string &name, const SourcePosition &ident_pos)
static V8_EXPORT_PRIVATE void AddBindingUse(SourcePosition use_position, Binding< LocalValue > *binding)
static V8_EXPORT_PRIVATE kythe_entity_t AddBindingDefinition(Binding< LocalValue > *binding)
static V8_EXPORT_PRIVATE kythe_entity_t AddFunctionDefinition(Callable *callable)
Definition kythe-data.cc:53
static V8_EXPORT_PRIVATE kythe_entity_t AddTypeDefinition(const Declarable *type_decl)
static V8_EXPORT_PRIVATE void AddClassFieldUse(SourcePosition use_position, const Field *field)
Definition kythe-data.cc:99
static V8_EXPORT_PRIVATE kythe_entity_t AddClassFieldDefinition(const Field *field)
Definition kythe-data.cc:85
static V8_EXPORT_PRIVATE void AddTypeUse(SourcePosition use_position, const Declarable *type_decl)
static V8_EXPORT_PRIVATE void AddConstantUse(SourcePosition use_position, const Value *constant)
Definition kythe-data.cc:43
static V8_EXPORT_PRIVATE void AddCall(Callable *caller, SourcePosition call_position, Callable *callee)
Definition kythe-data.cc:68
static const std::string & PathFromV8Root(SourceId file)
uint64_t kythe_entity_t
Definition kythe-data.h:23
#define CHECK(condition)
Definition logging.h:124
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define DCHECK(condition)
Definition logging.h:482
NameAndType name_and_type
Definition types.h:222
SourcePosition pos
Definition types.h:219