Skip to content

Commit 4c4818c

Browse files
alexcrichtonwillemneal
authored andcommitted
Split out the Option and Expected types from Variant (bytecodealliance#215)
* Split out the `Option` and `Expected` types from `Variant` This commit is like prior PRs to split out specializations of types into their own AST type to avoid conflicting with the main type (in this case `variant`). I originally thought these two types would be relatively simple but this is probably one of the more complicated transitions, as evidenced by the lines changed here. The main churn was that variants already have a significant amount of code to support them and this is in some places "duplicating" code for option/expected and in other cases splitting what was already an if/else. Overall I think that the generated code gets a little better since it's clear when something is and `option` vs `expected` now rather than trying to have everything shoehorned into one. Notably the C code generator now generates descriptive fields like `bool is_some` or `bool is_err` instead of a bland `uint8_t tag` with some comments about how to use it. * Remove `Variant::as_{option,expected}` ... as these are separate variants now. * Review comments
1 parent 74cbb3b commit 4c4818c

File tree

42 files changed

+2081
-1157
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2081
-1157
lines changed

crates/gen-c/src/lib.rs

Lines changed: 343 additions & 123 deletions
Large diffs are not rendered by default.

crates/gen-core/src/lib.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ pub trait Generator {
6565
variant: &Variant,
6666
docs: &Docs,
6767
);
68+
fn type_option(
69+
&mut self,
70+
iface: &Interface,
71+
id: TypeId,
72+
name: &str,
73+
payload: &Type,
74+
docs: &Docs,
75+
);
76+
fn type_expected(
77+
&mut self,
78+
iface: &Interface,
79+
id: TypeId,
80+
name: &str,
81+
expected: &Expected,
82+
docs: &Docs,
83+
);
6884
fn type_enum(&mut self, iface: &Interface, id: TypeId, name: &str, enum_: &Enum, docs: &Docs);
6985
fn type_resource(&mut self, iface: &Interface, ty: ResourceId);
7086
fn type_alias(&mut self, iface: &Interface, id: TypeId, name: &str, ty: &Type, docs: &Docs);
@@ -97,6 +113,8 @@ pub trait Generator {
97113
TypeDefKind::Variant(variant) => {
98114
self.type_variant(iface, id, name, variant, &ty.docs)
99115
}
116+
TypeDefKind::Option(t) => self.type_option(iface, id, name, t, &ty.docs),
117+
TypeDefKind::Expected(e) => self.type_expected(iface, id, name, e, &ty.docs),
100118
TypeDefKind::List(t) => self.type_list(iface, id, name, t, &ty.docs),
101119
TypeDefKind::Type(t) => self.type_alias(iface, id, name, t, &ty.docs),
102120
}
@@ -215,6 +233,13 @@ impl Types {
215233
TypeDefKind::Type(ty) => {
216234
info = self.type_info(iface, ty);
217235
}
236+
TypeDefKind::Option(ty) => {
237+
info = self.type_info(iface, ty);
238+
}
239+
TypeDefKind::Expected(e) => {
240+
info = self.type_info(iface, &e.ok);
241+
info |= self.type_info(iface, &e.err);
242+
}
218243
}
219244
self.type_info.insert(ty, info);
220245
return info;
@@ -252,8 +277,13 @@ impl Types {
252277
}
253278
}
254279
}
255-
TypeDefKind::List(ty) => self.set_param_result_ty(iface, ty, param, result),
256-
TypeDefKind::Type(ty) => self.set_param_result_ty(iface, ty, param, result),
280+
TypeDefKind::List(ty) | TypeDefKind::Type(ty) | TypeDefKind::Option(ty) => {
281+
self.set_param_result_ty(iface, ty, param, result)
282+
}
283+
TypeDefKind::Expected(e) => {
284+
self.set_param_result_ty(iface, &e.ok, param, result);
285+
self.set_param_result_ty(iface, &e.err, param, result);
286+
}
257287
}
258288
}
259289

0 commit comments

Comments
 (0)