annotate dmd/SymOffExp.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 4290d870944a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.SymOffExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Declaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.MATCH;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.InlineDoState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.backend.dt_t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.SymbolExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.DelegateExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.ThisExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.ErrorExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 class SymOffExp : SymbolExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 uint offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 this(Loc loc, Declaration var, uint offset, int hasOverloads = 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 super(loc, TOK.TOKsymoff, SymOffExp.sizeof, var, hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 this.offset = offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 VarDeclaration v = var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 if (v && v.needThis())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 error("need 'this' for address of %s", v.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 Expression semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 version(LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 printf("SymOffExp::semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 //var.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 type = var.type.pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 VarDeclaration v = var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 if (v)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 v.checkNestedReference(sc, loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 void checkEscape()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 VarDeclaration v = var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 if (v)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 if (!v.isDataseg())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 error("escaping reference to local variable %s", v.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 int isConst()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 return 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 bool isBool(bool result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 Expression doInline(InlineDoState ids)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 int i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 //printf("SymOffExp.doInline(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 for (i = 0; i < ids.from.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 if (var is cast(Declaration)ids.from.data[i])
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 SymOffExp se = cast(SymOffExp)copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 se.var = cast(Declaration)ids.to.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 return se;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 MATCH implicitConvTo(Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n", toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 MATCH result = type.implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 //printf("\tresult = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 if (result == MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 // Look for pointers to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 FuncDeclaration f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 t = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 if (type.ty == Tpointer && type.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 (t.ty == Tpointer || t.ty == Tdelegate) && t.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 f = var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 f = f.overloadExactMatch(t.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 if ((t.ty == Tdelegate && (f.needThis() || f.isNested())) ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 (t.ty == Tpointer && !(f.needThis() || f.isNested())))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 result = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 //printf("\tresult = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 Expression castTo(Scope sc, Type t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n", toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 if (type == t && hasOverloads == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 Type typeb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 if (tb != typeb)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 // Look for pointers to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 FuncDeclaration f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 if (hasOverloads &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 typeb.ty == Tpointer && typeb.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 (tb.ty == Tpointer || tb.ty == Tdelegate) && tb.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 f = var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 f = f.overloadExactMatch(tb.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 if (tb.ty == Tdelegate)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 if (f.needThis() && hasThis(sc))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 e = new DelegateExp(loc, new ThisExp(loc), f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 else if (f.isNested())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 e = new DelegateExp(loc, new IntegerExp(0), f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 else if (f.needThis())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 error("no 'this' to create delegate for %s", f.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 e = new ErrorExp();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 error("cannot cast from function pointer to delegate");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 e = new ErrorExp();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 e = new SymOffExp(loc, f, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 e = Expression.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 e = copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 (cast(SymOffExp)e).hasOverloads = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 void scanForNestedRef(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 dt_t** toDt(dt_t** pdt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 //printf("SymOffExp.toDt('%s')\n", var.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 assert(var);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 if (!(var.isDataseg() || var.isCodeseg()) ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 var.needThis() ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 var.isThreadlocal()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 )
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 debug writef("SymOffExp.toDt()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 error("non-constant expression %s", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 return pdt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 Symbol* s = var.toSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 return dtxoff(pdt, s, offset, TYnptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230