Repository navigation
Expand file tree
/
Copy pathcheck_builtin.cpp
More file actions
7897 lines (6932 loc) · 242 KB
/
Copy pathcheck_builtin.cpp
File metadata and controls
7897 lines (6932 loc) · 242 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
typedef bool (BuiltinTypeIsProc)(Type *t);
gb_internal int enum_constant_entity_cmp(void const* a, void const* b) {
Entity const *ea = *(cast(Entity const **)a);
Entity const *eb = *(cast(Entity const **)b);
GB_ASSERT(ea->kind == Entity_Constant && eb->kind == Entity_Constant);
GB_ASSERT(ea->Constant.value.kind == ExactValue_Integer && eb->Constant.value.kind == ExactValue_Integer);
return big_int_cmp(&ea->Constant.value.value_integer, &eb->Constant.value.value_integer);
}
gb_global BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - BuiltinProc__type_simple_boolean_begin] = {
nullptr, // BuiltinProc__type_simple_boolean_begin
is_type_boolean,
is_type_integer,
is_type_rune,
is_type_float,
is_type_complex,
is_type_quaternion,
is_type_string,
is_type_string16,
is_type_cstring,
is_type_cstring16,
is_type_typeid,
is_type_any,
is_type_endian_platform,
is_type_endian_little,
is_type_endian_big,
is_type_unsigned,
is_type_numeric,
is_type_ordered,
is_type_ordered_numeric,
is_type_indexable,
is_type_sliceable,
is_type_comparable,
is_type_simple_compare,
is_type_nearly_simple_compare,
is_type_dereferenceable,
is_type_valid_for_keys,
is_type_valid_for_matrix_elems,
is_type_named,
is_type_pointer,
is_type_multi_pointer,
is_type_array,
is_type_enumerated_array,
is_type_slice,
is_type_dynamic_array,
is_type_map,
is_type_struct,
is_type_union,
is_type_enum,
is_type_proc,
is_type_bit_set,
is_type_bit_field,
is_type_simd_vector,
is_type_matrix,
is_type_raw_union,
is_type_polymorphic_record_specialized,
is_type_polymorphic_record_unspecialized,
type_has_nil,
};
gb_internal void check_or_else_right_type(CheckerContext *c, Ast *expr, String const &name, Type *right_type) {
if (right_type == nullptr) {
return;
}
if (!is_type_boolean(right_type) && !type_has_nil(right_type)) {
gbString str = type_to_string(right_type);
error(expr, "'%.*s' expects an \"optional ok\" like value, or an n-valued expression where the last value is either a boolean or can be compared against 'nil', got %s", LIT(name), str);
gb_string_free(str);
}
}
gb_internal void check_or_else_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
Type *left_type = nullptr;
Type *right_type = nullptr;
if (x->type->kind == Type_Tuple) {
auto const &vars = x->type->Tuple.variables;
auto lhs = slice(vars, 0, vars.count-1);
auto rhs = vars[vars.count-1];
if (lhs.count == 1) {
left_type = lhs[0]->type;
} else if (lhs.count != 0) {
left_type = alloc_type_tuple();
left_type->Tuple.variables = lhs;
}
right_type = rhs->type;
} else {
check_promote_optional_ok(c, x, &left_type, &right_type);
}
if (left_type_) *left_type_ = left_type;
if (right_type_) *right_type_ = right_type;
check_or_else_right_type(c, x->expr, name, right_type);
}
gb_internal void check_or_else_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint) {
ERROR_BLOCK();
gbString t = type_to_string(x.type);
error(x.expr, "'%.*s' does not return a value, value is of type %s", LIT(name), t);
if (is_type_union(type_deref(x.type))) {
Type *bsrc = base_type(type_deref(x.type));
gbString th = nullptr;
if (type_hint != nullptr) {
GB_ASSERT(bsrc->kind == Type_Union);
for (Type *vt : bsrc->Union.variants) {
if (are_types_identical(vt, type_hint)) {
th = type_to_string(type_hint);
break;
}
}
}
gbString expr_str = expr_to_string(x.expr);
if (th != nullptr) {
error_line("\tSuggestion: was a type assertion such as %s.(%s) or %s.? wanted?\n", expr_str, th, expr_str);
} else {
error_line("\tSuggestion: was a type assertion such as %s.(T) or %s.? wanted?\n", expr_str, expr_str);
}
gb_string_free(th);
gb_string_free(expr_str);
}
gb_string_free(t);
}
gb_internal void check_or_return_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_) {
Type *left_type = nullptr;
Type *right_type = nullptr;
if (x->type->kind == Type_Tuple) {
auto const &vars = x->type->Tuple.variables;
auto lhs = slice(vars, 0, vars.count-1);
auto rhs = vars[vars.count-1];
if (lhs.count == 1) {
left_type = lhs[0]->type;
} else if (lhs.count != 0) {
left_type = alloc_type_tuple();
left_type->Tuple.variables = lhs;
}
right_type = rhs->type;
} else {
check_promote_optional_ok(c, x, &left_type, &right_type);
}
if (left_type_) *left_type_ = left_type;
if (right_type_) *right_type_ = right_type;
check_or_else_right_type(c, x->expr, name, right_type);
}
gb_internal bool does_require_msgSend_stret(Type *return_type) {
if (return_type == nullptr) {
return false;
}
if (build_context.metrics.os != TargetOs_darwin) {
return false;
}
if (build_context.metrics.arch == TargetArch_i386 || build_context.metrics.arch == TargetArch_amd64) {
i64 struct_limit = type_size_of(t_uintptr) << 1;
return type_size_of(return_type) > struct_limit;
}
if (build_context.metrics.arch == TargetArch_arm64) {
return false;
}
// No objc here so this doesn't matter, right?
if (build_context.metrics.arch == TargetArch_riscv64) {
return false;
}
// if (build_context.metrics.arch == TargetArch_arm32) {
// i64 struct_limit = type_size_of(t_uintptr);
// // NOTE(bill): This is technically wrong
// return is_type_struct(return_type) && !is_type_raw_union(return_type) && type_size_of(return_type) > struct_limit;
// }
GB_PANIC("unsupported architecture");
return false;
}
gb_internal ObjcMsgKind get_objc_proc_kind(Type *return_type) {
if (return_type == nullptr) {
return ObjcMsg_normal;
}
if (build_context.metrics.arch == TargetArch_i386 || build_context.metrics.arch == TargetArch_amd64) {
if (is_type_float(return_type)) {
return ObjcMsg_fpret;
}
if (build_context.metrics.arch == TargetArch_amd64) {
if (is_type_complex(return_type)) {
// URL: https://github.com/opensource-apple/objc4/blob/cd5e62a5597ea7a31dccef089317abb3a661c154/runtime/message.h#L143-L159
return ObjcMsg_fpret;
}
}
}
if (build_context.metrics.arch != TargetArch_arm64) {
if (does_require_msgSend_stret(return_type)) {
return ObjcMsg_stret;
}
}
return ObjcMsg_normal;
}
void add_objc_proc_type(CheckerContext *c, Ast *call, Type *return_type, Slice<Type *> param_types) {
ObjcMsgKind kind = get_objc_proc_kind(return_type);
Scope *scope = create_scope(c->info, nullptr);
// NOTE(bill, 2022-02-08): the backend's ABI handling should handle this correctly, I hope
Type *params = alloc_type_tuple();
{
auto variables = array_make<Entity *>(permanent_allocator(), 0, param_types.count);
for (Type *type : param_types) {
Entity *param = alloc_entity_param(scope, blank_token, type, false, true);
array_add(&variables, param);
}
params->Tuple.variables = slice_from_array(variables);
}
Type *results = alloc_type_tuple();
if (return_type) {
auto variables = array_make<Entity *>(permanent_allocator(), 1);
results->Tuple.variables = slice_from_array(variables);
Entity *param = alloc_entity_param(scope, blank_token, return_type, false, true);
results->Tuple.variables[0] = param;
}
ObjcMsgData data = {};
data.kind = kind;
data.proc_type = alloc_type_proc(scope, params, param_types.count, results, results->Tuple.variables.count, false, ProcCC_CDecl);
mutex_lock(&c->info->objc_objc_msgSend_mutex);
map_set(&c->info->objc_msgSend_types, call, data);
mutex_unlock(&c->info->objc_objc_msgSend_mutex);
try_to_add_package_dependency(c, "runtime", "objc_msgSend");
try_to_add_package_dependency(c, "runtime", "objc_msgSend_fpret");
try_to_add_package_dependency(c, "runtime", "objc_msgSend_fp2ret");
try_to_add_package_dependency(c, "runtime", "objc_msgSend_stret");
Slice<Ast *> args = call->CallExpr.args;
if (args.count > 0 && args[0]->tav.objc_super_target) {
try_to_add_package_dependency(c, "runtime", "objc_msgSendSuper2");
try_to_add_package_dependency(c, "runtime", "objc_msgSendSuper2_stret");
}
}
gb_internal bool is_constant_string(CheckerContext *c, String const &builtin_name, Ast *expr, String *name_) {
Operand op = {};
check_expr(c, &op, expr);
if (op.mode == Addressing_Constant && op.value.kind == ExactValue_String) {
if (name_) *name_ = op.value.value_string;
return true;
}
gbString e = expr_to_string(op.expr);
gbString t = type_to_string(op.type);
error(op.expr, "'%.*s' expected a constant string value, got %s of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
gb_internal bool check_builtin_objc_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
String const &builtin_name = builtin_procs[id].name;
if (build_context.metrics.os != TargetOs_darwin) {
// allow on doc generation (e.g. Metal stuff)
if (build_context.command_kind != Command_doc && build_context.command_kind != Command_check) {
error(call, "'%.*s' only works on darwin", LIT(builtin_name));
}
}
ast_node(ce, CallExpr, call);
switch (id) {
default:
GB_PANIC("Implement objective built-in procedure: %.*s", LIT(builtin_name));
return false;
case BuiltinProc_objc_send: {
Type *return_type = nullptr;
Operand rt = {};
check_expr_or_type(c, &rt, ce->args[0]);
if (rt.mode == Addressing_Type) {
return_type = rt.type;
} else if (is_operand_nil(rt)) {
return_type = nullptr;
} else {
gbString e = expr_to_string(rt.expr);
error(rt.expr, "'%.*s' expected a type or nil to define the return type of the Objective-C call, got %s", LIT(builtin_name), e);
gb_string_free(e);
return false;
}
operand->type = return_type;
operand->mode = return_type ? Addressing_Value : Addressing_NoValue;
String class_name = {};
String sel_name = {};
Type *sel_type = t_objc_SEL;
Operand self = {};
check_expr_or_type(c, &self, ce->args[1]);
if (self.mode == Addressing_Type) {
if (!is_type_objc_object(self.type)) {
gbString t = type_to_string(self.type);
error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got type %s", LIT(builtin_name), t);
gb_string_free(t);
return false;
}
if (!has_type_got_objc_class_attribute(self.type)) {
gbString t = type_to_string(self.type);
error(self.expr, "'%.*s' expected a named type with the attribute @(obj_class=<string>) , got type %s", LIT(builtin_name), t);
gb_string_free(t);
return false;
}
sel_type = t_objc_Class;
} else if (!is_operand_value(self) || !check_is_assignable_to(c, &self, t_objc_id)) {
gbString e = expr_to_string(self.expr);
gbString t = type_to_string(self.type);
error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
} else if (!is_type_pointer(self.type)) {
gbString e = expr_to_string(self.expr);
gbString t = type_to_string(self.type);
error(self.expr, "'%.*s' expected a pointer of a value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
} else {
Type *type = type_deref(self.type);
if (!(type->kind == Type_Named &&
type->Named.type_name != nullptr &&
type->Named.type_name->TypeName.objc_class_name != "")) {
gbString t = type_to_string(type);
error(self.expr, "'%.*s' expected a named type with the attribute @(obj_class=<string>) , got type %s", LIT(builtin_name), t);
gb_string_free(t);
return false;
}
}
if (!is_constant_string(c, builtin_name, ce->args[2], &sel_name)) {
return false;
}
isize const arg_offset = 1;
auto param_types = permanent_slice_make<Type *>(ce->args.count-arg_offset);
param_types[0] = t_objc_id;
param_types[1] = sel_type;
for (isize i = 2+arg_offset; i < ce->args.count; i++) {
Operand x = {};
check_expr(c, &x, ce->args[i]);
if (is_type_untyped(x.type)) {
gbString e = expr_to_string(x.expr);
gbString t = type_to_string(x.type);
error(x.expr, "'%.*s' expects typed parameters, got %s of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
}
param_types[i-arg_offset] = x.type;
}
add_objc_proc_type(c, call, return_type, param_types);
return true;
} break;
case BuiltinProc_objc_find_selector:
case BuiltinProc_objc_find_class:
case BuiltinProc_objc_register_selector:
case BuiltinProc_objc_register_class:
{
String sel_name = {};
if (!is_constant_string(c, builtin_name, ce->args[0], &sel_name)) {
return false;
}
switch (id) {
case BuiltinProc_objc_find_selector:
case BuiltinProc_objc_register_selector:
operand->type = t_objc_SEL;
break;
case BuiltinProc_objc_find_class:
case BuiltinProc_objc_register_class:
operand->type = t_objc_Class;
break;
}
operand->mode = Addressing_Value;
try_to_add_package_dependency(c, "runtime", "objc_lookUpClass");
try_to_add_package_dependency(c, "runtime", "sel_registerName");
try_to_add_package_dependency(c, "runtime", "objc_allocateClassPair");
return true;
} break;
case BuiltinProc_objc_ivar_get:
{
Type *self_type = nullptr;
Operand self = {};
check_expr_or_type(c, &self, ce->args[0]);
if (!is_operand_value(self) || !check_is_assignable_to(c, &self, t_objc_id)) {
gbString e = expr_to_string(self.expr);
gbString t = type_to_string(self.type);
error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
} else if (!is_type_pointer(self.type)) {
gbString e = expr_to_string(self.expr);
gbString t = type_to_string(self.type);
error(self.expr, "'%.*s' expected a pointer of a value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
self_type = type_deref(self.type);
if (!(self_type->kind == Type_Named &&
self_type->Named.type_name != nullptr &&
self_type->Named.type_name->TypeName.objc_class_name != "")) {
gbString t = type_to_string(self_type);
error(self.expr, "'%.*s' expected a named type with the attribute @(objc_class=<string>) , got type %s", LIT(builtin_name), t);
gb_string_free(t);
return false;
}
Type *ivar_type = self_type->Named.type_name->TypeName.objc_ivar;
if (ivar_type == nullptr) {
gbString t = type_to_string(self_type);
error(self.expr, "'%.*s' requires that type %s have the attribute @(objc_ivar=<ivar_type_name>).", LIT(builtin_name), t);
gb_string_free(t);
return false;
}
if (type_hint != nullptr && type_hint->kind == Type_Pointer && type_hint->Pointer.elem == ivar_type) {
operand->type = type_hint;
} else {
operand->type = alloc_type_pointer(ivar_type);
}
operand->mode = Addressing_Value;
return true;
} break;
case BuiltinProc_objc_block:
{
// NOTE(harold): The last argument specified in the call is the handler proc,
// any other arguments before it are capture by-copy arguments.
auto param_operands = permanent_slice_make<Operand>(ce->args.count);
isize capture_arg_count = ce->args.count - 1;
// NOTE(harold): The first argument is already checked at check_builtin_procedure().
// Checking again would invalidate the Entity -> Value map for direct arguments if it's the handler proc.
param_operands[0] = *operand;
for (isize i = 0; i < ce->args.count-1; i++) {
Operand x = {};
check_expr(c, &x, ce->args[i]);
switch (x.mode) {
case Addressing_Value:
case Addressing_Context:
case Addressing_Variable:
case Addressing_Constant:
param_operands[i] = x;
break;
default:
gbString e = expr_to_string(x.expr);
gbString t = type_to_string(x.type);
error(x.expr, "'%.*s' capture arguments must be values, but got %s of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
}
// Validate handler proc
Operand handler = {};
if (capture_arg_count == 0) {
// It's already been checked and assigned
handler = param_operands[0];
} else {
check_expr_or_type(c, &handler, ce->args[capture_arg_count]);
param_operands[capture_arg_count] = handler;
}
if (!is_operand_value(handler) || handler.type->kind != Type_Proc) {
gbString e = expr_to_string(handler.expr);
gbString t = type_to_string(handler.type);
error(handler.expr, "'%.*s' expected a procedure, but got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
Ast *handler_node = unparen_expr(handler.expr);
// Only direct reference to procs are allowed
switch (handler_node->kind) {
case Ast_ProcLit: break; // ok
case Ast_Ident: {
auto& ident = handler_node->Ident;
if (ident.entity == nullptr) {
error(handler.expr, "'%.*s' failed to resolve entity from expression", LIT(builtin_name));
return false;
}
if (ident.entity->kind != Entity_Procedure) {
gbString e = expr_to_string(handler_node);
ERROR_BLOCK();
error(handler.expr, "'%.*s' expected a direct reference to a procedure", LIT(builtin_name));
if(ident.entity->kind == Entity_Variable) {
error_line("\tSuggestion: Variables referencing a procedure are not allowed, they are not a direct procedure reference.");
} else {
error_line("\tSuggestion: Ensure '%s' is not a runtime-evaluated expression.", e); // NOTE(harold): Is this case possible to hit?
}
error_line("\n\t Refer to a procedure directly by its name or declare it anonymously: %.*s(proc(){})", LIT(builtin_name));
gb_string_free(e);
return false;
}
} break;
default: {
gbString e = expr_to_string(handler_node);
ERROR_BLOCK();
error(handler.expr, "'%.*s' expected a direct reference to a procedure", LIT(builtin_name));
if( handler_node->kind == Ast_CallExpr) {
error_line("\tSuggestion: Do not use a procedure returned from another procedure.");
} else {
error_line("\tSuggestion: Ensure '%s' is not a runtime-evaluated expression.", e);
}
error_line("\n\t Refer to a procedure directly by its name or declare it anonymously: %.*s(proc(){})", LIT(builtin_name));
gb_string_free(e);
} return false;
} // End switch
auto& handler_type_proc = handler.type->Proc;
if (capture_arg_count > handler_type_proc.param_count) {
error(handler.expr, "'%.*s' captured arguments exceeded the handler's parameter count", LIT(builtin_name));
return false;
}
// If the handler proc is odin calling convention, but there must be a context defined in this scope.
if (handler_type_proc.calling_convention == ProcCC_Odin) {
if ((c->scope->flags & ScopeFlag_ContextDefined) == 0) {
ERROR_BLOCK();
error(handler.expr, "The handler procedure for '%.*s' requires a context, but no context is defined in the current scope", LIT(builtin_name));
error_line("\tSuggestion: 'context = runtime.default_context()', or use the \"c\" calling convention for the handler procedure");
return false;
}
}
// At most a single return value is supported
if (handler_type_proc.result_count > 1) {
error(handler_type_proc.node->ProcType.results, "Handler procedures for '%.*s' cannot have multiple return values", LIT(builtin_name));
return false;
}
// Ensure that captured args are assignable to the handler's corresponding capture params
if (handler_type_proc.param_count > 0) {
auto& handler_param_types = handler.type->Proc.params->Tuple.variables;
Slice<Entity *> handler_capture_param_types = slice(handler_param_types, handler_param_types.count - capture_arg_count, handler_param_types.count);
for (isize i = 0; i < capture_arg_count; i++) {
Operand op = param_operands[i];
if (!check_is_assignable_to(c, &op, handler_capture_param_types[i]->type)) {
gbString e = expr_to_string(op.expr);
gbString src = type_to_string(op.type);
gbString dst = type_to_string(handler_capture_param_types[i]->type);
error(op.expr, "'%.*s' captured value '%s' of type '%s' is not assignable to type '%s'", LIT(builtin_name), e, src, dst);
gb_string_free(e);
gb_string_free(src);
gb_string_free(dst);
return false;
}
}
}
ProcCallingConvention cc = handler_type_proc.calling_convention;
switch (cc) {
case ProcCC_Odin:
case ProcCC_Contextless:
case ProcCC_CDecl:
break; // ok
default:
ERROR_BLOCK();
error(handler.expr, "'%.*s' Invalid calling convention for block procedure.", LIT(builtin_name));
error_line("\tSuggestion: Do not specify a calling convention ot else use \"c\" or \"cotextless\"");
return false;
}
if (handler_type_proc.is_polymorphic) {
error(handler.expr, "'%.*s' Unspecialized polymorphic procedures are not allowed.", LIT(builtin_name));
return false;
}
// Create the specialized Objc_Block type that this intrinsic will return
Token ident = {};
ident.kind = Token_Ident;
ident.string = str_lit("Objc_Block");
ident.pos = ast_token(call).pos;
Token l_paren = {};
l_paren.kind = Token_OpenParen;
l_paren.string = str_lit("(");
l_paren.pos = ident.pos;
Token r_paren = {};
r_paren.kind = Token_CloseParen;
l_paren.string = str_lit(")");
r_paren.pos = ident.pos;
// Remove the capture args from the resulting Objc_Block type signature
Ast* handler_proc_type_copy = clone_ast(handler_type_proc.node);
handler_proc_type_copy->ProcType.params->FieldList.list.count -= capture_arg_count;
// Make sure the Objc_Block's specialized proc is always "c" calling conv,
// even if we have a context, as the invoker is always "c".
// This allows us to have compatibility with the target block types with either calling convention used.
handler_proc_type_copy->ProcType.calling_convention = ProcCC_CDecl;
Array<Ast *> poly_args = {};
array_init(&poly_args, permanent_allocator(), 1, 1);
poly_args[0] = handler_proc_type_copy;
Type *t_Objc_Block = find_core_type(c->checker, str_lit("Objc_Block"));
Operand poly_op = {};
poly_op.type = t_Objc_Block;
poly_op.mode = Addressing_Type;
Ast *poly_call = ast_call_expr(nullptr, ast_ident(nullptr, ident), poly_args, l_paren, r_paren, {});
auto err = check_polymorphic_record_type(c, &poly_op, poly_call);
if (err != 0) {
operand->mode = Addressing_Invalid;
operand->type = t_invalid;
error(handler.expr, "'%.*s' failed to determine resulting Objc_Block handler procedure", LIT(builtin_name));
return false;
}
GB_ASSERT(poly_op.type != t_Objc_Block);
GB_ASSERT(poly_op.mode == Addressing_Type);
bool is_global_block = capture_arg_count == 0 && handler_type_proc.calling_convention != ProcCC_Odin;
if (is_global_block) {
try_to_add_package_dependency(c, "runtime", "_NSConcreteGlobalBlock");
} else {
try_to_add_package_dependency(c, "runtime", "_NSConcreteStackBlock");
}
*operand = poly_op;
operand->type = alloc_type_pointer(operand->type);
operand->mode = Addressing_Value;
return true;
} break;
case BuiltinProc_objc_super:
{
// Must be a pointer to an Objective-C object.
Type *objc_obj = operand->type;
if (!is_type_objc_ptr_to_object(objc_obj)) {
gbString e = expr_to_string(operand->expr);
gbString t = type_to_string(objc_obj);
error(operand->expr, "'%.*s' expected a pointer to an Objective-C object, but got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
if (operand->mode != Addressing_Value && operand->mode != Addressing_Variable) {
gbString e = expr_to_string(operand->expr);
gbString t = type_to_string(operand->type);
error(operand->expr, "'%.*s' expression '%s', of type %s, must be a value or variable.", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
Type *obj_type = type_deref(objc_obj);
GB_ASSERT(obj_type->kind == Type_Named);
// NOTE(harold) Track original type before transforming it to the superclass.
// This is needed because objc_msgSendSuper2 must start its search on the subclass, not the superclass.
call->tav.objc_super_target = obj_type;
// The superclass type must be known at compile time. We require this so that the selector method expressions
// methods are resolved to the superclass's methods instead of the subclass's.
Type *superclass = obj_type->Named.type_name->TypeName.objc_superclass;
if (superclass == nullptr) {
gbString t = type_to_string(obj_type);
error(operand->expr, "'%.*s' target object '%.*s' does not have an Objective-C superclass. One must be set via the @(objc_superclass) attribute", LIT(builtin_name), t);
gb_string_free(t);
return false;
}
GB_ASSERT(superclass->Named.type_name->TypeName.objc_class_name.len > 0);
operand->type = alloc_type_pointer(superclass);
return true;
} break;
}
}
gb_internal bool check_atomic_memory_order_argument(CheckerContext *c, Ast *expr, String const &builtin_name, OdinAtomicMemoryOrder *memory_order_, char const *extra_message = nullptr) {
Operand x = {};
check_expr_with_type_hint(c, &x, expr, t_atomic_memory_order);
if (x.mode == Addressing_Invalid) {
return false;
}
if (!are_types_identical(x.type, t_atomic_memory_order) || x.mode != Addressing_Constant) {
gbString str = type_to_string(x.type);
if (extra_message) {
error(x.expr, "Expected a constant Atomic_Memory_Order value for the %s of '%.*s', got %s", extra_message, LIT(builtin_name), str);
} else {
error(x.expr, "Expected a constant Atomic_Memory_Order value for '%.*s', got %s", LIT(builtin_name), str);
}
gb_string_free(str);
return false;
}
i64 value = exact_value_to_i64(x.value);
if (value < 0 || value >= OdinAtomicMemoryOrder_COUNT) {
error(x.expr, "Illegal Atomic_Memory_Order value, got %lld", cast(long long)value);
return false;
}
if (memory_order_) {
*memory_order_ = cast(OdinAtomicMemoryOrder)value;
}
return true;
}
gb_internal bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
ast_node(ce, CallExpr, call);
String const &builtin_name = builtin_procs[id].name;
switch (id) {
// Any numeric
case BuiltinProc_simd_add:
case BuiltinProc_simd_sub:
case BuiltinProc_simd_mul:
case BuiltinProc_simd_div:
case BuiltinProc_simd_min:
case BuiltinProc_simd_max:
{
Operand x = {};
Operand y = {};
check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
if (!is_type_simd_vector(x.type)) {
error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
if (!is_type_simd_vector(y.type)) {
error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
if (!are_types_identical(x.type, y.type)) {
gbString xs = type_to_string(x.type);
gbString ys = type_to_string(y.type);
error(x.expr, "'%.*s' expected 2 arguments of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, ys);
gb_string_free(ys);
gb_string_free(xs);
return false;
}
Type *elem = base_array_type(x.type);
if (!is_type_integer(elem) && !is_type_float(elem)) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
return false;
}
if (id == BuiltinProc_simd_div && is_type_integer(elem)) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' is not supported for integer elements, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
// don't return
}
operand->mode = Addressing_Value;
operand->type = x.type;
return true;
}
// Integer only
case BuiltinProc_simd_saturating_add:
case BuiltinProc_simd_saturating_sub:
case BuiltinProc_simd_bit_and:
case BuiltinProc_simd_bit_or:
case BuiltinProc_simd_bit_xor:
case BuiltinProc_simd_bit_and_not:
{
Operand x = {};
Operand y = {};
check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
if (!is_type_simd_vector(x.type)) {
error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
if (!is_type_simd_vector(y.type)) {
error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
if (!are_types_identical(x.type, y.type)) {
gbString xs = type_to_string(x.type);
gbString ys = type_to_string(y.type);
error(x.expr, "'%.*s' expected 2 arguments of the same type, got '%s' vs '%s'", LIT(builtin_name), xs, ys);
gb_string_free(ys);
gb_string_free(xs);
return false;
}
Type *elem = base_array_type(x.type);
switch (id) {
case BuiltinProc_simd_saturating_add:
case BuiltinProc_simd_saturating_sub:
if (!is_type_integer(elem)) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' expected a #simd type with an integer element, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
return false;
}
break;
default:
if (!is_type_integer(elem) && !is_type_boolean(elem)) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' expected a #simd type with an integer or boolean element, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
return false;
}
break;
}
operand->mode = Addressing_Value;
operand->type = x.type;
return true;
}
case BuiltinProc_simd_shl: // Odin-like
case BuiltinProc_simd_shr: // Odin-like
case BuiltinProc_simd_shl_masked: // C-like
case BuiltinProc_simd_shr_masked: // C-like
{
Operand x = {};
Operand y = {};
check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
if (!is_type_simd_vector(x.type)) {
error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
if (!is_type_simd_vector(y.type)) {
error(y.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
GB_ASSERT(x.type->kind == Type_SimdVector);
GB_ASSERT(y.type->kind == Type_SimdVector);
Type *xt = x.type;
Type *yt = y.type;
if (xt->SimdVector.count != yt->SimdVector.count) {
error(x.expr, "'%.*s' mismatched simd vector lengths, got '%lld' vs '%lld'",
LIT(builtin_name),
cast(long long)xt->SimdVector.count,
cast(long long)yt->SimdVector.count);
return false;
}
if (!is_type_integer(base_array_type(x.type))) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' expected a #simd type with an integer element, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
return false;
}
if (!is_type_unsigned(base_array_type(y.type))) {
gbString ys = type_to_string(y.type);
error(y.expr, "'%.*s' expected a #simd type with an unsigned integer element as the shifting operand, got '%s'", LIT(builtin_name), ys);
gb_string_free(ys);
return false;
}
operand->mode = Addressing_Value;
operand->type = x.type;
return true;
}
// Unary
case BuiltinProc_simd_neg:
case BuiltinProc_simd_abs:
{
Operand x = {};
check_expr(c, &x, ce->args[0]);
if (x.mode == Addressing_Invalid) {
return false;
}
if (!is_type_simd_vector(x.type)) {
error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
Type *elem = base_array_type(x.type);
if (!is_type_integer(elem) && !is_type_float(elem)) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
return false;
}
operand->mode = Addressing_Value;
operand->type = x.type;
return true;
}
// Return integer masks
case BuiltinProc_simd_lanes_eq:
case BuiltinProc_simd_lanes_ne:
case BuiltinProc_simd_lanes_lt:
case BuiltinProc_simd_lanes_le:
case BuiltinProc_simd_lanes_gt:
case BuiltinProc_simd_lanes_ge:
{
// op(#simd[N]T, #simd[N]T) -> #simd[N]V
// where `V` is an integer, `size_of(T) == size_of(V)`
// `V` will all 0s if false and all 1s if true (e.g. 0x00 and 0xff for false and true, respectively)
Operand x = {};
Operand y = {};
check_expr(c, &x, ce->args[0]); if (x.mode == Addressing_Invalid) return false;
check_expr_with_type_hint(c, &y, ce->args[1], x.type); if (y.mode == Addressing_Invalid) return false;
convert_to_typed(c, &y, x.type); if (y.mode == Addressing_Invalid) return false;
if (!is_type_simd_vector(x.type)) {
error(x.expr, "'%.*s' expected a simd vector type", LIT(builtin_name));
return false;
}
Type *elem = base_array_type(x.type);
switch (id) {
case BuiltinProc_simd_lanes_eq:
case BuiltinProc_simd_lanes_ne:
if (!is_type_integer(elem) && !is_type_float(elem) && !is_type_boolean(elem)) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' expected a #simd type with an integer, floating point, or boolean element, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
return false;
}
break;
default:
if (!is_type_integer(elem) && !is_type_float(elem)) {
gbString xs = type_to_string(x.type);
error(x.expr, "'%.*s' expected a #simd type with an integer or floating point element, got '%s'", LIT(builtin_name), xs);
gb_string_free(xs);
return false;
}
break;
}
if (!are_types_identical(x.type, y.type)) {
gbString tx = type_to_string(x.type);
gbString ty = type_to_string(y.type);
error(call, "Mismatched types to '%.*s', '%s' vs '%s'", LIT(builtin_name), tx, ty);
gb_string_free(ty);