use std::collections::{HashMap, HashSet};
use crate::format::*;
mod encode;
mod typed;
mod typed_cfg;
pub(crate) mod typed_generics;
mod typed_macro;
pub use self::typed_macro::RawMacroDef;
#[cfg(test)]
use self::encode::align_up;
#[derive(Debug, Clone)]
pub struct RawDefinition {
pub def_id: u32,
pub kind: DefKind,
pub visibility: u8,
pub is_macro_expansion: bool,
pub name: String,
pub path: String,
pub signature: Option<String>,
pub file: String,
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
}
#[derive(Debug, Clone)]
pub struct RawReference {
pub file: String,
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
pub target_def_id: Option<u32>,
pub target_path: String,
pub target_file: Option<String>,
pub target_unit: Option<String>,
pub target_line: u32,
pub target_col: u32,
pub kind: DefKind,
pub is_write: bool,
pub is_macro_expansion: bool,
}
#[derive(Debug, Clone)]
pub struct RawType {
pub file: String,
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
pub type_name: String,
}
#[derive(Debug, Clone)]
pub struct RawNote {
pub file: String,
pub start_line: u32,
pub start_col: u32,
pub end_line: u32,
pub end_col: u32,
pub kind: u32,
pub text: String,
}
pub struct IndexWriter {
crate_name: String,
crate_version: String,
string_pool: Vec<u8>,
string_map: HashMap<String, u32>,
file_map: HashMap<String, u32>,
file_offsets: Vec<u32>,
definitions: Vec<RawDefinition>,
def_owners: Vec<(u32, String)>,
references: Vec<RawReference>,
types: Vec<(RawType, Option<String>)>,
derefs: Vec<(String, String)>,
notes: Vec<RawNote>,
ty_interner: crate::ty_arena::TyInterner,
fn_sigs: Vec<FnSigEntry>,
impls: Vec<typed::PendingImpl>,
fields: Vec<FieldEntry>,
def_tys: Vec<DefTyEntry>,
def_generic_params: Vec<GenericParamEntry>,
def_where_preds: Vec<WherePredEntry>,
lang_items: Vec<LangItemEntry>,
uses: Vec<UseEntry>,
cfgs: Vec<CfgEntry>,
macro_defs: Vec<MacroDefEntry>,
target_triple: Option<String>,
unit_key: Option<String>,
generation: Option<String>,
file_hashes: HashMap<String, u64>,
reexport_indices: HashSet<u32>,
}
impl IndexWriter {
pub fn new(crate_name: impl Into<String>, crate_version: impl Into<String>) -> Self {
let mut writer = Self {
crate_name: crate_name.into(),
crate_version: crate_version.into(),
string_pool: Vec::with_capacity(64 * 1024),
string_map: HashMap::new(),
file_map: HashMap::new(),
file_offsets: Vec::new(),
definitions: Vec::new(),
def_owners: Vec::new(),
references: Vec::new(),
types: Vec::new(),
derefs: Vec::new(),
notes: Vec::new(),
ty_interner: crate::ty_arena::TyInterner::new(),
fn_sigs: Vec::new(),
impls: Vec::new(),
fields: Vec::new(),
def_tys: Vec::new(),
def_generic_params: Vec::new(),
def_where_preds: Vec::new(),
lang_items: Vec::new(),
uses: Vec::new(),
cfgs: Vec::new(),
macro_defs: Vec::new(),
target_triple: None,
unit_key: None,
generation: None,
file_hashes: HashMap::new(),
reexport_indices: HashSet::new(),
};
writer.string_pool.push(0);
writer.string_map.insert(String::new(), 0);
writer
}
}
impl Default for IndexWriter {
fn default() -> Self {
Self::new("", "")
}
}
impl IndexWriter {
pub fn set_unit_key(&mut self, unit_key: impl Into<String>) {
self.unit_key = Some(unit_key.into());
}
pub fn unit_key(&self) -> Option<&str> {
self.unit_key.as_deref()
}
pub fn set_generation(&mut self, generation: impl Into<String>) {
self.generation = Some(generation.into());
}
pub fn generation(&self) -> Option<&str> {
self.generation.as_deref()
}
pub fn set_file_hashes(&mut self, hashes: HashMap<String, u64>) {
self.file_hashes = hashes;
}
pub fn add_file_hash(&mut self, file: &str, hash: u64) {
self.file_hashes.insert(file.to_string(), hash);
}
pub fn intern_string(&mut self, s: &str) -> u32 {
Self::intern_in_pool(&mut self.string_pool, &mut self.string_map, s)
}
pub(crate) fn intern_in_pool(string_pool: &mut Vec<u8>, string_map: &mut HashMap<String, u32>, s: &str) -> u32 {
if string_pool.is_empty() {
string_pool.push(0);
string_map.insert(String::new(), 0);
}
if let Some(&offset) = string_map.get(s) {
return offset;
}
let offset = string_pool.len() as u32;
string_pool.extend_from_slice(s.as_bytes());
string_pool.push(0); string_map.insert(s.to_string(), offset);
offset
}
pub fn get_or_insert_file(&mut self, path: &str) -> u32 {
if let Some(&id) = self.file_map.get(path) {
return id;
}
let id = self.file_offsets.len() as u32;
let offset = self.intern_string(path);
self.file_offsets.push(offset);
self.file_map.insert(path.to_string(), id);
id
}
pub fn add_definition(&mut self, def: RawDefinition) -> u32 {
let idx = self.definitions.len() as u32;
if let Some((parent, _)) = def.path.rsplit_once("::") {
if !parent.is_empty() {
self.def_owners.push((idx, parent.to_string()));
}
}
self.definitions.push(def);
idx
}
pub fn add_definition_unowned(&mut self, def: RawDefinition) -> u32 {
let idx = self.definitions.len() as u32;
self.definitions.push(def);
idx
}
pub fn add_definition_with_owners(&mut self, def: RawDefinition, owners: Vec<String>) -> u32 {
let idx = self.definitions.len() as u32;
for owner in owners {
if !owner.is_empty() {
self.def_owners.push((idx, owner));
}
}
if let Some((parent, _)) = def.path.rsplit_once("::") {
if !parent.is_empty() {
self.def_owners.push((idx, parent.to_string()));
}
}
self.definitions.push(def);
idx
}
pub fn add_reexport_with_owners(&mut self, def: RawDefinition, owners: Vec<String>) -> u32 {
let idx = self.definitions.len() as u32;
self.reexport_indices.insert(idx);
self.add_definition_with_owners(def, owners)
}
pub fn add_def_owner(&mut self, def_id: u32, owner: String) {
if let Some(idx) = self.definitions.iter().position(|d| d.def_id == def_id) {
self.def_owners.push((idx as u32, owner));
}
}
pub fn add_def_owner_by_index(&mut self, idx: u32, owner: String) {
debug_assert!((idx as usize) < self.definitions.len(), "def index {idx} out of bounds");
if (idx as usize) < self.definitions.len() && !owner.is_empty() {
self.def_owners.push((idx, owner));
}
}
pub fn add_reference(&mut self, r: RawReference) {
self.references.push(r);
}
pub fn add_type(&mut self, t: RawType) {
self.types.push((t, None));
}
pub fn add_type_with_owner(&mut self, t: RawType, owner_path: Option<String>) {
self.types.push((t, owner_path));
}
pub fn add_deref(&mut self, source_type: impl Into<String>, target_type: impl Into<String>) {
self.derefs.push((source_type.into(), target_type.into()));
}
pub fn add_note(&mut self, note: RawNote) {
self.notes.push(note);
}
pub fn note_count(&self) -> usize {
self.notes.len()
}
pub fn definition_count(&self) -> usize {
self.definitions.len()
}
pub fn reference_count(&self) -> usize {
self.references.len()
}
pub fn type_count(&self) -> usize {
self.types.len()
}
pub fn deref_count(&self) -> usize {
self.derefs.len()
}
pub fn indexed_files(&self) -> Vec<String> {
let mut set = std::collections::HashSet::new();
for k in self.file_map.keys() {
set.insert(k.clone());
}
for def in &self.definitions {
set.insert(def.file.clone());
}
for r in &self.references {
set.insert(r.file.clone());
}
for t in &self.types {
set.insert(t.0.file.clone());
}
let mut list: Vec<String> = set.into_iter().collect();
list.sort();
list
}
}
#[cfg(test)]
mod tests;