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