1
|
1
|
|
2 // Compiler implementation of the D programming language
|
|
3 // Copyright (c) 1999-2007 by Digital Mars
|
|
4 // All Rights Reserved
|
|
5 // written by Walter Bright
|
|
6 // http://www.digitalmars.com
|
|
7 // License for redistribution is by either the Artistic License
|
|
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
9 // See the included readme.txt for details.
|
|
10
|
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <assert.h>
|
|
14
|
|
15 #if _WIN32 || IN_GCC || IN_LLVM
|
|
16 #include "mem.h"
|
|
17 #elif linux
|
|
18 #include "../root/mem.h"
|
|
19 #endif
|
|
20
|
|
21 #include "init.h"
|
|
22 #include "declaration.h"
|
|
23 #include "attrib.h"
|
|
24 #include "cond.h"
|
|
25 #include "scope.h"
|
|
26 #include "id.h"
|
|
27 #include "expression.h"
|
|
28 #include "dsymbol.h"
|
|
29 #include "aggregate.h"
|
|
30 #include "module.h"
|
|
31 #include "parse.h"
|
|
32
|
|
33 #include "../gen/enums.h"
|
|
34
|
|
35 extern void obj_includelib(char *name);
|
|
36
|
|
37
|
|
38 /********************************* AttribDeclaration ****************************/
|
|
39
|
|
40 AttribDeclaration::AttribDeclaration(Array *decl)
|
|
41 : Dsymbol()
|
|
42 {
|
|
43 this->decl = decl;
|
|
44 }
|
|
45
|
|
46 Array *AttribDeclaration::include(Scope *sc, ScopeDsymbol *sd)
|
|
47 {
|
|
48 return decl;
|
|
49 }
|
|
50
|
|
51 int AttribDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
|
|
52 {
|
|
53 unsigned i;
|
|
54 int m = 0;
|
|
55 Array *d = include(sc, sd);
|
|
56
|
|
57 if (d)
|
|
58 {
|
|
59 for (i = 0; i < d->dim; i++)
|
|
60 { Dsymbol *s;
|
|
61
|
|
62 s = (Dsymbol *)d->data[i];
|
|
63 m |= s->addMember(sc, sd, m | memnum);
|
|
64 }
|
|
65 }
|
|
66 return m;
|
|
67 }
|
|
68
|
|
69 void AttribDeclaration::semantic(Scope *sc)
|
|
70 {
|
|
71 Array *d = include(sc, NULL);
|
|
72
|
|
73 //printf("\tAttribDeclaration::semantic '%s'\n",toChars());
|
|
74 if (d)
|
|
75 {
|
|
76 for (unsigned i = 0; i < d->dim; i++)
|
|
77 {
|
|
78 Dsymbol *s = (Dsymbol *)d->data[i];
|
|
79
|
|
80 s->semantic(sc);
|
|
81 }
|
|
82 }
|
|
83 }
|
|
84
|
|
85 void AttribDeclaration::semantic2(Scope *sc)
|
|
86 {
|
|
87 unsigned i;
|
|
88 Array *d = include(sc, NULL);
|
|
89
|
|
90 if (d)
|
|
91 {
|
|
92 for (i = 0; i < d->dim; i++)
|
|
93 { Dsymbol *s;
|
|
94
|
|
95 s = (Dsymbol *)d->data[i];
|
|
96 s->semantic2(sc);
|
|
97 }
|
|
98 }
|
|
99 }
|
|
100
|
|
101 void AttribDeclaration::semantic3(Scope *sc)
|
|
102 {
|
|
103 unsigned i;
|
|
104 Array *d = include(sc, NULL);
|
|
105
|
|
106 if (d)
|
|
107 {
|
|
108 for (i = 0; i < d->dim; i++)
|
|
109 { Dsymbol *s;
|
|
110
|
|
111 s = (Dsymbol *)d->data[i];
|
|
112 s->semantic3(sc);
|
|
113 }
|
|
114 }
|
|
115 }
|
|
116
|
|
117 void AttribDeclaration::inlineScan()
|
|
118 {
|
|
119 unsigned i;
|
|
120 Array *d = include(NULL, NULL);
|
|
121
|
|
122 if (d)
|
|
123 {
|
|
124 for (i = 0; i < d->dim; i++)
|
|
125 { Dsymbol *s;
|
|
126
|
|
127 s = (Dsymbol *)d->data[i];
|
|
128 //printf("AttribDeclaration::inlineScan %s\n", s->toChars());
|
|
129 s->inlineScan();
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133
|
|
134 void AttribDeclaration::addComment(unsigned char *comment)
|
|
135 {
|
|
136 if (comment)
|
|
137 {
|
|
138 unsigned i;
|
|
139 Array *d = include(NULL, NULL);
|
|
140
|
|
141 if (d)
|
|
142 {
|
|
143 for (i = 0; i < d->dim; i++)
|
|
144 { Dsymbol *s;
|
|
145
|
|
146 s = (Dsymbol *)d->data[i];
|
|
147 //printf("AttribDeclaration::addComment %s\n", s->toChars());
|
|
148 s->addComment(comment);
|
|
149 }
|
|
150 }
|
|
151 }
|
|
152 }
|
|
153
|
|
154 void AttribDeclaration::emitComment(Scope *sc)
|
|
155 {
|
|
156 //printf("AttribDeclaration::emitComment(sc = %p)\n", sc);
|
|
157
|
|
158 /* If generating doc comment, skip this because if we're inside
|
|
159 * a template, then include(NULL, NULL) will fail.
|
|
160 */
|
|
161 // if (sc->docbuf)
|
|
162 // return;
|
|
163
|
|
164 unsigned i;
|
|
165 Array *d = include(NULL, NULL);
|
|
166
|
|
167 if (d)
|
|
168 {
|
|
169 for (i = 0; i < d->dim; i++)
|
|
170 { Dsymbol *s;
|
|
171
|
|
172 s = (Dsymbol *)d->data[i];
|
|
173 //printf("AttribDeclaration::emitComment %s\n", s->toChars());
|
|
174 s->emitComment(sc);
|
|
175 }
|
|
176 }
|
|
177 }
|
|
178
|
|
179 void AttribDeclaration::toObjFile()
|
|
180 {
|
|
181 unsigned i;
|
|
182 Array *d = include(NULL, NULL);
|
|
183
|
|
184 if (d)
|
|
185 {
|
|
186 for (i = 0; i < d->dim; i++)
|
|
187 { Dsymbol *s;
|
|
188
|
|
189 s = (Dsymbol *)d->data[i];
|
|
190 s->toObjFile();
|
|
191 }
|
|
192 }
|
|
193 }
|
|
194
|
19
|
195 int AttribDeclaration::cvMember(unsigned char *p)
|
|
196 {
|
|
197 unsigned i;
|
|
198 int nwritten = 0;
|
|
199 int n;
|
|
200 Array *d = include(NULL, NULL);
|
|
201
|
|
202 if (d)
|
|
203 {
|
|
204 for (i = 0; i < d->dim; i++)
|
|
205 { Dsymbol *s;
|
|
206
|
|
207 s = (Dsymbol *)d->data[i];
|
|
208 n = s->cvMember(p);
|
|
209 if (p)
|
|
210 p += n;
|
|
211 nwritten += n;
|
|
212 }
|
|
213 }
|
|
214 return nwritten;
|
|
215 }
|
|
216
|
1
|
217 int AttribDeclaration::hasPointers()
|
|
218 {
|
|
219 Array *d = include(NULL, NULL);
|
|
220
|
|
221 if (d)
|
|
222 {
|
|
223 for (size_t i = 0; i < d->dim; i++)
|
|
224 {
|
|
225 Dsymbol *s = (Dsymbol *)d->data[i];
|
|
226 if (s->hasPointers())
|
|
227 return 1;
|
|
228 }
|
|
229 }
|
|
230 return 0;
|
|
231 }
|
|
232
|
|
233 char *AttribDeclaration::kind()
|
|
234 {
|
|
235 return "attribute";
|
|
236 }
|
|
237
|
|
238 int AttribDeclaration::oneMember(Dsymbol **ps)
|
|
239 {
|
|
240 Array *d = include(NULL, NULL);
|
|
241
|
|
242 return Dsymbol::oneMembers(d, ps);
|
|
243 }
|
|
244
|
|
245 void AttribDeclaration::checkCtorConstInit()
|
|
246 {
|
|
247 unsigned i;
|
|
248 Array *d = include(NULL, NULL);
|
|
249
|
|
250 if (d)
|
|
251 {
|
|
252 for (i = 0; i < d->dim; i++)
|
|
253 { Dsymbol *s;
|
|
254
|
|
255 s = (Dsymbol *)d->data[i];
|
|
256 s->checkCtorConstInit();
|
|
257 }
|
|
258 }
|
|
259 }
|
|
260
|
|
261 /****************************************
|
|
262 */
|
|
263
|
|
264 void AttribDeclaration::addLocalClass(ClassDeclarations *aclasses)
|
|
265 { unsigned i;
|
|
266 Array *d = include(NULL, NULL);
|
|
267
|
|
268 if (d)
|
|
269 {
|
|
270 for (i = 0; i < d->dim; i++)
|
|
271 { Dsymbol *s;
|
|
272
|
|
273 s = (Dsymbol *)d->data[i];
|
|
274 s->addLocalClass(aclasses);
|
|
275 }
|
|
276 }
|
|
277 }
|
|
278
|
|
279
|
|
280 void AttribDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
281 {
|
|
282 if (decl)
|
|
283 {
|
|
284 buf->writenl();
|
|
285 buf->writeByte('{');
|
|
286 buf->writenl();
|
|
287 for (unsigned i = 0; i < decl->dim; i++)
|
|
288 {
|
|
289 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
290
|
|
291 buf->writestring(" ");
|
|
292 s->toCBuffer(buf, hgs);
|
|
293 }
|
|
294 buf->writeByte('}');
|
|
295 }
|
|
296 else
|
|
297 buf->writeByte(';');
|
|
298 buf->writenl();
|
|
299 }
|
|
300
|
|
301 /************************* StorageClassDeclaration ****************************/
|
|
302
|
|
303 StorageClassDeclaration::StorageClassDeclaration(unsigned stc, Array *decl)
|
|
304 : AttribDeclaration(decl)
|
|
305 {
|
|
306 this->stc = stc;
|
|
307 }
|
|
308
|
|
309 Dsymbol *StorageClassDeclaration::syntaxCopy(Dsymbol *s)
|
|
310 {
|
|
311 StorageClassDeclaration *scd;
|
|
312
|
|
313 assert(!s);
|
|
314 scd = new StorageClassDeclaration(stc, Dsymbol::arraySyntaxCopy(decl));
|
|
315 return scd;
|
|
316 }
|
|
317
|
|
318 void StorageClassDeclaration::semantic(Scope *sc)
|
|
319 {
|
|
320 if (decl)
|
|
321 { unsigned stc_save = sc->stc;
|
|
322
|
|
323 if (stc & (STCauto | STCscope | STCstatic | STCextern))
|
|
324 sc->stc &= ~(STCauto | STCscope | STCstatic | STCextern);
|
|
325 sc->stc |= stc;
|
|
326 for (unsigned i = 0; i < decl->dim; i++)
|
|
327 {
|
|
328 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
329
|
|
330 s->semantic(sc);
|
|
331 }
|
|
332 sc->stc = stc_save;
|
|
333 }
|
|
334 else
|
|
335 sc->stc = stc;
|
|
336 }
|
|
337
|
|
338 void StorageClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
339 {
|
|
340 struct SCstring
|
|
341 {
|
|
342 int stc;
|
|
343 enum TOK tok;
|
|
344 };
|
|
345
|
|
346 static SCstring table[] =
|
|
347 {
|
|
348 { STCauto, TOKauto },
|
|
349 { STCscope, TOKscope },
|
|
350 { STCstatic, TOKstatic },
|
|
351 { STCextern, TOKextern },
|
|
352 { STCconst, TOKconst },
|
|
353 { STCfinal, TOKfinal },
|
|
354 { STCabstract, TOKabstract },
|
|
355 { STCsynchronized, TOKsynchronized },
|
|
356 { STCdeprecated, TOKdeprecated },
|
|
357 { STCoverride, TOKoverride },
|
|
358 };
|
|
359
|
|
360 int written = 0;
|
|
361 for (int i = 0; i < sizeof(table)/sizeof(table[0]); i++)
|
|
362 {
|
|
363 if (stc & table[i].stc)
|
|
364 {
|
|
365 if (written)
|
|
366 buf->writeByte(' ');
|
|
367 written = 1;
|
|
368 buf->writestring(Token::toChars(table[i].tok));
|
|
369 }
|
|
370 }
|
|
371
|
|
372 AttribDeclaration::toCBuffer(buf, hgs);
|
|
373 }
|
|
374
|
|
375 /********************************* LinkDeclaration ****************************/
|
|
376
|
|
377 LinkDeclaration::LinkDeclaration(enum LINK p, Array *decl)
|
|
378 : AttribDeclaration(decl)
|
|
379 {
|
|
380 //printf("LinkDeclaration(linkage = %d, decl = %p)\n", p, decl);
|
|
381 linkage = p;
|
|
382 }
|
|
383
|
|
384 Dsymbol *LinkDeclaration::syntaxCopy(Dsymbol *s)
|
|
385 {
|
|
386 LinkDeclaration *ld;
|
|
387
|
|
388 assert(!s);
|
|
389 ld = new LinkDeclaration(linkage, Dsymbol::arraySyntaxCopy(decl));
|
|
390 return ld;
|
|
391 }
|
|
392
|
|
393 void LinkDeclaration::semantic(Scope *sc)
|
|
394 {
|
|
395 //printf("LinkDeclaration::semantic(linkage = %d, decl = %p)\n", linkage, decl);
|
|
396 if (decl)
|
|
397 { enum LINK linkage_save = sc->linkage;
|
|
398
|
|
399 sc->linkage = linkage;
|
|
400 for (unsigned i = 0; i < decl->dim; i++)
|
|
401 {
|
|
402 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
403
|
|
404 s->semantic(sc);
|
|
405 }
|
|
406 sc->linkage = linkage_save;
|
|
407 }
|
|
408 else
|
|
409 {
|
|
410 sc->linkage = linkage;
|
|
411 }
|
|
412 }
|
|
413
|
|
414 void LinkDeclaration::semantic3(Scope *sc)
|
|
415 {
|
|
416 //printf("LinkDeclaration::semantic3(linkage = %d, decl = %p)\n", linkage, decl);
|
|
417 if (decl)
|
|
418 { enum LINK linkage_save = sc->linkage;
|
|
419
|
|
420 sc->linkage = linkage;
|
|
421 for (unsigned i = 0; i < decl->dim; i++)
|
|
422 {
|
|
423 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
424
|
|
425 s->semantic3(sc);
|
|
426 }
|
|
427 sc->linkage = linkage_save;
|
|
428 }
|
|
429 else
|
|
430 {
|
|
431 sc->linkage = linkage;
|
|
432 }
|
|
433 }
|
|
434
|
|
435 void LinkDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
436 { char *p;
|
|
437
|
|
438 switch (linkage)
|
|
439 {
|
|
440 case LINKd: p = "D"; break;
|
|
441 case LINKc: p = "C"; break;
|
|
442 case LINKcpp: p = "C++"; break;
|
|
443 case LINKwindows: p = "Windows"; break;
|
|
444 case LINKpascal: p = "Pascal"; break;
|
|
445 default:
|
|
446 assert(0);
|
|
447 break;
|
|
448 }
|
|
449 buf->writestring("extern (");
|
|
450 buf->writestring(p);
|
|
451 buf->writestring(") ");
|
|
452 AttribDeclaration::toCBuffer(buf, hgs);
|
|
453 }
|
|
454
|
|
455 char *LinkDeclaration::toChars()
|
|
456 {
|
|
457 return "extern ()";
|
|
458 }
|
|
459
|
|
460 /********************************* ProtDeclaration ****************************/
|
|
461
|
|
462 ProtDeclaration::ProtDeclaration(enum PROT p, Array *decl)
|
|
463 : AttribDeclaration(decl)
|
|
464 {
|
|
465 protection = p;
|
|
466 //printf("decl = %p\n", decl);
|
|
467 }
|
|
468
|
|
469 Dsymbol *ProtDeclaration::syntaxCopy(Dsymbol *s)
|
|
470 {
|
|
471 ProtDeclaration *pd;
|
|
472
|
|
473 assert(!s);
|
|
474 pd = new ProtDeclaration(protection, Dsymbol::arraySyntaxCopy(decl));
|
|
475 return pd;
|
|
476 }
|
|
477
|
|
478 void ProtDeclaration::semantic(Scope *sc)
|
|
479 {
|
|
480 if (decl)
|
|
481 { enum PROT protection_save = sc->protection;
|
|
482 int explicitProtection_save = sc->explicitProtection;
|
|
483
|
|
484 sc->protection = protection;
|
|
485 sc->explicitProtection = 1;
|
|
486 for (unsigned i = 0; i < decl->dim; i++)
|
|
487 {
|
|
488 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
489
|
|
490 s->semantic(sc);
|
|
491 }
|
|
492 sc->protection = protection_save;
|
|
493 sc->explicitProtection = explicitProtection_save;
|
|
494 }
|
|
495 else
|
|
496 { sc->protection = protection;
|
|
497 sc->explicitProtection = 1;
|
|
498 }
|
|
499 }
|
|
500
|
|
501 void ProtDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
502 { char *p;
|
|
503
|
|
504 switch (protection)
|
|
505 {
|
|
506 case PROTprivate: p = "private"; break;
|
|
507 case PROTpackage: p = "package"; break;
|
|
508 case PROTprotected: p = "protected"; break;
|
|
509 case PROTpublic: p = "public"; break;
|
|
510 case PROTexport: p = "export"; break;
|
|
511 default:
|
|
512 assert(0);
|
|
513 break;
|
|
514 }
|
|
515 buf->writestring(p);
|
|
516 AttribDeclaration::toCBuffer(buf, hgs);
|
|
517 }
|
|
518
|
|
519 /********************************* AlignDeclaration ****************************/
|
|
520
|
|
521 AlignDeclaration::AlignDeclaration(unsigned sa, Array *decl)
|
|
522 : AttribDeclaration(decl)
|
|
523 {
|
|
524 salign = sa;
|
|
525 }
|
|
526
|
|
527 Dsymbol *AlignDeclaration::syntaxCopy(Dsymbol *s)
|
|
528 {
|
|
529 AlignDeclaration *ad;
|
|
530
|
|
531 assert(!s);
|
|
532 ad = new AlignDeclaration(salign, Dsymbol::arraySyntaxCopy(decl));
|
|
533 return ad;
|
|
534 }
|
|
535
|
|
536 void AlignDeclaration::semantic(Scope *sc)
|
|
537 {
|
|
538 //printf("\tAlignDeclaration::semantic '%s'\n",toChars());
|
|
539 if (decl)
|
|
540 { unsigned salign_save = sc->structalign;
|
|
541
|
|
542 sc->structalign = salign;
|
|
543 for (unsigned i = 0; i < decl->dim; i++)
|
|
544 {
|
|
545 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
546
|
|
547 s->semantic(sc);
|
|
548 }
|
|
549 sc->structalign = salign_save;
|
|
550 }
|
|
551 else
|
|
552 sc->structalign = salign;
|
|
553 }
|
|
554
|
|
555
|
|
556 void AlignDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
557 {
|
|
558 buf->printf("align (%d)", salign);
|
|
559 AttribDeclaration::toCBuffer(buf, hgs);
|
|
560 }
|
|
561
|
|
562 /********************************* AnonDeclaration ****************************/
|
|
563
|
|
564 AnonDeclaration::AnonDeclaration(Loc loc, int isunion, Array *decl)
|
|
565 : AttribDeclaration(decl)
|
|
566 {
|
|
567 this->loc = loc;
|
|
568 this->isunion = isunion;
|
|
569 this->scope = NULL;
|
|
570 this->sem = 0;
|
|
571 }
|
|
572
|
|
573 Dsymbol *AnonDeclaration::syntaxCopy(Dsymbol *s)
|
|
574 {
|
|
575 AnonDeclaration *ad;
|
|
576
|
|
577 assert(!s);
|
|
578 ad = new AnonDeclaration(loc, isunion, Dsymbol::arraySyntaxCopy(decl));
|
|
579 return ad;
|
|
580 }
|
|
581
|
|
582 void AnonDeclaration::semantic(Scope *sc)
|
|
583 {
|
|
584 //printf("\tAnonDeclaration::semantic %s %p\n", isunion ? "union" : "struct", this);
|
|
585
|
|
586 Scope *scx = NULL;
|
|
587 if (scope)
|
|
588 { sc = scope;
|
|
589 scx = scope;
|
|
590 scope = NULL;
|
|
591 }
|
|
592
|
|
593 assert(sc->parent);
|
|
594
|
|
595 Dsymbol *parent = sc->parent->pastMixin();
|
|
596 AggregateDeclaration *ad = parent->isAggregateDeclaration();
|
|
597
|
|
598 if (!ad || (!ad->isStructDeclaration() && !ad->isClassDeclaration()))
|
|
599 {
|
|
600 error("can only be a part of an aggregate");
|
|
601 return;
|
|
602 }
|
|
603
|
|
604 if (decl)
|
|
605 {
|
|
606 AnonymousAggregateDeclaration aad;
|
|
607 int adisunion;
|
|
608
|
|
609 if (sc->anonAgg)
|
|
610 { ad = sc->anonAgg;
|
|
611 adisunion = sc->inunion;
|
|
612 }
|
|
613 else
|
|
614 adisunion = ad->isUnionDeclaration() != NULL;
|
|
615
|
|
616 // printf("\tsc->anonAgg = %p\n", sc->anonAgg);
|
|
617 // printf("\tad = %p\n", ad);
|
|
618 // printf("\taad = %p\n", &aad);
|
|
619
|
|
620 sc = sc->push();
|
|
621 sc->anonAgg = &aad;
|
|
622 sc->stc &= ~(STCauto | STCscope | STCstatic);
|
|
623 sc->inunion = isunion;
|
|
624 sc->offset = 0;
|
|
625 sc->flags = 0;
|
|
626 aad.structalign = sc->structalign;
|
|
627 aad.parent = ad;
|
|
628
|
|
629 for (unsigned i = 0; i < decl->dim; i++)
|
|
630 {
|
|
631 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
632
|
|
633 s->semantic(sc);
|
|
634 if (isunion)
|
|
635 sc->offset = 0;
|
|
636 if (aad.sizeok == 2)
|
|
637 {
|
|
638 break;
|
|
639 }
|
|
640 }
|
|
641 sc = sc->pop();
|
|
642
|
|
643 // If failed due to forward references, unwind and try again later
|
|
644 if (aad.sizeok == 2)
|
|
645 {
|
|
646 ad->sizeok = 2;
|
|
647 //printf("\tsetting ad->sizeok %p to 2\n", ad);
|
|
648 if (!sc->anonAgg)
|
|
649 {
|
|
650 scope = scx ? scx : new Scope(*sc);
|
|
651 scope->setNoFree();
|
|
652 scope->module->addDeferredSemantic(this);
|
|
653 }
|
|
654 //printf("\tforward reference %p\n", this);
|
|
655 return;
|
|
656 }
|
|
657 if (sem == 0)
|
|
658 { Module::dprogress++;
|
|
659 sem = 1;
|
|
660 //printf("\tcompleted %p\n", this);
|
|
661 }
|
|
662 else
|
|
663 ;//printf("\talready completed %p\n", this);
|
|
664
|
|
665 // 0 sized structs are set to 1 byte
|
|
666 if (aad.structsize == 0)
|
|
667 {
|
|
668 aad.structsize = 1;
|
|
669 aad.alignsize = 1;
|
|
670 }
|
|
671
|
|
672 // Align size of anonymous aggregate
|
|
673 //printf("aad.structalign = %d, aad.alignsize = %d, sc->offset = %d\n", aad.structalign, aad.alignsize, sc->offset);
|
|
674 ad->alignmember(aad.structalign, aad.alignsize, &sc->offset);
|
|
675 //ad->structsize = sc->offset;
|
|
676 //printf("sc->offset = %d\n", sc->offset);
|
|
677
|
|
678 // Add members of aad to ad
|
|
679 //printf("\tadding members of aad to '%s'\n", ad->toChars());
|
|
680 for (unsigned i = 0; i < aad.fields.dim; i++)
|
|
681 {
|
|
682 VarDeclaration *v = (VarDeclaration *)aad.fields.data[i];
|
|
683
|
|
684 v->offset += sc->offset;
|
|
685 ad->fields.push(v);
|
|
686 }
|
|
687
|
|
688 // Add size of aad to ad
|
|
689 if (adisunion)
|
|
690 {
|
|
691 if (aad.structsize > ad->structsize)
|
|
692 ad->structsize = aad.structsize;
|
|
693 sc->offset = 0;
|
|
694 }
|
|
695 else
|
|
696 {
|
|
697 ad->structsize = sc->offset + aad.structsize;
|
|
698 sc->offset = ad->structsize;
|
|
699 }
|
|
700
|
|
701 if (ad->alignsize < aad.alignsize)
|
|
702 ad->alignsize = aad.alignsize;
|
|
703 }
|
|
704 }
|
|
705
|
|
706
|
|
707 void AnonDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
708 {
|
|
709 buf->printf(isunion ? "union" : "struct");
|
|
710 buf->writestring("\n{\n");
|
|
711 if (decl)
|
|
712 {
|
|
713 for (unsigned i = 0; i < decl->dim; i++)
|
|
714 {
|
|
715 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
716
|
|
717 //buf->writestring(" ");
|
|
718 s->toCBuffer(buf, hgs);
|
|
719 }
|
|
720 }
|
|
721 buf->writestring("}\n");
|
|
722 }
|
|
723
|
|
724 char *AnonDeclaration::kind()
|
|
725 {
|
|
726 return (char *)(isunion ? "anonymous union" : "anonymous struct");
|
|
727 }
|
|
728
|
|
729 /********************************* PragmaDeclaration ****************************/
|
|
730
|
|
731 PragmaDeclaration::PragmaDeclaration(Loc loc, Identifier *ident, Expressions *args, Array *decl)
|
|
732 : AttribDeclaration(decl)
|
|
733 {
|
|
734 this->loc = loc;
|
|
735 this->ident = ident;
|
|
736 this->args = args;
|
|
737 }
|
|
738
|
|
739 Dsymbol *PragmaDeclaration::syntaxCopy(Dsymbol *s)
|
|
740 {
|
|
741 PragmaDeclaration *pd;
|
|
742
|
|
743 assert(!s);
|
|
744 pd = new PragmaDeclaration(loc, ident,
|
|
745 Expression::arraySyntaxCopy(args), Dsymbol::arraySyntaxCopy(decl));
|
|
746 return pd;
|
|
747 }
|
|
748
|
|
749 void PragmaDeclaration::semantic(Scope *sc)
|
|
750 { // Should be merged with PragmaStatement
|
|
751
|
|
752 #if IN_LLVM
|
|
753 int llvm_internal = 0;
|
|
754 char* llvm_str1 = NULL;
|
|
755
|
|
756 #endif
|
|
757
|
|
758 //printf("\tPragmaDeclaration::semantic '%s'\n",toChars());
|
|
759 if (ident == Id::msg)
|
|
760 {
|
|
761 if (args)
|
|
762 {
|
|
763 for (size_t i = 0; i < args->dim; i++)
|
|
764 {
|
|
765 Expression *e = (Expression *)args->data[i];
|
|
766
|
|
767 e = e->semantic(sc);
|
|
768 e = e->optimize(WANTvalue | WANTinterpret);
|
|
769 if (e->op == TOKstring)
|
|
770 {
|
|
771 StringExp *se = (StringExp *)e;
|
|
772 fprintf(stdmsg, "%.*s", (int)se->len, se->string);
|
|
773 }
|
|
774 else
|
|
775 error("string expected for message, not '%s'", e->toChars());
|
|
776 }
|
|
777 fprintf(stdmsg, "\n");
|
|
778 }
|
|
779 goto Lnodecl;
|
|
780 }
|
|
781 else if (ident == Id::lib)
|
|
782 {
|
|
783 if (!args || args->dim != 1)
|
|
784 error("string expected for library name");
|
|
785 else
|
|
786 {
|
|
787 Expression *e = (Expression *)args->data[0];
|
|
788
|
|
789 e = e->semantic(sc);
|
|
790 e = e->optimize(WANTvalue | WANTinterpret);
|
|
791 args->data[0] = (void *)e;
|
|
792 if (e->op != TOKstring)
|
|
793 error("string expected for library name, not '%s'", e->toChars());
|
|
794 else if (global.params.verbose)
|
|
795 {
|
|
796 StringExp *se = (StringExp *)e;
|
|
797 char *name = (char *)mem.malloc(se->len + 1);
|
|
798 memcpy(name, se->string, se->len);
|
|
799 name[se->len] = 0;
|
|
800 printf("library %s\n", name);
|
|
801 mem.free(name);
|
|
802 }
|
|
803 }
|
|
804 goto Lnodecl;
|
|
805 }
|
|
806 #if IN_GCC
|
|
807 else if (ident == Id::GNU_asm)
|
|
808 {
|
|
809 if (! args || args->dim != 2)
|
|
810 error("identifier and string expected for asm name");
|
|
811 else
|
|
812 {
|
|
813 Expression *e;
|
|
814 Declaration *d = NULL;
|
|
815 StringExp *s = NULL;
|
|
816
|
|
817 e = (Expression *)args->data[0];
|
|
818 e = e->semantic(sc);
|
|
819 if (e->op == TOKvar)
|
|
820 {
|
|
821 d = ((VarExp *)e)->var;
|
|
822 if (! d->isFuncDeclaration() && ! d->isVarDeclaration())
|
|
823 d = NULL;
|
|
824 }
|
|
825 if (!d)
|
|
826 error("first argument of GNU_asm must be a function or variable declaration");
|
|
827
|
|
828 e = (Expression *)args->data[1];
|
|
829 e = e->semantic(sc);
|
|
830 e = e->optimize(WANTvalue);
|
|
831 if (e->op == TOKstring && ((StringExp *)e)->sz == 1)
|
|
832 s = ((StringExp *)e);
|
|
833 else
|
|
834 error("second argument of GNU_asm must be a char string");
|
|
835
|
|
836 if (d && s)
|
|
837 d->c_ident = Lexer::idPool((char*) s->string);
|
|
838 }
|
|
839 goto Lnodecl;
|
|
840 }
|
|
841 #endif
|
|
842 #if IN_LLVM
|
|
843 else if (ident == Id::LLVM_internal)
|
|
844 {
|
|
845 if (!args || args->dim < 1 || args->dim > 2)
|
|
846 error("needs 1-3 parameters");
|
|
847 else if (!decl || decl->dim < 1)
|
|
848 error("must apply to at least one declaration");
|
|
849 else
|
|
850 {
|
|
851 Expression *e;
|
|
852 StringExp *s = NULL;
|
|
853
|
|
854 e = (Expression *)args->data[0];
|
|
855 e = e->semantic(sc);
|
|
856 e = e->optimize(WANTvalue);
|
|
857 if (e->op == TOKstring && (s = (StringExp *)e))
|
|
858 {
|
|
859 char* str = (char*)s->string;
|
|
860 if (strcmp(str,"intrinsic")==0) {
|
|
861 llvm_internal = LLVMintrinsic;
|
|
862 assert(args->dim == 2);
|
|
863 }
|
|
864 else if (strcmp(str,"null")==0) {
|
|
865 llvm_internal = LLVMnull;
|
|
866 assert(args->dim == 1);
|
|
867 }
|
|
868 else if (strcmp(str,"mangle")==0) {
|
|
869 llvm_internal = LLVMmangle;
|
|
870 assert(args->dim == 2);
|
|
871 }
|
|
872 else if (strcmp(str,"bind")==0) {
|
|
873 llvm_internal = LLVMbind;
|
|
874 assert(args->dim == 2);
|
|
875 }
|
|
876 else {
|
|
877 error("unknown pragma command: %s", str);
|
|
878 }
|
|
879 }
|
|
880 else
|
|
881 error("1st argument must be a string");
|
|
882
|
|
883 if (llvm_internal)
|
|
884 switch (llvm_internal)
|
|
885 {
|
|
886 case LLVMintrinsic:
|
|
887 case LLVMmangle:
|
|
888 case LLVMbind:
|
|
889 e = (Expression *)args->data[1];
|
|
890 e = e->semantic(sc);
|
|
891 e = e->optimize(WANTvalue);
|
|
892 if (e->op == TOKstring && (s = (StringExp *)e)) {
|
|
893 llvm_str1 = (char*)s->string;
|
|
894 }
|
|
895 else
|
|
896 error("2nd argument must be a string");
|
|
897 break;
|
|
898
|
|
899 case LLVMnull:
|
|
900 break;
|
|
901
|
|
902 default:
|
|
903 assert(0);
|
|
904 }
|
|
905 }
|
|
906 }
|
|
907 #endif
|
|
908 else
|
|
909 error("unrecognized pragma(%s)", ident->toChars());
|
|
910
|
|
911 if (decl)
|
|
912 {
|
|
913 for (unsigned i = 0; i < decl->dim; i++)
|
|
914 {
|
|
915 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
916
|
|
917 s->semantic(sc);
|
|
918
|
|
919 #if IN_LLVM
|
|
920 if (llvm_internal)
|
|
921 {
|
|
922 switch(llvm_internal)
|
|
923 {
|
|
924 case LLVMintrinsic:
|
|
925 case LLVMmangle:
|
|
926 if (FuncDeclaration* fd = s->isFuncDeclaration()) {
|
|
927 fd->llvmInternal = llvm_internal;
|
|
928 fd->llvmInternal1 = llvm_str1;
|
|
929 }
|
|
930 else {
|
|
931 error("may only be used on function declarations");
|
|
932 assert(0);
|
|
933 }
|
|
934 break;
|
|
935
|
|
936 case LLVMnull:
|
|
937 if (StaticCtorDeclaration* sd = s->isStaticCtorDeclaration()) {
|
|
938 sd->llvmInternal = llvm_internal;
|
|
939 }
|
|
940 else {
|
|
941 error("may only be used on static constructors");
|
|
942 assert(0);
|
|
943 }
|
|
944 break;
|
|
945
|
|
946 case LLVMbind:
|
|
947 if (VarDeclaration* vd = s->isVarDeclaration()) {
|
|
948 vd->llvmInternal = llvm_internal;
|
|
949 vd->llvmInternal1 = llvm_str1;
|
|
950 }
|
|
951 else {
|
|
952 error("may only be used on var declarations");
|
|
953 assert(0);
|
|
954 }
|
|
955 break;
|
|
956
|
|
957 default:
|
|
958 assert(0 && "invalid LLVM_internal pragma got through :/");
|
|
959 }
|
|
960 }
|
|
961
|
|
962 #endif
|
|
963 }
|
|
964 }
|
|
965 return;
|
|
966
|
|
967 Lnodecl:
|
|
968 if (decl)
|
|
969 error("pragma is missing closing ';'");
|
|
970 }
|
|
971
|
|
972 int PragmaDeclaration::oneMember(Dsymbol **ps)
|
|
973 {
|
|
974 *ps = NULL;
|
|
975 return TRUE;
|
|
976 }
|
|
977
|
|
978 char *PragmaDeclaration::kind()
|
|
979 {
|
|
980 return "pragma";
|
|
981 }
|
|
982
|
|
983 void PragmaDeclaration::toObjFile()
|
|
984 {
|
|
985 if (ident == Id::lib)
|
|
986 {
|
|
987 assert(args && args->dim == 1);
|
|
988
|
|
989 Expression *e = (Expression *)args->data[0];
|
|
990
|
|
991 assert(e->op == TOKstring);
|
|
992
|
|
993 StringExp *se = (StringExp *)e;
|
|
994 char *name = (char *)mem.malloc(se->len + 1);
|
|
995 memcpy(name, se->string, se->len);
|
|
996 name[se->len] = 0;
|
|
997 obj_includelib(name);
|
|
998 }
|
|
999 AttribDeclaration::toObjFile();
|
|
1000 }
|
|
1001
|
|
1002 void PragmaDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
1003 {
|
|
1004 buf->printf("pragma(%s", ident->toChars());
|
|
1005 if (args)
|
|
1006 {
|
|
1007 for (size_t i = 0; i < args->dim; i++)
|
|
1008 {
|
|
1009 Expression *e = (Expression *)args->data[i];
|
|
1010
|
|
1011 buf->writestring(", ");
|
|
1012 e->toCBuffer(buf, hgs);
|
|
1013 }
|
|
1014 }
|
|
1015 buf->writestring(")");
|
|
1016 AttribDeclaration::toCBuffer(buf, hgs);
|
|
1017 }
|
|
1018
|
|
1019
|
|
1020 /********************************* ConditionalDeclaration ****************************/
|
|
1021
|
|
1022 ConditionalDeclaration::ConditionalDeclaration(Condition *condition, Array *decl, Array *elsedecl)
|
|
1023 : AttribDeclaration(decl)
|
|
1024 {
|
|
1025 //printf("ConditionalDeclaration::ConditionalDeclaration()\n");
|
|
1026 this->condition = condition;
|
|
1027 this->elsedecl = elsedecl;
|
|
1028 }
|
|
1029
|
|
1030 Dsymbol *ConditionalDeclaration::syntaxCopy(Dsymbol *s)
|
|
1031 {
|
|
1032 ConditionalDeclaration *dd;
|
|
1033
|
|
1034 assert(!s);
|
|
1035 dd = new ConditionalDeclaration(condition->syntaxCopy(),
|
|
1036 Dsymbol::arraySyntaxCopy(decl),
|
|
1037 Dsymbol::arraySyntaxCopy(elsedecl));
|
|
1038 return dd;
|
|
1039 }
|
|
1040
|
|
1041
|
|
1042 int ConditionalDeclaration::oneMember(Dsymbol **ps)
|
|
1043 {
|
|
1044 //printf("ConditionalDeclaration::oneMember(), inc = %d\n", condition->inc);
|
|
1045 if (condition->inc)
|
|
1046 {
|
|
1047 Array *d = condition->include(NULL, NULL) ? decl : elsedecl;
|
|
1048 return Dsymbol::oneMembers(d, ps);
|
|
1049 }
|
|
1050 *ps = NULL;
|
|
1051 return TRUE;
|
|
1052 }
|
|
1053
|
|
1054 void ConditionalDeclaration::emitComment(Scope *sc)
|
|
1055 {
|
|
1056 //printf("ConditionalDeclaration::emitComment(sc = %p)\n", sc);
|
|
1057 if (condition->inc)
|
|
1058 {
|
|
1059 AttribDeclaration::emitComment(sc);
|
|
1060 }
|
|
1061 }
|
|
1062
|
|
1063 // Decide if 'then' or 'else' code should be included
|
|
1064
|
|
1065 Array *ConditionalDeclaration::include(Scope *sc, ScopeDsymbol *sd)
|
|
1066 {
|
|
1067 //printf("ConditionalDeclaration::include()\n");
|
|
1068 assert(condition);
|
|
1069 return condition->include(sc, sd) ? decl : elsedecl;
|
|
1070 }
|
|
1071
|
|
1072
|
|
1073 void ConditionalDeclaration::addComment(unsigned char *comment)
|
|
1074 {
|
|
1075 /* Because addComment is called by the parser, if we called
|
|
1076 * include() it would define a version before it was used.
|
|
1077 * But it's no problem to drill down to both decl and elsedecl,
|
|
1078 * so that's the workaround.
|
|
1079 */
|
|
1080
|
|
1081 if (comment)
|
|
1082 {
|
|
1083 Array *d = decl;
|
|
1084
|
|
1085 for (int j = 0; j < 2; j++)
|
|
1086 {
|
|
1087 if (d)
|
|
1088 {
|
|
1089 for (unsigned i = 0; i < d->dim; i++)
|
|
1090 { Dsymbol *s;
|
|
1091
|
|
1092 s = (Dsymbol *)d->data[i];
|
|
1093 //printf("ConditionalDeclaration::addComment %s\n", s->toChars());
|
|
1094 s->addComment(comment);
|
|
1095 }
|
|
1096 }
|
|
1097 d = elsedecl;
|
|
1098 }
|
|
1099 }
|
|
1100 }
|
|
1101
|
|
1102 void ConditionalDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
1103 {
|
|
1104 condition->toCBuffer(buf, hgs);
|
|
1105 if (decl || elsedecl)
|
|
1106 {
|
|
1107 buf->writenl();
|
|
1108 buf->writeByte('{');
|
|
1109 buf->writenl();
|
|
1110 if (decl)
|
|
1111 {
|
|
1112 for (unsigned i = 0; i < decl->dim; i++)
|
|
1113 {
|
|
1114 Dsymbol *s = (Dsymbol *)decl->data[i];
|
|
1115
|
|
1116 buf->writestring(" ");
|
|
1117 s->toCBuffer(buf, hgs);
|
|
1118 }
|
|
1119 }
|
|
1120 buf->writeByte('}');
|
|
1121 if (elsedecl)
|
|
1122 {
|
|
1123 buf->writenl();
|
|
1124 buf->writestring("else");
|
|
1125 buf->writenl();
|
|
1126 buf->writeByte('{');
|
|
1127 buf->writenl();
|
|
1128 for (unsigned i = 0; i < elsedecl->dim; i++)
|
|
1129 {
|
|
1130 Dsymbol *s = (Dsymbol *)elsedecl->data[i];
|
|
1131
|
|
1132 buf->writestring(" ");
|
|
1133 s->toCBuffer(buf, hgs);
|
|
1134 }
|
|
1135 buf->writeByte('}');
|
|
1136 }
|
|
1137 }
|
|
1138 else
|
|
1139 buf->writeByte(':');
|
|
1140 buf->writenl();
|
|
1141 }
|
|
1142
|
|
1143 /***************************** StaticIfDeclaration ****************************/
|
|
1144
|
|
1145 StaticIfDeclaration::StaticIfDeclaration(Condition *condition,
|
|
1146 Array *decl, Array *elsedecl)
|
|
1147 : ConditionalDeclaration(condition, decl, elsedecl)
|
|
1148 {
|
|
1149 //printf("StaticIfDeclaration::StaticIfDeclaration()\n");
|
|
1150 sd = NULL;
|
|
1151 addisdone = 0;
|
|
1152 }
|
|
1153
|
|
1154
|
|
1155 Dsymbol *StaticIfDeclaration::syntaxCopy(Dsymbol *s)
|
|
1156 {
|
|
1157 StaticIfDeclaration *dd;
|
|
1158
|
|
1159 assert(!s);
|
|
1160 dd = new StaticIfDeclaration(condition->syntaxCopy(),
|
|
1161 Dsymbol::arraySyntaxCopy(decl),
|
|
1162 Dsymbol::arraySyntaxCopy(elsedecl));
|
|
1163 return dd;
|
|
1164 }
|
|
1165
|
|
1166
|
|
1167 int StaticIfDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
|
|
1168 {
|
|
1169 /* This is deferred until semantic(), so that
|
|
1170 * expressions in the condition can refer to declarations
|
|
1171 * in the same scope, such as:
|
|
1172 *
|
|
1173 * template Foo(int i)
|
|
1174 * {
|
|
1175 * const int j = i + 1;
|
|
1176 * static if (j == 3)
|
|
1177 * const int k;
|
|
1178 * }
|
|
1179 */
|
|
1180 this->sd = sd;
|
|
1181 int m = 0;
|
|
1182
|
|
1183 if (memnum == 0)
|
|
1184 { m = AttribDeclaration::addMember(sc, sd, memnum);
|
|
1185 addisdone = 1;
|
|
1186 }
|
|
1187 return m;
|
|
1188 }
|
|
1189
|
|
1190
|
|
1191 void StaticIfDeclaration::semantic(Scope *sc)
|
|
1192 {
|
|
1193 Array *d = include(sc, sd);
|
|
1194
|
|
1195 //printf("\tStaticIfDeclaration::semantic '%s'\n",toChars());
|
|
1196 if (d)
|
|
1197 {
|
|
1198 if (!addisdone)
|
|
1199 { AttribDeclaration::addMember(sc, sd, 1);
|
|
1200 addisdone = 1;
|
|
1201 }
|
|
1202
|
|
1203 for (unsigned i = 0; i < d->dim; i++)
|
|
1204 {
|
|
1205 Dsymbol *s = (Dsymbol *)d->data[i];
|
|
1206
|
|
1207 s->semantic(sc);
|
|
1208 }
|
|
1209 }
|
|
1210 }
|
|
1211
|
|
1212 char *StaticIfDeclaration::kind()
|
|
1213 {
|
|
1214 return "static if";
|
|
1215 }
|
|
1216
|
|
1217
|
|
1218 /***************************** CompileDeclaration *****************************/
|
|
1219
|
|
1220 CompileDeclaration::CompileDeclaration(Loc loc, Expression *exp)
|
|
1221 : AttribDeclaration(NULL)
|
|
1222 {
|
|
1223 this->exp = exp;
|
|
1224 this->sd = NULL;
|
|
1225 }
|
|
1226
|
|
1227 Dsymbol *CompileDeclaration::syntaxCopy(Dsymbol *s)
|
|
1228 {
|
|
1229 //printf("CompileDeclaration::syntaxCopy('%s')\n", toChars());
|
|
1230 CompileDeclaration *sc = new CompileDeclaration(loc, exp->syntaxCopy());
|
|
1231 return sc;
|
|
1232 }
|
|
1233
|
|
1234 int CompileDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
|
|
1235 {
|
|
1236 this->sd = sd;
|
|
1237 return memnum;
|
|
1238 }
|
|
1239
|
|
1240 void CompileDeclaration::semantic(Scope *sc)
|
|
1241 {
|
|
1242 //printf("CompileDeclaration::semantic()\n");
|
|
1243 exp = exp->semantic(sc);
|
|
1244 exp = resolveProperties(sc, exp);
|
|
1245 exp = exp->optimize(WANTvalue | WANTinterpret);
|
|
1246 if (exp->op != TOKstring)
|
|
1247 { error("argument to mixin must be a string, not (%s)", exp->toChars());
|
|
1248 return;
|
|
1249 }
|
|
1250 StringExp *se = (StringExp *)exp;
|
|
1251 se = se->toUTF8(sc);
|
|
1252 Parser p(sc->module, (unsigned char *)se->string, se->len, 0);
|
|
1253 p.loc = loc;
|
|
1254 p.nextToken();
|
|
1255 decl = p.parseDeclDefs(0);
|
|
1256 if (p.token.value != TOKeof)
|
|
1257 {
|
|
1258 error("incomplete mixin declaration (%s)", se->toChars());
|
|
1259 }
|
|
1260
|
|
1261 AttribDeclaration::addMember(sc, sd, 0);
|
|
1262 AttribDeclaration::semantic(sc);
|
|
1263 }
|
|
1264
|
|
1265 void CompileDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
1266 {
|
|
1267 buf->writestring("mixin(");
|
|
1268 exp->toCBuffer(buf, hgs);
|
|
1269 buf->writestring(");");
|
|
1270 buf->writenl();
|
|
1271 }
|