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