Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum fixes #684

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/aro/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,10 @@ pub fn eql(a_param: Type, b_param: Type, comp: *const Compilation, check_qualifi
if (!b.isFunc()) return false;
} else if (a.isArray()) {
if (!b.isArray()) return false;
} else if (a.specifier == .@"enum" and a.data.@"enum".fixed and b.specifier != .@"enum") {
return a.data.@"enum".tag_ty.eql(b, comp, check_qualifiers);
} else if (b.specifier == .@"enum" and b.data.@"enum".fixed and a.specifier != .@"enum") {
return a.eql(b.data.@"enum".tag_ty, comp, check_qualifiers);
} else if (a.specifier != b.specifier) return false;

if (a.qual.atomic != b.qual.atomic) return false;
Expand Down Expand Up @@ -1314,13 +1318,18 @@ pub fn integerRank(ty: Type, comp: *const Compilation) usize {
.typeof_expr => ty.data.expr.ty.integerRank(comp),
.attributed => ty.data.attributed.base.integerRank(comp),

.@"enum" => {
std.debug.assert(real.data.@"enum".fixed);
return real.data.@"enum".tag_ty.integerRank(comp);
},
else => unreachable,
});
}

/// Returns true if `a` and `b` are integer types that differ only in sign
pub fn sameRankDifferentSign(a: Type, b: Type, comp: *const Compilation) bool {
if (!a.isInt() or !b.isInt()) return false;
if (a.hasIncompleteSize() or b.hasIncompleteSize()) return false;
if (a.integerRank(comp) != b.integerRank(comp)) return false;
return a.isUnsignedInt(comp) != b.isUnsignedInt(comp);
}
Expand Down
42 changes: 42 additions & 0 deletions test/cases/enum fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,43 @@ enum E e;
enum E: int;
void fn(enum E);

enum Signed: int {
S,
};

enum Signed2: int {
S2,
};

enum Unsigned: unsigned {
U,
};

enum Plain {
P,
};

_Static_assert(!__builtin_types_compatible_p(enum Signed, enum Signed2), "");
_Static_assert(!__builtin_types_compatible_p(enum Signed, enum Unsigned), "");
_Static_assert(__builtin_types_compatible_p(enum Signed, int), "");
_Static_assert(__builtin_types_compatible_p(enum Unsigned, unsigned), "");

_Static_assert(!__builtin_types_compatible_p(enum Signed, enum Plain), "");
_Static_assert(!__builtin_types_compatible_p(enum Unsigned, enum Plain), "");

void pointers(void) {
int x;
unsigned y;

enum Signed *e1 = &x;
enum Signed *e2 = &y;
enum Unsigned *e3 = &x;
enum Unsigned *e4 = &y;

enum Incomplete *i1 = &x;
enum Incomplete *i2 = &y;
}

#define EXPECTED_ERRORS "enum fixed.c:2:7: warning: enumeration types with a fixed underlying type are a Clang extension [-Wfixed-enum-extension]" \
"enum fixed.c:4:6: error: enumeration previously declared with fixed underlying type" \
"enum fixed.c:2:6: note: previous definition is here" \
Expand All @@ -43,3 +80,8 @@ void fn(enum E);
"enum fixed.c:9:6: note: previous definition is here" \
"enum fixed.c:14:5: error: enumerator value is not representable in the underlying type 'unsigned char'" \
"enum fixed.c:18:5: error: enumerator value is not representable in the underlying type 'char'" \
"enum fixed.c:66:23: warning: incompatible pointer types initializing 'enum Signed: int *' from incompatible type 'unsigned int *' converts between pointers to integer types with different sign [-Wpointer-sign]" \
"enum fixed.c:67:25: warning: incompatible pointer types initializing 'enum Unsigned: unsigned int *' from incompatible type 'int *' [-Wincompatible-pointer-types]" \
"enum fixed.c:70:27: warning: incompatible pointer types initializing 'enum Incomplete *' from incompatible type 'int *' [-Wincompatible-pointer-types]" \
"enum fixed.c:71:27: warning: incompatible pointer types initializing 'enum Incomplete *' from incompatible type 'unsigned int *' [-Wincompatible-pointer-types]" \

Loading