blob: e07892e6c1280b8e32f7013d044cbba9d35052a0 [file] [log] [blame]
Douglas Gregor74296542009-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"
20
21using namespace clang;
22
23//===----------------------------------------------------------------------===/
24// Template Instantiation for Types
25//===----------------------------------------------------------------------===/
26
27static QualType PerformTypeInstantiation(Sema &SemaRef,
28 const ExtQualType *T,
29 unsigned CVR,
30 const TemplateArgument *TemplateArgs,
31 unsigned NumTemplateArgs,
32 SourceLocation Loc,
33 DeclarationName Entity) {
34 // FIXME: Implement this
35 assert(false && "Cannot instantiate ExtQualType yet");
36 return QualType();
37}
38
39static QualType PerformTypeInstantiation(Sema &SemaRef,
40 const BuiltinType *T,
41 unsigned CVR,
42 const TemplateArgument *TemplateArgs,
43 unsigned NumTemplateArgs,
44 SourceLocation Loc,
45 DeclarationName Entity) {
46 assert(false && "BuiltinType is never dependent and cannot be instantiated");
47 return QualType(T, CVR);
48}
49
50static QualType PerformTypeInstantiation(Sema &SemaRef,
51 const FixedWidthIntType *T,
52 unsigned CVR,
53 const TemplateArgument *TemplateArgs,
54 unsigned NumTemplateArgs,
55 SourceLocation Loc,
56 DeclarationName Entity) {
57 // FIXME: Implement this
58 assert(false && "Cannot instantiate FixedWidthIntType yet");
59 return QualType();
60}
61
62static QualType PerformTypeInstantiation(Sema &SemaRef,
63 const ComplexType *T,
64 unsigned CVR,
65 const TemplateArgument *TemplateArgs,
66 unsigned NumTemplateArgs,
67 SourceLocation Loc,
68 DeclarationName Entity) {
69 // FIXME: Implement this
70 assert(false && "Cannot instantiate ComplexType yet");
71 return QualType();
72}
73
74static QualType PerformTypeInstantiation(Sema &SemaRef,
75 const PointerType *T,
76 unsigned CVR,
77 const TemplateArgument *TemplateArgs,
78 unsigned NumTemplateArgs,
79 SourceLocation Loc,
80 DeclarationName Entity) {
81 // FIXME: Implement this
82 assert(false && "Cannot instantiate PointerType yet");
83 return QualType();
84}
85
86static QualType PerformTypeInstantiation(Sema &SemaRef,
87 const BlockPointerType *T,
88 unsigned CVR,
89 const TemplateArgument *TemplateArgs,
90 unsigned NumTemplateArgs,
91 SourceLocation Loc,
92 DeclarationName Entity) {
93 // FIXME: Implement this
94 assert(false && "Cannot instantiate BlockPointerType yet");
95 return QualType();
96}
97
98static QualType PerformTypeInstantiation(Sema &SemaRef,
99 const ReferenceType *T,
100 unsigned CVR,
101 const TemplateArgument *TemplateArgs,
102 unsigned NumTemplateArgs,
103 SourceLocation Loc,
104 DeclarationName Entity) {
105 // FIXME: Implement this
106 assert(false && "Cannot instantiate ReferenceType yet");
107 return QualType();
108}
109
110static QualType PerformTypeInstantiation(Sema &SemaRef,
111 const MemberPointerType *T,
112 unsigned CVR,
113 const TemplateArgument *TemplateArgs,
114 unsigned NumTemplateArgs,
115 SourceLocation Loc,
116 DeclarationName Entity) {
117 // FIXME: Implement this
118 assert(false && "Cannot instantiate MemberPointerType yet");
119 return QualType();
120}
121
122static QualType PerformTypeInstantiation(Sema &SemaRef,
123 const ConstantArrayType *T,
124 unsigned CVR,
125 const TemplateArgument *TemplateArgs,
126 unsigned NumTemplateArgs,
127 SourceLocation Loc,
128 DeclarationName Entity) {
129 // FIXME: Implement this
130 assert(false && "Cannot instantiate ConstantArrayType yet");
131 return QualType();
132}
133
134static QualType PerformTypeInstantiation(Sema &SemaRef,
135 const IncompleteArrayType *T,
136 unsigned CVR,
137 const TemplateArgument *TemplateArgs,
138 unsigned NumTemplateArgs,
139 SourceLocation Loc,
140 DeclarationName Entity) {
141 // FIXME: Implement this
142 assert(false && "Cannot instantiate IncompleteArrayType yet");
143 return QualType();
144}
145
146static QualType PerformTypeInstantiation(Sema &SemaRef,
147 const VariableArrayType *T,
148 unsigned CVR,
149 const TemplateArgument *TemplateArgs,
150 unsigned NumTemplateArgs,
151 SourceLocation Loc,
152 DeclarationName Entity) {
153 // FIXME: Implement this
154 assert(false && "Cannot instantiate VariableArrayType yet");
155 return QualType();
156}
157
158static QualType PerformTypeInstantiation(Sema &SemaRef,
159 const DependentSizedArrayType *T,
160 unsigned CVR,
161 const TemplateArgument *TemplateArgs,
162 unsigned NumTemplateArgs,
163 SourceLocation Loc,
164 DeclarationName Entity) {
165 // FIXME: Implement this
166 assert(false && "Cannot instantiate DependentSizedArrayType yet");
167 return QualType();
168}
169
170static QualType PerformTypeInstantiation(Sema &SemaRef,
171 const VectorType *T,
172 unsigned CVR,
173 const TemplateArgument *TemplateArgs,
174 unsigned NumTemplateArgs,
175 SourceLocation Loc,
176 DeclarationName Entity) {
177 // FIXME: Implement this
178 assert(false && "Cannot instantiate VectorType yet");
179 return QualType();
180}
181
182static QualType PerformTypeInstantiation(Sema &SemaRef,
183 const ExtVectorType *T,
184 unsigned CVR,
185 const TemplateArgument *TemplateArgs,
186 unsigned NumTemplateArgs,
187 SourceLocation Loc,
188 DeclarationName Entity) {
189 // FIXME: Implement this
190 assert(false && "Cannot instantiate ExtVectorType yet");
191 return QualType();
192}
193
194static QualType PerformTypeInstantiation(Sema &SemaRef,
195 const FunctionProtoType *T,
196 unsigned CVR,
197 const TemplateArgument *TemplateArgs,
198 unsigned NumTemplateArgs,
199 SourceLocation Loc,
200 DeclarationName Entity) {
201 // FIXME: Implement this
202 assert(false && "Cannot instantiate FunctionProtoType yet");
203 return QualType();
204}
205
206static QualType PerformTypeInstantiation(Sema &SemaRef,
207 const FunctionNoProtoType *T,
208 unsigned CVR,
209 const TemplateArgument *TemplateArgs,
210 unsigned NumTemplateArgs,
211 SourceLocation Loc,
212 DeclarationName Entity) {
213 // FIXME: Implement this
214 assert(false && "Cannot instantiate FunctionNoProtoType yet");
215 return QualType();
216}
217
218static QualType PerformTypeInstantiation(Sema &SemaRef,
219 const TypedefType *T,
220 unsigned CVR,
221 const TemplateArgument *TemplateArgs,
222 unsigned NumTemplateArgs,
223 SourceLocation Loc,
224 DeclarationName Entity) {
225 // FIXME: Implement this
226 assert(false && "Cannot instantiate TypedefType yet");
227 return QualType();
228}
229
230static QualType PerformTypeInstantiation(Sema &SemaRef,
231 const TypeOfExprType *T,
232 unsigned CVR,
233 const TemplateArgument *TemplateArgs,
234 unsigned NumTemplateArgs,
235 SourceLocation Loc,
236 DeclarationName Entity) {
237 // FIXME: Implement this
238 assert(false && "Cannot instantiate TypeOfExprType yet");
239 return QualType();
240}
241
242static QualType PerformTypeInstantiation(Sema &SemaRef,
243 const TypeOfType *T,
244 unsigned CVR,
245 const TemplateArgument *TemplateArgs,
246 unsigned NumTemplateArgs,
247 SourceLocation Loc,
248 DeclarationName Entity) {
249 // FIXME: Implement this
250 assert(false && "Cannot instantiate TypeOfType yet");
251 return QualType();
252}
253
254static QualType PerformTypeInstantiation(Sema &SemaRef,
255 const RecordType *T,
256 unsigned CVR,
257 const TemplateArgument *TemplateArgs,
258 unsigned NumTemplateArgs,
259 SourceLocation Loc,
260 DeclarationName Entity) {
261 // FIXME: Implement this
262 assert(false && "Cannot instantiate RecordType yet");
263 return QualType();
264}
265
266static QualType PerformTypeInstantiation(Sema &SemaRef,
267 const CXXRecordType *T,
268 unsigned CVR,
269 const TemplateArgument *TemplateArgs,
270 unsigned NumTemplateArgs,
271 SourceLocation Loc,
272 DeclarationName Entity) {
273 // FIXME: Implement this
274 assert(false && "Cannot instantiate CXXRecordType yet");
275 return QualType();
276}
277
278static QualType PerformTypeInstantiation(Sema &SemaRef,
279 const EnumType *T,
280 unsigned CVR,
281 const TemplateArgument *TemplateArgs,
282 unsigned NumTemplateArgs,
283 SourceLocation Loc,
284 DeclarationName Entity) {
285 // FIXME: Implement this
286 assert(false && "Cannot instantiate EnumType yet");
287 return QualType();
288}
289
290static QualType PerformTypeInstantiation(Sema &SemaRef,
291 const TemplateTypeParmType *T,
292 unsigned CVR,
293 const TemplateArgument *TemplateArgs,
294 unsigned NumTemplateArgs,
295 SourceLocation Loc,
296 DeclarationName Entity) {
297 if (T->getDepth() == 0) {
298 // Replace the template type parameter with its corresponding
299 // template argument.
300 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
301 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
302 "Template argument kind mismatch");
303 QualType Result = TemplateArgs[T->getIndex()].getAsType();
304 if (Result.isNull() || !CVR)
305 return Result;
306
307 // C++ [dcl.ref]p1:
308 // [...] Cv-qualified references are ill-formed except when
309 // the cv-qualifiers are introduced through the use of a
310 // typedef (7.1.3) or of a template type argument (14.3), in
311 // which case the cv-qualifiers are ignored.
312 if (CVR && Result->isReferenceType())
313 CVR = 0;
314
315 return QualType(Result.getTypePtr(), CVR | Result.getCVRQualifiers());
316 }
317
318 // The template type parameter comes from an inner template (e.g.,
319 // the template parameter list of a member template inside the
320 // template we are instantiating). Create a new template type
321 // parameter with the template "level" reduced by one.
322 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
323 T->getIndex(),
324 T->getName());
325}
326
327static QualType PerformTypeInstantiation(Sema &SemaRef,
328 const ClassTemplateSpecializationType *T,
329 unsigned CVR,
330 const TemplateArgument *TemplateArgs,
331 unsigned NumTemplateArgs,
332 SourceLocation Loc,
333 DeclarationName Entity) {
334 // FIXME: Implement this
335 assert(false && "Cannot instantiate ClassTemplateSpecializationType yet");
336 return QualType();
337}
338
339static QualType PerformTypeInstantiation(Sema &SemaRef,
340 const ObjCInterfaceType *T,
341 unsigned CVR,
342 const TemplateArgument *TemplateArgs,
343 unsigned NumTemplateArgs,
344 SourceLocation Loc,
345 DeclarationName Entity) {
346 // FIXME: Implement this
347 assert(false && "Cannot instantiate ObjCInterfaceType yet");
348 return QualType();
349}
350
351static QualType PerformTypeInstantiation(Sema &SemaRef,
352 const ObjCQualifiedInterfaceType *T,
353 unsigned CVR,
354 const TemplateArgument *TemplateArgs,
355 unsigned NumTemplateArgs,
356 SourceLocation Loc,
357 DeclarationName Entity) {
358 // FIXME: Implement this
359 assert(false && "Cannot instantiate ObjCQualifiedInterfaceType yet");
360 return QualType();
361}
362
363static QualType PerformTypeInstantiation(Sema &SemaRef,
364 const ObjCQualifiedIdType *T,
365 unsigned CVR,
366 const TemplateArgument *TemplateArgs,
367 unsigned NumTemplateArgs,
368 SourceLocation Loc,
369 DeclarationName Entity) {
370 // FIXME: Implement this
371 assert(false && "Cannot instantiate ObjCQualifiedIdType yet");
372 return QualType();
373}
374
375static QualType PerformTypeInstantiation(Sema &SemaRef,
376 const ObjCQualifiedClassType *T,
377 unsigned CVR,
378 const TemplateArgument *TemplateArgs,
379 unsigned NumTemplateArgs,
380 SourceLocation Loc,
381 DeclarationName Entity) {
382 // FIXME: Implement this
383 assert(false && "Cannot instantiate ObjCQualifiedClassType yet");
384 return QualType();
385}
386
387
388/// \brief Instantiate the type T with a given set of template arguments.
389///
390/// This routine substitutes the given template arguments into the
391/// type T and produces the instantiated type.
392///
393/// \param T the type into which the template arguments will be
394/// substituted. If this type is not dependent, it will be returned
395/// immediately.
396///
397/// \param TemplateArgs the template arguments that will be
398/// substituted for the top-level template parameters within T.
399///
400/// \param NumTemplateArgs the number of template arguments provided
401/// by TemplateArgs.
402///
403/// \param Loc the location in the source code where this substitution
404/// is being performed. It will typically be the location of the
405/// declarator (if we're instantiating the type of some declaration)
406/// or the location of the type in the source code (if, e.g., we're
407/// instantiating the type of a cast expression).
408///
409/// \param Entity the name of the entity associated with a declaration
410/// being instantiated (if any). May be empty to indicate that there
411/// is no such entity (if, e.g., this is a type that occurs as part of
412/// a cast expression) or that the entity has no name (e.g., an
413/// unnamed function parameter).
414///
415/// \returns If the instantiation succeeds, the instantiated
416/// type. Otherwise, produces diagnostics and returns a NULL type.
417QualType Sema::InstantiateType(QualType T,
418 const TemplateArgument *TemplateArgs,
419 unsigned NumTemplateArgs,
420 SourceLocation Loc, DeclarationName Entity) {
421 // If T is not a dependent type, there is nothing to do.
422 if (!T->isDependentType())
423 return T;
424
425 switch (T->getTypeClass()) {
426#define TYPE(Class, Base) \
427 case Type::Class: \
428 return PerformTypeInstantiation(*this, \
429 cast<Class##Type>(T.getTypePtr()), \
430 T.getCVRQualifiers(), TemplateArgs, \
431 NumTemplateArgs, Loc, Entity);
432#define ABSTRACT_TYPE(Class, Base)
433#include "clang/AST/TypeNodes.def"
434 }
435
436 assert(false && "Not all types hav been decided for template instantiation");
437 return QualType();
438}