annotate dmd/StringExp.d @ 0:10317f0c89a5

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