Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Dec 11, 2024
1 parent 5422e74 commit 96bdf7b
Show file tree
Hide file tree
Showing 28 changed files with 1,213 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ use crate::cst::KindTypes;

pub type BindingGraph = metaslang_bindings::BindingGraph<KindTypes>;
pub type Definition<'a> = metaslang_bindings::Definition<'a, KindTypes>;
pub type DefinitionsIterator<'a, I> = metaslang_bindings::DefinitionsIterator<'a, KindTypes, I>;
pub type Reference<'a> = metaslang_bindings::Reference<'a, KindTypes>;
pub use metaslang_bindings::PathResolver;
pub type ReferencesIterator<'a, I> = metaslang_bindings::ReferencesIterator<'a, KindTypes, I>;
pub type BindingLocation = metaslang_bindings::BindingLocation<KindTypes>;
pub type UserFileLocation = metaslang_bindings::UserFileLocation<KindTypes>;

pub use metaslang_bindings::{BuiltInLocation, PathResolver};

pub fn create_with_resolver(
version: Version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::cst::{Cursor, KindTypes};
pub struct CompilationUnit {
language_version: Version,
files: BTreeMap<String, Rc<File>>,
binding_graph: OnceCell<Rc<BindingGraph>>,
binding_graph: OnceCell<BindingGraph>,
}

impl CompilationUnit {
Expand All @@ -37,7 +37,7 @@ impl CompilationUnit {
self.files.get(id).cloned()
}

pub fn binding_graph(&self) -> &Rc<BindingGraph> {
pub fn binding_graph(&self) -> &BindingGraph {
self.binding_graph.get_or_init(|| {
let resolver = Resolver {
files: self.files.clone(),
Expand All @@ -50,7 +50,7 @@ impl CompilationUnit {
binding_graph.add_user_file(id, file.create_tree_cursor());
}

Rc::new(binding_graph)
binding_graph
})
}
}
Expand Down
50 changes: 50 additions & 0 deletions crates/codegen/runtime/cargo/wasm/src/runtime/config.json.jinja2
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
{
"mappings": {
"nomic-foundation:slang:bindings:definition.id()": {
"Function": {
"as_getter": true
}
},
"nomic-foundation:slang:bindings:definition.name-location()": {
"Function": {
"as_getter": true
}
},
"nomic-foundation:slang:bindings:definition.definiens-location()": {
"Function": {
"as_getter": true
}
},
"nomic-foundation:slang:bindings:reference.id()": {
"Function": {
"as_getter": true
}
},
"nomic-foundation:slang:bindings:reference.location()": {
"Function": {
"as_getter": true
}
},
"nomic-foundation:slang:bindings:definitions-iterator": {
"Resource": {
"as_iterator": true
}
},
"nomic-foundation:slang:bindings:references-iterator": {
"Resource": {
"as_iterator": true
}
},
"nomic-foundation:slang:bindings:binding-location": {
"Variant": {
"as_direct_union_of_resource_classes": true
}
},
"nomic-foundation:slang:bindings:user-file-location.file-id()": {
"Function": {
"as_getter": true
}
},
"nomic-foundation:slang:bindings:user-file-location.cursor()": {
"Function": {
"as_getter": true
}
},
"nomic-foundation:slang:compilation:compilation-unit.language-version()": {
"Function": {
"as_getter": true
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,88 @@
interface bindings {
use cst.{cursor};

/// A giant graph that contains name binding information for all source files within the compilation unit.
/// It stores cursors to all definitions and references, and can resolve the edges between them.
resource binding-graph {
/// If the provided cursor points at a definition `Identifier`, it will return the
/// corresponding definition. Otherwise, it will return `undefined`.
definition-at: func(cursor: cursor) -> option<definition>;

/// Returns an iterator over all definitions in the graph.
all-definitions: func() -> definitions-iterator;

/// If the provided cursor points at a reference `Identifier`, it will return the
/// corresponding reference. Otherwise, it will return `undefined`.
reference-at: func(cursor: cursor) -> option<reference>;

/// Returns an iterator over all references in the graph.
all-references: func() -> references-iterator;
}

/// Represents a definition in the binding graph.
resource definition {
/// Returns a unique numerical identifier of the definition.
/// It is only valid for the lifetime of the binding graph.
/// It can change between multiple graphs, even for the same source code input.
id: func() -> u32;

/// Returns the location of the definition's name.
/// For `contract X {}`, that is the location of the `X` `Identifier` node.
name-location: func() -> binding-location;

/// Returns the location of the definition's definiens.
/// For `contract X {}`, that is the location of the parent `ContractDefinition` node.
definiens-location: func() -> binding-location;
}

/// Represents a reference in the binding graph.
resource reference {
/// Returns a unique numerical identifier of the reference.
/// It is only valid for the lifetime of the binding graph.
/// It can change between multiple graphs, even for the same source code input.
id: func() -> u32;

/// Returns the location of the reference.
/// For `new X()`, that is the location of the `X` `Identifier` node.
location: func() -> binding-location;

/// Returns a list of all definitions related to this reference.
/// Most references have a single definition, but some have multiple, such as when a symbol
/// is imported from another file, and renamed (re-defined) in the current file.
find-definitions: func() -> list<definition>;
}

/// Iterator over definitions in the binding graph.
resource definitions-iterator {
/// Returns the next definition in the iteration, or `undefined` if there are no more definitions.
next: func() -> option<definition>;
}

/// Iterator over references in the binding graph.
resource references-iterator {
/// Returns the next reference in the iteration, or `undefined` if there are no more references.
next: func() -> option<reference>;
}

/// Represents a location of a symbol (definition or reference) in the binding graph.
/// It can either be in a user file, or a built-in in the language.
variant binding-location {
/// Represents a location of a user-defined symbol in a user file.
user-file(user-file-location),
/// Represents a location of a built-in symbol in the language.
built-in(built-in-location)
}

/// Represents a location of a user-defined symbol in a user file.
resource user-file-location {
/// Returns the ID of the file that contains the symbol.
file-id: func() -> string;

/// Returns a cursor to the CST node that contains the symbol.
cursor: func() -> cursor;
}

/// Represents a location of a built-in symbol in the language.
resource built-in-location {
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 96bdf7b

Please sign in to comment.