0
|
1 module dmd.Declaration;
|
|
2
|
|
3 import dmd.Dsymbol;
|
|
4 import dmd.Type;
|
|
5 import dmd.PROT;
|
|
6 import dmd.LINK;
|
|
7 import dmd.Identifier;
|
|
8 import dmd.Scope;
|
|
9 import dmd.Loc;
|
|
10 import dmd.STC;
|
|
11 import dmd.FuncDeclaration;
|
|
12 import dmd.VarDeclaration;
|
|
13 import dmd.OutBuffer;
|
|
14
|
|
15 import std.stdio : writef;
|
|
16
|
|
17 import core.stdc.ctype;
|
|
18 import core.stdc.stdio : sprintf;
|
|
19
|
|
20 string mangle(Declaration sthis)
|
|
21 {
|
|
22 scope OutBuffer buf = new OutBuffer();
|
|
23
|
|
24 string id;
|
|
25 Dsymbol s = sthis;
|
|
26
|
|
27 //printf(".mangle(%s)\n", sthis.toChars());
|
|
28 do
|
|
29 {
|
|
30 //printf("mangle: s = %p, '%s', parent = %p\n", s, s.toChars(), s.parent);
|
|
31 if (s.ident)
|
|
32 {
|
|
33 FuncDeclaration fd = s.isFuncDeclaration();
|
|
34 if (s !is sthis && fd)
|
|
35 {
|
|
36 id = mangle(fd);
|
|
37 buf.prependstring(id);
|
|
38 goto L1;
|
|
39 }
|
|
40 else
|
|
41 {
|
|
42 id = s.ident.toChars();
|
|
43 int len = id.length;
|
|
44 char tmp[len.sizeof * 3 + 1];
|
|
45 buf.prependstring(id);
|
|
46 len = sprintf(tmp.ptr, "%d".ptr, len);
|
|
47 buf.prependstring(tmp[0..len]);
|
|
48 }
|
|
49 }
|
|
50 else
|
|
51 buf.prependstring("0");
|
|
52 s = s.parent;
|
|
53 } while (s);
|
|
54
|
|
55 // buf.prependstring("_D");
|
|
56 L1:
|
|
57 //printf("deco = '%s'\n", sthis.type.deco ? sthis.type.deco : "null");
|
|
58 //printf("sthis.type = %s\n", sthis.type.toChars());
|
|
59 FuncDeclaration fd = sthis.isFuncDeclaration();
|
|
60 if (fd && (fd.needThis() || fd.isNested()))
|
|
61 buf.writeByte(Type.needThisPrefix());
|
|
62 if (sthis.type.deco)
|
|
63 buf.writestring(sthis.type.deco);
|
|
64 else
|
|
65 {
|
|
66 debug {
|
|
67 if (!fd.inferRetType)
|
|
68 writef("%s\n", fd.toChars());
|
|
69 }
|
|
70 assert(fd.inferRetType);
|
|
71 }
|
|
72
|
|
73 id = buf.extractString();
|
|
74 return id;
|
|
75 }
|
|
76
|
|
77 class Declaration : Dsymbol
|
|
78 {
|
|
79 Type type;
|
|
80 Type originalType; // before semantic analysis
|
|
81 STC storage_class = STC.STCundefined;
|
|
82 PROT protection = PROT.PROTundefined;
|
|
83 LINK linkage = LINK.LINKdefault;
|
|
84 int inuse; // used to detect cycles
|
|
85
|
|
86 this(Identifier id)
|
|
87 {
|
|
88 super(id);
|
|
89 }
|
|
90
|
|
91 void semantic(Scope sc)
|
|
92 {
|
|
93 assert(false);
|
|
94 }
|
|
95
|
|
96 string kind()
|
|
97 {
|
|
98 assert(false);
|
|
99 }
|
|
100
|
|
101 uint size(Loc loc)
|
|
102 {
|
|
103 assert(false);
|
|
104 }
|
|
105
|
|
106 /*************************************
|
|
107 * Check to see if declaration can be modified in this context (sc).
|
|
108 * Issue error if not.
|
|
109 */
|
|
110 void checkModify(Loc loc, Scope sc, Type t)
|
|
111 {
|
|
112 if (sc.incontract && isParameter())
|
|
113 error(loc, "cannot modify parameter '%s' in contract", toChars());
|
|
114
|
|
115 if (isCtorinit())
|
|
116 {
|
|
117 // It's only modifiable if inside the right constructor
|
|
118 Dsymbol s = sc.func;
|
|
119 while (true)
|
|
120 {
|
|
121 FuncDeclaration fd = null;
|
|
122 if (s)
|
|
123 fd = s.isFuncDeclaration();
|
|
124 if (fd && ((fd.isCtorDeclaration() && storage_class & STC.STCfield) ||
|
|
125 (fd.isStaticCtorDeclaration() && !(storage_class & STC.STCfield))) &&
|
|
126 fd.toParent() == toParent()
|
|
127 )
|
|
128 {
|
|
129 VarDeclaration v = isVarDeclaration();
|
|
130 assert(v);
|
|
131 v.ctorinit = 1;
|
|
132 //printf("setting ctorinit\n");
|
|
133 }
|
|
134 else
|
|
135 {
|
|
136 if (s)
|
|
137 {
|
|
138 s = s.toParent2();
|
|
139 continue;
|
|
140 }
|
|
141 else
|
|
142 {
|
|
143 string p = isStatic() ? "static " : "";
|
|
144 error(loc, "can only initialize %sconst %s inside %sconstructor", p, toChars(), p);
|
|
145 }
|
|
146 }
|
|
147 break;
|
|
148 }
|
|
149 }
|
|
150 else
|
|
151 {
|
|
152 VarDeclaration v = isVarDeclaration();
|
|
153 if (v && v.canassign == 0)
|
|
154 {
|
|
155 string p = null;
|
|
156 if (isConst())
|
|
157 p = "const";
|
|
158 else if (isInvariant())
|
|
159 p = "immutable";
|
|
160 else if (storage_class & STC.STCmanifest)
|
|
161 p = "enum";
|
|
162 else if (!t.isAssignable())
|
|
163 p = "struct with immutable members";
|
|
164 if (p)
|
|
165 {
|
|
166 error(loc, "cannot modify %s", p);
|
|
167 }
|
|
168 }
|
|
169 }
|
|
170 }
|
|
171
|
|
172 void emitComment(Scope sc)
|
|
173 {
|
|
174 assert(false);
|
|
175 }
|
|
176
|
|
177 void toDocBuffer(OutBuffer buf)
|
|
178 {
|
|
179 assert(false);
|
|
180 }
|
|
181
|
|
182 string mangle()
|
|
183 out (result)
|
|
184 {
|
|
185 try
|
|
186 {
|
|
187 int len = result.length;
|
|
188
|
|
189 assert(len > 0);
|
|
190 //printf("mangle: '%s' => '%s'\n", toChars(), result);
|
|
191 for (int i = 0; i < len; i++)
|
|
192 {
|
|
193 assert(result[i] == '_' || result[i] == '@' || isalnum(result[i]) || result[i] & 0x80);
|
|
194 }
|
|
195 } catch {
|
|
196 writef("Incorrect mangle: '%s'\n", result);
|
|
197 assert(false);
|
|
198 }
|
|
199 }
|
|
200 body
|
|
201 {
|
|
202 //writef("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d)\n", this, toChars(), parent ? parent.toChars() : "null", linkage);
|
|
203 if (!parent || parent.isModule() || linkage == LINK.LINKcpp) // if at global scope
|
|
204 {
|
|
205 // If it's not a D declaration, no mangling
|
|
206 switch (linkage)
|
|
207 {
|
|
208 case LINK.LINKd:
|
|
209 break;
|
|
210
|
|
211 case LINK.LINKc:
|
|
212 case LINK.LINKwindows:
|
|
213 case LINK.LINKpascal:
|
|
214 return ident.toChars();
|
|
215
|
|
216 case LINK.LINKcpp:
|
|
217 version (CPP_MANGLE) {
|
|
218 return cpp_mangle(this);
|
|
219 } else {
|
|
220 // Windows C++ mangling is done by C++ back end
|
|
221 return ident.toChars();
|
|
222 }
|
|
223
|
|
224 case LINK.LINKdefault:
|
|
225 assert(false);
|
|
226 error("forward declaration");
|
|
227 return ident.toChars();
|
|
228
|
|
229 default:
|
|
230 writef("'%s', linkage = %d\n", toChars(), linkage);
|
|
231 assert(0);
|
|
232 }
|
|
233 }
|
|
234
|
|
235 string p = .mangle(this);
|
|
236 scope OutBuffer buf = new OutBuffer();
|
|
237 buf.writestring("_D");
|
|
238 buf.writestring(p);
|
|
239 p = buf.toChars();
|
|
240 buf.data = null;
|
|
241 //writef("Declaration.mangle(this = %p, '%s', parent = '%s', linkage = %d) = %s\n", this, toChars(), parent ? parent.toChars() : "null", linkage, p);
|
|
242 return p;
|
|
243 }
|
|
244
|
|
245 int isStatic() { return storage_class & STC.STCstatic; }
|
|
246
|
|
247 bool isStaticConstructor()
|
|
248 {
|
|
249 return false;
|
|
250 }
|
|
251
|
|
252 bool isStaticDestructor()
|
|
253 {
|
|
254 return false;
|
|
255 }
|
|
256
|
|
257 bool isDelete()
|
|
258 {
|
|
259 return false;
|
|
260 }
|
|
261
|
|
262 bool isDataseg()
|
|
263 {
|
|
264 return false;
|
|
265 }
|
|
266
|
|
267 bool isThreadlocal()
|
|
268 {
|
|
269 assert(false);
|
|
270 }
|
|
271
|
|
272 bool isCodeseg()
|
|
273 {
|
|
274 return false;
|
|
275 }
|
|
276
|
|
277 int isCtorinit() { return storage_class & STC.STCctorinit; }
|
|
278
|
|
279 int isFinal() { return storage_class & STC.STCfinal; }
|
|
280
|
|
281 bool isAbstract() { return (storage_class & STC.STCabstract) != 0; }
|
|
282
|
|
283 bool isConst() { return (storage_class & STC.STCconst) != 0; }
|
|
284
|
|
285 int isInvariant() { return storage_class & STC.STCinvariant; }
|
|
286
|
|
287 int isAuto() { return storage_class & STC.STCauto; }
|
|
288
|
|
289 int isScope() { return storage_class & (STC.STCscope | STC.STCauto); }
|
|
290
|
|
291 int isSynchronized() { return storage_class & STC.STCsynchronized; }
|
|
292
|
|
293 int isParameter() { return storage_class & STC.STCparameter; }
|
|
294
|
|
295 bool isDeprecated() { return (storage_class & STC.STCdeprecated) != 0; }
|
|
296
|
|
297 int isOverride() { return storage_class & STC.STCoverride; }
|
|
298
|
|
299 int isIn() { return storage_class & STC.STCin; }
|
|
300
|
|
301 int isOut() { return storage_class & STC.STCout; }
|
|
302
|
|
303 int isRef() { return storage_class & STC.STCref; }
|
|
304
|
|
305 PROT prot()
|
|
306 {
|
|
307 return protection;
|
|
308 }
|
|
309
|
|
310 Declaration isDeclaration() { return this; }
|
|
311 } |