annotate dmd/ForeachStatement.d @ 53:a8b50ff7f201

ForeachStatement.syntaxCopy SliceExp.syntaxCopy AnonDeclaration.syntaxCopy SwitchStatement.syntaxCopy CaseStatement.syntaxCopy BreakStatement.syntaxCopy ThrowStatement.syntaxCopy NewExp.syntaxCopy DefaultStatement.syntaxCopy AssertExp.syntaxCopy ClassDeclaration.syntaxCopy TypeTypedef.constConv eval_builtin ComplexExp.isConst DVCondition.syntaxCopy OrExp.getIntRange AndExp.getIntRange getMask IntegerExp.getIntRange Type.sizemask CastExp.getIntRange Expression.getIntRange
author korDen
date Sat, 21 Aug 2010 12:15:47 +0400
parents 832f71e6f96c
children cab4c37afb89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.ForeachStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.LINK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.BE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.TypeAArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.TypeTuple;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.TupleExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.Initializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.ExpInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.ExpStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.DeclarationExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.BreakStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.DefaultStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 import dmd.CaseStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.SwitchStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 import dmd.AliasDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 import dmd.CompoundStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 import dmd.ScopeStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 import dmd.UnrolledLoopStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 import dmd.Lexer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 import dmd.DeclarationStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 import dmd.CompoundDeclarationStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 import dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 import dmd.TypeClass;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 import dmd.NotExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 import dmd.TypeStruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 import dmd.FuncLiteralDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 import dmd.IdentifierExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 import dmd.TypeFunction;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 import dmd.GotoStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 import dmd.FuncExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 import dmd.ReturnStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 import dmd.IndexExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 import dmd.ForStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 import dmd.SliceExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 import dmd.PostExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 import dmd.AddAssignExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 import dmd.CmpExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 import dmd.Argument;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 import core.stdc.stdio;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 class ForeachStatement : Statement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 TOK op; // TOKforeach or TOKforeach_reverse
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 Arguments arguments; // array of Argument*'s
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 Expression aggr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 Statement body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 VarDeclaration key;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 VarDeclaration value;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 FuncDeclaration func; // function we're lexically in
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 Array cases; // put breaks, continues, gotos and returns here
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 Array gotos; // forward referenced goto's go here
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 this(Loc loc, TOK op, Arguments arguments, Expression aggr, Statement body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 this.op = op;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 this.arguments = arguments;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 this.aggr = aggr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 this.body_ = body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 gotos = new Array();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 cases = new Array();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 Statement syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 {
53
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 12
diff changeset
102 Arguments args = Argument.arraySyntaxCopy(arguments);
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 12
diff changeset
103 Expression exp = aggr.syntaxCopy();
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 12
diff changeset
104 ForeachStatement s = new ForeachStatement(loc, op, args, exp,
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 12
diff changeset
105 body_ ? body_.syntaxCopy() : null);
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 12
diff changeset
106 return s;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 Statement semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 //printf("ForeachStatement.semantic() %p\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 ScopeDsymbol sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 Statement s = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 size_t dim = arguments.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 TypeAArray taa = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 Type tn = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Type tnv = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 func = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 if (func.fes)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 func = func.fes.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 aggr = aggr.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 aggr = resolveProperties(sc, aggr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 aggr = aggr.optimize(WANT.WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 if (!aggr.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 error("invalid foreach aggregate %s", aggr.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 inferApplyArgTypes(op, arguments, aggr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 /* Check for inference errors
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 if (dim != arguments.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 //printf("dim = %d, arguments.dim = %d\n", dim, arguments.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 error("cannot uniquely infer foreach argument types");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 Type tab = aggr.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 if (tab.ty == TY.Ttuple) // don't generate new scope for tuple loops
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 if (dim < 1 || dim > 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 error("only one (value) or two (key,value) arguments for tuple foreach");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 TypeTuple tuple = cast(TypeTuple)tab;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 Statements statements = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 //printf("aggr: op = %d, %s\n", aggr.op, aggr.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 size_t n;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 TupleExp te = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 if (aggr.op == TOK.TOKtuple) // expression tuple
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 te = cast(TupleExp)aggr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 n = te.exps.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 else if (aggr.op == TOK.TOKtype) // type tuple
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 n = Argument.dim(tuple.arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 for (size_t j = 0; j < n; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 size_t k = (op == TOK.TOKforeach) ? j : n - 1 - j;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 if (te)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 e = cast(Expression)te.exps.data[k];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 t = Argument.getNth(tuple.arguments, k).type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 Argument arg = cast(Argument)arguments.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 Statements st = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 if (dim == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 // Declare key
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 if (arg.storageClass & (STC.STCout | STC.STCref | STC.STClazy))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 error("no storage class for key %s", arg.ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 TY keyty = arg.type.ty;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 if (keyty != TY.Tint32 && keyty != TY.Tuns32)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 if (global.params.isX86_64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 if (keyty != TY.Tint64 && keyty != TY.Tuns64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 error("foreach: key type must be int or uint, long or ulong, not %s", arg.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 error("foreach: key type must be int or uint, not %s", arg.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 Initializer ie = new ExpInitializer(Loc(0), new IntegerExp(k));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 VarDeclaration var = new VarDeclaration(loc, arg.type, arg.ident, ie);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 var.storage_class |= STC.STCmanifest;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 DeclarationExp de = new DeclarationExp(loc, var);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 st.push(cast(void*)new ExpStatement(loc, de));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 arg = cast(Argument)arguments.data[1]; // value
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 // Declare value
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 if (arg.storageClass & (STC.STCout | STC.STCref | STC.STClazy))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 error("no storage class for value %s", arg.ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 Dsymbol var;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 if (te)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 Type tb = e.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 if ((tb.ty == TY.Tfunction || tb.ty == TY.Tsarray) && e.op == TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 VarExp ve = cast(VarExp)e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 var = new AliasDeclaration(loc, arg.ident, ve.var);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 arg.type = e.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 Initializer ie = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 VarDeclaration v = new VarDeclaration(loc, arg.type, arg.ident, ie);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 if (e.isConst())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 v.storage_class |= STC.STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 var = v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 var = new AliasDeclaration(loc, arg.ident, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 DeclarationExp de = new DeclarationExp(loc, var);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 st.push(cast(void*)new ExpStatement(loc, de));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 st.push(cast(void*)body_.syntaxCopy());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 s = new CompoundStatement(loc, st);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 s = new ScopeStatement(loc, s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 statements.push(cast(void*)s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 s = new UnrolledLoopStatement(loc, statements);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 s = s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 sym = new ScopeDsymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 sym.parent = sc.scopesym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 sc = sc.push(sym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 sc.noctor++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 switch (tab.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 case TY.Tarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 case TY.Tsarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 if (!checkForArgTypes())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 if (dim < 1 || dim > 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 error("only one or two arguments for array foreach");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 /* Look for special case of parsing char types out of char type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 * array.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 tn = tab.nextOf().toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 if (tn.ty == TY.Tchar || tn.ty == TY.Twchar || tn.ty == TY.Tdchar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 Argument arg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 int i = (dim == 1) ? 0 : 1; // index of value
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 arg = cast(Argument)arguments.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 arg.type = arg.type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 tnv = arg.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 if (tnv.ty != tn.ty && (tnv.ty == TY.Tchar || tnv.ty == TY.Twchar || tnv.ty == TY.Tdchar))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 if (arg.storageClass & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 error("foreach: value of UTF conversion cannot be ref");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 if (dim == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 arg = cast(Argument)arguments.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 if (arg.storageClass & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 error("foreach: key cannot be ref");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 goto Lapply;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 for (size_t i = 0; i < dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 // Declare args
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 Argument arg = cast(Argument)arguments.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 Type argtype = arg.type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 VarDeclaration var = new VarDeclaration(loc, argtype, arg.ident, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 var.storage_class |= STC.STCforeach;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 var.storage_class |= arg.storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STC_TYPECTOR);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 if (var.storage_class & (STC.STCref | STC.STCout))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 var.storage_class |= STC.STCnodtor;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 if (dim == 2 && i == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 key = var;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 //var.storage_class |= STCfinal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 value = var;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 /* Reference to immutable data should be marked as const
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 if (var.storage_class & STC.STCref && !tn.isMutable())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 var.storage_class |= STC.STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 DeclarationExp de = new DeclarationExp(loc, var);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 de.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 /* Convert to a ForStatement
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 * foreach (key, value; a) body =>
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 * for (T[] tmp = a[], size_t key; key < tmp.length; ++key)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 * { T value = tmp[k]; body }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 *
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 * foreach_reverse (key, value; a) body =>
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 * for (T[] tmp = a[], size_t key = tmp.length; key--; )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 * { T value = tmp[k]; body }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 Identifier id = Lexer.uniqueId("__aggr");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 ExpInitializer ie = new ExpInitializer(loc, new SliceExp(loc, aggr, null, null));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 VarDeclaration tmp = new VarDeclaration(loc, aggr.type.nextOf().arrayOf(), id, ie);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 Expression tmp_length = new DotIdExp(loc, new VarExp(loc, tmp), Id.length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 if (!key)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 Identifier id2 = Lexer.uniqueId("__key");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 key = new VarDeclaration(loc, Type.tsize_t, id2, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 if (op == TOK.TOKforeach_reverse)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 key.init = new ExpInitializer(loc, tmp_length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 key.init = new ExpInitializer(loc, new IntegerExp(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 Statements cs = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 cs.push(cast(void*)new DeclarationStatement(loc, new DeclarationExp(loc, tmp)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 cs.push(cast(void*)new DeclarationStatement(loc, new DeclarationExp(loc, key)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 Statement forinit = new CompoundDeclarationStatement(loc, cs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 Expression cond;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 if (op == TOK.TOKforeach_reverse)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 // key--
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 cond = new PostExp(TOK.TOKminusminus, loc, new VarExp(loc, key));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 // key < tmp.length
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 cond = new CmpExp(TOK.TOKlt, loc, new VarExp(loc, key), tmp_length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 Expression increment = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 if (op == TOK.TOKforeach)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 // key += 1
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 increment = new AddAssignExp(loc, new VarExp(loc, key), new IntegerExp(1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 // T value = tmp[key];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 value.init = new ExpInitializer(loc, new IndexExp(loc, new VarExp(loc, tmp), new VarExp(loc, key)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 Statement ds = new DeclarationStatement(loc, new DeclarationExp(loc, value));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 body_ = new CompoundStatement(loc, ds, body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 ForStatement fs = new ForStatement(loc, forinit, cond, increment, body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379 s = fs.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 if (tab.nextOf().implicitConvTo(value.type) < MATCH.MATCHconst)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 if (aggr.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 aggr = aggr.implicitCastTo(sc, value.type.arrayOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 error("foreach: %s is not an array of %s",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389 tab.toChars(), value.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 if (key)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 if (key.type.ty != Tint32 && key.type.ty != Tuns32)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 if (global.params.isX86_64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
398 if (key.type.ty != Tint64 && key.type.ty != Tuns64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 error("foreach: key type must be int or uint, long or ulong, not %s", key.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402 error("foreach: key type must be int or uint, not %s", key.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 if (key.storage_class & (STCout | STCref))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 error("foreach: key cannot be out or ref");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
409 sc.sbreak = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410 sc.scontinue = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 body_ = body_.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415 case TY.Taarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 if (!checkForArgTypes())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 taa = cast(TypeAArray)tab;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 if (dim < 1 || dim > 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 error("only one or two arguments for associative array foreach");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425 if (op == TOK.TOKforeach_reverse)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427 error("no reverse iteration on associative arrays");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429 goto Lapply;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 case TY.Tclass:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432 case TY.Tstruct:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434 { /* Look for range iteration, i.e. the properties
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435 * .empty, .next, .retreat, .head and .rear
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436 * foreach (e; aggr) { ... }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
437 * translates to:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438 * for (auto __r = aggr[]; !__r.empty; __r.next)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 * { auto e = __r.head;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 * ...
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
441 * }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
442 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
443 if (dim != 1) // only one argument allowed with ranges
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
444 goto Lapply;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
445
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
446 AggregateDeclaration ad = (tab.ty == TY.Tclass)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
447 ? cast(AggregateDeclaration)(cast(TypeClass)tab).sym
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
448 : cast(AggregateDeclaration)(cast(TypeStruct)tab).sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
449
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
450 Identifier idhead;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
451 Identifier idnext;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
452 if (op == TOK.TOKforeach)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
453 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
454 idhead = Id.Fhead;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
455 idnext = Id.Fnext;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
456 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
457 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
458 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
459 idhead = Id.Ftoe;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
460 idnext = Id.Fretreat;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
461 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
462
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
463 Dsymbol shead = search_function(ad, idhead);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
464 if (!shead)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
465 goto Lapply;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
466
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
467 /* Generate a temporary __r and initialize it with the aggregate.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
468 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
469 Identifier id = Identifier.generateId("__r");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
470 Expression rinit = new SliceExp(loc, aggr, null, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
471 rinit = rinit.trySemantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
472 if (!rinit) // if application of [] failed
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
473 rinit = aggr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
474
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
475 VarDeclaration r = new VarDeclaration(loc, null, id, new ExpInitializer(loc, rinit));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
476
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
477 // r.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
478 //printf("r: %s, init: %s\n", r.toChars(), r.init.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
479 Statement init = new DeclarationStatement(loc, r);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
480 //printf("init: %s\n", init.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
481
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
482 // !__r.empty
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
483 Expression e = new VarExp(loc, r);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
484 e = new DotIdExp(loc, e, Id.Fempty);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
485 Expression condition = new NotExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
486
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
487 // __r.next
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
488 e = new VarExp(loc, r);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
489 Expression increment = new DotIdExp(loc, e, idnext);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
490
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
491 /* Declaration statement for e:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
492 * auto e = __r.idhead;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
493 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
494 e = new VarExp(loc, r);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
495 Expression einit = new DotIdExp(loc, e, idhead);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
496 // einit = einit.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
497 Argument arg = cast(Argument)arguments.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
498 VarDeclaration ve = new VarDeclaration(loc, arg.type, arg.ident, new ExpInitializer(loc, einit));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
499 ve.storage_class |= STC.STCforeach;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
500 ve.storage_class |= arg.storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STC_TYPECTOR);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
501
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
502 DeclarationExp de = new DeclarationExp(loc, ve);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504 Statement body2 = new CompoundStatement(loc, new DeclarationStatement(loc, de), this.body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
505 s = new ForStatement(loc, init, condition, increment, body2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
506
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
507 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
508 printf("init: %s\n", init.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
509 printf("condition: %s\n", condition.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
510 printf("increment: %s\n", increment.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
511 printf("body: %s\n", body2.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
512 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
513 s = s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
514 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
515 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
516 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
517 case TY.Tdelegate:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
518 Lapply:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
519 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
520 FuncDeclaration fdapply;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
521 Arguments args;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
522 Expression ec;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
523 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
524 FuncLiteralDeclaration fld;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
525 Argument a;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
526 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
527 Expression flde;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
528 Identifier id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
529 Type tret;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
530
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
531 if (!checkForArgTypes())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
532 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
533 body_ = body_.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
534 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
536
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
537 tret = func.type.nextOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
538
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
539 // Need a variable to hold value from any return statements in body.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 if (!sc.func.vresult && tret && tret != Type.tvoid)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
541 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
542 VarDeclaration v = new VarDeclaration(loc, tret, Id.result, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
543 v.noauto = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
544 v.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
545 if (!sc.insert(v))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
547
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
548 v.parent = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
549 sc.func.vresult = v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
550 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
551
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
552 /* Turn body into the function literal:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
553 * int delegate(ref T arg) { body }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
554 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
555 args = new Arguments();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
556 for (size_t i = 0; i < dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
557 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
558 Argument arg = cast(Argument)arguments.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
559
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
560 arg.type = arg.type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
561 if (arg.storageClass & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
562 id = arg.ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
563 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
564 { // Make a copy of the ref argument so it isn't
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
565 // a reference.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
566 VarDeclaration v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
567 Initializer ie;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
568
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
569 id = Lexer.uniqueId("__applyArg", i);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
570
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
571 ie = new ExpInitializer(Loc(0), new IdentifierExp(Loc(0), id));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
572 v = new VarDeclaration(Loc(0), arg.type, arg.ident, ie);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
573 s = new DeclarationStatement(Loc(0), v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
574 body_ = new CompoundStatement(loc, s, body_);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
575 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
576 a = new Argument(STC.STCref, arg.type, id, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
577 args.push(cast(void*)a);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
578 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
579 t = new TypeFunction(args, Type.tint32, 0, LINK.LINKd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
580 fld = new FuncLiteralDeclaration(loc, Loc(0), t, TOK.TOKdelegate, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
581 fld.fbody = body_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
582 flde = new FuncExp(loc, fld);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
583 flde = flde.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
584 fld.tookAddressOf = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
585
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
586 // Resolve any forward referenced goto's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
587 for (int i = 0; i < gotos.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
588 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
589 CompoundStatement cs = cast(CompoundStatement)gotos.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
590 GotoStatement gs = cast(GotoStatement)cs.statements.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
591
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
592 if (!gs.label.statement)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
593 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
594 // 'Promote' it to this scope, and replace with a return
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
595 cases.push(cast(void*)gs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
596 s = new ReturnStatement(Loc(0), new IntegerExp(cases.dim + 1));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
597 cs.statements.data[0] = cast(void*)s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
598 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
599 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
600
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
601 if (tab.ty == TY.Taarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
602 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
603 // Check types
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
604 Argument arg = cast(Argument)arguments.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
605 if (dim == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
606 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
607 if (arg.storageClass & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
608 error("foreach: index cannot be ref");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
609 if (!arg.type.equals(taa.index))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
610 error("foreach: index must be type %s, not %s", taa.index.toChars(), arg.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
611
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
612 arg = cast(Argument)arguments.data[1];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
613 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
614 if (!arg.type.equals(taa.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
615 error("foreach: value must be type %s, not %s", taa.nextOf().toChars(), arg.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
616
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
617 /* Call:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
618 * _aaApply(aggr, keysize, flde)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
619 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
620 if (dim == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
621 fdapply = FuncDeclaration.genCfunc(Type.tindex, "_aaApply2");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
622 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
623 fdapply = FuncDeclaration.genCfunc(Type.tindex, "_aaApply");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
624
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
625 ec = new VarExp(Loc(0), fdapply);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
626 Expressions exps = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
627 exps.push(cast(void*)aggr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
628 size_t keysize = cast(uint)taa.index.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
629 keysize = (keysize + (PTRSIZE-1)) & ~(PTRSIZE-1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
630 exps.push(cast(void*)new IntegerExp(Loc(0), keysize, Type.tsize_t));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
631 exps.push(cast(void*)flde);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
632 e = new CallExp(loc, ec, exps);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
633 e.type = Type.tindex; // don't run semantic() on e
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
634 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
635 else if (tab.ty == TY.Tarray || tab.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
636 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
637 /* Call:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
638 * _aApply(aggr, flde)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
639 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
640 static char fntab[9][3] =
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
641 [ "cc","cw","cd",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
642 "wc","cc","wd",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
643 "dc","dw","dd"
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
644 ];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
645 char fdname[7+1+2+ dim.sizeof*3 + 1];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
646 int flag;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
647
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
648 switch (tn.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
649 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
650 case TY.Tchar: flag = 0; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
651 case TY.Twchar: flag = 3; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
652 case TY.Tdchar: flag = 6; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
653 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
654 switch (tnv.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
655 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
656 case TY.Tchar: flag += 0; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
657 case TY.Twchar: flag += 1; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
658 case TY.Tdchar: flag += 2; break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
659 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
660 string r = (op == TOK.TOKforeach_reverse) ? "R" : "";
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
661 int j = sprintf(fdname.ptr, "_aApply%.*s%.*s%ld".ptr, r, 2, fntab[flag].ptr, dim);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
662 assert(j < fdname.sizeof);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
663 fdapply = FuncDeclaration.genCfunc(Type.tindex, fdname[0..j].idup);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
664
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
665 ec = new VarExp(Loc(0), fdapply);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
666 Expressions exps = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
667 if (tab.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
668 aggr = aggr.castTo(sc, tn.arrayOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
669 exps.push(cast(void*)aggr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
670 exps.push(cast(void*)flde);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
671 e = new CallExp(loc, ec, exps);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
672 e.type = Type.tindex; // don't run semantic() on e
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
673 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
674 else if (tab.ty == TY.Tdelegate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
675 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
676 /* Call:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
677 * aggr(flde)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
678 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
679 Expressions exps = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
680 exps.push(cast(void*)flde);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
681 e = new CallExp(loc, aggr, exps);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
682 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
683 if (e.type != Type.tint32)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
684 error("opApply() function for %s must return an int", tab.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
685 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
686 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
687 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
688 assert(tab.ty == TY.Tstruct || tab.ty == TY.Tclass);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
689 Identifier idapply = (op == TOK.TOKforeach_reverse)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
690 ? Id.applyReverse : Id.apply;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
691 Dsymbol sapply = search_function(cast(AggregateDeclaration)tab.toDsymbol(sc), idapply);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
692 Expressions exps = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
693 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
694 TemplateDeclaration td;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
695 if (sapply && (td = sapply.isTemplateDeclaration()) !is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
696 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
697 /* Call:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
698 * aggr.apply!(fld)()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
699 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
700 TemplateInstance ti = new TemplateInstance(loc, idapply);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
701 Objects tiargs = new Objects();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
702 tiargs.push(cast(void*)fld);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
703 ti.tiargs = tiargs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
704 ec = new DotTemplateInstanceExp(loc, aggr, ti);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
705 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
706 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
707 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
708 /* Call:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
709 * aggr.apply(flde)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
710 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
711 ec = new DotIdExp(loc, aggr, idapply);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
712 exps.push(cast(void*)flde);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
713 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
714 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
715 ec = new DotIdExp(loc, aggr, idapply);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
716 exps.push(cast(void*)flde);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
717 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
718 e = new CallExp(loc, ec, exps);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
719 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
720 if (e.type != Type.tint32) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
721 error("opApply() function for %s must return an int", tab.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
722 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
723 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
724
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
725 if (!cases.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
726 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
727 // Easy case, a clean exit from the loop
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
728 s = new ExpStatement(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
729 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
730 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
731 { // Construct a switch statement around the return value
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
732 // of the apply function.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
733 Statements a2 = new Statements();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
734
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
735 // default: break; takes care of cases 0 and 1
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
736 s = new BreakStatement(Loc(0), null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
737 s = new DefaultStatement(Loc(0), s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
738 a2.push(cast(void*)s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
739
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
740 // cases 2...
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
741 for (int i = 0; i < cases.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
742 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
743 s = cast(Statement)cases.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
744 s = new CaseStatement(Loc(0), new IntegerExp(i + 2), s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
745 a2.push(cast(void*)s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
746 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
747
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
748 s = new CompoundStatement(loc, a2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
749 s = new SwitchStatement(loc, e, s, false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
750 s = s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
751 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
752 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
753 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
754
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
755 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
756 error("foreach: %s is not an aggregate type", aggr.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
757 s = null; // error recovery
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
758 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
759 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
760
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
761 sc.noctor--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
762 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
763 return s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
764 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
765
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
766 bool checkForArgTypes()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
767 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
768 bool result = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
769
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
770 for (size_t i = 0; i < arguments.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
771 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
772 Argument arg = cast(Argument)arguments.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
773 if (!arg.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
774 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
775 error("cannot infer type for %s", arg.ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
776 arg.type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
777 result = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
778 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
779 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
780 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
781 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
782
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
783 bool hasBreak()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
784 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
785 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
786 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
787
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
788 bool hasContinue()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
789 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
790 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
791 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
792
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
793 bool usesEH()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
794 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
795 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
796 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
797
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
798 BE blockExit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
799 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
800 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
801 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
802
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
803 bool comeFrom()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
804 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
805 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
806 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
807
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
808 Expression interpret(InterState* istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
809 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
810 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
811 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
812
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
813 void toCBuffer(OutBuffer uf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
814 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
815 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
816 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
817
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
818 Statement inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
819 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
820 aggr = aggr.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
821 if (body_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
822 body_ = body_.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
823 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
824 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
825
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
826 void toIR(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
827 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
828 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
829 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
830 }