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