Skip to main content

Index

Struct Index 

Source
pub struct Index { /* private fields */ }
Expand description

The persistent definition/reference index of a resolved program.

§Examples

use hir_lang::{Builder, Name, Span};
use intern_lang::Interner;

// fn f(n) { n }  — the use of `n` is a reference to the parameter.
let mut names = Interner::new();
let n = Name::new(names.intern("n"));
let mut b = Builder::new();
b.set_span(Span::new(5, 6));
let (param, _) = b.local_param(n);
b.set_span(Span::new(10, 11));
let use_n = b.name_expr(n);
let body = b.block(&[], Some(use_n));
let f = b.func(Name::new(names.intern("f")), &[param], body);
let root = b.module(None, &[f]);
let hir = b.finish(root)?;
let unit = hir.unit();

let res = resolve_lang::resolve(hir, &names)?;
let index = res.index();
let hit = index.at(unit, 10).unwrap();
let def = index.definition(hit.def).unwrap();
assert_eq!(def.location.unwrap().span, Span::new(5, 6));
assert_eq!(index.references(hit.def).len(), 1);

Implementations§

Source§

impl Index

Source

pub fn definitions(&self) -> &[Definition]

Every definition, in unit order, then preorder (externals last).

§Examples
use resolve_lang::Index;

assert!(Index::default().definitions().is_empty());
Source

pub fn references_all(&self) -> &[Reference]

Every reference, by unit, then path, then segment.

§Examples
use resolve_lang::Index;

assert!(Index::default().references_all().is_empty());
Source

pub fn definition(&self, def: DefRef) -> Option<&Definition>

One definition.

§Examples
use resolve_lang::{DefRef, Index};

assert!(Index::default().definition(DefRef::from_index(0)).is_none());
Source

pub fn references(&self, def: DefRef) -> References<'_> ⓘ

The references to a definition, sorted by location (find-references). Allocation-free: an iterator over the definition’s slice.

§Examples
use resolve_lang::{DefRef, Index};

assert_eq!(Index::default().references(DefRef::from_index(0)).len(), 0);
Source

pub fn def_of(&self, target: Target) -> Option<DefRef>

The definition of a target (an item, variant, binder, aliased import, or a referenced external symbol).

§Examples
use hir_lang::{BinderId, UnitId};
use resolve_lang::{Index, Target};

let t = Target::Local(UnitId::new(0), BinderId::from_index(0).unwrap());
assert!(Index::default().def_of(t).is_none());
Source

pub fn resolve_at( &self, unit: UnitId, path: PathId, segment: u32, ) -> Option<DefRef>

Go-to-definition by HIR id: what segment segment of path names.

§Examples
use hir_lang::{PathId, UnitId};
use resolve_lang::Index;

assert!(Index::default().resolve_at(UnitId::new(0), PathId::from_index(0).unwrap(), 0).is_none());
Source

pub fn path_references(&self, unit: UnitId, path: PathId) -> &[Reference]

The references inside one path, by segment.

§Examples
use hir_lang::{PathId, UnitId};
use resolve_lang::Index;

assert!(Index::default().path_references(UnitId::new(0), PathId::from_index(0).unwrap()).is_empty());
Source

pub fn at(&self, unit: UnitId, offset: u32) -> Option<Occurrence>

Go-to-definition by position: the definition or reference whose name contains byte offset of unit’s source.

§Examples
use hir_lang::UnitId;
use resolve_lang::Index;

assert!(Index::default().at(UnitId::new(0), 3).is_none());
Source

pub fn rename_set(&self, def: DefRef) -> RenameSet

Every edit renaming a definition implies: its own name and every reference that spells the same name, up to the case folding of a case-insensitive table (references through an aliased import spell the alias and are left alone). Renaming an aliased import edits the alias and the references that went through it.

§Examples
use resolve_lang::{DefRef, Index};

assert!(Index::default().rename_set(DefRef::from_index(0)).edits.is_empty());
Source

pub fn document_symbols(&self, unit: UnitId) -> Vec<(DefRef, &Definition)>

The item definitions of a unit in preorder (with parent links), for a document outline.

§Examples
use hir_lang::UnitId;
use resolve_lang::Index;

assert!(Index::default().document_symbols(UnitId::new(0)).is_empty());

Trait Implementations§

Source§

impl Clone for Index

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Index

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Index

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Index

§

impl RefUnwindSafe for Index

§

impl Send for Index

§

impl Sync for Index

§

impl Unpin for Index

§

impl UnsafeUnpin for Index

§

impl UnwindSafe for Index

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.