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