annotate dmd/DelegateExp.d @ 161:584dc990e12f

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