Skip to content

Commit f6f55a9

Browse files
committed
Compiler: move c2_assert to c2
* compute the assert string and function call at parse time * add libc2 module in libs/libc/c2_internals.c2i * add `auto_func` function parameter attribute * reject invalid combinations of `auto_xxx` attributes
1 parent 11eb436 commit f6f55a9

22 files changed

+164
-74
lines changed

analyser/module_analyser_function.c2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ fn void Analyser.analyseFunction(Analyser* ma, FunctionDecl* fd) {
9999
if (v.hasAttrAutoLine() && !ref.isU32()) { // check for 'u32'
100100
ma.error(ref.getLoc(), "attribute 'auto_line' requires a parameter of type 'u32'");
101101
}
102+
if (v.hasAttrAutoFunc() && !ref.isConstCharPtr()) { // check for 'const char *'
103+
ma.error(ref.getLoc(), "attribute 'auto_func' requires a parameter of type 'const char*'");
104+
}
102105
}
103106

104107
if (v.hasPrintfFormat()) {

analyser/module_analyser_stmt.c2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ fn void Analyser.analyseAssertStmt(Analyser* ma, Stmt* s) {
374374

375375
Expr* inner = a.getInner();
376376
ma.checker.check(builtins[BuiltinKind.Bool], qt, a.getInner2(), inner.getLoc());
377+
378+
if (a.getCall()) {
379+
qt = ma.analyseExpr(a.getCall2(), true, RHS);
380+
if (qt.isInvalid()) return;
381+
}
377382
}
378383

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

ast/assert_stmt.c2

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,43 @@ import string_buffer;
2222
public type AssertStmt struct @(opaque) {
2323
Stmt base;
2424
Expr* inner;
25+
Expr* call;
2526
}
2627

2728
public fn AssertStmt* AssertStmt.create(ast_context.Context* c,
28-
SrcLoc loc,
29-
Expr* inner)
29+
SrcLoc loc,
30+
Expr* inner,
31+
Expr* call)
3032
{
3133
AssertStmt* s = c.alloc(sizeof(AssertStmt));
3234
s.base.init(StmtKind.Assert, loc);
3335
s.inner = inner;
36+
s.call = call;
3437
#if AstStatistics
3538
Stats.addStmt(StmtKind.Assert, sizeof(AssertStmt));
3639
#endif
3740
return s;
3841
}
3942

4043
fn Stmt* AssertStmt.instantiate(AssertStmt* s, Instantiator* inst) {
41-
AssertStmt* s2 = AssertStmt.create(inst.c, s.base.loc, s.inner.instantiate(inst));
44+
AssertStmt* s2 = AssertStmt.create(inst.c, s.base.loc,
45+
s.inner.instantiate(inst),
46+
s.call ? s.call.instantiate(inst) : nil);
4247
return (Stmt*)s2;
4348
}
4449

4550
public fn Expr* AssertStmt.getInner(const AssertStmt* s) { return s.inner; }
4651

4752
public fn Expr** AssertStmt.getInner2(AssertStmt* s) { return &s.inner; }
4853

54+
public fn Expr* AssertStmt.getCall(const AssertStmt* s) { return s.call; }
55+
56+
public fn Expr** AssertStmt.getCall2(AssertStmt* s) { return &s.call; }
57+
4958
fn void AssertStmt.print(const AssertStmt* s, string_buffer.Buf* out, u32 indent) {
5059
s.base.printKind(out, indent);
5160
out.newline();
5261
s.inner.print(out, indent + 1);
62+
if (s.call) s.call.print(out, indent + 1);
5363
}
5464

