Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions analyser/scope.c2
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ public fn void Scope.reset(Scope* s) {
fn void Scope.addImports(Scope* s) {
// Note: this also adds the module itself (import[0])

u32 num_imports = s.imports.size();
ast.ImportDecl** imports = s.imports.getDecls();
for (u32 i=0; i<num_imports; i++) {
ast.ImportDecl* id = imports[i];
for (u32 i = 0; i < s.imports.size(); i++) {
ast.ImportDecl* id = s.imports.get(i);
ast.Decl* d = (ast.Decl*)id;
u32 name_idx = id.getImportNameIdx();

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

// returns false if symbol exists and will give errors then
public fn bool Scope.checkGlobalSymbol(Scope* s, u32 name_idx, SrcLoc loc) {
u32 num_imports = s.imports.size();
ast.ImportDecl** imports = s.imports.getDecls();
ast.Decl* decl = nil;
for (u32 i=0; i<num_imports; i++) {
ast.ImportDecl* id = imports[i];
for (u32 i = 0; i < s.imports.size(); i++) {
ast.ImportDecl* id = s.imports.get(i);
// check if it is the import itself
if (name_idx == id.getImportNameIdx()) {
decl = (ast.Decl*)id;
Expand Down Expand Up @@ -418,10 +414,8 @@ fn ast.Decl* Scope.findGlobalSymbol(Scope* s, u32 name_idx, SrcLoc loc, bool* ot
ast.ImportDecl* used_import = nil;
bool ambiguous = false;

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

ast.Module* dest = id.getDest();
Expand Down Expand Up @@ -477,8 +471,8 @@ fn ast.Decl* Scope.findGlobalSymbol(Scope* s, u32 name_idx, SrcLoc loc, bool* ot
} else {
if (search_privates) {
// try finding a non-public symbol
for (u32 i=0; i<num_imports; i++) {
ast.ImportDecl* id = imports[i];
for (u32 i = 0; i < s.imports.size(); i++) {
ast.ImportDecl* id = s.imports.get(i);
if (!id.isLocal()) continue;
ast.Module* dest = id.getDest();
ast.Decl* d = dest.findPrivateSymbol(name_idx);
Expand Down
67 changes: 27 additions & 40 deletions ast/ast.c2
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public fn u32 AST.getNameIdx(const AST* a) {
}

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

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

public fn ImportDecl* AST.findImport(const AST* a, u32 name) {
ImportDecl** imports = a.imports.getDecls();
for (u32 i=1; i<a.imports.size(); i++) {
ImportDecl* d = imports[i];
ImportDecl* d = a.imports.get(i);
if (d.asDecl().getNameIdx() == name) return d;

}
return nil;
}
Expand Down Expand Up @@ -146,10 +144,9 @@ public fn void AST.addArrayValue(AST* a, ArrayValue* v) {
public type ImportVisitor fn void (void* arg, ImportDecl* d);

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

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

fn void AST.visitTypeFunctions(const AST* a, FunctionVisitor visitor, void* arg) {
FunctionDecl** functions = a.functions.getDecls();
for (u32 i=0; i<a.functions.size(); i++) {
FunctionDecl* d = functions[i];
FunctionDecl* d = a.functions.get(i);
if (d.hasPrefix()) visitor(arg, d);
}
}

public fn void AST.visitFunctions(const AST* a, FunctionVisitor visitor, void* arg) {
FunctionDecl** functions = a.functions.getDecls();
for (u32 i=0; i<a.functions.size(); i++) {
FunctionDecl* d = functions[i];
FunctionDecl* d = a.functions.get(i);
visitor(arg, d);
}
}

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

public fn void AST.visitTypeDecls(const AST* a, TypeDeclVisitor visitor, void* arg) {
Decl** types = a.types.getDecls();
for (u32 i=0; i<a.types.size(); i++) {
visitor(arg, types[i]);
visitor(arg, a.types.get(i));
}
}

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

public fn void AST.visitVarDecls(const AST* a, VarDeclVisitor visitor, void* arg) {
Decl** variables = a.variables.getDecls();
for (u32 i=0; i<a.variables.size(); i++) {
visitor(arg, (VarDecl*)variables[i]);
visitor(arg, (VarDecl*)a.variables.get(i));
}
}

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

public fn void AST.visitDecls(const AST* a, DeclVisitor visitor, void* arg) {
// imports
ImportDecl** imports = a.imports.getDecls();
for (u32 i=0; i<a.imports.size(); i++) {
visitor(arg, (Decl*)imports[i]);
visitor(arg, (Decl*)a.imports.get(i));
}

a.visitDeclsWithoutImports(visitor, arg);
}

fn void AST.visitDeclsWithoutImports(const AST* a, DeclVisitor visitor, void* arg) {
// types
Decl** types = a.types.getDecls();
for (u32 i=0; i<a.types.size(); i++) {
visitor(arg, types[i]);
visitor(arg, a.types.get(i));
}

// variables
Decl** variables = a.variables.getDecls();
for (u32 i=0; i<a.variables.size(); i++) {
visitor(arg, variables[i]);
visitor(arg, a.variables.get(i));
}

// functions
FunctionDecl** functions = a.functions.getDecls();
for (u32 i=0; i<a.functions.size(); i++) {
FunctionDecl* d = functions[i];
visitor(arg, (Decl*)d);
visitor(arg, (Decl*)a.functions.get(i));
}
}

fn Decl* AST.findType(const AST* a, u32 name_idx) {
Decl** types = a.types.getDecls();
for (u32 i=0; i<a.types.size(); i++) {
Decl* d = types[i];
Decl* d = a.types.get(i);
if (d.getNameIdx() == name_idx) return d;
}
return nil;
Expand Down Expand Up @@ -274,28 +261,28 @@ fn void AST.print(const AST* a, string_buffer.Buf* out, bool show_funcs) {
out.color(col_Normal);
out.print("---- AST %s ----\n", a.getFilename());

ImportDecl** imports = a.imports.getDecls();
for (u32 i=1; i<a.imports.size(); i++) {
imports[i].print(out, 0);
ImportDecl* d = a.imports.get(i);
d.print(out, 0);
}
if (a.imports.size() > 1) out.newline();

Decl** types = a.types.getDecls();
for (u32 i=0; i<a.types.size(); i++) {
types[i].print(out, 0);
Decl* d = a.types.get(i);
d.print(out, 0);
out.newline();
}

Decl** variables = a.variables.getDecls();
for (u32 i=0; i<a.variables.size(); i++) {
variables[i].print(out, 0);
Decl* d = a.variables.get(i);
d.print(out, 0);
out.newline();
}

if (show_funcs) {
FunctionDecl** functions = a.functions.getDecls();
for (u32 i=0; i<a.functions.size(); i++) {
functions[i].print(out, 0);
FunctionDecl* d = a.functions.get(i);
d.print(out, 0);
out.newline();
}
}
Expand All @@ -308,21 +295,21 @@ fn void AST.print(const AST* a, string_buffer.Buf* out, bool show_funcs) {

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

// variables
Decl** variables = a.variables.getDecls();
for (u32 i=0; i<a.variables.size(); i++) {
variables[i].setExportedIfPublic();
Decl* d = a.variables.get(i);
d.setExportedIfPublic();
}

// functions
Decl** functions = (Decl**)a.functions.getDecls();
for (u32 i=0; i<a.functions.size(); i++) {
functions[i].setExportedIfPublic();
Decl* d = (Decl*)a.functions.get(i);
d.setExportedIfPublic();
}
}

4 changes: 4 additions & 0 deletions ast/call_expr.c2
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ public fn Expr** CallExpr.getArgs(CallExpr* e) {
return e.args;
}

public fn Expr* CallExpr.getArg(const CallExpr* e, u32 i) {
return e.args[i];
}

fn void CallExpr.printLiteral(const CallExpr* e, string_buffer.Buf* out) {
e.func.printLiteral(out);
out.lparen();
Expand Down
4 changes: 4 additions & 0 deletions ast/function_decl_list.c2
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public fn u32 FunctionDeclList.size(const FunctionDeclList* l) {
return l.count;
}

public fn FunctionDecl* FunctionDeclList.get(const FunctionDeclList* l, u32 i) @(unused) {
return l.decls[i];
}

public fn FunctionDecl** FunctionDeclList.getDecls(const FunctionDeclList* l) {
return l.decls;
}
Expand Down
4 changes: 2 additions & 2 deletions ast/import_decl_list.c2
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public fn u32 ImportDeclList.size(const ImportDeclList* l) {
return l.count;
}

public fn ImportDecl** ImportDeclList.getDecls(const ImportDeclList* l) {
return l.decls;
public fn ImportDecl* ImportDeclList.get(const ImportDeclList* l, u32 i) {
return l.decls[i];
}

public fn ImportDecl* ImportDeclList.find(const ImportDeclList* l, u32 name_idx) {
Expand Down
5 changes: 2 additions & 3 deletions ast/struct_type_decl.c2
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ public fn bool StructTypeDecl.isUnion(const StructTypeDecl* d) {
return !d.base.structTypeDeclBits.is_struct;
}

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

fn u32 StructTypeDecl.getNumStructFunctions(const StructTypeDecl* d) {
Expand Down
7 changes: 3 additions & 4 deletions ast/symbol_table.c2
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public fn u32 SymbolTable.size(const SymbolTable* t) {
return t.num_public + t.num_private;
}

public fn Decl** SymbolTable.getDecls(const SymbolTable* t) {
return t.decls;
public fn Decl* SymbolTable.get(const SymbolTable* t, u32 i) {
return t.decls[i];
}

fn void SymbolTable.resize(SymbolTable* t, u32 capacity) {
Expand Down Expand Up @@ -124,9 +124,8 @@ public fn void SymbolTable.print(const SymbolTable* t, string_buffer.Buf* out) {
out.newline();
if (d.isStructType()) {
StructTypeDecl* std = (StructTypeDecl*)d;
const FunctionDecl** fds = std.getStructFunctions();
for (u32 j=0; j<std.getNumStructFunctions(); j++) {
Decl* fd = (Decl*)fds[j];
Decl* fd = (Decl*)std.getStructFunction(j);
out.color(fd.isUsed() ? color.Normal : color.Grey);
out.indent(6);
out.print("%s.%s()", name, fd.getName());
Expand Down
10 changes: 8 additions & 2 deletions ast/type_ref.c2
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ fn void TypeRef.init(TypeRef* dest, const TypeRefHolder* h) {
}

for (u32 i=0; i<r.flags.num_arrays; i++) {
Expr** a = dest.getArray2(i);
*a = h.arrays[i];
dest.setArray(i, h.arrays[i]);
}
}

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

fn void TypeRef.setArray(TypeRef* r, u32 idx, Expr* d) {
const u32 numrefs = r.isUser() + r.flags.has_prefix;
const u8* ptr = (u8*)r.refs + numrefs * sizeof(Ref);
Expr** arrays = (Expr**)ptr;
arrays[idx] = d;
}

fn void TypeRef.printLiteral(const TypeRef* r, string_buffer.Buf* out, bool print_prefix) {
if (r.isConst()) out.add("const ");
if (r.isVolatile()) out.add("volatile ");
Expand Down
9 changes: 3 additions & 6 deletions generator/c/c_generator_special.c2
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,11 @@ fn void Generator.createExportsFile(Generator* gen, const char* output_dir, comp

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

for (u32 i=0; i<count; i++) {
for (u32 i = 0; i < mods.length(); i++) {
const ast.SymbolTable* symbols = mods.at(i).getSymbols();
ast.Decl** decls = symbols.getDecls();
u32 num_symbols = symbols.size();
for (u32 j=0; j<num_symbols; j++) {
ast.Decl* d = decls[j];
for (u32 j = 0; j < symbols.size(); j++) {
ast.Decl* d = symbols.get(j);
if (!d.isExported()) continue;
if (!d.isFunction() && !d.isVariable()) continue;
out.add("\t\t");
Expand Down
4 changes: 2 additions & 2 deletions generator/c2i/c2i_generator_expr.c2
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ fn void Generator.emitCall(Generator* gen, string_buffer.Buf* out, const CallExp
// arguments
u32 num_args = call.getNumArgs();
Expr** args = ((CallExpr*)call).getArgs();
for (u32 i=0; i<num_args; i++) {
for (u32 i = 0; i < call.getNumArgs(); i++) {
if (i != 0) out.add(", ");
gen.emitExpr(out, args[i]);
gen.emitExpr(out, call.getArg(i));
}

out.rparen();
Expand Down
Loading