Skip to content

Commit 7d5de80

Browse files
committed
source_libs: implement source libs
* see documentation on how to use * added test_source_lib to libs for testing * fix manifest.yaml generation for multiple deps, c2 dep * cgen: only use linkname for static/dynamic libs * extract generation phase from compiler.c2 to own file * extract analysis phase from compiler.c2 to own file
1 parent ecd38ee commit 7d5de80

Some content is hidden

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

44 files changed

+1270
-607
lines changed

analyser/module_analyser.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ fn void Analyser.handleImport(void* arg, ast.ImportDecl* id) {
273273
if (!d.isUsedPublic()) return;
274274

275275
Module* dest = id.getDest();
276-
if (dest.isExternal() || dest.isInternal()) return;
276+
if (dest.isExternal()) return;
277277
if (!dest.isExported()) {
278278
ma.error(d.getLoc(), "exported module '%s' publicly uses non-exported module '%s'", ma.mod.getName(), dest.getName());
279279
}

analyser/module_analyser_member.c2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn QualType Analyser.analyseMemberExpr(Analyser* ma, Expr** e_ptr, u32 side) {
9999
if (is_ptr && ck != CallKind.Invalid) is_const = false;
100100
m.setConstBase(is_const);
101101
if (is_const && ck == CallKind.Invalid) baseType.setConst();
102-
// TODO BBB set ck same was as Enum below
102+
// TODO set ck same was as Enum below
103103

104104
// no need to update valtype
105105
break;
@@ -203,7 +203,6 @@ fn QualType Analyser.analyseMemberExpr(Analyser* ma, Expr** e_ptr, u32 side) {
203203

204204
if (ck == CallKind.Invalid && d.isFunction()) ck = CallKind.Normal;
205205
// NOTE: set SF/SSF based on member: Foo.next will be SSF, f.next SF
206-
// TODO BBB
207206
if (ck == CallKind.TypeFunc) m.setIsTypeFunc();
208207
if (ck == CallKind.StaticTypeFunc) m.setIsStaticTypeFunc();
209208
// Note: dont call setValType here, done in setExprFlags

ast/ast.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ public fn void AST.info(const AST* a, string_buffer.Buf* out) {
276276
*/
277277

278278
fn void AST.print(const AST* a, string_buffer.Buf* out, bool show_funcs) {
279+
out.color(col_Normal);
279280
out.print("---- AST %s ----\n", a.getFilename());
280281

281282
ImportDecl** imports = a.imports.getDecls();

ast/module.c2

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ import string;
2525

2626
public type Module struct @(opaque) {
2727
u32 name_idx;
28-
bool is_used;
29-
bool is_external;
30-
bool is_internal; // currently only for c2 module
31-
bool is_direct; // otherwise indirectly loaded via library.
32-
bool is_foreign; // module identifiers are not mangled
33-
bool is_exported;
34-
bool is_loaded; // the file(s) parsed. For external libs
28+
u32 is_used : 1;
29+
u32 is_external : 1; // if not in the main component
30+
u32 is_foreign : 1; // module identifiers are not mangled
31+
u32 is_exported : 1; // if exported in library interface
32+
u32 is_loaded : 1; // the file(s) parsed. For external dynamic/static libs
33+
u32 is_private : 1; // for source-libs
3534
ModuleType* mt;
3635

3736
ast.AST** files;
@@ -41,17 +40,15 @@ public type Module struct @(opaque) {
4140
SymbolTable symbols;
4241
InstanceTable instances;
4342
}
44-
static_assert(88, sizeof(Module));
43+
static_assert(80, sizeof(Module));
4544

4645
// Note: name must be allocated in target StringPool
47-
public fn Module* Module.create(ast_context.Context* c, u32 name_idx, bool is_external, bool is_direct) {
46+
public fn Module* Module.create(ast_context.Context* c, u32 name_idx, bool is_external) {
4847
Module* m = stdlib.calloc(1, sizeof(Module));
4948
m.mt = ModuleType.create(c, m);
5049
m.name_idx = name_idx;
5150
m.is_external = is_external;
5251
m.is_foreign = is_external; // default for external modules
53-
m.is_internal = false;
54-
m.is_direct = is_direct;
5552
m.is_loaded = false;
5653
m.resizeFiles(1);
5754
m.symbols.init(16);
@@ -72,10 +69,6 @@ public fn void Module.free(Module* m) {
7269
public fn void Module.setUsed(Module* m) { m.is_used = true; }
7370
public fn bool Module.isUsed(const Module* m) { return m.is_used; }
7471

75-
// TODO use use a keyword or an attribute for this
76-
public fn void Module.setInternal(Module* m) @(unused) { m.is_internal = true; }
77-
public fn bool Module.isInternal(const Module* m) { return m.is_internal; }
78-
7972
public fn bool Module.isExternal(const Module* m) { return m.is_external; }
8073

8174
public fn void Module.setForeign(Module* m, bool is_foreign) { m.is_foreign = is_foreign; }
@@ -90,10 +83,10 @@ public fn void Module.setExported(Module* m) {
9083
m.files[i].setExported();
9184
}
9285
}
93-
9486
public fn bool Module.isExported(const Module* m) { return m.is_exported; }
9587

96-
public fn bool Module.isDirect(const Module* m) { return m.is_direct; }
88+
public fn void Module.setPrivate(Module* m, bool is_private) { m.is_private = is_private; }
89+
public fn bool Module.isPrivate(const Module* m) { return m.is_private; }
9790

9891
public fn const SymbolTable* Module.getSymbols(const Module* m) { return &m.symbols; }
9992

@@ -229,32 +222,9 @@ public fn ast.FunctionDecl* Module.getInstance(const Module* m, ast.FunctionDecl
229222
return m.instances.get(fd, idx);
230223
}
231224

232-
/*
233-
public type TemplateVisitor fn void (void* arg, FunctionDecl* fd, u32 idx);
234-
235-
public fn void Module.visitInstances(const Module* m, ast.FunctionDecl* fd, TemplateVisitor visitor, void* arg) {
236-
m.instances.visit(fd, visitor, arg);
237-
}
238-
*/
239-
240-
/*
241-
public fn void Module.info(const Module* m, string_buffer.Buf* out) {
242-
out.print(" module %s\n", idx2name(m.name_idx));
243-
for (u32 i=0; i<m.num_files; i++) {
244-
m.files[i].info(out);
245-
}
246-
#if 0
247-
out.print(" Symbol: (%u)\n", m.num_symbols);
248-
for (u32 i=0; i<m.num_symbols; i++) {
249-
out.print(" %s\n", idx2name(m.symbols[i]));
250-
}
251-
#endif
252-
}
253-
*/
254-
255225
public fn void Module.print(const Module* m, string_buffer.Buf* out, bool show_funcs) {
256226
out.color(color.Normal);
257-
out.print("------ module %s (used %d, exported %d) ------\n", idx2name(m.name_idx), m.is_used, m.is_exported);
227+
out.print("------ module %s (used %d, exported %d, private %d) ------\n", idx2name(m.name_idx), m.is_used, m.is_exported, m.is_private);
258228
for (u32 i=0; i<m.num_files; i++) {
259229
m.files[i].print(out, show_funcs);
260230
}

common/build_target.c2

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
module build_target;
1717

18-
import library_list;
18+
import file_list local;
1919
import src_loc local;
2020
import string_list;
2121
import string_pool;
@@ -35,6 +35,7 @@ public type Kind enum u8 {
3535
Executable,
3636
StaticLibrary,
3737
DynamicLibrary,
38+
SourceLibrary,
3839
}
3940

4041
public fn bool has_dynamic(Kind k) {
@@ -47,6 +48,8 @@ public fn bool has_dynamic(Kind k) {
4748
return false;
4849
case DynamicLibrary:
4950
return true;
51+
case SourceLibrary:
52+
return false;
5053
}
5154
return false;
5255
}
@@ -94,11 +97,6 @@ public fn void PluginList.add(PluginList* l, u32 name, u32 options, SrcLoc loc)
9497
l.count++;
9598
}
9699

97-
public type File struct {
98-
u32 name; // index into auxPool
99-
SrcLoc loc;
100-
}
101-
102100
public type Target struct @(opaque) {
103101
u32 name_idx; // index into auxPool
104102
SrcLoc loc;
@@ -112,17 +110,12 @@ public type Target struct @(opaque) {
112110
bool backend_fast;
113111

114112
string_list.List features;
115-
library_list.List libs;
113+
List libs;
116114
string_list.List exports;
117115
PluginList plugins;
118116

119-
File* files;
120-
u32 num_files;
121-
u32 max_files;
122-
123-
File* asm_files;
124-
u32 asm_file_count;
125-
u32 asm_file_max;
117+
FileList files;
118+
FileList asm_files;
126119
}
127120

128121
public fn Target* create(u32 name_idx, SrcLoc loc, Kind kind, string_pool.Pool* pool) {
@@ -132,13 +125,11 @@ public fn Target* create(u32 name_idx, SrcLoc loc, Kind kind, string_pool.Pool*
132125
// remove this once the bootstrap handles improved flow analysis
133126
t.warnings.no_unreachable_code = true;
134127
t.kind = kind;
135-
t.max_files = 8;
136128
t.features.init(pool);
137129
t.libs.init();
138130
t.exports.init(pool);
139-
t.files = stdlib.malloc(t.max_files * sizeof(File));
140-
t.asm_file_max = 4;
141-
t.asm_files = stdlib.malloc(t.asm_file_max * sizeof(File));
131+
t.files.init(8);
132+
t.asm_files.init(0);
142133
return t;
143134
}
144135

@@ -147,8 +138,8 @@ public fn void Target.free(Target* t) {
147138
t.plugins.free();
148139
t.libs.free();
149140
t.features.free();
150-
stdlib.free(t.files);
151-
stdlib.free(t.asm_files);
141+
t.asm_files.free();
142+
t.files.free();
152143
stdlib.free(t);
153144
}
154145

@@ -158,9 +149,9 @@ public fn void Target.setNameIdx(Target* t, u32 name_idx) {
158149
t.name_idx = name_idx;
159150
}
160151

161-
public fn u32 Target.numFiles(const Target* t) { return t.num_files; }
152+
public fn u32 Target.numFiles(const Target* t) { return t.files.getCount(); }
162153

163-
public fn u32 Target.numAsmFiles(const Target* t) { return t.asm_file_count; }
154+
public fn u32 Target.numAsmFiles(const Target* t) { return t.asm_files.getCount(); }
164155

165156
public fn const string_list.List* Target.getFeatures(const Target* t) { return &t.features; }
166157

@@ -179,16 +170,18 @@ public fn void Target.disableAsserts(Target* t) { t.disable_asserts = true; }
179170

180171
public fn bool Target.hasAsserts(const Target* t) { return !t.disable_asserts; }
181172

182-
public fn void Target.visitLibs(const Target* t, library_list.Visitor visitor, void* arg) {
173+
public type Visitor fn void (void* arg, u32 name, Kind kind);
174+
175+
public fn void Target.visitLibs(const Target* t, Visitor visitor, void* arg) {
183176
t.libs.visit(visitor, arg);
184177
}
185178

186179
public fn bool Target.hasLib(const Target* t, u32 lib) {
187180
return t.libs.contains(lib);
188181
}
189182

190-
public fn void Target.addLib(Target* t, u32 lib, bool is_static) {
191-
t.libs.add(lib, is_static);
183+
public fn void Target.addLib(Target* t, u32 lib, Kind kind) {
184+
t.libs.add(lib, kind);
192185
}
193186

194187
public fn void Target.disableWarnings(Target* t) {
@@ -242,57 +235,26 @@ public fn bool Target.needsMain(const Target* t) {
242235
return true;
243236
case StaticLibrary:
244237
case DynamicLibrary:
238+
case SourceLibrary:
245239
break;
246240
}
247241
return false;
248242
}
249243

250244
public fn bool Target.addFile(Target* t, u32 filename, SrcLoc loc) {
251-
// check for duplicates
252-
for (u32 i=0; i<t.num_files; i++) {
253-
if (t.files[i].name == filename) return false;
254-
}
255-
256-
if (t.num_files == t.max_files) {
257-
t.max_files *= 2;
258-
File* files2 = stdlib.malloc(t.max_files * sizeof(File));
259-
string.memcpy(files2, t.files, t.num_files * sizeof(File));
260-
stdlib.free(t.files);
261-
t.files = files2;
262-
}
263-
264-
t.files[t.num_files].name = filename;
265-
t.files[t.num_files].loc = loc;
266-
t.num_files++;
267-
return true;
245+
return t.files.add(filename, loc);
268246
}
269247

270248
public fn const File* Target.getFile(const Target* t, u32 idx) {
271-
return &t.files[idx];
249+
return t.files.get(idx);
272250
}
273251

274252
public fn bool Target.addAsmFile(Target* t, u32 filename, SrcLoc loc) {
275-
// check for duplicates
276-
for (u32 i=0; i<t.asm_file_count; i++) {
277-
if (t.asm_files[i].name == filename) return false;
278-
}
279-
280-
if (t.asm_file_count == t.asm_file_max) {
281-
t.asm_file_max *= 2;
282-
File* files2 = stdlib.malloc(t.asm_file_max * sizeof(File));
283-
string.memcpy(files2, t.files, t.asm_file_count * sizeof(File));
284-
stdlib.free(t.asm_files);
285-
t.asm_files = files2;
286-
}
287-
288-
t.asm_files[t.asm_file_count].name = filename;
289-
t.asm_files[t.asm_file_count].loc = loc;
290-
t.asm_file_count++;
291-
return true;
253+
return t.asm_files.add(filename, loc);
292254
}
293255

294256
public fn const File* Target.getAsmFile(const Target* t, u32 idx) {
295-
return &t.asm_files[idx];
257+
return t.asm_files.get(idx);
296258
}
297259

298260
public fn void Target.setNoLibC(Target* t) {

0 commit comments

Comments
 (0)