forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.dd
2130 lines (1725 loc) · 66.9 KB
/
expression.dd
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
Ddoc
$(SPEC_S Expressions,
$(P C and C++ programmers will find the D expressions very familiar,
with a few interesting additions.
)
$(P Expressions are used to compute values with a resulting type.
These values can then be assigned,
tested, or ignored. Expressions can also have side effects.
)
$(H3 $(LNAME2 order-of-evaluation, Order Of Evaluation))
$(P Binary expressions and function arguments are evaluated in strictly
left-to-right order. This is similar to Java but different to C and C++,
where the evaluation order is unspecified.
Thus, the following code is valid and well defined.
-------------
import std.conv;
int i = 0;
(i = 2) = ++i * i++ + i;
assert(i == 13); // left to right evaluation of side effects
assert(text(++i, ++i) == "1415"); // left to right evaluation of arguments
-------------
)
$(P But even though the order of evaluation is well defined, writing code that
depends on it is rarely recommended.
$(B Note that dmd currently does not comply with left to right evaluation of
function arguments and AssignExpression.)
)
$(H3 $(LEGACY_LNAME2 Expression, expression, Expressions))
$(GRAMMAR
$(GNAME Expression):
$(I CommaExpression)
$(GNAME CommaExpression):
$(GLINK AssignExpression)
$(GLINK AssignExpression) $(D ,) $(I CommaExpression)
)
$(P The left operand of the $(D ,) is evaluated, then the right operand
is evaluated. The type of the expression is the type of the right
operand, and the result is the result of the right operand.
)
$(H3 Assign Expressions)
$(GRAMMAR
$(GNAME AssignExpression):
$(GLINK ConditionalExpression)
$(GLINK ConditionalExpression) $(D =) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D +=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D -=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D *=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D /=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D %=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D &=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D |=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D ^=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D ~=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D <)$(D <)$(D =) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D >>=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D >>>=) $(I AssignExpression)
$(GLINK ConditionalExpression) $(D ^^=) $(I AssignExpression)
)
$(P The right operand is implicitly converted to the type of the
left operand, and assigned to it. The result type is the type
of the left operand, and the result value is the value of the left
operand after the assignment.
)
$(P The left operand must be an lvalue.)
$(H4 Assignment Operator Expressions)
$(P Assignment operator expressions, such as:
--------------
a op= b
--------------
are semantically equivalent to:
--------------
a = cast(typeof(a))(a op b)
--------------
)
$(P except that:)
$(UL
$(LI operand $(D a) is only evaluated once)
$(LI overloading $(I op) uses a different function than overloading $(I op)= does)
$(LI the left operand of $(D >>>=) does not undergo integral promotions before shifting)
)
$(H3 Conditional Expressions)
$(GRAMMAR
$(GNAME ConditionalExpression):
$(GLINK OrOrExpression)
$(GLINK OrOrExpression) $(D ?) $(GLINK Expression) $(D :) $(I ConditionalExpression)
)
$(P The first expression is converted to $(D bool), and is evaluated.
)
$(P If it is $(D true), then the second expression is evaluated, and
its result is the result of the conditional expression.
)
$(P If it is $(D false), then the third expression is evaluated, and
its result is the result of the conditional expression.
)
$(P If either the second or third expressions are of type $(D void),
then the resulting type is $(D void). Otherwise, the second and third
expressions are implicitly converted to a common type which becomes
the result type of the conditional expression.
)
$(H3 OrOr Expressions)
$(GRAMMAR
$(GNAME OrOrExpression):
$(GLINK AndAndExpression)
$(I OrOrExpression) $(D ||) $(GLINK AndAndExpression)
)
$(P The result type of an $(I OrOrExpression) is $(D bool), unless the right operand
has type $(D void), when the result is type $(D void).
)
$(P The $(I OrOrExpression) evaluates its left operand.
)
$(P If the left operand, converted to type $(D bool), evaluates to
$(D true), then the right operand is not evaluated. If the result type of
the $(I OrOrExpression) is $(D bool) then the result of the
expression is $(D true).
)
$(P If the left operand is $(D false), then the right
operand is evaluated.
If the result type of
the $(I OrOrExpression) is $(D bool) then the result of the
expression is the right operand converted to type $(D bool).
)
$(H3 AndAnd Expressions)
$(GRAMMAR
$(GNAME AndAndExpression):
$(GLINK OrExpression)
$(I AndAndExpression) $(CODE_AMP)$(CODE_AMP) $(GLINK OrExpression)
)
$(P The result type of an $(I AndAndExpression) is $(D bool), unless the right operand
has type $(D void), when the result is type $(D void).
)
$(P The $(I AndAndExpression) evaluates its left operand.
)
$(P If the left operand, converted to type $(D bool), evaluates to
$(D false), then the right operand is not evaluated. If the result type of
the $(I AndAndExpression) is $(D bool) then the result of the
expression is $(D false).
)
$(P If the left operand is $(D true), then the right
operand is evaluated.
If the result type of
the $(I AndAndExpression) is $(D bool) then the result of the
expression is the right operand converted to type $(D bool).
)
$(H3 Bitwise Expressions)
$(P Bit wise expressions perform a bitwise operation on their operands.
Their operands must be integral types.
First, the default integral promotions are done. Then, the bitwise
operation is done.
)
$(H4 Or Expressions)
$(GRAMMAR
$(GNAME OrExpression):
$(GLINK XorExpression)
$(I OrExpression) $(D |) $(GLINK XorExpression)
)
$(P The operands are OR'd together.)
$(H4 Xor Expressions)
$(GRAMMAR
$(GNAME XorExpression):
$(GLINK AndExpression)
$(I XorExpression) $(D ^) $(GLINK AndExpression)
)
$(P The operands are XOR'd together.)
$(H4 And Expressions)
$(GRAMMAR
$(GNAME AndExpression):
$(GLINK CmpExpression)
$(I AndExpression) $(D &) $(GLINK CmpExpression)
)
$(P The operands are AND'd together.)
$(H3 Compare Expressions)
$(GRAMMAR
$(GNAME CmpExpression):
$(GLINK ShiftExpression)
$(GLINK EqualExpression)
$(GLINK IdentityExpression)
$(GLINK RelExpression)
$(GLINK InExpression)
)
$(H3 Equality Expressions)
$(GRAMMAR
$(GNAME EqualExpression):
$(GLINK ShiftExpression) $(D ==) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !=) $(GLINK ShiftExpression)
)
$(P Equality expressions compare the two operands for equality ($(D ==))
or inequality ($(D !=)).
The type of the result is $(D bool). The operands
go through the usual conversions to bring them to a common type before
comparison.
)
$(P If they are integral values or pointers, equality
is defined as the bit pattern of the type matches exactly.
)
$(P Equality for floating point types is more complicated. $(D -0) and
$(D +0) compare as equal. If either or both operands are NAN, then
both the $(D ==) returns false and $(D !=) returns $(D true). Otherwise, the bit
patterns are compared for equality.
)
$(P For complex numbers, equality is defined as equivalent to:
---
x.re == y.re && x.im == y.im
---
and inequality is defined as equivalent to:
---
x.re != y.re || x.im != y.im
---
)
$(P Equality for struct objects means the logical product of all equality
results of the corresponding object fields.
If all struct fields use bitwise equality, the whole struct equality
could be optimized to one memory comparison operation (the existence of
alignment holes in the objects is accounted for, usually by setting them
all to 0 upon initialization).
)
$(P For class and struct objects, the expression $(D (a == b))
is rewritten as
$(D a.opEquals(b)), and $(D (a != b)) is rewritten as
$(D !a.opEquals(b)).
)
$(P For class objects, the $(D ==) and $(D !=)
operators compare the
contents of the objects. Therefore, comparing against
$(D null) is invalid, as $(D null) has no contents.
Use the $(D is) and $(D !is) operators instead.
---
class C;
C c;
if (c == null) // error
...
if (c is null) // ok
...
---
)
$(P For static and dynamic arrays, equality is defined as the
lengths of the arrays
matching, and all the elements are equal.
)
$(H4 Identity Expressions)
$(GRAMMAR
$(GNAME IdentityExpression):
$(GLINK ShiftExpression) $(D is) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !is) $(GLINK ShiftExpression)
)
$(P The $(D is) compares for identity.
To compare for not identity, use $(D e1 !is e2).
The type of the result is $(D bool). The operands
go through the usual conversions to bring them to a common type before
comparison.
)
$(P For class objects, identity is defined as the object references
are for the same object. Null class objects can be compared with
$(D is).
)
$(P For struct objects, identity is defined as the bits in the
struct being identical.
)
$(P For static and dynamic arrays, identity is defined as referring
to the same array elements and the same number of elements.
)
$(P For other operand types, identity is defined as being the same
as equality.
)
$(P The identity operator $(D is) cannot be overloaded.
)
$(H3 Relational Expressions)
$(GRAMMAR
$(GNAME RelExpression):
$(GLINK ShiftExpression) $(D <) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D <)$(D =) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D >) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D >=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !<>=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !<>) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D <>) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D <>=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !>) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !>=) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !<) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !<=) $(GLINK ShiftExpression)
)
$(P First, the integral promotions are done on the operands.
The result type of a relational expression is $(D bool).
)
$(P For class objects, the result of Object.opCmp() forms the left
operand, and 0 forms the right operand. The result of the
relational expression (o1 op o2) is:
---
(o1.opCmp(o2) op 0)
---
)
$(P It is an error to compare objects if one is $(D null).)
$(P For static and dynamic arrays, the result of the relational
op is the result of the operator applied to the first non-equal
element of the array. If two arrays compare equal, but are of
different lengths, the shorter array compares as "less" than the
longer array.
)
$(H4 Integer comparisons)
$(P Integer comparisons happen when both operands are integral
types.
)
$(TABLE2 Integer comparison operators,
$(THEAD Operator, Relation)
$(TROW $(D <), less)
$(TROW $(D >), greater)
$(TROW $(D <)$(D =), less or equal)
$(TROW $(D >=), greater or equal)
$(TROW $(D ==), equal)
$(TROW $(D !=), not equal)
)
$(P It is an error to have one operand be signed and the other
unsigned for a $(D <), $(D <)$(D =), $(D >) or
$(D >)$(D =) expression.
Use casts to make both operands signed or both operands unsigned.
)
$(H4 $(LEGACY_LNAME2 floating_point_comparisons, floating-point-comparisons, Floating point comparisons))
$(P If one or both operands are floating point, then a floating
point comparison is performed.
)
$(P Useful floating point operations must take into account NAN values.
In particular, a relational operator can have NAN operands.
The result of a relational operation on float
values is less, greater, equal, or unordered (unordered means
either or both of the
operands is a NAN). That means there are 14 possible comparison
conditions to test for:
$(TABLE2 Floating point comparison operators,
$(THEAD Operator, Greater, Less, Equal, Unordered, Exception, Relation)
$(TROW $(D ==),$(D F),$(D F),$(D T),$(D F),$(D no),equal)
$(TROW $(D !=),$(D T),$(D T),$(D F),$(D T),$(D no),$(ARGS unordered, less, or greater))
$(TROW $(D >),$(D T),$(D F),$(D F),$(D F),$(D yes),$(ARGS greater))
$(TROW $(D >=),$(D T),$(D F),$(D T),$(D F),$(D yes),$(ARGS greater or equal))
$(TROW $(D <),$(D F),$(D T),$(D F),$(D F),$(D yes),$(ARGS less))
$(TROW $(D <)$(D =),$(D F),$(D T),$(D T),$(D F),$(D yes),$(ARGS less or equal))
$(TROW $(D !)$(D <)$(D >)$(D =), $(D F),$(D F),$(D F),$(D T),$(D no),$(ARGS unordered))
$(TROW $(D <)$(D >),$(D T),$(D T),$(D F),$(D F),$(D yes),$(ARGS less or greater))
$(TROW $(D <)$(D >)$(D =),$(D T),$(D T),$(D T),$(D F),$(D yes),$(ARGS less, equal, or greater))
$(TROW $(D !<=),$(D T),$(D F),$(D F),$(D T),$(D no),$(ARGS unordered or greater))
$(TROW $(D !<),$(D T),$(D F),$(D T),$(D T),$(D no),$(ARGS unordered, greater, or equal))
$(TROW $(D !>=),$(D F),$(D T),$(D F),$(D T),$(D no),$(ARGS unordered or less))
$(TROW $(D !>),$(D F),$(D T),$(D T),$(D T),$(D no),$(ARGS unordered, less, or equal))
$(TROW $(D !<>),$(D F),$(D F),$(D T),$(D T),$(D no),$(ARGS unordered or equal))
)
)
$(H5 Notes:)
$(OL
$(LI For floating point comparison operators,
$(CODE (a !op b))
is not the same as $(CODE !(a op b)).)
$(LI "Unordered" means one or both of the operands is a NAN.)
$(LI "Exception" means the $(I Invalid Exception) is raised if one
of the operands is a NAN. It does not mean an exception
is thrown. The $(I Invalid Exception) can be checked
using the functions in $(D core.stdc.fenv).
)
)
$(H4 $(LEGACY_LNAME2 class_comparisons, class-comparisons, Class comparisons))
$(P For class objects, the relational
operators compare the
contents of the objects. Therefore, comparing against
$(CODE null) is invalid, as $(CODE null) has no contents.
---
class C;
C c;
if (c < null) // error
...
---
)
$(H3 In Expressions)
$(GRAMMAR
$(GNAME InExpression):
$(GLINK ShiftExpression) $(D in) $(GLINK ShiftExpression)
$(GLINK ShiftExpression) $(D !in) $(GLINK ShiftExpression)
)
$(P An associative array can be tested to see if an element is in the array:
-------------
int foo[char[]];
...
if ("hello" in foo)
...
-------------
)
$(P The $(D in) expression has the same precedence as the
relational expressions $(D <), $(D <)$(D =),
etc.
The return value of the $(I InExpression) is $(D null)
if the element is not in the array;
if it is in the array it is a pointer to the element.
)
$(P The $(D !in) expression is the logical negation of the $(D in)
operation.
)
$(H3 Shift Expressions)
$(GRAMMAR
$(GNAME ShiftExpression):
$(GLINK AddExpression)
$(I ShiftExpression) $(D <)$(D <) $(GLINK AddExpression)
$(I ShiftExpression) $(D >)$(D >) $(GLINK AddExpression)
$(I ShiftExpression) $(D >)$(D >)$(D >) $(GLINK AddExpression)
)
$(P The operands must be integral types, and undergo the usual integral
promotions. The result type is the type of the left operand after
the promotions. The result value is the result of shifting the bits
by the right operand's value.
)
$(P $(D <)$(D <) is a left shift.
$(D >)$(D >) is a signed right shift.
$(D >)$(D >)$(D >) is an unsigned right shift.
)
$(P It's illegal to shift by the same or more bits than the size of the
quantity being shifted:
-------------
int c;
auto x = c << 33; // error
-------------
)
$(H3 Add Expressions)
$(GRAMMAR
$(GNAME AddExpression):
$(GLINK MulExpression)
$(I AddExpression) $(D +) $(GLINK MulExpression)
$(I AddExpression) $(D -) $(GLINK MulExpression)
$(GLINK CatExpression)
)
$(P If the operands are of integral types, they undergo integral
promotions, and then are brought to a common type using the
usual arithmetic conversions.
)
$(P If either operand is a floating point type, the other is implicitly
converted to floating point and they are brought to a common type
via the usual arithmetic conversions.
)
$(P If the operator is $(D +) or $(D -), and
the first operand is a pointer, and the second is an integral type,
the resulting type is the type of the first operand, and the resulting
value is the pointer plus (or minus) the second operand multiplied by
the size of the type pointed to by the first operand.
)
$(P If the second operand is a pointer, and the first is an integral type,
and the operator is $(D +),
the operands are reversed and the pointer arithmetic just described
is applied.
)
$(P If both operands are pointers, and the operator is $(D +),
then it is illegal. For $(D -), the pointers are subtracted and the
result is divided by the size of the type pointed to by the
operands. It is an error if the pointers point to different types.
)
$(P If both operands are of integral types and an overflow or underflow
occurs in the computation, wrapping will happen. That is,
$(D uint.max + 1 == uint.min) and $(D uint.min - 1 == uint.max).
)
$(P Add expressions for floating point operands are not associative.
)
$(H3 Cat Expressions)
$(GRAMMAR
$(GNAME CatExpression):
$(GLINK AddExpression) $(D ~) $(GLINK MulExpression)
)
$(P A $(I CatExpression) concatenates arrays, producing
a dynmaic array with the result. The arrays must be
arrays of the same element type. If one operand is an array
and the other is of that array's element type, that element
is converted to an array of length 1 of that element,
and then the concatenation is performed.
)
$(H3 Mul Expressions)
$(GRAMMAR
$(GNAME MulExpression):
$(GLINK UnaryExpression)
$(I MulExpression) $(D *) $(GLINK UnaryExpression)
$(I MulExpression) $(D /) $(GLINK UnaryExpression)
$(I MulExpression) $(D %) $(GLINK UnaryExpression)
)
$(P The operands must be arithmetic types. They undergo integral
promotions, and then are brought to a common type using the
usual arithmetic conversions.
)
$(P For integral operands, the $(D *), $(D /), and $(D %)
correspond to multiply, divide, and modulus operations.
For multiply, overflows are ignored and simply chopped to fit
into the integral type.
)
$(P For integral operands of the $(D /) and $(D %) operators,
the quotient rounds towards zero and the remainder has the
same sign as the dividend.
If the divisor is zero, an Exception is thrown.
)
$(P For floating point operands, the * and / operations correspond
to the IEEE 754 floating point equivalents. % is not the same as
the IEEE 754 remainder. For example, 15.0 % 10.0 == 5.0, whereas
for IEEE 754, remainder(15.0,10.0) == -5.0.
)
$(P Mul expressions for floating point operands are not associative.
)
$(H3 $(LEGACY_LNAME2 UnaryExpression, unary-expression, Unary Expressions))
$(GRAMMAR
$(GNAME UnaryExpression):
$(CODE_AMP) $(I UnaryExpression)
$(D ++) $(I UnaryExpression)
$(D --) $(I UnaryExpression)
$(D *) $(I UnaryExpression)
$(D -) $(I UnaryExpression)
$(D +) $(I UnaryExpression)
$(D !) $(I UnaryExpression)
$(GLINK ComplementExpression)
$(D $(LPAREN)) $(GLINK2 declaration, Type) $(D $(RPAREN) .) $(IDENTIFIER)
$(D $(LPAREN)) $(GLINK2 declaration, Type) $(D $(RPAREN) .) $(GLINK2 template, TemplateInstance)
$(GLINK DeleteExpression)
$(GLINK CastExpression)
$(GLINK PowExpression)
)
$(H4 Complement Expressions)
$(GRAMMAR
$(GNAME ComplementExpression):
$(D ~) $(GLINK UnaryExpression)
)
$(P $(I ComplementExpression)s work on integral types (except $(D bool)).
All the bits in the value are complemented.
)
$(P $(B Note:) unlike in C and C++, the usual integral promotions are not performed
prior to the complement operation.
)
$(H4 New Expressions)
$(GRAMMAR
$(GNAME NewExpression):
$(D new) $(I AllocatorArguments)$(OPT) $(GLINK2 declaration, Type)
$(GLINK NewExpressionWithArgs)
$(GNAME NewExpressionWithArgs):
$(D new) $(I AllocatorArguments)$(OPT) $(GLINK2 declaration, Type) $(D [) $(GLINK AssignExpression) $(D ])
$(D new) $(I AllocatorArguments)$(OPT) $(GLINK2 declaration, Type) $(D $(LPAREN)) $(GLINK ArgumentList)$(OPT) $(D $(RPAREN))
$(GLINK2 class, NewAnonClassExpression)
$(GNAME AllocatorArguments):
$(D $(LPAREN)) $(GLINK ArgumentList)$(OPT) $(D $(RPAREN))
$(GNAME ArgumentList):
$(GLINK AssignExpression)
$(GLINK AssignExpression) $(D ,)
$(GLINK AssignExpression) $(D ,) $(I ArgumentList)
)
$(P $(I NewExpression)s are used to allocate memory on the garbage
collected heap (default) or using a class or struct specific allocator.
)
$(P To allocate multidimensional arrays, the declaration reads
in the same order as the prefix array declaration order.
-------------
char[][] foo; // dynamic array of strings
...
foo = new char[][30]; // allocate array of 30 strings
-------------
)
$(P The above allocation can also be written as:
-------------
foo = new char[][](30); // allocate array of 30 strings
-------------
)
$(P To allocate the nested arrays, multiple arguments can be used:
---------------
int[][][] bar;
...
bar = new int[][][](5, 20, 30);
---------------
)
$(P Which is equivalent to:
----------
bar = new int[][][5];
foreach (ref a; bar)
{
a = new int[][20];
foreach (ref b; a)
{
b = new int[30];
}
}
-----------
)
$(P If there is a $(D new $(LPAREN)) $(GLINK ArgumentList) $(D $(RPAREN)),
then
those arguments are passed to the class or struct specific
$(DDSUBLINK class, allocators, allocator function) after the size argument.
)
$(P If a $(I NewExpression) is used as an initializer for
a function local variable with $(D scope) storage class,
and the $(GLINK ArgumentList) to $(D new) is empty, then
the instance is allocated on the stack rather than the heap
or using the class specific allocator.
)
$(H4 Delete Expressions)
$(GRAMMAR
$(GNAME DeleteExpression):
$(D delete) $(GLINK UnaryExpression)
)
$(P If the $(I UnaryExpression) is a class object reference, and
there is a destructor for that class, the destructor
is called for that object instance.
)
$(P Next, if the $(I UnaryExpression) is a class object reference, or
a pointer to a struct instance, and the class or struct
has overloaded operator delete, then that operator delete is called
for that class object instance or struct instance.
)
$(P Otherwise, the garbage collector is called to immediately free the
memory allocated for the class instance or struct instance.
If the garbage collector was not used to allocate the memory for
the instance, undefined behavior will result.
)
$(P If the $(I UnaryExpression) is a pointer or a dynamic array,
the garbage collector is called to immediately release the
memory.
If the garbage collector was not used to allocate the memory for
the instance, undefined behavior will result.
)
$(P The pointer, dynamic array, or reference is set to $(D null)
after the delete is performed.
Any attempt to reference the data after the deletion via another
reference to it will result in undefined behavior.
)
$(P If $(I UnaryExpression) is a variable allocated
on the stack, the class destructor (if any) is called for that
instance. Neither the garbage collector nor any class deallocator
is called.
)
$(H4 Cast Expressions)
$(GRAMMAR
$(GNAME CastExpression):
$(D cast $(LPAREN)) $(GLINK2 declaration, Type) $(D $(RPAREN)) $(GLINK UnaryExpression)
$(D cast $(LPAREN)) $(GLINK2 declaration, TypeCtors)$(OPT) $(D $(RPAREN)) $(GLINK UnaryExpression)
)
$(P A $(I CastExpression) converts the $(I UnaryExpression)
to $(GLINK2 declaration, Type).
-------------
cast(foo) -p; // cast (-p) to type foo
(foo) - p; // subtract p from foo
-------------
)
$(P Any casting of a class reference to a
derived class reference is done with a runtime check to make sure it
really is a downcast. $(D null) is the result if it isn't.
)
$(P $(B Note:) This is equivalent to the behavior of the
$(D dynamic_cast) operator in C++.
-------------
class A { ... }
class B : A { ... }
void test(A a, B b)
{
B bx = a; // error, need cast
B bx = cast(B) a; // bx is null if a is not a B
A ax = b; // no cast needed
A ax = cast(A) b; // no runtime check needed for upcast
}
-------------
)
$(P In order to determine if an object $(D o) is an instance of
a class $(D B) use a cast:
-------------
if (cast(B) o)
{
// o is an instance of B
}
else
{
// o is not an instance of B
}
-------------
)
$(P Casting a pointer type to and from a class type is done as a type paint
(i.e. a reinterpret cast).
)
$(P Casting a dynamic array to another dynamic array is done only if the
array lengths multiplied by the element sizes match. The cast is done
as a type paint, with the array length adjusted to match any change in
element size. If there's not a match, a runtime error is generated.
---
import std.stdio;
int main()
{
byte[] a = [1,2,3];
auto b = cast(int[])a; // runtime array cast misalignment
int[] c = [1, 2, 3];
auto d = cast(byte[])c; // ok
// prints:
// [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]
writeln(d);
return 0;
}
---
)
$(P Casting a floating point literal from one type to another
changes its type, but internally it is retained at full
precision for the purposes of constant folding.
---
void test()
{
real a = 3.40483L;
real b;
b = 3.40483; // literal is not truncated to double precision
assert(a == b);
assert(a == 3.40483);
assert(a == 3.40483L);
assert(a == 3.40483F);
double d = 3.40483; // truncate literal when assigned to variable
assert(d != a); // so it is no longer the same
const double x = 3.40483; // assignment to const is not
assert(x == a); // truncated if the initializer is visible
}
---
)
$(P Casting a value $(I v) to a struct $(I S), when value is not a struct
of the same type, is equivalent to:
---
S(v)
---
)
$(P Casting to a $(GLINK CastQual) replaces the qualifiers to the type of
the $(GLINK UnaryExpression).
---
shared int x;
assert(is(typeof(cast(const)x) == const int));
---
)
$(P Casting with no $(GLINK Type) or $(GLINK CastQual) removes
any top level $(D const), $(D immutable), $(D shared) or $(D inout)
type modifiers from the type
of the $(GLINK UnaryExpression).
---
shared int x;
assert(is(typeof(cast()x) == int));
---
)
$(P Casting an expression to $(D void) type is allowed to mark that
the result is unused. On $(GLINK2 statement, ExpressionStatement),
it could be used properly to avoid "has no effect" error.
----
void foo(lazy void exp) {}
void main()
{
foo(10); // NG - has no effect in expression '10'
foo(cast(void)10); // OK
}
----
)
$(H3 Pow Expressions)
$(GRAMMAR
$(GNAME PowExpression):
$(GLINK PostfixExpression)
$(GLINK PostfixExpression) $(D ^^) $(GLINK UnaryExpression)
)
$(P $(I PowExpression) raises its left operand to the power of its
right operand.
)
$(H3 Postfix Expressions)
$(GRAMMAR
$(GNAME PostfixExpression):
$(GLINK PrimaryExpression)
$(I PostfixExpression) $(D .) $(IDENTIFIER)
$(I PostfixExpression) $(D .) $(GLINK2 template, TemplateInstance)
$(I PostfixExpression) $(D .) $(GLINK NewExpression)
$(I PostfixExpression) $(D ++)
$(I PostfixExpression) $(D --)
$(I PostfixExpression) $(D $(LPAREN)) $(GLINK ArgumentList)$(OPT) $(D $(RPAREN))
$(GLINK2 declaration, TypeCtors)$(OPT) $(GLINK2 declaration, BasicType) $(D $(LPAREN)) $(GLINK ArgumentList)$(OPT) $(D $(RPAREN))
$(GLINK IndexExpression)
$(GLINK SliceExpression)
)
$(H3 Index Expressions)
$(GRAMMAR
$(GNAME IndexExpression):
$(GLINK PostfixExpression) $(D [) $(GLINK ArgumentList) $(D ])
)
$(P $(I PostfixExpression) is evaluated.
If $(I PostfixExpression) is an expression of type
static array or dynamic array, the symbol $(DOLLAR) is
set to be the the number of elements in the array.
If $(I PostfixExpression) is an $(I ExpressionTuple),
the symbol $(DOLLAR) is
set to be the the number of elements in the tuple.
A new declaration scope is created for the evaluation of the
$(GLINK ArgumentList) and $(DOLLAR) appears in that scope only.
)
$(P If $(I PostfixExpression) is an $(I ExpressionTuple),
then the $(GLINK ArgumentList) must consist of only one argument,
and that must be statically evaluatable to an integral constant.
That integral constant $(I n) then selects the $(I n)th
expression in the $(I ExpressionTuple), which is the result
of the $(I IndexExpression).
It is an error if $(I n) is out of bounds of the $(I ExpressionTuple).
)
$(H3 Slice Expressions)
$(GRAMMAR
$(GNAME SliceExpression):
$(GLINK PostfixExpression) $(D [ ])
$(GLINK PostfixExpression) $(D [) $(GLINK AssignExpression) $(D ..) $(GLINK AssignExpression) $(D ])
)
$(P $(I PostfixExpression) is evaluated.
if $(I PostfixExpression) is an expression of type
static array or dynamic array, the special variable $(DOLLAR)
is declared and set to be the length of the array.
A new declaration scope is created for the evaluation of the
$(GLINK AssignExpression)..$(GLINK AssignExpression)
and $(DOLLAR) appears in that scope only.
)
$(P The first $(I AssignExpression) is taken to be the inclusive
lower bound