blob: f864a50c5ec9543ad5b5b3939a6d7d03a99af653 [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 Gregor99ebf652009-02-27 19:31:52 +000068 assert(false && "BuiltinType is never 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.
137 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
138 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
139 &ArraySize, T->getIndexTypeQualifier(),
140 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000141}
142
Douglas Gregorcd281c32009-02-28 00:25:32 +0000143QualType
144TemplateTypeInstantiator::
145InstantiateIncompleteArrayType(const IncompleteArrayType *T,
146 unsigned Quals) const {
147 QualType ElementType = Instantiate(T->getElementType());
148 if (ElementType.isNull())
149 return ElementType;
150
151 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
152 0, T->getIndexTypeQualifier(),
153 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000154}
155
Douglas Gregorcd281c32009-02-28 00:25:32 +0000156QualType
157TemplateTypeInstantiator::
158InstantiateVariableArrayType(const VariableArrayType *T,
159 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000160 // FIXME: Implement this
161 assert(false && "Cannot instantiate VariableArrayType yet");
162 return QualType();
163}
164
Douglas Gregorcd281c32009-02-28 00:25:32 +0000165QualType
166TemplateTypeInstantiator::
167InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
168 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000169 // FIXME: Implement this
170 assert(false && "Cannot instantiate DependentSizedArrayType yet");
171 return QualType();
172}
173
Douglas Gregorcd281c32009-02-28 00:25:32 +0000174QualType
175TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
176 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000177 // FIXME: Implement this
178 assert(false && "Cannot instantiate VectorType yet");
179 return QualType();
180}
181
Douglas Gregorcd281c32009-02-28 00:25:32 +0000182QualType
183TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
184 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000185 // FIXME: Implement this
186 assert(false && "Cannot instantiate ExtVectorType yet");
187 return QualType();
188}
189
Douglas Gregorcd281c32009-02-28 00:25:32 +0000190QualType
191TemplateTypeInstantiator::
192InstantiateFunctionProtoType(const FunctionProtoType *T,
193 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000194 // FIXME: Implement this
195 assert(false && "Cannot instantiate FunctionProtoType yet");
196 return QualType();
197}
198
Douglas Gregorcd281c32009-02-28 00:25:32 +0000199QualType
200TemplateTypeInstantiator::
201InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
202 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000203 // FIXME: Implement this
204 assert(false && "Cannot instantiate FunctionNoProtoType yet");
205 return QualType();
206}
207
Douglas Gregorcd281c32009-02-28 00:25:32 +0000208QualType
209TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
210 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000211 // FIXME: Implement this
212 assert(false && "Cannot instantiate TypedefType yet");
213 return QualType();
214}
215
Douglas Gregorcd281c32009-02-28 00:25:32 +0000216QualType
217TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
218 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000219 // FIXME: Implement this
220 assert(false && "Cannot instantiate TypeOfExprType yet");
221 return QualType();
222}
223
Douglas Gregorcd281c32009-02-28 00:25:32 +0000224QualType
225TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
226 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000227 // FIXME: Implement this
228 assert(false && "Cannot instantiate TypeOfType yet");
229 return QualType();
230}
231
Douglas Gregorcd281c32009-02-28 00:25:32 +0000232QualType
233TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
234 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000235 // FIXME: Implement this
236 assert(false && "Cannot instantiate RecordType yet");
237 return QualType();
238}
239
Douglas Gregorcd281c32009-02-28 00:25:32 +0000240QualType
241TemplateTypeInstantiator::InstantiateCXXRecordType(const CXXRecordType *T,
242 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000243 // FIXME: Implement this
244 assert(false && "Cannot instantiate CXXRecordType yet");
245 return QualType();
246}
247
Douglas Gregorcd281c32009-02-28 00:25:32 +0000248QualType
249TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
250 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000251 // FIXME: Implement this
252 assert(false && "Cannot instantiate EnumType yet");
253 return QualType();
254}
255
Douglas Gregorcd281c32009-02-28 00:25:32 +0000256QualType
257TemplateTypeInstantiator::
258InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
259 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000260 if (T->getDepth() == 0) {
261 // Replace the template type parameter with its corresponding
262 // template argument.
263 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
264 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
265 "Template argument kind mismatch");
266 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000267 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000268 return Result;
269
270 // C++ [dcl.ref]p1:
271 // [...] Cv-qualified references are ill-formed except when
272 // the cv-qualifiers are introduced through the use of a
273 // typedef (7.1.3) or of a template type argument (14.3), in
274 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000275 if (Quals && Result->isReferenceType())
276 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000277
Douglas Gregorcd281c32009-02-28 00:25:32 +0000278 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000279 }
280
281 // The template type parameter comes from an inner template (e.g.,
282 // the template parameter list of a member template inside the
283 // template we are instantiating). Create a new template type
284 // parameter with the template "level" reduced by one.
285 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
286 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000287 T->getName())
288 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000289}
290
Douglas Gregorcd281c32009-02-28 00:25:32 +0000291QualType
292TemplateTypeInstantiator::
293InstantiateClassTemplateSpecializationType(
294 const ClassTemplateSpecializationType *T,
295 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000296 // FIXME: Implement this
297 assert(false && "Cannot instantiate ClassTemplateSpecializationType yet");
298 return QualType();
299}
300
Douglas Gregorcd281c32009-02-28 00:25:32 +0000301QualType
302TemplateTypeInstantiator::
303InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
304 unsigned Quals) const {
305 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000306 return QualType();
307}
308
Douglas Gregorcd281c32009-02-28 00:25:32 +0000309QualType
310TemplateTypeInstantiator::
311InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
312 unsigned Quals) const {
313 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000314 return QualType();
315}
316
Douglas Gregorcd281c32009-02-28 00:25:32 +0000317QualType
318TemplateTypeInstantiator::
319InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
320 unsigned Quals) const {
321 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000322 return QualType();
323}
324
Douglas Gregorcd281c32009-02-28 00:25:32 +0000325QualType
326TemplateTypeInstantiator::
327InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
328 unsigned Quals) const {
329 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000330 return QualType();
331}
332
Douglas Gregorcd281c32009-02-28 00:25:32 +0000333/// \brief The actual implementation of Sema::InstantiateType().
334QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
335 // If T is not a dependent type, there is nothing to do.
336 if (!T->isDependentType())
337 return T;
338
339 switch (T->getTypeClass()) {
340#define TYPE(Class, Base) \
341 case Type::Class: \
342 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
343 T.getCVRQualifiers());
344#define ABSTRACT_TYPE(Class, Base)
345#include "clang/AST/TypeNodes.def"
346 }
347
348 assert(false && "Not all types have been decoded for instantiation");
349 return QualType();
350}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000351
352/// \brief Instantiate the type T with a given set of template arguments.
353///
354/// This routine substitutes the given template arguments into the
355/// type T and produces the instantiated type.
356///
357/// \param T the type into which the template arguments will be
358/// substituted. If this type is not dependent, it will be returned
359/// immediately.
360///
361/// \param TemplateArgs the template arguments that will be
362/// substituted for the top-level template parameters within T.
363///
364/// \param NumTemplateArgs the number of template arguments provided
365/// by TemplateArgs.
366///
367/// \param Loc the location in the source code where this substitution
368/// is being performed. It will typically be the location of the
369/// declarator (if we're instantiating the type of some declaration)
370/// or the location of the type in the source code (if, e.g., we're
371/// instantiating the type of a cast expression).
372///
373/// \param Entity the name of the entity associated with a declaration
374/// being instantiated (if any). May be empty to indicate that there
375/// is no such entity (if, e.g., this is a type that occurs as part of
376/// a cast expression) or that the entity has no name (e.g., an
377/// unnamed function parameter).
378///
379/// \returns If the instantiation succeeds, the instantiated
380/// type. Otherwise, produces diagnostics and returns a NULL type.
381QualType Sema::InstantiateType(QualType T,
382 const TemplateArgument *TemplateArgs,
383 unsigned NumTemplateArgs,
384 SourceLocation Loc, DeclarationName Entity) {
385 // If T is not a dependent type, there is nothing to do.
386 if (!T->isDependentType())
387 return T;
388
Douglas Gregorcd281c32009-02-28 00:25:32 +0000389 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
390 Loc, Entity);
391 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000392}