Skip to content

Commit 59ad896

Browse files
committed
Compiler: remve builtins global variable
* move the allocated array `builtins` to the `ast.globals` structure * add `getBuiltinQT()` to retrieve `QualType` values for builtin types * remove `ast_builtins` in `plugin_into.Info` structure * remove `BuiltinType.isInt8()`, `BuiltinType.isChar()`, `BuiltinType.isUInt8()`, `QualType.isInt8()` * add `QualType.isCharCompatible()`
1 parent 63c5833 commit 59ad896

28 files changed

+105
-103
lines changed

analyser/conversion_checker.c2

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ fn bool Checker.checkPointer2Builtin(Checker* c, const Type* lcanon, const Type*
299299

300300
BuiltinKind kind = bi.getKind();
301301
if (kind == BuiltinKind.Bool) {
302-
c.builder.insertImplicitCast(PointerToBoolean, c.expr_ptr, builtins[BuiltinKind.Bool]);
302+
c.builder.insertImplicitCast(PointerToBoolean, c.expr_ptr, getBuiltinQT(Bool));
303303
return true;
304304
}
305305

@@ -318,7 +318,7 @@ fn bool Checker.checkPointer2Builtin(Checker* c, const Type* lcanon, const Type*
318318
}
319319
}
320320

321-
c.builder.insertImplicitCast(PointerToInteger, c.expr_ptr, builtins[BuiltinKind.USize]);
321+
c.builder.insertImplicitCast(PointerToInteger, c.expr_ptr, getBuiltinQT(USize));
322322
return true;
323323
}
324324

@@ -647,7 +647,7 @@ const u8[elemsof(BuiltinKind)][elemsof(BuiltinKind)] ConditionalOperatorResult =
647647
}
648648
public fn QualType get_common_arithmetic_type(BuiltinType* bi1, BuiltinType* bi2) {
649649
BuiltinKind kind = (BuiltinKind)ConditionalOperatorResult[bi2.getBaseKind()][bi1.getBaseKind()];
650-
return ast.builtins[kind];
650+
return getBuiltinQT(kind);
651651
}
652652

