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