Repository navigation
Expand file tree
/
Copy pathchecker.cpp
More file actions
7551 lines (6486 loc) · 220 KB
/
Copy pathchecker.cpp
File metadata and controls
7551 lines (6486 loc) · 220 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define DEBUG_CHECK_ALL_PROCEDURES 1
#include "entity.cpp"
#include "types.cpp"
gb_internal u64 type_hash_canonical_type(Type *type);
gb_internal String get_final_microarchitecture();
gb_internal void check_expr(CheckerContext *c, Operand *operand, Ast *expression);
gb_internal void check_expr_or_type(CheckerContext *c, Operand *operand, Ast *expression, Type *type_hint=nullptr);
gb_internal void add_comparison_procedures_for_fields(CheckerContext *c, Type *t);
gb_internal Type *check_type(CheckerContext *ctx, Ast *e);
gb_internal bool is_operand_value(Operand o) {
switch (o.mode) {
case Addressing_Value:
case Addressing_Context:
case Addressing_Variable:
case Addressing_Constant:
case Addressing_MapIndex:
case Addressing_OptionalOk:
case Addressing_OptionalOkPtr:
case Addressing_SoaVariable:
case Addressing_SwizzleValue:
case Addressing_SwizzleVariable:
return true;
}
return false;
}
gb_internal bool is_operand_nil(Operand o) {
return o.mode == Addressing_Value && o.type == t_untyped_nil;
}
gb_internal bool is_operand_uninit(Operand o) {
return o.mode == Addressing_Value && o.type == t_untyped_uninit;
}
gb_internal bool check_rtti_type_disallowed(Token const &token, Type *type, char const *format) {
if (build_context.no_rtti && type) {
if (is_type_any(type)) {
gbString t = type_to_string(type);
error(token, format, t);
gb_string_free(t);
return true;
}
}
return false;
}
gb_internal bool check_rtti_type_disallowed(Ast *expr, Type *type, char const *format) {
GB_ASSERT(expr != nullptr);
return check_rtti_type_disallowed(ast_token(expr), type, format);
}
gb_internal void scope_reserve(Scope *scope, isize count) {
string_map_reserve(&scope->elements, 2*count);
}
gb_internal void entity_graph_node_set_destroy(EntityGraphNodeSet *s) {
ptr_set_destroy(s);
}
gb_internal void entity_graph_node_set_add(EntityGraphNodeSet *s, EntityGraphNode *n) {
ptr_set_add(s, n);
}
// gb_internal bool entity_graph_node_set_exists(EntityGraphNodeSet *s, EntityGraphNode *n) {
// return ptr_set_exists(s, n);
// }
gb_internal void entity_graph_node_set_remove(EntityGraphNodeSet *s, EntityGraphNode *n) {
ptr_set_remove(s, n);
}
gb_internal void entity_graph_node_destroy(EntityGraphNode *n, gbAllocator a) {
entity_graph_node_set_destroy(&n->pred);
entity_graph_node_set_destroy(&n->succ);
gb_free(a, n);
}
gb_internal int entity_graph_node_cmp(EntityGraphNode **data, isize i, isize j) {
EntityGraphNode *x = data[i];
EntityGraphNode *y = data[j];
u64 a = x->entity->order_in_src;
u64 b = y->entity->order_in_src;
if (x->dep_count < y->dep_count) {
return -1;
}
if (x->dep_count == y->dep_count) {
return a < b ? -1 : b > a;
}
return +1;
}
gb_internal void entity_graph_node_swap(EntityGraphNode **data, isize i, isize j) {
EntityGraphNode *x = data[i];
EntityGraphNode *y = data[j];
data[i] = y;
data[j] = x;
x->index = j;
y->index = i;
}
gb_internal void import_graph_node_set_destroy(ImportGraphNodeSet *s) {
ptr_set_destroy(s);
}
gb_internal void import_graph_node_set_add(ImportGraphNodeSet *s, ImportGraphNode *n) {
ptr_set_add(s, n);
}
// gb_internal bool import_graph_node_set_exists(ImportGraphNodeSet *s, ImportGraphNode *n) {
// return ptr_set_exists(s, n);
// }
// gb_internal void import_graph_node_set_remove(ImportGraphNodeSet *s, ImportGraphNode *n) {
// ptr_set_remove(s, n);
// }
gb_internal ImportGraphNode *import_graph_node_create(gbAllocator a, AstPackage *pkg) {
ImportGraphNode *n = gb_alloc_item(a, ImportGraphNode);
n->pkg = pkg;
n->scope = pkg->scope;
return n;
}
gb_internal void import_graph_node_destroy(ImportGraphNode *n, gbAllocator a) {
import_graph_node_set_destroy(&n->pred);
import_graph_node_set_destroy(&n->succ);
gb_free(a, n);
}
gb_internal int import_graph_node_cmp(ImportGraphNode **data, isize i, isize j) {
ImportGraphNode *x = data[i];
ImportGraphNode *y = data[j];
GB_ASSERT(x != y);
GB_ASSERT(x->scope != y->scope);
bool xg = (x->scope->flags&ScopeFlag_Global) != 0;
bool yg = (y->scope->flags&ScopeFlag_Global) != 0;
if (xg != yg) return xg ? -1 : +1;
if (xg && yg) return x->pkg->id < y->pkg->id ? +1 : -1;
if (x->dep_count < y->dep_count) return -1;
if (x->dep_count > y->dep_count) return +1;
return 0;
}
gb_internal void import_graph_node_swap(ImportGraphNode **data, isize i, isize j) {
ImportGraphNode *x = data[i];
ImportGraphNode *y = data[j];
data[i] = y;
data[j] = x;
x->index = j;
y->index = i;
}
gb_internal void init_decl_info(DeclInfo *d, Scope *scope, DeclInfo *parent) {
gb_zero_item(d);
if (parent) {
mutex_lock(&parent->next_mutex);
d->next_sibling = parent->next_child;
parent->next_child = d;
mutex_unlock(&parent->next_mutex);
}
d->parent = parent;
d->scope = scope;
ptr_set_init(&d->deps, 0);
type_set_init(&d->type_info_deps, 0);
d->labels.allocator = heap_allocator();
d->variadic_reuses.allocator = heap_allocator();
d->variadic_reuse_max_bytes = 0;
d->variadic_reuse_max_align = 1;
}
gb_internal DeclInfo *make_decl_info(Scope *scope, DeclInfo *parent) {
DeclInfo *d = gb_alloc_item(permanent_allocator(), DeclInfo);
init_decl_info(d, scope, parent);
return d;
}
// gb_internal void destroy_declaration_info(DeclInfo *d) {
// mutex_destroy(&d->proc_checked_mutex);
// ptr_set_destroy(&d->deps);
// array_free(&d->labels);
// }
// gb_internal bool decl_info_has_init(DeclInfo *d) {
// if (d->init_expr != nullptr) {
// return true;
// }
// if (d->proc_lit != nullptr) {
// switch (d->proc_lit->kind) {
// case_ast_node(pl, ProcLit, d->proc_lit);
// if (pl->body != nullptr) {
// return true;
// }
// case_end;
// }
// }
// return false;
// }
gb_internal Scope *create_scope(CheckerInfo *info, Scope *parent) {
Scope *s = gb_alloc_item(permanent_allocator(), Scope);
s->parent = parent;
if (parent != nullptr && parent != builtin_pkg->scope) {
Scope *prev_head_child = parent->head_child.exchange(s, std::memory_order_acq_rel);
if (prev_head_child) {
s->next.store(prev_head_child, std::memory_order_release);
}
}
if (parent != nullptr && parent->flags & ScopeFlag_ContextDefined) {
s->flags |= ScopeFlag_ContextDefined;
}
return s;
}
gb_internal Scope *create_scope_from_file(CheckerInfo *info, AstFile *f) {
GB_ASSERT(f != nullptr);
GB_ASSERT(f->pkg != nullptr);
GB_ASSERT(f->pkg->scope != nullptr);
isize init_elements_capacity = gb_max(DEFAULT_SCOPE_CAPACITY, 2*f->total_file_decl_count);
Scope *s = create_scope(info, f->pkg->scope);
string_map_init(&s->elements, init_elements_capacity);
s->flags |= ScopeFlag_File;
s->file = f;
f->scope = s;
return s;
}
gb_internal Scope *create_scope_from_package(CheckerContext *c, AstPackage *pkg) {
GB_ASSERT(pkg != nullptr);
isize total_pkg_decl_count = 0;
for (AstFile *file : pkg->files) {
total_pkg_decl_count += file->total_file_decl_count;
}
isize init_elements_capacity = gb_max(DEFAULT_SCOPE_CAPACITY, 2*total_pkg_decl_count);
Scope *s = create_scope(c->info, builtin_pkg->scope);
string_map_init(&s->elements, init_elements_capacity);
s->flags |= ScopeFlag_Pkg;
s->pkg = pkg;
pkg->scope = s;
if (pkg->fullpath == c->checker->parser->init_fullpath || pkg->kind == Package_Init) {
s->flags |= ScopeFlag_Init;
}
if (pkg->kind == Package_Runtime) {
s->flags |= ScopeFlag_Global;
}
if (s->flags & (ScopeFlag_Init|ScopeFlag_Global)) {
s->flags |= ScopeFlag_HasBeenImported;
}
s->flags |= ScopeFlag_ContextDefined;
return s;
}
gb_internal void destroy_scope(Scope *scope) {
for (Scope *child = scope->head_child; child != nullptr; child = child->next) {
destroy_scope(child);
}
string_map_destroy(&scope->elements);
ptr_set_destroy(&scope->imported);
// NOTE(bill): No need to free scope as it "should" be allocated in an arena (except for the global scope)
}
gb_internal void add_scope(CheckerContext *c, Ast *node, Scope *scope) {
GB_ASSERT(node != nullptr);
GB_ASSERT(scope != nullptr);
scope->node = node;
switch (node->kind) {
case Ast_BlockStmt: node->BlockStmt.scope = scope; break;
case Ast_IfStmt: node->IfStmt.scope = scope; break;
case Ast_ForStmt: node->ForStmt.scope = scope; break;
case Ast_RangeStmt: node->RangeStmt.scope = scope; break;
case Ast_UnrollRangeStmt: node->UnrollRangeStmt.scope = scope; break;
case Ast_CaseClause: node->CaseClause.scope = scope; break;
case Ast_SwitchStmt: node->SwitchStmt.scope = scope; break;
case Ast_TypeSwitchStmt: node->TypeSwitchStmt.scope = scope; break;
case Ast_ProcType: node->ProcType.scope = scope; break;
case Ast_StructType: node->StructType.scope = scope; break;
case Ast_UnionType: node->UnionType.scope = scope; break;
case Ast_EnumType: node->EnumType.scope = scope; break;
case Ast_BitFieldType: node->BitFieldType.scope = scope; break;
default: GB_PANIC("Invalid node for add_scope: %.*s", LIT(ast_strings[node->kind]));
}
}
gb_internal Scope *scope_of_node(Ast *node) {
if (node == nullptr) {
return nullptr;
}
switch (node->kind) {
case Ast_BlockStmt: return node->BlockStmt.scope;
case Ast_IfStmt: return node->IfStmt.scope;
case Ast_ForStmt: return node->ForStmt.scope;
case Ast_RangeStmt: return node->RangeStmt.scope;
case Ast_UnrollRangeStmt: return node->UnrollRangeStmt.scope;
case Ast_CaseClause: return node->CaseClause.scope;
case Ast_SwitchStmt: return node->SwitchStmt.scope;
case Ast_TypeSwitchStmt: return node->TypeSwitchStmt.scope;
case Ast_ProcType: return node->ProcType.scope;
case Ast_StructType: return node->StructType.scope;
case Ast_UnionType: return node->UnionType.scope;
case Ast_EnumType: return node->EnumType.scope;
case Ast_BitFieldType: return node->BitFieldType.scope;
}
GB_PANIC("Invalid node for add_scope: %.*s", LIT(ast_strings[node->kind]));
return nullptr;
}
gb_internal void check_open_scope(CheckerContext *c, Ast *node) {
node = unparen_expr(node);
GB_ASSERT(node != nullptr);
GB_ASSERT(node->kind == Ast_Invalid ||
is_ast_stmt(node) ||
is_ast_type(node));
Scope *scope = create_scope(c->info, c->scope);
add_scope(c, node, scope);
switch (node->kind) {
case Ast_ProcType:
scope->flags |= ScopeFlag_Proc;
break;
case Ast_StructType:
case Ast_EnumType:
case Ast_UnionType:
case Ast_BitSetType:
case Ast_BitFieldType:
scope->flags |= ScopeFlag_Type;
break;
}
if (c->decl && c->decl->proc_lit) {
// Number the scopes within a procedure body depth-first
scope->index = c->decl->scope_index++;
}
c->scope = scope;
c->state_flags |= StateFlag_bounds_check;
}
gb_internal void check_close_scope(CheckerContext *c) {
c->scope = c->scope->parent;
}
gb_internal Entity *scope_lookup_current(Scope *s, String const &name) {
Entity **found = string_map_get(&s->elements, name);
if (found) {
return *found;
}
return nullptr;
}
gb_global std::atomic<bool> in_single_threaded_checker_stage;
gb_internal void scope_lookup_parent(Scope *scope, String const &name, Scope **scope_, Entity **entity_, u32 hash) {
bool is_single_threaded = in_single_threaded_checker_stage.load(std::memory_order_relaxed);
if (scope != nullptr) {
bool gone_thru_proc = false;
bool gone_thru_package = false;
StringHashKey key = {};
if (hash) {
key.hash = hash;
key.string = name;
} else {
key = string_hash_string(name);
}
for (Scope *s = scope; s != nullptr; s = s->parent) {
Entity **found = nullptr;
if (!is_single_threaded) rw_mutex_shared_lock(&s->mutex);
found = string_map_get(&s->elements, key);
if (!is_single_threaded) rw_mutex_shared_unlock(&s->mutex);
if (found) {
Entity *e = *found;
if (gone_thru_proc) {
if (e->kind == Entity_Label) {
continue;
}
if (e->kind == Entity_Variable) {
if (e->scope->flags&ScopeFlag_File) {
// Global variables are file to access
} else if (e->flags&EntityFlag_Static) {
// Allow static/thread_local variables to be referenced
} else {
continue;
}
}
}
if (entity_) *entity_ = e;
if (scope_) *scope_ = s;
return;
}
if (s->flags&ScopeFlag_Proc) {
gone_thru_proc = true;
}
if (s->flags&ScopeFlag_Pkg) {
gone_thru_package = true;
}
}
}
if (entity_) *entity_ = nullptr;
if (scope_) *scope_ = nullptr;
}
gb_internal Entity *scope_lookup(Scope *s, String const &name, u32 hash) {
Entity *entity = nullptr;
scope_lookup_parent(s, name, nullptr, &entity, hash);
return entity;
}
gb_internal Entity *scope_insert_with_name_no_mutex(Scope *s, String const &name, Entity *entity) {
if (name == "") {
return nullptr;
}
StringHashKey key = string_hash_string(name);
Entity **found = nullptr;
Entity *result = nullptr;
found = string_map_get(&s->elements, key);
if (found) {
if (entity != *found) {
result = *found;
}
goto end;
}
if (s->parent != nullptr && (s->parent->flags & ScopeFlag_Proc) != 0) {
found = string_map_get(&s->parent->elements, key);
if (found) {
if ((*found)->flags & EntityFlag_Result) {
if (entity != *found) {
result = *found;
}
goto end;
}
}
}
string_map_set(&s->elements, key, entity);
if (entity->scope == nullptr) {
entity->scope = s;
}
end:;
return result;
}
gb_internal Entity *scope_insert_with_name(Scope *s, String const &name, Entity *entity) {
if (name == "") {
return nullptr;
}
StringHashKey key = string_hash_string(name);
Entity **found = nullptr;
Entity *result = nullptr;
rw_mutex_lock(&s->mutex);
found = string_map_get(&s->elements, key);
if (found) {
if (entity != *found) {
result = *found;
}
goto end;
}
if (s->parent != nullptr && (s->parent->flags & ScopeFlag_Proc) != 0) {
found = string_map_get(&s->parent->elements, key);
if (found) {
if ((*found)->flags & EntityFlag_Result) {
if (entity != *found) {
result = *found;
}
goto end;
}
}
}
string_map_set(&s->elements, key, entity);
if (entity->scope == nullptr) {
entity->scope = s;
}
end:;
rw_mutex_unlock(&s->mutex);
return result;
}
gb_internal Entity *scope_insert(Scope *s, Entity *entity) {
String name = entity->token.string;
if (in_single_threaded_checker_stage.load(std::memory_order_relaxed)) {
return scope_insert_with_name_no_mutex(s, name, entity);
} else {
return scope_insert_with_name(s, name, entity);
}
}
gb_internal Entity *scope_insert_no_mutex(Scope *s, Entity *entity) {
String name = entity->token.string;
return scope_insert_with_name_no_mutex(s, name, entity);
}
gb_internal GB_COMPARE_PROC(entity_variable_pos_cmp) {
Entity *x = *cast(Entity **)a;
Entity *y = *cast(Entity **)b;
return token_pos_cmp(x->token.pos, y->token.pos);
}
gb_internal u64 check_vet_flags(CheckerContext *c) {
AstFile *file = c->file;
if (file == nullptr &&
c->curr_proc_decl &&
c->curr_proc_decl->proc_lit) {
file = c->curr_proc_decl->proc_lit->file();
}
return ast_file_vet_flags(file);
}
gb_internal u64 check_vet_flags(Ast *node) {
AstFile *file = node->file();
return ast_file_vet_flags(file);
}
gb_internal u64 check_feature_flags(CheckerContext *c, Ast *node) {
AstFile *file = c->file;
if (file == nullptr &&
c->curr_proc_decl &&
c->curr_proc_decl->proc_lit) {
file = c->curr_proc_decl->proc_lit->file();
}
if (file == nullptr) {
file = node->file();
}
if (file != nullptr && file->feature_flags_set) {
return file->feature_flags;
}
return 0;
}
gb_internal u64 check_feature_flags(Entity *e) {
if (e == nullptr) {
return 0;
}
AstFile *file = nullptr;
if (e->file == nullptr) {
file = e->file;
}
if (file == nullptr) {
if (e->decl_info && e->decl_info->decl_node) {
file = e->decl_info->decl_node->file();
}
}
if (file != nullptr && file->feature_flags_set) {
return file->feature_flags;
}
return 0;
}
enum VettedEntityKind {
VettedEntity_Invalid,
VettedEntity_Unused,
VettedEntity_Shadowed,
VettedEntity_Shadowed_And_Unused,
};
struct VettedEntity {
VettedEntityKind kind;
Entity *entity;
Entity *other;
};
gb_internal GB_COMPARE_PROC(vetted_entity_variable_pos_cmp) {
Entity *x = (cast(VettedEntity *)a)->entity;
Entity *y = (cast(VettedEntity *)b)->entity;
GB_ASSERT(x != nullptr);
GB_ASSERT(y != nullptr);
return token_pos_cmp(x->token.pos, y->token.pos);
}
gb_internal bool check_vet_shadowing_assignment(Checker *c, Entity *shadowed, Ast *expr) {
Ast *init = unparen_expr(expr);
if (init == nullptr) {
return false;
}
if (init->kind == Ast_Ident) {
// TODO(bill): Which logic is better? Same name or same entity
// bool ignore = init->Ident.token.string == name;
bool ignore = init->Ident.entity == shadowed;
if (ignore) {
return true;
}
} else if (init->kind == Ast_TernaryIfExpr) {
bool x = check_vet_shadowing_assignment(c, shadowed, init->TernaryIfExpr.x);
bool y = check_vet_shadowing_assignment(c, shadowed, init->TernaryIfExpr.y);
if (x || y) {
return true;
}
}
return false;
}
gb_internal bool check_vet_shadowing(Checker *c, Entity *e, VettedEntity *ve) {
if (e->kind != Entity_Variable) {
return false;
}
String name = e->token.string;
if (name == "_") {
return false;
}
if (e->flags & EntityFlag_Param) {
return false;
}
if (e->scope->flags & (ScopeFlag_Global|ScopeFlag_File|ScopeFlag_Proc)) {
return false;
}
Scope *parent = e->scope->parent;
if (parent->flags & (ScopeFlag_Global|ScopeFlag_File)) {
return false;
}
Entity *shadowed = scope_lookup(parent, name);
if (shadowed == nullptr) {
return false;
}
if (shadowed->kind != Entity_Variable) {
return false;
}
if (shadowed->scope->flags & (ScopeFlag_Global|ScopeFlag_File)) {
// return false;
}
// NOTE(bill): The entities must be in the same file
if (e->token.pos.file_id != shadowed->token.pos.file_id) {
return false;
}
// NOTE(bill): The shaded identifier must appear before this one to be an
// instance of shadowing
if (token_pos_cmp(shadowed->token.pos, e->token.pos) > 0) {
return false;
}
// NOTE(bill): If the types differ, don't complain
if (!are_types_identical(e->type, shadowed->type)) {
return false;
}
// NOTE(bill): Ignore intentional redeclaration
// x := x
// Suggested in issue #637 (2020-05-11)
// Also allow the following
// x := x if cond else y
// x := z if cond else x
if ((e->flags & EntityFlag_Using) == 0 && e->kind == Entity_Variable) {
if (check_vet_shadowing_assignment(c, shadowed, e->Variable.init_expr)) {
return false;
}
}
gb_zero_item(ve);
ve->kind = VettedEntity_Shadowed;
ve->entity = e;
ve->other = shadowed;
return true;
}
gb_internal bool check_vet_unused(Checker *c, Entity *e, VettedEntity *ve) {
if ((e->flags&EntityFlag_Used) == 0) {
switch (e->kind) {
case Entity_Variable:
if (e->scope->flags & (ScopeFlag_Global|ScopeFlag_Type|ScopeFlag_File)) {
return false;
} else if (e->flags & EntityFlag_Static) {
// ignore these for the time being
return false;
}
case Entity_ImportName:
case Entity_LibraryName:
gb_zero_item(ve);
ve->kind = VettedEntity_Unused;
ve->entity = e;
return true;
}
}
return false;
}
gb_internal void check_scope_usage_internal(Checker *c, Scope *scope, u64 vet_flags, bool per_entity) {
u64 original_vet_flags = vet_flags;
TEMPORARY_ALLOCATOR_GUARD();
Array<VettedEntity> vetted_entities = {};
array_init(&vetted_entities, temporary_allocator());
rw_mutex_shared_lock(&scope->mutex);
for (auto const &entry : scope->elements) {
Entity *e = entry.value;
if (e == nullptr) continue;
vet_flags = original_vet_flags;
if (per_entity) {
vet_flags = ast_file_vet_flags(e->file);
}
bool vet_unused = (vet_flags & VetFlag_Unused) != 0;
bool vet_shadowing = (vet_flags & (VetFlag_Shadowing|VetFlag_Using)) != 0;
bool vet_unused_procedures = (vet_flags & VetFlag_UnusedProcedures) != 0;
if (vet_unused_procedures && e->pkg && e->pkg->kind == Package_Runtime) {
vet_unused_procedures = false;
}
VettedEntity ve_unused = {};
VettedEntity ve_shadowed = {};
bool is_unused = false;
if (vet_unused && check_vet_unused(c, e, &ve_unused)) {
is_unused = true;
} else if (vet_unused_procedures && e->kind == Entity_Procedure) {
if (e->flags&EntityFlag_Used) {
is_unused = false;
} else if (e->flags & EntityFlag_Require) {
is_unused = false;
} else if (e->flags & EntityFlag_Init) {
is_unused = false;
} else if (e->flags & EntityFlag_Fini) {
is_unused = false;
} else if (e->Procedure.is_export) {
is_unused = false;
} else if (e->pkg && e->pkg->kind == Package_Init && e->token.string == "main") {
is_unused = false;
} else {
is_unused = true;
ve_unused.kind = VettedEntity_Unused;
ve_unused.entity = e;
}
}
bool is_shadowed = vet_shadowing && check_vet_shadowing(c, e, &ve_shadowed);
if (is_unused && is_shadowed) {
VettedEntity ve_both = ve_shadowed;
ve_both.kind = VettedEntity_Shadowed_And_Unused;
array_add(&vetted_entities, ve_both);
} else if (is_unused) {
array_add(&vetted_entities, ve_unused);
} else if (is_shadowed) {
array_add(&vetted_entities, ve_shadowed);
} else if (e->kind == Entity_Variable && (e->flags & (EntityFlag_Param|EntityFlag_Using|EntityFlag_Static|EntityFlag_Field)) == 0 && !e->Variable.is_global) {
i64 sz = type_size_of(e->type);
// TODO(bill): When is a good size warn?
// Is >256 KiB good enough?
if (sz > 1ll<<18) {
bool is_ref = false;
if((e->flags & EntityFlag_ForValue) != 0) {
is_ref = type_deref(e->Variable.for_loop_parent_type) != NULL;
}
if(!is_ref) {
gbString type_str = type_to_string(e->type);
warning(e->token, "Declaration of '%.*s' may cause a stack overflow due to its type '%s' having a size of %lld bytes", LIT(e->token.string), type_str, cast(long long)sz);
gb_string_free(type_str);
}
}
}
}
rw_mutex_shared_unlock(&scope->mutex);
array_sort(vetted_entities, vetted_entity_variable_pos_cmp);
for (auto const &ve : vetted_entities) {
Entity *e = ve.entity;
Entity *other = ve.other;
String name = e->token.string;
vet_flags = original_vet_flags;
if (per_entity) {
vet_flags = ast_file_vet_flags(e->file);
}
if (ve.kind == VettedEntity_Shadowed_And_Unused) {
error(e->token, "'%.*s' declared but not used, possibly shadows declaration at line %d", LIT(name), other->token.pos.line);
} else if (vet_flags) {
switch (ve.kind) {
case VettedEntity_Unused:
if (e->kind == Entity_Variable && (vet_flags & VetFlag_UnusedVariables) != 0) {
error(e->token, "'%.*s' declared but not used", LIT(name));
}
if (e->kind == Entity_Procedure && (vet_flags & VetFlag_UnusedProcedures) != 0) {
error(e->token, "'%.*s' declared but not used", LIT(name));
}
if ((e->kind == Entity_ImportName || e->kind == Entity_LibraryName) && (vet_flags & VetFlag_UnusedImports) != 0) {
error(e->token, "'%.*s' declared but not used", LIT(name));
}
break;
case VettedEntity_Shadowed:
if ((vet_flags & (VetFlag_Shadowing|VetFlag_Using)) != 0 && e->flags&EntityFlag_Using) {
error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %d", LIT(name), other->token.pos.line);
} else if ((vet_flags & (VetFlag_Shadowing)) != 0) {
error(e->token, "Declaration of '%.*s' shadows declaration at line %d", LIT(name), other->token.pos.line);
}
break;
default:
break;
}
}
}
}
gb_internal void check_scope_usage(Checker *c, Scope *scope, u64 vet_flags) {
check_scope_usage_internal(c, scope, vet_flags, false);
for (Scope *child = scope->head_child; child != nullptr; child = child->next) {
if (child->flags & (ScopeFlag_Proc|ScopeFlag_Type|ScopeFlag_File)) {
// Ignore these
} else {
check_scope_usage(c, child, vet_flags);
}
}
}
gb_internal void add_dependency(CheckerInfo *info, DeclInfo *d, Entity *e) {
if (in_single_threaded_checker_stage.load(std::memory_order_relaxed)) {
ptr_set_add(&d->deps, e);
} else {
rw_mutex_lock(&d->deps_mutex);
ptr_set_add(&d->deps, e);
rw_mutex_unlock(&d->deps_mutex);
}
}
gb_internal void add_type_info_dependency(CheckerInfo *info, DeclInfo *d, Type *type) {
if (d == nullptr || type == nullptr) {
return;
}
if (type->kind == Type_Named) {
Entity *e = type->Named.type_name;
if (e->TypeName.is_type_alias) {
type = type->Named.base;
}
}
rw_mutex_lock(&d->type_info_deps_mutex);
type_set_add(&d->type_info_deps, type);
rw_mutex_unlock(&d->type_info_deps_mutex);
}
gb_internal AstPackage *get_runtime_package(CheckerInfo *info) {
String name = str_lit("runtime");
gbAllocator a = heap_allocator();
String path = get_fullpath_base_collection(a, name, nullptr);
defer (gb_free(a, path.text));
auto found = string_map_get(&info->packages, path);
if (found == nullptr) {
gb_printf_err("Name: %.*s\n", LIT(name));
gb_printf_err("Fullpath: %.*s\n", LIT(path));
for (auto const &entry : info->packages) {
gb_printf_err("%.*s\n", LIT(entry.key));
}
GB_ASSERT_MSG(found != nullptr, "Missing core package %.*s", LIT(name));
}
return *found;
}
gb_internal AstPackage *get_core_package(CheckerInfo *info, String name) {
if (name == "runtime") {
return get_runtime_package(info);
}
gbAllocator a = heap_allocator();
String path = get_fullpath_core_collection(a, name, nullptr);
defer (gb_free(a, path.text));
auto found = string_map_get(&info->packages, path);
if (found == nullptr) {
gb_printf_err("Name: %.*s\n", LIT(name));
gb_printf_err("Fullpath: %.*s\n", LIT(path));
for (auto const &entry : info->packages) {
gb_printf_err("%.*s\n", LIT(entry.key));
}
GB_ASSERT_MSG(found != nullptr, "Missing core package %.*s", LIT(name));
}
return *found;
}
gb_internal void add_package_dependency(CheckerContext *c, char const *package_name, char const *name, bool required=false) {
String n = make_string_c(name);
AstPackage *p = get_core_package(&c->checker->info, make_string_c(package_name));
Entity *e = scope_lookup(p->scope, n);
GB_ASSERT_MSG(e != nullptr, "%s", name);
GB_ASSERT(c->decl != nullptr);
e->flags |= EntityFlag_Used;
if (required) {
e->flags |= EntityFlag_Require;
}
add_dependency(c->info, c->decl, e);
}
gb_internal void try_to_add_package_dependency(CheckerContext *c, char const *package_name, char const *name) {
String n = make_string_c(name);
AstPackage *p = get_core_package(&c->checker->info, make_string_c(package_name));
Entity *e = scope_lookup(p->scope, n);
if (e == nullptr) {
return;
}
GB_ASSERT(c->decl != nullptr);
e->flags |= EntityFlag_Used;
add_dependency(c->info, c->decl, e);
}
gb_internal void add_declaration_dependency(CheckerContext *c, Entity *e) {
if (e == nullptr) {
return;
}
if (e->flags & EntityFlag_Disabled) {
// ignore the dependencies if it has been `@(disabled=true)`
return;
}
if (c->decl != nullptr) {
add_dependency(c->info, c->decl, e);
}
}
gb_internal Entity *add_global_entity(Entity *entity, Scope *scope=builtin_pkg->scope) {
String name = entity->token.string;
defer (entity->state = EntityState_Resolved);
if (gb_memchr(name.text, ' ', name.len)) {
return entity; // NOTE(bill): Usually an 'untyped thing'
}
if (scope_insert(scope, entity)) {
compiler_error("double declaration");
}
return entity;
}
gb_internal void add_global_constant(char const *name, Type *type, ExactValue value) {
Entity *entity = alloc_entity(Entity_Constant, nullptr, make_token_ident(name), type);
entity->Constant.value = value;
add_global_entity(entity);
}
gb_internal void add_global_string_constant(char const *name, String const &value) {
add_global_constant(name, t_untyped_string, exact_value_string(value));
}
gb_internal void add_global_bool_constant(char const *name, bool value) {
add_global_constant(name, t_untyped_bool, exact_value_bool(value));
}
gb_internal void add_global_type_entity(String name, Type *type) {
add_global_entity(alloc_entity_type_name(nullptr, make_token_ident(name), type));
}
gb_internal AstPackage *create_builtin_package(char const *name) {
gbAllocator a = permanent_allocator();