159
|
1
|
|
2 // Compiler implementation of the D programming language
|
|
3 // Copyright (c) 1999-2006 by Digital Mars
|
|
4 // All Rights Reserved
|
|
5 // written by Walter Bright
|
|
6 // http://www.digitalmars.com
|
|
7 // License for redistribution is by either the Artistic License
|
|
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
9 // See the included readme.txt for details.
|
|
10
|
|
11 #ifndef DMD_TEMPLATE_H
|
|
12 #define DMD_TEMPLATE_H
|
|
13
|
|
14 #ifdef __DMC__
|
|
15 #pragma once
|
|
16 #endif /* __DMC__ */
|
|
17
|
|
18 #include "root.h"
|
|
19 #include "arraytypes.h"
|
|
20 #include "dsymbol.h"
|
|
21 #include "mtype.h"
|
|
22
|
|
23
|
|
24 struct OutBuffer;
|
|
25 struct Identifier;
|
|
26 struct TemplateInstance;
|
|
27 struct TemplateParameter;
|
|
28 struct TemplateTypeParameter;
|
336
|
29 struct TemplateThisParameter;
|
159
|
30 struct TemplateValueParameter;
|
|
31 struct TemplateAliasParameter;
|
|
32 struct TemplateTupleParameter;
|
|
33 struct Type;
|
|
34 struct TypeTypeof;
|
|
35 struct Scope;
|
|
36 struct Expression;
|
|
37 struct AliasDeclaration;
|
|
38 struct FuncDeclaration;
|
|
39 struct HdrGenState;
|
|
40 enum MATCH;
|
|
41
|
|
42 struct Tuple : Object
|
|
43 {
|
|
44 Objects objects;
|
|
45
|
|
46 int dyncast() { return DYNCAST_TUPLE; } // kludge for template.isType()
|
|
47 };
|
|
48
|
|
49
|
|
50 struct TemplateDeclaration : ScopeDsymbol
|
|
51 {
|
|
52 TemplateParameters *parameters; // array of TemplateParameter's
|
|
53
|
|
54 TemplateParameters *origParameters; // originals for Ddoc
|
|
55
|
336
|
56 Array instances; // array of TemplateInstance's
|
159
|
57
|
|
58 TemplateDeclaration *overnext; // next overloaded TemplateDeclaration
|
|
59 TemplateDeclaration *overroot; // first in overnext list
|
|
60
|
|
61 Scope *scope;
|
|
62 Dsymbol *onemember; // if !=NULL then one member of this template
|
|
63
|
|
64 TemplateDeclaration(Loc loc, Identifier *id, TemplateParameters *parameters, Array *decldefs);
|
|
65 Dsymbol *syntaxCopy(Dsymbol *);
|
|
66 void semantic(Scope *sc);
|
|
67 int overloadInsert(Dsymbol *s);
|
|
68 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
336
|
69 const char *kind();
|
159
|
70 char *toChars();
|
|
71
|
|
72 void emitComment(Scope *sc);
|
|
73 // void toDocBuffer(OutBuffer *buf);
|
|
74
|
|
75 MATCH matchWithInstance(TemplateInstance *ti, Objects *atypes, int flag);
|
|
76 int leastAsSpecialized(TemplateDeclaration *td2);
|
|
77
|
|
78 MATCH deduceFunctionTemplateMatch(Objects *targsi, Expressions *fargs, Objects *dedargs);
|
|
79 FuncDeclaration *deduceFunctionTemplate(Scope *sc, Loc loc, Objects *targsi, Expressions *fargs);
|
|
80 void declareParameter(Scope *sc, TemplateParameter *tp, Object *o);
|
|
81
|
|
82 TemplateDeclaration *isTemplateDeclaration() { return this; }
|
|
83
|
|
84 TemplateTupleParameter *isVariadic();
|
|
85 int isOverloadable();
|
|
86 };
|
|
87
|
|
88 struct TemplateParameter
|
|
89 {
|
|
90 /* For type-parameter:
|
|
91 * template Foo(ident) // specType is set to NULL
|
|
92 * template Foo(ident : specType)
|
|
93 * For value-parameter:
|
|
94 * template Foo(valType ident) // specValue is set to NULL
|
|
95 * template Foo(valType ident : specValue)
|
|
96 * For alias-parameter:
|
|
97 * template Foo(alias ident)
|
|
98 */
|
|
99
|
|
100 Loc loc;
|
|
101 Identifier *ident;
|
|
102
|
|
103 Declaration *sparam;
|
|
104
|
|
105 TemplateParameter(Loc loc, Identifier *ident);
|
|
106
|
|
107 virtual TemplateTypeParameter *isTemplateTypeParameter();
|
|
108 virtual TemplateValueParameter *isTemplateValueParameter();
|
|
109 virtual TemplateAliasParameter *isTemplateAliasParameter();
|
|
110 virtual TemplateTupleParameter *isTemplateTupleParameter();
|
|
111
|
|
112 virtual TemplateParameter *syntaxCopy() = 0;
|
|
113 virtual void declareParameter(Scope *sc) = 0;
|
|
114 virtual void semantic(Scope *) = 0;
|
|
115 virtual void print(Object *oarg, Object *oded) = 0;
|
|
116 virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs) = 0;
|
|
117 virtual Object *specialization() = 0;
|
|
118 virtual Object *defaultArg(Scope *sc) = 0;
|
|
119
|
|
120 /* If TemplateParameter's match as far as overloading goes.
|
|
121 */
|
|
122 virtual int overloadMatch(TemplateParameter *) = 0;
|
|
123
|
|
124 /* Match actual argument against parameter.
|
|
125 */
|
|
126 virtual MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam) = 0;
|
|
127
|
|
128 /* Create dummy argument based on parameter.
|
|
129 */
|
|
130 virtual void *dummyArg() = 0;
|
|
131 };
|
|
132
|
|
133 struct TemplateTypeParameter : TemplateParameter
|
|
134 {
|
|
135 /* Syntax:
|
|
136 * ident : specType = defaultType
|
|
137 */
|
|
138 Type *specType; // type parameter: if !=NULL, this is the type specialization
|
|
139 Type *defaultType;
|
|
140
|
|
141 TemplateTypeParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
|
|
142
|
|
143 TemplateTypeParameter *isTemplateTypeParameter();
|
|
144 TemplateParameter *syntaxCopy();
|
|
145 void declareParameter(Scope *sc);
|
|
146 void semantic(Scope *);
|
|
147 void print(Object *oarg, Object *oded);
|
|
148 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
|
149 Object *specialization();
|
|
150 Object *defaultArg(Scope *sc);
|
|
151 int overloadMatch(TemplateParameter *);
|
|
152 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam);
|
|
153 void *dummyArg();
|
|
154 };
|
|
155
|
336
|
156 #if DMDV2
|
|
157 struct TemplateThisParameter : TemplateTypeParameter
|
|
158 {
|
|
159 /* Syntax:
|
|
160 * this ident : specType = defaultType
|
|
161 */
|
|
162 Type *specType; // type parameter: if !=NULL, this is the type specialization
|
|
163 Type *defaultType;
|
|
164
|
|
165 TemplateThisParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
|
|
166
|
|
167 TemplateThisParameter *isTemplateThisParameter();
|
|
168 TemplateParameter *syntaxCopy();
|
|
169 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
|
170 };
|
|
171 #endif
|
|
172
|
159
|
173 struct TemplateValueParameter : TemplateParameter
|
|
174 {
|
|
175 /* Syntax:
|
|
176 * valType ident : specValue = defaultValue
|
|
177 */
|
|
178
|
|
179 Type *valType;
|
|
180 Expression *specValue;
|
|
181 Expression *defaultValue;
|
|
182
|
|
183 static Expression *edummy;
|
|
184
|
|
185 TemplateValueParameter(Loc loc, Identifier *ident, Type *valType, Expression *specValue, Expression *defaultValue);
|
|
186
|
|
187 TemplateValueParameter *isTemplateValueParameter();
|
|
188 TemplateParameter *syntaxCopy();
|
|
189 void declareParameter(Scope *sc);
|
|
190 void semantic(Scope *);
|
|
191 void print(Object *oarg, Object *oded);
|
|
192 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
|
193 Object *specialization();
|
|
194 Object *defaultArg(Scope *sc);
|
|
195 int overloadMatch(TemplateParameter *);
|
|
196 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam);
|
|
197 void *dummyArg();
|
|
198 };
|
|
199
|
|
200 struct TemplateAliasParameter : TemplateParameter
|
|
201 {
|
|
202 /* Syntax:
|
|
203 * ident : specAlias = defaultAlias
|
|
204 */
|
|
205
|
|
206 Type *specAliasT;
|
|
207 Dsymbol *specAlias;
|
|
208
|
|
209 Type *defaultAlias;
|
|
210
|
|
211 static Dsymbol *sdummy;
|
|
212
|
|
213 TemplateAliasParameter(Loc loc, Identifier *ident, Type *specAliasT, Type *defaultAlias);
|
|
214
|
|
215 TemplateAliasParameter *isTemplateAliasParameter();
|
|
216 TemplateParameter *syntaxCopy();
|
|
217 void declareParameter(Scope *sc);
|
|
218 void semantic(Scope *);
|
|
219 void print(Object *oarg, Object *oded);
|
|
220 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
|
221 Object *specialization();
|
|
222 Object *defaultArg(Scope *sc);
|
|
223 int overloadMatch(TemplateParameter *);
|
|
224 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam);
|
|
225 void *dummyArg();
|
|
226 };
|
|
227
|
|
228 struct TemplateTupleParameter : TemplateParameter
|
|
229 {
|
|
230 /* Syntax:
|
|
231 * ident ...
|
|
232 */
|
|
233
|
|
234 TemplateTupleParameter(Loc loc, Identifier *ident);
|
|
235
|
|
236 TemplateTupleParameter *isTemplateTupleParameter();
|
|
237 TemplateParameter *syntaxCopy();
|
|
238 void declareParameter(Scope *sc);
|
|
239 void semantic(Scope *);
|
|
240 void print(Object *oarg, Object *oded);
|
|
241 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
|
242 Object *specialization();
|
|
243 Object *defaultArg(Scope *sc);
|
|
244 int overloadMatch(TemplateParameter *);
|
|
245 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam);
|
|
246 void *dummyArg();
|
|
247 };
|
|
248
|
|
249 struct TemplateInstance : ScopeDsymbol
|
|
250 {
|
|
251 /* Given:
|
|
252 * foo!(args) =>
|
|
253 * name = foo
|
|
254 * tiargs = args
|
|
255 */
|
|
256 Identifier *name;
|
|
257 //Array idents;
|
|
258 Objects *tiargs; // Array of Types/Expressions of template
|
|
259 // instance arguments [int*, char, 10*10]
|
|
260
|
|
261 Objects tdtypes; // Array of Types/Expressions corresponding
|
|
262 // to TemplateDeclaration.parameters
|
|
263 // [int, char, 100]
|
|
264
|
|
265 TemplateDeclaration *tempdecl; // referenced by foo.bar.abc
|
|
266 TemplateInstance *inst; // refer to existing instance
|
|
267 ScopeDsymbol *argsym; // argument symbol table
|
|
268 AliasDeclaration *aliasdecl; // !=NULL if instance is an alias for its
|
|
269 // sole member
|
|
270 WithScopeSymbol *withsym; // if a member of a with statement
|
|
271 int semanticdone; // has semantic() been done?
|
336
|
272 int semantictiargsdone; // has semanticTiargs() been done?
|
159
|
273 int nest; // for recursion detection
|
|
274 int havetempdecl; // 1 if used second constructor
|
|
275 Dsymbol *isnested; // if referencing local symbols, this is the context
|
|
276 int errors; // 1 if compiled with errors
|
|
277 #ifdef IN_GCC
|
|
278 /* On some targets, it is necessary to know whether a symbol
|
|
279 will be emitted in the output or not before the symbol
|
|
280 is used. This can be different from getModule(). */
|
|
281 Module * objFileModule;
|
|
282 #endif
|
|
283
|
|
284 TemplateInstance(Loc loc, Identifier *temp_id);
|
|
285 TemplateInstance(Loc loc, TemplateDeclaration *tempdecl, Objects *tiargs);
|
|
286 static Objects *arraySyntaxCopy(Objects *objs);
|
|
287 Dsymbol *syntaxCopy(Dsymbol *);
|
|
288 void semantic(Scope *sc);
|
|
289 void semantic2(Scope *sc);
|
|
290 void semantic3(Scope *sc);
|
|
291 void inlineScan();
|
|
292 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
|
293 Dsymbol *toAlias(); // resolve real symbol
|
336
|
294 const char *kind();
|
159
|
295 int oneMember(Dsymbol **ps);
|
|
296 char *toChars();
|
|
297 char *mangle();
|
|
298
|
336
|
299 void toObjFile(int multiobj); // compile to .obj file
|
159
|
300
|
|
301 // Internal
|
|
302 static void semanticTiargs(Loc loc, Scope *sc, Objects *tiargs);
|
|
303 void semanticTiargs(Scope *sc);
|
|
304 TemplateDeclaration *findTemplateDeclaration(Scope *sc);
|
|
305 TemplateDeclaration *findBestMatch(Scope *sc);
|
|
306 void declareParameters(Scope *sc);
|
|
307 int isNested(Objects *tiargs);
|
|
308 Identifier *genIdent();
|
|
309
|
|
310 TemplateInstance *isTemplateInstance() { return this; }
|
|
311 AliasDeclaration *isAliasDeclaration();
|
|
312 };
|
|
313
|
|
314 struct TemplateMixin : TemplateInstance
|
|
315 {
|
|
316 Array *idents;
|
|
317 Type *tqual;
|
|
318
|
|
319 Scope *scope; // for forward referencing
|
|
320
|
|
321 TemplateMixin(Loc loc, Identifier *ident, Type *tqual, Array *idents, Objects *tiargs);
|
|
322 Dsymbol *syntaxCopy(Dsymbol *s);
|
|
323 void semantic(Scope *sc);
|
|
324 void semantic2(Scope *sc);
|
|
325 void semantic3(Scope *sc);
|
|
326 void inlineScan();
|
336
|
327 const char *kind();
|
159
|
328 int oneMember(Dsymbol **ps);
|
|
329 int hasPointers();
|
|
330 char *toChars();
|
321
|
331 char *mangle();
|
159
|
332 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
|
|
333
|
336
|
334 void toObjFile(int multiobj); // compile to .obj file
|
159
|
335
|
|
336 TemplateMixin *isTemplateMixin() { return this; }
|
|
337 };
|
|
338
|
|
339 Expression *isExpression(Object *o);
|
|
340 Dsymbol *isDsymbol(Object *o);
|
|
341 Type *isType(Object *o);
|
|
342 Tuple *isTuple(Object *o);
|
|
343 Type *getType(Object *o);
|
|
344 Dsymbol *getDsymbol(Object *o);
|
|
345
|
|
346 void ObjectToCBuffer(OutBuffer *buf, HdrGenState *hgs, Object *oarg);
|
|
347
|
|
348 #endif /* DMD_TEMPLATE_H */
|