2020-01-12 22:59:16 -05:00
|
|
|
|
// Abstract semantic graph (ASG) intermediate representation (IR)
|
|
|
|
|
//
|
2022-05-03 14:14:29 -04:00
|
|
|
|
// Copyright (C) 2014-2022 Ryan Specialty Group, LLC.
|
2020-03-06 11:05:18 -05:00
|
|
|
|
//
|
|
|
|
|
// This file is part of TAME.
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
//! Abstract semantic graph.
|
|
|
|
|
//!
|
|
|
|
|
//! The [abstract semantic graph][asg] (ASG) is an IR representing the
|
|
|
|
|
//! relationship between objects using a directed [graph][].
|
|
|
|
|
//! An _object_ is an identifier or expression.
|
|
|
|
|
//!
|
|
|
|
|
//! Since TAME is a declarative language,
|
|
|
|
|
//! the ASG does not represent control flow;
|
|
|
|
|
//! instead, it represents the relationship between objects and their
|
|
|
|
|
//! dependencies.
|
|
|
|
|
//! Control flow is determined solely by the [linker][crate::ld] based on
|
|
|
|
|
//! these dependencies.
|
|
|
|
|
//!
|
|
|
|
|
//! See [`crate::global`] for available index sizes depending on context.
|
|
|
|
|
//! For example,
|
|
|
|
|
//! a linker may choose to use [`crate::global::ProgIdentSize`];
|
|
|
|
|
//!
|
|
|
|
|
//!
|
|
|
|
|
//! Graph Structure
|
|
|
|
|
//! ===============
|
2020-03-16 11:49:41 -04:00
|
|
|
|
//! Each node (vector) in the graph represents an [object][IdentObject],
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! such as an identifier or an expression.
|
|
|
|
|
//! Each directed edge `(A->B)` represents that `A` depends upon `B`.
|
|
|
|
|
//!
|
|
|
|
|
//! Graphs may contain cycles for recursive functions—that is,
|
|
|
|
|
//! TAME's ASG is _not_ a DAG.
|
|
|
|
|
//! Mutually recursive functions are therefore represented as
|
|
|
|
|
//! [strongly connected components][scc].
|
|
|
|
|
//!
|
|
|
|
|
//! [asg]: https://en.wikipedia.org/wiki/Abstract_semantic_graph
|
|
|
|
|
//! [graph]: https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)
|
|
|
|
|
//! [scc]: https://en.wikipedia.org/wiki/Strongly_connected_component
|
|
|
|
|
//!
|
|
|
|
|
//! Each object may have a number of valid states;
|
2020-03-16 11:49:41 -04:00
|
|
|
|
//! see [`IdentObject`] for valid object states and transitions.
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//!
|
|
|
|
|
//!
|
|
|
|
|
//! How To Use
|
|
|
|
|
//! ==========
|
|
|
|
|
//! A suitable concrete [`Asg`] implementation is provided by
|
|
|
|
|
//! [`DefaultAsg`].
|
|
|
|
|
//!
|
|
|
|
|
//! ```
|
|
|
|
|
//! use tamer::global;
|
2022-05-11 16:38:59 -04:00
|
|
|
|
//! use tamer::asg::{DefaultAsg, IdentKind, IdentObject, Source};
|
2021-09-23 14:52:53 -04:00
|
|
|
|
//! use tamer::sym::{Interner, DefaultProgInterner};
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//!
|
|
|
|
|
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
//! // Be sure to choose size and initial capacities appropriate for your
|
|
|
|
|
//! // situation.
|
2022-05-12 15:44:32 -04:00
|
|
|
|
//! let mut asg = DefaultAsg::with_capacity(
|
2020-03-14 00:10:03 -04:00
|
|
|
|
//! 1024,
|
|
|
|
|
//! 1024,
|
|
|
|
|
//! );
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//!
|
2021-09-23 14:52:53 -04:00
|
|
|
|
//! let interner = DefaultProgInterner::new();
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! let identa_sym = interner.intern("identa");
|
|
|
|
|
//! let identb_sym = interner.intern("identb");
|
|
|
|
|
//!
|
2020-03-26 00:10:47 -04:00
|
|
|
|
//! let identa = asg.declare(identa_sym, IdentKind::Meta, Source::default())?;
|
2020-03-25 23:49:37 -04:00
|
|
|
|
//! let identb = asg.declare_extern(identb_sym, IdentKind::Meta, Source::default())?;
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//!
|
|
|
|
|
//! assert_eq!(
|
2020-03-26 00:54:20 -04:00
|
|
|
|
//! Some(&IdentObject::Extern(identb_sym, IdentKind::Meta, Source::default())),
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! asg.get(identb),
|
|
|
|
|
//! );
|
|
|
|
|
//!
|
|
|
|
|
//! // Dependencies can be declared even if an identifier is
|
|
|
|
|
//! // unresolved. This declares `(identa)->(identb)`.
|
|
|
|
|
//! asg.add_dep(identa, identb);
|
|
|
|
|
//! assert!(asg.has_dep(identa, identb));
|
|
|
|
|
//!
|
|
|
|
|
//! // TODO: extern resolution
|
|
|
|
|
//!
|
|
|
|
|
//! // Identifiers are indexed by symbol name.
|
|
|
|
|
//! assert_eq!(Some(identa), asg.lookup(identa_sym));
|
|
|
|
|
//! #
|
|
|
|
|
//! # Ok(()) // main
|
|
|
|
|
//! # }
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
2020-01-14 16:26:36 -05:00
|
|
|
|
//! Missing Identifiers
|
|
|
|
|
//! -------------------
|
|
|
|
|
//! Since identifiers in TAME can be defined in any order relative to their
|
|
|
|
|
//! dependencies within a source file,
|
|
|
|
|
//! it is often the case that a dependency will have to be added to the
|
|
|
|
|
//! graph before it is resolved.
|
|
|
|
|
//! For example,
|
2020-03-16 11:49:41 -04:00
|
|
|
|
//! [`Asg::add_dep_lookup`] will add an [`IdentObject::Missing`] to the graph
|
2020-01-14 16:26:36 -05:00
|
|
|
|
//! if either identifier has not yet been declared.
|
|
|
|
|
//!
|
|
|
|
|
//! ```
|
|
|
|
|
//! # use tamer::global;
|
2022-05-11 16:38:59 -04:00
|
|
|
|
//! # use tamer::asg::{DefaultAsg, IdentKind, IdentObject, FragmentText, Source};
|
2021-09-23 14:52:53 -04:00
|
|
|
|
//! # use tamer::sym::{Interner, DefaultProgInterner};
|
2020-01-14 16:26:36 -05:00
|
|
|
|
//! #
|
|
|
|
|
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2022-05-12 15:44:32 -04:00
|
|
|
|
//! # let mut asg = DefaultAsg::with_capacity(
|
2020-03-14 00:10:03 -04:00
|
|
|
|
//! # 1024,
|
|
|
|
|
//! # 1024,
|
|
|
|
|
//! # );
|
2021-09-23 14:52:53 -04:00
|
|
|
|
//! # let interner = DefaultProgInterner::new();
|
2020-01-14 16:26:36 -05:00
|
|
|
|
//! #
|
|
|
|
|
//! let identa_sym = interner.intern("identa");
|
|
|
|
|
//! let identb_sym = interner.intern("identb");
|
|
|
|
|
//! let (identa, identb) = asg.add_dep_lookup(identa_sym, identb_sym);
|
|
|
|
|
//!
|
2020-03-16 11:49:41 -04:00
|
|
|
|
//! assert_eq!(Some(&IdentObject::Missing(identa_sym)), asg.get(identa));
|
|
|
|
|
//! assert_eq!(Some(&IdentObject::Missing(identb_sym)), asg.get(identb));
|
2020-01-14 16:26:36 -05:00
|
|
|
|
//!
|
|
|
|
|
//! // The identifiers returned above are proper objects on the graph.
|
|
|
|
|
//! assert_eq!(Some(identa), asg.lookup(identa_sym));
|
|
|
|
|
//! assert_eq!(Some(identb), asg.lookup(identb_sym));
|
|
|
|
|
//!
|
|
|
|
|
//! // Once declared, the missing identifier changes state and dependencies
|
|
|
|
|
//! // are retained.
|
2020-03-26 00:10:47 -04:00
|
|
|
|
//! asg.declare(identa_sym, IdentKind::Meta, Source::default())?;
|
2020-01-14 16:26:36 -05:00
|
|
|
|
//!
|
|
|
|
|
//! assert_eq!(
|
2020-03-16 11:49:41 -04:00
|
|
|
|
//! Some(&IdentObject::Ident(identa_sym, IdentKind::Meta, Source::default())),
|
2020-01-14 16:26:36 -05:00
|
|
|
|
//! asg.get(identa),
|
|
|
|
|
//! );
|
|
|
|
|
//!
|
|
|
|
|
//! assert!(asg.has_dep(identa, identb));
|
|
|
|
|
//! #
|
|
|
|
|
//! # Ok(()) // main
|
|
|
|
|
//! # }
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! Fragments
|
|
|
|
|
//! ---------
|
|
|
|
|
//! A compiled fragment can be attached to any resolved identifier (see
|
2020-03-16 11:49:41 -04:00
|
|
|
|
//! [`IdentObject::Ident`]) using [`Asg::set_fragment`].
|
|
|
|
|
//! Doing so changes the state of the identifier to [`IdentObject::IdentFragment`],
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! and it is an error to attempt to overwrite that fragment once it is
|
|
|
|
|
//! set.
|
|
|
|
|
//!
|
|
|
|
|
//! ```
|
|
|
|
|
//! # use tamer::global;
|
2022-05-11 16:38:59 -04:00
|
|
|
|
//! # use tamer::asg::{DefaultAsg, IdentKind, IdentObject, FragmentText, Source};
|
2022-05-16 10:53:07 -04:00
|
|
|
|
//! # use tamer::sym::{Interner, GlobalSymbolIntern};
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! #
|
|
|
|
|
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2022-05-12 15:44:32 -04:00
|
|
|
|
//! # let mut asg = DefaultAsg::with_capacity(
|
2020-03-14 00:10:03 -04:00
|
|
|
|
//! # 1024,
|
|
|
|
|
//! # 1024,
|
|
|
|
|
//! # );
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! #
|
2022-05-16 10:53:07 -04:00
|
|
|
|
//! let sym = "ident".intern();
|
|
|
|
|
//!
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! // Fragments can be attached to resolved identifiers.
|
2020-01-13 15:15:38 -05:00
|
|
|
|
//! let ident = asg.declare(
|
2022-05-16 10:53:07 -04:00
|
|
|
|
//! sym, IdentKind::Meta, Source::default()
|
2020-01-13 15:15:38 -05:00
|
|
|
|
//! )?;
|
2022-05-16 10:53:07 -04:00
|
|
|
|
//! asg.set_fragment(sym, FragmentText::from("test fragment"))?;
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//!
|
|
|
|
|
//! assert_eq!(
|
2020-03-16 11:49:41 -04:00
|
|
|
|
//! Some(&IdentObject::IdentFragment(
|
2022-05-16 10:53:07 -04:00
|
|
|
|
//! sym,
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! IdentKind::Meta,
|
2020-01-13 15:15:38 -05:00
|
|
|
|
//! Source::default(),
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! FragmentText::from("test fragment"),
|
|
|
|
|
//! )),
|
|
|
|
|
//! asg.get(ident),
|
|
|
|
|
//! );
|
|
|
|
|
//!
|
|
|
|
|
//! // But overwriting will fail
|
2022-05-16 10:53:07 -04:00
|
|
|
|
//! let bad = asg.set_fragment(sym, FragmentText::from("overwrite"));
|
2020-01-12 22:59:16 -05:00
|
|
|
|
//! assert!(bad.is_err());
|
|
|
|
|
//! #
|
|
|
|
|
//! # Ok(()) // main
|
|
|
|
|
//! # }
|
|
|
|
|
//! ```
|
|
|
|
|
|
2022-05-11 16:38:59 -04:00
|
|
|
|
mod error;
|
2020-01-12 22:59:16 -05:00
|
|
|
|
mod graph;
|
|
|
|
|
mod ident;
|
|
|
|
|
mod object;
|
|
|
|
|
|
2022-05-11 16:38:59 -04:00
|
|
|
|
pub use error::AsgError;
|
|
|
|
|
pub use graph::{Asg, AsgResult, IndexType, ObjectRef};
|
2022-05-19 11:05:20 -04:00
|
|
|
|
pub use ident::{
|
|
|
|
|
FragmentText, IdentKind, IdentObject, Source, TransitionError,
|
|
|
|
|
TransitionResult, UnresolvedError,
|
2020-03-16 11:49:41 -04:00
|
|
|
|
};
|
2020-01-12 22:59:16 -05:00
|
|
|
|
|
|
|
|
|
/// Default concrete ASG implementation.
|
2022-05-12 15:44:32 -04:00
|
|
|
|
pub type DefaultAsg = graph::Asg;
|