This repository has been archived by the owner on Jun 20, 2019. It is now read-only.
/
d-codegen.cc
3639 lines (2980 loc) · 95.4 KB
/
d-codegen.cc
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
// d-codegen.cc -- D frontend for GCC.
// Copyright (C) 2011-2013 Free Software Foundation, Inc.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "d-system.h"
#include "d-lang.h"
#include "d-codegen.h"
#include "attrib.h"
#include "template.h"
#include "init.h"
#include "id.h"
#include "module.h"
#include "dfrontend/target.h"
Module *current_module_decl;
IRState *current_irstate;
// Return the DECL_CONTEXT for symbol DSYM.
tree
d_decl_context (Dsymbol *dsym)
{
Dsymbol *parent = dsym;
while ((parent = parent->toParent2()))
{
// Nested functions.
if (parent->isFuncDeclaration())
return parent->toSymbol()->Stree;
// Methods of classes or structs.
AggregateDeclaration *ad = parent->isAggregateDeclaration();
if (ad != NULL)
{
tree context = ad->type->toCtype();
// Want the underlying RECORD_TYPE.
if (ad->isClassDeclaration())
context = TREE_TYPE (context);
return context;
}
// We've reached the top-level module namespace.
// Set DECL_CONTEXT as the NAMESPACE_DECL of the enclosing module,
// but only for extern(D) symbols.
if (parent->isModule())
{
Declaration *decl = dsym->isDeclaration();
if (decl != NULL && decl->linkage != LINKd)
return NULL_TREE;
return parent->toImport()->Stree;
}
}
return NULL_TREE;
}
// Add local variable VD into the current body of function fd.
void
build_local_var (VarDeclaration *vd, FuncDeclaration *fd)
{
gcc_assert (!vd->isDataseg() && !vd->isMember());
Symbol *sym = vd->toSymbol();
tree var = sym->Stree;
gcc_assert (!TREE_STATIC (var));
set_input_location (vd->loc);
d_pushdecl (var);
DECL_CONTEXT (var) = fd->toSymbol()->Stree;
// Compiler generated symbols
if (vd == fd->vresult || vd == fd->v_argptr || vd == fd->v_arguments_var)
DECL_ARTIFICIAL (var) = 1;
if (sym->SframeField)
{
// Fixes debugging local variables.
SET_DECL_VALUE_EXPR (var, get_decl_tree (vd, fd));
DECL_HAS_VALUE_EXPR_P (var) = 1;
}
}
// Return an unnamed local temporary of type TYPE.
tree
build_local_temp (tree type)
{
tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
DECL_CONTEXT (decl) = current_function_decl;
DECL_ARTIFICIAL (decl) = 1;
DECL_IGNORED_P (decl) = 1;
d_pushdecl (decl);
return decl;
}
// Return an undeclared local temporary of type TYPE
// for use with BIND_EXPR.
tree
create_temporary_var (tree type)
{
tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
DECL_CONTEXT (decl) = current_function_decl;
DECL_ARTIFICIAL (decl) = 1;
DECL_IGNORED_P (decl) = 1;
layout_decl (decl, 0);
return decl;
}
// Return an undeclared local temporary OUT_VAR initialised
// with result of expression EXP.
tree
maybe_temporary_var (tree exp, tree *out_var)
{
tree t = exp;
// Get the base component.
while (TREE_CODE (t) == COMPONENT_REF)
t = TREE_OPERAND (t, 0);
if (!DECL_P (t) && !REFERENCE_CLASS_P (t))
{
*out_var = create_temporary_var (TREE_TYPE (exp));
DECL_INITIAL (*out_var) = exp;
return *out_var;
}
else
{
*out_var = NULL_TREE;
return exp;
}
}
// Emit an INIT_EXPR for decl DECL.
void
expand_decl (tree decl)
{
// Nothing, d_pushdecl will add decl to a BIND_EXPR
if (DECL_INITIAL (decl))
{
tree exp = build_vinit (decl, DECL_INITIAL (decl));
current_irstate->addExp (exp);
DECL_INITIAL (decl) = NULL_TREE;
}
}
// Return the correct decl to be used for variable DECL accessed from
// function FUNC. Could be a VAR_DECL, or a FIELD_DECL from a closure.
tree
get_decl_tree (Declaration *decl, FuncDeclaration *func)
{
VarDeclaration *vd = decl->isVarDeclaration();
if (vd)
{
Symbol *vsym = vd->toSymbol();
if (vsym->SnamedResult != NULL_TREE)
{
// Get the named return value.
gcc_assert (TREE_CODE (vsym->SnamedResult) == RESULT_DECL);
return vsym->SnamedResult;
}
else if (vsym->SframeField != NULL_TREE)
{
// Get the closure holding the var decl.
FuncDeclaration *parent = vd->toParent2()->isFuncDeclaration();
tree frame_ref = get_framedecl (func, parent);
return component_ref (build_deref (frame_ref), vsym->SframeField);
}
}
// Static var or auto var that the back end will handle for us
return decl->toSymbol()->Stree;
}
// Return expression EXP, whose type has been converted to TYPE.
tree
d_convert (tree type, tree exp)
{
// Check this first before passing to lang_dtype.
if (error_operand_p (type) || error_operand_p (exp))
return error_mark_node;
Type *totype = lang_dtype (type);
Type *etype = lang_dtype (TREE_TYPE (exp));
if (totype && etype)
return convert_expr (exp, etype, totype);
return convert (type, exp);
}
// Return expression EXP, whose type has been convert from ETYPE to TOTYPE.
tree
convert_expr (tree exp, Type *etype, Type *totype)
{
tree result = NULL_TREE;
gcc_assert (etype && totype);
Type *ebtype = etype->toBasetype();
Type *tbtype = totype->toBasetype();
if (d_types_same (etype, totype))
return exp;
if (error_operand_p (exp))
return exp;
switch (ebtype->ty)
{
case Tdelegate:
if (tbtype->ty == Tdelegate)
{
exp = maybe_make_temp (exp);
return build_delegate_cst (delegate_method (exp), delegate_object (exp), totype);
}
else if (tbtype->ty == Tpointer)
{
// The front-end converts <delegate>.ptr to cast (void *)<delegate>.
// Maybe should only allow void* ?
exp = delegate_object (exp);
}
else
{
error ("can't convert a delegate expression to %s", totype->toChars());
return error_mark_node;
}
break;
case Tstruct:
if (tbtype->ty == Tstruct)
{
if (totype->size() == etype->size())
{
// Allowed to cast to structs with same type size.
result = build_vconvert (totype->toCtype(), exp);
}
else if (tbtype->ty == Taarray)
{
tbtype = ((TypeAArray *) tbtype)->getImpl()->type;
return convert_expr (exp, etype, tbtype);
}
else
{
error ("can't convert struct %s to %s", etype->toChars(), totype->toChars());
return error_mark_node;
}
}
// else, default conversion, which should produce an error
break;
case Tclass:
if (tbtype->ty == Tclass)
{
ClassDeclaration *cdfrom = tbtype->isClassHandle();
ClassDeclaration *cdto = ebtype->isClassHandle();
int offset;
if (cdfrom->isBaseOf (cdto, &offset) && offset != OFFSET_RUNTIME)
{
// Casting up the inheritance tree: Don't do anything special.
// Cast to an implemented interface: Handle at compile time.
if (offset)
{
tree t = totype->toCtype();
exp = maybe_make_temp (exp);
return build3 (COND_EXPR, t,
build_boolop (NE_EXPR, exp, null_pointer_node),
build_nop (t, build_offset (exp, size_int (offset))),
build_nop (t, null_pointer_node));
}
// d_convert will make a no-op cast
break;
}
// More cases for no-op cast
if (cdfrom == cdto)
break;
if (cdfrom->cpp && cdto->cpp)
break;
// Casting from a C++ interface to a class/non-C++ interface
// always results in null as there is no runtime information,
// and no way one can derive from the other.
if (cdto->isCOMclass() || cdfrom->cpp != cdto->cpp)
{
warning (OPT_Wcast_result, "cast to %s will produce null result", totype->toChars());
result = d_convert (totype->toCtype(), null_pointer_node);
// Make sure the expression is still evaluated if necessary
if (TREE_SIDE_EFFECTS (exp))
result = compound_expr (exp, result);
return result;
}
// The offset can only be determined at runtime, do dynamic cast
tree args[2];
args[0] = exp;
args[1] = build_address (cdfrom->toSymbol()->Stree);
return build_libcall (cdto->isInterfaceDeclaration()
? LIBCALL_INTERFACE_CAST : LIBCALL_DYNAMIC_CAST, 2, args);
}
// else default conversion
break;
case Tsarray:
if (tbtype->ty == Tpointer)
{
result = build_nop (totype->toCtype(), build_address (exp));
}
else if (tbtype->ty == Tarray)
{
dinteger_t dim = ((TypeSArray *) ebtype)->dim->toInteger();
dinteger_t esize = ebtype->nextOf()->size();
dinteger_t tsize = tbtype->nextOf()->size();
tree ptrtype = tbtype->nextOf()->pointerTo()->toCtype();
if ((dim * esize) % tsize != 0)
{
error ("cannot cast %s to %s since sizes don't line up",
etype->toChars(), totype->toChars());
return error_mark_node;
}
dim = (dim * esize) / tsize;
// Assumes casting to dynamic array of same type or void
return d_array_value (totype->toCtype(), size_int (dim),
build_nop (ptrtype, build_address (exp)));
}
else if (tbtype->ty == Tsarray)
{
// D apparently allows casting a static array to any static array type
return build_vconvert (totype->toCtype(), exp);
}
else if (tbtype->ty == Tstruct)
{
// And allows casting a static array to any struct type too.
// %% type sizes should have already been checked by the frontend.
gcc_assert (totype->size() == etype->size());
result = build_vconvert (totype->toCtype(), exp);
}
else
{
error ("cannot cast expression of type %s to type %s",
etype->toChars(), totype->toChars());
return error_mark_node;
}
break;
case Tarray:
if (tbtype->ty == Tpointer)
{
return d_convert (totype->toCtype(), d_array_ptr (exp));
}
else if (tbtype->ty == Tarray)
{
// assume tvoid->size() == 1
Type *src_elem_type = ebtype->nextOf()->toBasetype();
Type *dst_elem_type = tbtype->nextOf()->toBasetype();
d_uns64 sz_src = src_elem_type->size();
d_uns64 sz_dst = dst_elem_type->size();
if (sz_src == sz_dst)
{
// Convert from void[] or elements are the same size -- don't change length
return build_vconvert (totype->toCtype(), exp);
}
else
{
unsigned mult = 1;
tree args[3];
args[0] = build_integer_cst (sz_dst, Type::tsize_t->toCtype());
args[1] = build_integer_cst (sz_src * mult, Type::tsize_t->toCtype());
args[2] = exp;
return build_libcall (LIBCALL_ARRAYCAST, 3, args, totype->toCtype());
}
}
else if (tbtype->ty == Tsarray)
{
// %% Strings are treated as dynamic arrays D2.
if (ebtype->isString() && tbtype->isString())
return indirect_ref (totype->toCtype(), d_array_ptr (exp));
}
else
{
error ("cannot cast expression of type %s to %s",
etype->toChars(), totype->toChars());
return error_mark_node;
}
break;
case Taarray:
if (tbtype->ty == Taarray)
return build_vconvert (totype->toCtype(), exp);
else if (tbtype->ty == Tstruct)
{
ebtype = ((TypeAArray *) ebtype)->getImpl()->type;
return convert_expr (exp, ebtype, totype);
}
// Can convert associative arrays to void pointers.
else if (tbtype == Type::tvoidptr)
return build_vconvert (totype->toCtype(), exp);
// else, default conversion, which should product an error
break;
case Tpointer:
// Can convert void pointers to associative arrays too...
if (tbtype->ty == Taarray && ebtype == Type::tvoidptr)
return build_vconvert (totype->toCtype(), exp);
break;
case Tnull:
if (tbtype->ty == Tarray)
{
tree ptrtype = tbtype->nextOf()->pointerTo()->toCtype();
return d_array_value (totype->toCtype(), size_int (0),
build_nop (ptrtype, exp));
}
break;
case Tvector:
if (tbtype->ty == Tsarray)
{
if (tbtype->size() == ebtype->size())
return build_vconvert (totype->toCtype(), exp);
}
break;
default:
exp = fold_convert (etype->toCtype(), exp);
gcc_assert (TREE_CODE (exp) != STRING_CST);
break;
}
return result ? result :
convert (totype->toCtype(), exp);
}
// Apply semantics of assignment to a values of type TOTYPE to EXPR
// (e.g., pointer = array -> pointer = &array[0])
// Return a TREE representation of EXPR implictly converted to TOTYPE
// for use in assignment expressions MODIFY_EXPR, INIT_EXPR...
tree
convert_for_assignment (tree expr, Type *etype, Type *totype)
{
Type *ebtype = etype->toBasetype();
Type *tbtype = totype->toBasetype();
// Assuming this only has to handle converting a non Tsarray type to
// arbitrarily dimensioned Tsarrays.
if (tbtype->ty == Tsarray)
{
Type *telem = tbtype->nextOf()->baseElemOf();
if (d_types_same (telem, ebtype))
{
// %% what about implicit converions...?
TypeSArray *sa_type = (TypeSArray *) tbtype;
uinteger_t count = sa_type->dim->toUInteger();
tree ctor = build_constructor (totype->toCtype(), NULL);
if (count)
{
vec<constructor_elt, va_gc> *ce = NULL;
tree index = build2 (RANGE_EXPR, Type::tsize_t->toCtype(),
integer_zero_node, build_integer_cst (count - 1));
tree value = convert_for_assignment (expr, etype, sa_type->next);
// Can't use VAR_DECLs in CONSTRUCTORS.
if (TREE_CODE (value) == VAR_DECL)
{
value = DECL_INITIAL (value);
gcc_assert (value);
}
CONSTRUCTOR_APPEND_ELT (ce, index, value);
CONSTRUCTOR_ELTS (ctor) = ce;
}
TREE_READONLY (ctor) = 1;
TREE_CONSTANT (ctor) = 1;
return ctor;
}
}
if (tbtype->ty == Tstruct && ebtype->isintegral())
{
// D Front end uses IntegerExp (0) to mean zero-init a structure.
// Use memset to fill struct.
if (integer_zerop (expr))
{
StructDeclaration *sd = ((TypeStruct *) tbtype)->sym;
tree var = build_local_temp (totype->toCtype());
tree init = d_build_call_nary (builtin_decl_explicit (BUILT_IN_MEMSET), 3,
build_address (var), expr,
size_int (sd->structsize));
return compound_expr (init, var);
}
else
gcc_unreachable();
}
return convert_expr (expr, etype, totype);
}
// Return a TREE representation of EXPR converted to represent parameter type ARG.
tree
convert_for_argument (tree exp_tree, Expression *expr, Parameter *arg)
{
if (arg_reference_p (arg))
{
// Front-end already sometimes automatically takes the address
if (expr->op != TOKaddress && expr->op != TOKsymoff && expr->op != TOKadd)
exp_tree = build_address (exp_tree);
return convert (type_passed_as (arg), exp_tree);
}
// Lazy arguments: expr should already be a delegate
return exp_tree;
}
// Perform default promotions for data used in expressions.
// Arrays and functions are converted to pointers;
// enumeral types or short or char, to int.
// In addition, manifest constants symbols are replaced by their values.
// Return truth-value conversion of expression EXPR from value type TYPE.
tree
convert_for_condition (tree expr, Type *type)
{
tree result = NULL_TREE;
tree obj, func, tmp;
switch (type->toBasetype()->ty)
{
case Taarray:
// Shouldn't this be...
// result = _aaLen (&expr);
result = component_ref (expr, TYPE_FIELDS (TREE_TYPE (expr)));
break;
case Tarray:
// Checks (length || ptr) (i.e ary !is null)
tmp = maybe_make_temp (expr);
obj = delegate_object (tmp);
func = delegate_method (tmp);
if (TYPE_MODE (TREE_TYPE (obj)) == TYPE_MODE (TREE_TYPE (func)))
{
result = build2 (BIT_IOR_EXPR, TREE_TYPE (obj), obj,
d_convert (TREE_TYPE (obj), func));
}
else
{
obj = d_truthvalue_conversion (obj);
func = d_truthvalue_conversion (func);
// probably not worth using TRUTH_OROR ...
result = build2 (TRUTH_OR_EXPR, TREE_TYPE (obj), obj, func);
}
break;
case Tdelegate:
// Checks (function || object), but what good is it
// if there is a null function pointer?
if (D_METHOD_CALL_EXPR (expr))
extract_from_method_call (expr, obj, func);
else
{
tmp = maybe_make_temp (expr);
obj = delegate_object (tmp);
func = delegate_method (tmp);
}
obj = d_truthvalue_conversion (obj);
func = d_truthvalue_conversion (func);
// probably not worth using TRUTH_ORIF ...
result = build2 (BIT_IOR_EXPR, TREE_TYPE (obj), obj, func);
break;
default:
result = expr;
break;
}
return d_truthvalue_conversion (result);
}
// Convert EXP to a dynamic array.
// EXP must be a static array or dynamic array.
tree
d_array_convert (Expression *exp)
{
TY ty = exp->type->toBasetype()->ty;
if (ty == Tarray)
return exp->toElem (current_irstate);
else if (ty == Tsarray)
{
Type *totype = exp->type->toBasetype()->nextOf()->arrayOf();
return convert_expr (exp->toElem (current_irstate), exp->type, totype);
}
// Invalid type passed.
gcc_unreachable();
}
// Return TRUE if declaration DECL is a reference type.
bool
decl_reference_p (Declaration *decl)
{
Type *base_type = decl->type->toBasetype();
if (base_type->ty == Treference)
return true;
if (decl->storage_class & (STCout | STCref))
return true;
return false;
}
// Returns the real type for declaration DECL.
// Reference decls are converted to reference-to-types.
// Lazy decls are converted into delegates.
tree
declaration_type (Declaration *decl)
{
tree decl_type = decl->type->toCtype();
if (decl_reference_p (decl))
decl_type = build_reference_type (decl_type);
else if (decl->storage_class & STClazy)
{
TypeFunction *tf = new TypeFunction (NULL, decl->type, false, LINKd);
TypeDelegate *t = new TypeDelegate (tf);
decl_type = t->merge()->toCtype();
}
else if (decl->isThisDeclaration())
decl_type = insert_type_modifiers (decl_type, MODconst);
return decl_type;
}
// These should match the Declaration versions above
// Return TRUE if parameter ARG is a reference type.
bool
arg_reference_p (Parameter *arg)
{
Type *base_type = arg->type->toBasetype();
if (base_type->ty == Treference)
return true;
if (arg->storageClass & (STCout | STCref))
return true;
return false;
}
// Returns the real type for parameter ARG.
// Reference parameters are converted to reference-to-types.
// Lazy parameters are converted into delegates.
tree
type_passed_as (Parameter *arg)
{
tree arg_type = arg->type->toCtype();
if (arg_reference_p (arg))
arg_type = build_reference_type (arg_type);
else if (arg->storageClass & STClazy)
{
TypeFunction *tf = new TypeFunction (NULL, arg->type, false, LINKd);
TypeDelegate *t = new TypeDelegate (tf);
arg_type = t->merge()->toCtype();
}
return arg_type;
}
// Returns an array of type TYPE_NODE which has SIZE number of elements.
tree
d_array_type (Type *d_type, uinteger_t size)
{
tree index_type_node;
tree type_node = d_type->toCtype();
if (size > 0)
{
index_type_node = size_int (size - 1);
index_type_node = build_index_type (index_type_node);
}
else
index_type_node = build_range_type (sizetype, size_zero_node,
NULL_TREE);
tree array_type = build_array_type (type_node, index_type_node);
if (size == 0)
{
TYPE_SIZE (array_type) = bitsize_zero_node;
TYPE_SIZE_UNIT (array_type) = size_zero_node;
}
return array_type;
}
// Appends the type attribute ATTRNAME with value VALUE onto type TYPE.
tree
insert_type_attribute (tree type, const char *attrname, tree value)
{
tree attrib;
tree ident = get_identifier (attrname);
if (value)
value = tree_cons (NULL_TREE, value, NULL_TREE);
// types built by functions in tree.c need to be treated as immutabl
if (!TYPE_ATTRIBUTES (type))
type = build_variant_type_copy (type);
attrib = tree_cons (ident, value, NULL_TREE);
TYPE_ATTRIBUTES (type) = merge_attributes (TYPE_ATTRIBUTES (type), attrib);
return type;
}
// Appends the decl attribute ATTRNAME with value VALUE onto decl DECL.
void
insert_decl_attribute (tree decl, const char *attrname, tree value)
{
tree attrib;
tree ident = get_identifier (attrname);
if (value)
value = tree_cons (NULL_TREE, value, NULL_TREE);
attrib = tree_cons (ident, value, NULL_TREE);
DECL_ATTRIBUTES (decl) = merge_attributes (DECL_ATTRIBUTES (decl), attrib);
}
bool
d_attribute_p (const char* name)
{
static StringTable* table;
if(table == NULL)
{
// Build the table of attributes exposed to the language.
// Common and format attributes are kept internal.
size_t n = 0;
table = new StringTable();
for (const attribute_spec *p = lang_hooks.attribute_table; p->name; p++)
n++;
for (const attribute_spec *p = targetm.attribute_table; p->name; p++)
n++;
if(n != 0)
{
table->_init(n);
for (const attribute_spec *p = lang_hooks.attribute_table; p->name; p++)
table->insert(p->name, strlen(p->name));
for (const attribute_spec *p = targetm.attribute_table; p->name; p++)
table->insert(p->name, strlen(p->name));
}
}
return table->lookup(name, strlen(name)) != NULL;
}
// Return chain of all GCC attributes found in list IN_ATTRS.
tree
build_attributes (Expressions *in_attrs)
{
if (!in_attrs)
return NULL_TREE;
expandTuples(in_attrs);
tree out_attrs = NULL_TREE;
for (size_t i = 0; i < in_attrs->dim; i++)
{
Expression *attr = (*in_attrs)[i]->optimize (WANTexpand);
Dsymbol *sym = attr->type->toDsymbol (0);
if (!sym)
continue;
Dsymbol *mod = (Dsymbol*) sym->getModule();
if (!(strcmp(mod->toChars(), "attribute") == 0
&& mod->parent != NULL
&& strcmp(mod->parent->toChars(), "gcc") == 0
&& !mod->parent->parent))
continue;
if (attr->op == TOKcall)
attr = attr->ctfeInterpret();
gcc_assert(attr->op == TOKstructliteral);
Expressions *elem = ((StructLiteralExp*) attr)->elements;
if ((*elem)[0]->op == TOKnull)
{
error ("expected string attribute, not null");
return error_mark_node;
}
gcc_assert((*elem)[0]->op == TOKstring);
StringExp *nameExp = (StringExp*) (*elem)[0];
gcc_assert(nameExp->sz == 1);
const char* name = (const char*) nameExp->string;
if (!d_attribute_p (name))
{
error ("unknown attribute %s", name);
return error_mark_node;
}
tree args = NULL_TREE;
for (size_t j = 1; j < elem->dim; j++)
{
Expression *ae = (*elem)[j];
tree aet;
if (ae->op == TOKstring && ((StringExp *) ae)->sz == 1)
{
StringExp *s = (StringExp *) ae;
aet = build_string (s->len, (const char *) s->string);
}
else
aet = ae->toElem (current_irstate);
args = chainon (args, build_tree_list (0, aet));
}
tree list = build_tree_list (get_identifier (name), args);
out_attrs = chainon (out_attrs, list);
}
return out_attrs;
}
// Return qualified type variant of TYPE determined by modifier value MOD.
tree
insert_type_modifiers (tree type, unsigned mod)
{
int quals = 0;
gcc_assert (type);
switch (mod)
{
case 0:
break;
case MODconst:
case MODwild:
case MODwildconst:
case MODimmutable:
quals |= TYPE_QUAL_CONST;
break;
case MODshared:
quals |= TYPE_QUAL_VOLATILE;
break;
case MODshared | MODconst:
case MODshared | MODwild:
case MODshared | MODwildconst:
quals |= TYPE_QUAL_CONST;
quals |= TYPE_QUAL_VOLATILE;
break;
default:
gcc_unreachable();
}
return build_qualified_type (type, quals);
}
// Build INTEGER_CST of type TYPE with the value VALUE.
tree
build_integer_cst (dinteger_t value, tree type)
{
// The type is error_mark_node, we can't do anything.
if (error_operand_p (type))
return type;
return build_int_cst_type (type, value);
}
// Build REAL_CST of type TOTYPE with the value VALUE.
tree
build_float_cst (const real_t& value, Type *totype)
{
real_t new_value;
TypeBasic *tb = totype->isTypeBasic();
gcc_assert (tb != NULL);
tree type_node = tb->toCtype();
real_convert (&new_value.rv(), TYPE_MODE (type_node), &value.rv());
// Value grew as a result of the conversion. %% precision bug ??
// For now just revert back to original.
if (new_value > value)
new_value = value;
return build_real (type_node, new_value.rv());
}
// Returns the .length component from the D dynamic array EXP.
tree
d_array_length (tree exp)
{
// backend will ICE otherwise
if (error_operand_p (exp))
return exp;
// Get the backend type for the array and pick out the array
// length field (assumed to be the first field.)
tree len_field = TYPE_FIELDS (TREE_TYPE (exp));
return component_ref (exp, len_field);
}
// Returns the .ptr component from the D dynamic array EXP.
tree
d_array_ptr (tree exp)
{
// backend will ICE otherwise
if (error_operand_p (exp))
return exp;
// Get the backend type for the array and pick out the array
// data pointer field (assumed to be the second field.)
tree ptr_field = TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp)));
return component_ref (exp, ptr_field);
}
// Returns a constructor for D dynamic array type TYPE of .length LEN
// and .ptr pointing to DATA.