-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathtranslate_c.cpp
4869 lines (4399 loc) · 211 KB
/
translate_c.cpp
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
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#include "all_types.hpp"
#include "analyze.hpp"
#include "c_tokenizer.hpp"
#include "error.hpp"
#include "ir.hpp"
#include "os.hpp"
#include "translate_c.hpp"
#include "parser.hpp"
#if __GNUC__ >= 8
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wclass-memaccess"
#endif
#include <clang/Frontend/ASTUnit.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/AST/Expr.h>
#if __GNUC__ >= 8
#pragma GCC diagnostic pop
#endif
#include <string.h>
using namespace clang;
struct Alias {
Buf *new_name;
Buf *canon_name;
};
enum TransScopeId {
TransScopeIdSwitch,
TransScopeIdVar,
TransScopeIdBlock,
TransScopeIdRoot,
TransScopeIdWhile,
};
struct TransScope {
TransScopeId id;
TransScope *parent;
};
struct TransScopeSwitch {
TransScope base;
AstNode *switch_node;
uint32_t case_index;
bool found_default;
Buf *end_label_name;
};
struct TransScopeVar {
TransScope base;
Buf *c_name;
Buf *zig_name;
};
struct TransScopeBlock {
TransScope base;
AstNode *node;
};
struct TransScopeRoot {
TransScope base;
};
struct TransScopeWhile {
TransScope base;
AstNode *node;
};
struct Context {
ImportTableEntry *import;
ZigList<ErrorMsg *> *errors;
VisibMod visib_mod;
bool want_export;
AstNode *root;
HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table;
HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> global_table;
SourceManager *source_manager;
ZigList<Alias> aliases;
AstNode *source_node;
bool warnings_on;
CodeGen *codegen;
ASTContext *ctx;
TransScopeRoot *global_scope;
HashMap<Buf *, bool, buf_hash, buf_eql_buf> ptr_params;
};
enum ResultUsed {
ResultUsedNo,
ResultUsedYes,
};
enum TransLRValue {
TransLValue,
TransRValue,
};
static TransScopeRoot *trans_scope_root_create(Context *c);
static TransScopeWhile *trans_scope_while_create(Context *c, TransScope *parent_scope);
static TransScopeBlock *trans_scope_block_create(Context *c, TransScope *parent_scope);
static TransScopeVar *trans_scope_var_create(Context *c, TransScope *parent_scope, Buf *wanted_name);
static TransScopeSwitch *trans_scope_switch_create(Context *c, TransScope *parent_scope);
static TransScopeBlock *trans_scope_block_find(TransScope *scope);
static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl);
static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl);
static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl);
static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
ResultUsed result_used, TransLRValue lrval,
AstNode **out_node, TransScope **out_child_scope,
TransScope **out_node_scope);
static TransScope *trans_stmt(Context *c, TransScope *scope, const Stmt *stmt, AstNode **out_node);
static AstNode *trans_expr(Context *c, ResultUsed result_used, TransScope *scope, const Expr *expr, TransLRValue lrval);
static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc);
static AstNode *trans_bool_expr(Context *c, ResultUsed result_used, TransScope *scope, const Expr *expr, TransLRValue lrval);
ATTRIBUTE_PRINTF(3, 4)
static void emit_warning(Context *c, const SourceLocation &sl, const char *format, ...) {
if (!c->warnings_on) {
return;
}
va_list ap;
va_start(ap, format);
Buf *msg = buf_vprintf(format, ap);
va_end(ap);
StringRef filename = c->source_manager->getFilename(c->source_manager->getSpellingLoc(sl));
const char *filename_bytes = (const char *)filename.bytes_begin();
Buf *path;
if (filename_bytes) {
path = buf_create_from_str(filename_bytes);
} else {
path = buf_sprintf("(no file)");
}
unsigned line = c->source_manager->getSpellingLineNumber(sl);
unsigned column = c->source_manager->getSpellingColumnNumber(sl);
fprintf(stderr, "%s:%u:%u: warning: %s\n", buf_ptr(path), line, column, buf_ptr(msg));
}
static void add_global_weak_alias(Context *c, Buf *new_name, Buf *canon_name) {
Alias *alias = c->aliases.add_one();
alias->new_name = new_name;
alias->canon_name = canon_name;
}
static Buf *trans_lookup_zig_symbol(Context *c, TransScope *scope, Buf *c_symbol_name) {
while (scope != nullptr) {
if (scope->id == TransScopeIdVar) {
TransScopeVar *var_scope = (TransScopeVar *)scope;
if (buf_eql_buf(var_scope->c_name, c_symbol_name)) {
return var_scope->zig_name;
}
}
scope = scope->parent;
}
return c_symbol_name;
}
static AstNode * trans_create_node(Context *c, NodeType id) {
AstNode *node = allocate<AstNode>(1);
node->type = id;
node->owner = c->import;
// TODO line/column. mapping to C file??
return node;
}
static AstNode *trans_create_node_break(Context *c, Buf *label_name, AstNode *value_node) {
AstNode *node = trans_create_node(c, NodeTypeBreak);
node->data.break_expr.name = label_name;
node->data.break_expr.expr = value_node;
return node;
}
static AstNode *trans_create_node_return(Context *c, AstNode *value_node) {
AstNode *node = trans_create_node(c, NodeTypeReturnExpr);
node->data.return_expr.kind = ReturnKindUnconditional;
node->data.return_expr.expr = value_node;
return node;
}
static AstNode *trans_create_node_if(Context *c, AstNode *cond_node, AstNode *then_node, AstNode *else_node) {
AstNode *node = trans_create_node(c, NodeTypeIfBoolExpr);
node->data.if_bool_expr.condition = cond_node;
node->data.if_bool_expr.then_block = then_node;
node->data.if_bool_expr.else_node = else_node;
return node;
}
static AstNode *trans_create_node_float_lit(Context *c, double value) {
AstNode *node = trans_create_node(c, NodeTypeFloatLiteral);
node->data.float_literal.bigfloat = allocate<BigFloat>(1);
bigfloat_init_64(node->data.float_literal.bigfloat, value);
return node;
}
static AstNode *trans_create_node_symbol(Context *c, Buf *name) {
AstNode *node = trans_create_node(c, NodeTypeSymbol);
node->data.symbol_expr.symbol = name;
return node;
}
static AstNode *trans_create_node_symbol_str(Context *c, const char *name) {
return trans_create_node_symbol(c, buf_create_from_str(name));
}
static AstNode *trans_create_node_builtin_fn_call(Context *c, Buf *name) {
AstNode *node = trans_create_node(c, NodeTypeFnCallExpr);
node->data.fn_call_expr.fn_ref_expr = trans_create_node_symbol(c, name);
node->data.fn_call_expr.is_builtin = true;
return node;
}
static AstNode *trans_create_node_builtin_fn_call_str(Context *c, const char *name) {
return trans_create_node_builtin_fn_call(c, buf_create_from_str(name));
}
static AstNode *trans_create_node_opaque(Context *c) {
return trans_create_node_builtin_fn_call_str(c, "OpaqueType");
}
static AstNode *trans_create_node_fn_call_1(Context *c, AstNode *fn_ref_expr, AstNode *arg1) {
AstNode *node = trans_create_node(c, NodeTypeFnCallExpr);
node->data.fn_call_expr.fn_ref_expr = fn_ref_expr;
node->data.fn_call_expr.params.append(arg1);
return node;
}
static AstNode *trans_create_node_field_access(Context *c, AstNode *container, Buf *field_name) {
AstNode *node = trans_create_node(c, NodeTypeFieldAccessExpr);
if (container->type == NodeTypeSymbol) {
assert(container->data.symbol_expr.symbol != nullptr);
}
node->data.field_access_expr.struct_expr = container;
node->data.field_access_expr.field_name = field_name;
return node;
}
static AstNode *trans_create_node_field_access_str(Context *c, AstNode *container, const char *field_name) {
return trans_create_node_field_access(c, container, buf_create_from_str(field_name));
}
static AstNode *trans_create_node_ptr_deref(Context *c, AstNode *child_node) {
AstNode *node = trans_create_node(c, NodeTypePtrDeref);
node->data.ptr_deref_expr.target = child_node;
return node;
}
static AstNode *trans_create_node_prefix_op(Context *c, PrefixOp op, AstNode *child_node) {
AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
node->data.prefix_op_expr.prefix_op = op;
node->data.prefix_op_expr.primary_expr = child_node;
return node;
}
static AstNode *trans_create_node_unwrap_null(Context *c, AstNode *child_node) {
AstNode *node = trans_create_node(c, NodeTypeUnwrapOptional);
node->data.unwrap_optional.expr = child_node;
return node;
}
static AstNode *trans_create_node_bin_op(Context *c, AstNode *lhs_node, BinOpType op, AstNode *rhs_node) {
AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
node->data.bin_op_expr.op1 = lhs_node;
node->data.bin_op_expr.bin_op = op;
node->data.bin_op_expr.op2 = rhs_node;
return node;
}
static AstNode *maybe_suppress_result(Context *c, ResultUsed result_used, AstNode *node) {
if (result_used == ResultUsedYes) return node;
return trans_create_node_bin_op(c,
trans_create_node_symbol_str(c, "_"),
BinOpTypeAssign,
node);
}
static AstNode *trans_create_node_ptr_type(Context *c, bool is_const, bool is_volatile, AstNode *child_node, PtrLen ptr_len) {
AstNode *node = trans_create_node(c, NodeTypePointerType);
node->data.pointer_type.star_token = allocate<ZigToken>(1);
node->data.pointer_type.star_token->id = (ptr_len == PtrLenSingle) ? TokenIdStar: TokenIdBracketStarBracket;
node->data.pointer_type.is_const = is_const;
node->data.pointer_type.is_const = is_const;
node->data.pointer_type.is_volatile = is_volatile;
node->data.pointer_type.op_expr = child_node;
return node;
}
static AstNode *trans_create_node_addr_of(Context *c, AstNode *child_node) {
AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
node->data.prefix_op_expr.prefix_op = PrefixOpAddrOf;
node->data.prefix_op_expr.primary_expr = child_node;
return node;
}
static AstNode *trans_create_node_bool(Context *c, bool value) {
AstNode *bool_node = trans_create_node(c, NodeTypeBoolLiteral);
bool_node->data.bool_literal.value = value;
return bool_node;
}
static AstNode *trans_create_node_str_lit_c(Context *c, Buf *buf) {
AstNode *node = trans_create_node(c, NodeTypeStringLiteral);
node->data.string_literal.buf = buf;
node->data.string_literal.c = true;
return node;
}
static AstNode *trans_create_node_str_lit_non_c(Context *c, Buf *buf) {
AstNode *node = trans_create_node(c, NodeTypeStringLiteral);
node->data.string_literal.buf = buf;
node->data.string_literal.c = false;
return node;
}
static AstNode *trans_create_node_unsigned_negative(Context *c, uint64_t x, bool is_negative) {
AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
node->data.int_literal.bigint = allocate<BigInt>(1);
bigint_init_data(node->data.int_literal.bigint, &x, 1, is_negative);
return node;
}
static AstNode *trans_create_node_unsigned(Context *c, uint64_t x) {
return trans_create_node_unsigned_negative(c, x, false);
}
static AstNode *trans_create_node_cast(Context *c, AstNode *dest, AstNode *src) {
AstNode *node = trans_create_node(c, NodeTypeFnCallExpr);
node->data.fn_call_expr.fn_ref_expr = dest;
node->data.fn_call_expr.params.resize(1);
node->data.fn_call_expr.params.items[0] = src;
return node;
}
static AstNode *trans_create_node_unsigned_negative_type(Context *c, uint64_t x, bool is_negative,
const char *type_name)
{
AstNode *lit_node = trans_create_node_unsigned_negative(c, x, is_negative);
return trans_create_node_cast(c, trans_create_node_symbol_str(c, type_name), lit_node);
}
static AstNode *trans_create_node_array_type(Context *c, AstNode *size_node, AstNode *child_type_node) {
AstNode *node = trans_create_node(c, NodeTypeArrayType);
node->data.array_type.size = size_node;
node->data.array_type.child_type = child_type_node;
return node;
}
static AstNode *trans_create_node_var_decl(Context *c, VisibMod visib_mod, bool is_const, Buf *var_name,
AstNode *type_node, AstNode *init_node)
{
AstNode *node = trans_create_node(c, NodeTypeVariableDeclaration);
node->data.variable_declaration.visib_mod = visib_mod;
node->data.variable_declaration.symbol = var_name;
node->data.variable_declaration.is_const = is_const;
node->data.variable_declaration.type = type_node;
node->data.variable_declaration.expr = init_node;
return node;
}
static AstNode *trans_create_node_var_decl_global(Context *c, bool is_const, Buf *var_name, AstNode *type_node,
AstNode *init_node)
{
return trans_create_node_var_decl(c, c->visib_mod, is_const, var_name, type_node, init_node);
}
static AstNode *trans_create_node_var_decl_local(Context *c, bool is_const, Buf *var_name, AstNode *type_node,
AstNode *init_node)
{
return trans_create_node_var_decl(c, VisibModPrivate, is_const, var_name, type_node, init_node);
}
static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *ref_node, AstNode *src_proto_node) {
AstNode *fn_def = trans_create_node(c, NodeTypeFnDef);
AstNode *fn_proto = trans_create_node(c, NodeTypeFnProto);
fn_proto->data.fn_proto.visib_mod = c->visib_mod;
fn_proto->data.fn_proto.name = fn_name;
fn_proto->data.fn_proto.is_inline = true;
fn_proto->data.fn_proto.return_type = src_proto_node->data.fn_proto.return_type; // TODO ok for these to alias?
fn_def->data.fn_def.fn_proto = fn_proto;
fn_proto->data.fn_proto.fn_def_node = fn_def;
AstNode *unwrap_node = trans_create_node_unwrap_null(c, ref_node);
AstNode *fn_call_node = trans_create_node(c, NodeTypeFnCallExpr);
fn_call_node->data.fn_call_expr.fn_ref_expr = unwrap_node;
for (size_t i = 0; i < src_proto_node->data.fn_proto.params.length; i += 1) {
AstNode *src_param_node = src_proto_node->data.fn_proto.params.at(i);
Buf *param_name = src_param_node->data.param_decl.name;
if (!param_name) param_name = buf_sprintf("arg%" ZIG_PRI_usize "", i);
AstNode *dest_param_node = trans_create_node(c, NodeTypeParamDecl);
dest_param_node->data.param_decl.name = param_name;
dest_param_node->data.param_decl.type = src_param_node->data.param_decl.type;
dest_param_node->data.param_decl.is_noalias = src_param_node->data.param_decl.is_noalias;
fn_proto->data.fn_proto.params.append(dest_param_node);
fn_call_node->data.fn_call_expr.params.append(trans_create_node_symbol(c, param_name));
}
AstNode *block = trans_create_node(c, NodeTypeBlock);
block->data.block.statements.resize(1);
block->data.block.statements.items[0] = trans_create_node_return(c, fn_call_node);
fn_def->data.fn_def.body = block;
return fn_def;
}
static AstNode *get_global(Context *c, Buf *name) {
{
auto entry = c->global_table.maybe_get(name);
if (entry) {
return entry->value;
}
}
{
auto entry = c->macro_table.maybe_get(name);
if (entry)
return entry->value;
}
if (get_primitive_type(c->codegen, name) != nullptr) {
return trans_create_node_symbol(c, name);
}
return nullptr;
}
static void add_top_level_decl(Context *c, Buf *name, AstNode *node) {
c->global_table.put(name, node);
c->root->data.root.top_level_decls.append(node);
}
static AstNode *add_global_var(Context *c, Buf *var_name, AstNode *value_node) {
bool is_const = true;
AstNode *type_node = nullptr;
AstNode *node = trans_create_node_var_decl_global(c, is_const, var_name, type_node, value_node);
add_top_level_decl(c, var_name, node);
return node;
}
static Buf *string_ref_to_buf(StringRef string_ref) {
return buf_create_from_mem((const char *)string_ref.bytes_begin(), string_ref.size());
}
static const char *decl_name(const Decl *decl) {
const NamedDecl *named_decl = static_cast<const NamedDecl *>(decl);
return (const char *)named_decl->getName().bytes_begin();
}
static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {
AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
node->data.int_literal.bigint = allocate<BigInt>(1);
bool is_negative = aps_int.isNegative();
if (!is_negative) {
bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), false);
return node;
}
llvm::APSInt negated = -aps_int;
bigint_init_data(node->data.int_literal.bigint, negated.getRawData(), negated.getNumWords(), true);
return node;
}
static const Type *qual_type_canon(QualType qt) {
return qt.getCanonicalType().getTypePtr();
}
static QualType get_expr_qual_type(Context *c, const Expr *expr) {
// String literals in C are `char *` but they should really be `const char *`.
if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) {
const ImplicitCastExpr *cast_expr = static_cast<const ImplicitCastExpr *>(expr);
if (cast_expr->getCastKind() == CK_ArrayToPointerDecay) {
const Expr *sub_expr = cast_expr->getSubExpr();
if (sub_expr->getStmtClass() == Stmt::StringLiteralClass) {
QualType array_qt = sub_expr->getType();
const ArrayType *array_type = static_cast<const ArrayType *>(array_qt.getTypePtr());
QualType pointee_qt = array_type->getElementType();
pointee_qt.addConst();
return c->ctx->getPointerType(pointee_qt);
}
}
}
return expr->getType();
}
static QualType get_expr_qual_type_before_implicit_cast(Context *c, const Expr *expr) {
if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) {
const ImplicitCastExpr *cast_expr = static_cast<const ImplicitCastExpr *>(expr);
return get_expr_qual_type(c, cast_expr->getSubExpr());
}
return expr->getType();
}
static AstNode *get_expr_type(Context *c, const Expr *expr) {
return trans_qual_type(c, get_expr_qual_type(c, expr), expr->getLocStart());
}
static bool qual_types_equal(QualType t1, QualType t2) {
if (t1.isConstQualified() != t2.isConstQualified()) {
return false;
}
if (t1.isVolatileQualified() != t2.isVolatileQualified()) {
return false;
}
if (t1.isRestrictQualified() != t2.isRestrictQualified()) {
return false;
}
return t1.getTypePtr() == t2.getTypePtr();
}
static bool is_c_void_type(AstNode *node) {
return (node->type == NodeTypeSymbol && buf_eql_str(node->data.symbol_expr.symbol, "c_void"));
}
static bool expr_types_equal(Context *c, const Expr *expr1, const Expr *expr2) {
QualType t1 = get_expr_qual_type(c, expr1);
QualType t2 = get_expr_qual_type(c, expr2);
return qual_types_equal(t1, t2);
}
static bool qual_type_is_ptr(QualType qt) {
const Type *ty = qual_type_canon(qt);
return ty->getTypeClass() == Type::Pointer;
}
static const FunctionProtoType *qual_type_get_fn_proto(QualType qt, bool *is_ptr) {
const Type *ty = qual_type_canon(qt);
*is_ptr = false;
if (ty->getTypeClass() == Type::Pointer) {
*is_ptr = true;
const PointerType *pointer_ty = static_cast<const PointerType*>(ty);
QualType child_qt = pointer_ty->getPointeeType();
ty = child_qt.getTypePtr();
}
if (ty->getTypeClass() == Type::FunctionProto) {
return static_cast<const FunctionProtoType*>(ty);
}
return nullptr;
}
static bool qual_type_is_fn_ptr(QualType qt) {
bool is_ptr;
if (qual_type_get_fn_proto(qt, &is_ptr)) {
return is_ptr;
}
return false;
}
static uint32_t qual_type_int_bit_width(Context *c, const QualType &qt, const SourceLocation &source_loc) {
const Type *ty = qt.getTypePtr();
switch (ty->getTypeClass()) {
case Type::Builtin:
{
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
switch (builtin_ty->getKind()) {
case BuiltinType::Char_U:
case BuiltinType::UChar:
case BuiltinType::Char_S:
case BuiltinType::SChar:
return 8;
case BuiltinType::UInt128:
case BuiltinType::Int128:
return 128;
default:
return 0;
}
zig_unreachable();
}
case Type::Typedef:
{
const TypedefType *typedef_ty = static_cast<const TypedefType*>(ty);
const TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
const char *type_name = decl_name(typedef_decl);
if (strcmp(type_name, "uint8_t") == 0 || strcmp(type_name, "int8_t") == 0) {
return 8;
} else if (strcmp(type_name, "uint16_t") == 0 || strcmp(type_name, "int16_t") == 0) {
return 16;
} else if (strcmp(type_name, "uint32_t") == 0 || strcmp(type_name, "int32_t") == 0) {
return 32;
} else if (strcmp(type_name, "uint64_t") == 0 || strcmp(type_name, "int64_t") == 0) {
return 64;
} else {
return 0;
}
}
default:
return 0;
}
zig_unreachable();
}
static AstNode *qual_type_to_log2_int_ref(Context *c, const QualType &qt,
const SourceLocation &source_loc)
{
uint32_t int_bit_width = qual_type_int_bit_width(c, qt, source_loc);
if (int_bit_width != 0) {
// we can perform the log2 now.
uint64_t cast_bit_width = log2_u64(int_bit_width);
return trans_create_node_symbol(c, buf_sprintf("u%" ZIG_PRI_u64, cast_bit_width));
}
AstNode *zig_type_node = trans_qual_type(c, qt, source_loc);
// @import("std").math.Log2Int(c_long);
//
// FnCall
// FieldAccess
// FieldAccess
// FnCall (.builtin = true)
// Symbol "import"
// StringLiteral "std"
// Symbol "math"
// Symbol "Log2Int"
// zig_type_node
AstNode *import_fn_call = trans_create_node_builtin_fn_call_str(c, "import");
import_fn_call->data.fn_call_expr.params.append(trans_create_node_str_lit_non_c(c, buf_create_from_str("std")));
AstNode *inner_field_access = trans_create_node_field_access_str(c, import_fn_call, "math");
AstNode *outer_field_access = trans_create_node_field_access_str(c, inner_field_access, "Log2Int");
AstNode *log2int_fn_call = trans_create_node_fn_call_1(c, outer_field_access, zig_type_node);
return log2int_fn_call;
}
static bool qual_type_child_is_fn_proto(const QualType &qt) {
if (qt.getTypePtr()->getTypeClass() == Type::Paren) {
const ParenType *paren_type = static_cast<const ParenType *>(qt.getTypePtr());
if (paren_type->getInnerType()->getTypeClass() == Type::FunctionProto) {
return true;
}
} else if (qt.getTypePtr()->getTypeClass() == Type::Attributed) {
const AttributedType *attr_type = static_cast<const AttributedType *>(qt.getTypePtr());
return qual_type_child_is_fn_proto(attr_type->getEquivalentType());
}
return false;
}
static AstNode* trans_c_cast(Context *c, const SourceLocation &source_location, QualType dest_type,
QualType src_type, AstNode *expr)
{
if (qual_types_equal(dest_type, src_type)) {
return expr;
}
if (qual_type_is_ptr(dest_type) && qual_type_is_ptr(src_type)) {
AstNode *ptr_cast_node = trans_create_node_builtin_fn_call_str(c, "ptrCast");
ptr_cast_node->data.fn_call_expr.params.append(trans_qual_type(c, dest_type, source_location));
ptr_cast_node->data.fn_call_expr.params.append(expr);
return ptr_cast_node;
}
// TODO: maybe widen to increase size
// TODO: maybe bitcast to change sign
// TODO: maybe truncate to reduce size
return trans_create_node_fn_call_1(c, trans_qual_type(c, dest_type, source_location), expr);
}
static bool c_is_signed_integer(Context *c, QualType qt) {
const Type *c_type = qual_type_canon(qt);
if (c_type->getTypeClass() != Type::Builtin)
return false;
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(c_type);
switch (builtin_ty->getKind()) {
case BuiltinType::SChar:
case BuiltinType::Short:
case BuiltinType::Int:
case BuiltinType::Long:
case BuiltinType::LongLong:
case BuiltinType::Int128:
case BuiltinType::WChar_S:
return true;
default:
return false;
}
}
static bool c_is_unsigned_integer(Context *c, QualType qt) {
const Type *c_type = qual_type_canon(qt);
if (c_type->getTypeClass() != Type::Builtin)
return false;
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(c_type);
switch (builtin_ty->getKind()) {
case BuiltinType::Char_U:
case BuiltinType::UChar:
case BuiltinType::Char_S:
case BuiltinType::UShort:
case BuiltinType::UInt:
case BuiltinType::ULong:
case BuiltinType::ULongLong:
case BuiltinType::UInt128:
case BuiltinType::WChar_U:
return true;
default:
return false;
}
}
static bool c_is_builtin_type(Context *c, QualType qt, BuiltinType::Kind kind) {
const Type *c_type = qual_type_canon(qt);
if (c_type->getTypeClass() != Type::Builtin)
return false;
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(c_type);
return builtin_ty->getKind() == kind;
}
static bool c_is_float(Context *c, QualType qt) {
const Type *c_type = qt.getTypePtr();
if (c_type->getTypeClass() != Type::Builtin)
return false;
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(c_type);
switch (builtin_ty->getKind()) {
case BuiltinType::Half:
case BuiltinType::Float:
case BuiltinType::Double:
case BuiltinType::Float128:
case BuiltinType::LongDouble:
return true;
default:
return false;
}
}
static bool qual_type_has_wrapping_overflow(Context *c, QualType qt) {
if (c_is_signed_integer(c, qt) || c_is_float(c, qt)) {
// float and signed integer overflow is undefined behavior.
return false;
} else {
// unsigned integer overflow wraps around.
return true;
}
}
static bool type_is_opaque(Context *c, const Type *ty, const SourceLocation &source_loc) {
switch (ty->getTypeClass()) {
case Type::Builtin: {
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
return builtin_ty->getKind() == BuiltinType::Void;
}
case Type::Record: {
const RecordType *record_ty = static_cast<const RecordType*>(ty);
return record_ty->getDecl()->getDefinition() == nullptr;
}
case Type::Elaborated: {
const ElaboratedType *elaborated_ty = static_cast<const ElaboratedType*>(ty);
return type_is_opaque(c, elaborated_ty->getNamedType().getTypePtr(), source_loc);
}
case Type::Typedef: {
const TypedefType *typedef_ty = static_cast<const TypedefType*>(ty);
const TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
return type_is_opaque(c, typedef_decl->getUnderlyingType().getTypePtr(), source_loc);
}
default:
return false;
}
}
static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &source_loc) {
switch (ty->getTypeClass()) {
case Type::Builtin:
{
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
switch (builtin_ty->getKind()) {
case BuiltinType::Void:
return trans_create_node_symbol_str(c, "c_void");
case BuiltinType::Bool:
return trans_create_node_symbol_str(c, "bool");
case BuiltinType::Char_U:
case BuiltinType::UChar:
case BuiltinType::Char_S:
case BuiltinType::Char8:
return trans_create_node_symbol_str(c, "u8");
case BuiltinType::SChar:
return trans_create_node_symbol_str(c, "i8");
case BuiltinType::UShort:
return trans_create_node_symbol_str(c, "c_ushort");
case BuiltinType::UInt:
return trans_create_node_symbol_str(c, "c_uint");
case BuiltinType::ULong:
return trans_create_node_symbol_str(c, "c_ulong");
case BuiltinType::ULongLong:
return trans_create_node_symbol_str(c, "c_ulonglong");
case BuiltinType::Short:
return trans_create_node_symbol_str(c, "c_short");
case BuiltinType::Int:
return trans_create_node_symbol_str(c, "c_int");
case BuiltinType::Long:
return trans_create_node_symbol_str(c, "c_long");
case BuiltinType::LongLong:
return trans_create_node_symbol_str(c, "c_longlong");
case BuiltinType::UInt128:
return trans_create_node_symbol_str(c, "u128");
case BuiltinType::Int128:
return trans_create_node_symbol_str(c, "i128");
case BuiltinType::Float:
return trans_create_node_symbol_str(c, "f32");
case BuiltinType::Double:
return trans_create_node_symbol_str(c, "f64");
case BuiltinType::Float128:
return trans_create_node_symbol_str(c, "f128");
case BuiltinType::Float16:
return trans_create_node_symbol_str(c, "f16");
case BuiltinType::LongDouble:
return trans_create_node_symbol_str(c, "c_longdouble");
case BuiltinType::WChar_U:
case BuiltinType::Char16:
case BuiltinType::Char32:
case BuiltinType::WChar_S:
case BuiltinType::Half:
case BuiltinType::NullPtr:
case BuiltinType::ObjCId:
case BuiltinType::ObjCClass:
case BuiltinType::ObjCSel:
case BuiltinType::OMPArraySection:
case BuiltinType::Dependent:
case BuiltinType::Overload:
case BuiltinType::BoundMember:
case BuiltinType::PseudoObject:
case BuiltinType::UnknownAny:
case BuiltinType::BuiltinFn:
case BuiltinType::ARCUnbridgedCast:
case BuiltinType::ShortAccum:
case BuiltinType::Accum:
case BuiltinType::LongAccum:
case BuiltinType::UShortAccum:
case BuiltinType::UAccum:
case BuiltinType::ULongAccum:
case BuiltinType::OCLImage1dRO:
case BuiltinType::OCLImage1dArrayRO:
case BuiltinType::OCLImage1dBufferRO:
case BuiltinType::OCLImage2dRO:
case BuiltinType::OCLImage2dArrayRO:
case BuiltinType::OCLImage2dDepthRO:
case BuiltinType::OCLImage2dArrayDepthRO:
case BuiltinType::OCLImage2dMSAARO:
case BuiltinType::OCLImage2dArrayMSAARO:
case BuiltinType::OCLImage2dMSAADepthRO:
case BuiltinType::OCLImage2dArrayMSAADepthRO:
case BuiltinType::OCLImage3dRO:
case BuiltinType::OCLImage1dWO:
case BuiltinType::OCLImage1dArrayWO:
case BuiltinType::OCLImage1dBufferWO:
case BuiltinType::OCLImage2dWO:
case BuiltinType::OCLImage2dArrayWO:
case BuiltinType::OCLImage2dDepthWO:
case BuiltinType::OCLImage2dArrayDepthWO:
case BuiltinType::OCLImage2dMSAAWO:
case BuiltinType::OCLImage2dArrayMSAAWO:
case BuiltinType::OCLImage2dMSAADepthWO:
case BuiltinType::OCLImage2dArrayMSAADepthWO:
case BuiltinType::OCLImage3dWO:
case BuiltinType::OCLImage1dRW:
case BuiltinType::OCLImage1dArrayRW:
case BuiltinType::OCLImage1dBufferRW:
case BuiltinType::OCLImage2dRW:
case BuiltinType::OCLImage2dArrayRW:
case BuiltinType::OCLImage2dDepthRW:
case BuiltinType::OCLImage2dArrayDepthRW:
case BuiltinType::OCLImage2dMSAARW:
case BuiltinType::OCLImage2dArrayMSAARW:
case BuiltinType::OCLImage2dMSAADepthRW:
case BuiltinType::OCLImage2dArrayMSAADepthRW:
case BuiltinType::OCLImage3dRW:
case BuiltinType::OCLSampler:
case BuiltinType::OCLEvent:
case BuiltinType::OCLClkEvent:
case BuiltinType::OCLQueue:
case BuiltinType::OCLReserveID:
case BuiltinType::ShortFract:
case BuiltinType::Fract:
case BuiltinType::LongFract:
case BuiltinType::UShortFract:
case BuiltinType::UFract:
case BuiltinType::ULongFract:
case BuiltinType::SatShortAccum:
case BuiltinType::SatAccum:
case BuiltinType::SatLongAccum:
case BuiltinType::SatUShortAccum:
case BuiltinType::SatUAccum:
case BuiltinType::SatULongAccum:
case BuiltinType::SatShortFract:
case BuiltinType::SatFract:
case BuiltinType::SatLongFract:
case BuiltinType::SatUShortFract:
case BuiltinType::SatUFract:
case BuiltinType::SatULongFract:
emit_warning(c, source_loc, "unsupported builtin type");
return nullptr;
}
break;
}
case Type::Pointer:
{
const PointerType *pointer_ty = static_cast<const PointerType*>(ty);
QualType child_qt = pointer_ty->getPointeeType();
AstNode *child_node = trans_qual_type(c, child_qt, source_loc);
if (child_node == nullptr) {
emit_warning(c, source_loc, "pointer to unsupported type");
return nullptr;
}
if (qual_type_child_is_fn_proto(child_qt)) {
return trans_create_node_prefix_op(c, PrefixOpOptional, child_node);
}
PtrLen ptr_len = type_is_opaque(c, child_qt.getTypePtr(), source_loc) ? PtrLenSingle : PtrLenUnknown;
AstNode *pointer_node = trans_create_node_ptr_type(c, child_qt.isConstQualified(),
child_qt.isVolatileQualified(), child_node, ptr_len);
return trans_create_node_prefix_op(c, PrefixOpOptional, pointer_node);
}
case Type::Typedef:
{
const TypedefType *typedef_ty = static_cast<const TypedefType*>(ty);
const TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
return resolve_typedef_decl(c, typedef_decl);
}
case Type::Elaborated:
{
const ElaboratedType *elaborated_ty = static_cast<const ElaboratedType*>(ty);
switch (elaborated_ty->getKeyword()) {
case ETK_Struct:
case ETK_Enum:
case ETK_Union:
return trans_qual_type(c, elaborated_ty->getNamedType(), source_loc);
case ETK_Interface:
case ETK_Class:
case ETK_Typename:
case ETK_None:
emit_warning(c, source_loc, "unsupported elaborated type");
return nullptr;
}
}
case Type::FunctionProto:
{
const FunctionProtoType *fn_proto_ty = static_cast<const FunctionProtoType*>(ty);
AstNode *proto_node = trans_create_node(c, NodeTypeFnProto);
switch (fn_proto_ty->getCallConv()) {
case CC_C: // __attribute__((cdecl))
proto_node->data.fn_proto.cc = CallingConventionC;
proto_node->data.fn_proto.is_extern = true;
break;
case CC_X86StdCall: // __attribute__((stdcall))
proto_node->data.fn_proto.cc = CallingConventionStdcall;
break;
case CC_X86FastCall: // __attribute__((fastcall))
emit_warning(c, source_loc, "unsupported calling convention: x86 fastcall");
return nullptr;
case CC_X86ThisCall: // __attribute__((thiscall))
emit_warning(c, source_loc, "unsupported calling convention: x86 thiscall");
return nullptr;
case CC_X86VectorCall: // __attribute__((vectorcall))
emit_warning(c, source_loc, "unsupported calling convention: x86 vectorcall");
return nullptr;
case CC_X86Pascal: // __attribute__((pascal))
emit_warning(c, source_loc, "unsupported calling convention: x86 pascal");
return nullptr;
case CC_Win64: // __attribute__((ms_abi))
emit_warning(c, source_loc, "unsupported calling convention: win64");
return nullptr;
case CC_X86_64SysV: // __attribute__((sysv_abi))
emit_warning(c, source_loc, "unsupported calling convention: x86 64sysv");
return nullptr;
case CC_X86RegCall:
emit_warning(c, source_loc, "unsupported calling convention: x86 reg");
return nullptr;
case CC_AAPCS: // __attribute__((pcs("aapcs")))
emit_warning(c, source_loc, "unsupported calling convention: aapcs");
return nullptr;
case CC_AAPCS_VFP: // __attribute__((pcs("aapcs-vfp")))
emit_warning(c, source_loc, "unsupported calling convention: aapcs-vfp");
return nullptr;
case CC_IntelOclBicc: // __attribute__((intel_ocl_bicc))
emit_warning(c, source_loc, "unsupported calling convention: intel_ocl_bicc");
return nullptr;
case CC_SpirFunction: // default for OpenCL functions on SPIR target
emit_warning(c, source_loc, "unsupported calling convention: SPIR function");
return nullptr;