653653
/*
@@ -703,17 +703,17 @@ public fn QualType usual_arithmetic_conversion(const BuiltinType* b1, const Buil
703703

704704
switch(UsualArithmeticConversions[k2][k1]) {
705705
case 0:
706-
return ast.builtins[BuiltinKind.Int32];
706+
return getBuiltinQT(Int32);
707707
case 1:
708-
return ast.builtins[BuiltinKind.UInt32];
708+
return getBuiltinQT(UInt32);
709709
case 2:
710-
return ast.builtins[BuiltinKind.Int64];
710+
return getBuiltinQT(Int64);
711711
case 3:
712-
return ast.builtins[BuiltinKind.UInt64];
712+
return getBuiltinQT(UInt64);
713713
case 4:
714-
return ast.builtins[BuiltinKind.Float32];
714+
return getBuiltinQT(Float32);
715715
case 5:
716-
return ast.builtins[BuiltinKind.Float64];
716+
return getBuiltinQT(Float64);
717717
case 6:
718718
break;
719719
}

analyser/ctv_analyser.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public fn bool checkTypeRange(diagnostics.Diags* diags, QualType qt, Value* valu
7575

7676
BuiltinType* bi = canon.getBuiltin();
7777
// TODO: check float32 range
78-
if (bi.getKind() == BuiltinKind.Float32 || bi.getKind() == BuiltinKind.Float64) return true;
78+
if (bi.isFloatingPoint()) return true;
7979

8080
Limit limit.init(bi.getWidth(), bi.isSigned());
8181

analyser/module_analyser_binop.c2

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ fn QualType Analyser.checkBinopShiftArgs(Analyser* ma, BinaryOperator* b, QualTy
227227

228228
// perform integer promotion on both arguments
229229
if (bl.isPromotableIntegerType()) {
230-
lcanon = ast.builtins[BuiltinKind.Int32];
230+
lcanon = getBuiltinQT(Int32);
231231
width = 32;
232232
if (!assign) ma.builder.insertImplicitCast(IntegralCast, b.getLHS2(), lcanon);
233233
}
234234
if (br.isPromotableIntegerType()) {
235-
rcanon = ast.builtins[BuiltinKind.Int32];
235+
rcanon = getBuiltinQT(Int32);
236236
ma.builder.insertImplicitCast(IntegralCast, b.getRHS2(), rcanon);
237237
}
238238
Expr* lhs = b.getLHS();
@@ -269,7 +269,7 @@ fn QualType Analyser.checkBinopLogical(Analyser* ma, BinaryOperator* b, QualType
269269
}
270270
if (!validTestType(rhs)) return ma.invalidBinOp1(b, b.getRHS(), rhs);
271271

272-
return builtins[BuiltinKind.Bool];
272+
return getBuiltinQT(Bool);
273273
}
274274

275275
// 0 cannot happen
@@ -514,7 +514,7 @@ fn QualType Analyser.checkBinopSubArgs(Analyser* ma, BinaryOperator* b, QualType
514514
QualType t1 = pt1.getInner();
515515
QualType t2 = pt2.getInner();
516516
if (t1.getTypeOrNil() != t2.getTypeOrNil() || t1.isVoid() || t2.isVoid()) goto invalid;
517-
return builtins[BuiltinKind.ISize];
517+
return getBuiltinQT(ISize);
518518
}
519519
return ma.invalidBinOp2(b, lhs, rhs);
520520
}
@@ -582,7 +582,7 @@ fn QualType Analyser.checkBinopComparison(Analyser* ma, BinaryOperator* b, QualT
582582
// TODO should detect comparison between i32/u32 and i32/u32
583583
// TODO i32 < u32 is not the same as i32 = u32! dont use checker
584584
//if (ma.checker.check(lhs, rhs, &e, e.getLoc())) return QualType_Invalid;
585-
return builtins[BuiltinKind.Bool];
585+
return getBuiltinQT(Bool);
586586
case 3: // builtin : enum
587587
rcanon = rcanon.getImplType();
588588
goto builtin;
@@ -598,7 +598,7 @@ fn QualType Analyser.checkBinopComparison(Analyser* ma, BinaryOperator* b, QualT
598598
ma.error(e.getLoc(), "comparing pointers to different types ('%s' and '%s')", lhs.diagName(), rhs.diagName());
599599
return QualType_Invalid;
600600
}
601-
return builtins[BuiltinKind.Bool];
601+
return getBuiltinQT(Bool);
602602
case 5: // enum : builtin
603603
lcanon = lcanon.getImplType();
604604
goto builtin;
@@ -607,7 +607,7 @@ fn QualType Analyser.checkBinopComparison(Analyser* ma, BinaryOperator* b, QualT
607607
ma.error(e.getLoc(), "comparing enums of different types ('%s' and '%s')", lhs.diagName(), rhs.diagName());
608608
return QualType_Invalid;
609609
}
610-
return builtins[BuiltinKind.Bool];
610+
return getBuiltinQT(Bool);
611611
case 7: // pointer : function
612612
if (is_relative) return ma.invalidFuncCompare(b);
613613
// TODO accept 0 == f, (void*)0 == f and (void*)nil == f
@@ -620,7 +620,7 @@ fn QualType Analyser.checkBinopComparison(Analyser* ma, BinaryOperator* b, QualT
620620
return ma.checkFuncTest(lcanon.getFunctionType(), e.getLoc());
621621
case 9: // function : function
622622
if (is_relative) return ma.invalidFuncCompare(b);
623-
return builtins[BuiltinKind.Bool];
623+
return getBuiltinQT(Bool);
624624
case 10: // invalid lhs
625625
invalid_lhs:
626626
if (rcanon.isVoid()) goto invalid;
@@ -638,7 +638,7 @@ fn QualType Analyser.checkBinopComparison(Analyser* ma, BinaryOperator* b, QualT
638638
fn QualType Analyser.checkFuncTest(Analyser* ma, FunctionType* ft, SrcLoc loc) {
639639
// only allow nil vs weak-function or function pointer
640640
FunctionDecl* fd = ft.getDecl();
641-
if (fd.isType() || fd.hasAttrWeak()) return builtins[BuiltinKind.Bool]; // Variable with Function type (eg callback)
641+
if (fd.isType() || fd.hasAttrWeak()) return getBuiltinQT(Bool); // Variable with Function type (eg callback)
642642
ma.error(loc, "comparison of function '%s' will always be true", fd.asDecl().getFullName());
643643
return QualType_Invalid;
644644
}
@@ -686,7 +686,7 @@ fn QualType Analyser.analyseBinaryOperator(Analyser* ma, Expr** e_ptr) {
686686
if (opcode.isComparison()) {
687687
// special case for enum comparison without prefix (eg kind >= KW_void)
688688
if (ma.checkEnumArg(b.getRHS2(), ltype))
689-
return builtins[BuiltinKind.Bool];
689+
return getBuiltinQT(Bool);
690690
}
691691
QualType rtype;
692692
if (opcode == BinaryOpcode.Assign) {

analyser/module_analyser_builtin.c2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn QualType Analyser.analyseSizeof(Analyser* ma, BuiltinExpr* e) {
8787

8888
size_analyser.TypeSize info = size_analyser.sizeOfType(qt);
8989
e.setUValue(info.size);
90-
return builtins[BuiltinKind.UInt32];
90+
return getBuiltinQT(UInt32);
9191
}
9292

9393
fn QualType Analyser.analyseElemsof(Analyser* ma, BuiltinExpr* b) {
@@ -107,13 +107,13 @@ fn QualType Analyser.analyseElemsof(Analyser* ma, BuiltinExpr* b) {
107107
return QualType_Invalid;
108108
}
109109
b.setUValue(at.getSize());
110-
return builtins[BuiltinKind.UInt32];
110+
return getBuiltinQT(UInt32);
111111
}
112112
const EnumType* et = qt.getEnumTypeOrNil();
113113
if (et) {
114114
const EnumTypeDecl* etd = et.getDecl();
115115
b.setUValue(etd.getNumConstants());
116-
return builtins[BuiltinKind.UInt32];
116+
return getBuiltinQT(UInt32);
117117
}
118118
ma.error(inner.getLoc(), "elemsof can only be used on arrays/enums");
119119
return QualType_Invalid;
@@ -149,7 +149,7 @@ fn QualType Analyser.analyseOffsetOf(Analyser* ma, BuiltinExpr* b) {
149149
QualType qt = ma.analyseExpr(&inner, false, RHS);
150150
if (qt.isInvalid()) return QualType_Invalid;
151151

152-
e.setType(builtins[BuiltinKind.UInt32]);
152+
e.setType(getBuiltinQT(UInt32));
153153

154154
StructType* st = qt.getStructTypeOrNil();
155155
if (!st) {

analyser/module_analyser_call.c2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ fn bool on_format_specifier(void* context, printf_utils.Specifier specifier, u32
308308
}
309309
break;
310310
case Char:
311-
// TODO check can be more efficient
312-
if (!qt.isChar() && !qt.isInt8() && !qt.isUInt8()) {
311+
if (!qt.isCharCompatible()) {
313312
ma.error(arg.getStartLoc(), "format '%%c' expects a character argument");
314313
}
315314
break;

analyser/module_analyser_expr.c2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,10 @@ fn QualType usualUnaryConversions(Expr* e) {
405405

406406
if (canon.isBuiltin()) {
407407
BuiltinType* bi = canon.getBuiltin();
408-
if (bi.isPromotableIntegerType()) return ast.builtins[BuiltinKind.Int32];
408+
if (bi.isPromotableIntegerType()) return getBuiltinQT(Int32);
409409
} else if (canon.isPointer()) {
410410
// TODO depend on arch width
411-
return ast.builtins[BuiltinKind.UInt64];
411+
return getBuiltinQT(UInt64);
412412
}
413413

414414
return qt;
@@ -522,13 +522,13 @@ fn QualType Analyser.analyseBitOffsetExpr(Analyser* ma, QualType ltype, Expr* ba
522522
Value width = lval.minus(&rval);
523523
u64 w = width.as_u64() + 1;
524524
if (w <= 8) {
525-
ltype = ast.builtins[BuiltinKind.UInt8];
525+
ltype = getBuiltinQT(UInt8);
526526
} else if (w <= 16) {
527-
ltype = ast.builtins[BuiltinKind.UInt16];
527+
ltype = getBuiltinQT(UInt16);
528528
} else if (w <= 32) {
529-
ltype = builtins[BuiltinKind.UInt32];
529+
ltype = getBuiltinQT(UInt32);
530530
} else {
531-
ltype = ast.builtins[BuiltinKind.UInt64];
531+
ltype = getBuiltinQT(UInt64);
532532
}
533533

534534
bo.setWidth((u8)w);

analyser/module_analyser_init.c2

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ fn bool Analyser.analyseInitExpr(Analyser* ma, Expr** e_ptr, QualType expectedTy
3737
if (at) {
3838
// check that element type is char/i8/u8
3939
QualType elem = at.getElemType();
40-
if (elem.getTypeOrNil() != builtins[BuiltinKind.Char].getTypeOrNil()
41-
&& elem.getTypeOrNil() != builtins[BuiltinKind.Int8].getTypeOrNil()
42-
&& elem.getTypeOrNil() != builtins[BuiltinKind.UInt8].getTypeOrNil()) {
40+
if (!elem.isCharCompatible()) {
4341
ma.errorRange(assignLoc, e.getRange(), "cannot initialize array of '%s' with a string literal", elem.diagName());
4442
return false;
4543
}

analyser/module_analyser_stmt.c2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn QualType Analyser.analyseCondition(Analyser* ma, Stmt** s_ptr, bool check_ass
215215
assert(s.isExpr());
216216
QualType qt = ma.analyseExpr((Expr**)s_ptr, true, RHS);
217217
Expr* e = (Expr*)*s_ptr;
218-
if (qt.isValid()) ma.checker.check(builtins[BuiltinKind.Bool], qt, (Expr**)s_ptr, e.getLoc());
218+
if (qt.isValid()) ma.checker.check(getBuiltinQT(Bool), qt, (Expr**)s_ptr, e.getLoc());
219219
e = (Expr*)*s_ptr; // re-read in case of ImplicitCast insertions
220220

221221
if (check_assign && e.isAssignment()) {
@@ -266,7 +266,7 @@ fn FlowBits Analyser.analyseForStmt(Analyser* ma, Stmt* s) {
266266
if (cond) {
267267
QualType qt = ma.analyseExpr(cond, true, RHS);
268268
if (qt.isInvalid()) goto done;
269-
ma.checker.check(builtins[BuiltinKind.Bool], qt, cond, (*cond).getLoc());
269+
ma.checker.check(getBuiltinQT(Bool), qt, cond, (*cond).getLoc());
270270
if ((*cond).isCtv()) {
271271
Value v = ast.evalExpr((*cond));
272272
if (v.isZero()) {
@@ -435,7 +435,7 @@ fn void Analyser.analyseAssertStmt(Analyser* ma, Stmt* s) {
435435
if (qt.isInvalid()) return;
436436

437437
Expr* inner = a.getInner();
438-
ma.checker.check(builtins[BuiltinKind.Bool], qt, a.getInner2(), inner.getLoc());
438+
ma.checker.check(getBuiltinQT(Bool), qt, a.getInner2(), inner.getLoc());
439439
}
440440

441441
fn void Analyser.analyseReturnStmt(Analyser* ma, Stmt* s) {

analyser/module_analyser_type.c2

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,20 @@ fn QualType Analyser.analyseTypeRef(Analyser* ma, TypeRef* ref) {
272272
fn QualType Analyser.analyseIncrTypeRef(Analyser* ma, TypeRef* ref, u32 size) {
273273
// TODO refactor common code with analyseTypeRef
274274
QualType base;
275-
if (ref.isUser()) {
276-
base = ma.analyseUserTypeRef(ref);
277-
if (base.isInvalid()) return base;
278-
assert(base.hasCanonicalType());
279-
} else {
275+
switch (ref.getKind()) {
276+
case Builtin:
280277
BuiltinKind kind = ref.getBuiltinKind();
281278
base = ma.builder.actOnBuiltinType(kind);
282279
assert(base.isValid());
280+
break;
281+
case User:
282+
base = ma.analyseUserTypeRef(ref);
283+
if (base.isInvalid()) return base;
284+
assert(base.hasCanonicalType());
285+
break;
286+
default:
287+
assert(0);
288+
break;
283289
}
284290

285291
if (ref.isConst()) base.setConst();
@@ -288,7 +294,7 @@ fn QualType Analyser.analyseIncrTypeRef(Analyser* ma, TypeRef* ref, u32 size) {
288294
QualType resolved = base;
289295
u32 num_ptrs = ref.getNumPointers();
290296
for (u32 i=0; i<num_ptrs; i++) {
291-
resolved = ma.builder.actOnPointerType(resolved) ;
297+
resolved = ma.builder.actOnPointerType(resolved);
292298
}
293299

294300
if (resolved.isVoid()) {

analyser/module_analyser_unaryop.c2

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn QualType Analyser.analyseUnaryOperator(Analyser* ma, Expr** e_ptr, u32 side)
145145
break;
146146
case LNot:
147147
e.copyConstantFlags(inner);
148-
return builtins[BuiltinKind.Bool];
148+
return getBuiltinQT(Bool);
149149
}
150150
return t;
151151

@@ -275,22 +275,22 @@ fn QualType getMinusType(QualType qt) {
275275
case Int8:
276276
case Int16:
277277
case Bool:
278-
return ast.builtins[BuiltinKind.Int32];
278+
return getBuiltinQT(Int32);
279279
case Int32:
280280
case Int64:
281281
return qt;
282282
case UInt8:
283283
case UInt16:
284284
case UInt32:
285-
return ast.builtins[BuiltinKind.Int32];
285+
return getBuiltinQT(Int32);
286286
case UInt64:
287-
return ast.builtins[BuiltinKind.Int64];
287+
return getBuiltinQT(Int64);
288288
case Float32:
289289
case Float64:
290290
case ISize:
291291
return qt;
292292
case USize:
293-
return builtins[BuiltinKind.ISize];
293+
return getBuiltinQT(ISize);
294294
case Void:
295295
break;
296296
}

0 commit comments

Comments
 (0)