kythe-data.cc 6.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/torque/kythe-data.h"

namespace v8 {
namespace internal {
namespace torque {

DEFINE_CONTEXTUAL_VARIABLE(KytheData)

namespace {

KythePosition MakeKythePosition(const SourcePosition& pos) {
  KythePosition p;
  if (pos.source.IsValid()) {
    p.file_path = SourceFileMap::PathFromV8Root(pos.source);
  } else {
    p.file_path = "UNKNOWN";
  }
  p.start_offset = pos.start.offset;
  p.end_offset = pos.end.offset;
  return p;
}

}  // namespace

// Constants
kythe_entity_t KytheData::AddConstantDefinition(const Value* constant) {
  DCHECK(constant->IsNamespaceConstant() || constant->IsExternConstant());
  KytheData* that = &KytheData::Get();
  // Check if we know the constant already.
  auto it = that->constants_.find(constant);
  if (it != that->constants_.end()) return it->second;

  // Register this constant.
  KythePosition pos = MakeKythePosition(constant->name()->pos);
  kythe_entity_t constant_id = that->consumer_->AddDefinition(
      KytheConsumer::Kind::Constant, constant->name()->value, pos);
  that->constants_.insert(it, std::make_pair(constant, constant_id));
  return constant_id;
}

void KytheData::AddConstantUse(SourcePosition use_position,
                               const Value* constant) {
  DCHECK(constant->IsNamespaceConstant() || constant->IsExternConstant());
  KytheData* that = &Get();
  kythe_entity_t constant_id = AddConstantDefinition(constant);
  KythePosition use_pos = MakeKythePosition(use_position);
  that->consumer_->AddUse(KytheConsumer::Kind::Constant, constant_id, use_pos);
}

// Callables
kythe_entity_t KytheData::AddFunctionDefinition(Callable* callable) {
  KytheData* that = &KytheData::Get();
  // Check if we know the caller already.
  auto it = that->callables_.find(callable);
  if (it != that->callables_.end()) return it->second;

  // Register this callable.
  auto ident_pos = callable->IdentifierPosition();
  kythe_entity_t callable_id = that->consumer_->AddDefinition(
      KytheConsumer::Kind::Function, callable->ExternalName(),
      MakeKythePosition(ident_pos));
  that->callables_.insert(it, std::make_pair(callable, callable_id));
  return callable_id;
}

void KytheData::AddCall(Callable* caller, SourcePosition call_position,
                        Callable* callee) {
  if (!caller) return;  // Ignore those for now.
  DCHECK_NOT_NULL(caller);
  DCHECK_NOT_NULL(callee);
  KytheData* that = &Get();
  if (call_position.source.IsValid()) {
    kythe_entity_t caller_id = AddFunctionDefinition(caller);
    kythe_entity_t callee_id = AddFunctionDefinition(callee);

    KythePosition call_pos = MakeKythePosition(call_position);
    that->consumer_->AddCall(KytheConsumer::Kind::Function, caller_id, call_pos,
                             callee_id);
  }
}

// Class fields
kythe_entity_t KytheData::AddClassFieldDefinition(const Field* field) {
  DCHECK(field);
  KytheData* that = &KytheData::Get();
  // Check if we know that field already.
  auto it = that->class_fields_.find(field);
  if (it != that->class_fields_.end()) return it->second;
  // Register this field.
  KythePosition pos = MakeKythePosition(field->pos);
  kythe_entity_t field_id = that->consumer_->AddDefinition(
      KytheConsumer::Kind::ClassField, field->name_and_type.name, pos);
  that->class_fields_.insert(it, std::make_pair(field, field_id));
  return field_id;
}

void KytheData::AddClassFieldUse(SourcePosition use_position,
                                 const Field* field) {
  DCHECK(field);
  KytheData* that = &KytheData::Get();
  kythe_entity_t field_id = AddClassFieldDefinition(field);

  KythePosition use_pos = MakeKythePosition(use_position);
  that->consumer_->AddUse(KytheConsumer::Kind::ClassField, field_id, use_pos);
}

// Bindings
kythe_entity_t KytheData::AddBindingDefinition(Binding<LocalValue>* binding) {
  CHECK(binding);
  const uint64_t binding_index = binding->unique_index();
  return AddBindingDefinitionImpl(binding_index, binding->name(),
                                  binding->declaration_position());
}

kythe_entity_t KytheData::AddBindingDefinition(Binding<LocalLabel>* binding) {
  CHECK(binding);
  const uint64_t binding_index = binding->unique_index();
  return AddBindingDefinitionImpl(binding_index, binding->name(),
                                  binding->declaration_position());
}

kythe_entity_t KytheData::AddBindingDefinitionImpl(
    uint64_t binding_index, const std::string& name,
    const SourcePosition& ident_pos) {
  KytheData* that = &KytheData::Get();
  // Check if we know the binding already.
  auto it = that->local_bindings_.find(binding_index);
  if (it != that->local_bindings_.end()) return it->second;
  // Register this binding.
  kythe_entity_t binding_id = that->consumer_->AddDefinition(
      KytheConsumer::Kind::Variable, name, MakeKythePosition(ident_pos));
  that->local_bindings_.insert(it, std::make_pair(binding_index, binding_id));
  return binding_id;
}

void KytheData::AddBindingUse(SourcePosition use_position,
                              Binding<LocalValue>* binding) {
  CHECK(binding);
  KytheData* that = &KytheData::Get();
  kythe_entity_t binding_id = AddBindingDefinition(binding);

  KythePosition use_pos = MakeKythePosition(use_position);
  that->consumer_->AddUse(KytheConsumer::Kind::Variable, binding_id, use_pos);
}

void KytheData::AddBindingUse(SourcePosition use_position,
                              Binding<LocalLabel>* binding) {
  CHECK(binding);
  KytheData* that = &KytheData::Get();
  kythe_entity_t binding_id = AddBindingDefinition(binding);

  KythePosition use_pos = MakeKythePosition(use_position);
  that->consumer_->AddUse(KytheConsumer::Kind::Variable, binding_id, use_pos);
}

// Types
kythe_entity_t KytheData::AddTypeDefinition(const Declarable* type_decl) {
  CHECK(type_decl);
  KytheData* that = &KytheData::Get();
  // Check if we know that type already.
  auto it = that->types_.find(type_decl);
  if (it != that->types_.end()) return it->second;
  // Register this type.
  KythePosition pos = MakeKythePosition(type_decl->IdentifierPosition());
  kythe_entity_t type_id = that->consumer_->AddDefinition(
      KytheConsumer::Kind::Type, type_decl->type_name(), pos);
  that->types_.insert(it, std::make_pair(type_decl, type_id));
  return type_id;
}

void KytheData::AddTypeUse(SourcePosition use_position,
                           const Declarable* type_decl) {
  CHECK(type_decl);
  KytheData* that = &KytheData::Get();
  kythe_entity_t type_id = AddTypeDefinition(type_decl);

  KythePosition use_pos = MakeKythePosition(use_position);
  that->consumer_->AddUse(KytheConsumer::Kind::Type, type_id, use_pos);
}

}  // namespace torque
}  // namespace internal
}  // namespace v8