comparison dmd/TypeStruct.d @ 174:af724d3510d7

lot os toCBuffer methods implemented moved shared Type.* stuff into Global
author korDen
date Sun, 10 Oct 2010 03:47:23 +0400
parents 14feb7ae01a6
children e3afd1303184
comparison
equal deleted inserted replaced
173:d237b38b5858 174:af724d3510d7
68 { 68 {
69 super(TY.Tstruct); 69 super(TY.Tstruct);
70 this.sym = sym; 70 this.sym = sym;
71 } 71 }
72 version (DumbClone) { 72 version (DumbClone) {
73 } else { 73 } else {
74 final TypeStruct cloneTo(TypeStruct t) 74 final TypeStruct cloneTo(TypeStruct t)
75 { 75 {
76 super.cloneTo(t); 76 super.cloneTo(t);
77 assert(t.sym is sym); 77 assert(t.sym is sym);
78 return t; 78 return t;
79 } 79 }
80 80
81 TypeStruct clone() 81 TypeStruct clone()
82 { 82 {
83 assert(this.classinfo == TypeStruct.classinfo); 83 assert(this.classinfo == TypeStruct.classinfo);
84 return cloneTo(new TypeStruct(sym)); 84 return cloneTo(new TypeStruct(sym));
85 } 85 }
86 } 86 }
87 override ulong size(Loc loc) 87 override ulong size(Loc loc)
88 { 88 {
89 return sym.size(loc); 89 return sym.size(loc);
90 } 90 }
91 91
92 override uint alignsize() 92 override uint alignsize()
93 { 93 {
94 uint sz; 94 uint sz;
95 95
96 sym.size(Loc(0)); // give error for forward references 96 sym.size(Loc(0)); // give error for forward references
97 sz = sym.alignsize; 97 sz = sym.alignsize;
98 if (sz > sym.structalign) 98 if (sz > sym.structalign)
99 sz = sym.structalign; 99 sz = sym.structalign;
100 return sz; 100 return sz;
101 } 101 }
102 102
103 override string toChars() 103 override string toChars()
104 { 104 {
105 //printf("sym.parent: %s, deco = %s\n", sym.parent.toChars(), deco); 105 //printf("sym.parent: %s, deco = %s\n", sym.parent.toChars(), deco);
106 if (mod) 106 if (mod)
107 return Type.toChars(); 107 return Type.toChars();
110 { 110 {
111 return ti.toChars(); 111 return ti.toChars();
112 } 112 }
113 return sym.toChars(); 113 return sym.toChars();
114 } 114 }
115 115
116 override Type syntaxCopy() 116 override Type syntaxCopy()
117 { 117 {
118 assert(false); 118 assert(false);
119 } 119 }
120 120
121 override Type semantic(Loc loc, Scope sc) 121 override Type semantic(Loc loc, Scope sc)
122 { 122 {
123 //printf("TypeStruct.semantic('%s')\n", sym.toChars()); 123 //printf("TypeStruct.semantic('%s')\n", sym.toChars());
124 124
125 /* Cannot do semantic for sym because scope chain may not 125 /* Cannot do semantic for sym because scope chain may not
127 */ 127 */
128 //sym.semantic(sc); 128 //sym.semantic(sc);
129 129
130 return merge(); 130 return merge();
131 } 131 }
132 132
133 override Dsymbol toDsymbol(Scope sc) 133 override Dsymbol toDsymbol(Scope sc)
134 { 134 {
135 return sym; 135 return sym;
136 } 136 }
137 137
138 override void toDecoBuffer(OutBuffer buf, int flag) 138 override void toDecoBuffer(OutBuffer buf, int flag)
139 { 139 {
140 string name = sym.mangle(); 140 string name = sym.mangle();
141 //printf("TypeStruct.toDecoBuffer('%s') = '%s'\n", toChars(), name); 141 //printf("TypeStruct.toDecoBuffer('%s') = '%s'\n", toChars(), name);
142 Type.toDecoBuffer(buf, flag); 142 Type.toDecoBuffer(buf, flag);
143 buf.printf("%s", name); 143 buf.printf("%s", name);
144 } 144 }
145 145
146 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) 146 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod)
147 { 147 {
148 if (mod != this.mod) 148 if (mod != this.mod)
149 { 149 {
150 toCBuffer3(buf, hgs, mod); 150 toCBuffer3(buf, hgs, mod);
151 return; 151 return;
152 } 152 }
153 TemplateInstance ti = sym.parent.isTemplateInstance(); 153 TemplateInstance ti = sym.parent.isTemplateInstance();
154 if (ti && ti.toAlias() == sym) 154 if (ti && ti.toAlias() == sym)
155 buf.writestring(ti.toChars()); 155 buf.writestring(ti.toChars());
156 else 156 else
157 buf.writestring(sym.toChars()); 157 buf.writestring(sym.toChars());
158 } 158 }
159 159
160 override Expression dotExp(Scope sc, Expression e, Identifier ident) 160 override Expression dotExp(Scope sc, Expression e, Identifier ident)
161 { 161 {
162 uint offset; 162 uint offset;
163 163
164 VarDeclaration v; 164 VarDeclaration v;
184 */ 184 */
185 e = e.semantic(sc); // do this before turning on noaccesscheck 185 e = e.semantic(sc); // do this before turning on noaccesscheck
186 auto exps = new Expressions; 186 auto exps = new Expressions;
187 exps.reserve(sym.fields.dim); 187 exps.reserve(sym.fields.dim);
188 for (size_t i = 0; i < sym.fields.dim; i++) 188 for (size_t i = 0; i < sym.fields.dim; i++)
189 { 189 {
190 VarDeclaration v2 = cast(VarDeclaration)sym.fields[i]; 190 VarDeclaration v2 = cast(VarDeclaration)sym.fields[i];
191 Expression fe = new DotVarExp(e.loc, e, v2); 191 Expression fe = new DotVarExp(e.loc, e, v2);
192 exps.push(fe); 192 exps.push(fe);
193 } 193 }
194 e = new TupleExp(e.loc, exps); 194 e = new TupleExp(e.loc, exps);
198 sc.pop(); 198 sc.pop();
199 return e; 199 return e;
200 } 200 }
201 201
202 if (e.op == TOK.TOKdotexp) 202 if (e.op == TOK.TOKdotexp)
203 { 203 {
204 DotExp de2 = cast(DotExp)e; 204 DotExp de2 = cast(DotExp)e;
205 205
206 if (de2.e1.op == TOK.TOKimport) 206 if (de2.e1.op == TOK.TOKimport)
207 { 207 {
208 assert(0); // cannot find a case where this happens; leave 208 assert(0); // cannot find a case where this happens; leave
230 v = s.isVarDeclaration(); 230 v = s.isVarDeclaration();
231 if (v && !v.isDataseg()) 231 if (v && !v.isDataseg())
232 { 232 {
233 Expression ei = v.getConstInitializer(); 233 Expression ei = v.getConstInitializer();
234 if (ei) 234 if (ei)
235 { 235 {
236 e = ei.copy(); // need to copy it if it's a StringExp 236 e = ei.copy(); // need to copy it if it's a StringExp
237 e = e.semantic(sc); 237 e = e.semantic(sc);
238 return e; 238 return e;
239 } 239 }
240 } 240 }
268 return e; 268 return e;
269 } 269 }
270 270
271 TemplateInstance ti = s.isTemplateInstance(); 271 TemplateInstance ti = s.isTemplateInstance();
272 if (ti) 272 if (ti)
273 { 273 {
274 if (!ti.semanticRun) 274 if (!ti.semanticRun)
275 ti.semantic(sc); 275 ti.semantic(sc);
276 s = ti.inst.toAlias(); 276 s = ti.inst.toAlias();
277 if (!s.isTemplateInstance()) 277 if (!s.isTemplateInstance())
278 goto L1; 278 goto L1;
289 return e; 289 return e;
290 } 290 }
291 291
292 OverloadSet o = s.isOverloadSet(); 292 OverloadSet o = s.isOverloadSet();
293 if (o) 293 if (o)
294 { 294 {
295 /* We really should allow this, triggered by: 295 /* We really should allow this, triggered by:
296 * template c() 296 * template c()
297 * { 297 * {
298 * void a(); 298 * void a();
299 * void b () { this.a(); } 299 * void b () { this.a(); }
316 writef("d = %s '%s'\n", s.kind(), s.toChars()); 316 writef("d = %s '%s'\n", s.kind(), s.toChars());
317 } 317 }
318 assert(d); 318 assert(d);
319 319
320 if (e.op == TOK.TOKtype) 320 if (e.op == TOK.TOKtype)
321 { 321 {
322 FuncDeclaration fd = sc.func; 322 FuncDeclaration fd = sc.func;
323 323
324 if (d.isTupleDeclaration()) 324 if (d.isTupleDeclaration())
325 { 325 {
326 e = new TupleExp(e.loc, d.isTupleDeclaration()); 326 e = new TupleExp(e.loc, d.isTupleDeclaration());
367 } 367 }
368 368
369 de = new DotVarExp(e.loc, e, d); 369 de = new DotVarExp(e.loc, e, d);
370 return de.semantic(sc); 370 return de.semantic(sc);
371 } 371 }
372 372
373 override uint memalign(uint salign) 373 override uint memalign(uint salign)
374 { 374 {
375 sym.size(Loc(0)); // give error for forward references 375 sym.size(Loc(0)); // give error for forward references
376 return sym.structalign; 376 return sym.structalign;
377 } 377 }
378 378
379 override Expression defaultInit(Loc loc) 379 override Expression defaultInit(Loc loc)
380 { 380 {
381 version (LOGDEFAULTINIT) { 381 version (LOGDEFAULTINIT) {
382 printf("TypeStruct::defaultInit() '%s'\n", toChars()); 382 printf("TypeStruct::defaultInit() '%s'\n", toChars());
383 } 383 }
385 Declaration d = new SymbolDeclaration(sym.loc, s, sym); 385 Declaration d = new SymbolDeclaration(sym.loc, s, sym);
386 assert(d); 386 assert(d);
387 d.type = this; 387 d.type = this;
388 return new VarExp(sym.loc, d); 388 return new VarExp(sym.loc, d);
389 } 389 }
390 390
391 /*************************************** 391 /***************************************
392 * Use when we prefer the default initializer to be a literal, 392 * Use when we prefer the default initializer to be a literal,
393 * rather than a global immutable variable. 393 * rather than a global immutable variable.
394 */ 394 */
395 override Expression defaultInitLiteral(Loc loc) 395 override Expression defaultInitLiteral(Loc loc)
404 auto vd = cast(VarDeclaration)(sym.fields[j]); 404 auto vd = cast(VarDeclaration)(sym.fields[j]);
405 Expression e; 405 Expression e;
406 if (vd.init) 406 if (vd.init)
407 e = vd.init.toExpression(); 407 e = vd.init.toExpression();
408 else 408 else
409 e = vd.type.defaultInitLiteral(); 409 e = vd.type.defaultInitLiteral(Loc(0));
410 structelems[j] = e; 410 structelems[j] = e;
411 } 411 }
412 auto structinit = new StructLiteralExp(loc, cast(StructDeclaration)sym, structelems); 412 auto structinit = new StructLiteralExp(loc, cast(StructDeclaration)sym, structelems);
413 // Why doesn't the StructLiteralExp constructor do this, when 413 // Why doesn't the StructLiteralExp constructor do this, when
414 // sym->type != NULL ? 414 // sym->type != NULL ?
415 structinit.type = sym.type; 415 structinit.type = sym.type;
416 return structinit; 416 return structinit;
417 } 417 }
418 418
419 419
420 override bool isZeroInit(Loc loc) 420 override bool isZeroInit(Loc loc)
421 { 421 {
422 return sym.zeroInit; 422 return sym.zeroInit;
423 } 423 }
424 424
425 override bool isAssignable() 425 override bool isAssignable()
426 { 426 {
427 /* If any of the fields are const or invariant, 427 /* If any of the fields are const or invariant,
428 * then one cannot assign this struct. 428 * then one cannot assign this struct.
429 */ 429 */
430 for (size_t i = 0; i < sym.fields.dim; i++) 430 for (size_t i = 0; i < sym.fields.dim; i++)
431 { 431 {
432 VarDeclaration v = cast(VarDeclaration)sym.fields[i]; 432 VarDeclaration v = cast(VarDeclaration)sym.fields[i];
433 if (v.isConst() || v.isImmutable()) 433 if (v.isConst() || v.isImmutable())
434 return false; 434 return false;
435 } 435 }
436 return true; 436 return true;
437 } 437 }
438 438
439 override bool checkBoolean() 439 override bool checkBoolean()
440 { 440 {
441 return false; 441 return false;
442 } 442 }
443 443
444 override dt_t** toDt(dt_t** pdt) 444 override dt_t** toDt(dt_t** pdt)
445 { 445 {
446 sym.toDt(pdt); 446 sym.toDt(pdt);
447 return pdt; 447 return pdt;
448 } 448 }
449 449
450 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) 450 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes)
451 { 451 {
452 //printf("TypeStruct.deduceType()\n"); 452 //printf("TypeStruct.deduceType()\n");
453 //printf("\tthis.parent = %s, ", sym.parent.toChars()); print(); 453 //printf("\tthis.parent = %s, ", sym.parent.toChars()); print();
454 //printf("\ttparam = %d, ", tparam.ty); tparam.print(); 454 //printf("\ttparam = %d, ", tparam.ty); tparam.print();
497 if (sym != tp.sym) 497 if (sym != tp.sym)
498 return MATCHnomatch; 498 return MATCHnomatch;
499 } 499 }
500 return Type.deduceType(sc, tparam, parameters, dedtypes); 500 return Type.deduceType(sc, tparam, parameters, dedtypes);
501 } 501 }
502 502
503 override TypeInfoDeclaration getTypeInfoDeclaration() 503 override TypeInfoDeclaration getTypeInfoDeclaration()
504 { 504 {
505 return new TypeInfoStructDeclaration(this); 505 return new TypeInfoStructDeclaration(this);
506 } 506 }
507 507
508 override bool hasPointers() 508 override bool hasPointers()
509 { 509 {
510 // Probably should cache this information in sym rather than recompute 510 // Probably should cache this information in sym rather than recompute
511 StructDeclaration s = sym; 511 StructDeclaration s = sym;
512 512
518 return true; 518 return true;
519 } 519 }
520 520
521 return false; 521 return false;
522 } 522 }
523 523
524 override MATCH implicitConvTo(Type to) 524 override MATCH implicitConvTo(Type to)
525 { 525 {
526 MATCH m; 526 MATCH m;
527 527
528 //printf("TypeStruct.implicitConvTo(%s => %s)\n", toChars(), to.toChars()); 528 //printf("TypeStruct.implicitConvTo(%s => %s)\n", toChars(), to.toChars());
529 if (ty == to.ty && sym == (cast(TypeStruct)to).sym) 529 if (ty == to.ty && sym == (cast(TypeStruct)to).sym)
530 { 530 {
531 m = MATCHexact; // exact match 531 m = MATCHexact; // exact match
532 if (mod != to.mod) 532 if (mod != to.mod)
533 { 533 {
534 if (MODimplicitConv(mod, to.mod)) 534 if (MODimplicitConv(mod, to.mod))
535 m = MATCHconst; 535 m = MATCHconst;
536 else 536 else
537 { /* Check all the fields. If they can all be converted, 537 { /* Check all the fields. If they can all be converted,
538 * allow the conversion. 538 * allow the conversion.
539 */ 539 */
540 foreach (VarDeclaration v; sym.fields) 540 foreach (VarDeclaration v; sym.fields)
541 { 541 {
542 assert(v && v.storage_class & STCfield); 542 assert(v && v.storage_class & STCfield);
543 543
544 // 'from' type 544 // 'from' type
545 Type tvf = v.type.addMod(mod); 545 Type tvf = v.type.addMod(mod);
546 546
558 else if (sym.aliasthis) 558 else if (sym.aliasthis)
559 { 559 {
560 m = MATCHnomatch; 560 m = MATCHnomatch;
561 Declaration d = sym.aliasthis.isDeclaration(); 561 Declaration d = sym.aliasthis.isDeclaration();
562 if (d) 562 if (d)
563 { 563 {
564 assert(d.type); 564 assert(d.type);
565 Type t = d.type.addMod(mod); 565 Type t = d.type.addMod(mod);
566 m = t.implicitConvTo(to); 566 m = t.implicitConvTo(to);
567 } 567 }
568 } 568 }
569 else 569 else
570 m = MATCHnomatch; // no match 570 m = MATCHnomatch; // no match
571 return m; 571 return m;
572 } 572 }
573 573
574 override MATCH constConv(Type to) 574 override MATCH constConv(Type to)
575 { 575 {
576 if (equals(to)) 576 if (equals(to))
577 return MATCHexact; 577 return MATCHexact;
578 if (ty == to.ty && sym == (cast(TypeStruct)to).sym && 578 if (ty == to.ty && sym == (cast(TypeStruct)to).sym &&
579 MODimplicitConv(mod, to.mod)) 579 MODimplicitConv(mod, to.mod))
580 return MATCHconst; 580 return MATCHconst;
581 return MATCHnomatch; 581 return MATCHnomatch;
582 } 582 }
583 583
584 override Type toHeadMutable() 584 override Type toHeadMutable()
585 { 585 {
586 assert(false); 586 assert(false);
587 } 587 }
588 588
589 version (CPP_MANGLE) { 589 version (CPP_MANGLE) {
590 void toCppMangle(OutBuffer buf, CppMangleState* cms) 590 void toCppMangle(OutBuffer buf, CppMangleState* cms)
591 { 591 {
592 assert(false); 592 assert(false);
593 } 593 }
623 /* Add in fields of the struct 623 /* Add in fields of the struct
624 * (after setting ctype to avoid infinite recursion) 624 * (after setting ctype to avoid infinite recursion)
625 */ 625 */
626 if (global.params.symdebug) { 626 if (global.params.symdebug) {
627 for (int i = 0; i < sym.fields.dim; i++) 627 for (int i = 0; i < sym.fields.dim; i++)
628 { 628 {
629 VarDeclaration v = cast(VarDeclaration)sym.fields[i]; 629 VarDeclaration v = cast(VarDeclaration)sym.fields[i];
630 630
631 Symbol* s2 = symbol_name(toStringz(v.ident.toChars()), SC.SCmember, v.type.toCtype()); 631 Symbol* s2 = symbol_name(toStringz(v.ident.toChars()), SC.SCmember, v.type.toCtype());
632 s2.Smemoff = v.offset; 632 s2.Smemoff = v.offset;
633 list_append(&s.Sstruct.Sfldlst, s2); 633 list_append(&s.Sstruct.Sfldlst, s2);