Skip to main content

ProgramHeader

Struct ProgramHeader 

Source
pub struct ProgramHeader<V = Value, Ty = Type> { /* private fields */ }
Expand description

Standard header directives in header order, assembled into ProgramMetadata.

No table index is written by hand. This collects directives in header order and derives every table:

  • functions: one per .func, in .func order. A .func belongs to the file named by the closest .file before it, or to the empty file "" if there is none.
  • entry: .entry @name selects the function declared by .func @name. It is optional and may appear at most once.
  • constants: one per .const, in .const order. .const str "text" is a string constant and .const fn @name a function reference; other values use the value type’s own syntax, such as u64 42.
  • source symbols: one per .source_symbol <index> "<name>"; indices are unique.
  • strings: each distinct string once, at its first occurrence, in this order: function names in .func order, then parameter names (by function, then by position), then file names in .func order, then .const str literals in .const order. Lowering then appends the string literals of .text in address order (MetadataLoweringContext interns them), so the same file always produces the same table.

Application directives compose with these in one header: an application declaration enum has a variant holding a ProgramDirective (usually with #[pattern = "$0"]) and implements StandardDirective; a loader pushes the standard directives into a ProgramHeader and handles the others itself.

use vihaco_module::module::{NoInfo, ProgramHeader};
use vihaco_parser::Parse;
use chumsky::Parser;

let header: ProgramHeader = [".file \"a.sst\"", ".func @main() -> u64, locals 0", ".entry @main"]
    .into_iter()
    .map(|line| vihaco_module::module::ProgramDirective::parser().parse(line).into_result().unwrap())
    .collect();
let metadata = header.finish(NoInfo).unwrap();
assert_eq!(metadata.strings, ["main", "a.sst"]);
assert_eq!(metadata.main_function, Some(0));

Implementations§

Source§

impl<V, Ty> ProgramHeader<V, Ty>

Source

pub fn new() -> Self

Source

pub fn push(&mut self, directive: ProgramDirective<V, Ty>)

Append the next directive.

Source

pub fn directives(&self) -> &[ProgramDirective<V, Ty>]

The directives pushed so far, in order.

Source

pub fn assemble<Info>( self, extra: Info, ) -> Result<ProgramMetadata<V, Ty, Info>, Vec<HeaderError>>
where V: ConstantValue,

Assemble the tables, reporting every problem.

Problems are duplicate .func names, locals smaller than the parameter count, more than one .entry, an .entry or .const fn naming no .func, a constant written as a table index (.const str 3 or .const fn 0), string or function constants the value model cannot represent, and duplicate .source_symbol indices. Whether each function has an @name: symbol is checked when the metadata is bound to .text (ProgramMetadata::validate).

Source

pub fn finish<Info>(self, extra: Info) -> Result<ProgramMetadata<V, Ty, Info>>
where V: ConstantValue,

assemble, failing with the first problem.

Trait Implementations§

Source§

impl<V: Clone, Ty: Clone> Clone for ProgramHeader<V, Ty>

Source§

fn clone(&self) -> ProgramHeader<V, Ty>

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<V: Debug, Ty: Debug> Debug for ProgramHeader<V, Ty>

Source§

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

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

impl<V, Ty> Default for ProgramHeader<V, Ty>

Source§

fn default() -> Self

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

impl<V, Ty> Extend<ProgramDirective<V, Ty>> for ProgramHeader<V, Ty>

Source§

fn extend<T: IntoIterator<Item = ProgramDirective<V, Ty>>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<V, Ty> FromIterator<ProgramDirective<V, Ty>> for ProgramHeader<V, Ty>

Source§

fn from_iter<T: IntoIterator<Item = ProgramDirective<V, Ty>>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<V: PartialEq, Ty: PartialEq> PartialEq for ProgramHeader<V, Ty>

Source§

fn eq(&self, other: &ProgramHeader<V, Ty>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<V: PartialEq, Ty: PartialEq> StructuralPartialEq for ProgramHeader<V, Ty>

Auto Trait Implementations§

§

impl<V, Ty> Freeze for ProgramHeader<V, Ty>

§

impl<V, Ty> RefUnwindSafe for ProgramHeader<V, Ty>

§

impl<V, Ty> Send for ProgramHeader<V, Ty>
where V: Send, Ty: Send,

§

impl<V, Ty> Sync for ProgramHeader<V, Ty>
where V: Sync, Ty: Sync,

§

impl<V, Ty> Unpin for ProgramHeader<V, Ty>
where V: Unpin, Ty: Unpin,

§

impl<V, Ty> UnsafeUnpin for ProgramHeader<V, Ty>

§

impl<V, Ty> UnwindSafe for ProgramHeader<V, Ty>
where V: UnwindSafe, Ty: UnwindSafe,

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.

§

impl<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

§

type Proj<U: 'src> = U

§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

§

impl<T> OrderedSeq<'_, T> for T
where T: Clone,

§

impl<'p, T> Seq<'p, T> for T
where T: Clone,

§

type Item<'a> = &'a T where T: 'a

The item yielded by the iterator.
§

type Iter<'a> = Once<&'a T> where T: 'a

An iterator over the items within this container, by reference.
§

fn seq_iter(&self) -> <T as Seq<'p, T>>::Iter<'_>

Iterate over the elements of the container.
§

fn contains(&self, val: &T) -> bool
where T: PartialEq,

Check whether an item is contained within this sequence.
§

fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>
where 'p: 'b,

Convert an item of the sequence into a [MaybeRef].
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.