annotate dmd/StringExp.d @ 4:d706d958e4e8

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