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