tamer: asg::air: Root AirIdent operations using AirAggregateCtx

This is the culmination of a great deal of work over the past few
weeks.  Indeed, this change has been prototyped a number of different ways
and has lived in a stash of mine, in one form or another, for a few weeks.

This is not done just yet---I have to finish moving the index out of Asg,
and then clean up a little bit more---but this is a significant
simplification of the system.  It was very difficult to reason about prior
approaches, and this finally moves toward doing something that I wasn't sure
if I'd be able to do successfully: formalize scope using AirAggregate's
stack and encapsulate indexing as something that is _supplemental_ to the
graph, rather than an integral component of it.

This _does not yet_ index the AirIdent operation on the package itself
because the active state is not part of the stack; that is one of the
remaining changes I still have stashed.  It will be needed shortly for
package imports.

This rationale will have to appear in docs, which I intend to write soon,
but: this means that `Asg` contains _resolved_ data and itself has no
concept of scope.  The state of the ASG immediately after parsing _can_ be
used to derive what the scope _must_ be (and indeed that's what
`asg::air::test::scope::derive_scopes_from_asg` does), but once we start
performing optimizations, that will no longer be true in all cases.

This means that lexical scope is a property of parsing, which, well, seems
kind of obvious from its name.  But the awkwardness was that, if we consider
scope to be purely a parse-time thing---used only to construct the
relationships on the graph and then be discarded---then how do we query for
information on the graph?  We'd have to walk the graph in search of an
identifier, which is slow.

But when do we need to do such a thing?  For tests, it doesn't matter if
it's a little bit slow, and the graphs aren't all that large.  And for
operations like template expansion and optimizations, if they need access to
a particular index, then we'll be sure to generate or provide the
appropriate one.  If we need a central database of identifiers for tooling
in the future, we'll create one then.  No general-purpose identifier lookup
_is_ actually needed.

And with that, `Asg::lookup_or_missing` is removed.  It has been around
since the beginning of the ASG, when the linker was just a prototype, so
it's the end of TAMER's early era as I was trying to discover exactly what I
wanted the ASG to represent.

DEV-13162
main
Mike Gerwitz 2023-05-17 11:57:46 -04:00
parent 716e217c9f
commit 94bbc2d725
6 changed files with 74 additions and 91 deletions

View File

