Fixed some lexing problems with DOS line-endings
[delight/core.git] / dmd2 / template.h
blobe518e267da6c880880bc9b6d818c0214d2d34a5f
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.
11 #ifndef DMD_TEMPLATE_H
12 #define DMD_TEMPLATE_H
14 #ifdef __DMC__
15 #pragma once
16 #endif /* __DMC__ */
18 #include "root.h"
19 #include "arraytypes.h"
20 #include "dsymbol.h"
23 struct OutBuffer;
24 struct Identifier;
25 struct TemplateInstance;
26 struct TemplateParameter;
27 struct TemplateTypeParameter;
28 struct TemplateThisParameter;
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;
41 struct Tuple : Object
43 Objects objects;
45 int dyncast() { return DYNCAST_TUPLE; } // kludge for template.isType()
49 struct TemplateDeclaration : ScopeDsymbol
51 TemplateParameters *parameters; // array of TemplateParameter's
53 TemplateParameters *origParameters; // originals for Ddoc
55 Array instances; // array of TemplateInstance's
57 TemplateDeclaration *overnext; // next overloaded TemplateDeclaration
58 TemplateDeclaration *overroot; // first in overnext list
60 Scope *scope;
61 Dsymbol *onemember; // if !=NULL then one member of this template
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();
71 void emitComment(Scope *sc);
72 // void toDocBuffer(OutBuffer *buf);
74 MATCH matchWithInstance(TemplateInstance *ti, Objects *atypes, int flag);
75 MATCH leastAsSpecialized(TemplateDeclaration *td2);
77 MATCH deduceFunctionTemplateMatch(Loc loc, Objects *targsi, Expression *ethis, Expressions *fargs, Objects *dedargs);
78 FuncDeclaration *deduceFunctionTemplate(Scope *sc, Loc loc, Objects *targsi, Expression *ethis, Expressions *fargs, int flags = 0);
79 void declareParameter(Scope *sc, TemplateParameter *tp, Object *o);
81 TemplateDeclaration *isTemplateDeclaration() { return this; }
83 TemplateTupleParameter *isVariadic();
84 int isOverloadable();
87 struct TemplateParameter
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 * For this-parameter:
98 * template Foo(this ident)
101 Loc loc;
102 Identifier *ident;
104 Declaration *sparam;
106 TemplateParameter(Loc loc, Identifier *ident);
108 virtual TemplateTypeParameter *isTemplateTypeParameter();
109 virtual TemplateValueParameter *isTemplateValueParameter();
110 virtual TemplateAliasParameter *isTemplateAliasParameter();
111 virtual TemplateThisParameter *isTemplateThisParameter();
112 virtual TemplateTupleParameter *isTemplateTupleParameter();
114 virtual TemplateParameter *syntaxCopy() = 0;
115 virtual void declareParameter(Scope *sc) = 0;
116 virtual void semantic(Scope *) = 0;
117 virtual void print(Object *oarg, Object *oded) = 0;
118 virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs) = 0;
119 virtual Object *specialization() = 0;
120 virtual Object *defaultArg(Loc loc, Scope *sc) = 0;
122 /* If TemplateParameter's match as far as overloading goes.
124 virtual int overloadMatch(TemplateParameter *) = 0;
126 /* Match actual argument against parameter.
128 virtual MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags = 0) = 0;
130 /* Create dummy argument based on parameter.
132 virtual void *dummyArg() = 0;
135 struct TemplateTypeParameter : TemplateParameter
137 /* Syntax:
138 * ident : specType = defaultType
140 Type *specType; // type parameter: if !=NULL, this is the type specialization
141 Type *defaultType;
143 TemplateTypeParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
145 TemplateTypeParameter *isTemplateTypeParameter();
146 TemplateParameter *syntaxCopy();
147 void declareParameter(Scope *sc);
148 void semantic(Scope *);
149 void print(Object *oarg, Object *oded);
150 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
151 Object *specialization();
152 Object *defaultArg(Loc loc, Scope *sc);
153 int overloadMatch(TemplateParameter *);
154 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
155 void *dummyArg();
158 #if V2
159 struct TemplateThisParameter : TemplateTypeParameter
161 /* Syntax:
162 * this ident : specType = defaultType
164 Type *specType; // type parameter: if !=NULL, this is the type specialization
165 Type *defaultType;
167 TemplateThisParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
169 TemplateThisParameter *isTemplateThisParameter();
170 TemplateParameter *syntaxCopy();
171 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
173 #endif
175 struct TemplateValueParameter : TemplateParameter
177 /* Syntax:
178 * valType ident : specValue = defaultValue
181 Type *valType;
182 Expression *specValue;
183 Expression *defaultValue;
185 static Expression *edummy;
187 TemplateValueParameter(Loc loc, Identifier *ident, Type *valType, Expression *specValue, Expression *defaultValue);
189 TemplateValueParameter *isTemplateValueParameter();
190 TemplateParameter *syntaxCopy();
191 void declareParameter(Scope *sc);
192 void semantic(Scope *);
193 void print(Object *oarg, Object *oded);
194 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
195 Object *specialization();
196 Object *defaultArg(Loc loc, Scope *sc);
197 int overloadMatch(TemplateParameter *);
198 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
199 void *dummyArg();
202 struct TemplateAliasParameter : TemplateParameter
204 /* Syntax:
205 * ident : specAlias = defaultAlias
208 Type *specAliasT;
209 Dsymbol *specAlias;
211 Type *defaultAlias;
213 static Dsymbol *sdummy;
215 TemplateAliasParameter(Loc loc, Identifier *ident, Type *specAliasT, Type *defaultAlias);
217 TemplateAliasParameter *isTemplateAliasParameter();
218 TemplateParameter *syntaxCopy();
219 void declareParameter(Scope *sc);
220 void semantic(Scope *);
221 void print(Object *oarg, Object *oded);
222 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
223 Object *specialization();
224 Object *defaultArg(Loc loc, Scope *sc);
225 int overloadMatch(TemplateParameter *);
226 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
227 void *dummyArg();
230 struct TemplateTupleParameter : TemplateParameter
232 /* Syntax:
233 * ident ...
236 TemplateTupleParameter(Loc loc, Identifier *ident);
238 TemplateTupleParameter *isTemplateTupleParameter();
239 TemplateParameter *syntaxCopy();
240 void declareParameter(Scope *sc);
241 void semantic(Scope *);
242 void print(Object *oarg, Object *oded);
243 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
244 Object *specialization();
245 Object *defaultArg(Loc loc, Scope *sc);
246 int overloadMatch(TemplateParameter *);
247 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam, int flags);
248 void *dummyArg();
251 struct TemplateInstance : ScopeDsymbol
253 /* Given:
254 * foo!(args) =>
255 * name = foo
256 * tiargs = args
258 Identifier *name;
259 //Array idents;
260 Objects *tiargs; // Array of Types/Expressions of template
261 // instance arguments [int*, char, 10*10]
263 Objects tdtypes; // Array of Types/Expressions corresponding
264 // to TemplateDeclaration.parameters
265 // [int, char, 100]
267 TemplateDeclaration *tempdecl; // referenced by foo.bar.abc
268 TemplateInstance *inst; // refer to existing instance
269 ScopeDsymbol *argsym; // argument symbol table
270 AliasDeclaration *aliasdecl; // !=NULL if instance is an alias for its
271 // sole member
272 WithScopeSymbol *withsym; // if a member of a with statement
273 int semanticdone; // has semantic() been done?
274 int nest; // for recursion detection
275 int havetempdecl; // 1 if used second constructor
276 Dsymbol *isnested; // if referencing local symbols, this is the context
277 int errors; // 1 if compiled with errors
278 #ifdef IN_GCC
279 /* On some targets, it is necessary to know whether a symbol
280 will be emitted in the output or not before the symbol
281 is used. This can be different from getModule(). */
282 Module * objFileModule;
283 #endif
285 TemplateInstance(Loc loc, Identifier *temp_id);
286 TemplateInstance(Loc loc, TemplateDeclaration *tempdecl, Objects *tiargs);
287 static Objects *arraySyntaxCopy(Objects *objs);
288 Dsymbol *syntaxCopy(Dsymbol *);
289 void semantic(Scope *sc);
290 void semantic2(Scope *sc);
291 void semantic3(Scope *sc);
292 void inlineScan();
293 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
294 Dsymbol *toAlias(); // resolve real symbol
295 char *kind();
296 int oneMember(Dsymbol **ps);
297 char *toChars();
298 char *mangle();
300 void toObjFile(int multiobj); // compile to .obj file
302 // Internal
303 static void semanticTiargs(Loc loc, Scope *sc, Objects *tiargs, int flags);
304 void semanticTiargs(Scope *sc);
305 TemplateDeclaration *findTemplateDeclaration(Scope *sc);
306 TemplateDeclaration *findBestMatch(Scope *sc);
307 void declareParameters(Scope *sc);
308 int isNested(Objects *tiargs);
309 Identifier *genIdent();
311 TemplateInstance *isTemplateInstance() { return this; }
312 AliasDeclaration *isAliasDeclaration();
315 struct TemplateMixin : TemplateInstance
317 Array *idents;
318 Type *tqual;
320 Scope *scope; // for forward referencing
322 TemplateMixin(Loc loc, Identifier *ident, Type *tqual, Array *idents, Objects *tiargs);
323 Dsymbol *syntaxCopy(Dsymbol *s);
324 void semantic(Scope *sc);
325 void semantic2(Scope *sc);
326 void semantic3(Scope *sc);
327 void inlineScan();
328 char *kind();
329 int oneMember(Dsymbol **ps);
330 int hasPointers();
331 char *toChars();
332 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
334 void toObjFile(int multiobj); // compile to .obj file
336 TemplateMixin *isTemplateMixin() { return this; }
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);
346 void ObjectToCBuffer(OutBuffer *buf, HdrGenState *hgs, Object *oarg);
348 #endif /* DMD_TEMPLATE_H */