xref: /minix3/external/bsd/llvm/dist/clang/lib/AST/TemplateBase.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- TemplateBase.cpp - Common template AST class implementation ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements common classes used throughout C++ template
11f4a2713aSLionel Sambuc // representations.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "clang/AST/TemplateBase.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclBase.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
19f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
20f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
21f4a2713aSLionel Sambuc #include "clang/AST/Type.h"
22f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
23f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/FoldingSet.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
27f4a2713aSLionel Sambuc #include <algorithm>
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc using namespace clang;
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc /// \brief Print a template integral argument value.
32f4a2713aSLionel Sambuc ///
33f4a2713aSLionel Sambuc /// \param TemplArg the TemplateArgument instance to print.
34f4a2713aSLionel Sambuc ///
35f4a2713aSLionel Sambuc /// \param Out the raw_ostream instance to use for printing.
36*0a6a1f1dSLionel Sambuc ///
37*0a6a1f1dSLionel Sambuc /// \param Policy the printing policy for EnumConstantDecl printing.
printIntegral(const TemplateArgument & TemplArg,raw_ostream & Out,const PrintingPolicy & Policy)38f4a2713aSLionel Sambuc static void printIntegral(const TemplateArgument &TemplArg,
39*0a6a1f1dSLionel Sambuc                           raw_ostream &Out, const PrintingPolicy& Policy) {
40f4a2713aSLionel Sambuc   const ::clang::Type *T = TemplArg.getIntegralType().getTypePtr();
41f4a2713aSLionel Sambuc   const llvm::APSInt &Val = TemplArg.getAsIntegral();
42f4a2713aSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc   if (const EnumType *ET = T->getAs<EnumType>()) {
44*0a6a1f1dSLionel Sambuc     for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) {
45*0a6a1f1dSLionel Sambuc       // In Sema::CheckTemplateArugment, enum template arguments value are
46*0a6a1f1dSLionel Sambuc       // extended to the size of the integer underlying the enum type.  This
47*0a6a1f1dSLionel Sambuc       // may create a size difference between the enum value and template
48*0a6a1f1dSLionel Sambuc       // argument value, requiring isSameValue here instead of operator==.
49*0a6a1f1dSLionel Sambuc       if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
50*0a6a1f1dSLionel Sambuc         ECD->printQualifiedName(Out, Policy);
51*0a6a1f1dSLionel Sambuc         return;
52*0a6a1f1dSLionel Sambuc       }
53*0a6a1f1dSLionel Sambuc     }
54*0a6a1f1dSLionel Sambuc   }
55*0a6a1f1dSLionel Sambuc 
56f4a2713aSLionel Sambuc   if (T->isBooleanType()) {
57f4a2713aSLionel Sambuc     Out << (Val.getBoolValue() ? "true" : "false");
58f4a2713aSLionel Sambuc   } else if (T->isCharType()) {
59f4a2713aSLionel Sambuc     const char Ch = Val.getZExtValue();
60f4a2713aSLionel Sambuc     Out << ((Ch == '\'') ? "'\\" : "'");
61f4a2713aSLionel Sambuc     Out.write_escaped(StringRef(&Ch, 1), /*UseHexEscapes=*/ true);
62f4a2713aSLionel Sambuc     Out << "'";
63f4a2713aSLionel Sambuc   } else {
64f4a2713aSLionel Sambuc     Out << Val;
65f4a2713aSLionel Sambuc   }
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
69f4a2713aSLionel Sambuc // TemplateArgument Implementation
70f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
71f4a2713aSLionel Sambuc 
TemplateArgument(ASTContext & Ctx,const llvm::APSInt & Value,QualType Type)72f4a2713aSLionel Sambuc TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value,
73f4a2713aSLionel Sambuc                                    QualType Type) {
74f4a2713aSLionel Sambuc   Integer.Kind = Integral;
75f4a2713aSLionel Sambuc   // Copy the APSInt value into our decomposed form.
76f4a2713aSLionel Sambuc   Integer.BitWidth = Value.getBitWidth();
77f4a2713aSLionel Sambuc   Integer.IsUnsigned = Value.isUnsigned();
78f4a2713aSLionel Sambuc   // If the value is large, we have to get additional memory from the ASTContext
79f4a2713aSLionel Sambuc   unsigned NumWords = Value.getNumWords();
80f4a2713aSLionel Sambuc   if (NumWords > 1) {
81f4a2713aSLionel Sambuc     void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t));
82f4a2713aSLionel Sambuc     std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t));
83f4a2713aSLionel Sambuc     Integer.pVal = static_cast<uint64_t *>(Mem);
84f4a2713aSLionel Sambuc   } else {
85f4a2713aSLionel Sambuc     Integer.VAL = Value.getZExtValue();
86f4a2713aSLionel Sambuc   }
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc   Integer.Type = Type.getAsOpaquePtr();
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc 
CreatePackCopy(ASTContext & Context,const TemplateArgument * Args,unsigned NumArgs)91f4a2713aSLionel Sambuc TemplateArgument TemplateArgument::CreatePackCopy(ASTContext &Context,
92f4a2713aSLionel Sambuc                                                   const TemplateArgument *Args,
93f4a2713aSLionel Sambuc                                                   unsigned NumArgs) {
94f4a2713aSLionel Sambuc   if (NumArgs == 0)
95f4a2713aSLionel Sambuc     return getEmptyPack();
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   TemplateArgument *Storage = new (Context) TemplateArgument [NumArgs];
98f4a2713aSLionel Sambuc   std::copy(Args, Args + NumArgs, Storage);
99f4a2713aSLionel Sambuc   return TemplateArgument(Storage, NumArgs);
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc 
isDependent() const102f4a2713aSLionel Sambuc bool TemplateArgument::isDependent() const {
103f4a2713aSLionel Sambuc   switch (getKind()) {
104f4a2713aSLionel Sambuc   case Null:
105f4a2713aSLionel Sambuc     llvm_unreachable("Should not have a NULL template argument");
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc   case Type:
108*0a6a1f1dSLionel Sambuc     return getAsType()->isDependentType() ||
109*0a6a1f1dSLionel Sambuc            isa<PackExpansionType>(getAsType());
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc   case Template:
112f4a2713aSLionel Sambuc     return getAsTemplate().isDependent();
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   case TemplateExpansion:
115f4a2713aSLionel Sambuc     return true;
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc   case Declaration:
118f4a2713aSLionel Sambuc     if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
119f4a2713aSLionel Sambuc       return DC->isDependentContext();
120f4a2713aSLionel Sambuc     return getAsDecl()->getDeclContext()->isDependentContext();
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   case NullPtr:
123f4a2713aSLionel Sambuc     return false;
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc   case Integral:
126f4a2713aSLionel Sambuc     // Never dependent
127f4a2713aSLionel Sambuc     return false;
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   case Expression:
130*0a6a1f1dSLionel Sambuc     return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent() ||
131*0a6a1f1dSLionel Sambuc             isa<PackExpansionExpr>(getAsExpr()));
132f4a2713aSLionel Sambuc 
133f4a2713aSLionel Sambuc   case Pack:
134*0a6a1f1dSLionel Sambuc     for (const auto &P : pack_elements())
135*0a6a1f1dSLionel Sambuc       if (P.isDependent())
136f4a2713aSLionel Sambuc         return true;
137f4a2713aSLionel Sambuc     return false;
138f4a2713aSLionel Sambuc   }
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
141f4a2713aSLionel Sambuc }
142f4a2713aSLionel Sambuc 
isInstantiationDependent() const143f4a2713aSLionel Sambuc bool TemplateArgument::isInstantiationDependent() const {
144f4a2713aSLionel Sambuc   switch (getKind()) {
145f4a2713aSLionel Sambuc   case Null:
146f4a2713aSLionel Sambuc     llvm_unreachable("Should not have a NULL template argument");
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc   case Type:
149f4a2713aSLionel Sambuc     return getAsType()->isInstantiationDependentType();
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   case Template:
152f4a2713aSLionel Sambuc     return getAsTemplate().isInstantiationDependent();
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc   case TemplateExpansion:
155f4a2713aSLionel Sambuc     return true;
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   case Declaration:
158f4a2713aSLionel Sambuc     if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
159f4a2713aSLionel Sambuc       return DC->isDependentContext();
160f4a2713aSLionel Sambuc     return getAsDecl()->getDeclContext()->isDependentContext();
161f4a2713aSLionel Sambuc 
162f4a2713aSLionel Sambuc   case NullPtr:
163f4a2713aSLionel Sambuc     return false;
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc   case Integral:
166f4a2713aSLionel Sambuc     // Never dependent
167f4a2713aSLionel Sambuc     return false;
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc   case Expression:
170f4a2713aSLionel Sambuc     return getAsExpr()->isInstantiationDependent();
171f4a2713aSLionel Sambuc 
172f4a2713aSLionel Sambuc   case Pack:
173*0a6a1f1dSLionel Sambuc     for (const auto &P : pack_elements())
174*0a6a1f1dSLionel Sambuc       if (P.isInstantiationDependent())
175f4a2713aSLionel Sambuc         return true;
176f4a2713aSLionel Sambuc     return false;
177f4a2713aSLionel Sambuc   }
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
180f4a2713aSLionel Sambuc }
181f4a2713aSLionel Sambuc 
isPackExpansion() const182f4a2713aSLionel Sambuc bool TemplateArgument::isPackExpansion() const {
183f4a2713aSLionel Sambuc   switch (getKind()) {
184f4a2713aSLionel Sambuc   case Null:
185f4a2713aSLionel Sambuc   case Declaration:
186f4a2713aSLionel Sambuc   case Integral:
187f4a2713aSLionel Sambuc   case Pack:
188f4a2713aSLionel Sambuc   case Template:
189f4a2713aSLionel Sambuc   case NullPtr:
190f4a2713aSLionel Sambuc     return false;
191f4a2713aSLionel Sambuc 
192f4a2713aSLionel Sambuc   case TemplateExpansion:
193f4a2713aSLionel Sambuc     return true;
194f4a2713aSLionel Sambuc 
195f4a2713aSLionel Sambuc   case Type:
196f4a2713aSLionel Sambuc     return isa<PackExpansionType>(getAsType());
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   case Expression:
199f4a2713aSLionel Sambuc     return isa<PackExpansionExpr>(getAsExpr());
200f4a2713aSLionel Sambuc   }
201f4a2713aSLionel Sambuc 
202f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
203f4a2713aSLionel Sambuc }
204f4a2713aSLionel Sambuc 
containsUnexpandedParameterPack() const205f4a2713aSLionel Sambuc bool TemplateArgument::containsUnexpandedParameterPack() const {
206f4a2713aSLionel Sambuc   switch (getKind()) {
207f4a2713aSLionel Sambuc   case Null:
208f4a2713aSLionel Sambuc   case Declaration:
209f4a2713aSLionel Sambuc   case Integral:
210f4a2713aSLionel Sambuc   case TemplateExpansion:
211f4a2713aSLionel Sambuc   case NullPtr:
212f4a2713aSLionel Sambuc     break;
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc   case Type:
215f4a2713aSLionel Sambuc     if (getAsType()->containsUnexpandedParameterPack())
216f4a2713aSLionel Sambuc       return true;
217f4a2713aSLionel Sambuc     break;
218f4a2713aSLionel Sambuc 
219f4a2713aSLionel Sambuc   case Template:
220f4a2713aSLionel Sambuc     if (getAsTemplate().containsUnexpandedParameterPack())
221f4a2713aSLionel Sambuc       return true;
222f4a2713aSLionel Sambuc     break;
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc   case Expression:
225f4a2713aSLionel Sambuc     if (getAsExpr()->containsUnexpandedParameterPack())
226f4a2713aSLionel Sambuc       return true;
227f4a2713aSLionel Sambuc     break;
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc   case Pack:
230*0a6a1f1dSLionel Sambuc     for (const auto &P : pack_elements())
231*0a6a1f1dSLionel Sambuc       if (P.containsUnexpandedParameterPack())
232f4a2713aSLionel Sambuc         return true;
233f4a2713aSLionel Sambuc 
234f4a2713aSLionel Sambuc     break;
235f4a2713aSLionel Sambuc   }
236f4a2713aSLionel Sambuc 
237f4a2713aSLionel Sambuc   return false;
238f4a2713aSLionel Sambuc }
239f4a2713aSLionel Sambuc 
getNumTemplateExpansions() const240f4a2713aSLionel Sambuc Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
241f4a2713aSLionel Sambuc   assert(getKind() == TemplateExpansion);
242f4a2713aSLionel Sambuc   if (TemplateArg.NumExpansions)
243f4a2713aSLionel Sambuc     return TemplateArg.NumExpansions - 1;
244f4a2713aSLionel Sambuc 
245f4a2713aSLionel Sambuc   return None;
246f4a2713aSLionel Sambuc }
247f4a2713aSLionel Sambuc 
Profile(llvm::FoldingSetNodeID & ID,const ASTContext & Context) const248f4a2713aSLionel Sambuc void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
249f4a2713aSLionel Sambuc                                const ASTContext &Context) const {
250f4a2713aSLionel Sambuc   ID.AddInteger(getKind());
251f4a2713aSLionel Sambuc   switch (getKind()) {
252f4a2713aSLionel Sambuc   case Null:
253f4a2713aSLionel Sambuc     break;
254f4a2713aSLionel Sambuc 
255f4a2713aSLionel Sambuc   case Type:
256f4a2713aSLionel Sambuc     getAsType().Profile(ID);
257f4a2713aSLionel Sambuc     break;
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc   case NullPtr:
260f4a2713aSLionel Sambuc     getNullPtrType().Profile(ID);
261f4a2713aSLionel Sambuc     break;
262f4a2713aSLionel Sambuc 
263f4a2713aSLionel Sambuc   case Declaration:
264*0a6a1f1dSLionel Sambuc     ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : nullptr);
265f4a2713aSLionel Sambuc     break;
266f4a2713aSLionel Sambuc 
267f4a2713aSLionel Sambuc   case Template:
268f4a2713aSLionel Sambuc   case TemplateExpansion: {
269f4a2713aSLionel Sambuc     TemplateName Template = getAsTemplateOrTemplatePattern();
270f4a2713aSLionel Sambuc     if (TemplateTemplateParmDecl *TTP
271f4a2713aSLionel Sambuc           = dyn_cast_or_null<TemplateTemplateParmDecl>(
272f4a2713aSLionel Sambuc                                                 Template.getAsTemplateDecl())) {
273f4a2713aSLionel Sambuc       ID.AddBoolean(true);
274f4a2713aSLionel Sambuc       ID.AddInteger(TTP->getDepth());
275f4a2713aSLionel Sambuc       ID.AddInteger(TTP->getPosition());
276f4a2713aSLionel Sambuc       ID.AddBoolean(TTP->isParameterPack());
277f4a2713aSLionel Sambuc     } else {
278f4a2713aSLionel Sambuc       ID.AddBoolean(false);
279f4a2713aSLionel Sambuc       ID.AddPointer(Context.getCanonicalTemplateName(Template)
280f4a2713aSLionel Sambuc                                                           .getAsVoidPointer());
281f4a2713aSLionel Sambuc     }
282f4a2713aSLionel Sambuc     break;
283f4a2713aSLionel Sambuc   }
284f4a2713aSLionel Sambuc 
285f4a2713aSLionel Sambuc   case Integral:
286f4a2713aSLionel Sambuc     getAsIntegral().Profile(ID);
287f4a2713aSLionel Sambuc     getIntegralType().Profile(ID);
288f4a2713aSLionel Sambuc     break;
289f4a2713aSLionel Sambuc 
290f4a2713aSLionel Sambuc   case Expression:
291f4a2713aSLionel Sambuc     getAsExpr()->Profile(ID, Context, true);
292f4a2713aSLionel Sambuc     break;
293f4a2713aSLionel Sambuc 
294f4a2713aSLionel Sambuc   case Pack:
295f4a2713aSLionel Sambuc     ID.AddInteger(Args.NumArgs);
296f4a2713aSLionel Sambuc     for (unsigned I = 0; I != Args.NumArgs; ++I)
297f4a2713aSLionel Sambuc       Args.Args[I].Profile(ID, Context);
298f4a2713aSLionel Sambuc   }
299f4a2713aSLionel Sambuc }
300f4a2713aSLionel Sambuc 
structurallyEquals(const TemplateArgument & Other) const301f4a2713aSLionel Sambuc bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
302f4a2713aSLionel Sambuc   if (getKind() != Other.getKind()) return false;
303f4a2713aSLionel Sambuc 
304f4a2713aSLionel Sambuc   switch (getKind()) {
305f4a2713aSLionel Sambuc   case Null:
306f4a2713aSLionel Sambuc   case Type:
307f4a2713aSLionel Sambuc   case Expression:
308f4a2713aSLionel Sambuc   case Template:
309f4a2713aSLionel Sambuc   case TemplateExpansion:
310f4a2713aSLionel Sambuc   case NullPtr:
311f4a2713aSLionel Sambuc     return TypeOrValue.V == Other.TypeOrValue.V;
312f4a2713aSLionel Sambuc 
313f4a2713aSLionel Sambuc   case Declaration:
314*0a6a1f1dSLionel Sambuc     return getAsDecl() == Other.getAsDecl();
315f4a2713aSLionel Sambuc 
316f4a2713aSLionel Sambuc   case Integral:
317f4a2713aSLionel Sambuc     return getIntegralType() == Other.getIntegralType() &&
318f4a2713aSLionel Sambuc            getAsIntegral() == Other.getAsIntegral();
319f4a2713aSLionel Sambuc 
320f4a2713aSLionel Sambuc   case Pack:
321f4a2713aSLionel Sambuc     if (Args.NumArgs != Other.Args.NumArgs) return false;
322f4a2713aSLionel Sambuc     for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
323f4a2713aSLionel Sambuc       if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
324f4a2713aSLionel Sambuc         return false;
325f4a2713aSLionel Sambuc     return true;
326f4a2713aSLionel Sambuc   }
327f4a2713aSLionel Sambuc 
328f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
329f4a2713aSLionel Sambuc }
330f4a2713aSLionel Sambuc 
getPackExpansionPattern() const331f4a2713aSLionel Sambuc TemplateArgument TemplateArgument::getPackExpansionPattern() const {
332f4a2713aSLionel Sambuc   assert(isPackExpansion());
333f4a2713aSLionel Sambuc 
334f4a2713aSLionel Sambuc   switch (getKind()) {
335f4a2713aSLionel Sambuc   case Type:
336f4a2713aSLionel Sambuc     return getAsType()->getAs<PackExpansionType>()->getPattern();
337f4a2713aSLionel Sambuc 
338f4a2713aSLionel Sambuc   case Expression:
339f4a2713aSLionel Sambuc     return cast<PackExpansionExpr>(getAsExpr())->getPattern();
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc   case TemplateExpansion:
342f4a2713aSLionel Sambuc     return TemplateArgument(getAsTemplateOrTemplatePattern());
343f4a2713aSLionel Sambuc 
344f4a2713aSLionel Sambuc   case Declaration:
345f4a2713aSLionel Sambuc   case Integral:
346f4a2713aSLionel Sambuc   case Pack:
347f4a2713aSLionel Sambuc   case Null:
348f4a2713aSLionel Sambuc   case Template:
349f4a2713aSLionel Sambuc   case NullPtr:
350f4a2713aSLionel Sambuc     return TemplateArgument();
351f4a2713aSLionel Sambuc   }
352f4a2713aSLionel Sambuc 
353f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc 
print(const PrintingPolicy & Policy,raw_ostream & Out) const356f4a2713aSLionel Sambuc void TemplateArgument::print(const PrintingPolicy &Policy,
357f4a2713aSLionel Sambuc                              raw_ostream &Out) const {
358f4a2713aSLionel Sambuc   switch (getKind()) {
359f4a2713aSLionel Sambuc   case Null:
360*0a6a1f1dSLionel Sambuc     Out << "(no value)";
361f4a2713aSLionel Sambuc     break;
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   case Type: {
364f4a2713aSLionel Sambuc     PrintingPolicy SubPolicy(Policy);
365f4a2713aSLionel Sambuc     SubPolicy.SuppressStrongLifetime = true;
366f4a2713aSLionel Sambuc     getAsType().print(Out, SubPolicy);
367f4a2713aSLionel Sambuc     break;
368f4a2713aSLionel Sambuc   }
369f4a2713aSLionel Sambuc 
370f4a2713aSLionel Sambuc   case Declaration: {
371f4a2713aSLionel Sambuc     NamedDecl *ND = cast<NamedDecl>(getAsDecl());
372f4a2713aSLionel Sambuc     Out << '&';
373f4a2713aSLionel Sambuc     if (ND->getDeclName()) {
374f4a2713aSLionel Sambuc       // FIXME: distinguish between pointer and reference args?
375f4a2713aSLionel Sambuc       ND->printQualifiedName(Out);
376f4a2713aSLionel Sambuc     } else {
377*0a6a1f1dSLionel Sambuc       Out << "(anonymous)";
378f4a2713aSLionel Sambuc     }
379f4a2713aSLionel Sambuc     break;
380f4a2713aSLionel Sambuc   }
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   case NullPtr:
383f4a2713aSLionel Sambuc     Out << "nullptr";
384f4a2713aSLionel Sambuc     break;
385f4a2713aSLionel Sambuc 
386f4a2713aSLionel Sambuc   case Template:
387f4a2713aSLionel Sambuc     getAsTemplate().print(Out, Policy);
388f4a2713aSLionel Sambuc     break;
389f4a2713aSLionel Sambuc 
390f4a2713aSLionel Sambuc   case TemplateExpansion:
391f4a2713aSLionel Sambuc     getAsTemplateOrTemplatePattern().print(Out, Policy);
392f4a2713aSLionel Sambuc     Out << "...";
393f4a2713aSLionel Sambuc     break;
394f4a2713aSLionel Sambuc 
395f4a2713aSLionel Sambuc   case Integral: {
396*0a6a1f1dSLionel Sambuc     printIntegral(*this, Out, Policy);
397f4a2713aSLionel Sambuc     break;
398f4a2713aSLionel Sambuc   }
399f4a2713aSLionel Sambuc 
400f4a2713aSLionel Sambuc   case Expression:
401*0a6a1f1dSLionel Sambuc     getAsExpr()->printPretty(Out, nullptr, Policy);
402f4a2713aSLionel Sambuc     break;
403f4a2713aSLionel Sambuc 
404f4a2713aSLionel Sambuc   case Pack:
405f4a2713aSLionel Sambuc     Out << "<";
406f4a2713aSLionel Sambuc     bool First = true;
407*0a6a1f1dSLionel Sambuc     for (const auto &P : pack_elements()) {
408f4a2713aSLionel Sambuc       if (First)
409f4a2713aSLionel Sambuc         First = false;
410f4a2713aSLionel Sambuc       else
411f4a2713aSLionel Sambuc         Out << ", ";
412f4a2713aSLionel Sambuc 
413*0a6a1f1dSLionel Sambuc       P.print(Policy, Out);
414f4a2713aSLionel Sambuc     }
415f4a2713aSLionel Sambuc     Out << ">";
416f4a2713aSLionel Sambuc     break;
417f4a2713aSLionel Sambuc   }
418f4a2713aSLionel Sambuc }
419f4a2713aSLionel Sambuc 
420f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
421f4a2713aSLionel Sambuc // TemplateArgumentLoc Implementation
422f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
423f4a2713aSLionel Sambuc 
TemplateArgumentLocInfo()424f4a2713aSLionel Sambuc TemplateArgumentLocInfo::TemplateArgumentLocInfo() {
425f4a2713aSLionel Sambuc   memset((void*)this, 0, sizeof(TemplateArgumentLocInfo));
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc 
getSourceRange() const428f4a2713aSLionel Sambuc SourceRange TemplateArgumentLoc::getSourceRange() const {
429f4a2713aSLionel Sambuc   switch (Argument.getKind()) {
430f4a2713aSLionel Sambuc   case TemplateArgument::Expression:
431f4a2713aSLionel Sambuc     return getSourceExpression()->getSourceRange();
432f4a2713aSLionel Sambuc 
433f4a2713aSLionel Sambuc   case TemplateArgument::Declaration:
434f4a2713aSLionel Sambuc     return getSourceDeclExpression()->getSourceRange();
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr:
437f4a2713aSLionel Sambuc     return getSourceNullPtrExpression()->getSourceRange();
438f4a2713aSLionel Sambuc 
439f4a2713aSLionel Sambuc   case TemplateArgument::Type:
440f4a2713aSLionel Sambuc     if (TypeSourceInfo *TSI = getTypeSourceInfo())
441f4a2713aSLionel Sambuc       return TSI->getTypeLoc().getSourceRange();
442f4a2713aSLionel Sambuc     else
443f4a2713aSLionel Sambuc       return SourceRange();
444f4a2713aSLionel Sambuc 
445f4a2713aSLionel Sambuc   case TemplateArgument::Template:
446f4a2713aSLionel Sambuc     if (getTemplateQualifierLoc())
447f4a2713aSLionel Sambuc       return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
448f4a2713aSLionel Sambuc                          getTemplateNameLoc());
449f4a2713aSLionel Sambuc     return SourceRange(getTemplateNameLoc());
450f4a2713aSLionel Sambuc 
451f4a2713aSLionel Sambuc   case TemplateArgument::TemplateExpansion:
452f4a2713aSLionel Sambuc     if (getTemplateQualifierLoc())
453f4a2713aSLionel Sambuc       return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
454f4a2713aSLionel Sambuc                          getTemplateEllipsisLoc());
455f4a2713aSLionel Sambuc     return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
456f4a2713aSLionel Sambuc 
457f4a2713aSLionel Sambuc   case TemplateArgument::Integral:
458f4a2713aSLionel Sambuc     return getSourceIntegralExpression()->getSourceRange();
459f4a2713aSLionel Sambuc 
460f4a2713aSLionel Sambuc   case TemplateArgument::Pack:
461f4a2713aSLionel Sambuc   case TemplateArgument::Null:
462f4a2713aSLionel Sambuc     return SourceRange();
463f4a2713aSLionel Sambuc   }
464f4a2713aSLionel Sambuc 
465f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
466f4a2713aSLionel Sambuc }
467f4a2713aSLionel Sambuc 
operator <<(const DiagnosticBuilder & DB,const TemplateArgument & Arg)468f4a2713aSLionel Sambuc const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
469f4a2713aSLionel Sambuc                                            const TemplateArgument &Arg) {
470f4a2713aSLionel Sambuc   switch (Arg.getKind()) {
471f4a2713aSLionel Sambuc   case TemplateArgument::Null:
472f4a2713aSLionel Sambuc     // This is bad, but not as bad as crashing because of argument
473f4a2713aSLionel Sambuc     // count mismatches.
474f4a2713aSLionel Sambuc     return DB << "(null template argument)";
475f4a2713aSLionel Sambuc 
476f4a2713aSLionel Sambuc   case TemplateArgument::Type:
477f4a2713aSLionel Sambuc     return DB << Arg.getAsType();
478f4a2713aSLionel Sambuc 
479f4a2713aSLionel Sambuc   case TemplateArgument::Declaration:
480f4a2713aSLionel Sambuc     return DB << Arg.getAsDecl();
481f4a2713aSLionel Sambuc 
482f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr:
483f4a2713aSLionel Sambuc     return DB << "nullptr";
484f4a2713aSLionel Sambuc 
485f4a2713aSLionel Sambuc   case TemplateArgument::Integral:
486f4a2713aSLionel Sambuc     return DB << Arg.getAsIntegral().toString(10);
487f4a2713aSLionel Sambuc 
488f4a2713aSLionel Sambuc   case TemplateArgument::Template:
489f4a2713aSLionel Sambuc     return DB << Arg.getAsTemplate();
490f4a2713aSLionel Sambuc 
491f4a2713aSLionel Sambuc   case TemplateArgument::TemplateExpansion:
492f4a2713aSLionel Sambuc     return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
493f4a2713aSLionel Sambuc 
494f4a2713aSLionel Sambuc   case TemplateArgument::Expression: {
495f4a2713aSLionel Sambuc     // This shouldn't actually ever happen, so it's okay that we're
496f4a2713aSLionel Sambuc     // regurgitating an expression here.
497f4a2713aSLionel Sambuc     // FIXME: We're guessing at LangOptions!
498f4a2713aSLionel Sambuc     SmallString<32> Str;
499f4a2713aSLionel Sambuc     llvm::raw_svector_ostream OS(Str);
500f4a2713aSLionel Sambuc     LangOptions LangOpts;
501f4a2713aSLionel Sambuc     LangOpts.CPlusPlus = true;
502f4a2713aSLionel Sambuc     PrintingPolicy Policy(LangOpts);
503*0a6a1f1dSLionel Sambuc     Arg.getAsExpr()->printPretty(OS, nullptr, Policy);
504f4a2713aSLionel Sambuc     return DB << OS.str();
505f4a2713aSLionel Sambuc   }
506f4a2713aSLionel Sambuc 
507f4a2713aSLionel Sambuc   case TemplateArgument::Pack: {
508f4a2713aSLionel Sambuc     // FIXME: We're guessing at LangOptions!
509f4a2713aSLionel Sambuc     SmallString<32> Str;
510f4a2713aSLionel Sambuc     llvm::raw_svector_ostream OS(Str);
511f4a2713aSLionel Sambuc     LangOptions LangOpts;
512f4a2713aSLionel Sambuc     LangOpts.CPlusPlus = true;
513f4a2713aSLionel Sambuc     PrintingPolicy Policy(LangOpts);
514f4a2713aSLionel Sambuc     Arg.print(Policy, OS);
515f4a2713aSLionel Sambuc     return DB << OS.str();
516f4a2713aSLionel Sambuc   }
517f4a2713aSLionel Sambuc   }
518f4a2713aSLionel Sambuc 
519f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
520f4a2713aSLionel Sambuc }
521f4a2713aSLionel Sambuc 
522f4a2713aSLionel Sambuc const ASTTemplateArgumentListInfo *
Create(ASTContext & C,const TemplateArgumentListInfo & List)523f4a2713aSLionel Sambuc ASTTemplateArgumentListInfo::Create(ASTContext &C,
524f4a2713aSLionel Sambuc                                     const TemplateArgumentListInfo &List) {
525*0a6a1f1dSLionel Sambuc   assert(llvm::alignOf<ASTTemplateArgumentListInfo>() >=
526*0a6a1f1dSLionel Sambuc          llvm::alignOf<TemplateArgumentLoc>());
527f4a2713aSLionel Sambuc   std::size_t size = ASTTemplateArgumentListInfo::sizeFor(List.size());
528f4a2713aSLionel Sambuc   void *Mem = C.Allocate(size, llvm::alignOf<ASTTemplateArgumentListInfo>());
529f4a2713aSLionel Sambuc   ASTTemplateArgumentListInfo *TAI = new (Mem) ASTTemplateArgumentListInfo();
530f4a2713aSLionel Sambuc   TAI->initializeFrom(List);
531f4a2713aSLionel Sambuc   return TAI;
532f4a2713aSLionel Sambuc }
533f4a2713aSLionel Sambuc 
initializeFrom(const TemplateArgumentListInfo & Info)534f4a2713aSLionel Sambuc void ASTTemplateArgumentListInfo::initializeFrom(
535f4a2713aSLionel Sambuc                                       const TemplateArgumentListInfo &Info) {
536f4a2713aSLionel Sambuc   LAngleLoc = Info.getLAngleLoc();
537f4a2713aSLionel Sambuc   RAngleLoc = Info.getRAngleLoc();
538f4a2713aSLionel Sambuc   NumTemplateArgs = Info.size();
539f4a2713aSLionel Sambuc 
540f4a2713aSLionel Sambuc   TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
541f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumTemplateArgs; ++i)
542f4a2713aSLionel Sambuc     new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
543f4a2713aSLionel Sambuc }
544f4a2713aSLionel Sambuc 
initializeFrom(const TemplateArgumentListInfo & Info,bool & Dependent,bool & InstantiationDependent,bool & ContainsUnexpandedParameterPack)545f4a2713aSLionel Sambuc void ASTTemplateArgumentListInfo::initializeFrom(
546f4a2713aSLionel Sambuc                                           const TemplateArgumentListInfo &Info,
547f4a2713aSLionel Sambuc                                                   bool &Dependent,
548f4a2713aSLionel Sambuc                                                   bool &InstantiationDependent,
549f4a2713aSLionel Sambuc                                        bool &ContainsUnexpandedParameterPack) {
550f4a2713aSLionel Sambuc   LAngleLoc = Info.getLAngleLoc();
551f4a2713aSLionel Sambuc   RAngleLoc = Info.getRAngleLoc();
552f4a2713aSLionel Sambuc   NumTemplateArgs = Info.size();
553f4a2713aSLionel Sambuc 
554f4a2713aSLionel Sambuc   TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
555f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumTemplateArgs; ++i) {
556f4a2713aSLionel Sambuc     Dependent = Dependent || Info[i].getArgument().isDependent();
557f4a2713aSLionel Sambuc     InstantiationDependent = InstantiationDependent ||
558f4a2713aSLionel Sambuc                              Info[i].getArgument().isInstantiationDependent();
559f4a2713aSLionel Sambuc     ContainsUnexpandedParameterPack
560f4a2713aSLionel Sambuc       = ContainsUnexpandedParameterPack ||
561f4a2713aSLionel Sambuc         Info[i].getArgument().containsUnexpandedParameterPack();
562f4a2713aSLionel Sambuc 
563f4a2713aSLionel Sambuc     new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
564f4a2713aSLionel Sambuc   }
565f4a2713aSLionel Sambuc }
566f4a2713aSLionel Sambuc 
copyInto(TemplateArgumentListInfo & Info) const567f4a2713aSLionel Sambuc void ASTTemplateArgumentListInfo::copyInto(
568f4a2713aSLionel Sambuc                                       TemplateArgumentListInfo &Info) const {
569f4a2713aSLionel Sambuc   Info.setLAngleLoc(LAngleLoc);
570f4a2713aSLionel Sambuc   Info.setRAngleLoc(RAngleLoc);
571f4a2713aSLionel Sambuc   for (unsigned I = 0; I != NumTemplateArgs; ++I)
572f4a2713aSLionel Sambuc     Info.addArgument(getTemplateArgs()[I]);
573f4a2713aSLionel Sambuc }
574f4a2713aSLionel Sambuc 
sizeFor(unsigned NumTemplateArgs)575f4a2713aSLionel Sambuc std::size_t ASTTemplateArgumentListInfo::sizeFor(unsigned NumTemplateArgs) {
576f4a2713aSLionel Sambuc   return sizeof(ASTTemplateArgumentListInfo) +
577f4a2713aSLionel Sambuc          sizeof(TemplateArgumentLoc) * NumTemplateArgs;
578f4a2713aSLionel Sambuc }
579f4a2713aSLionel Sambuc 
580f4a2713aSLionel Sambuc void
initializeFrom(SourceLocation TemplateKWLoc,const TemplateArgumentListInfo & Info)581f4a2713aSLionel Sambuc ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc,
582f4a2713aSLionel Sambuc                                          const TemplateArgumentListInfo &Info) {
583f4a2713aSLionel Sambuc   Base::initializeFrom(Info);
584f4a2713aSLionel Sambuc   setTemplateKeywordLoc(TemplateKWLoc);
585f4a2713aSLionel Sambuc }
586f4a2713aSLionel Sambuc 
587f4a2713aSLionel Sambuc void
588f4a2713aSLionel Sambuc ASTTemplateKWAndArgsInfo
initializeFrom(SourceLocation TemplateKWLoc,const TemplateArgumentListInfo & Info,bool & Dependent,bool & InstantiationDependent,bool & ContainsUnexpandedParameterPack)589f4a2713aSLionel Sambuc ::initializeFrom(SourceLocation TemplateKWLoc,
590f4a2713aSLionel Sambuc                  const TemplateArgumentListInfo &Info,
591f4a2713aSLionel Sambuc                  bool &Dependent,
592f4a2713aSLionel Sambuc                  bool &InstantiationDependent,
593f4a2713aSLionel Sambuc                  bool &ContainsUnexpandedParameterPack) {
594f4a2713aSLionel Sambuc   Base::initializeFrom(Info, Dependent, InstantiationDependent,
595f4a2713aSLionel Sambuc                        ContainsUnexpandedParameterPack);
596f4a2713aSLionel Sambuc   setTemplateKeywordLoc(TemplateKWLoc);
597f4a2713aSLionel Sambuc }
598f4a2713aSLionel Sambuc 
599f4a2713aSLionel Sambuc void
initializeFrom(SourceLocation TemplateKWLoc)600f4a2713aSLionel Sambuc ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) {
601f4a2713aSLionel Sambuc   // No explicit template arguments, but template keyword loc is valid.
602f4a2713aSLionel Sambuc   assert(TemplateKWLoc.isValid());
603f4a2713aSLionel Sambuc   LAngleLoc = SourceLocation();
604f4a2713aSLionel Sambuc   RAngleLoc = SourceLocation();
605f4a2713aSLionel Sambuc   NumTemplateArgs = 0;
606f4a2713aSLionel Sambuc   setTemplateKeywordLoc(TemplateKWLoc);
607f4a2713aSLionel Sambuc }
608f4a2713aSLionel Sambuc 
609f4a2713aSLionel Sambuc std::size_t
sizeFor(unsigned NumTemplateArgs)610f4a2713aSLionel Sambuc ASTTemplateKWAndArgsInfo::sizeFor(unsigned NumTemplateArgs) {
611f4a2713aSLionel Sambuc   // Add space for the template keyword location.
612f4a2713aSLionel Sambuc   // FIXME: There's room for this in the padding before the template args in
613f4a2713aSLionel Sambuc   //        64-bit builds.
614f4a2713aSLionel Sambuc   return Base::sizeFor(NumTemplateArgs) + sizeof(SourceLocation);
615f4a2713aSLionel Sambuc }
616