0
|
1 module dmd.AddExp;
|
|
2
|
|
3 import dmd.Expression;
|
|
4 import dmd.Identifier;
|
|
5 import dmd.backend.elem;
|
|
6 import dmd.InterState;
|
|
7 import dmd.OutBuffer;
|
|
8 import dmd.Loc;
|
|
9 import dmd.Id;
|
|
10 import dmd.Scope;
|
|
11 import dmd.IRState;
|
|
12 import dmd.ArrayTypes;
|
|
13 import dmd.BinExp;
|
|
14 import dmd.Type;
|
|
15 import dmd.TOK;
|
|
16 import dmd.TY;
|
|
17
|
|
18 import dmd.expression.Add;
|
|
19 import dmd.backend.Util;
|
|
20 import dmd.backend.OPER;
|
|
21
|
|
22 class AddExp : BinExp
|
|
23 {
|
|
24 this(Loc loc, Expression e1, Expression e2)
|
|
25 {
|
|
26 super(loc, TOK.TOKadd, AddExp.sizeof, e1, e2);
|
|
27 }
|
|
28
|
|
29 Expression semantic(Scope sc)
|
|
30 {
|
|
31 Expression e;
|
|
32
|
|
33 version (LOGSEMANTIC) {
|
|
34 printf("AddExp.semantic('%s')\n", toChars());
|
|
35 }
|
|
36 if (!type)
|
|
37 {
|
|
38 BinExp.semanticp(sc);
|
|
39
|
|
40 e = op_overload(sc);
|
|
41 if (e)
|
|
42 return e;
|
|
43
|
|
44 Type tb1 = e1.type.toBasetype();
|
|
45 Type tb2 = e2.type.toBasetype();
|
|
46
|
|
47 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) &&
|
|
48 (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray) &&
|
|
49 tb1.nextOf().equals(tb2.nextOf())
|
|
50 )
|
|
51 {
|
|
52 type = e1.type;
|
|
53 e = this;
|
|
54 }
|
|
55 else if (tb1.ty == TY.Tpointer && e2.type.isintegral() ||
|
|
56 tb2.ty == TY.Tpointer && e1.type.isintegral())
|
|
57 e = scaleFactor(sc);
|
|
58 else if (tb1.ty == TY.Tpointer && tb2.ty == TY.Tpointer)
|
|
59 {
|
|
60 incompatibleTypes();
|
|
61 type = e1.type;
|
|
62 e = this;
|
|
63 }
|
|
64 else
|
|
65 {
|
|
66 typeCombine(sc);
|
|
67 if ((e1.type.isreal() && e2.type.isimaginary()) ||
|
|
68 (e1.type.isimaginary() && e2.type.isreal()))
|
|
69 {
|
|
70 switch (type.toBasetype().ty)
|
|
71 {
|
|
72 case TY.Tfloat32:
|
|
73 case TY.Timaginary32:
|
|
74 type = Type.tcomplex32;
|
|
75 break;
|
|
76
|
|
77 case TY.Tfloat64:
|
|
78 case TY.Timaginary64:
|
|
79 type = Type.tcomplex64;
|
|
80 break;
|
|
81
|
|
82 case TY.Tfloat80:
|
|
83 case TY.Timaginary80:
|
|
84 type = Type.tcomplex80;
|
|
85 break;
|
|
86 }
|
|
87 }
|
|
88 e = this;
|
|
89 }
|
|
90 return e;
|
|
91 }
|
|
92 return this;
|
|
93 }
|
|
94
|
|
95 Expression optimize(int result)
|
|
96 {
|
|
97 Expression e;
|
|
98
|
|
99 //printf("AddExp::optimize(%s)\n", toChars());
|
|
100 e1 = e1.optimize(result);
|
|
101 e2 = e2.optimize(result);
|
|
102 if (e1.isConst() && e2.isConst())
|
|
103 {
|
|
104 if (e1.op == TOK.TOKsymoff && e2.op == TOK.TOKsymoff)
|
|
105 return this;
|
|
106 e = Add(type, e1, e2);
|
|
107 }
|
|
108 else
|
|
109 e = this;
|
|
110
|
|
111 return e;
|
|
112 }
|
|
113
|
|
114 Expression interpret(InterState* istate)
|
|
115 {
|
|
116 assert(false);
|
|
117 }
|
|
118
|
|
119 void buildArrayIdent(OutBuffer buf, Expressions arguments)
|
|
120 {
|
|
121 assert(false);
|
|
122 }
|
|
123
|
|
124 Expression buildArrayLoop(Arguments fparams)
|
|
125 {
|
|
126 assert(false);
|
|
127 }
|
|
128
|
|
129 bool isCommutative()
|
|
130 {
|
|
131 return true;
|
|
132 }
|
|
133
|
|
134 Identifier opId()
|
|
135 {
|
|
136 return Id.add;
|
|
137 }
|
|
138
|
|
139 Identifier opId_r()
|
|
140 {
|
|
141 return Id.add_r;
|
|
142 }
|
|
143
|
|
144 elem* toElem(IRState* irs)
|
|
145 {
|
|
146 elem *e;
|
|
147 Type tb1 = e1.type.toBasetype();
|
|
148 Type tb2 = e2.type.toBasetype();
|
|
149
|
|
150 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray))
|
|
151 {
|
|
152 error("Array operation %s not implemented", toChars());
|
|
153 e = el_long(type.totym(), 0); // error recovery
|
|
154 }
|
|
155 else
|
|
156 e = toElemBin(irs, OPER.OPadd);
|
|
157
|
|
158 return e;
|
|
159 }
|
|
160 }
|
|
161
|