annotate dmd/TemplateTypeParameter.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children b7d29f613539
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 TemplateTypeParameter isTemplateTypeParameter()
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 TemplateParameter syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 void declareParameter(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 //printf("TemplateTypeParameter.declareParameter('%s')\n", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 TypeIdentifier ti = new TypeIdentifier(loc, ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 sparam = new AliasDeclaration(loc, ident, ti);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 if (!sc.insert(sparam))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 error(loc, "parameter '%s' multiply defined", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 void semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 //printf("TemplateTypeParameter.semantic('%s')\n", ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 if (specType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 specType = specType.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 static if (false) { // Don't do semantic() until instantiation
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 if (defaultType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 defaultType = defaultType.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 void print(Object oarg, Object oded)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 buf.writestring(ident.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 if (specType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 buf.writestring(" : ");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 specType.toCBuffer(buf, null, hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 if (defaultType)
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 defaultType.toCBuffer(buf, null, hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 Object specialization()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 return specType;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 Object defaultArg(Loc loc, Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 t = defaultType;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 if (t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 t = t.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 t = t.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 return t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 bool overloadMatch(TemplateParameter)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 /*******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 * Match to a particular TemplateParameter.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 * Input:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 * i i'th argument
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 * tiargs[] actual arguments to template instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 * parameters[] template parameters
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 * dedtypes[] deduced arguments to template instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 * *psparam set to symbol declared and initialized to dedtypes[i]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 * flags 1: don't do 'toHeadMutable()'
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 MATCH matchArg(Scope sc, Objects tiargs, int i, TemplateParameters parameters, Objects dedtypes, Declaration* psparam, int flags)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 //printf("TemplateTypeParameter.matchArg()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 Object oarg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 MATCH m = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 Type ta;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 if (i < tiargs.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 oarg = cast(Object)tiargs.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 // Get default argument instead
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 oarg = defaultArg(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 if (!oarg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 assert(i < dedtypes.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 // It might have already been deduced
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 oarg = cast(Object)dedtypes.data[i];
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 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 flags |= 1; // already deduced, so don't to toHeadMutable()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 ta = isType(oarg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 if (!ta)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 //printf("%s %p %p %p\n", oarg.toChars(), isExpression(oarg), isDsymbol(oarg), isTuple(oarg));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 //printf("ta is %s\n", ta.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 t = cast(Type)dedtypes.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 if (specType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 //printf("\tcalling deduceType(): ta is %s, specType is %s\n", ta.toChars(), specType.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 MATCH m2 = ta.deduceType(sc, specType, parameters, dedtypes);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 if (m2 == MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 //printf("\tfailed deduceType\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 if (m2 < m)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 m = m2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 t = cast(Type)dedtypes.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 // So that matches with specializations are better
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 m = MATCHconvert;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 /* This is so that:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 * template Foo(T), Foo!(const int), => ta == int
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 // if (!(flags & 1))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 // ta = ta.toHeadMutable();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 if (t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 { // Must match already deduced type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 m = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 if (!t.equals(ta))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 //printf("t = %s ta = %s\n", t.toChars(), ta.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 goto Lnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 if (!t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 dedtypes.data[i] = cast(void*)ta;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 t = ta;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 *psparam = new AliasDeclaration(loc, ident, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 //printf("\tm = %d\n", m);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 return m;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 Lnomatch:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 *psparam = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 //printf("\tm = %d\n", MATCHnomatch);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 return MATCHnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 void* dummyArg()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 }