Skip to content

Commit 2a31629

Browse files
chqrliebvdberg
authored andcommitted
AST: remove redundant enum prefixes
* declare more arrays with enum size * remove most enum prefixes
1 parent 29cd4c3 commit 2a31629

16 files changed

+68
-88
lines changed

analyser/module_analyser_member.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn QualType Analyser.analyseMemberExpr(Analyser* ma, Expr** e_ptr, u32 side) {
118118
// no need to update valtype
119119
break;
120120
case Enum:
121-
//stdio.printf("ENUM BASE (type: %d)\n", valtype == ValType.NValue);
121+
//stdio.printf("ENUM BASE (type: %d)\n", valtype == NValue);
122122
EnumType* et = (EnumType*)t;
123123
EnumTypeDecl* etd = et.getDecl();
124124
d = (Decl*)etd;

ast/binary_operator.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public type BinaryOperator struct @(opaque) {
105105

106106
public fn BinaryOperator* BinaryOperator.create(ast_context.Context* c, SrcLoc loc, BinaryOpcode kind, Expr* lhs, Expr* rhs) {
107107
BinaryOperator* e = c.alloc(sizeof(BinaryOperator));
108-
e.base.init(BinaryOperator, loc, 0, 0, kind >= BinaryOpcode.Assign, RValue);
108+
e.base.init(BinaryOperator, loc, 0, 0, kind >= Assign, RValue);
109109
e.base.base.binaryOperatorBits.kind = kind;
110110
e.lhs = lhs;
111111
e.rhs = rhs;

ast/conditional_operator.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public fn ConditionalOperator* ConditionalOperator.create(ast_context.Context* c
3535
Expr* rhs)
3636
{
3737
ConditionalOperator* e = c.alloc(sizeof(ConditionalOperator));
38-
e.base.init(ConditionalOperator, questionLoc, 0, 1, 1, ValType.RValue);
38+
e.base.init(ConditionalOperator, questionLoc, 0, 1, 1, RValue);
3939
e.colonLoc = colonLoc;
4040
e.cond = cond;
4141
e.lhs = lhs;

ast/expr.c2

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public type ExprKind enum u8 {
4646
Alternate,
4747
}
4848

49-
const char*[] exprKind_names = {
49+
const char*[ExprKind] exprKind_names = {
5050
"IntegerLiteral",
5151
"FloatLiteral",
5252
"BooleanLiteral",
@@ -73,7 +73,6 @@ const char*[] exprKind_names = {
7373
"NamedArgument",
7474
"Alternate",
7575
}
76-
static_assert(elemsof(ExprKind), elemsof(exprKind_names));
7776

7877
/*
7978
An LValue may be on the left/right side of an assignment
@@ -102,12 +101,11 @@ public type ValType enum u8 {
102101
LValue, // can be used as LHS / RHS of assignment
103102
}
104103

105-
const char*[] valType_names = {
104+
const char*[ValType] valType_names = {
106105
"nvalue",
107106
"rvalue",
108107
"lvalue",
109108
}
110-
static_assert(elemsof(ValType), elemsof(valType_names));
111109

112110
type ExprBits struct {
113111
u32 : NumStmtBits;
@@ -315,17 +313,17 @@ public fn ValType Expr.getValType(const Expr* e) {
315313
}
316314

317315
public fn bool Expr.isNValue(const Expr* e) {
318-
return e.getValType() == ValType.NValue;
316+
return e.getValType() == NValue;
319317
}
320318

321319
/*
322320
public fn bool Expr.isRValue(const Expr* e) {
323-
return e.getValType() == ValType.RValue;
321+
return e.getValType() == RValue;
324322
}
325323
*/
326324

327325
public fn bool Expr.isLValue(const Expr* e) {
328-
return e.getValType() == ValType.LValue;
326+
return e.getValType() == LValue;
329327
}
330328

331329
public fn void Expr.setLValue(Expr* e) {

ast/function_decl.c2

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ public type CallKind enum u8 {
2929
StaticTypeFunc,
3030
}
3131

32-
const char*[] callKind_names = {
32+
const char*[CallKind] callKind_names = {
3333
"Invalid",
3434
"Normal",
3535
"SF",
3636
"SSF",
3737
}
38-
static_assert(elemsof(CallKind), elemsof(callKind_names));
3938

4039
public type DefKind enum u8 {
4140
Global, // fn void (..) { .. }
@@ -45,7 +44,7 @@ public type DefKind enum u8 {
4544
Template, // ..
4645
}
4746

48-
const char*[] defKind_names = {
47+
const char*[DefKind] defKind_names = {
4948
"global",
5049
"type",
5150
"struct-member",

ast/identifier_expr.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public type IdentifierExpr struct @(opaque) {
6161

6262
public fn IdentifierExpr* IdentifierExpr.create(ast_context.Context* c, SrcLoc loc, u32 name, u32 name_len) {
6363
IdentifierExpr* e = c.alloc(sizeof(IdentifierExpr));
64-
e.base.init(Identifier, loc, 0, 0, 0, ValType.NValue);
64+
e.base.init(Identifier, loc, 0, 0, 0, NValue);
6565
e.name_idx = name;
6666
e.base.base.identifierExprBits.name_len = name_len;
6767
#if AstStatistics

ast/implicit_cast_expr.c2

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public type ImplicitCastKind enum u8 {
2828
BitCast, // pointer conversion (char* -> i32*, or void* -> FunctionPtr)
2929
}
3030

31-
const char*[] implicitCastKind_names = {
31+
const char*[ImplicitCastKind] implicitCastKind_names = {
3232
"ArrayToPointerDecay",
3333
"LValueToRValue",
3434
"PointerToBoolean",
@@ -37,8 +37,6 @@ const char*[] implicitCastKind_names = {
3737
"BitCast",
3838
}
3939

40-
static_assert(elemsof(ImplicitCastKind), elemsof(implicitCastKind_names));
41-
4240
type ImplicitCastBits struct {
4341
u32 : NumExprBits;
4442
u32 kind : 3;

ast/statistics.c2

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,32 @@ fn void Stats.dump(const Stats* s) {
121121
printf("--- Types ---\n");
122122
u32 typesTotal = 0;
123123
u32 typesCount = 0;
124-
//for (u32 i = TypeKind.min; i <= TypeKind.max; i++) {
125-
for (u32 i=enum_min(TypeKind); i<=enum_max(TypeKind); i++) {
126-
s.types[i].dump(typeKind_names[i], &typesCount, &typesTotal);
124+
for (TypeKind kind = TypeKind.min; kind <= TypeKind.max; kind++) {
125+
s.types[kind].dump(typeKind_names[kind], &typesCount, &typesTotal);
127126
}
128127
printf(" %20s %6d %7d\n", "total", typesCount, typesTotal);
129128

130129
printf("--- Expressions ---\n");
131130
u32 exprTotal = 0;
132131
u32 exprCount = 0;
133-
//for (u32 i = ExprKind.min; i <= ExprKind.max; i++) {
134-
for (u32 i=enum_min(ExprKind); i<=enum_max(ExprKind); i++) {
132+
for (ExprKind i = ExprKind.min; i <= ExprKind.max; i++) {
135133
s.exprs[i].dump(exprKind_names[i], &exprCount, &exprTotal);
136134
}
137135
printf(" %20s %6d %7d\n", "total", exprCount, exprTotal);
138136

139137
printf("--- Statements ---\n");
140138
u32 stmtTotal = 0;
141139
u32 stmtCount = 0;
142-
//for (u32 i = StmtKind.min; i <= StmtKind.max; i++) {
143-
for (u32 i=enum_min(StmtKind); i<=enum_max(StmtKind); i++) {
140+
for (StmtKind i = StmtKind.min; i <= StmtKind.max; i++) {
144141
s.stmts[i].dump(stmtKind_names[i], &stmtCount, &stmtTotal);
145142
}
146143
printf(" %20s %6d %7d\n", "total", stmtCount, stmtTotal);
147144

148145
printf("--- Decls ---\n");
149146
u32 declTotal = 0;
150147
u32 declCount = 0;
151-
//for (u32 i = DeclKind.min; i <= DeclKind.max; i++) {
152-
for (u32 i=enum_min(DeclKind); i<=enum_max(DeclKind); i++) {
153-
s.decls[i].dump(declKind_names[(DeclKind)i], &declCount, &declTotal);
148+
for (DeclKind i = DeclKind.min; i <= DeclKind.max; i++) {
149+
s.decls[i].dump(declKind_names[i], &declCount, &declTotal);
154150
}
155151
printf(" %20s %6d %7d\n", "total", declCount, declTotal);
156152

ast/stmt.c2

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public type StmtKind enum u8 {
3636
Assert,
3737
}
3838

39-
const char*[] stmtKind_names = {
39+
const char*[StmtKind] stmtKind_names = {
4040
"ReturnStmt",
4141
"ExprStmt",
4242
"IfStmt",
@@ -54,8 +54,6 @@ const char*[] stmtKind_names = {
5454
"AssertStmt",
5555
}
5656

57-
static_assert(elemsof(StmtKind), elemsof(stmtKind_names));
58-
5957
type StmtBits struct {
6058
u32 kind : NumStmtBits;
6159
}

ast/type.c2

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public type TypeKind enum u8 {
2929
Module,
3030
}
3131

32-
const char*[] typeKind_names = {
32+
const char*[TypeKind] typeKind_names = {
3333
"Builtin",
3434
"Pointer",
3535
"Array",
@@ -41,8 +41,6 @@ const char*[] typeKind_names = {
4141
"Module",
4242
}
4343

44-
static_assert(elemsof(TypeKind), elemsof(typeKind_names));
45-
4644
type TypeBits struct {
4745
TypeKind kind : 8;
4846
}

0 commit comments

Comments
 (0)