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
4 changes: 2 additions & 2 deletions analyser/module_analyser.c2
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn void Analyser.handleStructFunc(void* arg, FunctionDecl* fd) {
} else {
// search data structure
bool found = false;
found = ma.prefixes.find(prefix_name_idx, &index);
found = ma.prefixes.findIndex(prefix_name_idx, &index);

if (!found) {
const char* msg = "a type-function type must be a struct/union/enum";
Expand All @@ -376,7 +376,7 @@ fn void Analyser.handleStructFunc(void* arg, FunctionDecl* fd) {
ma.error(prefix.loc, "public type-functions need a public type");
return;
}
index = ma.prefixes.add(prefix_name_idx);
index = ma.prefixes.add({ prefix_name_idx });
ma.type_fn_decls.addDecl(decl);
}
ma.prefix_cache_name = prefix_name_idx;
Expand Down
25 changes: 8 additions & 17 deletions analyser/module_analyser_struct.c2
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ fn void Analyser.analyseStructType(Analyser* ma, StructTypeDecl* d) {
ma.checkStack[ma.checkIndex-1].usedPublic = false;
ma.usedPublic = false;
}
u32 num_members = d.getNumMembers();
NameVector names.init(d.getNumMembers());

NameVector names.init(num_members);
NameVector locs.init(num_members);

ma.analyseStructNames(d, &names, &locs);
ma.analyseStructNames(d, &names);

names.free();
locs.free();

ma.analyseStructMembers(d);

Expand Down Expand Up @@ -175,7 +171,7 @@ fn void Analyser.analyseStructMember(Analyser* ma, VarDecl* v) {
// TODO check initValue
}

fn void Analyser.analyseStructNames(Analyser* ma, StructTypeDecl* d, NameVector* names, NameVector* locs) {
fn void Analyser.analyseStructNames(Analyser* ma, StructTypeDecl* d, NameVector* names) {
// note: already checked that struct doesn't have 0 members
u32 count = d.getNumMembers();
Decl** members = d.getMembers();
Expand All @@ -191,27 +187,22 @@ fn void Analyser.analyseStructNames(Analyser* ma, StructTypeDecl* d, NameVector*
if (name_idx == 0) {
// can be anonymous sub-struct/union or anonymous bit-field
if (member.isStructType()) {
ma.analyseStructNames(sub, names, locs);
ma.analyseStructNames(sub, names);
}
} else {
u32 old_index;
if (names.find(name_idx, &old_index)) {
if (NamePosition* found = names.findPtr(name_idx)) {
ma.error(member.getLoc(), "duplicate struct/union member '%s'", member.getName());
ma.note(locs.get(old_index), "previous declaration is here");
ma.note(found.loc, "previous declaration is here");
return;
}
names.add(name_idx);
locs.add(member.getLoc());
names.add({ name_idx, member.getLoc() });

if (member.isStructType()) {
NameVector sub_names.init(sub.getNumMembers());
NameVector sub_locs.init(sub.getNumMembers());
ma.analyseStructNames(sub, &sub_names, &sub_locs);
ma.analyseStructNames(sub, &sub_names);
sub_names.free();
sub_locs.free();
}
}
}
}


30 changes: 20 additions & 10 deletions analyser/name_vector.c2
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ module name_vector;
import stdlib;
import string;

public type NamePosition struct {
u32 name_idx;
u32 loc;
}

public type NameVector struct {
u32* data;
NamePosition* data;
u32 count;
u32 capacity;
}
Expand All @@ -28,7 +33,7 @@ public fn void NameVector.init(NameVector* v, u32 capacity) @(unused) {
v.data = nil;
v.count = 0;
v.capacity = capacity;
if (capacity) v.data = stdlib.malloc(capacity * sizeof(u32));
if (capacity) v.data = stdlib.malloc(capacity * sizeof(NamePosition));
}

public fn void NameVector.free(NameVector* v) {
Expand All @@ -44,30 +49,35 @@ public fn void NameVector.clear(NameVector* v) {

fn void NameVector.resize(NameVector* v) {
v.capacity = v.capacity == 0 ? 4 : v.capacity * 2;
void* data2 = stdlib.malloc(v.capacity * sizeof(u32));
void* data2 = stdlib.malloc(v.capacity * sizeof(NamePosition));
if (v.data) {
string.memcpy(data2, v.data, v.count * sizeof(u32));
string.memcpy(data2, v.data, v.count * sizeof(NamePosition));
stdlib.free(v.data);
}
v.data = data2;
}

public fn u32 NameVector.add(NameVector* v, u32 name_idx) {
public fn u32 NameVector.add(NameVector* v, NamePosition name_pos) {
if (v.count == v.capacity) v.resize();

u32 index = v.count;
v.data[index] = name_idx;
v.data[index] = name_pos;
v.count++;
return index;
}

public fn u32 NameVector.get(const NameVector* v, u32 idx) {
return v.data[idx];
public fn NamePosition* NameVector.findPtr(NameVector* v, u32 name_idx) {
for (u32 i=0; i<v.count; i++) {
if (v.data[i].name_idx == name_idx) {
return &v.data[i];
}
}
return nil;
}

public fn bool NameVector.find(NameVector* v, u32 name_idx, u32* index) {
public fn bool NameVector.findIndex(NameVector* v, u32 name_idx, u32* index) {
for (u32 i=0; i<v.count; i++) {
if (v.data[i] == name_idx) {
if (v.data[i].name_idx == name_idx) {
*index = i;
return true;
}
Expand Down