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