Mercurial > projects > ddmd
annotate dmd/AliasDeclaration.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | cd48cb899aee |
children |
rev | line source |
---|---|
0 | 1 module dmd.AliasDeclaration; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.LINK; |
5 import dmd.Declaration; | |
6 import dmd.TypedefDeclaration; | |
7 import dmd.VarDeclaration; | |
8 import dmd.FuncDeclaration; | |
9 import dmd.FuncAliasDeclaration; | |
10 import dmd.Dsymbol; | |
11 import dmd.ScopeDsymbol; | |
12 import dmd.Loc; | |
13 import dmd.Identifier; | |
14 import dmd.Type; | |
15 import dmd.OutBuffer; | |
16 import dmd.HdrGenState; | |
17 import dmd.Scope; | |
18 import dmd.STC; | |
19 import dmd.Expression; | |
20 import dmd.Global; | |
21 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
22 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
23 |
0 | 24 class AliasDeclaration : Declaration |
25 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
26 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
179
diff
changeset
|
27 |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
28 Dsymbol aliassym; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
29 Dsymbol overnext; // next in overload list |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
30 int inSemantic; |
0 | 31 |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
32 this(Loc loc, Identifier ident, Type type) |
0 | 33 { |
178 | 34 register(); |
0 | 35 super(ident); |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
36 |
0 | 37 //printf("AliasDeclaration(id = '%s', type = %p)\n", id.toChars(), type); |
38 //printf("type = '%s'\n", type.toChars()); | |
39 this.loc = loc; | |
40 this.type = type; | |
41 this.aliassym = null; | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
42 version (_DH) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
43 this.htype = null; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
44 this.haliassym = null; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
45 } |
0 | 46 |
47 assert(type); | |
48 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
49 |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
50 this(Loc loc, Identifier id, Dsymbol s) |
0 | 51 { |
178 | 52 register(); |
0 | 53 super(id); |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
54 |
0 | 55 //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s); |
56 assert(s !is this); /// huh? | |
57 this.loc = loc; | |
58 this.type = null; | |
59 this.aliassym = s; | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
60 version (_DH) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
61 this.htype = null; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
62 this.haliassym = null; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
63 } |
0 | 64 assert(s); |
65 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
66 |
72 | 67 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 68 { |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
69 //printf("AliasDeclaration::syntaxCopy()\n"); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
70 assert(!s); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
71 AliasDeclaration sa; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
72 if (type) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
73 sa = new AliasDeclaration(loc, ident, type.syntaxCopy()); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
74 else |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
75 sa = new AliasDeclaration(loc, ident, aliassym.syntaxCopy(null)); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
76 version (_DH) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
77 // Syntax copy for header file |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
78 if (!htype) // Don't overwrite original |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
79 { if (type) // Make copy for both old and new instances |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
80 { htype = type.syntaxCopy(); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
81 sa.htype = type.syntaxCopy(); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
82 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
83 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
84 else // Make copy of original for new instance |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
85 sa.htype = htype.syntaxCopy(); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
86 if (!haliassym) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
87 { if (aliassym) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
88 { haliassym = aliassym.syntaxCopy(s); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
89 sa.haliassym = aliassym.syntaxCopy(s); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
90 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
91 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
92 else |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
93 sa.haliassym = haliassym.syntaxCopy(s); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
94 } // version (_DH) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
95 return sa; |
0 | 96 } |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
97 |
72 | 98 override void semantic(Scope sc) |
0 | 99 { |
100 //printf("AliasDeclaration.semantic() %s\n", toChars()); | |
101 if (aliassym) | |
102 { | |
103 if (aliassym.isTemplateInstance()) | |
104 aliassym.semantic(sc); | |
105 return; | |
106 } | |
107 this.inSemantic = 1; | |
108 | |
135 | 109 version(DMDV1) { // don't really know why this is here |
0 | 110 if (storage_class & STC.STCconst) |
111 error("cannot be const"); | |
135 | 112 } |
0 | 113 storage_class |= sc.stc & STC.STCdeprecated; |
114 | |
115 // Given: | |
116 // alias foo.bar.abc def; | |
117 // it is not knowable from the syntax whether this is an alias | |
118 // for a type or an alias for a symbol. It is up to the semantic() | |
119 // pass to distinguish. | |
120 // If it is a type, then type is set and getType() will return that | |
121 // type. If it is a symbol, then aliassym is set and type is null - | |
122 // toAlias() will return aliasssym. | |
123 | |
124 Dsymbol s; | |
125 Type t; | |
126 Expression e; | |
127 | |
128 /* This section is needed because resolve() will: | |
129 * const x = 3; | |
130 * alias x y; | |
131 * try to alias y to 3. | |
132 */ | |
133 s = type.toDsymbol(sc); | |
134 if (s && ((s.getType() && type.equals(s.getType())) || s.isEnumMember())) | |
135 goto L2; // it's a symbolic alias | |
136 | |
98 | 137 ///version (DMDV2) { |
135 | 138 type = type.addStorageClass(storage_class); |
179 | 139 if (storage_class & (STC.STCref | STCnothrow | STCpure | STCdisable)) |
0 | 140 { // For 'ref' to be attached to function types, and picked |
141 // up by Type.resolve(), it has to go into sc. | |
142 sc = sc.push(); | |
179 | 143 sc.stc |= storage_class & (STCref | STCnothrow | STCpure | STCshared | STCdisable); |
0 | 144 type.resolve(loc, sc, &e, &t, &s); |
145 sc = sc.pop(); | |
146 } | |
147 else | |
98 | 148 /// #endif |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
149 type.resolve(loc, sc, &e, &t, &s); |
0 | 150 if (s) |
151 { | |
152 goto L2; | |
153 } | |
154 else if (e) | |
155 { | |
156 // Try to convert Expression to Dsymbol | |
157 s = getDsymbol(e); | |
158 if (s) | |
159 goto L2; | |
160 | |
161 error("cannot alias an expression %s", e.toChars()); | |
162 t = e.type; | |
163 } | |
164 else if (t) | |
165 { | |
166 type = t; | |
167 } | |
168 if (overnext) | |
169 ScopeDsymbol.multiplyDefined(Loc(0), this, overnext); | |
170 this.inSemantic = 0; | |
171 return; | |
172 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
173 L2: |
0 | 174 //printf("alias is a symbol %s %s\n", s.kind(), s.toChars()); |
175 type = null; | |
176 VarDeclaration v = s.isVarDeclaration(); | |
177 if (v && v.linkage == LINK.LINKdefault) | |
178 { | |
179 error("forward reference of %s", v.toChars()); | |
180 s = null; | |
181 } | |
182 else | |
183 { | |
184 FuncDeclaration f = s.toAlias().isFuncDeclaration(); | |
185 if (f) | |
186 { | |
187 if (overnext) | |
188 { | |
189 FuncAliasDeclaration fa = new FuncAliasDeclaration(f); | |
190 if (!fa.overloadInsert(overnext)) | |
191 ScopeDsymbol.multiplyDefined(Loc(0), f, overnext); | |
192 overnext = null; | |
193 s = fa; | |
194 s.parent = sc.parent; | |
195 } | |
196 } | |
197 if (overnext) | |
198 ScopeDsymbol.multiplyDefined(Loc(0), s, overnext); | |
199 if (s == this) | |
200 { | |
201 assert(global.errors); | |
202 s = null; | |
203 } | |
204 } | |
205 //printf("setting aliassym %p to %p\n", this, s); | |
206 aliassym = s; | |
207 this.inSemantic = 0; | |
208 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
209 |
72 | 210 override bool overloadInsert(Dsymbol s) |
0 | 211 { |
212 /* Don't know yet what the aliased symbol is, so assume it can | |
213 * be overloaded and check later for correctness. | |
214 */ | |
215 | |
216 //printf("AliasDeclaration.overloadInsert('%s')\n", s.toChars()); | |
217 if (overnext is null) | |
79 | 218 { |
219 static if (true) | |
220 { | |
221 if (s is this) | |
222 return true; | |
223 } | |
0 | 224 overnext = s; |
225 return true; | |
79 | 226 |
0 | 227 } |
228 else | |
229 { | |
230 return overnext.overloadInsert(s); | |
231 } | |
232 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
233 |
72 | 234 override string kind() |
0 | 235 { |
236 return "alias"; | |
237 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
238 |
72 | 239 override Type getType() |
0 | 240 { |
241 return type; | |
242 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
243 |
72 | 244 override Dsymbol toAlias() |
0 | 245 { |
246 //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : ""); | |
247 assert(this !is aliassym); | |
248 //static int count; if (++count == 10) *(char*)0=0; | |
249 if (inSemantic) | |
250 { | |
251 error("recursive alias declaration"); | |
252 aliassym = new TypedefDeclaration(loc, ident, Type.terror, null); | |
253 } | |
254 | |
255 Dsymbol s = aliassym ? aliassym.toAlias() : this; | |
256 return s; | |
257 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
258 |
72 | 259 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 260 { |
174 | 261 buf.writestring("alias "); |
262 /// static if (false) { // && _DH | |
263 /// if (hgs.hdrgen) | |
264 /// { | |
265 /// if (haliassym) | |
266 /// { | |
267 /// haliassym.toCBuffer(buf, hgs); | |
268 /// buf.writeByte(' '); | |
269 /// buf.writestring(ident.toChars()); | |
270 /// } | |
271 /// else | |
272 /// htype.toCBuffer(buf, ident, hgs); | |
273 /// } | |
274 /// else | |
275 /// } | |
276 { | |
277 if (aliassym) | |
278 { | |
279 aliassym.toCBuffer(buf, hgs); | |
280 buf.writeByte(' '); | |
281 buf.writestring(ident.toChars()); | |
282 } | |
283 else | |
284 type.toCBuffer(buf, ident, hgs); | |
285 } | |
286 buf.writeByte(';'); | |
287 buf.writenl(); | |
0 | 288 } |
289 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
290 version (_DH) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
291 Type htype; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
292 Dsymbol haliassym; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
293 } |
72 | 294 override void toDocBuffer(OutBuffer buf) |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
295 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
296 assert(false); |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
297 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
298 |
72 | 299 override AliasDeclaration isAliasDeclaration() { return this; } |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
300 } |