charon_lib/ast/
gast_utils.rs

1//! Implementations for [crate::gast]
2
3use crate::ast::*;
4use crate::llbc_ast;
5use crate::ullbc_ast;
6
7impl FunIdOrTraitMethodRef {
8    pub fn mk_builtin(aid: BuiltinFunId) -> Self {
9        Self::Fun(FunId::Builtin(aid))
10    }
11}
12
13impl Body {
14    pub fn as_unstructured(&self) -> Option<&ullbc_ast::ExprBody> {
15        if let Self::Unstructured(v) = self {
16            Some(v)
17        } else {
18            None
19        }
20    }
21    pub fn as_unstructured_mut(&mut self) -> Option<&mut ullbc_ast::ExprBody> {
22        if let Self::Unstructured(v) = self {
23            Some(v)
24        } else {
25            None
26        }
27    }
28
29    pub fn as_structured(&self) -> Option<&llbc_ast::ExprBody> {
30        if let Self::Structured(v) = self {
31            Some(v)
32        } else {
33            None
34        }
35    }
36    pub fn as_structured_mut(&mut self) -> Option<&mut llbc_ast::ExprBody> {
37        if let Self::Structured(v) = self {
38            Some(v)
39        } else {
40            None
41        }
42    }
43}
44
45impl Locals {
46    /// Creates a new variable and returns a place pointing to it.
47    pub fn new_var(&mut self, name: Option<String>, ty: Ty) -> Place {
48        let local_id = self.locals.push_with(|index| Local {
49            index,
50            name,
51            ty: ty.clone(),
52        });
53        Place::new(local_id, ty)
54    }
55
56    /// Gets a place pointing to the corresponding variable.
57    pub fn place_for_var(&self, local_id: LocalId) -> Place {
58        let ty = self.locals[local_id].ty.clone();
59        Place::new(local_id, ty)
60    }
61
62    /// The place where we write the return value.
63    pub fn return_place(&self) -> Place {
64        self.place_for_var(LocalId::new(0))
65    }
66
67    /// Locals that aren't arguments or return values.
68    pub fn non_argument_locals(&self) -> impl Iterator<Item = (LocalId, &Local)> {
69        self.locals.iter_indexed().skip(1 + self.arg_count)
70    }
71}
72
73impl std::ops::Index<LocalId> for Locals {
74    type Output = Local;
75    fn index(&self, local_id: LocalId) -> &Self::Output {
76        &self.locals[local_id]
77    }
78}
79
80impl TraitDecl {
81    pub fn methods(&self) -> impl Iterator<Item = &(TraitItemName, Binder<FunDeclRef>)> {
82        self.methods.iter()
83    }
84}
85impl TraitImpl {
86    pub fn methods(&self) -> impl Iterator<Item = &(TraitItemName, Binder<FunDeclRef>)> {
87        self.methods.iter()
88    }
89}