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