Skip to content

Commit 5459b97

Browse files
committed
Started working on support for anonymous enums
1 parent cac3c0e commit 5459b97

File tree

10 files changed

+78
-21
lines changed

10 files changed

+78
-21
lines changed

Diff for: src/asg/datatype/kind/anonymous_enum.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
use crate::{ast::EnumMember, asg::Type, source_files::Source};
1+
use crate::{asg::Type, ast::EnumMember, source_files::Source};
2+
use core::hash::Hash;
23
use indexmap::IndexMap;
34

45
#[derive(Clone, Debug)]
56
pub struct AnonymousEnum {
6-
pub ty: Box<Type>,
7-
pub source: Source,
7+
pub backing_type: Type,
88
pub members: IndexMap<String, EnumMember>,
9+
pub source: Source,
910
}
1011

1112
impl PartialEq for AnonymousEnum {
1213
fn eq(&self, other: &Self) -> bool {
13-
self.ty.eq(&other.ty) && self.members.eq(&other.members)
14+
self.backing_type.eq(&other.backing_type) && self.members.eq(&other.members)
15+
}
16+
}
17+
18+
impl Hash for AnonymousEnum {
19+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
20+
self.backing_type.hash(state);
21+
22+
for (key, value) in self.members.iter() {
23+
key.hash(state);
24+
value.hash(state);
25+
}
1426
}
1527
}

