comparison dmd/TemplateDeclaration.d @ 104:a1cf34da9ebe

+ TemplateDeclaration.toCBuffer + TypeExp.rvalue + TypeExp.toElem
author Trass3r
date Tue, 31 Aug 2010 16:35:41 +0200
parents 3a0b150c9841
children 12c0c84d13fd
comparison
equal deleted inserted replaced
102:3ddadd0c4534 104:a1cf34da9ebe
51 */ 51 */
52 52
53 TemplateTupleParameter isVariadic(TemplateParameters parameters) 53 TemplateTupleParameter isVariadic(TemplateParameters parameters)
54 { 54 {
55 size_t dim = parameters.dim; 55 size_t dim = parameters.dim;
56 TemplateTupleParameter tp = null; 56 TemplateTupleParameter tp = null;
57 57
58 if (dim) 58 if (dim)
59 tp = (cast(TemplateParameter)parameters.data[dim - 1]).isTemplateTupleParameter(); 59 tp = (cast(TemplateParameter)parameters.data[dim - 1]).isTemplateTupleParameter();
60 60
61 return tp; 61 return tp;
62 } 62 }
63 63
64 void ObjectToCBuffer(OutBuffer buf, HdrGenState* hgs, Object oarg) 64 void ObjectToCBuffer(OutBuffer buf, HdrGenState* hgs, Object oarg)
65 { 65 {
66 //printf("ObjectToCBuffer()\n"); 66 //printf("ObjectToCBuffer()\n");
67 Type t = isType(oarg); 67 Type t = isType(oarg);
68 Expression e = isExpression(oarg); 68 Expression e = isExpression(oarg);
69 Dsymbol s = isDsymbol(oarg); 69 Dsymbol s = isDsymbol(oarg);
70 Tuple v = isTuple(oarg); 70 Tuple v = isTuple(oarg);
71 if (t) 71 if (t)
72 { 72 {
73 //printf("\tt: %s ty = %d\n", t.toChars(), t.ty); 73 //printf("\tt: %s ty = %d\n", t.toChars(), t.ty);
74 t.toCBuffer(buf, null, hgs); 74 t.toCBuffer(buf, null, hgs);
75 } 75 }
76 else if (e) 76 else if (e)
77 e.toCBuffer(buf, hgs); 77 e.toCBuffer(buf, hgs);
78 else if (s) 78 else if (s)
79 { 79 {
80 string p = s.ident ? s.ident.toChars() : s.toChars(); 80 string p = s.ident ? s.ident.toChars() : s.toChars();
81 buf.writestring(p); 81 buf.writestring(p);
82 } 82 }
83 else if (v) 83 else if (v)
84 { 84 {
85 Objects args = v.objects; 85 Objects args = v.objects;
86 for (size_t i = 0; i < args.dim; i++) 86 for (size_t i = 0; i < args.dim; i++)
87 { 87 {
88 if (i) 88 if (i)
89 buf.writeByte(','); 89 buf.writeByte(',');
90 Object o = cast(Object)args.data[i]; 90 Object o = cast(Object)args.data[i];
91 ObjectToCBuffer(buf, hgs, o); 91 ObjectToCBuffer(buf, hgs, o);
92 } 92 }
93 } 93 }
94 else if (!oarg) 94 else if (!oarg)
95 { 95 {
96 buf.writestring("null"); 96 buf.writestring("null");
97 } 97 }
98 else 98 else
99 { 99 {
100 debug writef("bad Object = %p\n", oarg); 100 debug writef("bad Object = %p\n", oarg);
101 assert(0); 101 assert(0);
102 } 102 }
103 } 103 }
104 104
105 class TemplateDeclaration : ScopeDsymbol 105 class TemplateDeclaration : ScopeDsymbol
106 { 106 {
107 TemplateParameters parameters; // array of TemplateParameter's 107 TemplateParameters parameters; // array of TemplateParameter's
108 108
109 TemplateParameters origParameters; // originals for Ddoc 109 TemplateParameters origParameters; // originals for Ddoc
110 Expression constraint; 110 Expression constraint;
111 Array instances; // array of TemplateInstance's 111 Array instances; // array of TemplateInstance's
112 112
113 TemplateDeclaration overnext; // next overloaded TemplateDeclaration 113 TemplateDeclaration overnext; // next overloaded TemplateDeclaration
114 TemplateDeclaration overroot; // first in overnext list 114 TemplateDeclaration overroot; // first in overnext list
115 115
116 int semanticRun; // 1 semantic() run 116 int semanticRun; // 1 semantic() run
117 117
118 Dsymbol onemember; // if !=NULL then one member of this template 118 Dsymbol onemember; // if !=NULL then one member of this template
119 119
120 int literal; // this template declaration is a literal 120 int literal; // this template declaration is a literal
121 121
122 this(Loc loc, Identifier id, TemplateParameters parameters, Expression constraint, Dsymbols decldefs) 122 this(Loc loc, Identifier id, TemplateParameters parameters, Expression constraint, Dsymbols decldefs)
123 { 123 {
124 super(id); 124 super(id);
125 125
126 version (LOG) { 126 version (LOG) {
127 printf("TemplateDeclaration(this = %p, id = '%s')\n", this, id.toChars()); 127 printf("TemplateDeclaration(this = %p, id = '%s')\n", this, id.toChars());
148 this.members = decldefs; 148 this.members = decldefs;
149 149
150 instances = new Array(); 150 instances = new Array();
151 } 151 }
152 152
153 override Dsymbol syntaxCopy(Dsymbol) 153 override Dsymbol syntaxCopy(Dsymbol)
154 { 154 {
155 //printf("TemplateDeclaration.syntaxCopy()\n"); 155 //printf("TemplateDeclaration.syntaxCopy()\n");
156 TemplateDeclaration td; 156 TemplateDeclaration td;
157 TemplateParameters p; 157 TemplateParameters p;
158 Dsymbols d; 158 Dsymbols d;
175 d = Dsymbol.arraySyntaxCopy(members); 175 d = Dsymbol.arraySyntaxCopy(members);
176 td = new TemplateDeclaration(loc, ident, p, e, d); 176 td = new TemplateDeclaration(loc, ident, p, e, d);
177 return td; 177 return td;
178 } 178 }
179 179
180 override void semantic(Scope sc) 180 override void semantic(Scope sc)
181 { 181 {
182 version (LOG) { 182 version (LOG) {
183 printf("TemplateDeclaration.semantic(this = %p, id = '%s')\n", this, ident.toChars()); 183 printf("TemplateDeclaration.semantic(this = %p, id = '%s')\n", this, ident.toChars());
184 } 184 }
185 if (semanticRun) 185 if (semanticRun)
271 271
272 /********************************** 272 /**********************************
273 * Overload existing TemplateDeclaration 'this' with the new one 's'. 273 * Overload existing TemplateDeclaration 'this' with the new one 's'.
274 * Return !=0 if successful; i.e. no conflict. 274 * Return !=0 if successful; i.e. no conflict.
275 */ 275 */
276 override bool overloadInsert(Dsymbol s) 276 override bool overloadInsert(Dsymbol s)
277 { 277 {
278 TemplateDeclaration *pf; 278 TemplateDeclaration *pf;
279 TemplateDeclaration f; 279 TemplateDeclaration f;
280 280
281 version (LOG) { 281 version (LOG) {
321 } 321 }
322 322
323 return true; 323 return true;
324 } 324 }
325 325
326 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) 326 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
327 { 327 {
328 assert(false); 328 static if (false) // Should handle template functions
329 {
330 if (onemember && onemember.isFuncDeclaration())
331 buf.writestring("foo ");
332 }
333 buf.writestring(kind());
334 buf.writeByte(' ');
335 buf.writestring(ident.toChars());
336 buf.writeByte('(');
337 for (int i = 0; i < parameters.dim; i++)
338 {
339 TemplateParameter tp = cast(TemplateParameter)parameters.data[i];
340 if (hgs.ddoc)
341 tp = cast(TemplateParameter)origParameters.data[i];
342 if (i)
343 buf.writeByte(',');
344 tp.toCBuffer(buf, hgs);
345 }
346 buf.writeByte(')');
347 version(DMDV2)
348 {
349 if (constraint)
350 { buf.writestring(" if (");
351 constraint.toCBuffer(buf, hgs);
352 buf.writeByte(')');
353 }
354 }
355
356 if (hgs.hdrgen)
357 {
358 hgs.tpltMember++;
359 buf.writenl();
360 buf.writebyte('{');
361 buf.writenl();
362 foreach (Dsymbol s; members)
363 s.toCBuffer(buf, hgs);
364
365 buf.writebyte('}');
366 buf.writenl();
367 hgs.tpltMember--;
368 }
329 } 369 }
330 370
331 override void toJsonBuffer(OutBuffer buf) 371 override void toJsonBuffer(OutBuffer buf)
332 { 372 {
333 //writef("TemplateDeclaration.toJsonBuffer()\n"); 373 //writef("TemplateDeclaration.toJsonBuffer()\n");
356 buf.writestring("]\n"); 396 buf.writestring("]\n");
357 397
358 buf.writestring("}\n"); 398 buf.writestring("}\n");
359 } 399 }
360 400
361 override string kind() 401 override string kind()
362 { 402 {
363 return (onemember && onemember.isAggregateDeclaration()) 403 return (onemember && onemember.isAggregateDeclaration())
364 ? onemember.kind() 404 ? onemember.kind()
365 : "template"; 405 : "template";
366 } 406 }
367 407
368 override string toChars() 408 override string toChars()
369 { 409 {
370 OutBuffer buf = new OutBuffer(); 410 OutBuffer buf = new OutBuffer();
371 HdrGenState hgs; 411 HdrGenState hgs;
372 412
373 /// memset(&hgs, 0, hgs.sizeof); 413 /// memset(&hgs, 0, hgs.sizeof);
391 } 431 }
392 buf.writeByte(0); 432 buf.writeByte(0);
393 return buf.extractString(); 433 return buf.extractString();
394 } 434 }
395 435
396 override void emitComment(Scope sc) 436 override void emitComment(Scope sc)
397 { 437 {
398 assert(false); 438 assert(false);
399 } 439 }
400 440
401 // void toDocBuffer(OutBuffer *buf); 441 // void toDocBuffer(OutBuffer *buf);
402 442
403 /*************************************** 443 /***************************************
404 * Given that ti is an instance of this TemplateDeclaration, 444 * Given that ti is an instance of this TemplateDeclaration,
405 * deduce the types of the parameters to this, and store 445 * deduce the types of the parameters to this, and store
406 * those deduced types in dedtypes[]. 446 * those deduced types in dedtypes[].
409 * 2: don't change types in matchArg() 449 * 2: don't change types in matchArg()
410 * Output: 450 * Output:
411 * dedtypes deduced arguments 451 * dedtypes deduced arguments
412 * Return match level. 452 * Return match level.
413 */ 453 */
414 MATCH matchWithInstance(TemplateInstance ti, Objects dedtypes, int flag) 454 MATCH matchWithInstance(TemplateInstance ti, Objects dedtypes, int flag)
415 { 455 {
416 MATCH m; 456 MATCH m;
417 int dedtypes_dim = dedtypes.dim; 457 int dedtypes_dim = dedtypes.dim;
418 458
419 version (LOGM) { 459 version (LOGM) {
571 * Determine partial specialization order of 'this' vs td2. 611 * Determine partial specialization order of 'this' vs td2.
572 * Returns: 612 * Returns:
573 * match this is at least as specialized as td2 613 * match this is at least as specialized as td2
574 * 0 td2 is more specialized than this 614 * 0 td2 is more specialized than this
575 */ 615 */
576 MATCH leastAsSpecialized(TemplateDeclaration td2) 616 MATCH leastAsSpecialized(TemplateDeclaration td2)
577 { 617 {
578 /* This works by taking the template parameters to this template 618 /* This works by taking the template parameters to this template
579 * declaration and feeding them to td2 as if it were a template 619 * declaration and feeding them to td2 as if it were a template
580 * instance. 620 * instance.
581 * If it works, then this template is at least as specialized 621 * If it works, then this template is at least as specialized
582 * as td2. 622 * as td2.
583 */ 623 */
640 * Output: 680 * Output:
641 * dedargs Expression/Type deduced template arguments 681 * dedargs Expression/Type deduced template arguments
642 * Returns: 682 * Returns:
643 * match level 683 * match level
644 */ 684 */
645 MATCH deduceFunctionTemplateMatch(Loc loc, Objects targsi, Expression ethis, Expressions fargs, Objects dedargs) 685 MATCH deduceFunctionTemplateMatch(Loc loc, Objects targsi, Expression ethis, Expressions fargs, Objects dedargs)
646 { 686 {
647 size_t nfparams; 687 size_t nfparams;
648 size_t nfargs; 688 size_t nfargs;
649 size_t nargsi; // array size of targsi 689 size_t nargsi; // array size of targsi
650 int fptupindex = -1; 690 int fptupindex = -1;
1094 Lnomatch: 1134 Lnomatch:
1095 paramscope.pop(); 1135 paramscope.pop();
1096 //printf("\tnomatch\n"); 1136 //printf("\tnomatch\n");
1097 return MATCHnomatch; 1137 return MATCHnomatch;
1098 } 1138 }
1099 1139
1100 /************************************************* 1140 /*************************************************
1101 * Given function arguments, figure out which template function 1141 * Given function arguments, figure out which template function
1102 * to expand, and return that function. 1142 * to expand, and return that function.
1103 * If no match, give error message and return null. 1143 * If no match, give error message and return null.
1104 * Input: 1144 * Input:
1118 TemplateInstance ti; 1158 TemplateInstance ti;
1119 FuncDeclaration fd; 1159 FuncDeclaration fd;
1120 1160
1121 static if (false) { 1161 static if (false) {
1122 printf("TemplateDeclaration.deduceFunctionTemplate() %s\n", toChars()); 1162 printf("TemplateDeclaration.deduceFunctionTemplate() %s\n", toChars());
1123 printf(" targsi:\n"); 1163 printf(" targsi:\n");
1124 if (targsi) 1164 if (targsi)
1125 { 1165 {
1126 for (int i = 0; i < targsi.dim; i++) 1166 for (int i = 0; i < targsi.dim; i++)
1127 { 1167 {
1128 Object arg = cast(Object)targsi.data[i]; 1168 Object arg = cast(Object)targsi.data[i];
1129 printf("\t%s\n", arg.toChars()); 1169 printf("\t%s\n", arg.toChars());
1130 } 1170 }
1131 } 1171 }
1132 printf(" fargs:\n"); 1172 printf(" fargs:\n");
1133 for (int i = 0; i < fargs.dim; i++) 1173 for (int i = 0; i < fargs.dim; i++)
1134 { 1174 {
1135 Expression arg = cast(Expression)fargs.data[i]; 1175 Expression arg = cast(Expression)fargs.data[i];
1136 printf("\t%s %s\n", arg.type.toChars(), arg.toChars()); 1176 printf("\t%s %s\n", arg.type.toChars(), arg.toChars());
1137 //printf("\tty = %d\n", arg.type.ty); 1177 //printf("\tty = %d\n", arg.type.ty);
1246 } 1286 }
1247 1287
1248 /************************************************** 1288 /**************************************************
1249 * Declare template parameter tp with value o, and install it in the scope sc. 1289 * Declare template parameter tp with value o, and install it in the scope sc.
1250 */ 1290 */
1251 void declareParameter(Scope sc, TemplateParameter tp, Object o) 1291 void declareParameter(Scope sc, TemplateParameter tp, Object o)
1252 { 1292 {
1253 //printf("TemplateDeclaration.declareParameter('%s', o = %p)\n", tp.ident.toChars(), o); 1293 //printf("TemplateDeclaration.declareParameter('%s', o = %p)\n", tp.ident.toChars(), o);
1254 1294
1255 Type targ = isType(o); 1295 Type targ = isType(o);
1256 Expression ea = isExpression(o); 1296 Expression ea = isExpression(o);
1314 error("declaration %s is already defined", tp.ident.toChars()); 1354 error("declaration %s is already defined", tp.ident.toChars());
1315 1355
1316 s.semantic(sc); 1356 s.semantic(sc);
1317 } 1357 }
1318 1358
1319 override TemplateDeclaration isTemplateDeclaration() { return this; } 1359 override TemplateDeclaration isTemplateDeclaration() { return this; }
1320 1360
1321 TemplateTupleParameter isVariadic() 1361 TemplateTupleParameter isVariadic()
1322 { 1362 {
1323 return .isVariadic(parameters); 1363 return .isVariadic(parameters);
1324 } 1364 }
1325 1365
1326 /*********************************** 1366 /***********************************
1327 * We can overload templates. 1367 * We can overload templates.
1328 */ 1368 */
1329 override bool isOverloadable() 1369 override bool isOverloadable()
1330 { 1370 {
1331 return 1; 1371 return true;
1332 } 1372 }
1333 } 1373 }