11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#include "src/torque/kythe-data.h" 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cinamespace v8 { 81cb0ef41Sopenharmony_cinamespace internal { 91cb0ef41Sopenharmony_cinamespace torque { 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciDEFINE_CONTEXTUAL_VARIABLE(KytheData) 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cinamespace { 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciKythePosition MakeKythePosition(const SourcePosition& pos) { 161cb0ef41Sopenharmony_ci KythePosition p; 171cb0ef41Sopenharmony_ci if (pos.source.IsValid()) { 181cb0ef41Sopenharmony_ci p.file_path = SourceFileMap::PathFromV8Root(pos.source); 191cb0ef41Sopenharmony_ci } else { 201cb0ef41Sopenharmony_ci p.file_path = "UNKNOWN"; 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci p.start_offset = pos.start.offset; 231cb0ef41Sopenharmony_ci p.end_offset = pos.end.offset; 241cb0ef41Sopenharmony_ci return p; 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci} // namespace 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci// Constants 301cb0ef41Sopenharmony_cikythe_entity_t KytheData::AddConstantDefinition(const Value* constant) { 311cb0ef41Sopenharmony_ci DCHECK(constant->IsNamespaceConstant() || constant->IsExternConstant()); 321cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 331cb0ef41Sopenharmony_ci // Check if we know the constant already. 341cb0ef41Sopenharmony_ci auto it = that->constants_.find(constant); 351cb0ef41Sopenharmony_ci if (it != that->constants_.end()) return it->second; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci // Register this constant. 381cb0ef41Sopenharmony_ci KythePosition pos = MakeKythePosition(constant->name()->pos); 391cb0ef41Sopenharmony_ci kythe_entity_t constant_id = that->consumer_->AddDefinition( 401cb0ef41Sopenharmony_ci KytheConsumer::Kind::Constant, constant->name()->value, pos); 411cb0ef41Sopenharmony_ci that->constants_.insert(it, std::make_pair(constant, constant_id)); 421cb0ef41Sopenharmony_ci return constant_id; 431cb0ef41Sopenharmony_ci} 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_civoid KytheData::AddConstantUse(SourcePosition use_position, 461cb0ef41Sopenharmony_ci const Value* constant) { 471cb0ef41Sopenharmony_ci DCHECK(constant->IsNamespaceConstant() || constant->IsExternConstant()); 481cb0ef41Sopenharmony_ci KytheData* that = &Get(); 491cb0ef41Sopenharmony_ci kythe_entity_t constant_id = AddConstantDefinition(constant); 501cb0ef41Sopenharmony_ci KythePosition use_pos = MakeKythePosition(use_position); 511cb0ef41Sopenharmony_ci that->consumer_->AddUse(KytheConsumer::Kind::Constant, constant_id, use_pos); 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci// Callables 551cb0ef41Sopenharmony_cikythe_entity_t KytheData::AddFunctionDefinition(Callable* callable) { 561cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 571cb0ef41Sopenharmony_ci // Check if we know the caller already. 581cb0ef41Sopenharmony_ci auto it = that->callables_.find(callable); 591cb0ef41Sopenharmony_ci if (it != that->callables_.end()) return it->second; 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci // Register this callable. 621cb0ef41Sopenharmony_ci auto ident_pos = callable->IdentifierPosition(); 631cb0ef41Sopenharmony_ci kythe_entity_t callable_id = that->consumer_->AddDefinition( 641cb0ef41Sopenharmony_ci KytheConsumer::Kind::Function, callable->ExternalName(), 651cb0ef41Sopenharmony_ci MakeKythePosition(ident_pos)); 661cb0ef41Sopenharmony_ci that->callables_.insert(it, std::make_pair(callable, callable_id)); 671cb0ef41Sopenharmony_ci return callable_id; 681cb0ef41Sopenharmony_ci} 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_civoid KytheData::AddCall(Callable* caller, SourcePosition call_position, 711cb0ef41Sopenharmony_ci Callable* callee) { 721cb0ef41Sopenharmony_ci if (!caller) return; // Ignore those for now. 731cb0ef41Sopenharmony_ci DCHECK_NOT_NULL(caller); 741cb0ef41Sopenharmony_ci DCHECK_NOT_NULL(callee); 751cb0ef41Sopenharmony_ci KytheData* that = &Get(); 761cb0ef41Sopenharmony_ci if (call_position.source.IsValid()) { 771cb0ef41Sopenharmony_ci kythe_entity_t caller_id = AddFunctionDefinition(caller); 781cb0ef41Sopenharmony_ci kythe_entity_t callee_id = AddFunctionDefinition(callee); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci KythePosition call_pos = MakeKythePosition(call_position); 811cb0ef41Sopenharmony_ci that->consumer_->AddCall(KytheConsumer::Kind::Function, caller_id, call_pos, 821cb0ef41Sopenharmony_ci callee_id); 831cb0ef41Sopenharmony_ci } 841cb0ef41Sopenharmony_ci} 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci// Class fields 871cb0ef41Sopenharmony_cikythe_entity_t KytheData::AddClassFieldDefinition(const Field* field) { 881cb0ef41Sopenharmony_ci DCHECK(field); 891cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 901cb0ef41Sopenharmony_ci // Check if we know that field already. 911cb0ef41Sopenharmony_ci auto it = that->class_fields_.find(field); 921cb0ef41Sopenharmony_ci if (it != that->class_fields_.end()) return it->second; 931cb0ef41Sopenharmony_ci // Register this field. 941cb0ef41Sopenharmony_ci KythePosition pos = MakeKythePosition(field->pos); 951cb0ef41Sopenharmony_ci kythe_entity_t field_id = that->consumer_->AddDefinition( 961cb0ef41Sopenharmony_ci KytheConsumer::Kind::ClassField, field->name_and_type.name, pos); 971cb0ef41Sopenharmony_ci that->class_fields_.insert(it, std::make_pair(field, field_id)); 981cb0ef41Sopenharmony_ci return field_id; 991cb0ef41Sopenharmony_ci} 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_civoid KytheData::AddClassFieldUse(SourcePosition use_position, 1021cb0ef41Sopenharmony_ci const Field* field) { 1031cb0ef41Sopenharmony_ci DCHECK(field); 1041cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 1051cb0ef41Sopenharmony_ci kythe_entity_t field_id = AddClassFieldDefinition(field); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci KythePosition use_pos = MakeKythePosition(use_position); 1081cb0ef41Sopenharmony_ci that->consumer_->AddUse(KytheConsumer::Kind::ClassField, field_id, use_pos); 1091cb0ef41Sopenharmony_ci} 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci// Bindings 1121cb0ef41Sopenharmony_cikythe_entity_t KytheData::AddBindingDefinition(Binding<LocalValue>* binding) { 1131cb0ef41Sopenharmony_ci CHECK(binding); 1141cb0ef41Sopenharmony_ci const uint64_t binding_index = binding->unique_index(); 1151cb0ef41Sopenharmony_ci return AddBindingDefinitionImpl(binding_index, binding->name(), 1161cb0ef41Sopenharmony_ci binding->declaration_position()); 1171cb0ef41Sopenharmony_ci} 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_cikythe_entity_t KytheData::AddBindingDefinition(Binding<LocalLabel>* binding) { 1201cb0ef41Sopenharmony_ci CHECK(binding); 1211cb0ef41Sopenharmony_ci const uint64_t binding_index = binding->unique_index(); 1221cb0ef41Sopenharmony_ci return AddBindingDefinitionImpl(binding_index, binding->name(), 1231cb0ef41Sopenharmony_ci binding->declaration_position()); 1241cb0ef41Sopenharmony_ci} 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_cikythe_entity_t KytheData::AddBindingDefinitionImpl( 1271cb0ef41Sopenharmony_ci uint64_t binding_index, const std::string& name, 1281cb0ef41Sopenharmony_ci const SourcePosition& ident_pos) { 1291cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 1301cb0ef41Sopenharmony_ci // Check if we know the binding already. 1311cb0ef41Sopenharmony_ci auto it = that->local_bindings_.find(binding_index); 1321cb0ef41Sopenharmony_ci if (it != that->local_bindings_.end()) return it->second; 1331cb0ef41Sopenharmony_ci // Register this binding. 1341cb0ef41Sopenharmony_ci kythe_entity_t binding_id = that->consumer_->AddDefinition( 1351cb0ef41Sopenharmony_ci KytheConsumer::Kind::Variable, name, MakeKythePosition(ident_pos)); 1361cb0ef41Sopenharmony_ci that->local_bindings_.insert(it, std::make_pair(binding_index, binding_id)); 1371cb0ef41Sopenharmony_ci return binding_id; 1381cb0ef41Sopenharmony_ci} 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_civoid KytheData::AddBindingUse(SourcePosition use_position, 1411cb0ef41Sopenharmony_ci Binding<LocalValue>* binding) { 1421cb0ef41Sopenharmony_ci CHECK(binding); 1431cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 1441cb0ef41Sopenharmony_ci kythe_entity_t binding_id = AddBindingDefinition(binding); 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci KythePosition use_pos = MakeKythePosition(use_position); 1471cb0ef41Sopenharmony_ci that->consumer_->AddUse(KytheConsumer::Kind::Variable, binding_id, use_pos); 1481cb0ef41Sopenharmony_ci} 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_civoid KytheData::AddBindingUse(SourcePosition use_position, 1511cb0ef41Sopenharmony_ci Binding<LocalLabel>* binding) { 1521cb0ef41Sopenharmony_ci CHECK(binding); 1531cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 1541cb0ef41Sopenharmony_ci kythe_entity_t binding_id = AddBindingDefinition(binding); 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci KythePosition use_pos = MakeKythePosition(use_position); 1571cb0ef41Sopenharmony_ci that->consumer_->AddUse(KytheConsumer::Kind::Variable, binding_id, use_pos); 1581cb0ef41Sopenharmony_ci} 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci// Types 1611cb0ef41Sopenharmony_cikythe_entity_t KytheData::AddTypeDefinition(const Declarable* type_decl) { 1621cb0ef41Sopenharmony_ci CHECK(type_decl); 1631cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 1641cb0ef41Sopenharmony_ci // Check if we know that type already. 1651cb0ef41Sopenharmony_ci auto it = that->types_.find(type_decl); 1661cb0ef41Sopenharmony_ci if (it != that->types_.end()) return it->second; 1671cb0ef41Sopenharmony_ci // Register this type. 1681cb0ef41Sopenharmony_ci KythePosition pos = MakeKythePosition(type_decl->IdentifierPosition()); 1691cb0ef41Sopenharmony_ci kythe_entity_t type_id = that->consumer_->AddDefinition( 1701cb0ef41Sopenharmony_ci KytheConsumer::Kind::Type, type_decl->type_name(), pos); 1711cb0ef41Sopenharmony_ci that->types_.insert(it, std::make_pair(type_decl, type_id)); 1721cb0ef41Sopenharmony_ci return type_id; 1731cb0ef41Sopenharmony_ci} 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_civoid KytheData::AddTypeUse(SourcePosition use_position, 1761cb0ef41Sopenharmony_ci const Declarable* type_decl) { 1771cb0ef41Sopenharmony_ci CHECK(type_decl); 1781cb0ef41Sopenharmony_ci KytheData* that = &KytheData::Get(); 1791cb0ef41Sopenharmony_ci kythe_entity_t type_id = AddTypeDefinition(type_decl); 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci KythePosition use_pos = MakeKythePosition(use_position); 1821cb0ef41Sopenharmony_ci that->consumer_->AddUse(KytheConsumer::Kind::Type, type_id, use_pos); 1831cb0ef41Sopenharmony_ci} 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci} // namespace torque 1861cb0ef41Sopenharmony_ci} // namespace internal 1871cb0ef41Sopenharmony_ci} // namespace v8 188