annotate dmd/CallExp.d @ 64:4290d870944a

More fixes
author korDen
date Mon, 23 Aug 2010 20:29:15 +0400
parents cab4c37afb89
children efb1e5bdf63c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Expression;
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
4 import dmd.Cast;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.BUILTIN;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.TypeFunction;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.CastExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.GlobalExpressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.TypePointer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.ThisExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.OverExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.CSX;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.TypeDelegate;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.ClassDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.DsymbolExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.DotExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.TemplateExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.TypeStruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.TypeClass;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.Lexer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.DeclarationExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.CtorDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.PtrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.TemplateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.StructLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.StructDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.DotTemplateExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.CommaExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.ScopeExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 import dmd.LINK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 import dmd.DotTemplateInstanceExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 import dmd.TemplateInstance;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 import dmd.DelegateExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 import dmd.IdentifierExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 import dmd.DotVarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 import dmd.TypeAArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 import dmd.RemoveExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 import dmd.UnaExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 import dmd.InlineCostState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 import dmd.InlineDoState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 import dmd.PREC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 import dmd.backend.TYPE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 import dmd.codegen.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
74 import std.stdio;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
75
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 class CallExp : UnaExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 Expressions arguments;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 this(Loc loc, Expression e, Expressions exps)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 super(loc, TOK.TOKcall, CallExp.sizeof, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 this.arguments = exps;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 this(Loc loc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 super(loc, TOK.TOKcall, CallExp.sizeof, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 this(Loc loc, Expression e, Expression earg1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 super(loc, TOK.TOKcall, CallExp.sizeof, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 Expressions arguments = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 arguments.setDim(1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 arguments.data[0] = cast(void*)earg1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 this.arguments = arguments;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 this(Loc loc, Expression e, Expression earg1, Expression earg2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 super(loc, TOK.init, 0, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 Expression syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 return new CallExp(loc, e1.syntaxCopy(), arraySyntaxCopy(arguments));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 Expression semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 TypeFunction tf;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 FuncDeclaration f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 int i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Type t1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 int istemp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 Objects targsi; // initial list of template arguments
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 TemplateInstance tierror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 printf("CallExp.semantic() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 return this; // semantic() already run
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 if (arguments && arguments.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 Expression earg = cast(Expression)arguments.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 earg.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 if (earg.type) earg.type.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 if (e1.op == TOK.TOKdelegate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 DelegateExp de = cast(DelegateExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 e1 = new DotVarExp(de.loc, de.e1, de.func);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 return semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 /* Transform:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 * array.id(args) into .id(array,args)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 * aa.remove(arg) into delete aa[arg]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 if (e1.op == TOK.TOKdot)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 // BUG: we should handle array.a.b.c.e(args) too
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 DotIdExp dotid = cast(DotIdExp)(e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 dotid.e1 = dotid.e1.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 assert(dotid.e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 if (dotid.e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 TY e1ty = dotid.e1.type.toBasetype().ty;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 if (e1ty == TY.Taarray && dotid.ident == Id.remove)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 if (!arguments || arguments.dim != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 error("expected key as argument to aa.remove()");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 goto Lagain;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 Expression key = cast(Expression)arguments.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 key = key.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 key = resolveProperties(sc, key);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 key.rvalue();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 TypeAArray taa = cast(TypeAArray)dotid.e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 key = key.implicitCastTo(sc, taa.index);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 return new RemoveExp(loc, dotid.e1, key);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 else if (e1ty == TY.Tarray || e1ty == TY.Tsarray || e1ty == TY.Taarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 if (!arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 arguments = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 arguments.shift(cast(void*)dotid.e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 e1 = new DotIdExp(dotid.loc, new IdentifierExp(dotid.loc, Id.empty), dotid.ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 e1 = new IdentifierExp(dotid.loc, dotid.ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 /* This recognizes:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 * foo!(tiargs)(funcargs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 if (e1.op == TOK.TOKimport && !e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 ScopeExp se = cast(ScopeExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 TemplateInstance ti = se.sds.isTemplateInstance();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 if (ti && !ti.semanticRun)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 /* Attempt to instantiate ti. If that works, go with it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 * If not, go with partial explicit specialization.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 ti.semanticTiargs(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 uint errors = global.errors;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 global.gag++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 ti.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 global.gag--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 if (errors != global.errors)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 /* Didn't work, go with partial explicit specialization
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 global.errors = errors;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 targsi = ti.tiargs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 tierror = ti; // for error reporting
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 e1 = new IdentifierExp(loc, ti.name);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 /* This recognizes:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 * expr.foo!(tiargs)(funcargs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 if (e1.op == TOK.TOKdotti && !e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 DotTemplateInstanceExp se = cast(DotTemplateInstanceExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 TemplateInstance ti = se.ti;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 if (!ti.semanticRun)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 /* Attempt to instantiate ti. If that works, go with it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 * If not, go with partial explicit specialization.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 ti.semanticTiargs(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 Expression etmp = e1.trySemantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 if (etmp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 e1 = etmp; // it worked
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 else // didn't work
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 targsi = ti.tiargs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 tierror = ti; // for error reporting
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 e1 = new DotIdExp(loc, se.e1, ti.name);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 istemp = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 Lagain:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 //printf("Lagain: %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 f = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 if (e1.op == TOK.TOKthis || e1.op == TOK.TOKsuper)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 // semantic() run later for these
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 UnaExp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 /* Look for e1 being a lazy parameter
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 if (e1.op == TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 if (ve.var.storage_class & STC.STClazy)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 TypeFunction tff = new TypeFunction(null, ve.var.type, 0, LINK.LINKd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 TypeDelegate t = new TypeDelegate(tff);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 ve.type = t.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 if (e1.op == TOK.TOKimport)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 // Perhaps this should be moved to ScopeExp.semantic()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 ScopeExp se = cast(ScopeExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 e1 = new DsymbolExp(loc, se.sds);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 e1 = e1.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 ///static if (true) { // patch for #540 by Oskar Linde
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 else if (e1.op == TOK.TOKdotexp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 DotExp de = cast(DotExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 if (de.e2.op == TOK.TOKimport)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 // This should *really* be moved to ScopeExp.semantic()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 ScopeExp se = cast(ScopeExp)de.e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 de.e2 = new DsymbolExp(loc, se.sds);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 de.e2 = de.e2.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 if (de.e2.op == TOK.TOKtemplate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 TemplateExp te = cast(TemplateExp)de.e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 e1 = new DotTemplateExp(loc,de.e1,te.td);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 ///}
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 if (e1.op == TOK.TOKcomma)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 CommaExp ce = cast(CommaExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 e1 = ce.e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 e1.type = ce.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 ce.e2 = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 ce.type = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 return ce.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 t1 = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 if (e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 t1 = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 // Check for call operator overload
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 if (t1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 AggregateDeclaration ad;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 if (t1.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 ad = (cast(TypeStruct)t1).sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 // First look for constructor
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 if (ad.ctor && arguments && arguments.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 // Create variable that will get constructed
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 Identifier idtmp = Lexer.uniqueId("__ctmp");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 VarDeclaration tmp = new VarDeclaration(loc, t1, idtmp, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 Expression av = new DeclarationExp(loc, tmp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 av = new CommaExp(loc, av, new VarExp(loc, tmp));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 CtorDeclaration cf = ad.ctor.isCtorDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 if (cf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 e = new DotVarExp(loc, av, cf, 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 TemplateDeclaration td = ad.ctor.isTemplateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 assert(td);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 e = new DotTemplateExp(loc, av, td);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 e = new CallExp(loc, e, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 version (STRUCTTHISREF) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 /* Constructors return a pointer to the instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 e = new PtrExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 // No constructor, look for overload of opCall
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 if (search_function(ad, Id.call))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358 goto L1; // overload of opCall, therefore it's a call
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 if (e1.op != TOK.TOKtype)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 error("%s %s does not overload ()", ad.kind(), ad.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 /* It's a struct literal
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 Expression e = new StructLiteralExp(loc, cast(StructDeclaration)ad, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 e.type = e1.type; // in case e1.type was a typedef
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 else if (t1.ty == TY.Tclass)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 ad = (cast(TypeClass)t1).sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375 // Rewrite as e1.call(arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 Expression e = new DotIdExp(loc, e1, Id.call);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 e = new CallExp(loc, e, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 arrayExpressionSemantic(arguments, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 preFunctionArguments(loc, sc, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 if (e1.op == TOK.TOKdotvar && t1.ty == TY.Tfunction ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387 e1.op == TOK.TOKdottd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389 DotVarExp dve;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390 DotTemplateExp dte;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 AggregateDeclaration ad;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 UnaExp ue = cast(UnaExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 if (e1.op == TOK.TOKdotvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 // Do overload resolution
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397 dve = cast(DotVarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
398
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 f = dve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 assert(f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 f = f.overloadResolve(loc, ue.e1, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403 ad = f.toParent().isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 dte = cast(DotTemplateExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408 TemplateDeclaration td = dte.td;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
409 assert(td);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 if (!arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 // Should fix deduceFunctionTemplate() so it works on null argument
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413 arguments = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415 f = td.deduceFunctionTemplate(sc, loc, targsi, ue.e1, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 if (!f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 ad = td.toParent().isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424 if (f.needThis())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 ue.e1 = getRightThis(loc, sc, ad, ue.e1, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429 /* Cannot call public functions from inside invariant
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430 * (because then the invariant would have infinite recursion)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432 if (sc.func && sc.func.isInvariantDeclaration() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433 ue.e1.op == TOK.TOKthis && f.addPostInvariant())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435 error("cannot call public/export function %s from invariant", f.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
437
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438 checkDeprecated(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 checkPurity(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
441 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
442 accessCheck(loc, sc, ue.e1, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
443 if (!f.needThis())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
444 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
445 VarExp ve = new VarExp(loc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
446 e1 = new CommaExp(loc, ue.e1, ve);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
447 e1.type = f.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
448 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
449 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
450 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
451 if (e1.op == TOK.TOKdotvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
452 dve.var = f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
453 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
454 e1 = new DotVarExp(loc, dte.e1, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
455
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
456 e1.type = f.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
457 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
458 printf("ue.e1 = %s\n", ue.e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
459 printf("f = %s\n", f.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
460 printf("t = %s\n", t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
461 printf("e1 = %s\n", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
462 printf("e1.type = %s\n", e1.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
463 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
464 // Const member function can take const/immutable/mutable this
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
465 if (!(f.type.isConst()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
466 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
467 // Check for const/immutable compatibility
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
468 Type tthis = ue.e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
469 if (tthis.ty == TY.Tpointer)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
470 tthis = tthis.nextOf().toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
471
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
472 static if (false) { // this checking should have been already done
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
473 if (f.type.isInvariant())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
474 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
475 if (tthis.mod != MOD.MODinvariant)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
476 error("%s can only be called with an immutable object", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
477 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
478 else if (f.type.isShared())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
479 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
480 if (tthis.mod != MOD.MODinvariant && tthis.mod != MOD.MODshared && tthis.mod != (MOD.MODshared | MOD.MODconst))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
481 error("shared %s can only be called with a shared or immutable object", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
482 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
483 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
484 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
485 if (tthis.mod != MOD.MODundefined)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
486 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
487 //printf("mod = %x\n", tthis.mod);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
488 error("%s can only be called with a mutable object, not %s", e1.toChars(), tthis.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
489 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
490 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
491 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
492 /* Cannot call mutable method on a final struct
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
493 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
494 if (tthis.ty == TY.Tstruct &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
495 ue.e1.op == TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
496 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
497 VarExp v = cast(VarExp)ue.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
498 if (v.var.storage_class & STC.STCfinal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
499 error("cannot call mutable method on final struct");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
500 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
501 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
502
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503 // See if we need to adjust the 'this' pointer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504 AggregateDeclaration add = f.isThis();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
505 ClassDeclaration cd = ue.e1.type.isClassHandle();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
506 if (add && cd && add.isClassDeclaration() && add != cd && ue.e1.op != TOK.TOKsuper)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
507 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
508 ue.e1 = ue.e1.castTo(sc, add.type); //new CastExp(loc, ue.e1, add.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
509 ue.e1 = ue.e1.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
510 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
511 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
512 t1 = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
513 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
514 else if (e1.op == TOK.TOKsuper)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
515 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
516 // Base class constructor call
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
517 ClassDeclaration cd = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
518
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
519 if (sc.func)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
520 cd = sc.func.toParent().isClassDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
521 if (!cd || !cd.baseClass || !sc.func.isCtorDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
522 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
523 error("super class constructor call must be in a constructor");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
524 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
525 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
526 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
527 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
528 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
529 if (!cd.baseClass.ctor)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
530 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
531 error("no super class constructor for %s", cd.baseClass.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
532 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
533 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
534 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
536 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
537 if (!sc.intypeof)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
538 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
539 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 if (sc.callSuper & (CSX.CSXthis | CSX.CSXsuper))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
541 error("reference to this before super()");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
542 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
543 if (sc.noctor || sc.callSuper & CSX.CSXlabel)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
544 error("constructor calls not allowed in loops or after labels");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
545 if (sc.callSuper & (CSX.CSXsuper_ctor | CSX.CSXthis_ctor))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546 error("multiple constructor calls");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
547 sc.callSuper |= CSX.CSXany_ctor | CSX.CSXsuper_ctor;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
548 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
549
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
550 f = resolveFuncCall(sc, loc, cd.baseClass.ctor, null, null, arguments, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
551 checkDeprecated(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
552 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
553 checkPurity(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
554 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
555 e1 = new DotVarExp(e1.loc, e1, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
556 e1 = e1.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
557 t1 = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
558 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
559 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
560 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
561 else if (e1.op == TOK.TOKthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
562 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
563 // same class constructor call
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
564 AggregateDeclaration cd = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
565
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
566 if (sc.func)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
567 cd = sc.func.toParent().isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
568 if (!cd || !sc.func.isCtorDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
569 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
570 error("constructor call must be in a constructor");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
571 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
572 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
573 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
574 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
575 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
576 if (!sc.intypeof)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
577 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
578 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
579 if (sc.callSuper & (CSXthis | CSXsuper))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
580 error("reference to this before super()");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
581 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
582 if (sc.noctor || sc.callSuper & CSX.CSXlabel)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
583 error("constructor calls not allowed in loops or after labels");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
584 if (sc.callSuper & (CSX.CSXsuper_ctor | CSX.CSXthis_ctor))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
585 error("multiple constructor calls");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
586 sc.callSuper |= CSX.CSXany_ctor | CSX.CSXthis_ctor;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
587 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
588
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
589 f = resolveFuncCall(sc, loc, cd.ctor, null, null, arguments, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
590 checkDeprecated(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
591 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
592 checkPurity(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
593 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
594 e1 = new DotVarExp(e1.loc, e1, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
595 e1 = e1.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
596 t1 = e1.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
597
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
598 // BUG: this should really be done by checking the static
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
599 // call graph
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
600 if (f == sc.func)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
601 error("cyclic constructor call");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
602 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
603 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
604 else if (e1.op == TOK.TOKoverloadset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
605 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
606 OverExp eo = cast(OverExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
607 FuncDeclaration ff = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
608 for (int j = 0; j < eo.vars.a.dim; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
609 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
610 Dsymbol s = cast(Dsymbol)eo.vars.a.data[j];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
611 FuncDeclaration f2 = s.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
612 if (f2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
613 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
614 f2 = f2.overloadResolve(loc, null, arguments, 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
615 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
616 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
617 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
618 TemplateDeclaration td = s.isTemplateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
619 assert(td);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
620 f2 = td.deduceFunctionTemplate(sc, loc, targsi, null, arguments, 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
621 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
622 if (f2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
623 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
624 if (ff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
625 /* Error if match in more than one overload set,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
626 * even if one is a 'better' match than the other.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
627 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
628 ScopeDsymbol.multiplyDefined(loc, ff, f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
629 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
630 ff = f2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
631 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
632 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
633 if (!ff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
634 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
635 /* No overload matches, just set ff and rely on error
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
636 * message being generated later.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
637 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
638 ff = cast(FuncDeclaration)eo.vars.a.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
639 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
640 e1 = new VarExp(loc, ff);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
641 goto Lagain;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
642 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
643 else if (!t1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
644 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
645 error("function expected before (), not '%s'", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
646 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
647 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
648 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
649 else if (t1.ty != TY.Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
650 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
651 if (t1.ty == TY.Tdelegate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
652 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
653 TypeDelegate td = cast(TypeDelegate)t1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
654 assert(td.next.ty == TY.Tfunction);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
655 tf = cast(TypeFunction)(td.next);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
656 if (sc.func && sc.func.isPure() && !tf.ispure)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
657 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
658 error("pure function '%s' cannot call impure delegate '%s'", sc.func.toChars(), e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
659 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
660 goto Lcheckargs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
661 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
662 else if (t1.ty == TY.Tpointer && (cast(TypePointer)t1).next.ty == TY.Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
663 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
664 Expression e = new PtrExp(loc, e1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
665 t1 = (cast(TypePointer)t1).next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
666 if (sc.func && sc.func.isPure() && !(cast(TypeFunction)t1).ispure)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
667 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
668 error("pure function '%s' cannot call impure function pointer '%s'", sc.func.toChars(), e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
669 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
670 e.type = t1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
671 e1 = e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
672 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
673 else if (e1.op == TOK.TOKtemplate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
674 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
675 TemplateExp te = cast(TemplateExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
676 f = te.td.deduceFunctionTemplate(sc, loc, targsi, null, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
677 if (!f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
678 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
679 if (tierror)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
680 tierror.error("errors instantiating template"); // give better error message
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
681 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
682 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
683 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
684 if (f.needThis() && hasThis(sc))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
685 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
686 // Supply an implicit 'this', as in
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
687 // this.ident
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
688
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
689 e1 = new DotTemplateExp(loc, (new ThisExp(loc)).semantic(sc), te.td);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
690 goto Lagain;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
691 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
692
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
693 e1 = new VarExp(loc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
694 goto Lagain;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
695 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
696 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
697 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
698 error("function expected before (), not %s of type %s", e1.toChars(), e1.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
699 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
700 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
701 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
702 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
703 else if (e1.op == TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
704 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
705 // Do overload resolution
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
706 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
707
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
708 f = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
709 assert(f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
710
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
711 if (ve.hasOverloads)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
712 f = f.overloadResolve(loc, null, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
713
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
714 checkDeprecated(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
715 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
716 checkPurity(sc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
717 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
718
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
719 if (f.needThis() && hasThis(sc))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
720 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
721 // Supply an implicit 'this', as in
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
722 // this.ident
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
723
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
724 e1 = new DotVarExp(loc, new ThisExp(loc), f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
725 goto Lagain;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
726 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
727
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
728 accessCheck(loc, sc, null, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
729
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
730 ve.var = f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
731 // ve.hasOverloads = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
732 ve.type = f.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
733 t1 = f.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
734 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
735 assert(t1.ty == TY.Tfunction);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
736 tf = cast(TypeFunction)t1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
737
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
738 Lcheckargs:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
739 assert(tf.ty == TY.Tfunction);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
740 type = tf.next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
741
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
742 if (!arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
743 arguments = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
744
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
745 functionArguments(loc, sc, tf, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
746
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
747 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
748 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
749 error("forward reference to inferred return type of function call %s", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
750 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
751 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
752
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
753 if (f && f.tintro)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
754 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
755 Type t = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
756 int offset = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
757 TypeFunction tff = cast(TypeFunction)f.tintro;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
758
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
759 if (tff.next.isBaseOf(t, &offset) && offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
760 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
761 type = tff.next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
762 return castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
763 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
764 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
765
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
766 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
767 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
768
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
769 Expression optimize(int result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
770 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
771 //printf("CallExp::optimize(result = %d) %s\n", result, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
772 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
773
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
774 // Optimize parameters
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
775 if (arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
776 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
777 for (size_t i = 0; i < arguments.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
778 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
779 Expression ee = cast(Expression)arguments.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
780
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
781 ee = ee.optimize(WANT.WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
782 arguments.data[i] = cast(void*)ee;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
783 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
784 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
785
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
786 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
787 if (e1.op == TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
788 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
789 FuncDeclaration fd = (cast(VarExp)e1).var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
790 if (fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
791 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
792 BUILTIN b = fd.isBuiltin();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
793 if (b)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
794 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
795 e = eval_builtin(b, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
796 if (!e) // failed
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
797 e = this; // evaluate at runtime
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
798 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
799 else if (result & WANT.WANTinterpret)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
800 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
801 Expression eresult = fd.interpret(null, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
802 if (eresult && eresult !is EXP_VOID_INTERPRET)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
803 e = eresult;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
804 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
805 error("cannot evaluate %s at compile time", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
806 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
807 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
808 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
809
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
810 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
811 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
812
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
813 Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
814 {
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
815 Expression e = EXP_CANT_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
816
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
817 version (LOG) {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
818 printf("CallExp.interpret() %.*s\n", toChars());
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
819 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
820 if (e1.op == TOKdotvar)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
821 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
822 Expression pthis = (cast(DotVarExp)e1).e1;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
823 FuncDeclaration fd = (cast(DotVarExp)e1).var.isFuncDeclaration();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
824 TypeFunction tf = fd ? cast(TypeFunction)fd.type : null;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
825 if (tf)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
826 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
827 // Member function call
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
828 if(pthis.op == TOKthis)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
829 pthis = istate.localThis;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
830 Expression eresult = fd.interpret(istate, arguments, pthis);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
831 if (eresult)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
832 e = eresult;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
833 else if (fd.type.toBasetype().nextOf().ty == Tvoid && !global.errors)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
834 e = EXP_VOID_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
835 else
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
836 error("cannot evaluate %s at compile time", toChars());
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
837 return e;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
838 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
839 error("cannot evaluate %s at compile time", toChars());
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
840 return EXP_CANT_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
841 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
842 if (e1.op == TOKvar)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
843 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
844 FuncDeclaration fd = (cast(VarExp)e1).var.isFuncDeclaration();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
845 if (fd)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
846 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
847 ///version (DMDV2) {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
848 BUILTIN b = fd.isBuiltin();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
849 if (b)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
850 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
851 scope Expressions args = new Expressions();
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
852 args.setDim(arguments.dim);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
853 for (size_t i = 0; i < args.dim; i++)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
854 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
855 Expression earg = cast(Expression)arguments.data[i];
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
856 earg = earg.interpret(istate);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
857 if (earg == EXP_CANT_INTERPRET)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
858 return earg;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
859 args.data[i] = cast(void*)earg;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
860 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
861 e = eval_builtin(b, args);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
862 if (!e)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
863 e = EXP_CANT_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
864 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
865 else
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
866 ///}
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
867 // Inline .dup
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
868 if (fd.ident == Id.adDup && arguments && arguments.dim == 2)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
869 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
870 e = cast(Expression)arguments.data[1];
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
871 e = e.interpret(istate);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
872 if (e !is EXP_CANT_INTERPRET)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
873 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
874 e = expType(type, e);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
875 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
876 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
877 else
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
878 {
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
879 Expression eresult = fd.interpret(istate, arguments);
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
880 if (eresult)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
881 e = eresult;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
882 else if (fd.type.toBasetype().nextOf().ty == Tvoid && !global.errors)
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
883 e = EXP_VOID_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
884 else
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
885 error("cannot evaluate %s at compile time", toChars());
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
886 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
887 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
888 }
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
889 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
890 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
891
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
892 bool checkSideEffect(int flag)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
893 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
894 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
895 if (flag != 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
896 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
897
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
898 if (e1.checkSideEffect(2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
899 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
900
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
901 /* If any of the arguments have side effects, this expression does
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
902 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
903 for (size_t i = 0; i < arguments.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
904 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
905 Expression e = cast(Expression)arguments.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
906
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
907 if (e.checkSideEffect(2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
908 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
909 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
910
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
911 /* If calling a function or delegate that is typed as pure,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
912 * then this expression has no side effects.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
913 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
914 Type t = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
915 if (t.ty == TY.Tfunction && (cast(TypeFunction)t).ispure)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
916 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
917 if (t.ty == TY.Tdelegate && (cast(TypeFunction)(cast(TypeDelegate)t).next).ispure)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
918 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
919 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
920 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
921 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
922
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
923 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
924 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
925 int i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
926 expToCBuffer(buf, hgs, e1, precedence[op]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
927 buf.writeByte('(');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
928 argsToCBuffer(buf, arguments, hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
929 buf.writeByte(')');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
930 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
931
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
932 void dump(int indent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
933 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
934 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
935 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
936
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
937 elem* toElem(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
938 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
939 //printf("CallExp::toElem('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
940 assert(e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
941 elem* ec;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
942 int directcall;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
943 FuncDeclaration fd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
944 Type t1 = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
945 Type ectype = t1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
946
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
947 elem* ehidden = irs.ehidden;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
948 irs.ehidden = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
949
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
950 directcall = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
951 fd = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
952 if (e1.op == TOK.TOKdotvar && t1.ty != TY.Tdelegate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
953 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
954 DotVarExp dve = cast(DotVarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
955
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
956 fd = dve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
957 Expression ex = dve.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
958 while (1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
959 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
960 switch (ex.op)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
961 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
962 case TOK.TOKsuper: // super.member() calls directly
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
963 case TOK.TOKdottype: // type.member() calls directly
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
964 directcall = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
965 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
966
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
967 case TOK.TOKcast:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
968 ex = (cast(CastExp)ex).e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
969 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
970
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
971 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
972 //ex.dump(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
973 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
974 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
975 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
976 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
977 ec = dve.e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
978 ectype = dve.e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
979 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
980 else if (e1.op == TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
981 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
982 fd = (cast(VarExp)e1).var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
983
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
984 if (fd && fd.ident == Id.alloca && !fd.fbody && fd.linkage == LINK.LINKc && arguments && arguments.dim == 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
985 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
986 Expression arg = cast(Expression)arguments.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
987 arg = arg.optimize(WANT.WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
988 if (arg.isConst() && arg.type.isintegral())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
989 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
990 long sz = arg.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
991 if (sz > 0 && sz < 0x40000)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
992 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
993 // It's an alloca(sz) of a fixed amount.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
994 // Replace with an array allocated on the stack
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
995 // of the same size: char[sz] tmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
996
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
997 Symbol* stmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
998 .type* t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
999
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1000 assert(!ehidden);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1001 t = type_allocn(TYM.TYarray, tschar);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1002 t.Tdim = cast(uint)sz;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1003 stmp = symbol_genauto(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1004 ec = el_ptr(stmp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1005 el_setLoc(ec,loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1006 return ec;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1007 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1008 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1009 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1010
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1011 ec = e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1012 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1013 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1014 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1015 ec = e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1016 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1017 ec = callfunc(loc, irs, directcall, type, ec, ectype, fd, t1, ehidden, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1018 el_setLoc(ec,loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1019 return ec;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1020 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1021
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1022 void scanForNestedRef(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1023 {
64
4290d870944a More fixes
korDen
parents: 63
diff changeset
1024 //printf("CallExp.scanForNestedRef(Scope *sc): %s\n", toChars());
4290d870944a More fixes
korDen
parents: 63
diff changeset
1025 e1.scanForNestedRef(sc);
4290d870944a More fixes
korDen
parents: 63
diff changeset
1026 arrayExpressionScanForNestedRef(sc, arguments);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1027 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1028
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1029 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1030 int isLvalue()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1031 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1032 // if (type.toBasetype().ty == Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1033 // return 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1034 Type tb = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1035 if (tb.ty == Tfunction && (cast(TypeFunction)tb).isref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1036 return 1; // function returns a reference
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1037 return 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1038 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1039 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1040 Expression toLvalue(Scope sc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1041 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1042 if (isLvalue())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1043 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1044 return Expression.toLvalue(sc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1045 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1046
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1047 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1048 bool canThrow()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1049 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1050 //printf("CallExp::canThrow() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1051 if (e1.canThrow())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1052 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1053
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1054 /* If any of the arguments can throw, then this expression can throw
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1055 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1056 for (size_t i = 0; i < arguments.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1057 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1058 Expression e = cast(Expression)arguments.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1059
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1060 if (e && e.canThrow())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1061 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1062 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1063
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1064 if (global.errors && !e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1065 return false; // error recovery
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1066
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1067 /* If calling a function or delegate that is typed as nothrow,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1068 * then this expression cannot throw.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1069 * Note that pure functions can throw.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1070 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1071 Type t = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1072 if (t.ty == TY.Tfunction && (cast(TypeFunction)t).isnothrow)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1073 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1074 if (t.ty == TY.Tdelegate && (cast(TypeFunction)(cast(TypeDelegate)t).next).isnothrow)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1075 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1076
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1077 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1078 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1079 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1080 int inlineCost(InlineCostState* ics)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1081 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1082 return 1 + e1.inlineCost(ics) + arrayInlineCost(ics, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1083 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1084
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1085 Expression doInline(InlineDoState ids)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1086 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1087 CallExp ce = cast(CallExp)copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1088 ce.e1 = e1.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1089 ce.arguments = arrayExpressiondoInline(arguments, ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1090 return ce;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1091 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1092
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1093 Expression inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1094 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1095 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1096
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1097 //printf("CallExp.inlineScan()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1098 e1 = e1.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1099 arrayInlineScan(iss, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1100
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1101 if (e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1102 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1103 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1104 FuncDeclaration fd = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1106 if (fd && fd != iss.fd && fd.canInline(0))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1108 e = fd.doInline(iss, null, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1111 else if (e1.op == TOKdotvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1113 DotVarExp dve = cast(DotVarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1114 FuncDeclaration fd = dve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1115
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1116 if (fd && fd != iss.fd && fd.canInline(1))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1118 if (dve.e1.op == TOKcall &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1119 dve.e1.type.toBasetype().ty == Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1120 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1121 /* To create ethis, we'll need to take the address
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1122 * of dve.e1, but this won't work if dve.e1 is
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1123 * a function call.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1124 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1125 ;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1126 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1127 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1128 e = fd.doInline(iss, dve.e1, arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1129 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1130 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1131
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1132 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1133 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1134 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1135