John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 1 | //===--- TemplateBase.cpp - Common template AST class implementation ------===// |
| 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 | // |
| 10 | // This file implements common classes used throughout C++ template |
| 11 | // representations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 15 | #include "clang/AST/TemplateBase.h" |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclBase.h" |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/FoldingSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 27 | #include <cctype> |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 31 | /// \brief Print a template integral argument value. |
| 32 | /// |
| 33 | /// \param TemplArg the TemplateArgument instance to print. |
| 34 | /// |
| 35 | /// \param Out the raw_ostream instance to use for printing. |
| 36 | static void printIntegral(const TemplateArgument &TemplArg, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 37 | raw_ostream &Out) { |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 38 | const ::clang::Type *T = TemplArg.getIntegralType().getTypePtr(); |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 39 | const llvm::APSInt &Val = TemplArg.getAsIntegral(); |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 40 | |
| 41 | if (T->isBooleanType()) { |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 42 | Out << (Val.getBoolValue() ? "true" : "false"); |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 43 | } else if (T->isCharType()) { |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 44 | const char Ch = Val.getZExtValue(); |
Chandler Carruth | 774e2b4 | 2011-02-25 20:09:13 +0000 | [diff] [blame] | 45 | Out << ((Ch == '\'') ? "'\\" : "'"); |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 46 | Out.write_escaped(StringRef(&Ch, 1), /*UseHexEscapes=*/ true); |
Chandler Carruth | 774e2b4 | 2011-02-25 20:09:13 +0000 | [diff] [blame] | 47 | Out << "'"; |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 48 | } else { |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 49 | Out << Val; |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
| 54 | // TemplateArgument Implementation |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 57 | TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value, |
| 58 | QualType Type) |
| 59 | : Kind(Integral) { |
| 60 | // Copy the APSInt value into our decomposed form. |
| 61 | Integer.BitWidth = Value.getBitWidth(); |
| 62 | Integer.IsUnsigned = Value.isUnsigned(); |
| 63 | // If the value is large, we have to get additional memory from the ASTContext |
| 64 | if (Integer.BitWidth > 64) { |
| 65 | void *Mem = Ctx.Allocate(Integer.BitWidth / 8); |
| 66 | std::memcpy(Mem, Value.getRawData(), Integer.BitWidth / 8); |
| 67 | Integer.pVal = static_cast<uint64_t *>(Mem); |
| 68 | } else { |
| 69 | Integer.VAL = Value.getZExtValue(); |
| 70 | } |
| 71 | |
| 72 | Integer.Type = Type.getAsOpaquePtr(); |
| 73 | } |
| 74 | |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 75 | TemplateArgument TemplateArgument::CreatePackCopy(ASTContext &Context, |
| 76 | const TemplateArgument *Args, |
| 77 | unsigned NumArgs) { |
| 78 | if (NumArgs == 0) |
| 79 | return TemplateArgument(0, 0); |
| 80 | |
| 81 | TemplateArgument *Storage = new (Context) TemplateArgument [NumArgs]; |
| 82 | std::copy(Args, Args + NumArgs, Storage); |
| 83 | return TemplateArgument(Storage, NumArgs); |
| 84 | } |
| 85 | |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 86 | bool TemplateArgument::isDependent() const { |
| 87 | switch (getKind()) { |
| 88 | case Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 89 | llvm_unreachable("Should not have a NULL template argument"); |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 90 | |
| 91 | case Type: |
| 92 | return getAsType()->isDependentType(); |
| 93 | |
| 94 | case Template: |
| 95 | return getAsTemplate().isDependent(); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 96 | |
| 97 | case TemplateExpansion: |
| 98 | return true; |
| 99 | |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 100 | case Declaration: |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 101 | if (Decl *D = getAsDecl()) { |
| 102 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 103 | return DC->isDependentContext(); |
| 104 | return D->getDeclContext()->isDependentContext(); |
| 105 | } |
| 106 | |
| 107 | return false; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 108 | |
| 109 | case Integral: |
| 110 | // Never dependent |
| 111 | return false; |
| 112 | |
| 113 | case Expression: |
| 114 | return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent()); |
| 115 | |
| 116 | case Pack: |
| 117 | for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) { |
| 118 | if (P->isDependent()) |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 125 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 128 | bool TemplateArgument::isInstantiationDependent() const { |
| 129 | switch (getKind()) { |
| 130 | case Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 131 | llvm_unreachable("Should not have a NULL template argument"); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 132 | |
| 133 | case Type: |
| 134 | return getAsType()->isInstantiationDependentType(); |
| 135 | |
| 136 | case Template: |
| 137 | return getAsTemplate().isInstantiationDependent(); |
| 138 | |
| 139 | case TemplateExpansion: |
| 140 | return true; |
| 141 | |
| 142 | case Declaration: |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 143 | if (Decl *D = getAsDecl()) { |
| 144 | if (DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 145 | return DC->isDependentContext(); |
| 146 | return D->getDeclContext()->isDependentContext(); |
| 147 | } |
| 148 | return false; |
| 149 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 150 | case Integral: |
| 151 | // Never dependent |
| 152 | return false; |
| 153 | |
| 154 | case Expression: |
| 155 | return getAsExpr()->isInstantiationDependent(); |
| 156 | |
| 157 | case Pack: |
| 158 | for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) { |
| 159 | if (P->isInstantiationDependent()) |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | return false; |
| 164 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 165 | |
| 166 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 169 | bool TemplateArgument::isPackExpansion() const { |
| 170 | switch (getKind()) { |
| 171 | case Null: |
| 172 | case Declaration: |
| 173 | case Integral: |
| 174 | case Pack: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 175 | case Template: |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 176 | return false; |
| 177 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 178 | case TemplateExpansion: |
| 179 | return true; |
| 180 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 181 | case Type: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 182 | return isa<PackExpansionType>(getAsType()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 184 | case Expression: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 185 | return isa<PackExpansionExpr>(getAsExpr()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 186 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 187 | |
| 188 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 191 | bool TemplateArgument::containsUnexpandedParameterPack() const { |
| 192 | switch (getKind()) { |
| 193 | case Null: |
| 194 | case Declaration: |
| 195 | case Integral: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 196 | case TemplateExpansion: |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 197 | break; |
| 198 | |
| 199 | case Type: |
| 200 | if (getAsType()->containsUnexpandedParameterPack()) |
| 201 | return true; |
| 202 | break; |
| 203 | |
| 204 | case Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 205 | if (getAsTemplate().containsUnexpandedParameterPack()) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 206 | return true; |
| 207 | break; |
| 208 | |
| 209 | case Expression: |
| 210 | if (getAsExpr()->containsUnexpandedParameterPack()) |
| 211 | return true; |
| 212 | break; |
| 213 | |
| 214 | case Pack: |
| 215 | for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) |
| 216 | if (P->containsUnexpandedParameterPack()) |
| 217 | return true; |
| 218 | |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 225 | llvm::Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const { |
| 226 | assert(Kind == TemplateExpansion); |
| 227 | if (TemplateArg.NumExpansions) |
| 228 | return TemplateArg.NumExpansions - 1; |
| 229 | |
| 230 | return llvm::Optional<unsigned>(); |
| 231 | } |
| 232 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 233 | void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 234 | const ASTContext &Context) const { |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 235 | ID.AddInteger(Kind); |
| 236 | switch (Kind) { |
| 237 | case Null: |
| 238 | break; |
| 239 | |
| 240 | case Type: |
| 241 | getAsType().Profile(ID); |
| 242 | break; |
| 243 | |
| 244 | case Declaration: |
| 245 | ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0); |
| 246 | break; |
| 247 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 248 | case Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 249 | case TemplateExpansion: { |
| 250 | TemplateName Template = getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 251 | if (TemplateTemplateParmDecl *TTP |
| 252 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 253 | Template.getAsTemplateDecl())) { |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 254 | ID.AddBoolean(true); |
| 255 | ID.AddInteger(TTP->getDepth()); |
| 256 | ID.AddInteger(TTP->getPosition()); |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 257 | ID.AddBoolean(TTP->isParameterPack()); |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 258 | } else { |
| 259 | ID.AddBoolean(false); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 260 | ID.AddPointer(Context.getCanonicalTemplateName(Template) |
| 261 | .getAsVoidPointer()); |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 262 | } |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 263 | break; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 264 | } |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 265 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 266 | case Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 267 | getAsIntegral().Profile(ID); |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 268 | getIntegralType().Profile(ID); |
| 269 | break; |
| 270 | |
| 271 | case Expression: |
| 272 | getAsExpr()->Profile(ID, Context, true); |
| 273 | break; |
| 274 | |
| 275 | case Pack: |
| 276 | ID.AddInteger(Args.NumArgs); |
| 277 | for (unsigned I = 0; I != Args.NumArgs; ++I) |
| 278 | Args.Args[I].Profile(ID, Context); |
| 279 | } |
| 280 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 281 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 282 | bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const { |
| 283 | if (getKind() != Other.getKind()) return false; |
| 284 | |
| 285 | switch (getKind()) { |
| 286 | case Null: |
| 287 | case Type: |
| 288 | case Declaration: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 289 | case Expression: |
| 290 | case Template: |
| 291 | case TemplateExpansion: |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 292 | return TypeOrValue == Other.TypeOrValue; |
| 293 | |
| 294 | case Integral: |
| 295 | return getIntegralType() == Other.getIntegralType() && |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 296 | getAsIntegral() == Other.getAsIntegral(); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 297 | |
| 298 | case Pack: |
| 299 | if (Args.NumArgs != Other.Args.NumArgs) return false; |
| 300 | for (unsigned I = 0, E = Args.NumArgs; I != E; ++I) |
| 301 | if (!Args.Args[I].structurallyEquals(Other.Args.Args[I])) |
| 302 | return false; |
| 303 | return true; |
| 304 | } |
| 305 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 306 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 309 | TemplateArgument TemplateArgument::getPackExpansionPattern() const { |
| 310 | assert(isPackExpansion()); |
| 311 | |
| 312 | switch (getKind()) { |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 313 | case Type: |
| 314 | return getAsType()->getAs<PackExpansionType>()->getPattern(); |
| 315 | |
| 316 | case Expression: |
| 317 | return cast<PackExpansionExpr>(getAsExpr())->getPattern(); |
| 318 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 319 | case TemplateExpansion: |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 320 | return TemplateArgument(getAsTemplateOrTemplatePattern()); |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 321 | |
| 322 | case Declaration: |
| 323 | case Integral: |
| 324 | case Pack: |
| 325 | case Null: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 326 | case Template: |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 327 | return TemplateArgument(); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 328 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 329 | |
| 330 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 333 | void TemplateArgument::print(const PrintingPolicy &Policy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 334 | raw_ostream &Out) const { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 335 | switch (getKind()) { |
| 336 | case Null: |
| 337 | Out << "<no value>"; |
| 338 | break; |
| 339 | |
| 340 | case Type: { |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 341 | PrintingPolicy SubPolicy(Policy); |
| 342 | SubPolicy.SuppressStrongLifetime = true; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 343 | std::string TypeStr; |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 344 | getAsType().getAsStringInternal(TypeStr, SubPolicy); |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 345 | Out << TypeStr; |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | case Declaration: { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 350 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getAsDecl())) { |
| 351 | if (ND->getDeclName()) { |
Benjamin Kramer | a59d20b | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 352 | Out << *ND; |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 353 | } else { |
| 354 | Out << "<anonymous>"; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 355 | } |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 356 | } else { |
| 357 | Out << "nullptr"; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 358 | } |
| 359 | break; |
| 360 | } |
| 361 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 362 | case Template: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 363 | getAsTemplate().print(Out, Policy); |
| 364 | break; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 365 | |
| 366 | case TemplateExpansion: |
| 367 | getAsTemplateOrTemplatePattern().print(Out, Policy); |
| 368 | Out << "..."; |
| 369 | break; |
| 370 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 371 | case Integral: { |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 372 | printIntegral(*this, Out); |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 373 | break; |
| 374 | } |
| 375 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 376 | case Expression: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 377 | getAsExpr()->printPretty(Out, 0, Policy); |
| 378 | break; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 379 | |
| 380 | case Pack: |
| 381 | Out << "<"; |
| 382 | bool First = true; |
| 383 | for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end(); |
| 384 | P != PEnd; ++P) { |
| 385 | if (First) |
| 386 | First = false; |
| 387 | else |
| 388 | Out << ", "; |
| 389 | |
| 390 | P->print(Policy, Out); |
| 391 | } |
| 392 | Out << ">"; |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 397 | //===----------------------------------------------------------------------===// |
| 398 | // TemplateArgumentLoc Implementation |
| 399 | //===----------------------------------------------------------------------===// |
| 400 | |
Douglas Gregor | b0ddf3a | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 401 | TemplateArgumentLocInfo::TemplateArgumentLocInfo() { |
Chandler Carruth | 75c4064 | 2011-04-28 08:19:45 +0000 | [diff] [blame] | 402 | memset((void*)this, 0, sizeof(TemplateArgumentLocInfo)); |
Douglas Gregor | b0ddf3a | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 403 | } |
| 404 | |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 405 | SourceRange TemplateArgumentLoc::getSourceRange() const { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 406 | switch (Argument.getKind()) { |
| 407 | case TemplateArgument::Expression: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 408 | return getSourceExpression()->getSourceRange(); |
Zhanyong Wan | f38ef0c | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 409 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 410 | case TemplateArgument::Declaration: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 411 | return getSourceDeclExpression()->getSourceRange(); |
Zhanyong Wan | f38ef0c | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 412 | |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 413 | case TemplateArgument::Type: |
Zhanyong Wan | f38ef0c | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 414 | if (TypeSourceInfo *TSI = getTypeSourceInfo()) |
| 415 | return TSI->getTypeLoc().getSourceRange(); |
| 416 | else |
| 417 | return SourceRange(); |
| 418 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 419 | case TemplateArgument::Template: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 420 | if (getTemplateQualifierLoc()) |
| 421 | return SourceRange(getTemplateQualifierLoc().getBeginLoc(), |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 422 | getTemplateNameLoc()); |
| 423 | return SourceRange(getTemplateNameLoc()); |
| 424 | |
| 425 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 426 | if (getTemplateQualifierLoc()) |
| 427 | return SourceRange(getTemplateQualifierLoc().getBeginLoc(), |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 428 | getTemplateEllipsisLoc()); |
| 429 | return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc()); |
| 430 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 431 | case TemplateArgument::Integral: |
| 432 | case TemplateArgument::Pack: |
| 433 | case TemplateArgument::Null: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 434 | return SourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 435 | } |
| 436 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 437 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 438 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 439 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 440 | TemplateArgumentLoc |
| 441 | TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 442 | llvm::Optional<unsigned> &NumExpansions, |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 443 | ASTContext &Context) const { |
| 444 | assert(Argument.isPackExpansion()); |
| 445 | |
| 446 | switch (Argument.getKind()) { |
| 447 | case TemplateArgument::Type: { |
Douglas Gregor | 03491de | 2010-12-21 22:10:26 +0000 | [diff] [blame] | 448 | // FIXME: We shouldn't ever have to worry about missing |
| 449 | // type-source info! |
| 450 | TypeSourceInfo *ExpansionTSInfo = getTypeSourceInfo(); |
| 451 | if (!ExpansionTSInfo) |
| 452 | ExpansionTSInfo = Context.getTrivialTypeSourceInfo( |
| 453 | getArgument().getAsType(), |
| 454 | Ellipsis); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 455 | PackExpansionTypeLoc Expansion |
Douglas Gregor | 03491de | 2010-12-21 22:10:26 +0000 | [diff] [blame] | 456 | = cast<PackExpansionTypeLoc>(ExpansionTSInfo->getTypeLoc()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 457 | Ellipsis = Expansion.getEllipsisLoc(); |
| 458 | |
| 459 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 460 | NumExpansions = Expansion.getTypePtr()->getNumExpansions(); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 461 | |
| 462 | // FIXME: This is horrible. We know where the source location data is for |
| 463 | // the pattern, and we have the pattern's type, but we are forced to copy |
| 464 | // them into an ASTContext because TypeSourceInfo bundles them together |
| 465 | // and TemplateArgumentLoc traffics in TypeSourceInfo pointers. |
| 466 | TypeSourceInfo *PatternTSInfo |
| 467 | = Context.CreateTypeSourceInfo(Pattern.getType(), |
| 468 | Pattern.getFullDataSize()); |
| 469 | memcpy(PatternTSInfo->getTypeLoc().getOpaqueData(), |
| 470 | Pattern.getOpaqueData(), Pattern.getFullDataSize()); |
| 471 | return TemplateArgumentLoc(TemplateArgument(Pattern.getType()), |
| 472 | PatternTSInfo); |
| 473 | } |
| 474 | |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 475 | case TemplateArgument::Expression: { |
Douglas Gregor | b0ddf3a | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 476 | PackExpansionExpr *Expansion |
| 477 | = cast<PackExpansionExpr>(Argument.getAsExpr()); |
| 478 | Expr *Pattern = Expansion->getPattern(); |
| 479 | Ellipsis = Expansion->getEllipsisLoc(); |
Douglas Gregor | 67fd125 | 2011-01-14 21:20:45 +0000 | [diff] [blame] | 480 | NumExpansions = Expansion->getNumExpansions(); |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 481 | return TemplateArgumentLoc(Pattern, Pattern); |
| 482 | } |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 483 | |
| 484 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b0ddf3a | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 485 | Ellipsis = getTemplateEllipsisLoc(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 486 | NumExpansions = Argument.getNumTemplateExpansions(); |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 487 | return TemplateArgumentLoc(Argument.getPackExpansionPattern(), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 488 | getTemplateQualifierLoc(), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 489 | getTemplateNameLoc()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 490 | |
| 491 | case TemplateArgument::Declaration: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 492 | case TemplateArgument::Template: |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 493 | case TemplateArgument::Integral: |
| 494 | case TemplateArgument::Pack: |
| 495 | case TemplateArgument::Null: |
| 496 | return TemplateArgumentLoc(); |
| 497 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 498 | |
| 499 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 502 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 503 | const TemplateArgument &Arg) { |
| 504 | switch (Arg.getKind()) { |
| 505 | case TemplateArgument::Null: |
John McCall | 67c4a0c | 2010-08-05 04:58:04 +0000 | [diff] [blame] | 506 | // This is bad, but not as bad as crashing because of argument |
| 507 | // count mismatches. |
| 508 | return DB << "(null template argument)"; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 509 | |
| 510 | case TemplateArgument::Type: |
| 511 | return DB << Arg.getAsType(); |
| 512 | |
| 513 | case TemplateArgument::Declaration: |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 514 | if (Decl *D = Arg.getAsDecl()) |
| 515 | return DB << D; |
| 516 | return DB << "nullptr"; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 517 | |
| 518 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame^] | 519 | return DB << Arg.getAsIntegral().toString(10); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 520 | |
| 521 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 522 | return DB << Arg.getAsTemplate(); |
| 523 | |
| 524 | case TemplateArgument::TemplateExpansion: |
| 525 | return DB << Arg.getAsTemplateOrTemplatePattern() << "..."; |
| 526 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 527 | case TemplateArgument::Expression: { |
| 528 | // This shouldn't actually ever happen, so it's okay that we're |
| 529 | // regurgitating an expression here. |
| 530 | // FIXME: We're guessing at LangOptions! |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 531 | SmallString<32> Str; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 532 | llvm::raw_svector_ostream OS(Str); |
| 533 | LangOptions LangOpts; |
| 534 | LangOpts.CPlusPlus = true; |
| 535 | PrintingPolicy Policy(LangOpts); |
| 536 | Arg.getAsExpr()->printPretty(OS, 0, Policy); |
| 537 | return DB << OS.str(); |
| 538 | } |
| 539 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 540 | case TemplateArgument::Pack: { |
| 541 | // FIXME: We're guessing at LangOptions! |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 542 | SmallString<32> Str; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 543 | llvm::raw_svector_ostream OS(Str); |
| 544 | LangOptions LangOpts; |
| 545 | LangOpts.CPlusPlus = true; |
| 546 | PrintingPolicy Policy(LangOpts); |
| 547 | Arg.print(Policy, OS); |
| 548 | return DB << OS.str(); |
| 549 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 550 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 551 | |
| 552 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 553 | } |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 554 | |
| 555 | const ASTTemplateArgumentListInfo * |
| 556 | ASTTemplateArgumentListInfo::Create(ASTContext &C, |
| 557 | const TemplateArgumentListInfo &List) { |
| 558 | std::size_t size = sizeof(CXXDependentScopeMemberExpr) + |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 559 | ASTTemplateArgumentListInfo::sizeFor(List.size()); |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 560 | void *Mem = C.Allocate(size, llvm::alignOf<ASTTemplateArgumentListInfo>()); |
| 561 | ASTTemplateArgumentListInfo *TAI = new (Mem) ASTTemplateArgumentListInfo(); |
| 562 | TAI->initializeFrom(List); |
| 563 | return TAI; |
| 564 | } |
| 565 | |
| 566 | void ASTTemplateArgumentListInfo::initializeFrom( |
| 567 | const TemplateArgumentListInfo &Info) { |
| 568 | LAngleLoc = Info.getLAngleLoc(); |
| 569 | RAngleLoc = Info.getRAngleLoc(); |
| 570 | NumTemplateArgs = Info.size(); |
| 571 | |
| 572 | TemplateArgumentLoc *ArgBuffer = getTemplateArgs(); |
| 573 | for (unsigned i = 0; i != NumTemplateArgs; ++i) |
| 574 | new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]); |
| 575 | } |
| 576 | |
| 577 | void ASTTemplateArgumentListInfo::initializeFrom( |
| 578 | const TemplateArgumentListInfo &Info, |
| 579 | bool &Dependent, |
| 580 | bool &InstantiationDependent, |
| 581 | bool &ContainsUnexpandedParameterPack) { |
| 582 | LAngleLoc = Info.getLAngleLoc(); |
| 583 | RAngleLoc = Info.getRAngleLoc(); |
| 584 | NumTemplateArgs = Info.size(); |
| 585 | |
| 586 | TemplateArgumentLoc *ArgBuffer = getTemplateArgs(); |
| 587 | for (unsigned i = 0; i != NumTemplateArgs; ++i) { |
| 588 | Dependent = Dependent || Info[i].getArgument().isDependent(); |
| 589 | InstantiationDependent = InstantiationDependent || |
| 590 | Info[i].getArgument().isInstantiationDependent(); |
| 591 | ContainsUnexpandedParameterPack |
| 592 | = ContainsUnexpandedParameterPack || |
| 593 | Info[i].getArgument().containsUnexpandedParameterPack(); |
| 594 | |
| 595 | new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void ASTTemplateArgumentListInfo::copyInto( |
| 600 | TemplateArgumentListInfo &Info) const { |
| 601 | Info.setLAngleLoc(LAngleLoc); |
| 602 | Info.setRAngleLoc(RAngleLoc); |
| 603 | for (unsigned I = 0; I != NumTemplateArgs; ++I) |
| 604 | Info.addArgument(getTemplateArgs()[I]); |
| 605 | } |
| 606 | |
| 607 | std::size_t ASTTemplateArgumentListInfo::sizeFor(unsigned NumTemplateArgs) { |
| 608 | return sizeof(ASTTemplateArgumentListInfo) + |
| 609 | sizeof(TemplateArgumentLoc) * NumTemplateArgs; |
| 610 | } |
| 611 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 612 | void |
| 613 | ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc, |
| 614 | const TemplateArgumentListInfo &Info) { |
| 615 | Base::initializeFrom(Info); |
| 616 | setTemplateKeywordLoc(TemplateKWLoc); |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 617 | } |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 618 | |
| 619 | void |
| 620 | ASTTemplateKWAndArgsInfo |
| 621 | ::initializeFrom(SourceLocation TemplateKWLoc, |
| 622 | const TemplateArgumentListInfo &Info, |
| 623 | bool &Dependent, |
| 624 | bool &InstantiationDependent, |
| 625 | bool &ContainsUnexpandedParameterPack) { |
| 626 | Base::initializeFrom(Info, Dependent, InstantiationDependent, |
| 627 | ContainsUnexpandedParameterPack); |
| 628 | setTemplateKeywordLoc(TemplateKWLoc); |
| 629 | } |
| 630 | |
| 631 | void |
| 632 | ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) { |
| 633 | // No explicit template arguments, but template keyword loc is valid. |
| 634 | assert(TemplateKWLoc.isValid()); |
| 635 | LAngleLoc = SourceLocation(); |
| 636 | RAngleLoc = SourceLocation(); |
| 637 | NumTemplateArgs = 0; |
| 638 | setTemplateKeywordLoc(TemplateKWLoc); |
| 639 | } |
| 640 | |
| 641 | std::size_t |
| 642 | ASTTemplateKWAndArgsInfo::sizeFor(unsigned NumTemplateArgs) { |
| 643 | // Add space for the template keyword location. |
| 644 | return Base::sizeFor(NumTemplateArgs) + sizeof(SourceLocation); |
| 645 | } |
| 646 | |