Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into built-in-contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Feb 11, 2025
2 parents 04b43f4 + aea2dd0 commit 70b9c13
Show file tree
Hide file tree
Showing 76 changed files with 1,004 additions and 182 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-cooks-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

unreserve `jump` and `jumpi` yul keywords between `0.6.0` and `0.8.0`
5 changes: 5 additions & 0 deletions .changeset/famous-monkeys-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": minor
---

add `definition.references()` API to find all references that resolve to a definition.
5 changes: 5 additions & 0 deletions .changeset/famous-mugs-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

make sure assembly flags are only enabled starting from `0.8.13`
5 changes: 5 additions & 0 deletions .changeset/red-beans-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

enable yul's `true` and `false` keywords starting from `0.6.2`
5 changes: 5 additions & 0 deletions .changeset/silver-scissors-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

make sure `super` and `this` keywords are unreserved before `0.8.0`.
2 changes: 2 additions & 0 deletions Cargo.lock

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
Expand Up @@ -172,7 +172,7 @@ impl SequenceHelper {
expected_terminals: next.expected_terminals,
}));
}
// If the sequence is unwinding and and we didn't find a match, then it means
// If the sequence is unwinding and we didn't find a match, then it means
// that we recovered past it and we need to push the recovery up.
(ParserResult::SkippedUntil(_), ParserResult::NoMatch(_)) => {
// Skip any possible subsequent expected elements in this sequence until
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ interface bindings {

/// 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.
/// Most cursors pointing to identifier terminals will resolve to either a definition or a reference. For example, in `contract A is B {}` the cursor to identifier `A` will resolve to a definition, and the cursor to identifier `B` will resolve to a reference.
/// There is one specific case in which a cursor to an identifier resolves to both: a non-aliased symbol import `import {X} from "library"`, where the identifier `X` is both a definition and a reference (to the symbol exported from `"library"`).
/// Also, an identifier denoting a feature in a `pragma experimental` directive will not resolve to either.
resource binding-graph {
/// Tries to resolve the identifier terminal pointed at by the provided cursor to a definition.
/// If successful, returns the definition. Otherwise, returns `undefined`.
Expand Down Expand Up @@ -31,6 +34,9 @@ interface bindings {
/// 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;

/// Returns a list of all references that bind to this definition.
references: func() -> list<reference>;
}

/// Represents a reference in the binding graph.
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
Expand Up @@ -66,6 +66,10 @@ define_wrapper! { Definition {
fn definiens_location(&self) -> ffi::BindingLocation {
self._borrow_ffi().definiens_location()._into_ffi()
}

fn references(&self) -> Vec<ffi::Reference> {
self._borrow_ffi().references().iter().cloned().map(IntoFFI::_into_ffi).collect()
}
} }

//================================================
Expand Down

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

1 change: 1 addition & 0 deletions crates/metaslang/bindings/generated/public_api.txt

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

6 changes: 5 additions & 1 deletion crates/metaslang/bindings/src/graph/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::rc::Rc;
use metaslang_cst::cursor::Cursor;
use metaslang_cst::kinds::KindTypes;

use super::{BindingGraph, BindingLocation};
use super::{BindingGraph, BindingLocation, Reference};
use crate::builder::{FileDescriptor, GraphHandle};
use crate::graph::DisplayCursor;

Expand Down Expand Up @@ -57,6 +57,10 @@ impl<KT: KindTypes + 'static> Definition<KT> {
.get_file_descriptor(self.handle)
.expect("Definition to have a valid file descriptor")
}

pub fn references(&self) -> Vec<Reference<KT>> {
self.owner.resolve_definition(self.handle)
}
}

impl<KT: KindTypes + 'static> Display for Definition<KT> {
Expand Down
13 changes: 13 additions & 0 deletions crates/metaslang/bindings/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ impl<KT: KindTypes + 'static> BindingGraph<KT> {
})
.collect()
}

fn resolve_definition(self: &Rc<Self>, handle: GraphHandle) -> Vec<Reference<KT>> {
let mut resolver = self.resolver.borrow_mut();
resolver.ensure_all_references_resolved(&self.graph);
let references = resolver.definition_to_references(handle);
references
.iter()
.map(|handle| Reference {
owner: Rc::clone(self),
handle: *handle,
})
.collect()
}
}

struct DisplayCursor<'a, KT: KindTypes + 'static> {
Expand Down
45 changes: 45 additions & 0 deletions crates/metaslang/bindings/src/graph/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct Resolver {
partials: PartialPaths,
database: Database,
references: HashMap<GraphHandle, Vec<GraphHandle>>,
definitions: Option<HashMap<GraphHandle, Vec<GraphHandle>>>,
}

impl Resolver {
Expand All @@ -27,6 +28,7 @@ impl Resolver {
partials,
database,
references: HashMap::new(),
definitions: None,
};
resolver.build(graph);
resolver
Expand Down Expand Up @@ -182,6 +184,49 @@ impl Resolver {
Vec::new()
}
}

pub(crate) fn ensure_all_references_resolved<KT: KindTypes + 'static>(
&mut self,
graph: &ExtendedStackGraph<KT>,
) {
if self.definitions.is_some() {
return;
}

// Resolve all references
for handle in graph.iter_references() {
if !self.references.contains_key(&handle)
&& graph
.get_file_descriptor(handle)
.is_some_and(|file| file.is_user())
{
let definition_handles = self.resolve_internal(graph, handle, true);
self.references.insert(handle, definition_handles);
}
}

// Build reverse mapping from definitions to reference handles
let mut definitions: HashMap<GraphHandle, Vec<GraphHandle>> = HashMap::new();
for (reference, resolved_definitions) in &self.references {
for definition in resolved_definitions {
if let Some(references) = definitions.get_mut(definition) {
references.push(*reference);
} else {
definitions.insert(*definition, vec![*reference]);
}
}
}
self.definitions = Some(definitions);
}