Diff for: src/asg/datatype/kind/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
source_files::Source,
1111
target::Target,
1212
};
13+
pub use anonymous_enum::AnonymousEnum;
1314
pub use constraint::Constraint;
1415
use core::hash::Hash;
1516
use derive_more::{IsVariant, Unwrap};
@@ -33,7 +34,7 @@ pub enum TypeKind {
3334
Never,
3435
AnonymousStruct(),
3536
AnonymousUnion(),
36-
AnonymousEnum(),
37+
AnonymousEnum(Box<AnonymousEnum>),
3738
FixedArray(Box<FixedArray>),
3839
FuncPtr(FuncPtr),
3940
Enum(HumanName, EnumRef),
@@ -63,7 +64,7 @@ impl TypeKind {
6364
TypeKind::Void | TypeKind::Never => false,
6465
TypeKind::AnonymousStruct() => false,
6566
TypeKind::AnonymousUnion() => false,
66-
TypeKind::AnonymousEnum() => false,
67+
TypeKind::AnonymousEnum(_) => false,
6768
TypeKind::FixedArray(fixed_array) => fixed_array.inner.kind.contains_polymorph(),
6869
TypeKind::FuncPtr(_) => todo!(),
6970
TypeKind::Enum(_, _) => false,
@@ -89,6 +90,7 @@ impl TypeKind {
8990
}
9091
TypeKind::TypeAlias(_, _type_ref) => todo!(),
9192
TypeKind::Unresolved => panic!(),
93+
TypeKind::AnonymousEnum(enumeration) => enumeration.backing_type.kind.sign(target),
9294
TypeKind::Floating(_)
9395
| TypeKind::FloatLiteral(_)
9496
| TypeKind::Ptr(_)
@@ -100,7 +102,6 @@ impl TypeKind {
100102
| TypeKind::FixedArray(..)
101103
| TypeKind::FuncPtr(..)
102104
| TypeKind::Enum(_, _)
103-
| TypeKind::AnonymousEnum()
104105
| TypeKind::Polymorph(_, _)
105106
| TypeKind::Trait(_, _, _) => None,
106107
}
@@ -141,7 +142,7 @@ impl TypeKind {
141142
| TypeKind::Never
142143
| TypeKind::AnonymousStruct()
143144
| TypeKind::AnonymousUnion()
144-
| TypeKind::AnonymousEnum() => Ok(Cow::Borrowed(self)),
145+
| TypeKind::AnonymousEnum(_) => Ok(Cow::Borrowed(self)),
145146
TypeKind::FixedArray(fixed_array) => {
146147
let TypeParam::Size(size) = mapper(TypeParam::Size(fixed_array.size))
147148
.map_err(TypeParamError::MappingError)?

Diff for: src/asg/datatype/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ impl Type {
4141
TypeKind::Ptr(inner) => inner.strip_constraints(),
4242
TypeKind::Void => (),
4343
TypeKind::Never => (),
44-
TypeKind::AnonymousStruct() => todo!(),
45-
TypeKind::AnonymousUnion() => todo!(),
46-
TypeKind::AnonymousEnum() => todo!(),
44+
TypeKind::AnonymousStruct() => todo!("strip_constraints for anonymous struct"),
45+
TypeKind::AnonymousUnion() => todo!("strip_constraints for anonymous union"),
46+
TypeKind::AnonymousEnum(_) => (),
4747
TypeKind::FixedArray(fixed_array) => fixed_array.inner.strip_constraints(),
48-
TypeKind::FuncPtr(_) => todo!(),
48+
TypeKind::FuncPtr(_) => todo!("strip_constraints for function pointer"),
4949
TypeKind::Enum(_, _) => (),
5050
TypeKind::Structure(_, _, parameters) => {
5151
for parameter in parameters {

Diff for: src/ast/enumeration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Enum {
1212
pub privacy: Privacy,
1313
}
1414

15-
#[derive(Clone, Debug, PartialEq)]
15+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1616
pub struct EnumMember {
1717
pub value: BigInt,
1818
pub explicit_value: bool,

Diff for: src/lower/datatype.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn lower_type(
9494
asg::TypeKind::AnonymousUnion() => {
9595
todo!("lower anonymous union")
9696
}
97-
asg::TypeKind::AnonymousEnum() => {
97+
asg::TypeKind::AnonymousEnum(_) => {
9898
todo!("lower anonymous enum")
9999
}
100100
asg::TypeKind::FixedArray(fixed_array) => {

Diff for: src/resolve/collect_constraints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn collect_constraints_into(map: &mut HashMap<String, HashSet<Constraint>>,
2929
asg::TypeKind::Never => (),
3030
asg::TypeKind::AnonymousStruct() => todo!(),
3131
asg::TypeKind::AnonymousUnion() => todo!(),
32-
asg::TypeKind::AnonymousEnum() => todo!(),
32+
asg::TypeKind::AnonymousEnum(_) => (),
3333
asg::TypeKind::FixedArray(fixed_array) => collect_constraints_into(map, &fixed_array.inner),
3434
asg::TypeKind::FuncPtr(_) => todo!(),
3535
asg::TypeKind::Enum(_, _) => (),

Diff for: src/resolve/impl_head/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn matches(
201201
},
202202
asg::TypeKind::AnonymousStruct()
203203
| asg::TypeKind::AnonymousUnion()
204-
| asg::TypeKind::AnonymousEnum() => (ty_in_impl == ty_in_trait)
204+
| asg::TypeKind::AnonymousEnum(_) => (ty_in_impl == ty_in_trait)
205205
.then_some(Ok(()))
206206
.unwrap_or_else(|| Err(mismatch(ty_in_impl.source))),
207207
asg::TypeKind::FixedArray(trait_fixed_array) => match &ty_in_impl.kind {

Diff for: src/resolve/polymorph/matcher.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,34 @@ impl<'local, 'ast, 'root_ctx> TypeMatcher<'local, 'ast, 'root_ctx> {
124124
},
125125
TypeKind::AnonymousStruct() => todo!(),
126126
TypeKind::AnonymousUnion() => todo!(),
127-
TypeKind::AnonymousEnum() => todo!(),
127+
TypeKind::AnonymousEnum(pattern_inner) => match &concrete.kind {
128+
TypeKind::AnonymousEnum(concrete_inner) => {
129+
// NOTE: Can never be polymorphic, so ok
130+
if pattern_inner.backing_type != concrete_inner.backing_type
131+
|| pattern_inner.members.len() != concrete_inner.members.len()
132+
{
133+
return no_match();
134+
}
135+
136+
for ((pattern_name, pattern_value), (concrete_name, concrete_value)) in
137+
pattern_inner
138+
.members
139+
.iter()
140+
.zip(concrete_inner.members.iter())
141+
{
142+
// NOTE: We are only checking the actual values match,
143+
// should we care if the explicitness between them is different too?
144+
if pattern_name != concrete_name
145+
|| pattern_value.value == concrete_value.value
146+
{
147+
return no_match();
148+
}
149+
}
150+
151+
Ok(())
152+
}
153+
_ => no_match(),
154+
},
128155
TypeKind::FixedArray(pattern_inner) => match &concrete.kind {
129156
TypeKind::FixedArray(concrete_inner) => {
130157
self.match_type(&pattern_inner.inner, &concrete_inner.inner)

Diff for: src/resolve/polymorph/resolver.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ impl<'a> PolyRecipeResolver<'a> {
5858
}
5959
asg::TypeKind::AnonymousStruct() => todo!(),
6060
asg::TypeKind::AnonymousUnion() => todo!(),
61-
asg::TypeKind::AnonymousEnum() => todo!(),
61+
asg::TypeKind::AnonymousEnum(_) => {
62+
// NOTE: There are no inner polymorphs
63+
ty.clone()
64+
}
6265
asg::TypeKind::FixedArray(fixed_array) => {
6366
asg::TypeKind::FixedArray(Box::new(asg::FixedArray {
6467
size: fixed_array.size,

Diff for: src/resolve/type_ctx/resolve_type.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{resolve_constraints, ResolveTypeCtx};
22
use crate::{
3-
asg::{self, Params},
3+
asg::{self, AnonymousEnum, Params},
44
ast::{self, IntegerBits},
55
ir::IntegerSign,
66
resolve::error::{ResolveError, ResolveErrorKind},
@@ -69,8 +69,22 @@ impl<'a> ResolveTypeCtx<'a> {
6969
ast::TypeKind::Floating(size) => Ok(asg::TypeKind::Floating(*size)),
7070
ast::TypeKind::AnonymousStruct(..) => todo!("resolve anonymous struct type"),
7171
ast::TypeKind::AnonymousUnion(..) => todo!("resolve anonymous union type"),
72-
ast::TypeKind::AnonymousEnum(_) => {
73-
todo!("resolve anonymous enum type")
72+
ast::TypeKind::AnonymousEnum(enumeration) => {
73+
let backing_type = enumeration
74+
.backing_type
75+
.as_ref()
76+
.map(|ty| self.resolve(ty.as_ref()))
77+
.transpose()?
78+
.unwrap_or_else(|| {
79+
asg::TypeKind::Integer(IntegerBits::Bits32, IntegerSign::Signed)
80+
.at(ast_type.source)
81+
});
82+
83+
Ok(asg::TypeKind::AnonymousEnum(Box::new(AnonymousEnum {
84+
members: enumeration.members.clone(),
85+
backing_type,
86+
source: ast_type.source,
87+
})))
7488
}
7589
ast::TypeKind::FixedArray(fixed_array) => {
7690
if let ast::ExprKind::Integer(integer) = &fixed_array.count.kind {

0 commit comments

Comments
 (0)