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