Skip to content

Commit 7b6e205

Browse files
committed
Analyser: store loc in NameVector
* simplify struct member duplicate check
1 parent 67f32b6 commit 7b6e205

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

analyser/module_analyser.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ fn void Analyser.handleStructFunc(void* arg, FunctionDecl* fd) {
360360
} else {
361361
// search data structure
362362
bool found = false;
363-
found = ma.prefixes.find(prefix_name_idx, &index);
363+
found = ma.prefixes.findIndex(prefix_name_idx, &index);
364364

365365
if (!found) {
366366
const char* msg = "a type-function type must be a struct/union/enum";
@@ -382,7 +382,7 @@ fn void Analyser.handleStructFunc(void* arg, FunctionDecl* fd) {
382382
ma.error(prefix.loc, "public type-functions need a public type");
383383
return;
384384
}
385-
index = ma.prefixes.add(prefix_name_idx);
385+
index = ma.prefixes.add({ prefix_name_idx });
386386
ma.type_fn_decls.addDecl(decl);
387387
}
388388
ma.prefix_cache_name = prefix_name_idx;

analyser/module_analyser_struct.c2

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ fn void Analyser.analyseStructType(Analyser* ma, StructTypeDecl* d) {
2424
ma.checkStack[ma.checkIndex-1].usedPublic = false;
2525
ma.usedPublic = false;
2626
}
27-
u32 num_members = d.getNumMembers();
27+
NameVector names.init(d.getNumMembers());
2828

29-
NameVector names.init(num_members);
30-
NameVector locs.init(num_members);
31-
32-
ma.analyseStructNames(d, &names, &locs);
29+
ma.analyseStructNames(d, &names);
3330

3431
names.free();
35-
locs.free();
3632

3733
ma.analyseStructMembers(d);
3834

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

178-
fn void Analyser.analyseStructNames(Analyser* ma, StructTypeDecl* d, NameVector* names, NameVector* locs) {
174+
fn void Analyser.analyseStructNames(Analyser* ma, StructTypeDecl* d, NameVector* names) {
179175
// note: already checked that struct doesn't have 0 members
180176
u32 count = d.getNumMembers();
181177
Decl** members = d.getMembers();
@@ -191,25 +187,22 @@ fn void Analyser.analyseStructNames(Analyser* ma, StructTypeDecl* d, NameVector*
191187
if (name_idx == 0) {
192188
// can be anonymous sub-struct/union or anonymous bit-field
193189
if (member.isStructType()) {
194-
ma.analyseStructNames(sub, names, locs);
190+
ma.analyseStructNames(sub, names);
195191
}
196192
} else {
197-
u32 old_index;
198-
if (names.find(name_idx, &old_index)) {
193+
if (NamePosition* found = names.findPtr(name_idx)) {
199194
ma.error(member.getLoc(), "duplicate struct/union member '%s'", member.getName());
200-
ma.note(locs.get(old_index), "previous declaration is here");
195+
ma.note(found.loc, "previous declaration is here");
201196
return;
202197
}
203-
names.add(name_idx);
204-
locs.add(member.getLoc());
198+
names.add({ name_idx, member.getLoc() });
205199

206200
if (member.isStructType()) {
207201
NameVector sub_names.init(sub.getNumMembers());
208-
NameVector sub_locs.init(sub.getNumMembers());
209-
ma.analyseStructNames(sub, &sub_names, &sub_locs);
202+
ma.analyseStructNames(sub, &sub_names);
210203
sub_names.free();
211-
sub_locs.free();
212204
}
213205
}
214206
}
215207
}
208+

analyser/name_vector.c2

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ module name_vector;
1818
import stdlib;
1919
import string;
2020

21+
public type NamePosition struct {
22+
u32 name_idx;
23+
u32 loc;
24+
}
25+
2126
public type NameVector struct {
22-
u32* data;
27+
NamePosition* data;
2328
u32 count;
2429
u32 capacity;
2530
}
@@ -28,7 +33,7 @@ public fn void NameVector.init(NameVector* v, u32 capacity) @(unused) {
2833
v.data = nil;
2934
v.count = 0;
3035
v.capacity = capacity;
31-
if (capacity) v.data = stdlib.malloc(capacity * sizeof(u32));
36+
if (capacity) v.data = stdlib.malloc(capacity * sizeof(NamePosition));
3237
}
3338

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

4550
fn void NameVector.resize(NameVector* v) {
4651
v.capacity = v.capacity == 0 ? 4 : v.capacity * 2;
47-
void* data2 = stdlib.malloc(v.capacity * sizeof(u32));
52+
void* data2 = stdlib.malloc(v.capacity * sizeof(NamePosition));
4853
if (v.data) {
49-
string.memcpy(data2, v.data, v.count * sizeof(u32));
54+
string.memcpy(data2, v.data, v.count * sizeof(NamePosition));
5055
stdlib.free(v.data);
5156
}
5257
v.data = data2;
5358
}
5459

55-
public fn u32 NameVector.add(NameVector* v, u32 name_idx) {
60+
public fn u32 NameVector.add(NameVector* v, NamePosition name_pos) {
5661
if (v.count == v.capacity) v.resize();
5762

5863
u32 index = v.count;
59-
v.data[index] = name_idx;
64+
v.data[index] = name_pos;
6065
v.count++;
6166
return index;
6267
}
6368

64-
public fn u32 NameVector.get(const NameVector* v, u32 idx) {
65-
return v.data[idx];
69+
public fn NamePosition* NameVector.findPtr(NameVector* v, u32 name_idx) {
70+
for (u32 i=0; i<v.count; i++) {
71+
if (v.data[i].name_idx == name_idx) {
72+
return &v.data[i];
73+
}
74+
}
75+
return nil;
6676
}
6777

68-
public fn bool NameVector.find(NameVector* v, u32 name_idx, u32* index) {
78+
public fn bool NameVector.findIndex(NameVector* v, u32 name_idx, u32* index) {
6979
for (u32 i=0; i<v.count; i++) {
70-
if (v.data[i] == name_idx) {
80+
if (v.data[i].name_idx == name_idx) {
7181
*index = i;
7282
return true;
7383
}

0 commit comments

Comments
 (0)