0
|
1 module dmd.AddAssignExp;
|
|
2
|
|
3 import dmd.BinExp;
|
|
4 import dmd.Loc;
|
|
5 import dmd.Expression;
|
|
6 import dmd.Scope;
|
|
7 import dmd.InterState;
|
|
8 import dmd.OutBuffer;
|
|
9 import dmd.ArrayTypes;
|
|
10 import dmd.Identifier;
|
|
11 import dmd.IRState;
|
|
12 import dmd.TOK;
|
|
13 import dmd.Type;
|
|
14 import dmd.TY;
|
|
15 import dmd.AddExp;
|
|
16 import dmd.CastExp;
|
|
17 import dmd.AssignExp;
|
|
18 import dmd.Global;
|
|
19 import dmd.Id;
|
|
20
|
|
21 import dmd.backend.OPER;
|
|
22 import dmd.backend.elem;
|
|
23
|
|
24 class AddAssignExp : BinExp
|
|
25 {
|
|
26 this(Loc loc, Expression e1, Expression e2)
|
|
27 {
|
|
28 super(loc, TOK.TOKaddass, AddAssignExp.sizeof, e1, e2);
|
|
29 }
|
|
30
|
|
31 Expression semantic(Scope sc)
|
|
32 {
|
|
33 Expression e;
|
|
34
|
|
35 if (type)
|
|
36 return this;
|
|
37
|
|
38 BinExp.semantic(sc);
|
|
39 e2 = resolveProperties(sc, e2);
|
|
40
|
|
41 e = op_overload(sc);
|
|
42 if (e)
|
|
43 return e;
|
|
44
|
|
45 Type tb1 = e1.type.toBasetype();
|
|
46 Type tb2 = e2.type.toBasetype();
|
|
47
|
|
48 if (e1.op == TOK.TOKslice)
|
|
49 {
|
|
50 typeCombine(sc);
|
|
51 type = e1.type;
|
|
52 return arrayOp(sc);
|
|
53 }
|
|
54 else
|
|
55 {
|
|
56 e1 = e1.modifiableLvalue(sc, e1);
|
|
57 }
|
|
58
|
|
59 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray) && tb1.nextOf().equals(tb2.nextOf()))
|
|
60 {
|
|
61 type = e1.type;
|
|
62 typeCombine(sc);
|
|
63 e = this;
|
|
64 }
|
|
65 else
|
|
66 {
|
|
67 e1.checkScalar();
|
|
68 e1.checkNoBool();
|
|
69 if (tb1.ty == TY.Tpointer && tb2.isintegral())
|
|
70 e = scaleFactor(sc);
|
|
71 else if (tb1.ty == TY.Tbit || tb1.ty == TY.Tbool)
|
|
72 {
|
|
73 static if (false) {
|
|
74 // Need to rethink this
|
|
75 if (e1.op != TOK.TOKvar)
|
|
76 {
|
|
77 // Rewrite e1+=e2 to (v=&e1),*v=*v+e2
|
|
78 VarDeclaration v;
|
|
79 Expression ea;
|
|
80 Expression ex;
|
|
81
|
|
82 Identifier id = Lexer.uniqueId("__name");
|
|
83
|
|
84 v = new VarDeclaration(loc, tb1.pointerTo(), id, null);
|
|
85 v.semantic(sc);
|
|
86 if (!sc.insert(v))
|
|
87 assert(0);
|
|
88
|
|
89 v.parent = sc.func;
|
|
90
|
|
91 ea = new AddrExp(loc, e1);
|
|
92 ea = new AssignExp(loc, new VarExp(loc, v), ea);
|
|
93
|
|
94 ex = new VarExp(loc, v);
|
|
95 ex = new PtrExp(loc, ex);
|
|
96 e = new AddExp(loc, ex, e2);
|
|
97 e = new CastExp(loc, e, e1.type);
|
|
98 e = new AssignExp(loc, ex.syntaxCopy(), e);
|
|
99
|
|
100 e = new CommaExp(loc, ea, e);
|
|
101 }
|
|
102 else
|
|
103 {
|
|
104 // Rewrite e1+=e2 to e1=e1+e2
|
|
105 // BUG: doesn't account for side effects in e1
|
|
106 // BUG: other assignment operators for bits aren't handled at all
|
|
107 e = new AddExp(loc, e1, e2);
|
|
108 e = new CastExp(loc, e, e1.type);
|
|
109 e = new AssignExp(loc, e1.syntaxCopy(), e);
|
|
110 }
|
|
111 } else {
|
|
112 // Rewrite e1+=e2 to e1=e1+e2
|
|
113 // BUG: doesn't account for side effects in e1
|
|
114 // BUG: other assignment operators for bits aren't handled at all
|
|
115 e = new AddExp(loc, e1, e2);
|
|
116 e = new CastExp(loc, e, e1.type);
|
|
117 e = new AssignExp(loc, e1.syntaxCopy(), e);
|
|
118 }
|
|
119 e = e.semantic(sc);
|
|
120 }
|
|
121 else
|
|
122 {
|
|
123 type = e1.type;
|
|
124 typeCombine(sc);
|
|
125 e1.checkArithmetic();
|
|
126 e2.checkArithmetic();
|
|
127
|
|
128 if (type.isreal() || type.isimaginary())
|
|
129 {
|
|
130 assert(global.errors || e2.type.isfloating());
|
|
131 e2 = e2.castTo(sc, e1.type);
|
|
132 }
|
|
133 e = this;
|
|
134 }
|
|
135 }
|
|
136 return e;
|
|
137 }
|
|
138
|
|
139 Expression interpret(InterState* istate)
|
|
140 {
|
|
141 assert(false);
|
|
142 }
|
|
143
|
|
144 void buildArrayIdent(OutBuffer buf, Expressions arguments)
|
|
145 {
|
|
146 assert(false);
|
|
147 }
|
|
148
|
|
149 Expression buildArrayLoop(Arguments fparams)
|
|
150 {
|
|
151 assert(false);
|
|
152 }
|
|
153
|
|
154 Identifier opId() /* For operator overloading */
|
|
155 {
|
|
156 return Id.addass;
|
|
157 }
|
|
158
|
|
159 elem* toElem(IRState* irs)
|
|
160 {
|
|
161 //printf("AddAssignExp::toElem() %s\n", toChars());
|
|
162 elem *e;
|
|
163 Type tb1 = e1.type.toBasetype();
|
|
164 Type tb2 = e2.type.toBasetype();
|
|
165
|
|
166 if ((tb1.ty == TY.Tarray || tb1.ty == TY.Tsarray) && (tb2.ty == TY.Tarray || tb2.ty == TY.Tsarray))
|
|
167 {
|
|
168 error("Array operations not implemented");
|
|
169 }
|
|
170 else
|
|
171 e = toElemBin(irs, OPER.OPaddass);
|
|
172
|
|
173 return e;
|
|
174 }
|
|
175 } |