annotate dmd/expression/Slice.d @ 135:af1bebfd96a4 dmd2037

dmd 2.038
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Mon, 13 Sep 2010 22:19:42 +0100
parents e28b18c23469
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.expression.Slice;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 90
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.GlobalExpressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.ArrayLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.ArrayTypes;
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 4
diff changeset
12 import dmd.Util : printf;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13
4
d706d958e4e8 Step 2 of restoring GC functionality.
korDen
parents: 2
diff changeset
14 import core.memory;
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
15
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import core.stdc.stdlib;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import core.stdc.string;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18
34
544b922227c7 update to work with dmd 2.048
korDen
parents: 16
diff changeset
19 import std.exception;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 4
diff changeset
21
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 /* Also return EXP_CANT_INTERPRET if this fails
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 Expression Slice(Type type, Expression e1, Expression lwr, Expression upr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 Expression e = EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 printf("Slice()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 if (lwr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 printf("\te1 = %s\n", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 printf("\tlwr = %s\n", lwr.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 printf("\tupr = %s\n", upr.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 if (e1.op == TOKstring && lwr.op == TOKint64 && upr.op == TOKint64)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 34
diff changeset
40 auto es1 = cast(StringExp)e1;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 ulong ilwr = lwr.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 ulong iupr = upr.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 if (iupr > es1.len || ilwr > iupr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 e1.error("string slice [%ju .. %ju] is out of bounds", ilwr, iupr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 size_t len = cast(size_t)(iupr - ilwr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 int sz = es1.sz;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50
2
7427ded8caf7 Removed unreferenced modules
korDen
parents: 0
diff changeset
51 char* s = cast(char*)GC.malloc((len + 1) * sz);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 memcpy(s, cast(ubyte*)es1.string_ + ilwr * sz, len * sz);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 memset(s + len * sz, 0, sz);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 34
diff changeset
55 auto es = new StringExp(loc, assumeUnique(s[0..len]), es1.postfix);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 es.sz = cast(ubyte)sz;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 es.committed = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 es.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 e = es;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 else if (e1.op == TOKarrayliteral &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 lwr.op == TOKint64 && upr.op == TOKint64 &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 !e1.checkSideEffect(2))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 34
diff changeset
66 auto es1 = cast(ArrayLiteralExp)e1;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 ulong ilwr = lwr.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 ulong iupr = upr.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 if (iupr > es1.elements.dim || ilwr > iupr)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 e1.error("array slice [%ju .. %ju] is out of bounds", ilwr, iupr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 34
diff changeset
74 auto elements = new Expressions();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 elements.setDim(cast(uint)(iupr - ilwr));
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
76 memcpy(elements.ptr,
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
77 es1.elements.ptr + ilwr,
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
78 cast(uint)(iupr - ilwr) * (*es1.elements.ptr).sizeof);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 e = new ArrayLiteralExp(e1.loc, elements);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 return e;
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 4
diff changeset
84 }