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