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