ast/qualtype.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public fn u32 QualType.getBitFieldWidth(const QualType* qt) {
9797
return bi.getBitfieldSize();
9898
}
9999
if (t.isEnumType()) {
100+
// TODO return bits needed for max value to allow use in bitfields
100101
EnumType* et = (EnumType*)t;
101102
QualType impl = et.getImplType();
102103
return impl.getBitFieldWidth();

ast/utils.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static_assert(80, sizeof(FunctionDecl));
3939
static_assert(8, sizeof(Stmt));
4040
static_assert(12, sizeof(GotoStmt));
4141
static_assert(24, sizeof(LabelStmt));
42-
static_assert(16, sizeof(AssertStmt));
42+
static_assert(24, sizeof(AssertStmt));
4343
static_assert(8, sizeof(DeclStmt));
4444
static_assert(16, sizeof(SwitchStmt));
4545
static_assert(24, sizeof(AsmStmt));

ast/var_decl.c2

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ const char*[] varDeclNames = {
3535

3636
static_assert(elemsof(VarDeclKind), elemsof(varDeclNames));
3737

38+
type AutoAttr enum u32 {
39+
None,
40+
File,
41+
Line,
42+
Func,
43+
}
44+
3845
type VarDeclBits struct {
3946
u32 : NumDeclBits;
4047
u32 kind : 3;
@@ -43,8 +50,7 @@ type VarDeclBits struct {
4350
u32 has_init_call : 1; // local variables only
4451
u32 attr_weak : 1; // globals only
4552
u32 addr_used : 1;
46-
u32 auto_file : 1; // for parameters only
47-
u32 auto_line : 1; // for parameters only
53+
u32 auto_attr : 2; // AutoAttr, for parameters only
4854
u32 printf_format : 1; // for parameters only
4955
}
5056

@@ -246,23 +252,31 @@ public fn bool VarDecl.hasAttrWeak(const VarDecl* d) {
246252
}
247253

248254
public fn void VarDecl.setAttrAutoFile(VarDecl* d) {
249-
d.base.varDeclBits.auto_file = 1;
255+
d.base.varDeclBits.auto_attr = AutoAttr.File;
250256
}
251257

252258
public fn bool VarDecl.hasAttrAutoFile(const VarDecl* d) {
253-
return d.base.varDeclBits.auto_file;
259+
return d.base.varDeclBits.auto_attr == AutoAttr.File;
254260
}
255261

256262
public fn void VarDecl.setAttrAutoLine(VarDecl* d) {
257-
d.base.varDeclBits.auto_line = 1;
263+
d.base.varDeclBits.auto_attr = AutoAttr.Line;
258264
}
259265

260266
public fn bool VarDecl.hasAttrAutoLine(const VarDecl* d) {
261-
return d.base.varDeclBits.auto_line;
267+
return d.base.varDeclBits.auto_attr == AutoAttr.Line;
268+
}
269+
270+
public fn void VarDecl.setAttrAutoFunc(VarDecl* d) {
271+
d.base.varDeclBits.auto_attr = AutoAttr.Func;
272+
}
273+
274+
public fn bool VarDecl.hasAttrAutoFunc(const VarDecl* d) {
275+
return d.base.varDeclBits.auto_attr == AutoAttr.Func;
262276
}
263277

264278
public fn bool VarDecl.hasAutoAttr(const VarDecl* d) {
265-
return d.base.varDeclBits.auto_file || d.base.varDeclBits.auto_line;
279+
return d.base.varDeclBits.auto_attr != AutoAttr.None;
266280
}
267281

268282
public fn void VarDecl.setPrintfFormat(VarDecl* d) {
@@ -288,8 +302,12 @@ fn void VarDecl.print(const VarDecl* d, string_buffer.Buf* out, u32 indent) {
288302
if (d.hasLocalQualifier()) out.add(" (local)");
289303
if (d.base.varDeclBits.attr_weak) out.add(" weak");
290304
if (d.base.varDeclBits.addr_used) out.add(" addr_used");
291-
if (d.base.varDeclBits.auto_file) out.add(" auto_file");
292-
if (d.base.varDeclBits.auto_line) out.add(" auto_line");
305+
switch ((AutoAttr)d.base.varDeclBits.auto_attr) {
306+
case None: break;
307+
case File: out.add(" auto_file"); break;
308+
case Line: out.add(" auto_line"); break;
309+
case Func: out.add(" auto_func"); break;
310+
}
293311
if (d.base.varDeclBits.printf_format) out.add(" printf_format");
294312
if (d.base.varDeclBits.has_init_call) out.add(" init_call");
295313
d.base.printBits(out);

ast_utils/attr.c2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public type AttrKind enum u8 {
4040
Pure, // Func
4141
AutoFile, // Var, function param only
4242
AutoLine, // Var, function param only
43+
AutoFunc, // Var, function param only
4344
}
4445

4546
const char*[] attrKind_names = {
@@ -63,6 +64,7 @@ const char*[] attrKind_names = {
6364
"pure",
6465
"auto_file",
6566
"auto_line",
67+
"auto_func",
6668
}
6769

6870
static_assert(elemsof(AttrKind), elemsof(attrKind_names));
@@ -140,6 +142,7 @@ const AttrReq[] Required_arg = {
140142
[AttrKind.Pure] = AttrReq.NoArg,
141143
[AttrKind.AutoFile] = AttrReq.NoArg,
142144
[AttrKind.AutoLine] = AttrReq.NoArg,
145+
[AttrKind.AutoFunc] = AttrReq.NoArg,
143146
}
144147
static_assert(elemsof(AttrKind), elemsof(Required_arg));
145148

compiler/compiler.c2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ fn void Compiler.build(Compiler* c,
300300
c.astPool,
301301
c.builder,
302302
&c.kwinfo,
303-
target.getFeatures());
303+
target.getFeatures(),
304+
c.target.hasAsserts());
304305

305306
ast.initialize(c.context, c.astPool, c.targetInfo.intWidth / 8, color.useColor());
306307

generator/ast_visitor.c2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ fn void Visitor.handleStmt(Visitor* v, Stmt* s) {
213213
case Assert:
214214
AssertStmt* a = cast<AssertStmt*>(s);
215215
v.handleExpr(a.getInner());
216+
Expr* call = a.getCall();
217+
if (call) v.handleExpr(call);
216218
break;
217219
}
218220
}

generator/c/c2i_generator_decl.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ fn void Generator.emitVarDecl(Generator* gen, const Decl* d, u32 indent) {
6969
if (v.hasAutoAttr()) {
7070
if (v.hasAttrAutoFile()) out.add(" @(auto_file)");
7171
if (v.hasAttrAutoLine()) out.add(" @(auto_line)");
72+
if (v.hasAttrAutoFunc()) out.add(" @(auto_func)");
7273
}
7374
// Note: dont add ';' because it could also be a function argument
7475
}

0 commit comments

Comments
 (0)