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