annotate dmd/TemplateTypeParameter.d @ 132:c494af1dba80

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