Mercurial > projects > ddmd
annotate dmd/AliasDeclaration.d @ 132:c494af1dba80
Fixes for dmd 2.037
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Fri, 10 Sep 2010 19:14:09 +0100 |
parents | e28b18c23469 |
children | af1bebfd96a4 |
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 | |
103 if (storage_class & STC.STCconst) | |
104 error("cannot be const"); | |
105 | |
106 storage_class |= sc.stc & STC.STCdeprecated; | |
107 | |
108 // Given: | |
109 // alias foo.bar.abc def; | |
110 // it is not knowable from the syntax whether this is an alias | |
111 // for a type or an alias for a symbol. It is up to the semantic() | |
112 // pass to distinguish. | |
113 // If it is a type, then type is set and getType() will return that | |
114 // type. If it is a symbol, then aliassym is set and type is null - | |
115 // toAlias() will return aliasssym. | |
116 | |
117 Dsymbol s; | |
118 Type t; | |
119 Expression e; | |
120 | |
121 /* This section is needed because resolve() will: | |
122 * const x = 3; | |
123 * alias x y; | |
124 * try to alias y to 3. | |
125 */ | |
126 s = type.toDsymbol(sc); | |
127 if (s && ((s.getType() && type.equals(s.getType())) || s.isEnumMember())) | |
128 goto L2; // it's a symbolic alias | |
129 | |
98 | 130 ///version (DMDV2) { |
131 if (storage_class & (STC.STCref | STCnothrow | STCpure)) | |
0 | 132 { // For 'ref' to be attached to function types, and picked |
133 // up by Type.resolve(), it has to go into sc. | |
134 sc = sc.push(); | |
98 | 135 sc.stc |= storage_class & (STCref | STCnothrow | STCpure); |
0 | 136 type.resolve(loc, sc, &e, &t, &s); |
137 sc = sc.pop(); | |
138 } | |
139 else | |
98 | 140 /// #endif |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
141 type.resolve(loc, sc, &e, &t, &s); |
0 | 142 if (s) |
143 { | |
144 goto L2; | |
145 } | |
146 else if (e) | |
147 { | |
148 // Try to convert Expression to Dsymbol | |
149 s = getDsymbol(e); | |
150 if (s) | |
151 goto L2; | |
152 | |
153 error("cannot alias an expression %s", e.toChars()); | |
154 t = e.type; | |
155 } | |
156 else if (t) | |
157 { | |
158 type = t; | |
159 } | |
160 if (overnext) | |
161 ScopeDsymbol.multiplyDefined(Loc(0), this, overnext); | |
162 this.inSemantic = 0; | |
163 return; | |
164 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
165 L2: |
0 | 166 //printf("alias is a symbol %s %s\n", s.kind(), s.toChars()); |
167 type = null; | |
168 VarDeclaration v = s.isVarDeclaration(); | |
169 if (v && v.linkage == LINK.LINKdefault) | |
170 { | |
171 error("forward reference of %s", v.toChars()); | |
172 s = null; | |
173 } | |
174 else | |
175 { | |
176 FuncDeclaration f = s.toAlias().isFuncDeclaration(); | |
177 if (f) | |
178 { | |
179 if (overnext) | |
180 { | |
181 FuncAliasDeclaration fa = new FuncAliasDeclaration(f); | |
182 if (!fa.overloadInsert(overnext)) | |
183 ScopeDsymbol.multiplyDefined(Loc(0), f, overnext); | |
184 overnext = null; | |
185 s = fa; | |
186 s.parent = sc.parent; | |
187 } | |
188 } | |
189 if (overnext) | |
190 ScopeDsymbol.multiplyDefined(Loc(0), s, overnext); | |
191 if (s == this) | |
192 { | |
193 assert(global.errors); | |
194 s = null; | |
195 } | |
196 } | |
197 //printf("setting aliassym %p to %p\n", this, s); | |
198 aliassym = s; | |
199 this.inSemantic = 0; | |
200 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
201 |
72 | 202 override bool overloadInsert(Dsymbol s) |
0 | 203 { |
204 /* Don't know yet what the aliased symbol is, so assume it can | |
205 * be overloaded and check later for correctness. | |
206 */ | |
207 | |
208 //printf("AliasDeclaration.overloadInsert('%s')\n", s.toChars()); | |
209 if (overnext is null) | |
79 | 210 { |
211 static if (true) | |
212 { | |
213 if (s is this) | |
214 return true; | |
215 } | |
0 | 216 overnext = s; |
217 return true; | |
79 | 218 |
0 | 219 } |
220 else | |
221 { | |
222 return overnext.overloadInsert(s); | |
223 } | |
224 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
225 |
72 | 226 override string kind() |
0 | 227 { |
228 return "alias"; | |
229 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
230 |
72 | 231 override Type getType() |
0 | 232 { |
233 return type; | |
234 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
235 |
72 | 236 override Dsymbol toAlias() |
0 | 237 { |
238 //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : ""); | |
239 assert(this !is aliassym); | |
240 //static int count; if (++count == 10) *(char*)0=0; | |
241 if (inSemantic) | |
242 { | |
243 error("recursive alias declaration"); | |
244 aliassym = new TypedefDeclaration(loc, ident, Type.terror, null); | |
245 } | |
246 | |
247 Dsymbol s = aliassym ? aliassym.toAlias() : this; | |
248 return s; | |
249 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
250 |
72 | 251 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 252 { |
253 assert(false); | |
254 } | |
255 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
256 version (_DH) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
257 Type htype; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
258 Dsymbol haliassym; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
259 } |
72 | 260 override void toDocBuffer(OutBuffer buf) |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
261 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
262 assert(false); |
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 |
72 | 265 override AliasDeclaration isAliasDeclaration() { return this; } |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
266 } |