annotate dmd/DelegateExp.d @ 73:ef02e2e203c2

Updating to dmd2.033
author korDen
date Sat, 28 Aug 2010 19:42:41 +0400
parents 2e2a5c3f943a
children e28b18c23469
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
1 module dmd.DelegateExp;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
2
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
3 import dmd.Expression;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.backend.elem;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
5 import dmd.AggregateDeclaration;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.UnaExp;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
7 import dmd.TypeDelegate;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
8 import dmd.FuncDeclaration;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
9 import dmd.MATCH;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
10 import dmd.Type;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
11 import dmd.OutBuffer;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Loc;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
13 import dmd.TY;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
14 import dmd.Scope;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
15 import dmd.InlineCostState;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.IRState;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
17 import dmd.PREC;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
18 import dmd.HdrGenState;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.codegen.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.backend.TYM;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
26 import dmd.backend.OPER;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
27
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 class DelegateExp : UnaExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 FuncDeclaration func;
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
31 bool hasOverloads;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
33 this(Loc loc, Expression e, FuncDeclaration f, bool hasOverloads = false)
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
34 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 super(loc, TOK.TOKdelegate, DelegateExp.sizeof, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 this.func = f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 this.hasOverloads = hasOverloads;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
40 override Expression semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 printf("DelegateExp.semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 e1 = e1.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 type = new TypeDelegate(func.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 type = type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 AggregateDeclaration ad = func.toParent().isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 if (func.needThis())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 e1 = getRightThis(loc, sc, ad, e1, func);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
57 override MATCH implicitConvTo(Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 printf("DelegateExp.implicitConvTo(this=%s, type=%s, t=%s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 MATCH result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 result = type.implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 if (result == MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 // Look for pointers to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 FuncDeclaration f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 t = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 if (type.ty == Tdelegate && type.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 t.ty == Tdelegate && t.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 if (func && func.overloadExactMatch(t.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 result = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
83 override Expression castTo(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 printf("DelegateExp.castTo(this=%s, type=%s, t=%s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 static string msg = "cannot form delegate due to covariant return type";
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 Type typeb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 if (tb != typeb)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 // Look for delegates to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 FuncDeclaration f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 if (typeb.ty == Tdelegate && typeb.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 tb.ty == Tdelegate && tb.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 if (func)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 f = func.overloadExactMatch(tb.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 int offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 if (f.tintro && f.tintro.nextOf().isBaseOf(f.type.nextOf(), &offset) && offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 error("%s", msg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 e = new DelegateExp(loc, e1, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 if (func.tintro)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 error("%s", msg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 e = Expression.castTo(sc, t);
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 int offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 func.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 if (func.tintro && func.tintro.nextOf().isBaseOf(func.type.nextOf(), &offset) && offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 error("%s", msg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 e = copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
134 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 buf.writeByte('&');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 if (!func.isNested())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 expToCBuffer(buf, hgs, e1, PREC.PREC_primary);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 buf.writeByte('.');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 buf.writestring(func.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
145 override void dump(int indent)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
150 override int inlineCost(InlineCostState* ics)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
155 override elem* toElem(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 elem* ethis;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 elem* ep;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 Symbol* sfunc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 int directcall = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 //printf("DelegateExp.toElem() '%s'\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 sfunc = func.toSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 if (func.isNested())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 ep = el_ptr(sfunc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 ethis = getEthis(loc, irs, func);
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 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 ethis = e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 if (e1.type.ty != Tclass && e1.type.ty != Tpointer)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 ethis = addressElem(ethis, e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 if (e1.op == TOKsuper)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 directcall = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 if (!func.isThis())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 error("delegates are only for non-static functions");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 if (!func.isVirtual() ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 directcall ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 func.isFinal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 ep = el_ptr(sfunc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 // Get pointer to function out of virtual table
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 assert(ethis);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 ep = el_same(&ethis);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 ep = el_una(OPind, TYnptr, ep);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 uint vindex = func.vtblIndex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 // Build *(ep + vindex * 4)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 ep = el_bin(OPadd,TYnptr,ep,el_long(TYint, vindex * 4));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 ep = el_una(OPind,TYnptr,ep);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 // if (func.tintro)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 // func.error(loc, "cannot form delegate due to covariant return type");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 if (ethis.Eoper == OPcomma)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 ethis.E2() = el_pair(TYullong, ethis.E2, ep);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 ethis.Ety = TYullong;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 e = ethis;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 e = el_pair(TYullong, ethis, ep);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 el_setLoc(e,loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
218 }