annotate dmd/AddAssignExp.d @ 0:10317f0c89a5

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