annotate dmd/AddAssignExp.d @ 117:fe941d774f4a

+ ctfe of assign operations
author Trass3r
date Thu, 02 Sep 2010 02:50:19 +0200
parents e28b18c23469
children 9e39c7de8438
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.AddAssignExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 73
diff changeset
3 import dmd.common;
117
fe941d774f4a + ctfe of assign operations
Trass3r
parents: 114
diff changeset
4 import dmd.expression.Add;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.BinExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.InterState;
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
10 import dmd.Argument;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
11 import dmd.STC;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.AddExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.CastExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.AssignExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.backend.OPER;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 class AddAssignExp : BinExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 this(Loc loc, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 super(loc, TOK.TOKaddass, AddAssignExp.sizeof, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
35 override Expression semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 BinExp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 e2 = resolveProperties(sc, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 e = op_overload(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 Type tb1 = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 Type tb2 = e2.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 if (e1.op == TOK.TOKslice)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 typeCombine(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 type = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 return arrayOp(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 e1 = e1.modifiableLvalue(sc, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray) && tb1.nextOf().equals(tb2.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 type = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 typeCombine(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 e1.checkScalar();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 e1.checkNoBool();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 if (tb1.ty == TY.Tpointer && tb2.isintegral())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 e = scaleFactor(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 else if (tb1.ty == TY.Tbit || tb1.ty == TY.Tbool)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 // Need to rethink this
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 if (e1.op != TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 // Rewrite e1+=e2 to (v=&e1),*v=*v+e2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 VarDeclaration v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 Expression ea;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 Expression ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 Identifier id = Lexer.uniqueId("__name");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 v = new VarDeclaration(loc, tb1.pointerTo(), id, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 v.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 if (!sc.insert(v))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 v.parent = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 ea = new AddrExp(loc, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 ea = new AssignExp(loc, new VarExp(loc, v), ea);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 ex = new VarExp(loc, v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 ex = new PtrExp(loc, ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 e = new AddExp(loc, ex, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 e = new CastExp(loc, e, e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 e = new AssignExp(loc, ex.syntaxCopy(), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 e = new CommaExp(loc, ea, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 // Rewrite e1+=e2 to e1=e1+e2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 // BUG: doesn't account for side effects in e1
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 // BUG: other assignment operators for bits aren't handled at all
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 e = new AddExp(loc, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 e = new CastExp(loc, e, e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 e = new AssignExp(loc, e1.syntaxCopy(), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 // Rewrite e1+=e2 to e1=e1+e2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 // BUG: doesn't account for side effects in e1
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 // BUG: other assignment operators for bits aren't handled at all
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 e = new AddExp(loc, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 e = new CastExp(loc, e, e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 e = new AssignExp(loc, e1.syntaxCopy(), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 type = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 typeCombine(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 e1.checkArithmetic();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 e2.checkArithmetic();
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
131 checkComplexAddAssign();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 if (type.isreal() || type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 assert(global.errors || e2.type.isfloating());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 e2 = e2.castTo(sc, e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
144 override Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 {
117
fe941d774f4a + ctfe of assign operations
Trass3r
parents: 114
diff changeset
146 return interpretAssignCommon(istate, &Add);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
149 override void buildArrayIdent(OutBuffer buf, Expressions arguments)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
151 AssignExp_buildArrayIdent(buf, arguments, "Add");
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
154 override Expression buildArrayLoop(Arguments fparams)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
156 /* Evaluate assign expressions right to left
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
157 */
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
158 Expression ex2 = e2.buildArrayLoop(fparams);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
159 Expression ex1 = e1.buildArrayLoop(fparams);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
160 Argument param = cast(Argument)fparams.data[0];
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
161 param.storageClass = STCundefined;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
162 Expression e = new AddAssignExp(Loc(0), ex1, ex2);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
163 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
166 override Identifier opId() /* For operator overloading */
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 return Id.addass;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
171 override elem* toElem(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 //printf("AddAssignExp::toElem() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 elem *e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 Type tb1 = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 Type tb2 = e2.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 error("Array operations not implemented");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 e = toElemBin(irs, OPER.OPaddass);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 63
diff changeset
187 }