Repository navigation
Expand file tree
/
Copy pathcheck_decl.cpp
More file actions
2236 lines (1918 loc) · 69.4 KB
/
Copy pathcheck_decl.cpp
File metadata and controls
2236 lines (1918 loc) · 69.4 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
gb_internal void check_stmt(CheckerContext *ctx, Ast *node, u32 flags);
// NOTE(bill): 'content_name' is for debugging and error messages
gb_internal Type *check_init_variable(CheckerContext *ctx, Entity *e, Operand *operand, String context_name) {
if (operand->mode == Addressing_Invalid ||
operand->type == t_invalid ||
e->type == t_invalid) {
if (operand->mode == Addressing_Builtin) {
ERROR_BLOCK();
gbString expr_str = expr_to_string(operand->expr);
error(operand->expr,
"Cannot assign built-in procedure '%s' in %.*s",
expr_str,
LIT(context_name));
error_line("\tBuilt-in procedures are implemented by the compiler and might not be actually instantiated procedure\n");
operand->mode = Addressing_Invalid;
gb_string_free(expr_str);
}
if (operand->mode == Addressing_ProcGroup) {
if (e->type == nullptr) {
error(operand->expr, "Cannot determine type from overloaded procedure '%.*s'", LIT(operand->proc_group->token.string));
} else {
check_assignment(ctx, operand, e->type, str_lit("variable assignment"));
if (operand->mode != Addressing_Type) {
return operand->type;
}
}
}
if (e->type == nullptr) {
e->type = t_invalid;
}
return nullptr;
}
if (e->kind == Entity_Variable) {
e->Variable.init_expr = operand->expr;
}
if (operand->mode == Addressing_Type) {
if (e->type != nullptr && is_type_typeid(e->type) && !is_type_polymorphic(operand->type)) {
add_type_info_type(ctx, operand->type);
add_type_and_value(ctx, operand->expr, Addressing_Value, e->type, exact_value_typeid(operand->type));
return e->type;
} else {
ERROR_BLOCK();
gbString t = type_to_string(operand->type);
defer (gb_string_free(t));
if (is_type_polymorphic(operand->type)) {
error(operand->expr, "Cannot assign a non-specialized polymorphic type '%s' to variable '%.*s'", t, LIT(e->token.string));
} else {
error(operand->expr, "Cannot assign a type '%s' to variable '%.*s'", t, LIT(e->token.string));
}
if (e->type == nullptr) {
error_line("\tThe type of the variable '%.*s' cannot be inferred as a type and does not have a default type\n", LIT(e->token.string));
}
e->type = operand->type;
return nullptr;
}
}
if (e->type == nullptr) {
// NOTE(bill): Use the type of the operand
Type *t = operand->type;
if (is_type_untyped(t)) {
if (is_type_untyped_uninit(t)) {
error(e->token, "Invalid use of --- in %.*s", LIT(context_name));
e->type = t_invalid;
return nullptr;
} else if (t == t_invalid || is_type_untyped_nil(t)) {
error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name));
e->type = t_invalid;
return nullptr;
}
t = default_type(t);
}
if (is_type_asm_proc(t)) {
error(e->token, "Invalid use of inline asm in %.*s", LIT(context_name));
e->type = t_invalid;
return nullptr;
} else if (is_type_polymorphic(t)) {
Entity *e2 = entity_of_node(operand->expr);
if (e2 == nullptr) {
e->type = t_invalid;
return nullptr;
}
if (e2->state.load() != EntityState_Resolved) {
e->type = t;
return nullptr;
}
gbString str = type_to_string(t);
defer (gb_string_free(str));
error(operand->expr, "Invalid use of a non-specialized polymorphic type '%s' in %.*s", str, LIT(context_name));
e->type = t_invalid;
return nullptr;
} else if (is_type_empty_union(t)) {
gbString str = type_to_string(t);
defer (gb_string_free(str));
error(e->token, "An empty union '%s' cannot be instantiated in %.*s", str, LIT(context_name));
e->type = t_invalid;
return nullptr;
}
GB_ASSERT(is_type_typed(t));
e->type = t;
}
e->parent_proc_decl = ctx->curr_proc_decl;
check_assignment(ctx, operand, e->type, context_name);
if (operand->mode == Addressing_Invalid) {
return nullptr;
}
return e->type;
}
gb_internal void check_init_variables(CheckerContext *ctx, Entity **lhs, isize lhs_count, Slice<Ast *> const &inits, String context_name) {
if ((lhs == nullptr || lhs_count == 0) && inits.count == 0) {
return;
}
// NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
// an extra allocation
TEMPORARY_ALLOCATOR_GUARD();
auto operands = array_make<Operand>(temporary_allocator(), 0, 2*lhs_count);
check_unpack_arguments(ctx, lhs, lhs_count, &operands, inits, UnpackFlag_AllowOk|UnpackFlag_AllowUndef);
isize rhs_count = operands.count;
isize max = gb_min(lhs_count, rhs_count);
for (isize i = 0; i < max; i++) {
Entity *e = lhs[i];
DeclInfo *d = decl_info_of_entity(e);
Operand *o = &operands[i];
check_init_variable(ctx, e, o, context_name);
if (d != nullptr) {
d->init_expr = o->expr;
}
}
if (rhs_count > 0 && lhs_count != rhs_count) {
error(lhs[0]->token, "Assignment count mismatch '%td' = '%td'", lhs_count, rhs_count);
}
}
gb_internal void override_entity_in_scope(Entity *original_entity, Entity *new_entity) {
// NOTE(bill): The original_entity's scope may not be same scope that it was inserted into
// e.g. file entity inserted into its package scope
String original_name = original_entity->token.string;
Scope *found_scope = nullptr;
Entity *found_entity = nullptr;
scope_lookup_parent(original_entity->scope, original_name, &found_scope, &found_entity);
if (found_scope == nullptr) {
return;
}
// IMPORTANT NOTE(bill, 2021-04-10): Overriding behaviour was flawed in that the
// original entity was still used check checked, but the checking was only
// relying on "constant" data such as the Entity.type and Entity.Constant.value
//
// Therefore two things can be done: the type can be assigned to state that it
// has been "evaluated" and the variant data can be copied across
rw_mutex_lock(&found_scope->mutex);
string_map_set(&found_scope->elements, original_name, new_entity);
rw_mutex_unlock(&found_scope->mutex);
original_entity->flags |= EntityFlag_Overridden;
original_entity->type = new_entity->type;
original_entity->kind = new_entity->kind;
original_entity->decl_info = new_entity->decl_info;
original_entity->aliased_of = new_entity;
original_entity->identifier.store(new_entity->identifier);
if (original_entity->identifier.load() != nullptr &&
original_entity->identifier.load()->kind == Ast_Ident) {
original_entity->identifier.load()->Ident.entity = new_entity;
}
// IMPORTANT NOTE(bill, 2021-04-10): copy only the variants
// This is most likely NEVER required, but it does not at all hurt to keep
isize offset = cast(u8 *)&original_entity->Dummy.start - cast(u8 *)original_entity;
isize size = gb_size_of(*original_entity) - offset;
gb_memmove(cast(u8 *)original_entity + offset, cast(u8 *)new_entity + offset, size);
}
gb_internal bool check_override_as_type_due_to_aliasing(CheckerContext *ctx, Entity *e, Entity *entity, Ast *init, Type *named_type) {
if (entity != nullptr && entity->kind == Entity_TypeName) {
// @TypeAliasingProblem
// NOTE(bill, 2022-02-03): This is used to solve the problem caused by type aliases
// being "confused" as constants
//
// A :: B
// C :: proc "c" (^A)
// B :: struct {x: C}
//
// A gets evaluated first, and then checks B.
// B then checks C.
// C then tries to check A which is unresolved but thought to be a constant.
// Therefore within C's check, A errs as "not a type".
//
// This is because a const declaration may or may not be a type and this cannot
// be determined from a syntactical standpoint.
// This check allows the compiler to override the entity to be checked as a type.
//
// There is no problem if B is prefixed with the `#type` helper enforcing at
// both a syntax and semantic level that B must be a type.
//
// A :: #type B
//
// This approach is not fool proof and can fail in case such as:
//
// X :: type_of(x)
// X :: Foo(int).Type
//
// Since even these kind of declarations may cause weird checking cycles.
// For the time being, these are going to be treated as an unfortunate error
// until there is a proper delaying system to try declaration again if they
// have failed.
if (e->type != nullptr && is_type_typed(e->type)) {
return false;
}
e->kind = Entity_TypeName;
check_type_decl(ctx, e, init, named_type);
return true;
}
return false;
}
gb_internal void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d);
gb_internal bool check_try_override_const_decl(CheckerContext *ctx, Entity *e, Entity *entity, Ast *init, Type *named_type) {
if (entity == nullptr) {
retry_proc_lit:;
init = unparen_expr(init);
if (init == nullptr) {
return false;
}
if (init->kind == Ast_TernaryWhenExpr) {
ast_node(we, TernaryWhenExpr, init);
if (we->cond == nullptr) {
return false;
}
if (we->cond->tav.value.kind != ExactValue_Bool) {
return false;
}
init = we->cond->tav.value.value_bool ? we->x : we->y;
goto retry_proc_lit;
} if (init->kind == Ast_ProcLit) {
// NOTE(bill, 2024-07-04): Override as a procedure entity because this could be within a `when` statement
e->kind = Entity_Procedure;
e->type = nullptr;
DeclInfo *d = decl_info_of_entity(e);
d->proc_lit = init;
check_proc_decl(ctx, e, d);
return true;
}
return false;
}
switch (entity->kind) {
case Entity_TypeName:
if (check_override_as_type_due_to_aliasing(ctx, e, entity, init, named_type)) {
return true;
}
break;
case Entity_Builtin:
if (e->type != nullptr) {
return false;
}
e->kind = Entity_Builtin;
e->Builtin.id = entity->Builtin.id;
e->type = t_invalid;
return true;
}
if (e->type != nullptr && entity->type != nullptr) {
Operand x = {};
x.type = entity->type;
x.mode = Addressing_Variable;
if (!check_is_assignable_to(ctx, &x, e->type)) {
return false;
}
}
// NOTE(bill): Override aliased entity
switch (entity->kind) {
case Entity_ProcGroup:
case Entity_Procedure:
override_entity_in_scope(e, entity);
return true;
}
return false;
}
gb_internal void check_init_constant(CheckerContext *ctx, Entity *e, Operand *operand) {
if (operand->mode == Addressing_Invalid ||
operand->type == t_invalid ||
e->type == t_invalid) {
if (e->type == nullptr) {
e->type = t_invalid;
}
return;
}
if (operand->mode != Addressing_Constant) {
Entity *entity = entity_of_node(operand->expr);
if (check_try_override_const_decl(ctx, e, entity, operand->expr, nullptr)) {
return;
}
}
if (operand->mode != Addressing_Constant) {
gbString str = expr_to_string(operand->expr);
error(operand->expr, "'%s' is not a compile-time known constant", str);
gb_string_free(str);
if (e->type == nullptr) {
e->type = t_invalid;
}
return;
}
if (e->type == nullptr) { // NOTE(bill): type inference
e->type = operand->type;
}
check_assignment(ctx, operand, e->type, str_lit("constant declaration"));
if (operand->mode == Addressing_Invalid) {
return;
}
if (is_type_proc(e->type)) {
error(e->token, "Illegal declaration of a constant procedure value");
}
e->parent_proc_decl = ctx->curr_proc_decl;
e->Constant.value = operand->value;
}
gb_internal bool is_type_distinct(Ast *node) {
for (;;) {
if (node == nullptr) {
return false;
}
if (node->kind == Ast_ParenExpr) {
node = node->ParenExpr.expr;
} else if (node->kind == Ast_HelperType) {
node = node->HelperType.type;
} else {
break;
}
}
switch (node->kind) {
case Ast_DistinctType:
return true;
case Ast_StructType:
case Ast_UnionType:
case Ast_EnumType:
case Ast_ProcType:
case Ast_BitFieldType:
return true;
case Ast_PointerType:
case Ast_ArrayType:
case Ast_DynamicArrayType:
case Ast_MapType:
return false;
}
return false;
}
gb_internal Ast *remove_type_alias_clutter(Ast *node) {
for (;;) {
if (node == nullptr) {
return nullptr;
}
if (node->kind == Ast_ParenExpr) {
node = node->ParenExpr.expr;
} else if (node->kind == Ast_DistinctType) {
node = node->DistinctType.type;
} else {
return node;
}
}
}
gb_internal Type *clone_enum_type(CheckerContext *ctx, Type *original_enum_type, Type *named_type) {
// NOTE(bill, 2022-02-05): Stupid edge case for `distinct` declarations
//
// X :: enum {A, B, C}
// Y :: distinct X
//
// To make Y be just like X, it will need to copy the elements of X and change their type
// so that they match Y rather than X.
GB_ASSERT(original_enum_type != nullptr);
GB_ASSERT(named_type != nullptr);
GB_ASSERT(original_enum_type->kind == Type_Enum);
GB_ASSERT(named_type->kind == Type_Named);
Scope *parent = original_enum_type->Enum.scope->parent;
Scope *scope = create_scope(nullptr, parent);
Type *et = alloc_type_enum();
et->Enum.base_type = original_enum_type->Enum.base_type;
et->Enum.min_value = original_enum_type->Enum.min_value;
et->Enum.max_value = original_enum_type->Enum.max_value;
et->Enum.min_value_index = original_enum_type->Enum.min_value_index;
et->Enum.max_value_index = original_enum_type->Enum.max_value_index;
et->Enum.scope = scope;
auto fields = array_make<Entity *>(permanent_allocator(), original_enum_type->Enum.fields.count);
for_array(i, fields) {
Entity *old = original_enum_type->Enum.fields[i];
Entity *e = alloc_entity_constant(scope, old->token, named_type, old->Constant.value);
e->file = old->file;
e->identifier = clone_ast(old->identifier);
e->flags |= EntityFlag_Visited;
e->state = EntityState_Resolved;
e->Constant.flags = old->Constant.flags;
e->Constant.docs = old->Constant.docs;
e->Constant.comment = old->Constant.comment;
fields[i] = e;
add_entity(ctx, scope, nullptr, e);
add_entity_use(ctx, e->identifier, e);
}
et->Enum.fields = fields;
return et;
}
gb_internal void check_type_decl(CheckerContext *ctx, Entity *e, Ast *init_expr, Type *def) {
GB_ASSERT(e->type == nullptr);
DeclInfo *decl = decl_info_of_entity(e);
bool is_distinct = is_type_distinct(init_expr);
Ast *te = remove_type_alias_clutter(init_expr);
e->type = t_invalid;
String name = e->token.string;
Type *named = alloc_type_named(name, nullptr, e);
if (def != nullptr && def->kind == Type_Named) {
def->Named.base = named;
}
e->type = named;
if (!is_distinct) {
e->TypeName.is_type_alias = true;
}
check_type_path_push(ctx, e);
Type *bt = check_type_expr(ctx, te, named);
check_type_path_pop(ctx);
Type *base = base_type(bt);
if (is_distinct && bt->kind == Type_Named && base->kind == Type_Enum) {
base = clone_enum_type(ctx, base, named);
}
named->Named.base = base;
if (is_distinct) {
if (is_type_typeid(e->type)) {
error(init_expr, "'distinct' cannot be applied to 'typeid'");
is_distinct = false;
} else if (is_type_any(e->type)) {
error(init_expr, "'distinct' cannot be applied to 'any'");
is_distinct = false;
} else if (is_type_simd_vector(e->type) || is_type_soa_pointer(e->type)) {
gbString str = type_to_string(e->type);
error(init_expr, "'distinct' cannot be applied to '%s'", str);
gb_string_free(str);
is_distinct = false;
}
} else {
if (is_type_typeid(e->type)) {
error(init_expr, "'typeid' cannot be aliased");
} else if (is_type_any(e->type)) {
error(init_expr, "'any' cannot be aliased");
}
}
if (!is_distinct) {
e->type = bt;
named->Named.base = bt;
}
e->TypeName.is_type_alias = !is_distinct;
if (decl->type_expr != nullptr) {
Type *t = check_type(ctx, decl->type_expr);
if (t != nullptr && !is_type_typeid(t)) {
Operand operand = {};
operand.mode = Addressing_Type;
operand.type = e->type;
operand.expr = init_expr;
check_assignment(ctx, &operand, t, str_lit("constant declaration"));
}
}
if (decl != nullptr) {
AttributeContext ac = {};
check_decl_attributes(ctx, decl->attributes, type_decl_attribute, &ac);
if (e->kind == Entity_TypeName && ac.objc_class != "") {
e->TypeName.objc_class_name = ac.objc_class;
if (ac.objc_is_implementation) {
e->TypeName.objc_is_implementation = ac.objc_is_implementation;
e->TypeName.objc_superclass = ac.objc_superclass;
e->TypeName.objc_ivar = ac.objc_ivar;
e->TypeName.objc_context_provider = ac.objc_context_provider;
mutex_lock(&ctx->info->objc_class_name_mutex);
bool class_exists = string_set_update(&ctx->info->obcj_class_name_set, ac.objc_class);
mutex_unlock(&ctx->info->objc_class_name_mutex);
if (class_exists) {
error(e->token, "@(objc_class) name '%.*s' has already been used elsewhere", LIT(ac.objc_class));
}
mpsc_enqueue(&ctx->info->objc_class_implementations, e);
GB_ASSERT(e->TypeName.objc_ivar == nullptr || e->TypeName.objc_ivar->kind == Type_Named);
// Enqueue the contex_provider proc to be checked after it is resolved
if (e->TypeName.objc_context_provider != nullptr) {
mpsc_enqueue(&ctx->checker->procs_with_objc_context_provider_to_check, e);
}
// TODO(harold): I think there's a Check elsewhere in the checker for checking cycles.
// See about moving this to the right location.
// Ensure superclass hierarchy are all Objective-C classes and does not cycle
// NOTE(harold): We check for superclass unconditionally (before checking if super is null)
// because this should be the case 99.99% of the time. Not subclassing something that
// is, or is the child of, NSObject means the objc runtime messaging will not properly work on this type.
TypeSet super_set{};
type_set_init(&super_set, 8);
defer (type_set_destroy(&super_set));
type_set_update(&super_set, e->type);
Type *super = ac.objc_superclass;
while (super != nullptr) {
if (super->kind != Type_Named) {
error(e->token, "@(objc_superclass) Referenced type must be a named struct");
break;
}
if (type_set_update(&super_set, super)) {
error(e->token, "@(objc_superclass) Superclass hierarchy cycle encountered");
break;
}
check_single_global_entity(ctx->checker, super->Named.type_name, super->Named.type_name->decl_info);
Type* named_type = base_named_type(super);
GB_ASSERT(named_type->kind == Type_Named);
if (!is_type_objc_object(named_type)) {
error(e->token, "@(objc_superclass) Superclass '%.*s' must be an Objective-C class", LIT(named_type->Named.name));
break;
}
if (named_type->Named.type_name->TypeName.objc_class_name == "") {
error(e->token, "@(objc_superclass) Superclass '%.*s' must have a valid @(objc_class) attribute", LIT(named_type->Named.name));
break;
}
super = named_type->Named.type_name->TypeName.objc_superclass;
}
} else {
if (ac.objc_ivar != nullptr) {
error(e->token, "@(objc_ivar) may only be applied when the @(obj_implement) attribute is also applied");
} else if (ac.objc_context_provider != nullptr) {
error(e->token, "@(objc_context_provider) may only be applied when the @(obj_implement) attribute is also applied");
}
}
if (type_size_of(e->type) > 0) {
error(e->token, "@(objc_class) marked type must be of zero size");
}
} else if (ac.objc_is_implementation) {
error(e->token, "@(objc_implement) may only be applied when the @(objc_class) attribute is also applied");
}
if (ac.raddbg_type_view) {
RaddbgTypeView type_view = {};
type_view.type = e->type;
type_view.view = ac.raddbg_type_view_string;
mpsc_enqueue(&ctx->info->raddbg_type_views_queue, type_view);
}
}
// using decl
if (decl->is_using) {
error(init_expr, "'using' an enum declaration is not allowed, prefer using implicit selector expressions e.g. '.A'");
}
}
gb_internal void check_const_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Ast *init, Type *named_type) {
GB_ASSERT(e->type == nullptr);
GB_ASSERT(e->kind == Entity_Constant);
init = unparen_expr(init);
if (e->flags & EntityFlag_Visited) {
e->type = t_invalid;
return;
}
e->flags |= EntityFlag_Visited;
if (type_expr) {
e->type = check_type(ctx, type_expr);
if (are_types_identical(e->type, t_typeid)) {
e->type = nullptr;
e->kind = Entity_TypeName;
check_type_decl(ctx, e, init, named_type);
return;
}
}
Operand operand = {};
if (init != nullptr) {
Entity *entity = check_entity_from_ident_or_selector(ctx, init, false);
if (check_override_as_type_due_to_aliasing(ctx, e, entity, init, named_type)) {
return;
}
entity = nullptr;
if (init->kind == Ast_Ident) {
entity = check_ident(ctx, &operand, init, nullptr, e->type, true);
} else if (init->kind == Ast_SelectorExpr) {
entity = check_selector(ctx, &operand, init, e->type);
} else {
check_expr_or_type(ctx, &operand, init, e->type);
if (init->kind == Ast_CallExpr) {
entity = init->CallExpr.entity_procedure_of;
}
}
switch (operand.mode) {
case Addressing_Type: {
if (e->type != nullptr && !is_type_typeid(e->type)) {
check_assignment(ctx, &operand, e->type, str_lit("constant declaration"));
}
e->kind = Entity_TypeName;
e->type = nullptr;
if (entity != nullptr && entity->type != nullptr &&
is_type_polymorphic_record_unspecialized(entity->type)) {
DeclInfo *decl = decl_info_of_entity(e);
if (decl != nullptr) {
if (decl->attributes.count > 0) {
error(decl->attributes[0], "Constant alias declarations cannot have attributes");
}
}
override_entity_in_scope(e, entity);
return;
}
check_type_decl(ctx, e, ctx->decl->init_expr, named_type);
return;
}
// NOTE(bill): Check to see if the expression it to be aliases
case Addressing_Builtin:
if (e->type != nullptr) {
error(type_expr, "A constant alias of a built-in procedure may not have a type initializer");
}
e->kind = Entity_Builtin;
e->Builtin.id = operand.builtin_id;
e->type = t_invalid;
return;
case Addressing_ProcGroup:
GB_ASSERT(operand.proc_group != nullptr);
GB_ASSERT(operand.proc_group->kind == Entity_ProcGroup);
// NOTE(bill, 2020-06-10): It is better to just clone the contents than overriding the entity in the scope
// Thank goodness I made entities a tagged union to allow for this implace patching
e->kind = Entity_ProcGroup;
e->ProcGroup.entities = array_clone(heap_allocator(), operand.proc_group->ProcGroup.entities);
return;
}
if (entity != nullptr) {
if (e->type != nullptr) {
Operand x = {};
x.type = entity->type;
x.mode = Addressing_Variable;
if (entity->kind == Entity_Constant) {
x.mode = Addressing_Constant;
x.value = entity->Constant.value;
}
if (!check_is_assignable_to(ctx, &x, e->type)) {
gbString expr_str = expr_to_string(init);
gbString op_type_str = type_to_string(entity->type);
gbString type_str = type_to_string(e->type);
error(e->token,
"Cannot assign '%s' of type '%s' to '%s'",
expr_str,
op_type_str,
type_str);
gb_string_free(type_str);
gb_string_free(op_type_str);
gb_string_free(expr_str);
}
}
// NOTE(bill): Override aliased entity
switch (entity->kind) {
case Entity_ProcGroup:
case Entity_Procedure:
case Entity_LibraryName:
case Entity_ImportName:
{
DeclInfo *decl = decl_info_of_entity(e);
if (decl != nullptr) {
if (decl->attributes.count > 0) {
error(decl->attributes[0], "Constant alias declarations cannot have attributes");
}
}
override_entity_in_scope(e, entity);
return;
}
}
}
}
check_init_constant(ctx, e, &operand);
if (operand.mode == Addressing_Invalid ||
base_type(operand.type) == t_invalid) {
gbString str = expr_to_string(init);
error(init, "Invalid declaration value '%s'", str);
gb_string_free(str);
}
DeclInfo *decl = decl_info_of_entity(e);
if (decl != nullptr) {
check_decl_attributes(ctx, decl->attributes, const_decl_attribute, nullptr);
}
}
typedef bool TypeCheckSig(Type *t);
gb_internal bool sig_compare(TypeCheckSig *a, Type *x, Type *y) {
x = core_type(x);
y = core_type(y);
return (a(x) && a(y));
}
gb_internal bool sig_compare(TypeCheckSig *a, TypeCheckSig *b, Type *x, Type *y) {
x = core_type(x);
y = core_type(y);
if (a == b) {
return sig_compare(a, x, y);
}
return ((a(x) && b(y)) || (b(x) && a(y)));
}
gb_internal bool signature_parameter_similar_enough(Type *x, Type *y) {
if (is_type_bit_set(x)) {
x = bit_set_to_int(x);
}
if (is_type_bit_set(y)) {
y = bit_set_to_int(y);
}
if (sig_compare(is_type_pointer, x, y)) {
return true;
}
if (sig_compare(is_type_multi_pointer, x, y)) {
return true;
}
if (sig_compare(is_type_proc, x, y)) {
return true;
}
if (sig_compare(is_type_integer, x, y)) {
GB_ASSERT(core_type(x)->kind == Type_Basic);
GB_ASSERT(core_type(y)->kind == Type_Basic);
i64 sx = type_size_of(x);
i64 sy = type_size_of(y);
if (sx == sy) return true;
}
if (sig_compare(is_type_integer, is_type_boolean, x, y)) {
GB_ASSERT(core_type(x)->kind == Type_Basic);
GB_ASSERT(core_type(y)->kind == Type_Basic);
i64 sx = type_size_of(x);
i64 sy = type_size_of(y);
if (sx == sy) return true;
}
if (sig_compare(is_type_cstring, is_type_u8_ptr, x, y)) {
return true;
}
if (sig_compare(is_type_cstring, is_type_u8_multi_ptr, x, y)) {
return true;
}
if (sig_compare(is_type_cstring16, is_type_u16_ptr, x, y)) {
return true;
}
if (sig_compare(is_type_cstring16, is_type_u16_multi_ptr, x, y)) {
return true;
}
if (sig_compare(is_type_uintptr, is_type_rawptr, x, y)) {
return true;
}
if (sig_compare(is_type_proc, is_type_pointer, x, y)) {
return true;
}
if (sig_compare(is_type_pointer, is_type_multi_pointer, x, y)) {
return true;
}
if (sig_compare(is_type_proc, is_type_multi_pointer, x, y)) {
return true;
}
if (sig_compare(is_type_slice, x, y)) {
Type *s1 = core_type(x);
Type *s2 = core_type(y);
if (signature_parameter_similar_enough(s1->Slice.elem, s2->Slice.elem)) {
return true;
}
}
Type *x_base = base_type(x);
Type *y_base = base_type(y);
if (x_base == y_base) {
return true;
}
if (x_base->kind == y_base->kind &&
x_base->kind == Type_Struct) {
i64 xs = type_size_of(x_base);
i64 ys = type_size_of(y_base);
i64 xa = type_align_of(x_base);
i64 ya = type_align_of(y_base);
if (x_base->Struct.is_raw_union == y_base->Struct.is_raw_union &&
xs == ys && xa == ya) {
if (xs > 16) {
// @@ABI NOTE(bill): Just allow anything over 16-bytes to be allowed, because on all current ABIs
// it will be passed by point
// NOTE(bill): this must be changed when ABI changes
return true;
}
if (x_base->Struct.is_raw_union) {
return true;
}
if (x->Struct.fields.count == y->Struct.fields.count) {
for (isize i = 0; i < x->Struct.fields.count; i++) {
Entity *a = x->Struct.fields[i];
Entity *b = y->Struct.fields[i];
bool similar = signature_parameter_similar_enough(a->type, b->type);
if (!similar) {
// NOTE(bill): If the fields are not similar enough, then stop.
goto end;
}
}
}
// HACK NOTE(bill): Allow this for the time begin until it actually becomes a practical problem
return true;
}
}
end:;
return are_types_identical(x, y);
}
gb_internal bool are_signatures_similar_enough(Type *a_, Type *b_) {
GB_ASSERT(a_->kind == Type_Proc);
GB_ASSERT(b_->kind == Type_Proc);
TypeProc *a = &a_->Proc;
TypeProc *b = &b_->Proc;
if (a->param_count != b->param_count) {
return false;
}
if (a->result_count != b->result_count) {
return false;
}
if (a->c_vararg != b->c_vararg) {
return false;
}
if (a->variadic != b->variadic) {
return false;
}
if (a->variadic && a->variadic_index != b->variadic_index) {
return false;
}
for (isize i = 0; i < a->param_count; i++) {
Type *x = core_type(a->params->Tuple.variables[i]->type);
Type *y = core_type(b->params->Tuple.variables[i]->type);
if (x->kind == Type_BitSet && x->BitSet.underlying) {
x = core_type(x->BitSet.underlying);
}
if (y->kind == Type_BitSet && y->BitSet.underlying) {
y = core_type(y->BitSet.underlying);
}
// Allow a `#c_vararg args: ..any` with `#c_vararg args: ..foo`.
if (a->variadic && i == a->variadic_index) {
GB_ASSERT(x->kind == Type_Slice);
GB_ASSERT(y->kind == Type_Slice);
Type *x_elem = core_type(x->Slice.elem);
Type *y_elem = core_type(y->Slice.elem);
if (is_type_any(x_elem) || is_type_any(y_elem)) {
continue;
}
}
if (!signature_parameter_similar_enough(x, y)) {
return false;
}
}
for (isize i = 0; i < a->result_count; i++) {
Type *x = core_type(a->results->Tuple.variables[i]->type);
Type *y = core_type(b->results->Tuple.variables[i]->type);
if (x->kind == Type_BitSet && x->BitSet.underlying) {
x = core_type(x->BitSet.underlying);
}
if (y->kind == Type_BitSet && y->BitSet.underlying) {
y = core_type(y->BitSet.underlying);
}
if (!signature_parameter_similar_enough(x, y)) {
return false;
}
}
return true;
}
gb_internal Entity *init_entity_foreign_library(CheckerContext *ctx, Entity *e) {
Ast *ident = nullptr;
Entity **foreign_library = nullptr;
switch (e->kind) {
case Entity_Procedure:
ident = e->Procedure.foreign_library_ident;
foreign_library = &e->Procedure.foreign_library;
break;
case Entity_Variable:
ident = e->Variable.foreign_library_ident;
foreign_library = &e->Variable.foreign_library;
break;
default:
return nullptr;
}
if (ident == nullptr) {
error(e->token, "foreign entities must declare which library they are from");
} else if (ident->kind != Ast_Ident) {
error(ident, "foreign library names must be an identifier");
} else {
String name = ident->Ident.token.string;
Entity *found = scope_lookup(ctx->scope, name, ident->Ident.hash);
if (found == nullptr) {
if (is_blank_ident(name)) {
// NOTE(bill): link against nothing
} else {
error(ident, "Undeclared name: %.*s", LIT(name));
}