2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2006 by Digital Mars
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
19 #include "arraytypes.h"
25 struct TemplateInstance
;
26 struct TemplateParameter
;
27 struct TemplateTypeParameter
;
28 struct TemplateThisParameter
;
29 struct TemplateValueParameter
;
30 struct TemplateAliasParameter
;
31 struct TemplateTupleParameter
;
36 struct AliasDeclaration
;
37 struct FuncDeclaration
;
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
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
);
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();
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)
98 * template Foo(this ident)
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
138 * ident : specType = defaultType
140 Type
*specType
; // type parameter: if !=NULL, this is the type specialization
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
);
159 struct TemplateThisParameter
: TemplateTypeParameter
162 * this ident : specType = defaultType
164 Type
*specType
; // type parameter: if !=NULL, this is the type specialization
167 TemplateThisParameter(Loc loc
, Identifier
*ident
, Type
*specType
, Type
*defaultType
);
169 TemplateThisParameter
*isTemplateThisParameter();
170 TemplateParameter
*syntaxCopy();
171 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
175 struct TemplateValueParameter
: TemplateParameter
178 * valType ident : specValue = defaultValue
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
);
202 struct TemplateAliasParameter
: TemplateParameter
205 * ident : specAlias = 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
);
230 struct TemplateTupleParameter
: TemplateParameter
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
);
251 struct TemplateInstance
: ScopeDsymbol
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
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
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
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
;
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
);
293 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
294 Dsymbol
*toAlias(); // resolve real symbol
296 int oneMember(Dsymbol
**ps
);
300 void toObjFile(int multiobj
); // compile to .obj file
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
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
);
329 int oneMember(Dsymbol
**ps
);
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 */