1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2018 Fabian Schuiki

use std::fmt;

use crate::common::source::{Span, Spanned};
use crate::common::score::Result;

use crate::score::ResolvableName;
use crate::hir::visit::Visitor;

/// Construct something from an AST node.
pub trait FromAst<'t>: Sized {
    type AllocInput: 't;
    type LatentInput: 't;
    type Context: 't;
    type Latent;

    /// Schedule construction of an HIR node from an AST node.
    ///
    /// This function performs initial setup, e.g. name declaration in the
    /// context, and then creates a `Slot` that constructs a ndoe of type `Self`
    /// on demand.
    fn alloc_slot(input: Self::AllocInput, context: Self::Context) -> Result<Self::Latent>;

    /// Construct an HIR node from an AST node.
    fn from_ast(input: Self::LatentInput, context: Self::Context) -> Result<Self>;
}

/// Common functions of HIR nodes.
pub trait Node<'t>: fmt::Debug {
    /// The source file location of this node.
    fn span(&self) -> Span;

    /// A human-readable description of the node's kind.
    ///
    /// For example "package" or "entity".
    fn desc_kind(&self) -> String;

    /// A human-readable description of the node, including its name.
    ///
    /// E.g. `package 'foo'` or `entity 'adder'`.
    fn desc_name(&self) -> String {
        self.desc_kind()
    }

    /// Accept a visitor and call its corresponding `visit_*` function.
    fn accept(&'t self, visitor: &mut Visitor<'t>);

    /// Walk a visitor over the node's subtree.
    fn walk(&'t self, visitor: &mut Visitor<'t>);
}

impl<'a, T: Node<'a>> From<&'a T> for &'a Node<'a> {
    fn from(t: &'a T) -> &'a Node<'a> {
        t
    }
}

/// Lazily resolve to a `Node`.
pub trait LatentNode<'t, T: 't + ?Sized>: fmt::Debug {
    /// Access the underlying node.
    ///
    /// On the first time this function is called, the node is created.
    /// Subsequent calls are guaranteed to return the same node. Node creation
    /// may fail for a variety of reasons, thus the function returns a `Result`.
    fn poll(&self) -> Result<&'t T>;

    /// Accept a visitor.
    ///
    /// Polls the latent node and if successful calls `accept()` on it.
    fn accept(&self, visitor: &mut Visitor<'t>);

    /// Walk a visitor over the latent node's subtree.
    ///
    /// Polls the latent node and if successful calls `walk()` on it.
    fn walk(&self, visitor: &mut Visitor<'t>);
}

/// Common functions of declaration HIR node.
pub trait Decl2<'t>: Node<'t> {
    /// The name of the declared item.
    fn name(&self) -> Spanned<ResolvableName>;
}

impl<'a, T: Decl2<'a>> From<&'a T> for &'a Decl2<'a> {
    fn from(t: &'a T) -> &'a Decl2<'a> {
        t
    }
}