Mercurial > projects > ddmd
annotate dmd/TypeSlice.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | e3afd1303184 |
children |
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; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
17 import dmd.Parameter; |
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 |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
25 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
26 |
0 | 27 class TypeSlice : TypeNext |
28 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
29 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
30 |
0 | 31 Expression lwr; |
32 Expression upr; | |
33 | |
34 this(Type next, Expression lwr, Expression upr) | |
35 { | |
178 | 36 register(); |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
37 super(TY.Tslice, next); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
38 //printf("TypeSlice[%s .. %s]\n", lwr.toChars(), upr.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
39 this.lwr = lwr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
40 this.upr = upr; |
0 | 41 } |
42 | |
72 | 43 override Type syntaxCopy() |
0 | 44 { |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
45 Type t = new TypeSlice(next.syntaxCopy(), lwr.syntaxCopy(), upr.syntaxCopy()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
46 t.mod = mod; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
47 return t; |
0 | 48 } |
49 | |
72 | 50 override Type semantic(Loc loc, Scope sc) |
0 | 51 { |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
52 //printf("TypeSlice.semantic() %s\n", toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
53 next = next.semantic(loc, sc); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
54 transitive(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
55 //printf("next: %s\n", next.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
56 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
57 Type tbn = next.toBasetype(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
58 if (tbn.ty != Ttuple) |
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 error(loc, "can only slice tuple types, not %s", tbn.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
61 return Type.terror; |
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 TypeTuple tt = cast(TypeTuple)tbn; |
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 lwr = semanticLength(sc, tbn, lwr); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
66 lwr = lwr.optimize(WANTvalue); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
67 ulong i1 = lwr.toUInteger(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
68 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
69 upr = semanticLength(sc, tbn, upr); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
70 upr = upr.optimize(WANTvalue); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
71 ulong i2 = upr.toUInteger(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
72 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
73 if (!(i1 <= i2 && i2 <= tt.arguments.dim)) |
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 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
|
76 return Type.terror; |
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 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
79 auto args = new Parameters; |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
80 args.reserve(cast(size_t)(i2 - i1)); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
81 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
|
82 { |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
83 auto arg = tt.arguments[i]; |
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
84 args.push(arg); |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
85 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
86 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
0
diff
changeset
|
87 return new TypeTuple(args); |
0 | 88 } |
89 | |
72 | 90 override void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps) |
0 | 91 { |
37 | 92 next.resolve(loc, sc, pe, pt, ps); |
93 if (*pe) | |
94 { // It's really a slice expression | |
95 Expression e; | |
96 e = new SliceExp(loc, *pe, lwr, upr); | |
97 *pe = e; | |
98 } | |
99 else if (*ps) | |
100 { | |
101 Dsymbol s = *ps; | |
102 TupleDeclaration td = s.isTupleDeclaration(); | |
103 if (td) | |
104 { | |
105 /* It's a slice of a TupleDeclaration | |
106 */ | |
107 ScopeDsymbol sym = new ArrayScopeSymbol(sc, td); | |
108 sym.parent = sc.scopesym; | |
109 sc = sc.push(sym); | |
110 | |
111 lwr = lwr.semantic(sc); | |
112 lwr = lwr.optimize(WANTvalue); | |
113 ulong i1 = lwr.toUInteger(); | |
114 | |
115 upr = upr.semantic(sc); | |
116 upr = upr.optimize(WANTvalue); | |
117 ulong i2 = upr.toUInteger(); | |
118 | |
119 sc = sc.pop(); | |
120 | |
121 if (!(i1 <= i2 && i2 <= td.objects.dim)) | |
122 { | |
123 error(loc, "slice [%ju..%ju] is out of range of [0..%u]", i1, i2, td.objects.dim); | |
124 goto Ldefault; | |
125 } | |
126 | |
127 if (i1 == 0 && i2 == td.objects.dim) | |
128 { | |
129 *ps = td; | |
130 return; | |
131 } | |
132 | |
133 /* Create a new TupleDeclaration which | |
134 * is a slice [i1..i2] out of the old one. | |
135 */ | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
136 auto objects = new Objects; |
37 | 137 objects.setDim(cast(uint)(i2 - i1)); |
138 for (size_t i = 0; i < objects.dim; i++) | |
139 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
140 objects[i] = td.objects[cast(size_t)i1 + i]; |
37 | 141 } |
142 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
143 auto tds = new TupleDeclaration(loc, td.ident, objects); |
37 | 144 *ps = tds; |
145 } | |
146 else | |
147 goto Ldefault; | |
148 } | |
149 else | |
150 { | |
151 Ldefault: | |
152 Type.resolve(loc, sc, pe, pt, ps); | |
153 } | |
0 | 154 } |
155 | |
72 | 156 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 157 { |
158 assert(false); | |
159 } | |
72 | 160 } |