Skip to content

Commit 2faf5c0

Browse files
committed
Compiler: add builtin properties for min/max etc.
* add support for builtin properties * handle `min`, `max` and `width` for integer types * handle `min`, `max`, `width`, `inf`, `nan` for floating point types * allow builtin names as module names * create c2 library with builtin property modules * add c2 module in c2 library and remove `c2module_loader.c2` - `min_xxx` and `max_xxx` definitions should be removed after boostrap - C types should be removed and replaced with `extern "C" {}` blocks * add `Kind.isVolatile()` and `Kind.isBuiltinType()` * rename `is_keyword()` and `Kind.isKeyword()` * move `va_list` and `va_xxx` macros to varargs module in C2 library * c2 builtin and varargs modules are imported implicitly * update tests
1 parent 0389077 commit 2faf5c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+302
-307
lines changed

analyser/module_analyser.c2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ fn void Analyser.analyseGlobalVarDecl(Analyser* ma, VarDecl* v) {
572572

573573
d.setType(res);
574574

575-
ma.checkName(d, res.isConstant());
575+
if (!d.getAST().isInterface())
576+
ma.checkName(d, res.isConstant());
576577

577578
if (d.isPublic()) setTypePublicUsed(res);
578579

analyser/module_analyser_stmt.c2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ fn QualType Analyser.analyseDecl(Analyser* ma, VarDecl* vd) {
304304
return QualType_Invalid;
305305
}
306306

307-
ma.checkName(d, false);
307+
if (!d.getAST().isInterface())
308+
ma.checkName(d, false);
308309

309310
Expr** initExpr = vd.getInit2();
310311
bool has_init_call = vd.hasInitCall();

ast/ast.c2

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public type AST struct @(opaque) {
3232
void* ptr; // pointer used during analyser/generation (in analysis: Scope*)
3333
u32 name; // into auxPool
3434
u32 idx; // ast idx
35+
bool is_interface;
3536
bool is_generated;
3637

3738
ImportDeclList imports;
@@ -46,12 +47,13 @@ public type AST struct @(opaque) {
4647

4748
static_assert(144+64, sizeof(AST));
4849

49-
fn AST* AST.create(string_pool.Pool* auxPool, u32 name, Module* mod, bool is_generated) {
50+
fn AST* AST.create(string_pool.Pool* auxPool, u32 name, Module* mod, bool is_interface, bool is_generated) {
5051
AST* a = stdlib.calloc(1, sizeof(AST));
5152
a.mod = mod;
5253
a.auxPool = auxPool;
5354
a.name = name;
5455
a.idx = addAST(a);
56+
a.is_interface = is_interface;
5557
a.is_generated = is_generated;
5658
a.imports.init();
5759
a.types.init();
@@ -97,7 +99,7 @@ public fn void AST.setPtr(AST* a, void* ptr) { a.ptr = ptr; }
9799

98100
public fn void* AST.getPtr(const AST* a) { return a.ptr; }
99101

100-
public fn Module* AST.getMod(const AST* a) { return a.mod; }
102+
public fn Module* AST.getMod(const AST* a) @(unused) { return a.mod; }
101103

102104
public fn void AST.addImport(AST* a, ImportDecl* d) {
103105
a.imports.add(d);
@@ -117,6 +119,10 @@ public fn bool AST.isGenerated(const AST* a) @(unused) {
117119
return a.is_generated;
118120
}
119121

122+
public fn bool AST.isInterface(const AST* a) @(unused) {
123+
return a.is_interface;
124+
}
125+
120126
public fn void AST.addFunc(AST* a, FunctionDecl* d) {
121127
a.functions.add(d);
122128
}

ast/integer_literal.c2

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,6 @@ public fn IntegerLiteral* IntegerLiteral.create(ast_context.Context* c, SrcLoc l
4949
return i;
5050
}
5151

52-
public fn IntegerLiteral* IntegerLiteral.createUnsignedConstant(ast_context.Context* c, SrcLoc loc, u64 val, QualType qt) {
53-
IntegerLiteral* i = IntegerLiteral.create(c, loc, 0, val, Radix.Default);
54-
i.base.setCtv();
55-
i.base.setCtc();
56-
i.base.setType(qt);
57-
return i;
58-
}
59-
60-
public fn IntegerLiteral* IntegerLiteral.createSignedConstant(ast_context.Context* c, SrcLoc loc, i64 val, QualType qt) {
61-
IntegerLiteral* i = IntegerLiteral.create(c, loc, 0, cast<u64>(val), Radix.Default);
62-
i.base.base.integerLiteralBits.is_signed = 1;
63-
i.base.setCtv();
64-
i.base.setCtc();
65-
i.base.setType(qt);
66-
return i;
67-
}
68-
6952
public fn u64 IntegerLiteral.getValue(const IntegerLiteral* e) { return e.val; }
7053

7154
fn SrcLoc IntegerLiteral.getEndLoc(const IntegerLiteral* e) {

ast/module.c2

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public fn void Module.free(Module* m) {
7070
public fn void Module.setUsed(Module* m) { m.is_used = true; }
7171
public fn bool Module.isUsed(const Module* m) { return m.is_used; }
7272

73-
public fn void Module.setInternal(Module* m) { m.is_internal = true; }
73+
// TODO use use a keyword or an attribute for this
74+
public fn void Module.setInternal(Module* m) @(unused) { m.is_internal = true; }
7475
public fn bool Module.isInternal(const Module* m) { return m.is_internal; }
7576

7677
public fn bool Module.isExternal(const Module* m) { return m.is_external; }
@@ -185,8 +186,8 @@ fn void Module.resizeFiles(Module* m, u32 cap) {
185186
m.files = buf;
186187
}
187188

188-
public fn ast.AST* Module.add(Module* m, string_pool.Pool* auxPool, u32 filename, bool is_generated) {
189-
ast.AST* a = ast.AST.create(auxPool, filename, m, is_generated);
189+
public fn ast.AST* Module.add(Module* m, string_pool.Pool* auxPool, u32 filename, bool is_interface, bool is_generated) {
190+
ast.AST* a = ast.AST.create(auxPool, filename, m, is_interface, is_generated);
190191

191192
if (m.num_files == m.max_files) m.resizeFiles(m.max_files * 2);
192193

ast/qualtype.c2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ public fn bool QualType.isCharPointer(const QualType* qt) {
222222
if (!qt.isPointer()) return false;
223223
const PointerType* pt = qt.getPointerType();
224224
QualType inner = pt.getInner();
225+
// accept aliases so c_char can be used with @(printf_format)
226+
inner = inner.getCanonicalType();
225227
return inner.isChar();
226228
}
227229

common/component.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public fn ast.Module* Component.getOrAddModule(Component* c, u32 name_idx) {
162162
return c.createModule(name_idx);
163163
}
164164

165-
public fn ast.Module* Component.createModule(Component* c, u32 name_idx) {
165+
public fn ast.Module* Component.createModule(Component* c, u32 name_idx) @(unused) {
166166
ast.Module* m = c.allmodules.find(name_idx);
167167
if (m) return nil;
168168

compiler/c2module_loader.c2

Lines changed: 0 additions & 168 deletions
This file was deleted.

compiler/compiler.c2

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import ast_context;
2121
import attr_handler;
2222
import build_file;
2323
import build_target;
24-
import c2module_loader;
2524
import c2_parser;
2625
import c_generator;
2726
import color;
@@ -284,6 +283,11 @@ fn void Compiler.build(Compiler* c,
284283

285284
c.addGlobalDefine("SYSTEM", c.targetInfo.getSystemName());
286285
c.addGlobalDefine("ARCH", c.targetInfo.getArchName());
286+
if (c.targetInfo.intWidth == 64) {
287+
c.addFeature("ARCH_64BIT", "1");
288+
} else {
289+
c.addFeature("ARCH_32BIT", "1");
290+
}
287291
if (opts.asan) c.addFeature("__ASAN__", "1");
288292
if (opts.msan) c.addFeature("__MSAN__", "1");
289293
if (opts.ubsan) c.addFeature("__UBSAN__", "1");
@@ -309,18 +313,6 @@ fn void Compiler.build(Compiler* c,
309313
// since not all members have been initialized, just exit here, to avoid free() not working
310314
stdlib.exit(0);
311315
}
312-
313-
// create c2 module - special that it has no Component.
314-
u32 c2comp_name = c.auxPool.addStr("(internal)", true);
315-
component.Component* c2comp = component.create(c.context,
316-
c.auxPool,
317-
&c.allmodules,
318-
c2comp_name,
319-
component.Kind.Internal,
320-
true);
321-
c2module_loader.load(c.context, c.astPool, c.auxPool, c2comp);
322-
c.components.add(c2comp);
323-
324316
c.load_libs();
325317

326318
c.mainComp = component.create(c.context,

compiler/main.c2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ fn bool Context.build_target(Context* c,
583583
}
584584
}
585585

586+
target.addLib(c.auxPool.add("c2", 2, true), false);
587+
586588
compiler.build(c.auxPool, c.sm, c.diags, c.build_info, target, &c.comp_opts, &c.pluginHandler);
587589

588590
// TODO unload target-specific plugins?

0 commit comments

Comments
 (0)