annotate dmd/Expression.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 ccbc1e0bb3f0
children 8c2c7b7579f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.TOK;
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
5 import dmd.Argument;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
6 import dmd.IdentifierExp;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.MATCH;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.IntRange;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.InlineCostState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.InlineDoState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.TypeExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.DYNCAST;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.TemplateInstance;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.CommaExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.NullExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.AddrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.ErrorExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.TypeStruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 import dmd.CastExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.Token;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 import dmd.TypeClass;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 import dmd.PtrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 import dmd.TypeReference;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 import dmd.Complex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 import dmd.backend.dt_t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
4
d706d958e4e8 Step 2 of restoring GC functionality.
korDen
parents: 2
diff changeset
49 import core.memory;
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
50
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 import std.stdio : writef;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 import std.conv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 /* Things like:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 * int.size
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 * foo.size
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 * (foo).size
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 * cast(foo).size
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 Expression typeDotIdExp(Loc loc, Type type, Identifier ident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 return new DotIdExp(loc, new TypeExp(loc, type), ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 /*****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 * Determine if 'this' is available.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 * If it is, return the FuncDeclaration that has it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 FuncDeclaration hasThis(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 FuncDeclaration fd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 FuncDeclaration fdthis;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 //printf("hasThis()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 fdthis = sc.parent.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis.toChars() : "");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 // Go upwards until we find the enclosing member function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 fd = fdthis;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 while (1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 if (!fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 goto Lno;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 if (!fd.isNested())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 Dsymbol parent = fd.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 while (parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 TemplateInstance ti = parent.isTemplateInstance();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 if (ti)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 parent = ti.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 fd = fd.parent.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 if (!fd.isThis())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 //printf("test '%s'\n", fd.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 goto Lno;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 assert(fd.vthis);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 return fd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 Lno:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 return null; // don't have 'this' available
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 /***************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 * Pull out any properties.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 Expression resolveProperties(Scope sc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 //printf("resolveProperties(%s)\n", e.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 if (e.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 Type t = e.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 if (t.ty == TY.Tfunction || e.op == TOK.TOKoverloadset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 e = new CallExp(e.loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 /* Look for e being a lazy parameter; rewrite as delegate call
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 else if (e.op == TOK.TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 { VarExp ve = cast(VarExp)e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 if (ve.var.storage_class & STC.STClazy)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 e = new CallExp(e.loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 e = e.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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 else if (e.op == TOK.TOKdotexp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 e.error("expression has no value");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 else if (e.op == TOK.TOKdottd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 e = new CallExp(e.loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 class Expression
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 Loc loc; // file location
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 TOK op; // handy to minimize use of dynamic_cast
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 Type type; // !=null means that semantic() has been run
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 int size; // # of bytes in Expression so we can copy() it
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 this(Loc loc, TOK op, int size)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 //writef("Expression.Expression(op = %d %s) this = %p\n", op, to!(string)(op), this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 this.op = op;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 this.size = size;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 type = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176
45
ccbc1e0bb3f0 StringExp.equals implemented
korDen
parents: 23
diff changeset
177 bool equals(Object o)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 return this is o;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 /*********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 * Does *not* do a deep copy.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 Expression copy() /// bad bad bad
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 if (!size)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 debug {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 writef("No expression copy for: %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 writef("op = %d\n", op);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 dump(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 auto size = this.classinfo.init.length;
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
198 auto ptr = GC.malloc(size);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 memcpy(ptr, cast(void*)this, size);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 return cast(Expression)ptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 Expression syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 //printf("Expression::syntaxCopy()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 //dump(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 return copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 Expression semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 printf("Expression.semantic() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 type = type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 type = Type.tvoid;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 Expression trySemantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 uint errors = global.errors;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 global.gag++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 Expression e = semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 global.gag--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 if (errors != global.errors)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 global.errors = errors;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 e = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 DYNCAST dyncast() { return DYNCAST.DYNCAST_EXPRESSION; } // kludge for template.isExpression()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 void print()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 string toChars()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 scope OutBuffer buf = new OutBuffer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 HdrGenState hgs;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 toCBuffer(buf, &hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 return buf.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 void dump(int indent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 void error(T...)(string format, T t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 .error(loc, format, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 void warning(T...)(string formar, T t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 .warning(loc, format, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 void rvalue()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 if (type && type.toBasetype().ty == TY.Tvoid)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 error("expression %s is void and has no value", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 dump(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 halt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 static Expression combine(Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 if (e1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 if (e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 e1 = new CommaExp(e1.loc, e1, e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 e1.type = e2.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 e1 = e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 return e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 static Expressions arraySyntaxCopy(Expressions exps)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 Expressions a = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 if (exps)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 a = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 a.setDim(exps.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 for (int i = 0; i < a.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 Expression e = cast(Expression)exps.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 e = e.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 a.data[i] = cast(void*)e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 return a;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 ulong toInteger()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 ulong toUInteger()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 //printf("Expression %s\n", Token.toChars(op));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 return cast(ulong)toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 real toReal()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 real toImaginary()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 Complex!(real) toComplex()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 buf.writestring(Token.toChars(op));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 void toMangleBuffer(OutBuffer buf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 int isLvalue()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 Expression toLvalue(Scope sc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 Expression modifiableLvalue(Scope sc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 //printf("Expression::modifiableLvalue() %s, type = %s\n", toChars(), type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 // See if this expression is a modifiable lvalue (i.e. not const)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 if (type && (!type.isMutable() || !type.isAssignable()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 error("%s is not mutable", e.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 return toLvalue(sc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 /**************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 * Do an implicit cast.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 * Issue error if it can't be done.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 Expression implicitCastTo(Scope sc, Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382 //printf("Expression.implicitCastTo(%s of type %s) => %s\n", toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 MATCH match = implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 if (match)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387 TY tyfrom = type.toBasetype().ty;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 TY tyto = t.toBasetype().ty;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390 version (DMDV1) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 if (global.params.warnings &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 Type.impcnvWarn[tyfrom][tyto] &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393 op != TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 Expression e = optimize(WANT.WANTflags | WANT.WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397 if (e.op == TOK.TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
398 return e.implicitCastTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 if (tyfrom == Tint32 && (op == TOKadd || op == TOKmin || op == TOKand || op == TOKor || op == TOKxor))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 /* This is really only a semi-kludge fix,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402 * we really should look at the operands of op
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403 * and see if they are narrower types.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 * For example, b=b|b and b=b|7 and s=b+b should be allowed,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 * but b=b|i should be an error.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 ;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
409 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 warning("implicit conversion of expression (%s) of type %s to %s can cause loss of data", toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 if (match == MATCH.MATCHconst && t == type.constOf())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418 Expression e = copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 return castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 Expression e = optimize(WANT.WANTflags | WANT.WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427 if (e != this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 return e.implicitCastTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 printf("ty = %d\n", type.ty);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432 print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433 type.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434 printf("to:\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435 t.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436 printf("%p %p type: %s to: %s\n", type.deco, t.deco, type.deco, t.deco);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
437 //printf("%p %p %p\n", type.nextOf().arrayOf(), type, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438 fflush(stdout);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 if (!t.deco) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
441 /* Can happen with:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
442 * enum E { One }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
443 * class A
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
444 * { static void fork(EDG dg) { dg(E.One); }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
445 * alias void delegate(E) EDG;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
446 * }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
447 * Should eventually make it work.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
448 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
449 error("forward reference to type %s", t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
450 } else if (t.reliesOnTident()) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
451 error("forward reference to type %s", t.reliesOnTident().toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
452 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
453
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
454 error("cannot implicitly convert expression (%s) of type %s to %s", toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
455 return castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
456 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
457
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
458 /*******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
459 * Return !=0 if we can implicitly convert this to type t.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
460 * Don't do the actual cast.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
461 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
462 MATCH implicitConvTo(Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
463 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
464 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
465 printf("Expression.implicitConvTo(this=%s, type=%s, t=%s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
466 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
467 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
468 //static int nest; if (++nest == 10) halt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
469 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
470 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
471 error("%s is not an expression", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
472 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
473 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
474 Expression e = optimize(WANT.WANTvalue | WANT.WANTflags);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
475 if (e.type == t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
476 return MATCH.MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
477 if (e != this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
478 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
479 //printf("\toptimized to %s of type %s\n", e.toChars(), e.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
480 return e.implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
481 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
482 MATCH match = type.implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
483 if (match != MATCH.MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
484 return match;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
485
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
486 /* See if we can do integral narrowing conversions
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
487 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
488 if (type.isintegral() && t.isintegral() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
489 type.isTypeBasic() && t.isTypeBasic())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
490 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
491 IntRange ir = getIntRange();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
492 if (ir.imax <= t.sizemask())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
493 return MATCH.MATCHconvert;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
494 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
495
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
496 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
497 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
498 if (tb.ty == Tdelegate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
499 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
500 TypeDelegate td = cast(TypeDelegate)tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
501 TypeFunction tf = cast(TypeFunction)td.nextOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
502
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503 if (!tf.varargs && !(tf.arguments && tf.arguments.dim))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
505 match = type.implicitConvTo(tf.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
506 if (match)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
507 return match;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
508 if (tf.nextOf().toBasetype().ty == Tvoid)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
509 return MATCH.MATCHconvert;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
510 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
511 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
512 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
513 return MATCH.MATCHnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
514 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
515
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
516 IntRange getIntRange()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
517 {
53
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 45
diff changeset
518 IntRange ir;
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 45
diff changeset
519 ir.imin = 0;
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 45
diff changeset
520 ir.imax = type.sizemask();
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 45
diff changeset
521 return ir;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
522 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
523
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
524 /**************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
525 * Do an explicit cast.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
526 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
527 Expression castTo(Scope sc, Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
528 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
529 //printf("Expression.castTo(this=%s, t=%s)\n", toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
530 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
531 writef("Expression.castTo(this=%s, type=%s, t=%s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
532 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
533 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
534 if (type is t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
536 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
537 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
538 Type typeb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
539 if (tb != typeb)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
541 // Do (type *) cast of (type [dim])
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
542 if (tb.ty == TY.Tpointer && typeb.ty == TY.Tsarray
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
543 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
544 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
545 //printf("Converting [dim] to *\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
547 if (typeb.size(loc) == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
548 e = new NullExp(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
549 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
550 e = new AddrExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
551 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
552 else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
553 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
554 if (tb.ty == Tdelegate && type.ty != Tdelegate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
555 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
556 TypeDelegate td = cast(TypeDelegate)tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
557 TypeFunction tf = cast(TypeFunction)td.nextOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
558 return toDelegate(sc, tf.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
559 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
560 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
561 if (typeb.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
562 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
563 TypeStruct ts = cast(TypeStruct)typeb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
564 if (!(tb.ty == TY.Tstruct && ts.sym == (cast(TypeStruct)tb).sym) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
565 ts.sym.aliasthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
566 { /* Forward the cast to our alias this member, rewrite to:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
567 * cast(to)e1.aliasthis
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
568 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
569 Expression e1 = new DotIdExp(loc, this, ts.sym.aliasthis.ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
570 Expression e2 = new CastExp(loc, e1, tb);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
571 e2 = e2.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
572 return e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
573 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
574 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
575 else if (typeb.ty == TY.Tclass)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
576 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
577 TypeClass ts = cast(TypeClass)typeb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
578 if (tb.ty != TY.Tclass && ts.sym.aliasthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
579 { /* Forward the cast to our alias this member, rewrite to:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
580 * cast(to)e1.aliasthis
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
581 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
582 Expression e1 = new DotIdExp(loc, this, ts.sym.aliasthis.ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
583 Expression e2 = new CastExp(loc, e1, tb);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
584 e2 = e2.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
585 return e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
586 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
587 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
588 e = new CastExp(loc, e, tb);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
589 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
590 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
591 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
592 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
593 e = e.copy(); // because of COW for assignment to e.type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
594 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
595
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
596 assert(e != this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
597 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
598 //printf("Returning: %s\n", e.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
599 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
600 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
601
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
602 /************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
603 * Detect cases where pointers to the stack can 'escape' the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
604 * lifetime of the stack frame.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
605 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
606 void checkEscape()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
607 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
608 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
609
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
610 void checkScalar()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
611 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
612 if (!type.isscalar())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
613 error("'%s' is not a scalar, it is a %s", toChars(), type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
614
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
615 rvalue();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
616 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
617
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
618 void checkNoBool()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
619 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
620 if (type.toBasetype().ty == TY.Tbool)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
621 error("operation not allowed on bool '%s'", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
622 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
623
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
624 Expression checkIntegral()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
625 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
626 if (!type.isintegral())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
627 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
628 error("'%s' is not of integral type, it is a %s", toChars(), type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
629 return new ErrorExp();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
630 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
631
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
632 rvalue();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
633 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
634 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
635
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
636 Expression checkArithmetic()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
637 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
638 if (!type.isintegral() && !type.isfloating())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
639 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
640 error("'%s' is not of arithmetic type, it is a %s", toChars(), type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
641 return new ErrorExp();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
642 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
643
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
644 rvalue();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
645 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
646 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
647
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
648 void checkDeprecated(Scope sc, Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
649 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
650 s.checkDeprecated(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
651 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
652
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
653 void checkPurity(Scope sc, FuncDeclaration f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
654 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
655 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
656 if (sc.func)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
657 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
658 FuncDeclaration outerfunc = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
659 while (outerfunc.toParent2() && outerfunc.toParent2().isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
660 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
661 outerfunc = outerfunc.toParent2().isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
662 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
663 if (outerfunc.isPure() && !sc.intypeof && (!f.isNested() && !f.isPure()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
664 error("pure function '%s' cannot call impure function '%s'\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
665 sc.func.toChars(), f.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
666 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
667 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
668 if (sc.func && sc.func.isPure() && !sc.intypeof && !f.isPure())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
669 error("pure function '%s' cannot call impure function '%s'\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
670 sc.func.toChars(), .toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
671 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
672 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
673
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
674 /*****************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
675 * Check that expression can be tested for true or false.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
676 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
677 Expression checkToBoolean()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
678 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
679 // Default is 'yes' - do nothing
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
680
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
681 debug {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
682 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
683 dump(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
684 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
685
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
686 if (!type.checkBoolean())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
687 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
688 error("expression %s of type %s does not have a boolean value", toChars(), type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
689 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
690
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
691 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
692 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
693
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
694 Expression checkToPointer()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
695 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
696 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
697 Type tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
698
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
699 //printf("Expression::checkToPointer()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
700 e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
701
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
702 // If C static array, convert to pointer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
703 tb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
704 if (tb.ty == Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
705 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
706 TypeSArray ts = cast(TypeSArray)tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
707 if (ts.size(loc) == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
708 e = new NullExp(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
709 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
710 e = new AddrExp(loc, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
711 e.type = ts.next.pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
712 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
713 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
714 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
715
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
716 Expression addressOf(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
717 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
718 //printf("Expression::addressOf()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
719 Expression e = toLvalue(sc, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
720 e = new AddrExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
721 e.type = type.pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
722 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
723 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
724
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
725 /******************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
726 * If this is a reference, dereference it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
727 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
728 Expression deref()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
729 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
730 //printf("Expression::deref()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
731 if (type.ty == TY.Treference)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
732 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
733 Expression e = new PtrExp(loc, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
734 e.type = (cast(TypeReference)type).next;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
735 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
736 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
737 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
738 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
739
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
740 /***********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
741 * Do integral promotions (convertchk).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
742 * Don't convert <array of> to <pointer to>
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
743 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
744 Expression integralPromotions(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
745 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
746 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
747
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
748 //printf("integralPromotions %s %s\n", e.toChars(), e.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
749 switch (type.toBasetype().ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
750 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
751 case TY.Tvoid:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
752 error("void has no value");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
753 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
754
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
755 case TY.Tint8:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
756 case TY.Tuns8:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
757 case TY.Tint16:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
758 case TY.Tuns16:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
759 case TY.Tbit:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
760 case TY.Tbool:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
761 case TY.Tchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
762 case TY.Twchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
763 e = e.castTo(sc, Type.tint32);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
764 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
765
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
766 case TY.Tdchar:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
767 e = e.castTo(sc, Type.tuns32);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
768 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
769 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
770 break; ///
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
771 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
772 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
773 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
774
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
775 Expression toDelegate(Scope sc, Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
776 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
777 assert(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 void scanForNestedRef(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
781 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
782 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
783 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
784
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
785 Expression optimize(int result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
786 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
787 //printf("Expression.optimize(result = x%x) %s\n", result, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
788 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
789 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
790
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
791 Expression interpret(InterState istate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
792 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
793 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
794 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
795
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
796 int isConst()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
797 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
798 //printf("Expression::isConst(): %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
799 return 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
800 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
801
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
802 /********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
803 * Does this expression statically evaluate to a boolean TRUE or FALSE?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
804 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
805 bool isBool(bool result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
806 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
807 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
808 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
809
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
810 int isBit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
811 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
812 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
813 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
814
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
815 /********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
816 * Check for expressions that have no use.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
817 * Input:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
818 * flag 0 not going to use the result, so issue error message if no
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
819 * side effects
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
820 * 1 the result of the expression is used, but still check
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
821 * for useless subexpressions
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
822 * 2 do not issue error messages, just return !=0 if expression
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
823 * has side effects
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
824 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
825 bool checkSideEffect(int flag)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
826 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
827 if (flag == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
828 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
829 if (op == TOKimport)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
830 error("%s has no effect", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
831 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
832 error("%s has no effect in expression (%s)",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
833
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
834 Token.toChars(op), toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
835 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
836
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
837 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
838 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
839
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
840 bool canThrow()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
841 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
842 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
843 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
844 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
845 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
846 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
847 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
848
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
849 int inlineCost(InlineCostState* ics)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
850 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
851 return 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
852 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
853
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
854 Expression doInline(InlineDoState ids)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
855 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
856 //printf("Expression.doInline(%s): %s\n", Token.toChars(op), toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
857 return copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
858 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
859
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
860 Expression inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
861 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
862 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
863 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
864
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
865 /***********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
866 * Determine if operands of binary op can be reversed
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
867 * to fit operator overload.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
868 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
869
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
870 // For operator overloading
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
871 bool isCommutative()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
872 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
873 return false; // default is no reverse
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
874 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
875
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
876 /***********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
877 * Get Identifier for operator overload.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
878 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
879 Identifier opId()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
880 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
881 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
882 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
883
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
884 /***********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
885 * Get Identifier for reverse operator overload,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
886 * null if not supported for this operator.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
887 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
888 Identifier opId_r()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
889 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
890 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
891 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
892
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
893 // For array ops
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
894 /******************************************
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
895 * Construct the identifier for the array operation function,
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
896 * and build the argument list to pass to it.
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
897 */
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
898 void buildArrayIdent(OutBuffer buf, Expressions arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
899 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
900 buf.writestring("Exp");
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
901 arguments.shift(cast(void*)this);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
902 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
903
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
904 Expression buildArrayLoop(Arguments fparams)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
905 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
906 Identifier id = Identifier.generateId("c", fparams.dim);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
907 Argument param = new Argument(STC.STCundefined, type, id, null);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
908 fparams.shift(cast(void*)param);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
909 Expression e = new IdentifierExp(Loc(0), id);
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 4
diff changeset
910 return e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
911 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
912
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
913 // Back end
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
914 elem* toElem(IRState* irs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
915 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
916 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
917 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
918
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
919 dt_t** toDt(dt_t** pdt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
920 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
921 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
922 }
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 4
diff changeset
923 }