Allow log_ statements outside of a class
[delight/core.git] / dmd / template.h
blob7e6e46a07c028a962c7f73611b3c8401a87a4454
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 int leastAsSpecialized(TemplateDeclaration *td2);
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);
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)
99 Loc loc;
100 Identifier *ident;
102 Declaration *sparam;
104 TemplateParameter(Loc loc, Identifier *ident);
106 virtual TemplateTypeParameter *isTemplateTypeParameter();
107 virtual TemplateValueParameter *isTemplateValueParameter();
108 virtual TemplateAliasParameter *isTemplateAliasParameter();
109 virtual TemplateTupleParameter *isTemplateTupleParameter();
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;
119 /* If TemplateParameter's match as far as overloading goes.
121 virtual int overloadMatch(TemplateParameter *) = 0;
123 /* Match actual argument against parameter.
125 virtual MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam) = 0;
127 /* Create dummy argument based on parameter.
129 virtual void *dummyArg() = 0;
132 struct TemplateTypeParameter : TemplateParameter
134 /* Syntax:
135 * ident : specType = defaultType
137 Type *specType; // type parameter: if !=NULL, this is the type specialization
138 Type *defaultType;
140 TemplateTypeParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
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();
155 #if V2
156 struct TemplateThisParameter : TemplateTypeParameter
158 /* Syntax:
159 * this ident : specType = defaultType
161 Type *specType; // type parameter: if !=NULL, this is the type specialization
162 Type *defaultType;
164 TemplateThisParameter(Loc loc, Identifier *ident, Type *specType, Type *defaultType);
166 TemplateThisParameter *isTemplateThisParameter();
167 TemplateParameter *syntaxCopy();
168 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
170 #endif
172 struct TemplateValueParameter : TemplateParameter
174 /* Syntax:
175 * valType ident : specValue = defaultValue
178 Type *valType;
179 Expression *specValue;
180 Expression *defaultValue;
182 static Expression *edummy;
184 TemplateValueParameter(Loc loc, Identifier *ident, Type *valType, Expression *specValue, Expression *defaultValue);
186 TemplateValueParameter *isTemplateValueParameter();
187 TemplateParameter *syntaxCopy();
188 void declareParameter(Scope *sc);
189 void semantic(Scope *);
190 void print(Object *oarg, Object *oded);
191 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
192 Object *specialization();
193 Object *defaultArg(Scope *sc);
194 int overloadMatch(TemplateParameter *);
195 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam);
196 void *dummyArg();
199 struct TemplateAliasParameter : TemplateParameter
201 /* Syntax:
202 * ident : specAlias = defaultAlias
205 Type *specAliasT;
206 Dsymbol *specAlias;
208 Type *defaultAlias;
210 static Dsymbol *sdummy;
212 TemplateAliasParameter(Loc loc, Identifier *ident, Type *specAliasT, Type *defaultAlias);
214 TemplateAliasParameter *isTemplateAliasParameter();
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();
227 struct TemplateTupleParameter : TemplateParameter
229 /* Syntax:
230 * ident ...
233 TemplateTupleParameter(Loc loc, Identifier *ident);
235 TemplateTupleParameter *isTemplateTupleParameter();
236 TemplateParameter *syntaxCopy();
237 void declareParameter(Scope *sc);
238 void semantic(Scope *);
239 void print(Object *oarg, Object *oded);
240 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
241 Object *specialization();
242 Object *defaultArg(Scope *sc);
243 int overloadMatch(TemplateParameter *);
244 MATCH matchArg(Scope *sc, Objects *tiargs, int i, TemplateParameters *parameters, Objects *dedtypes, Declaration **psparam);
245 void *dummyArg();
248 struct TemplateInstance : ScopeDsymbol
250 /* Given:
251 * foo!(args) =>
252 * name = foo
253 * tiargs = args
255 Identifier *name;
256 //Array idents;
257 Objects *tiargs; // Array of Types/Expressions of template
258 // instance arguments [int*, char, 10*10]
260 Objects tdtypes; // Array of Types/Expressions corresponding
261 // to TemplateDeclaration.parameters
262 // [int, char, 100]
264 TemplateDeclaration *tempdecl; // referenced by foo.bar.abc
265 TemplateInstance *inst; // refer to existing instance
266 ScopeDsymbol *argsym; // argument symbol table
267 AliasDeclaration *aliasdecl; // !=NULL if instance is an alias for its
268 // sole member
269 WithScopeSymbol *withsym; // if a member of a with statement
270 int semanticdone; // has semantic() been done?
271 int nest; // for recursion detection
272 int havetempdecl; // 1 if used second constructor
273 Dsymbol *isnested; // if referencing local symbols, this is the context
274 int errors; // 1 if compiled with errors
275 #ifdef IN_GCC
276 /* On some targets, it is necessary to know whether a symbol
277 will be emitted in the output or not before the symbol
278 is used. This can be different from getModule(). */
279 Module * objFileModule;
280 #endif
282 TemplateInstance(Loc loc, Identifier *temp_id);
283 TemplateInstance(Loc loc, TemplateDeclaration *tempdecl, Objects *tiargs);
284 static Objects *arraySyntaxCopy(Objects *objs);
285 Dsymbol *syntaxCopy(Dsymbol *);
286 void semantic(Scope *sc);
287 void semantic2(Scope *sc);
288 void semantic3(Scope *sc);
289 void inlineScan();
290 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
291 Dsymbol *toAlias(); // resolve real symbol
292 char *kind();
293 int oneMember(Dsymbol **ps);
294 char *toChars();
295 char *mangle();
297 void toObjFile(int multiobj); // compile to .obj file
299 // Internal
300 static void semanticTiargs(Loc loc, Scope *sc, Objects *tiargs);
301 void semanticTiargs(Scope *sc);
302 TemplateDeclaration *findTemplateDeclaration(Scope *sc);
303 TemplateDeclaration *findBestMatch(Scope *sc);
304 void declareParameters(Scope *sc);
305 int isNested(Objects *tiargs);
306 Identifier *genIdent();
308 TemplateInstance *isTemplateInstance() { return this; }
309 AliasDeclaration *isAliasDeclaration();
312 struct TemplateMixin : TemplateInstance
314 Array *idents;
315 Type *tqual;
317 Scope *scope; // for forward referencing
319 TemplateMixin(Loc loc, Identifier *ident, Type *tqual, Array *idents, Objects *tiargs);
320 Dsymbol *syntaxCopy(Dsymbol *s);
321 void semantic(Scope *sc);
322 void semantic2(Scope *sc);
323 void semantic3(Scope *sc);
324 void inlineScan();
325 char *kind();
326 int oneMember(Dsymbol **ps);
327 int hasPointers();
328 char *toChars();
329 void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
331 void toObjFile(int multiobj); // compile to .obj file
333 TemplateMixin *isTemplateMixin() { return this; }
336 Expression *isExpression(Object *o);
337 Dsymbol *isDsymbol(Object *o);
338 Type *isType(Object *o);
339 Tuple *isTuple(Object *o);
340 Type *getType(Object *o);
341 Dsymbol *getDsymbol(Object *o);
343 void ObjectToCBuffer(OutBuffer *buf, HdrGenState *hgs, Object *oarg);
345 #endif /* DMD_TEMPLATE_H */