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