Mercurial > projects > ddmd
annotate dmd/AddExp.d @ 12:832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
Some unittest-specific functions implemented
author | korDen |
---|---|
date | Mon, 12 Apr 2010 15:13:00 +0400 |
parents | 10317f0c89a5 |
children | cab4c37afb89 |
rev | line source |
---|---|
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 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
121 Exp_buildArrayIdent(buf, arguments, "Add"); |
0 | 122 } |
123 | |
124 Expression buildArrayLoop(Arguments fparams) | |
125 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
126 /* Evaluate assign expressions left to right |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
127 */ |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
128 Expression ex1 = e1.buildArrayLoop(fparams); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
129 Expression ex2 = e2.buildArrayLoop(fparams); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
130 Expression e = new AddExp(Loc(0), ex1, ex2); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
131 return e; |
0 | 132 } |
133 | |
134 bool isCommutative() | |
135 { | |
136 return true; | |
137 } | |
138 | |
139 Identifier opId() | |
140 { | |
141 return Id.add; | |
142 } | |
143 | |
144 Identifier opId_r() | |
145 { | |
146 return Id.add_r; | |
147 } | |
148 | |
149 elem* toElem(IRState* irs) | |
150 { | |
151 elem *e; | |
152 Type tb1 = e1.type.toBasetype(); | |
153 Type tb2 = e2.type.toBasetype(); | |
154 | |
155 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray)) | |
156 { | |
157 error("Array operation %s not implemented", toChars()); | |
158 e = el_long(type.totym(), 0); // error recovery | |
159 } | |
160 else | |
161 e = toElemBin(irs, OPER.OPadd); | |
162 | |
163 return e; | |
164 } | |
165 } | |
166 |