pub(crate) fn definition_to_references(&self, handle: GraphHandle) -> Vec<GraphHandle> {
self.definitions
.as_ref()
.expect("All references should have been resolved")
.get(&handle)
.cloned()
.unwrap_or_default()
}
}

// This is a partial paths database, but we also need to keep track of edges
Expand Down
47 changes: 35 additions & 12 deletions crates/solidity/inputs/language/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,10 @@ codegen_language_macros::compile!(Language(
Keyword(
name = SuperKeyword,
identifier = Identifier,
definitions = [KeywordDefinition(value = Atom("super"))]
definitions = [KeywordDefinition(
reserved = From("0.8.0"),
value = Atom("super")
)]
),
Keyword(
name = SupportsKeyword,
Expand Down Expand Up @@ -1423,7 +1426,10 @@ codegen_language_macros::compile!(Language(
Keyword(
name = ThisKeyword,
identifier = Identifier,
definitions = [KeywordDefinition(value = Atom("this"))]
definitions = [KeywordDefinition(
reserved = From("0.8.0"),
value = Atom("this")
)]
),
Keyword(
name = ThrowKeyword,
Expand Down Expand Up @@ -2889,12 +2895,16 @@ codegen_language_macros::compile!(Language(
fields = (
assembly_keyword = Required(AssemblyKeyword),
label = Optional(reference = StringLiteral),
flags = Optional(reference = AssemblyFlagsDeclaration),
flags = Optional(
reference = AssemblyFlagsDeclaration,
enabled = From("0.8.13")
),
body = Required(YulBlock)
)
),
Struct(
name = AssemblyFlagsDeclaration,
enabled = From("0.8.13"),
error_recovery = FieldsErrorRecovery(
delimiters =
FieldDelimiters(open = open_paren, close = close_paren)
Expand All @@ -2908,7 +2918,8 @@ codegen_language_macros::compile!(Language(
Separated(
name = AssemblyFlags,
reference = StringLiteral,
separator = Comma
separator = Comma,
enabled = From("0.8.13")
)
]
),
Expand Down Expand Up @@ -4410,8 +4421,8 @@ codegen_language_macros::compile!(Language(
Enum(
name = YulLiteral,
variants = [
EnumVariant(reference = YulTrueKeyword),
EnumVariant(reference = YulFalseKeyword),
EnumVariant(reference = YulTrueKeyword, enabled = From("0.6.2")),
EnumVariant(reference = YulFalseKeyword, enabled = From("0.6.2")),
EnumVariant(reference = YulDecimalLiteral),
EnumVariant(reference = YulHexLiteral),
EnumVariant(reference = HexStringLiteral),
Expand Down Expand Up @@ -4745,7 +4756,10 @@ codegen_language_macros::compile!(Language(
Keyword(
name = YulFalseKeyword,
identifier = YulIdentifier,
definitions = [KeywordDefinition(value = Atom("false"))]
definitions = [KeywordDefinition(
enabled = From("0.6.2"),
value = Atom("false")
)]
),
Keyword(
name = YulFinalKeyword,
Expand Down Expand Up @@ -5504,8 +5518,11 @@ codegen_language_macros::compile!(Language(
Keyword(
name = YulSuperKeyword,
identifier = YulIdentifier,
definitions =
[KeywordDefinition(enabled = Never, value = Atom("super"))]
definitions = [KeywordDefinition(
enabled = Never,
reserved = From("0.8.0"),
value = Atom("super")
)]
),
Keyword(
name = YulSupportsKeyword,
Expand Down Expand Up @@ -5533,8 +5550,11 @@ codegen_language_macros::compile!(Language(
Keyword(
name = YulThisKeyword,
identifier = YulIdentifier,
definitions =
[KeywordDefinition(enabled = Never, value = Atom("this"))]
definitions = [KeywordDefinition(
enabled = Never,
reserved = From("0.8.0"),
value = Atom("this")
)]
),
Keyword(
name = YulThrowKeyword,
Expand All @@ -5548,7 +5568,10 @@ codegen_language_macros::compile!(Language(
Keyword(
name = YulTrueKeyword,
identifier = YulIdentifier,
definitions = [KeywordDefinition(value = Atom("true"))]
definitions = [KeywordDefinition(
enabled = From("0.6.2"),
value = Atom("true")
)]
),
Keyword(
name = YulTryKeyword,
Expand Down

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

Loading

0 comments on commit 70b9c13

Please sign in to comment.