Mercurial > projects > ddmd
annotate dmd/TypeSlice.d @ 113:3482c73a991b
More cleanup for arrays
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Tue, 31 Aug 2010 23:57:32 +0100 |
parents | 2e2a5c3f943a |
children | e28b18c23469 |
rev | line source |
---|---|
0 | 1 module dmd.TypeSlice; |
2 | |
3 import dmd.Type; | |
4 import dmd.TypeNext; | |
5 import dmd.MOD; | |
6 import dmd.Expression; | |
7 import dmd.Loc; | |
8 import dmd.Scope; | |
9 import dmd.Dsymbol; | |
10 import dmd.OutBuffer; | |
11 import dmd.HdrGenState; | |
12 import dmd.TY; | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
13 import dmd.TypeTuple; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
14 import dmd.WANT; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
15 import dmd.ArrayTypes; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
16 import dmd.Argument; |
37 | 17 import dmd.SliceExp; |
18 import dmd.TupleDeclaration; | |
19 import dmd.ScopeDsymbol; | |
20 import dmd.ArrayScopeSymbol; | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
21 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
22 import dmd.type.Util; |
0 | 23 |
24 class TypeSlice : TypeNext | |
25 { | |
26 Expression lwr; | |
27 Expression upr; | |
28 | |
29 this(Type next, Expression lwr, Expression upr) | |
30 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
31 super(TY.Tslice, next); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
32 //printf("TypeSlice[%s .. %s]\n", lwr.toChars(), upr.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
33 this.lwr = lwr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
34 this.upr = upr; |
0 | 35 } |
36 | |
37 version (DumbClone) { | |
38 } else { | |
39 Type clone() | |
40 { | |
41 assert(false); | |
42 } | |
43 } | |
44 | |
72 | 45 override Type syntaxCopy() |
0 | 46 { |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
47 Type t = new TypeSlice(next.syntaxCopy(), lwr.syntaxCopy(), upr.syntaxCopy()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
48 t.mod = mod; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
49 return t; |
0 | 50 } |
51 | |
72 | 52 override Type semantic(Loc loc, Scope sc) |
0 | 53 { |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
54 //printf("TypeSlice.semantic() %s\n", toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
55 next = next.semantic(loc, sc); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
56 transitive(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
57 //printf("next: %s\n", next.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
58 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
59 Type tbn = next.toBasetype(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
60 if (tbn.ty != Ttuple) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
61 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
62 error(loc, "can only slice tuple types, not %s", tbn.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
63 return Type.terror; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
64 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
65 TypeTuple tt = cast(TypeTuple)tbn; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
66 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
67 lwr = semanticLength(sc, tbn, lwr); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
68 lwr = lwr.optimize(WANTvalue); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
69 ulong i1 = lwr.toUInteger(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
70 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
71 upr = semanticLength(sc, tbn, upr); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
72 upr = upr.optimize(WANTvalue); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
73 ulong i2 = upr.toUInteger(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
74 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
75 if (!(i1 <= i2 && i2 <= tt.arguments.dim)) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
76 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
77 error(loc, "slice [%ju..%ju] is out of range of [0..%u]", i1, i2, tt.arguments.dim); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
78 return Type.terror; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
79 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
80 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
81 Arguments args = new Arguments; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
82 args.reserve(cast(size_t)(i2 - i1)); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
83 for (size_t i = cast(size_t)i1; i < cast(size_t)i2; i++) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
84 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
85 Argument arg = cast(Argument)tt.arguments.data[i]; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
86 args.push(cast(void*)arg); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
87 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
88 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
89 return new TypeTuple(args); |
0 | 90 } |
91 | |
72 | 92 override void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps) |
0 | 93 { |
37 | 94 next.resolve(loc, sc, pe, pt, ps); |
95 if (*pe) | |
96 { // It's really a slice expression | |
97 Expression e; | |
98 e = new SliceExp(loc, *pe, lwr, upr); | |
99 *pe = e; | |
100 } | |
101 else if (*ps) | |
102 { | |
103 Dsymbol s = *ps; | |
104 TupleDeclaration td = s.isTupleDeclaration(); | |
105 if (td) | |
106 { | |
107 /* It's a slice of a TupleDeclaration | |
108 */ | |
109 ScopeDsymbol sym = new ArrayScopeSymbol(sc, td); | |
110 sym.parent = sc.scopesym; | |
111 sc = sc.push(sym); | |
112 | |
113 lwr = lwr.semantic(sc); | |
114 lwr = lwr.optimize(WANTvalue); | |
115 ulong i1 = lwr.toUInteger(); | |
116 | |
117 upr = upr.semantic(sc); | |
118 upr = upr.optimize(WANTvalue); | |
119 ulong i2 = upr.toUInteger(); | |
120 | |
121 sc = sc.pop(); | |
122 | |
123 if (!(i1 <= i2 && i2 <= td.objects.dim)) | |
124 { | |
125 error(loc, "slice [%ju..%ju] is out of range of [0..%u]", i1, i2, td.objects.dim); | |
126 goto Ldefault; | |
127 } | |
128 | |
129 if (i1 == 0 && i2 == td.objects.dim) | |
130 { | |
131 *ps = td; | |
132 return; | |
133 } | |
134 | |
135 /* Create a new TupleDeclaration which | |
136 * is a slice [i1..i2] out of the old one. | |
137 */ | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
138 auto objects = new Objects; |
37 | 139 objects.setDim(cast(uint)(i2 - i1)); |
140 for (size_t i = 0; i < objects.dim; i++) | |
141 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
142 objects[i] = td.objects[cast(size_t)i1 + i]; |
37 | 143 } |
144 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
145 auto tds = new TupleDeclaration(loc, td.ident, objects); |
37 | 146 *ps = tds; |
147 } | |
148 else | |
149 goto Ldefault; | |
150 } | |
151 else | |
152 { | |
153 Ldefault: | |
154 Type.resolve(loc, sc, pe, pt, ps); | |
155 } | |
0 | 156 } |
157 | |
72 | 158 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 159 { |
160 assert(false); | |
161 } | |
72 | 162 } |