Skip to content

Commit

Permalink
Split out the Option and Expected types from Variant (#215)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexcrichton authored May 5, 2022
1 parent d218845 commit 51af112
Show file tree
Hide file tree
Showing 42 changed files with 2,081 additions and 1,157 deletions.
466 changes: 343 additions & 123 deletions crates/gen-c/src/lib.rs

Large diffs are not rendered by default.

34 changes: 32 additions & 2 deletions crates/gen-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ pub trait Generator {
variant: &Variant,
docs: &Docs,
);
fn type_option(
&mut self,
iface: &Interface,
id: TypeId,
name: &str,
payload: &Type,
docs: &Docs,
);
fn type_expected(
&mut self,
iface: &Interface,
id: TypeId,
name: &str,
expected: &Expected,
docs: &Docs,
);
fn type_enum(&mut self, iface: &Interface, id: TypeId, name: &str, enum_: &Enum, docs: &Docs);
fn type_resource(&mut self, iface: &Interface, ty: ResourceId);
fn type_alias(&mut self, iface: &Interface, id: TypeId, name: &str, ty: &Type, docs: &Docs);
Expand Down Expand Up @@ -97,6 +113,8 @@ pub trait Generator {
TypeDefKind::Variant(variant) => {
self.type_variant(iface, id, name, variant, &ty.docs)
}
TypeDefKind::Option(t) => self.type_option(iface, id, name, t, &ty.docs),
TypeDefKind::Expected(e) => self.type_expected(iface, id, name, e, &ty.docs),
TypeDefKind::List(t) => self.type_list(iface, id, name, t, &ty.docs),
TypeDefKind::Type(t) => self.type_alias(iface, id, name, t, &ty.docs),
}
Expand Down Expand Up @@ -215,6 +233,13 @@ impl Types {
TypeDefKind::Type(ty) => {
info = self.type_info(iface, ty);
}
TypeDefKind::Option(ty) => {
info = self.type_info(iface, ty);
}
TypeDefKind::Expected(e) => {
info = self.type_info(iface, &e.ok);
info |= self.type_info(iface, &e.err);
}
}
self.type_info.insert(ty, info);
return info;
Expand Down Expand Up @@ -252,8 +277,13 @@ impl Types {
}
}
}
TypeDefKind::List(ty) => self.set_param_result_ty(iface, ty, param, result),
TypeDefKind::Type(ty) => self.set_param_result_ty(iface, ty, param, result),
TypeDefKind::List(ty) | TypeDefKind::Type(ty) | TypeDefKind::Option(ty) => {
self.set_param_result_ty(iface, ty, param, result)
}
TypeDefKind::Expected(e) => {
self.set_param_result_ty(iface, &e.ok, param, result);
self.set_param_result_ty(iface, &e.err, param, result);
}
}
}

Expand Down
Loading

0 comments on commit 51af112

Please sign in to comment.