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