blob: d2e8419d0b78e69c1e8ae748313978a96ed168e5 [file] [log] [blame]
Douglas Gregor99ebf652009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Expr.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000021
22using namespace clang;
23
24//===----------------------------------------------------------------------===/
25// Template Instantiation for Types
26//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +000027namespace {
28 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
29 Sema &SemaRef;
30 const TemplateArgument *TemplateArgs;
31 unsigned NumTemplateArgs;
32 SourceLocation Loc;
33 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +000034
Douglas Gregorcd281c32009-02-28 00:25:32 +000035 public:
36 TemplateTypeInstantiator(Sema &SemaRef,
37 const TemplateArgument *TemplateArgs,
38 unsigned NumTemplateArgs,
39 SourceLocation Loc,
40 DeclarationName Entity)
41 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
42 NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { }
43
44 QualType operator()(QualType T) const { return Instantiate(T); }
45
46 QualType Instantiate(QualType T) const;
47
48 // Declare instantiate functions for each type.
49#define TYPE(Class, Base) \
50 QualType Instantiate##Class##Type(const Class##Type *T, \
51 unsigned Quals) const;
52#define ABSTRACT_TYPE(Class, Base)
53#include "clang/AST/TypeNodes.def"
54 };
55}
56
57QualType
58TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
59 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +000060 // FIXME: Implement this
61 assert(false && "Cannot instantiate ExtQualType yet");
62 return QualType();
63}
64
Douglas Gregorcd281c32009-02-28 00:25:32 +000065QualType
66TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
67 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +000068 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +000069 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +000070}
71
Douglas Gregorcd281c32009-02-28 00:25:32 +000072QualType
73TemplateTypeInstantiator::
74InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +000075 // FIXME: Implement this
76 assert(false && "Cannot instantiate FixedWidthIntType yet");
77 return QualType();
78}
79
Douglas Gregorcd281c32009-02-28 00:25:32 +000080QualType
81TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
82 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +000083 // FIXME: Implement this
84 assert(false && "Cannot instantiate ComplexType yet");
85 return QualType();
86}
87
Douglas Gregorcd281c32009-02-28 00:25:32 +000088QualType
89TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
90 unsigned Quals) const {
91 QualType PointeeType = Instantiate(T->getPointeeType());
92 if (PointeeType.isNull())
93 return QualType();
94
95 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +000096}
97
Douglas Gregorcd281c32009-02-28 00:25:32 +000098QualType
99TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
100 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000101 // FIXME: Implement this
102 assert(false && "Cannot instantiate BlockPointerType yet");
103 return QualType();
104}
105
Douglas Gregorcd281c32009-02-28 00:25:32 +0000106QualType
107TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T,
108 unsigned Quals) const {
109 QualType ReferentType = Instantiate(T->getPointeeType());
110 if (ReferentType.isNull())
111 return QualType();
112
113 return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000114}
115
Douglas Gregorcd281c32009-02-28 00:25:32 +0000116QualType
117TemplateTypeInstantiator::
118InstantiateMemberPointerType(const MemberPointerType *T,
119 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000120 // FIXME: Implement this
121 assert(false && "Cannot instantiate MemberPointerType yet");
122 return QualType();
123}
124
Douglas Gregorcd281c32009-02-28 00:25:32 +0000125QualType
126TemplateTypeInstantiator::
127InstantiateConstantArrayType(const ConstantArrayType *T,
128 unsigned Quals) const {
129 QualType ElementType = Instantiate(T->getElementType());
130 if (ElementType.isNull())
131 return ElementType;
132
133 // Build a temporary integer literal to specify the size for
134 // BuildArrayType. Since we have already checked the size as part of
135 // creating the dependent array type in the first place, we know
136 // there aren't any errors.
Douglas Gregoraca13a72009-02-28 01:09:26 +0000137 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes
138 // problems that I have yet to investigate.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000139 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
140 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
141 &ArraySize, T->getIndexTypeQualifier(),
142 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000143}
144
Douglas Gregorcd281c32009-02-28 00:25:32 +0000145QualType
146TemplateTypeInstantiator::
147InstantiateIncompleteArrayType(const IncompleteArrayType *T,
148 unsigned Quals) const {
149 QualType ElementType = Instantiate(T->getElementType());
150 if (ElementType.isNull())
151 return ElementType;
152
153 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
154 0, T->getIndexTypeQualifier(),
155 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000156}
157
Douglas Gregorcd281c32009-02-28 00:25:32 +0000158QualType
159TemplateTypeInstantiator::
160InstantiateVariableArrayType(const VariableArrayType *T,
161 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000162 // FIXME: Implement this
163 assert(false && "Cannot instantiate VariableArrayType yet");
164 return QualType();
165}
166
Douglas Gregorcd281c32009-02-28 00:25:32 +0000167QualType
168TemplateTypeInstantiator::
169InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
170 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000171 // FIXME: Implement this
172 assert(false && "Cannot instantiate DependentSizedArrayType yet");
173 return QualType();
174}
175
Douglas Gregorcd281c32009-02-28 00:25:32 +0000176QualType
177TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
178 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000179 // FIXME: Implement this
180 assert(false && "Cannot instantiate VectorType yet");
181 return QualType();
182}
183
Douglas Gregorcd281c32009-02-28 00:25:32 +0000184QualType
185TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
186 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000187 // FIXME: Implement this
188 assert(false && "Cannot instantiate ExtVectorType yet");
189 return QualType();
190}
191
Douglas Gregorcd281c32009-02-28 00:25:32 +0000192QualType
193TemplateTypeInstantiator::
194InstantiateFunctionProtoType(const FunctionProtoType *T,
195 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000196 QualType ResultType = Instantiate(T->getResultType());
197 if (ResultType.isNull())
198 return ResultType;
199
200 llvm::SmallVector<QualType, 16> ParamTypes;
201 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
202 ParamEnd = T->arg_type_end();
203 Param != ParamEnd; ++Param) {
204 QualType P = Instantiate(*Param);
205 if (P.isNull())
206 return P;
207
208 ParamTypes.push_back(P);
209 }
210
211 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
212 ParamTypes.size(),
213 T->isVariadic(), T->getTypeQuals(),
214 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000215}
216
Douglas Gregorcd281c32009-02-28 00:25:32 +0000217QualType
218TemplateTypeInstantiator::
219InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
220 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000221 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000222 return QualType();
223}
224
Douglas Gregorcd281c32009-02-28 00:25:32 +0000225QualType
226TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
227 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000228 // FIXME: Implement this
229 assert(false && "Cannot instantiate TypedefType yet");
230 return QualType();
231}
232
Douglas Gregorcd281c32009-02-28 00:25:32 +0000233QualType
234TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
235 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000236 // FIXME: Implement this
237 assert(false && "Cannot instantiate TypeOfExprType yet");
238 return QualType();
239}
240
Douglas Gregorcd281c32009-02-28 00:25:32 +0000241QualType
242TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
243 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000244 // FIXME: Implement this
245 assert(false && "Cannot instantiate TypeOfType yet");
246 return QualType();
247}
248
Douglas Gregorcd281c32009-02-28 00:25:32 +0000249QualType
250TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
251 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000252 // FIXME: Implement this
253 assert(false && "Cannot instantiate RecordType yet");
254 return QualType();
255}
256
Douglas Gregorcd281c32009-02-28 00:25:32 +0000257QualType
258TemplateTypeInstantiator::InstantiateCXXRecordType(const CXXRecordType *T,
259 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000260 // FIXME: Implement this
261 assert(false && "Cannot instantiate CXXRecordType yet");
262 return QualType();
263}
264
Douglas Gregorcd281c32009-02-28 00:25:32 +0000265QualType
266TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
267 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000268 // FIXME: Implement this
269 assert(false && "Cannot instantiate EnumType yet");
270 return QualType();
271}
272
Douglas Gregorcd281c32009-02-28 00:25:32 +0000273QualType
274TemplateTypeInstantiator::
275InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
276 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000277 if (T->getDepth() == 0) {
278 // Replace the template type parameter with its corresponding
279 // template argument.
280 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
281 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
282 "Template argument kind mismatch");
283 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000284 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000285 return Result;
286
287 // C++ [dcl.ref]p1:
288 // [...] Cv-qualified references are ill-formed except when
289 // the cv-qualifiers are introduced through the use of a
290 // typedef (7.1.3) or of a template type argument (14.3), in
291 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000292 if (Quals && Result->isReferenceType())
293 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000294
Douglas Gregorcd281c32009-02-28 00:25:32 +0000295 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000296 }
297
298 // The template type parameter comes from an inner template (e.g.,
299 // the template parameter list of a member template inside the
300 // template we are instantiating). Create a new template type
301 // parameter with the template "level" reduced by one.
302 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
303 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000304 T->getName())
305 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000306}
307
Douglas Gregorcd281c32009-02-28 00:25:32 +0000308QualType
309TemplateTypeInstantiator::
310InstantiateClassTemplateSpecializationType(
311 const ClassTemplateSpecializationType *T,
312 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000313 // FIXME: Implement this
314 assert(false && "Cannot instantiate ClassTemplateSpecializationType yet");
315 return QualType();
316}
317
Douglas Gregorcd281c32009-02-28 00:25:32 +0000318QualType
319TemplateTypeInstantiator::
320InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
321 unsigned Quals) const {
322 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000323 return QualType();
324}
325
Douglas Gregorcd281c32009-02-28 00:25:32 +0000326QualType
327TemplateTypeInstantiator::
328InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
329 unsigned Quals) const {
330 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000331 return QualType();
332}
333
Douglas Gregorcd281c32009-02-28 00:25:32 +0000334QualType
335TemplateTypeInstantiator::
336InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
337 unsigned Quals) const {
338 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000339 return QualType();
340}
341
Douglas Gregorcd281c32009-02-28 00:25:32 +0000342QualType
343TemplateTypeInstantiator::
344InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
345 unsigned Quals) const {
346 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000347 return QualType();
348}
349
Douglas Gregorcd281c32009-02-28 00:25:32 +0000350/// \brief The actual implementation of Sema::InstantiateType().
351QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
352 // If T is not a dependent type, there is nothing to do.
353 if (!T->isDependentType())
354 return T;
355
356 switch (T->getTypeClass()) {
357#define TYPE(Class, Base) \
358 case Type::Class: \
359 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
360 T.getCVRQualifiers());
361#define ABSTRACT_TYPE(Class, Base)
362#include "clang/AST/TypeNodes.def"
363 }
364
365 assert(false && "Not all types have been decoded for instantiation");
366 return QualType();
367}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000368
369/// \brief Instantiate the type T with a given set of template arguments.
370///
371/// This routine substitutes the given template arguments into the
372/// type T and produces the instantiated type.
373///
374/// \param T the type into which the template arguments will be
375/// substituted. If this type is not dependent, it will be returned
376/// immediately.
377///
378/// \param TemplateArgs the template arguments that will be
379/// substituted for the top-level template parameters within T.
380///
381/// \param NumTemplateArgs the number of template arguments provided
382/// by TemplateArgs.
383///
384/// \param Loc the location in the source code where this substitution
385/// is being performed. It will typically be the location of the
386/// declarator (if we're instantiating the type of some declaration)
387/// or the location of the type in the source code (if, e.g., we're
388/// instantiating the type of a cast expression).
389///
390/// \param Entity the name of the entity associated with a declaration
391/// being instantiated (if any). May be empty to indicate that there
392/// is no such entity (if, e.g., this is a type that occurs as part of
393/// a cast expression) or that the entity has no name (e.g., an
394/// unnamed function parameter).
395///
396/// \returns If the instantiation succeeds, the instantiated
397/// type. Otherwise, produces diagnostics and returns a NULL type.
398QualType Sema::InstantiateType(QualType T,
399 const TemplateArgument *TemplateArgs,
400 unsigned NumTemplateArgs,
401 SourceLocation Loc, DeclarationName Entity) {
402 // If T is not a dependent type, there is nothing to do.
403 if (!T->isDependentType())
404 return T;
405
Douglas Gregorcd281c32009-02-28 00:25:32 +0000406 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
407 Loc, Entity);
408 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000409}