daml_lf/element/
visitor.rs

1#[cfg(feature = "full")]
2use crate::element::daml_expr::DamlScenario;
3#[cfg(feature = "full")]
4use crate::element::{
5    DamlAbs, DamlApp, DamlBinding, DamlBlock, DamlBuiltinFunction, DamlCase, DamlCaseAlt, DamlCaseAltCons,
6    DamlCaseAltEnum, DamlCaseAltOptionalSome, DamlCaseAltSum, DamlCaseAltVariant, DamlCommit, DamlCons, DamlCreate,
7    DamlDefValue, DamlEnumCon, DamlExercise, DamlExerciseByKey, DamlExpr, DamlFetch, DamlFieldWithExpr, DamlFromAny,
8    DamlFromAnyException, DamlLocalValueName, DamlNonLocalValueName, DamlOptionalSome, DamlPrimCon, DamlPrimLit,
9    DamlPure, DamlRecCon, DamlRecProj, DamlRecUpd, DamlRetrieveByKey, DamlScenarioEmbedExpr, DamlStructCon,
10    DamlStructProj, DamlStructUpd, DamlThrow, DamlToAny, DamlToAnyException, DamlTryCatch, DamlTyAbs, DamlTyApp,
11    DamlUpdate, DamlUpdateEmbedExpr, DamlValueName, DamlVarWithType, DamlVariantCon, RoundingMode,
12};
13use crate::element::{
14    DamlAbsoluteTyCon, DamlArchive, DamlArrow, DamlChoice, DamlData, DamlDefKey, DamlDefTypeSyn, DamlEnum, DamlField,
15    DamlForall, DamlKind, DamlLocalTyCon, DamlModule, DamlNonLocalTyCon, DamlPackage, DamlRecord, DamlStruct, DamlSyn,
16    DamlTemplate, DamlTyCon, DamlTyConName, DamlType, DamlTypeVarWithKind, DamlVar, DamlVariant,
17};
18
19/// A Daml [element](`crate::element`) that can be visited by a [`DamlElementVisitor`].
20///
21/// See [`DamlElementVisitor`].
22pub trait DamlVisitableElement<'a> {
23    fn accept(&'a self, visitor: &'a mut impl DamlElementVisitor);
24}
25
26/// A Daml element visitor.
27///
28/// Visit a tree of Daml [element](`crate::element`) types and apply an action.
29///
30/// # Examples
31///
32/// The following example opens a Daml [`DarFile`](`crate::DarFile`) and applies a visitor which records the names of
33/// all [`DamlEnum`] data items present in the tree.
34///
35/// ```no_run
36/// # use std::collections::HashSet;
37/// # use daml_lf::{DarFile, DamlLfResult};
38/// # use daml_lf::element::{DamlElementVisitor, DamlVisitableElement, DamlEnum};
39/// # fn main() -> DamlLfResult<()> {
40/// #[derive(Default)]
41/// pub struct GatherEnumsVisitor(HashSet<String>);
42///
43/// impl DamlElementVisitor for GatherEnumsVisitor {
44///     fn pre_visit_enum<'a>(&mut self, data_enum: &'a DamlEnum<'a>) {
45///         self.0.insert(data_enum.name().to_owned());
46///     }
47/// }
48///
49/// let mut visitor = GatherEnumsVisitor::default();
50/// let dar = DarFile::from_file("SomeDamlModel.dar")?;
51/// dar.apply(|archive| archive.accept(&mut visitor))?;
52/// # Ok(())
53/// # }
54/// ```
55#[allow(unused_variables)]
56pub trait DamlElementVisitor {
57    fn sort_elements(&self) -> bool {
58        false
59    }
60    fn pre_visit_archive<'a>(&mut self, archive: &'a DamlArchive<'a>) {}
61    fn post_visit_archive<'a>(&mut self, archive: &'a DamlArchive<'a>) {}
62    fn pre_visit_package<'a>(&mut self, package: &'a DamlPackage<'a>) {}
63    fn post_visit_package<'a>(&mut self, package: &'a DamlPackage<'a>) {}
64    fn pre_visit_module<'a>(&mut self, module: &'a DamlModule<'a>) {}
65    fn post_visit_module<'a>(&mut self, module: &'a DamlModule<'a>) {}
66    fn pre_visit_def_type_syn<'a>(&mut self, def_type_syn: &'a DamlDefTypeSyn<'a>) {}
67    fn post_visit_def_type_syn<'a>(&mut self, def_type_syn: &'a DamlDefTypeSyn<'a>) {}
68    fn pre_visit_data<'a>(&mut self, data: &'a DamlData<'a>) {}
69    fn post_visit_data<'a>(&mut self, data: &'a DamlData<'a>) {}
70    fn pre_visit_template<'a>(&mut self, template: &'a DamlTemplate<'a>) {}
71    fn post_visit_template<'a>(&mut self, template: &'a DamlTemplate<'a>) {}
72    fn pre_visit_choice<'a>(&mut self, choice: &'a DamlChoice<'a>) {}
73    fn post_visit_choice<'a>(&mut self, choice: &'a DamlChoice<'a>) {}
74    fn pre_visit_record<'a>(&mut self, record: &'a DamlRecord<'a>) {}
75    fn post_visit_record<'a>(&mut self, record: &'a DamlRecord<'a>) {}
76    fn pre_visit_variant<'a>(&mut self, variant: &'a DamlVariant<'a>) {}
77    fn post_visit_variant<'a>(&mut self, variant: &'a DamlVariant<'a>) {}
78    fn pre_visit_enum<'a>(&mut self, data_enum: &'a DamlEnum<'a>) {}
79    fn post_visit_enum<'a>(&mut self, data_enum: &'a DamlEnum<'a>) {}
80    fn pre_visit_field<'a>(&mut self, field: &'a DamlField<'a>) {}
81    fn post_visit_field<'a>(&mut self, field: &'a DamlField<'a>) {}
82    fn pre_visit_type<'a>(&mut self, ty: &'a DamlType<'a>) {}
83    fn post_visit_type<'a>(&mut self, ty: &'a DamlType<'a>) {}
84    fn pre_visit_type_var<'a>(&mut self, type_var: &'a DamlTypeVarWithKind<'a>) {}
85    fn post_visit_type_var<'a>(&mut self, type_var: &'a DamlTypeVarWithKind<'a>) {}
86    fn pre_visit_kind(&mut self, kind: &DamlKind) {}
87    fn post_visit_kind(&mut self, kind: &DamlKind) {}
88    fn pre_visit_arrow(&mut self, arrow: &DamlArrow) {}
89    fn post_visit_arrow(&mut self, arrow: &DamlArrow) {}
90    fn pre_visit_var<'a>(&mut self, var: &'a DamlVar<'a>) {}
91    fn post_visit_var<'a>(&mut self, var: &'a DamlVar<'a>) {}
92    fn pre_visit_forall<'a>(&mut self, forall: &'a DamlForall<'a>) {}
93    fn post_visit_forall<'a>(&mut self, forall: &'a DamlForall<'a>) {}
94    fn pre_visit_struct<'a>(&mut self, tuple: &'a DamlStruct<'a>) {}
95    fn post_visit_struct<'a>(&mut self, tuple: &'a DamlStruct<'a>) {}
96    fn pre_visit_syn<'a>(&mut self, syn: &'a DamlSyn<'a>) {}
97    fn post_visit_syn<'a>(&mut self, syn: &'a DamlSyn<'a>) {}
98    fn pre_visit_tycon<'a>(&mut self, tycon: &'a DamlTyCon<'a>) {}
99    fn post_visit_tycon<'a>(&mut self, tycon: &'a DamlTyCon<'a>) {}
100    fn pre_visit_tycon_name<'a>(&mut self, tycon_name: &'a DamlTyConName<'a>) {}
101    fn post_visit_tycon_name<'a>(&mut self, tycon_name: &'a DamlTyConName<'a>) {}
102    fn pre_visit_local_tycon<'a>(&mut self, local_tycon: &'a DamlLocalTyCon<'a>) {}
103    fn post_visit_local_tycon<'a>(&mut self, local_tycon: &'a DamlLocalTyCon<'a>) {}
104    fn pre_visit_non_local_tycon<'a>(&mut self, non_local_tycon: &'a DamlNonLocalTyCon<'a>) {}
105    fn post_visit_non_local_tycon<'a>(&mut self, non_local_tycon: &'a DamlNonLocalTyCon<'a>) {}
106    fn pre_visit_absolute_tycon<'a>(&mut self, absolute_tycon: &'a DamlAbsoluteTyCon<'a>) {}
107    fn post_visit_absolute_tycon<'a>(&mut self, absolute_tycon: &'a DamlAbsoluteTyCon<'a>) {}
108    fn pre_visit_def_key<'a>(&mut self, def_key: &DamlDefKey<'a>) {}
109    fn post_visit_def_key<'a>(&mut self, def_key: &DamlDefKey<'a>) {}
110
111    #[cfg(feature = "full")]
112    fn pre_visit_def_value<'a>(&mut self, def_value: &'a DamlDefValue<'a>) {}
113    #[cfg(feature = "full")]
114    fn post_visit_def_value<'a>(&mut self, def_value: &'a DamlDefValue<'a>) {}
115    #[cfg(feature = "full")]
116    fn pre_visit_expr<'a>(&mut self, expr: &'a DamlExpr<'a>) {}
117    #[cfg(feature = "full")]
118    fn post_visit_expr<'a>(&mut self, expr: &'a DamlExpr<'a>) {}
119    #[cfg(feature = "full")]
120    fn pre_visit_value_name<'a>(&mut self, value_name: &'a DamlValueName<'a>) {}
121    #[cfg(feature = "full")]
122    fn pre_visit_local_value_name<'a>(&mut self, local_value_name: &'a DamlLocalValueName<'a>) {}
123    #[cfg(feature = "full")]
124    fn post_visit_local_value_name<'a>(&mut self, local_value_name: &'a DamlLocalValueName<'a>) {}
125    #[cfg(feature = "full")]
126    fn pre_visit_non_local_value_name<'a>(&mut self, non_local_value_name: &'a DamlNonLocalValueName<'a>) {}
127    #[cfg(feature = "full")]
128    fn post_visit_non_local_value_name<'a>(&mut self, non_local_value_name: &'a DamlNonLocalValueName<'a>) {}
129    #[cfg(feature = "full")]
130    fn post_visit_value_name<'a>(&mut self, value_name: &'a DamlValueName<'a>) {}
131    #[cfg(feature = "full")]
132    fn pre_visit_builtin_function(&mut self, builtin: &DamlBuiltinFunction) {}
133    #[cfg(feature = "full")]
134    fn post_visit_builtin_function(&mut self, builtin: &DamlBuiltinFunction) {}
135    #[cfg(feature = "full")]
136    fn pre_visit_prim_con(&mut self, prim_con: DamlPrimCon) {}
137    #[cfg(feature = "full")]
138    fn post_visit_prim_con(&mut self, prim_con: DamlPrimCon) {}
139    #[cfg(feature = "full")]
140    fn pre_visit_prim_lit<'a>(&mut self, prim_lit: &DamlPrimLit<'a>) {}
141    #[cfg(feature = "full")]
142    fn post_visit_prim_lit<'a>(&mut self, prim_lit: &DamlPrimLit<'a>) {}
143    #[cfg(feature = "full")]
144    fn pre_visit_rounding_mode(&mut self, rounding_mode: &RoundingMode) {}
145    #[cfg(feature = "full")]
146    fn post_visit_rounding_mode(&mut self, rounding_mode: &RoundingMode) {}
147    #[cfg(feature = "full")]
148    fn pre_visit_rec_con<'a>(&mut self, rec_con: &DamlRecCon<'a>) {}
149    #[cfg(feature = "full")]
150    fn post_visit_rec_con<'a>(&mut self, rec_con: &DamlRecCon<'a>) {}
151    #[cfg(feature = "full")]
152    fn pre_visit_field_with_expr<'a>(&mut self, field_with_expr: &DamlFieldWithExpr<'a>) {}
153    #[cfg(feature = "full")]
154    fn post_visit_field_with_expr<'a>(&mut self, field_with_expr: &DamlFieldWithExpr<'a>) {}
155    #[cfg(feature = "full")]
156    fn pre_visit_rec_proj<'a>(&mut self, rec_proj: &DamlRecProj<'a>) {}
157    #[cfg(feature = "full")]
158    fn post_visit_rec_proj<'a>(&mut self, rec_proj: &DamlRecProj<'a>) {}
159    #[cfg(feature = "full")]
160    fn pre_visit_rec_upd<'a>(&mut self, rec_upd: &DamlRecUpd<'a>) {}
161    #[cfg(feature = "full")]
162    fn post_visit_rec_upd<'a>(&mut self, rec_upd: &DamlRecUpd<'a>) {}
163    #[cfg(feature = "full")]
164    fn pre_visit_variant_con<'a>(&mut self, variant_con: &DamlVariantCon<'a>) {}
165    #[cfg(feature = "full")]
166    fn post_visit_variant_con<'a>(&mut self, variant_con: &DamlVariantCon<'a>) {}
167    #[cfg(feature = "full")]
168    fn pre_visit_enum_con<'a>(&mut self, enum_con: &DamlEnumCon<'a>) {}
169    #[cfg(feature = "full")]
170    fn post_visit_enum_con<'a>(&mut self, enum_con: &DamlEnumCon<'a>) {}
171    #[cfg(feature = "full")]
172    fn pre_visit_struct_con<'a>(&mut self, struct_con: &DamlStructCon<'a>) {}
173    #[cfg(feature = "full")]
174    fn post_visit_struct_con<'a>(&mut self, struct_con: &DamlStructCon<'a>) {}
175    #[cfg(feature = "full")]
176    fn pre_visit_struct_proj<'a>(&mut self, struct_proj: &DamlStructProj<'a>) {}
177    #[cfg(feature = "full")]
178    fn post_visit_struct_proj<'a>(&mut self, struct_proj: &DamlStructProj<'a>) {}
179    #[cfg(feature = "full")]
180    fn pre_visit_struct_upd<'a>(&mut self, struct_upd: &DamlStructUpd<'a>) {}
181    #[cfg(feature = "full")]
182    fn post_visit_struct_upd<'a>(&mut self, struct_upd: &DamlStructUpd<'a>) {}
183    #[cfg(feature = "full")]
184    fn pre_visit_app<'a>(&mut self, app: &DamlApp<'a>) {}
185    #[cfg(feature = "full")]
186    fn post_visit_app<'a>(&mut self, app: &DamlApp<'a>) {}
187    #[cfg(feature = "full")]
188    fn pre_visit_ty_app<'a>(&mut self, ty_app: &DamlTyApp<'a>) {}
189    #[cfg(feature = "full")]
190    fn post_visit_ty_app<'a>(&mut self, ty_app: &DamlTyApp<'a>) {}
191    #[cfg(feature = "full")]
192    fn pre_visit_abs<'a>(&mut self, abs: &DamlAbs<'a>) {}
193    #[cfg(feature = "full")]
194    fn post_visit_abs<'a>(&mut self, abs: &DamlAbs<'a>) {}
195    #[cfg(feature = "full")]
196    fn pre_visit_var_with_type<'a>(&mut self, var_with_type: &DamlVarWithType<'a>) {}
197    #[cfg(feature = "full")]
198    fn post_visit_var_with_type<'a>(&mut self, var_with_type: &DamlVarWithType<'a>) {}
199    #[cfg(feature = "full")]
200    fn pre_visit_ty_abs<'a>(&mut self, ty_abs: &DamlTyAbs<'a>) {}
201    #[cfg(feature = "full")]
202    fn post_visit_ty_abs<'a>(&mut self, ty_abs: &DamlTyAbs<'a>) {}
203    #[cfg(feature = "full")]
204    fn pre_visit_case<'a>(&mut self, case: &DamlCase<'a>) {}
205    #[cfg(feature = "full")]
206    fn post_visit_case<'a>(&mut self, case: &DamlCase<'a>) {}
207    #[cfg(feature = "full")]
208    fn pre_visit_block<'a>(&mut self, block: &DamlBlock<'a>) {}
209    #[cfg(feature = "full")]
210    fn post_visit_block<'a>(&mut self, block: &DamlBlock<'a>) {}
211    #[cfg(feature = "full")]
212    fn pre_visit_binding<'a>(&mut self, binding: &DamlBinding<'a>) {}
213    #[cfg(feature = "full")]
214    fn post_visit_binding<'a>(&mut self, binding: &DamlBinding<'a>) {}
215    #[cfg(feature = "full")]
216    fn pre_visit_cons<'a>(&mut self, cons: &DamlCons<'a>) {}
217    #[cfg(feature = "full")]
218    fn post_visit_cons<'a>(&mut self, cons: &DamlCons<'a>) {}
219    #[cfg(feature = "full")]
220    fn pre_visit_optional_some<'a>(&mut self, optional_some: &DamlOptionalSome<'a>) {}
221    #[cfg(feature = "full")]
222    fn post_visit_optional_some<'a>(&mut self, optional_some: &DamlOptionalSome<'a>) {}
223    #[cfg(feature = "full")]
224    fn pre_visit_to_any<'a>(&mut self, to_any: &DamlToAny<'a>) {}
225    #[cfg(feature = "full")]
226    fn post_visit_to_any<'a>(&mut self, to_any: &DamlToAny<'a>) {}
227    #[cfg(feature = "full")]
228    fn pre_visit_from_any<'a>(&mut self, from_any: &DamlFromAny<'a>) {}
229    #[cfg(feature = "full")]
230    fn post_visit_from_any<'a>(&mut self, from_any: &DamlFromAny<'a>) {}
231    #[cfg(feature = "full")]
232    fn pre_visit_update<'a>(&mut self, update: &DamlUpdate<'a>) {}
233    #[cfg(feature = "full")]
234    fn post_visit_update<'a>(&mut self, update: &DamlUpdate<'a>) {}
235    #[cfg(feature = "full")]
236    fn pre_visit_pure<'a>(&mut self, pure: &DamlPure<'a>) {}
237    #[cfg(feature = "full")]
238    fn post_visit_pure<'a>(&mut self, pure: &DamlPure<'a>) {}
239    #[cfg(feature = "full")]
240    fn pre_visit_create<'a>(&mut self, create: &DamlCreate<'a>) {}
241    #[cfg(feature = "full")]
242    fn post_visit_create<'a>(&mut self, create: &DamlCreate<'a>) {}
243    #[cfg(feature = "full")]
244    fn pre_visit_exercise<'a>(&mut self, exercise: &DamlExercise<'a>) {}
245    #[cfg(feature = "full")]
246    fn post_visit_exercise<'a>(&mut self, exercise: &DamlExercise<'a>) {}
247    #[cfg(feature = "full")]
248    fn pre_visit_exercise_by_key<'a>(&mut self, exercise_by_key: &DamlExerciseByKey<'a>) {}
249    #[cfg(feature = "full")]
250    fn post_visit_exercise_by_key<'a>(&mut self, exercise_by_key: &DamlExerciseByKey<'a>) {}
251    #[cfg(feature = "full")]
252    fn pre_visit_fetch<'a>(&mut self, fetch: &DamlFetch<'a>) {}
253    #[cfg(feature = "full")]
254    fn post_visit_fetch<'a>(&mut self, fetch: &DamlFetch<'a>) {}
255    #[cfg(feature = "full")]
256    fn pre_visit_try_catch<'a>(&mut self, try_catch: &DamlTryCatch<'a>) {}
257    #[cfg(feature = "full")]
258    fn post_visit_try_catch<'a>(&mut self, try_catch: &DamlTryCatch<'a>) {}
259    #[cfg(feature = "full")]
260    fn pre_visit_retrieve_by_key<'a>(&mut self, retrieve_by_key: &DamlRetrieveByKey<'a>) {}
261    #[cfg(feature = "full")]
262    fn post_visit_retrieve_by_key<'a>(&mut self, retrieve_by_key: &DamlRetrieveByKey<'a>) {}
263    #[cfg(feature = "full")]
264    fn pre_visit_update_embed_expr<'a>(&mut self, update_embed_expr: &DamlUpdateEmbedExpr<'a>) {}
265    #[cfg(feature = "full")]
266    fn post_visit_update_embed_expr<'a>(&mut self, update_embed_expr: &DamlUpdateEmbedExpr<'a>) {}
267    #[cfg(feature = "full")]
268    fn pre_visit_scenario<'a>(&mut self, scenario: &DamlScenario<'a>) {}
269    #[cfg(feature = "full")]
270    fn post_visit_scenario<'a>(&mut self, scenario: &DamlScenario<'a>) {}
271    #[cfg(feature = "full")]
272    fn pre_visit_commit<'a>(&mut self, commit: &DamlCommit<'a>) {}
273    #[cfg(feature = "full")]
274    fn post_visit_commit<'a>(&mut self, commit: &DamlCommit<'a>) {}
275    #[cfg(feature = "full")]
276    fn pre_visit_scenario_embed_expr<'a>(&mut self, scenario_embed_expr: &DamlScenarioEmbedExpr<'a>) {}
277    #[cfg(feature = "full")]
278    fn post_visit_scenario_embed_expr<'a>(&mut self, scenario_embed_expr: &DamlScenarioEmbedExpr<'a>) {}
279    #[cfg(feature = "full")]
280    fn pre_visit_case_alt<'a>(&mut self, case_alt: &DamlCaseAlt<'a>) {}
281    #[cfg(feature = "full")]
282    fn post_visit_case_alt<'a>(&mut self, case_alt: &DamlCaseAlt<'a>) {}
283    #[cfg(feature = "full")]
284    fn pre_visit_case_alt_sum<'a>(&mut self, case_alt_sum: &DamlCaseAltSum<'a>) {}
285    #[cfg(feature = "full")]
286    fn post_visit_case_alt_sum<'a>(&mut self, case_alt_sum: &DamlCaseAltSum<'a>) {}
287    #[cfg(feature = "full")]
288    fn pre_visit_case_alt_variant<'a>(&mut self, case_alt_variant: &DamlCaseAltVariant<'a>) {}
289    #[cfg(feature = "full")]
290    fn post_visit_case_alt_variant<'a>(&mut self, case_alt_variant: &DamlCaseAltVariant<'a>) {}
291    #[cfg(feature = "full")]
292    fn pre_visit_case_alt_cons<'a>(&mut self, case_alt_cons: &DamlCaseAltCons<'a>) {}
293    #[cfg(feature = "full")]
294    fn post_visit_case_alt_cons<'a>(&mut self, case_alt_cons: &DamlCaseAltCons<'a>) {}
295    #[cfg(feature = "full")]
296    fn pre_visit_case_alt_opt_some<'a>(&mut self, case_alt_opt_some: &DamlCaseAltOptionalSome<'a>) {}
297    #[cfg(feature = "full")]
298    fn post_visit_case_alt_opt_some<'a>(&mut self, case_alt_opt_some: &DamlCaseAltOptionalSome<'a>) {}
299    #[cfg(feature = "full")]
300    fn pre_visit_case_alt_enum<'a>(&mut self, case_alt_enum: &DamlCaseAltEnum<'a>) {}
301    #[cfg(feature = "full")]
302    fn post_visit_case_alt_enum<'a>(&mut self, case_alt_enum: &DamlCaseAltEnum<'a>) {}
303
304    #[cfg(feature = "full")]
305    fn pre_visit_to_any_exception<'a>(&mut self, to_any_exception: &DamlToAnyException<'a>) {}
306    #[cfg(feature = "full")]
307    fn post_visit_to_any_exception<'a>(&mut self, to_any_exception: &DamlToAnyException<'a>) {}
308    #[cfg(feature = "full")]
309    fn pre_visit_from_any_exception<'a>(&mut self, from_any_exception: &DamlFromAnyException<'a>) {}
310    #[cfg(feature = "full")]
311    fn post_visit_from_any_exception<'a>(&mut self, from_any_exception: &DamlFromAnyException<'a>) {}
312    #[cfg(feature = "full")]
313    fn pre_visit_throw<'a>(&mut self, throw: &DamlThrow<'a>) {}
314    #[cfg(feature = "full")]
315    fn post_visit_throw<'a>(&mut self, throw: &DamlThrow<'a>) {}
316}