Skip to content

Commit b4911a9

Browse files
committed
Compiler: use size and get accessors in visitor functions
* allow for array reallocation in visitor handlers
1 parent 8d0f401 commit b4911a9

File tree

11 files changed

+67
-81
lines changed

11 files changed

+67
-81
lines changed

analyser/scope.c2

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ public fn void Scope.reset(Scope* s) {
106106
fn void Scope.addImports(Scope* s) {
107107
// Note: this also adds the module itself (import[0])
108108

109-
u32 num_imports = s.imports.size();
110-
ast.ImportDecl** imports = s.imports.getDecls();
111-
for (u32 i=0; i<num_imports; i++) {
112-
ast.ImportDecl* id = imports[i];
109+
for (u32 i = 0; i < s.imports.size(); i++) {
110+
ast.ImportDecl* id = s.imports.get(i);
113111
ast.Decl* d = (ast.Decl*)id;
114112
u32 name_idx = id.getImportNameIdx();
115113

@@ -308,11 +306,9 @@ public fn ast.Decl* Scope.find(Scope* s, u32 name_idx, SrcLoc loc, bool usedPubl
308306

309307
// returns false if symbol exists and will give errors then
310308
public fn bool Scope.checkGlobalSymbol(Scope* s, u32 name_idx, SrcLoc loc) {
311-
u32 num_imports = s.imports.size();
312-
ast.ImportDecl** imports = s.imports.getDecls();
313309
ast.Decl* decl = nil;
314-
for (u32 i=0; i<num_imports; i++) {
315-
ast.ImportDecl* id = imports[i];
310+
for (u32 i = 0; i < s.imports.size(); i++) {
311+
ast.ImportDecl* id = s.imports.get(i);
316312
// check if it is the import itself
317313
if (name_idx == id.getImportNameIdx()) {
318314
decl = (ast.Decl*)id;
@@ -418,10 +414,8 @@ fn ast.Decl* Scope.findGlobalSymbol(Scope* s, u32 name_idx, SrcLoc loc, bool* ot
418414
ast.ImportDecl* used_import = nil;
419415
bool ambiguous = false;
420416

421-
u32 num_imports = s.imports.size();
422-
ast.ImportDecl** imports = s.imports.getDecls();
423-
for (u32 i=0; i<num_imports; i++) {
424-
ast.ImportDecl* id = imports[i];
417+
for (u32 i = 0; i < s.imports.size(); i++) {
418+
ast.ImportDecl* id = s.imports.get(i);
425419
if (!id.isLocal()) continue; // this includes implicit self-import
426420

427421
ast.Module* dest = id.getDest();
@@ -477,8 +471,8 @@ fn ast.Decl* Scope.findGlobalSymbol(Scope* s, u32 name_idx, SrcLoc loc, bool* ot
477471
} else {
478472
if (search_privates) {
479473
// try finding a non-public symbol
480-
for (u32 i=0; i<num_imports; i++) {
481-
ast.ImportDecl* id = imports[i];
474+
for (u32 i = 0; i < s.imports.size(); i++) {
475+
ast.ImportDecl* id = s.imports.get(i);
482476
if (!id.isLocal()) continue;
483477
ast.Module* dest = id.getDest();
484478
ast.Decl* d = dest.findPrivateSymbol(name_idx);

ast/ast.c2

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public fn u32 AST.getNameIdx(const AST* a) {
9090
}
9191

9292
public fn SrcLoc AST.getLoc(const AST* a) {
93-
ImportDecl** imports = a.imports.getDecls();
94-
Decl* d = (Decl*)imports[0];
93+
// Note: use the first 'Import' since it is really the module statement
94+
Decl* d = (Decl*)a.imports.get(0);
9595
return d.getLoc();
9696
}
9797

@@ -106,11 +106,9 @@ public fn void AST.addImport(AST* a, ImportDecl* d) {
106106
}
107107

108108
public fn ImportDecl* AST.findImport(const AST* a, u32 name) {
109-
ImportDecl** imports = a.imports.getDecls();
110109
for (u32 i=1; i<a.imports.size(); i++) {
111-
ImportDecl* d = imports[i];
110+
ImportDecl* d = a.imports.get(i);
112111
if (d.asDecl().getNameIdx() == name) return d;
113-
114112
}
115113
return nil;
116114
}
@@ -146,10 +144,9 @@ public fn void AST.addArrayValue(AST* a, ArrayValue* v) {
146144
public type ImportVisitor fn void (void* arg, ImportDecl* d);
147145

148146
public fn void AST.visitImports(const AST* a, ImportVisitor visitor, void* arg) {
149-
ImportDecl** imports = a.imports.getDecls();
150147
// Note: skip first 'Import' since it is really the module statement
151148
for (u32 i=1; i<a.imports.size(); i++) {
152-
visitor(arg, imports[i]);
149+
visitor(arg, a.imports.get(i));
153150
}
154151
}
155152

@@ -167,36 +164,32 @@ fn void AST.visitArrayValues(AST* a, ArrayValueVisitor visitor, void* arg) {
167164
public type FunctionVisitor fn void (void* arg, FunctionDecl* d);
168165

169166
fn void AST.visitTypeFunctions(const AST* a, FunctionVisitor visitor, void* arg) {
170-
FunctionDecl** functions = a.functions.getDecls();
171167
for (u32 i=0; i<a.functions.size(); i++) {
172-
FunctionDecl* d = functions[i];
168+
FunctionDecl* d = a.functions.get(i);
173169
if (d.hasPrefix()) visitor(arg, d);
174170
}
175171
}
176172

177173
public fn void AST.visitFunctions(const AST* a, FunctionVisitor visitor, void* arg) {
178-
FunctionDecl** functions = a.functions.getDecls();
179174
for (u32 i=0; i<a.functions.size(); i++) {
180-
FunctionDecl* d = functions[i];
175+
FunctionDecl* d = a.functions.get(i);
181176
visitor(arg, d);
182177
}
183178
}
184179

185180
public type TypeDeclVisitor fn void (void* arg, Decl* d);
186181

187182
public fn void AST.visitTypeDecls(const AST* a, TypeDeclVisitor visitor, void* arg) {
188-
Decl** types = a.types.getDecls();
189183
for (u32 i=0; i<a.types.size(); i++) {
190-
visitor(arg, types[i]);
184+
visitor(arg, a.types.get(i));
191185
}
192186
}
193187

194188
public type VarDeclVisitor fn void (void* arg, VarDecl* d);
195189

196190
public fn void AST.visitVarDecls(const AST* a, VarDeclVisitor visitor, void* arg) {
197-
Decl** variables = a.variables.getDecls();
198191
for (u32 i=0; i<a.variables.size(); i++) {
199-
visitor(arg, (VarDecl*)variables[i]);
192+
visitor(arg, (VarDecl*)a.variables.get(i));
200193
}
201194
}
202195

@@ -213,39 +206,33 @@ public type DeclVisitor fn void (void* arg, Decl* d);
213206

214207
public fn void AST.visitDecls(const AST* a, DeclVisitor visitor, void* arg) {
215208
// imports
216-
ImportDecl** imports = a.imports.getDecls();
217209
for (u32 i=0; i<a.imports.size(); i++) {
218-
visitor(arg, (Decl*)imports[i]);
210+
visitor(arg, (Decl*)a.imports.get(i));
219211
}
220212

221213
a.visitDeclsWithoutImports(visitor, arg);
222214
}
223215

224216
fn void AST.visitDeclsWithoutImports(const AST* a, DeclVisitor visitor, void* arg) {
225217
// types
226-
Decl** types = a.types.getDecls();
227218
for (u32 i=0; i<a.types.size(); i++) {
228-
visitor(arg, types[i]);
219+
visitor(arg, a.types.get(i));
229220
}
230221

231222
// variables
232-
Decl** variables = a.variables.getDecls();
233223
for (u32 i=0; i<a.variables.size(); i++) {
234-
visitor(arg, variables[i]);
224+
visitor(arg, a.variables.get(i));
235225
}
236226

237227
// functions
238-
FunctionDecl** functions = a.functions.getDecls();
239228
for (u32 i=0; i<a.functions.size(); i++) {
240-
FunctionDecl* d = functions[i];
241-
visitor(arg, (Decl*)d);
229+
visitor(arg, (Decl*)a.functions.get(i));
242230
}
243231
}
244232

245233
fn Decl* AST.findType(const AST* a, u32 name_idx) {
246-
Decl** types = a.types.getDecls();
247234
for (u32 i=0; i<a.types.size(); i++) {
248-
Decl* d = types[i];
235+
Decl* d = a.types.get(i);
249236
if (d.getNameIdx() == name_idx) return d;
250237
}
251238
return nil;
@@ -274,28 +261,28 @@ fn void AST.print(const AST* a, string_buffer.Buf* out, bool show_funcs) {
274261
out.color(col_Normal);
275262
out.print("---- AST %s ----\n", a.getFilename());
276263

277-
ImportDecl** imports = a.imports.getDecls();
278264
for (u32 i=1; i<a.imports.size(); i++) {
279-
imports[i].print(out, 0);
265+
ImportDecl* d = a.imports.get(i);
266+
d.print(out, 0);
280267
}
281268
if (a.imports.size() > 1) out.newline();
282269

283-
Decl** types = a.types.getDecls();
284270
for (u32 i=0; i<a.types.size(); i++) {
285-
types[i].print(out, 0);
271+
Decl* d = a.types.get(i);
272+
d.print(out, 0);
286273
out.newline();
287274
}
288275

289-
Decl** variables = a.variables.getDecls();
290276
for (u32 i=0; i<a.variables.size(); i++) {
291-
variables[i].print(out, 0);
277+
Decl* d = a.variables.get(i);
278+
d.print(out, 0);
292279
out.newline();
293280
}
294281

295282
if (show_funcs) {
296-
FunctionDecl** functions = a.functions.getDecls();
297283
for (u32 i=0; i<a.functions.size(); i++) {
298-
functions[i].print(out, 0);
284+
FunctionDecl* d = a.functions.get(i);
285+
d.print(out, 0);
299286
out.newline();
300287
}
301288
}
@@ -308,21 +295,21 @@ fn void AST.print(const AST* a, string_buffer.Buf* out, bool show_funcs) {
308295

309296
fn void AST.setExported(AST* a) {
310297
// types, cannot be really exported, but do mark as such (for used?)
311-
Decl** types = a.types.getDecls();
312298
for (u32 i=0; i<a.types.size(); i++) {
313-
types[i].setExportedIfPublic();
299+
Decl* d = a.types.get(i);
300+
d.setExportedIfPublic();
314301
}
315302

316303
// variables
317-
Decl** variables = a.variables.getDecls();
318304
for (u32 i=0; i<a.variables.size(); i++) {
319-
variables[i].setExportedIfPublic();
305+
Decl* d = a.variables.get(i);
306+
d.setExportedIfPublic();
320307
}
321308

322309
// functions
323-
Decl** functions = (Decl**)a.functions.getDecls();
324310
for (u32 i=0; i<a.functions.size(); i++) {
325-
functions[i].setExportedIfPublic();
311+
Decl* d = (Decl*)a.functions.get(i);
312+
d.setExportedIfPublic();
326313
}
327314
}
328315

ast/call_expr.c2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ public fn Expr** CallExpr.getArgs(CallExpr* e) {
190190
return e.args;
191191
}
192192

193+
public fn Expr* CallExpr.getArg(const CallExpr* e, u32 i) {
194+
return e.args[i];
195+
}
196+
193197
fn void CallExpr.printLiteral(const CallExpr* e, string_buffer.Buf* out) {
194198
e.func.printLiteral(out);
195199
out.lparen();

ast/function_decl_list.c2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public fn u32 FunctionDeclList.size(const FunctionDeclList* l) {
5656
return l.count;
5757
}
5858

59+
public fn FunctionDecl* FunctionDeclList.get(const FunctionDeclList* l, u32 i) @(unused) {
60+
return l.decls[i];
61+
}
62+
5963
public fn FunctionDecl** FunctionDeclList.getDecls(const FunctionDeclList* l) {
6064
return l.decls;
6165
}

ast/import_decl_list.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public fn u32 ImportDeclList.size(const ImportDeclList* l) {
5252
return l.count;
5353
}
5454

55-
public fn ImportDecl** ImportDeclList.getDecls(const ImportDeclList* l) {
56-
return l.decls;
55+
public fn ImportDecl* ImportDeclList.get(const ImportDeclList* l, u32 i) {
56+
return l.decls[i];
5757
}
5858

5959
public fn ImportDecl* ImportDeclList.find(const ImportDeclList* l, u32 name_idx) {

ast/struct_type_decl.c2

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,8 @@ public fn bool StructTypeDecl.isUnion(const StructTypeDecl* d) {
166166
return !d.base.structTypeDeclBits.is_struct;
167167
}
168168

169-
fn const FunctionDecl** StructTypeDecl.getStructFunctions(const StructTypeDecl* d) {
170-
// TEMP const-cast until ptr-ptr -> const ptr-ptr is fixed in analyser
171-
return (const FunctionDecl**)d.struct_functions;
169+
fn FunctionDecl* StructTypeDecl.getStructFunction(const StructTypeDecl* d, u32 i) {
170+
return d.struct_functions[i];
172171
}
173172

174173
fn u32 StructTypeDecl.getNumStructFunctions(const StructTypeDecl* d) {

ast/symbol_table.c2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public fn u32 SymbolTable.size(const SymbolTable* t) {
4646
return t.num_public + t.num_private;
4747
}
4848

49-
public fn Decl** SymbolTable.getDecls(const SymbolTable* t) {
50-
return t.decls;
49+
public fn Decl* SymbolTable.get(const SymbolTable* t, u32 i) {
50+
return t.decls[i];
5151
}
5252

5353
fn void SymbolTable.resize(SymbolTable* t, u32 capacity) {
@@ -124,9 +124,8 @@ public fn void SymbolTable.print(const SymbolTable* t, string_buffer.Buf* out) {
124124
out.newline();
125125
if (d.isStructType()) {
126126
StructTypeDecl* std = (StructTypeDecl*)d;
127-
const FunctionDecl** fds = std.getStructFunctions();
128127
for (u32 j=0; j<std.getNumStructFunctions(); j++) {
129-
Decl* fd = (Decl*)fds[j];
128+
Decl* fd = (Decl*)std.getStructFunction(j);
130129
out.color(fd.isUsed() ? color.Normal : color.Grey);
131130
out.indent(6);
132131
out.print("%s.%s()", name, fd.getName());

ast/type_ref.c2

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ fn void TypeRef.init(TypeRef* dest, const TypeRefHolder* h) {
227227
}
228228

229229
for (u32 i=0; i<r.flags.num_arrays; i++) {
230-
Expr** a = dest.getArray2(i);
231-
*a = h.arrays[i];
230+
dest.setArray(i, h.arrays[i]);
232231
}
233232
}
234233

@@ -417,6 +416,13 @@ public fn Expr** TypeRef.getArray2(TypeRef* r, u32 idx) {
417416
return &arrays[idx];
418417
}
419418

419+
fn void TypeRef.setArray(TypeRef* r, u32 idx, Expr* d) {
420+
const u32 numrefs = r.isUser() + r.flags.has_prefix;
421+
const u8* ptr = (u8*)r.refs + numrefs * sizeof(Ref);
422+
Expr** arrays = (Expr**)ptr;
423+
arrays[idx] = d;
424+
}
425+
420426
fn void TypeRef.printLiteral(const TypeRef* r, string_buffer.Buf* out, bool print_prefix) {
421427
if (r.isConst()) out.add("const ");
422428
if (r.isVolatile()) out.add("volatile ");

generator/c/c_generator_special.c2

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,11 @@ fn void Generator.createExportsFile(Generator* gen, const char* output_dir, comp
239239

240240
// iterate all mods in main Component
241241
module_list.List* mods = mainComp.getModules();
242-
u32 count = mods.length();
243242

244-
for (u32 i=0; i<count; i++) {
243+
for (u32 i = 0; i < mods.length(); i++) {
245244
const ast.SymbolTable* symbols = mods.at(i).getSymbols();
246-
ast.Decl** decls = symbols.getDecls();
247-
u32 num_symbols = symbols.size();
248-
for (u32 j=0; j<num_symbols; j++) {
249-
ast.Decl* d = decls[j];
245+
for (u32 j = 0; j < symbols.size(); j++) {
246+
ast.Decl* d = symbols.get(j);
250247
if (!d.isExported()) continue;
251248
if (!d.isFunction() && !d.isVariable()) continue;
252249
out.add("\t\t");

generator/c2i/c2i_generator_expr.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ fn void Generator.emitCall(Generator* gen, string_buffer.Buf* out, const CallExp
250250
// arguments
251251
u32 num_args = call.getNumArgs();
252252
Expr** args = ((CallExpr*)call).getArgs();
253-
for (u32 i=0; i<num_args; i++) {
253+
for (u32 i = 0; i < call.getNumArgs(); i++) {
254254
if (i != 0) out.add(", ");
255-
gen.emitExpr(out, args[i]);
255+
gen.emitExpr(out, call.getArg(i));
256256
}
257257

258258
out.rparen();

0 commit comments

Comments
 (0)