ConstEnvironment

Trait ConstEnvironment 

Source
pub trait ConstEnvironment {
    type Error: From<ConstEvalError>;

    // Required methods
    fn get_source_file_for(&self, span: SourceSpan) -> Option<Arc<SourceFile>>;
    fn get(
        &self,
        name: &Ident,
    ) -> Result<Option<CachedConstantValue<'_>>, Self::Error>;
    fn get_by_path(
        &self,
        path: Span<&Path>,
    ) -> Result<Option<CachedConstantValue<'_>>, Self::Error>;

    // Provided methods
    fn get_error(&self, name: &Ident) -> Result<Option<Arc<str>>, Self::Error> { ... }
    fn get_error_by_path(
        &self,
        path: Span<&Path>,
    ) -> Result<Option<Arc<str>>, Self::Error> { ... }
    fn on_eval_start(&mut self, path: Span<&Path>) { ... }
    fn on_eval_completed(&mut self, name: Span<&Path>, value: &ConstantExpr) { ... }
}
Expand description

There are two phases to constant evaluation, one during semantic analysis, and another phase performed during linking of the final assembly, on any constant expressions that were left partially or unevaluated during semantic analysis due to external references. This trait is used to abstract over the environment in which the evaluator runs, so that we can use it in both phases by simply providing a suitable implementation.

Required Associated Types§

Source

type Error: From<ConstEvalError>

The error type used in the current evaluation phase.

The error type must support infallible conversions from ConstEvalError.

Required Methods§

Source

fn get_source_file_for(&self, span: SourceSpan) -> Option<Arc<SourceFile>>

Map a SourceSpan to the SourceFile to which it refers

Source

fn get( &self, name: &Ident, ) -> Result<Option<CachedConstantValue<'_>>, Self::Error>

Get the constant expression/value bound to name in the current scope.

Implementations should return Ok(None) if the symbol is defined, but not yet resolvable to a concrete definition.

Source

fn get_by_path( &self, path: Span<&Path>, ) -> Result<Option<CachedConstantValue<'_>>, Self::Error>

Get the constant expression/value defined at path, which is resolved using the imports and definitions in the current scope.

This function should return Ok(None) if unresolvable external references should be left unevaluated, rather than treated as an undefined symbol error.

This function should return Err if any of the following are true:

  • The path cannot be resolved, and the implementation wishes this to be treated as an error
  • The definition of the constant was found, but it does not have public visibility

Provided Methods§

Source

fn get_error(&self, name: &Ident) -> Result<Option<Arc<str>>, Self::Error>

A specialized form of ConstEnvironment::get, which validates that the constant expression returned by get evaluates to an error string, returning that string, or raising an error if invalid.

Source

fn get_error_by_path( &self, path: Span<&Path>, ) -> Result<Option<Arc<str>>, Self::Error>

A specialized form of ConstEnvironment::get_by_path, which validates that the constant expression returned by get_by_path evaluates to an error string, returning that string, or raising an error if invalid.

Source

fn on_eval_start(&mut self, path: Span<&Path>)

This method is called when the evaluator begins to evaluate the constant at path

Source

fn on_eval_completed(&mut self, name: Span<&Path>, value: &ConstantExpr)

This method is called when the evaluator has finished evaluating the constant at path.

The value here is the value produced as the result of evaluation.

Implementors§