0
|
1 module dmd.StringExp;
|
|
2
|
|
3 import dmd.Expression;
|
|
4 import dmd.backend.elem;
|
|
5 import dmd.InterState;
|
|
6 import dmd.TypeSArray;
|
|
7 import dmd.CastExp;
|
|
8 import dmd.MATCH;
|
|
9 import dmd.TY;
|
|
10 import dmd.TypeDArray;
|
|
11 import dmd.Type;
|
|
12 import dmd.TOK;
|
|
13 import dmd.OutBuffer;
|
|
14 import dmd.Loc;
|
|
15 import dmd.Scope;
|
|
16 import dmd.IRState;
|
|
17 import dmd.StringExp;
|
|
18 import dmd.HdrGenState;
|
|
19 import dmd.Utf;
|
|
20 import dmd.backend.dt_t;
|
|
21 import dmd.backend.Symbol;
|
|
22 import dmd.backend.StringTab;
|
|
23 import dmd.backend.Util;
|
|
24 import dmd.backend.SC;
|
|
25 import dmd.backend.TYM;
|
|
26 import dmd.backend.FL;
|
|
27 import dmd.backend.TYPE;
|
|
28 import dmd.backend.OPER;
|
|
29
|
4
|
30 import core.memory;
|
2
|
31
|
0
|
32 import core.stdc.string;
|
9
|
33 import core.stdc.ctype;
|
0
|
34
|
|
35 class StringExp : Expression
|
|
36 {
|
|
37 void* string_; // char, wchar, or dchar data
|
|
38 size_t len; // number of chars, wchars, or dchars
|
|
39 ubyte sz; // 1: char, 2: wchar, 4: dchar
|
|
40 ubyte committed; // !=0 if type is committed
|
|
41 ubyte postfix; // 'c', 'w', 'd'
|
|
42
|
|
43 this(Loc loc, string s)
|
|
44 {
|
|
45 this(loc, s, 0);
|
|
46 }
|
|
47
|
|
48 this(Loc loc, string s, ubyte postfix)
|
|
49 {
|
|
50 super(loc, TOK.TOKstring, StringExp.sizeof);
|
|
51
|
|
52 this.string_ = cast(void*)s.ptr;
|
|
53 this.len = s.length;
|
|
54 this.sz = 1;
|
|
55 this.committed = 0;
|
|
56 this.postfix = postfix;
|
|
57 }
|
|
58
|
|
59 int equals(Object o)
|
|
60 {
|
|
61 assert(false);
|
|
62 }
|
|
63
|
9
|
64 string toChars()
|
|
65 {
|
|
66 scope OutBuffer buf = new OutBuffer();
|
|
67 HdrGenState hgs;
|
|
68 char *p;
|
|
69
|
|
70 memset(&hgs, 0, hgs.sizeof);
|
|
71 toCBuffer(buf, &hgs);
|
|
72 buf.writeByte(0);
|
|
73 return buf.extractString();
|
0
|
74 }
|
|
75
|
|
76 Expression semantic(Scope sc)
|
|
77 {
|
|
78 version (LOGSEMANTIC) {
|
|
79 printf("StringExp.semantic() %s\n", toChars());
|
|
80 }
|
|
81 if (!type)
|
|
82 {
|
|
83 scope OutBuffer buffer = new OutBuffer();
|
|
84 size_t newlen = 0;
|
|
85 string p;
|
|
86 size_t u;
|
|
87 dchar c;
|
|
88
|
|
89 switch (postfix)
|
|
90 {
|
|
91 case 'd':
|
|
92 for (u = 0; u < len;)
|
|
93 {
|
|
94 p = utf_decodeChar(cast(string)string_[0..len], &u, &c);
|
|
95 if (p !is null)
|
|
96 {
|
|
97 error("%s", p);
|
|
98 break;
|
|
99 }
|
|
100 else
|
|
101 {
|
|
102 buffer.write4(c);
|
|
103 newlen++;
|
|
104 }
|
|
105 }
|
|
106 buffer.write4(0);
|
|
107 string_ = buffer.extractData();
|
|
108 len = newlen;
|
|
109 sz = 4;
|
|
110 //type = new TypeSArray(Type.tdchar, new IntegerExp(loc, len, Type.tindex));
|
|
111 type = new TypeDArray(Type.tdchar.invariantOf());
|
|
112 committed = 1;
|
|
113 break;
|
|
114
|
|
115 case 'w':
|
|
116 for (u = 0; u < len;)
|
|
117 {
|
|
118 p = utf_decodeChar(cast(string)string_[0..len], &u, &c);
|
|
119 if (p !is null)
|
|
120 {
|
|
121 error("%s", p);
|
|
122 break;
|
|
123 }
|
|
124 else
|
|
125 {
|
|
126 buffer.writeUTF16(c);
|
|
127 newlen++;
|
|
128 if (c >= 0x10000)
|
|
129 newlen++;
|
|
130 }
|
|
131 }
|
|
132 buffer.writeUTF16(0);
|
|
133 string_ = buffer.extractData();
|
|
134 len = newlen;
|
|
135 sz = 2;
|
|
136 //type = new TypeSArray(Type.twchar, new IntegerExp(loc, len, Type.tindex));
|
|
137 type = new TypeDArray(Type.twchar.invariantOf());
|
|
138 committed = 1;
|
|
139 break;
|
|
140
|
|
141 case 'c':
|
|
142 committed = 1;
|
|
143 default:
|
|
144 //type = new TypeSArray(Type.tchar, new IntegerExp(loc, len, Type.tindex));
|
|
145 type = new TypeDArray(Type.tchar.invariantOf());
|
|
146 break;
|
|
147 }
|
|
148 type = type.semantic(loc, sc);
|
|
149 //type = type.invariantOf();
|
|
150 //printf("type = %s\n", type.toChars());
|
|
151 }
|
|
152 return this;
|
|
153 }
|
|
154
|
|
155 Expression interpret(InterState* istate)
|
|
156 {
|
|
157 assert(false);
|
|
158 }
|
|
159
|
|
160 size_t length()
|
|
161 {
|
|
162 assert(false);
|
|
163 }
|
|
164
|
|
165 StringExp toUTF8(Scope sc)
|
|
166 {
|
|
167 assert(false);
|
|
168 }
|
|
169
|
|
170 Expression implicitCastTo(Scope sc, Type t)
|
|
171 {
|
|
172 //printf("StringExp.implicitCastTo(%s of type %s) => %s\n", toChars(), type.toChars(), t.toChars());
|
|
173 ubyte committed = this.committed;
|
|
174 Expression e = Expression.implicitCastTo(sc, t);
|
|
175 if (e.op == TOK.TOKstring)
|
|
176 {
|
|
177 // Retain polysemous nature if it started out that way
|
|
178 (cast(StringExp)e).committed = committed;
|
|
179 }
|
|
180 return e;
|
|
181 }
|
|
182
|
|
183 MATCH implicitConvTo(Type t)
|
|
184 {
|
|
185 MATCH m;
|
|
186
|
|
187 static if (false) {
|
|
188 printf("StringExp.implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
|
|
189 toChars(), committed, type.toChars(), t.toChars());
|
|
190 }
|
|
191 if (!committed)
|
|
192 {
|
|
193 if (!committed && t.ty == TY.Tpointer && t.nextOf().ty == TY.Tvoid)
|
|
194 {
|
|
195 return MATCH.MATCHnomatch;
|
|
196 }
|
|
197 if (type.ty == TY.Tsarray || type.ty == TY.Tarray || type.ty == TY.Tpointer)
|
|
198 {
|
|
199 TY tyn = type.nextOf().ty;
|
|
200 if (tyn == TY.Tchar || tyn == TY.Twchar || tyn == TY.Tdchar)
|
|
201 {
|
|
202 Type tn;
|
|
203 MATCH mm;
|
|
204
|
|
205 switch (t.ty)
|
|
206 {
|
|
207 case TY.Tsarray:
|
|
208 if (type.ty == TY.Tsarray)
|
|
209 {
|
|
210 if ((cast(TypeSArray)type).dim.toInteger() !=
|
|
211 (cast(TypeSArray)t).dim.toInteger())
|
|
212 return MATCH.MATCHnomatch;
|
|
213 TY tynto = t.nextOf().ty;
|
|
214 if (tynto == TY.Tchar || tynto == TY.Twchar || tynto == TY.Tdchar)
|
|
215 return MATCH.MATCHexact;
|
|
216 }
|
|
217 else if (type.ty == TY.Tarray)
|
|
218 {
|
|
219 if (length() > (cast(TypeSArray)t).dim.toInteger())
|
|
220 return MATCH.MATCHnomatch;
|
|
221 TY tynto = t.nextOf().ty;
|
|
222 if (tynto == TY.Tchar || tynto == TY.Twchar || tynto == TY.Tdchar)
|
|
223 return MATCH.MATCHexact;
|
|
224 }
|
|
225 case TY.Tarray:
|
|
226 case TY.Tpointer:
|
|
227 tn = t.nextOf();
|
|
228 mm = MATCH.MATCHexact;
|
|
229 if (type.nextOf().mod != tn.mod)
|
|
230 {
|
|
231 if (!tn.isConst())
|
|
232 return MATCH.MATCHnomatch;
|
|
233 mm = MATCH.MATCHconst;
|
|
234 }
|
|
235 switch (tn.ty)
|
|
236 {
|
|
237 case TY.Tchar:
|
|
238 case TY.Twchar:
|
|
239 case TY.Tdchar:
|
|
240 return mm;
|
|
241 }
|
|
242 break;
|
|
243 default:
|
|
244 break; ///
|
|
245 }
|
|
246 }
|
|
247 }
|
|
248 }
|
|
249 return Expression.implicitConvTo(t);
|
|
250 static if (false) {
|
|
251 m = cast(MATCH)type.implicitConvTo(t);
|
|
252 if (m)
|
|
253 {
|
|
254 return m;
|
|
255 }
|
|
256
|
|
257 return MATCH.MATCHnomatch;
|
|
258 }
|
|
259 }
|
|
260
|
|
261 static uint X(TY tf, TY tt) {
|
|
262 return ((tf) * 256 + (tt));
|
|
263 }
|
|
264
|
|
265 Expression castTo(Scope sc, Type t)
|
|
266 {
|
|
267 /* This follows copy-on-write; any changes to 'this'
|
|
268 * will result in a copy.
|
|
269 * The this.string member is considered immutable.
|
|
270 */
|
|
271 StringExp se;
|
|
272 Type tb;
|
|
273 int copied = 0;
|
|
274
|
|
275 //printf("StringExp.castTo(t = %s), '%s' committed = %d\n", t.toChars(), toChars(), committed);
|
|
276
|
|
277 if (!committed && t.ty == TY.Tpointer && t.nextOf().ty == TY.Tvoid)
|
|
278 {
|
|
279 error("cannot convert string literal to void*");
|
|
280 }
|
|
281
|
|
282 se = this;
|
|
283 if (!committed)
|
|
284 {
|
|
285 se = cast(StringExp)copy();
|
|
286 se.committed = 1;
|
|
287 copied = 1;
|
|
288 }
|
|
289
|
|
290 if (type == t)
|
|
291 {
|
|
292 return se;
|
|
293 }
|
|
294
|
|
295 tb = t.toBasetype();
|
|
296 //printf("\ttype = %s\n", type.toChars());
|
|
297 if (tb.ty == TY.Tdelegate && type.toBasetype().ty != TY.Tdelegate)
|
|
298 return Expression.castTo(sc, t);
|
|
299
|
|
300 Type typeb = type.toBasetype();
|
|
301 if (typeb == tb)
|
|
302 {
|
|
303 if (!copied)
|
|
304 {
|
|
305 se = cast(StringExp)copy();
|
|
306 copied = 1;
|
|
307 }
|
|
308 se.type = t;
|
|
309 return se;
|
|
310 }
|
|
311
|
|
312 if (committed && tb.ty == TY.Tsarray && typeb.ty == TY.Tarray)
|
|
313 {
|
|
314 se = cast(StringExp)copy();
|
|
315 se.sz = cast(ubyte)tb.nextOf().size();
|
|
316 se.len = (len * sz) / se.sz;
|
|
317 se.committed = 1;
|
|
318 se.type = t;
|
|
319 return se;
|
|
320 }
|
|
321
|
|
322 if (tb.ty != TY.Tsarray && tb.ty != TY.Tarray && tb.ty != TY.Tpointer)
|
|
323 {
|
|
324 if (!copied)
|
|
325 {
|
|
326 se = cast(StringExp)copy();
|
|
327 copied = 1;
|
|
328 }
|
|
329 goto Lcast;
|
|
330 }
|
|
331 if (typeb.ty != TY.Tsarray && typeb.ty != TY.Tarray && typeb.ty != TY.Tpointer)
|
|
332 {
|
|
333 if (!copied)
|
|
334 {
|
|
335 se = cast(StringExp)copy();
|
|
336 copied = 1;
|
|
337 }
|
|
338 goto Lcast;
|
|
339 }
|
|
340
|
|
341 if (typeb.nextOf().size() == tb.nextOf().size())
|
|
342 {
|
|
343 if (!copied)
|
|
344 {
|
|
345 se = cast(StringExp)copy();
|
|
346 copied = 1;
|
|
347 }
|
|
348
|
|
349 if (tb.ty == TY.Tsarray)
|
|
350 goto L2; // handle possible change in static array dimension
|
|
351 se.type = t;
|
|
352 return se;
|
|
353 }
|
|
354
|
|
355 if (committed)
|
|
356 goto Lcast;
|
|
357
|
|
358 {
|
|
359 scope OutBuffer buffer = new OutBuffer();
|
|
360 size_t newlen = 0;
|
|
361 TY tfty = typeb.nextOf().toBasetype().ty;
|
|
362 TY ttty = tb.nextOf().toBasetype().ty;
|
|
363 switch (X(tfty, ttty))
|
|
364 {
|
|
365 case X(TY.Tchar, TY.Tchar):
|
|
366 case X(TY.Twchar,TY.Twchar):
|
|
367 case X(TY.Tdchar,TY.Tdchar):
|
|
368 break;
|
|
369
|
|
370 case X(TY.Tchar, TY.Twchar):
|
|
371 for (size_t u = 0; u < len;)
|
|
372 {
|
|
373 dchar c;
|
|
374 string p = utf_decodeChar(cast(string)se.string_[0..len], &u, &c);
|
|
375 if (p !is null)
|
|
376 error("%s", p);
|
|
377 else
|
|
378 buffer.writeUTF16(c);
|
|
379 }
|
|
380 newlen = buffer.offset / 2;
|
|
381 buffer.writeUTF16(0);
|
|
382 goto L1;
|
|
383
|
|
384 case X(TY.Tchar, TY.Tdchar):
|
|
385 for (size_t u = 0; u < len;)
|
|
386 {
|
|
387 dchar c;
|
|
388 string p = utf_decodeChar(cast(string)se.string_[0..len], &u, &c);
|
|
389 if (p !is null)
|
|
390 error("%s", p);
|
|
391 buffer.write4(c);
|
|
392 newlen++;
|
|
393 }
|
|
394 buffer.write4(0);
|
|
395 goto L1;
|
|
396
|
|
397 case X(TY.Twchar,TY.Tchar):
|
|
398 for (size_t u = 0; u < len;)
|
|
399 {
|
|
400 dchar c;
|
|
401 string p = utf_decodeWchar(cast(wstring)se.string_[0..len], &u, &c);
|
|
402 if (p)
|
|
403 error("%s", p);
|
|
404 else
|
|
405 buffer.writeUTF8(c);
|
|
406 }
|
|
407 newlen = buffer.offset;
|
|
408 buffer.writeUTF8(0);
|
|
409 goto L1;
|
|
410
|
|
411 case X(TY.Twchar,TY.Tdchar):
|
|
412 for (size_t u = 0; u < len;)
|
|
413 {
|
|
414 dchar c;
|
|
415 string p = utf_decodeWchar(cast(wstring)se.string_[0..len], &u, &c);
|
|
416 if (p)
|
|
417 error("%s", p);
|
|
418 buffer.write4(c);
|
|
419 newlen++;
|
|
420 }
|
|
421 buffer.write4(0);
|
|
422 goto L1;
|
|
423
|
|
424 case X(TY.Tdchar,TY.Tchar):
|
|
425 for (size_t u = 0; u < len; u++)
|
|
426 {
|
|
427 dchar c = (cast(dchar*)se.string_)[u];
|
|
428 if (!utf_isValidDchar(c))
|
|
429 error("invalid UCS-32 char \\U%08x", c);
|
|
430 else
|
|
431 buffer.writeUTF8(c);
|
|
432 newlen++;
|
|
433 }
|
|
434 newlen = buffer.offset;
|
|
435 buffer.writeUTF8(0);
|
|
436 goto L1;
|
|
437
|
|
438 case X(TY.Tdchar,TY.Twchar):
|
|
439 for (size_t u = 0; u < len; u++)
|
|
440 {
|
|
441 dchar c = (cast(dchar*)se.string_)[u];
|
|
442 if (!utf_isValidDchar(c))
|
|
443 error("invalid UCS-32 char \\U%08x", c);
|
|
444 else
|
|
445 buffer.writeUTF16(c);
|
|
446 newlen++;
|
|
447 }
|
|
448 newlen = buffer.offset / 2;
|
|
449 buffer.writeUTF16(0);
|
|
450 goto L1;
|
|
451
|
|
452 L1:
|
|
453 if (!copied)
|
|
454 {
|
|
455 se = cast(StringExp)copy();
|
|
456 copied = 1;
|
|
457 }
|
|
458 se.string_ = buffer.extractData();
|
|
459 se.len = newlen;
|
|
460 se.sz = cast(ubyte)tb.nextOf().size();
|
|
461 break;
|
|
462
|
|
463 default:
|
|
464 assert(typeb.nextOf().size() != tb.nextOf().size());
|
|
465 goto Lcast;
|
|
466 }
|
|
467 }
|
|
468 L2:
|
|
469 assert(copied);
|
|
470
|
|
471 // See if need to truncate or extend the literal
|
|
472 if (tb.ty == TY.Tsarray)
|
|
473 {
|
|
474 int dim2 = cast(int)(cast(TypeSArray)tb).dim.toInteger();
|
|
475
|
|
476 //printf("dim from = %d, to = %d\n", se.len, dim2);
|
|
477
|
|
478 // Changing dimensions
|
|
479 if (dim2 != se.len)
|
|
480 {
|
|
481 // Copy when changing the string literal
|
|
482 uint newsz = se.sz;
|
|
483 void *s;
|
|
484 int d;
|
|
485
|
|
486 d = (dim2 < se.len) ? dim2 : se.len;
|
2
|
487 s = cast(ubyte*)GC.malloc((dim2 + 1) * newsz);
|
0
|
488 memcpy(s, se.string_, d * newsz);
|
|
489 // Extend with 0, add terminating 0
|
|
490 memset(cast(char*)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
|
|
491 se.string_ = s;
|
|
492 se.len = dim2;
|
|
493 }
|
|
494 }
|
|
495 se.type = t;
|
|
496 return se;
|
|
497
|
|
498 Lcast:
|
|
499 Expression e = new CastExp(loc, se, t);
|
|
500 e.type = t; // so semantic() won't be run on e
|
|
501 return e;
|
|
502 }
|
|
503
|
|
504 int compare(Object obj)
|
|
505 {
|
|
506 assert(false);
|
|
507 }
|
|
508
|
|
509 bool isBool(bool result)
|
|
510 {
|
|
511 assert(false);
|
|
512 }
|
|
513
|
|
514 uint charAt(size_t i)
|
|
515 {
|
9
|
516 uint value;
|
|
517
|
|
518 switch (sz)
|
|
519 {
|
|
520 case 1:
|
|
521 value = (cast(ubyte *)string_)[i];
|
|
522 break;
|
|
523
|
|
524 case 2:
|
|
525 value = (cast(ushort *)string_)[i];
|
|
526 break;
|
|
527
|
|
528 case 4:
|
|
529 value = (cast(uint *)string_)[i];
|
|
530 break;
|
|
531
|
|
532 default:
|
|
533 assert(0);
|
|
534 break;
|
|
535 }
|
|
536 return value;
|
0
|
537 }
|
|
538
|
|
539 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
|
540 {
|
9
|
541 buf.writeByte('"');
|
|
542 for (size_t i = 0; i < len; i++)
|
|
543 {
|
|
544 uint c = charAt(i);
|
|
545
|
|
546 switch (c)
|
|
547 {
|
|
548 case '"':
|
|
549 case '\\':
|
|
550 if (!hgs.console)
|
|
551 buf.writeByte('\\');
|
|
552 default:
|
|
553 if (c <= 0xFF)
|
|
554 {
|
|
555 if (c <= 0x7F && (isprint(c) || hgs.console))
|
|
556 buf.writeByte(c);
|
|
557 else
|
|
558 buf.printf("\\x%02x", c);
|
|
559 }
|
|
560 else if (c <= 0xFFFF)
|
|
561 buf.printf("\\x%02x\\x%02x", c & 0xFF, c >> 8);
|
|
562 else
|
|
563 buf.printf("\\x%02x\\x%02x\\x%02x\\x%02x", c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24);
|
|
564 break;
|
|
565 }
|
|
566 }
|
|
567 buf.writeByte('"');
|
|
568 if (postfix)
|
|
569 buf.writeByte(postfix);
|
0
|
570 }
|
|
571
|
|
572 void toMangleBuffer(OutBuffer buf)
|
|
573 {
|
|
574 assert(false);
|
|
575 }
|
|
576
|
|
577 elem* toElem(IRState* irs)
|
|
578 {
|
|
579 elem* e;
|
|
580 Type tb = type.toBasetype();
|
|
581
|
|
582 static if (false) {
|
|
583 printf("StringExp.toElem() %s, type = %s\n", toChars(), type.toChars());
|
|
584 }
|
|
585
|
|
586 if (tb.ty == TY.Tarray)
|
|
587 {
|
|
588 Symbol* si;
|
|
589 dt_t* dt;
|
|
590 StringTab* st;
|
|
591
|
|
592 static if (false) {
|
|
593 printf("irs.m = %p\n", irs.m);
|
|
594 printf(" m = %s\n", irs.m.toChars());
|
|
595 printf(" len = %d\n", len);
|
|
596 printf(" sz = %d\n", sz);
|
|
597 }
|
|
598 for (size_t i = 0; i < STSIZE; i++)
|
|
599 {
|
|
600 st = &stringTab[(stidx + i) % STSIZE];
|
|
601 //if (!st.m) continue;
|
|
602 //printf(" st.m = %s\n", st.m.toChars());
|
|
603 //printf(" st.len = %d\n", st.len);
|
|
604 //printf(" st.sz = %d\n", st.sz);
|
|
605 if (st.m is irs.m &&
|
|
606 st.si &&
|
|
607 st.len == len &&
|
|
608 st.sz == sz &&
|
|
609 memcmp(st.string_, string_, sz * len) == 0)
|
|
610 {
|
|
611 //printf("use cached value\n");
|
|
612 si = st.si; // use cached value
|
|
613 goto L1;
|
|
614 }
|
|
615 }
|
|
616
|
|
617 stidx = (stidx + 1) % STSIZE;
|
|
618 st = &stringTab[stidx];
|
|
619
|
|
620 dt = null;
|
|
621 toDt(&dt);
|
|
622
|
|
623 si = symbol_generate(SC.SCstatic, type_fake(TYM.TYdarray));
|
|
624 si.Sdt = dt;
|
|
625 si.Sfl = FL.FLdata;
|
|
626 version (ELFOBJ) {// Burton
|
|
627 si.Sseg = Segment.CDATA;
|
|
628 }
|
|
629 version (MACHOBJ) {
|
|
630 si.Sseg = Segment.DATA;
|
|
631 }
|
|
632 outdata(si);
|
|
633
|
|
634 st.m = irs.m;
|
|
635 st.si = si;
|
|
636 st.string_ = string_;
|
|
637 st.len = len;
|
|
638 st.sz = sz;
|
|
639 L1:
|
|
640 e = el_var(si);
|
|
641 }
|
|
642 else if (tb.ty == TY.Tsarray)
|
|
643 {
|
|
644 Symbol *si;
|
|
645 dt_t *dt = null;
|
|
646
|
|
647 toDt(&dt);
|
|
648 dtnzeros(&dt, sz); // leave terminating 0
|
|
649
|
|
650 si = symbol_generate(SC.SCstatic,type_allocn(TYM.TYarray, tschar));
|
|
651 si.Sdt = dt;
|
|
652 si.Sfl = FL.FLdata;
|
|
653
|
|
654 version (ELFOBJ_OR_MACHOBJ) { // Burton
|
|
655 si.Sseg = Segment.CDATA;
|
|
656 }
|
|
657 outdata(si);
|
|
658
|
|
659 e = el_var(si);
|
|
660 }
|
|
661 else if (tb.ty == TY.Tpointer)
|
|
662 {
|
|
663 e = el_calloc();
|
|
664 e.Eoper = OPER.OPstring;
|
|
665 static if (true) {
|
|
666 // Match MEM_PH_FREE for OPstring in ztc\el.c
|
2
|
667 e.EV.ss.Vstring = cast(char*)GC.malloc((len + 1) * sz);
|
0
|
668 memcpy(e.EV.ss.Vstring, string_, (len + 1) * sz);
|
|
669 } else {
|
|
670 e.EV.ss.Vstring = cast(char*)string_;
|
|
671 }
|
|
672 e.EV.ss.Vstrlen = (len + 1) * sz;
|
|
673 e.Ety = TYM.TYnptr;
|
|
674 }
|
|
675 else
|
|
676 {
|
|
677 writef("type is %s\n", type.toChars());
|
|
678 assert(0);
|
|
679 }
|
|
680 el_setLoc(e,loc);
|
|
681 return e;
|
|
682 }
|
|
683
|
|
684 dt_t** toDt(dt_t** pdt)
|
|
685 {
|
|
686 //printf("StringExp.toDt() '%s', type = %s\n", toChars(), type.toChars());
|
|
687 Type t = type.toBasetype();
|
|
688
|
|
689 // BUG: should implement some form of static string pooling
|
|
690 switch (t.ty)
|
|
691 {
|
|
692 case TY.Tarray:
|
|
693 dtdword(pdt, len);
|
|
694 pdt = dtabytes(pdt, TYM.TYnptr, 0, (len + 1) * sz, cast(char*)string_);
|
|
695 break;
|
|
696
|
|
697 case TY.Tsarray:
|
|
698 {
|
|
699 TypeSArray tsa = cast(TypeSArray)type;
|
|
700 long dim;
|
|
701
|
|
702 pdt = dtnbytes(pdt, len * sz, cast(const(char)*)string_);
|
|
703 if (tsa.dim)
|
|
704 {
|
|
705 dim = tsa.dim.toInteger();
|
|
706 if (len < dim)
|
|
707 {
|
|
708 // Pad remainder with 0
|
|
709 pdt = dtnzeros(pdt, cast(uint)((dim - len) * tsa.next.size()));
|
|
710 }
|
|
711 }
|
|
712 break;
|
|
713 }
|
|
714
|
|
715 case TY.Tpointer:
|
|
716 pdt = dtabytes(pdt, TYM.TYnptr, 0, (len + 1) * sz, cast(char*)string_);
|
|
717 break;
|
|
718
|
|
719 default:
|
|
720 writef("StringExp.toDt(type = %s)\n", type.toChars());
|
|
721 assert(0);
|
|
722 }
|
|
723
|
|
724 return pdt;
|
|
725 }
|
|
726 }
|
|
727
|