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 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();
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)
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
135 * ident : specType = defaultType
137 Type
*specType
; // type parameter: if !=NULL, this is the type specialization
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
);
156 struct TemplateThisParameter
: TemplateTypeParameter
159 * this ident : specType = defaultType
161 Type
*specType
; // type parameter: if !=NULL, this is the type specialization
164 TemplateThisParameter(Loc loc
, Identifier
*ident
, Type
*specType
, Type
*defaultType
);
166 TemplateThisParameter
*isTemplateThisParameter();
167 TemplateParameter
*syntaxCopy();
168 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
172 struct TemplateValueParameter
: TemplateParameter
175 * valType ident : specValue = defaultValue
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
);
199 struct TemplateAliasParameter
: TemplateParameter
202 * ident : specAlias = 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
);
227 struct TemplateTupleParameter
: TemplateParameter
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
);
248 struct TemplateInstance
: ScopeDsymbol
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
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
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
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
;
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
);
290 void toCBuffer(OutBuffer
*buf
, HdrGenState
*hgs
);
291 Dsymbol
*toAlias(); // resolve real symbol
293 int oneMember(Dsymbol
**ps
);
297 void toObjFile(int multiobj
); // compile to .obj file
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
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
);
326 int oneMember(Dsymbol
**ps
);
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 */