annotate dmd/TemplateTypeParameter.d @ 94:3a0b150c9841

Objects -> Vector!Object iteration 1
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Mon, 30 Aug 2010 23:00:34 +0100
parents 2e2a5c3f943a
children 3482c73a991b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.TemplateTypeParameter;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.TemplateParameter;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Declaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.TypeIdentifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.AliasDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.MATCH;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 class TemplateTypeParameter : TemplateParameter
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 /* Syntax:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 * ident : specType = defaultType
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 Type specType; // type parameter: if !=null, this is the type specialization
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 Type defaultType;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 this(Loc loc, Identifier ident, Type specType, Type defaultType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 super(loc, ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 this.ident = ident;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 this.specType = specType;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 this.defaultType = defaultType;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
34 override TemplateTypeParameter isTemplateTypeParameter()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
39 override TemplateParameter syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 {
51
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
41 TemplateTypeParameter tp = new TemplateTypeParameter(loc, ident, specType, defaultType);
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
42 if (tp.specType)
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
43 tp.specType = specType.syntaxCopy();
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
44 if (defaultType)
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
45 tp.defaultType = defaultType.syntaxCopy();
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
46 return tp;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
49 override void declareParameter(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 //printf("TemplateTypeParameter.declareParameter('%s')\n", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 TypeIdentifier ti = new TypeIdentifier(loc, ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 sparam = new AliasDeclaration(loc, ident, ti);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 if (!sc.insert(sparam))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 error(loc, "parameter '%s' multiply defined", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
58 override void semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 //printf("TemplateTypeParameter.semantic('%s')\n", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 if (specType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 specType = specType.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 static if (false) { // Don't do semantic() until instantiation
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 if (defaultType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 defaultType = defaultType.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
73 override void print(Object oarg, Object oded)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
78 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 buf.writestring(ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 if (specType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 buf.writestring(" : ");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 specType.toCBuffer(buf, null, hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 if (defaultType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 buf.writestring(" = ");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 defaultType.toCBuffer(buf, null, hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
93 override Object specialization()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 return specType;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
98 override Object defaultArg(Loc loc, Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 t = defaultType;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 if (t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 t = t.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 t = t.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 return t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
111 override bool overloadMatch(TemplateParameter)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 /*******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 * Match to a particular TemplateParameter.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 * Input:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 * i i'th argument
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 * tiargs[] actual arguments to template instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 * parameters[] template parameters
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 * dedtypes[] deduced arguments to template instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 * *psparam set to symbol declared and initialized to dedtypes[i]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 * flags 1: don't do 'toHeadMutable()'
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 */
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
126 override MATCH matchArg(Scope sc, Objects tiargs, int i, TemplateParameters parameters, Objects dedtypes, Declaration* psparam, int flags)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 //printf("TemplateTypeParameter.matchArg()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 Object oarg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 MATCH m = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 Type ta;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 if (i < tiargs.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 oarg = cast(Object)tiargs.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 // Get default argument instead
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 oarg = defaultArg(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 if (!oarg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 assert(i < dedtypes.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 // It might have already been deduced
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 oarg = cast(Object)dedtypes.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 if (!oarg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 flags |= 1; // already deduced, so don't to toHeadMutable()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 ta = isType(oarg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 if (!ta)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 //printf("%s %p %p %p\n", oarg.toChars(), isExpression(oarg), isDsymbol(oarg), isTuple(oarg));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 //printf("ta is %s\n", ta.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 t = cast(Type)dedtypes.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 if (specType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 //printf("\tcalling deduceType(): ta is %s, specType is %s\n", ta.toChars(), specType.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 MATCH m2 = ta.deduceType(sc, specType, parameters, dedtypes);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 if (m2 == MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 //printf("\tfailed deduceType\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 if (m2 < m)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 m = m2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 t = cast(Type)dedtypes.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 // So that matches with specializations are better
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 m = MATCHconvert;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 /* This is so that:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 * template Foo(T), Foo!(const int), => ta == int
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 // if (!(flags & 1))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 // ta = ta.toHeadMutable();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 if (t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 { // Must match already deduced type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 m = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 if (!t.equals(ta))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 //printf("t = %s ta = %s\n", t.toChars(), ta.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 if (!t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 {
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
202 dedtypes[i] = ta;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 t = ta;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 *psparam = new AliasDeclaration(loc, ident, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 //printf("\tm = %d\n", m);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 return m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 Lnomatch:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 *psparam = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 //printf("\tm = %d\n", MATCHnomatch);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 return MATCHnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
216 override Object dummyArg()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 {
51
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
218 Type t;
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
219
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
220 if (specType)
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
221 t = specType;
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
222 else
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
223 {
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
224 // Use this for alias-parameter's too (?)
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
225 t = new TypeIdentifier(loc, ident);
b7d29f613539 StaticAssertStatement.syntaxCopy
korDen
parents: 0
diff changeset
226 }
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
227 return t;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 51
diff changeset
229 }