comparison dmd2/template.h @ 758:f04dde6e882c

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