@ -601,6 +601,16 @@ impl AirAggregateCtx {
/// it does not form a hierarchy and local identifiers will not be
/// indexed outside of their package hierarchy,
/// so we'll have to continue searching for those.
///
/// The provided name's span is used to seed the missing object with
/// some sort of context to aid in debugging why a missing object
/// was introduced to the graph.
/// The provided span will be used by the returned [`ObjectIndex`] even
/// if an object exists on the graph,
/// which can be used for retaining information on the location that
/// requested the object.
/// To retrieve the span of a previously declared object,
/// you must resolve the [`ObjectIndex`] and inspect it.
fn lookup_lexical_or_missing(&mut self, name: SPair) -> ObjectIndex<Ident> {
let Self(asg, stack, _) = self;

View File

@ -128,53 +128,35 @@ impl ParseState for AirPkgAggregate {
.map(|_| ())
.transition(Toplevel(oi_pkg)),
(Toplevel(oi_pkg), AirIdent(IdentDecl(name, kind, src))) => {
let asg = ctx.asg_mut();
let oi_root = asg.root(name);
asg.lookup_or_missing(oi_root, name)
.declare(asg, name, kind, src)
.map(|_| ())
.transition(Toplevel(oi_pkg))
}
(Toplevel(oi_pkg), AirIdent(IdentDecl(name, kind, src))) => ctx
.lookup_lexical_or_missing(name)
.declare(ctx.asg_mut(), name, kind, src)
.map(|_| ())
.transition(Toplevel(oi_pkg)),
(Toplevel(oi_pkg), AirIdent(IdentExternDecl(name, kind, src))) => {
let asg = ctx.asg_mut();
let oi_root = asg.root(name);
asg.lookup_or_missing(oi_root, name)
.declare_extern(asg, name, kind, src)
ctx.lookup_lexical_or_missing(name)
.declare_extern(ctx.asg_mut(), name, kind, src)
.map(|_| ())
.transition(Toplevel(oi_pkg))
}
(Toplevel(oi_pkg), AirIdent(IdentDep(name, dep))) => {
let asg = ctx.asg_mut();
let oi_root = asg.root(dep);
let oi_from = asg.lookup_or_missing(oi_root, name);
let oi_to = asg.lookup_or_missing(oi_root, dep);
let oi_from = ctx.lookup_lexical_or_missing(name);
let oi_to = ctx.lookup_lexical_or_missing(dep);
oi_from.add_opaque_dep(ctx.asg_mut(), oi_to);
Transition(Toplevel(oi_pkg)).incomplete()
}
(Toplevel(oi_pkg), AirIdent(IdentFragment(name, text))) => {
let asg = ctx.asg_mut();
let oi_root = asg.root(name);
asg.lookup_or_missing(oi_root, name)
.set_fragment(asg, text)
.map(|_| ())
.transition(Toplevel(oi_pkg))
}
(Toplevel(oi_pkg), AirIdent(IdentFragment(name, text))) => ctx
.lookup_lexical_or_missing(name)
.set_fragment(ctx.asg_mut(), text)
.map(|_| ())
.transition(Toplevel(oi_pkg)),
(Toplevel(oi_pkg), AirIdent(IdentRoot(name))) => {
let asg = ctx.asg_mut();
let oi_root = asg.root(name);
let oi_ident = asg.lookup_or_missing(oi_root, name);
oi_root.root_ident(asg, oi_ident);
ctx.lookup_lexical_or_missing(name).root(ctx.asg_mut());
Transition(Toplevel(oi_pkg)).incomplete()
}

View File

@ -325,6 +325,52 @@ test_scopes! {
];
}
// From the perspective of the linker (tameld):
test_scopes! {
setup {
let pkg_a = SPair("/pkg/a".into(), S1);
let opaque_a = SPair("opaque_a".into(), S2);
let pkg_b = SPair("/pkg/b".into(), S4);
let opaque_b = SPair("opaque_b".into(), S5);
}
air {
[
// ENV: 0 global lexical scoping boundaries (envs)
PkgStart(S1, pkg_a), //- -.
// ENV: 1 pkg // :
IdentDecl( // v :v
opaque_a, // :
IdentKind::Meta, // :
Default::default(), // :
), // 1:
PkgEnd(S3), //- -'
// 0
PkgStart(S4, pkg_b), //- -.
// ENV: 1 pkg // 1:
IdentDecl( // v :v
opaque_b, // :
IdentKind::Meta, // :
Default::default(), // :
), // :
PkgEnd(S6), //- -'
]
}
#[test]
opaque_a == [
(Root, S0, Visible),
// TODO: (Pkg, m(S1, S3), Visible),
];
#[test]
opaque_b == [
(Root, S0, Visible),
// TODO: (Pkg, m(S4, S6), Visible),
];
}
///// Tests end above this line, plumbing below /////
/// Independently derive identifier scopes from the graph.

View File

@ -22,8 +22,8 @@
//! ![Visualization of ASG ontology](../ontviz.svg)
use self::object::{
DynObjectRel, NameableMissingObject, ObjectIndexRelTo, ObjectRelFrom,
ObjectRelTy, ObjectRelatable, Root,
DynObjectRel, ObjectIndexRelTo, ObjectRelFrom, ObjectRelTy,
ObjectRelatable, Root,
};
use super::{air::EnvScopeKind, AsgError, Object, ObjectIndex, ObjectKind};
@ -271,40 +271,6 @@ impl Asg {
}
}
/// Lookup `name or add a missing object to the graph relative to
/// the immediate environment `imm_env` and return a reference to it.
///
/// The provided span is necessary to seed the missing object with
/// some sort of context to aid in debugging why a missing object
/// was introduced to the graph.
/// The provided span will be used by the returned [`ObjectIndex`] even
/// if an object exists on the graph,
/// which can be used for retaining information on the location that
/// requested the object.
/// To retrieve the span of a previously declared object,
/// you must resolve the [`ObjectIndex`] and inspect it.
///
/// See [`Self::index`] for more information.
pub(super) fn lookup_or_missing<O: ObjectRelatable>(
&mut self,
imm_env: impl ObjectIndexRelTo<O>,
name: SPair,
) -> ObjectIndex<O>
where
O: NameableMissingObject,
{
self.lookup(imm_env, name).unwrap_or_else(|| {
let oi = self.create(O::missing(name));
// TODO: This responsibility is split between `Asg` and
// `AirAggregateCtx`!
let eoi = EnvScopeKind::Visible(oi);
self.index(imm_env, name.symbol(), eoi);
oi
})
}
/// Root object.
///
/// All [`Object`]s reachable from the root will be included in the

View File

@ -1065,18 +1065,3 @@ fn container_oops() -> Vec<AnnotatedSpan<'static>> {
),
]
}
/// An [`ObjectKind`] with a representation of a missing object associated
/// with a [`SymbolId`](crate::sym::SymbolId).
///
/// A _missing_ object has a node on the graph to which edges may be
/// drawn for when the object is eventually defined.
/// Missing objects are identified by a [`SymbolId`](crate::sym::SymbolId),
/// allowing them to be later retrieved for definition.
pub trait NameableMissingObject: ObjectKind {
/// Represent an object as missing on the [`Asg`].
///
/// The provided `name` will be used to later look up the object to
/// provide a definition.
fn missing(name: SPair) -> Self;
}

View File

@ -19,7 +19,7 @@
//! Identifiers (a type of [object](super)).
use super::{prelude::*, Expr, Meta, NameableMissingObject, Pkg, Tpl};
use super::{prelude::*, Expr, Meta, Pkg, Tpl};
use crate::{
diagnose::{Annotate, Diagnostic},
diagnostic_todo,
@ -169,12 +169,6 @@ impl Display for Ident {
}
}
impl NameableMissingObject for Ident {
fn missing(name: SPair) -> Self {
Self::Missing(name)
}
}
impl Ident {
/// Identifier name.
pub fn name(&self) -> SPair {