charon_lib/ast/
gast_utils.rs1use crate::ast::*;
4
5impl FnPtrKind {
6 pub fn mk_builtin(aid: BuiltinFunId) -> Self {
7 Self::Fun(FunId::Builtin(aid))
8 }
9}
10
11impl Body {
12 pub fn has_contents(&self) -> bool {
15 match self {
16 Body::Unstructured(..) | Body::Structured(..) => true,
17 Body::TraitMethodWithoutDefault | Body::Opaque | Body::Missing | Body::Error(..) => {
18 false
19 }
20 }
21 }
22
23 pub fn locals(&self) -> &Locals {
24 match self {
25 Body::Structured(body) => &body.locals,
26 Body::Unstructured(body) => &body.locals,
27 _ => panic!("called `locals` on a missing body"),
28 }
29 }
30}
31
32impl Locals {
33 pub fn new(arg_count: usize) -> Self {
34 Self {
35 arg_count,
36 locals: Default::default(),
37 }
38 }
39
40 pub fn new_var(&mut self, name: Option<String>, ty: Ty) -> Place {
43 let local_id = self.locals.push_with(|index| Local {
44 index,
45 name,
46 ty: ty.clone(),
47 });
48 Place::new(local_id, ty)
49 }
50
51 pub fn place_for_var(&self, local_id: LocalId) -> Place {
53 let ty = self.locals[local_id].ty.clone();
54 Place::new(local_id, ty)
55 }
56
57 pub fn is_return_or_arg(&self, lid: LocalId) -> bool {
59 lid.index() <= self.arg_count
60 }
61
62 pub fn return_place(&self) -> Place {
64 self.place_for_var(LocalId::new(0))
65 }
66
67 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}
79impl std::ops::IndexMut<LocalId> for Locals {
80 fn index_mut(&mut self, local_id: LocalId) -> &mut Self::Output {
81 &mut self.locals[local_id]
82 }
83}
84
85impl FunDecl {
86 pub fn substitute_params(self, subst: Binder<GenericArgs>) -> Self {
88 let FunDecl {
89 def_id,
90 item_meta,
91 generics: _,
92 signature,
93 src,
94 is_global_initializer,
95 body,
96 } = self;
97 let signature = signature.substitute(&subst.skip_binder);
98 let src = src.substitute(&subst.skip_binder);
99 let body = body.substitute(&subst.skip_binder);
100 FunDecl {
101 def_id,
102 item_meta,
103 generics: subst.params,
104 signature,
105 src,
106 is_global_initializer,
107 body,
108 }
109 }
110}
111impl TraitDecl {
112 pub fn methods(&self) -> impl Iterator<Item = &Binder<TraitMethod>> {
113 self.methods.iter()
114 }
115}
116impl TraitImpl {
117 pub fn methods(&self) -> impl Iterator<Item = &(TraitItemName, Binder<FunDeclRef>)> {
118 self.methods.iter()
119 }
120}
121
122impl Binder<TraitAssocTy> {
123 pub fn name(&self) -> &TraitItemName {
124 &self.skip_binder.name
125 }
126}
127impl Binder<TraitMethod> {
128 pub fn name(&self) -> TraitItemName {
129 self.skip_binder.name
130 }
131}