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" |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 26 | #include <cctype> |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 30 | /// \brief Print a template integral argument value. |
| 31 | /// |
| 32 | /// \param TemplArg the TemplateArgument instance to print. |
| 33 | /// |
| 34 | /// \param Out the raw_ostream instance to use for printing. |
| 35 | static void printIntegral(const TemplateArgument &TemplArg, |
| 36 | llvm::raw_ostream &Out) { |
| 37 | const ::clang::Type *T = TemplArg.getIntegralType().getTypePtr(); |
| 38 | const llvm::APSInt *Val = TemplArg.getAsIntegral(); |
| 39 | |
| 40 | if (T->isBooleanType()) { |
| 41 | Out << (Val->getBoolValue() ? "true" : "false"); |
| 42 | } else if (T->isCharType()) { |
Chandler Carruth | 774e2b4 | 2011-02-25 20:09:13 +0000 | [diff] [blame] | 43 | const unsigned char Ch = Val->getZExtValue(); |
| 44 | const std::string Str(1, Ch); |
| 45 | Out << ((Ch == '\'') ? "'\\" : "'"); |
| 46 | Out.write_escaped(Str, /*UseHexEscapes=*/ true); |
| 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: |
| 71 | assert(false && "Should not have a NULL template argument"); |
| 72 | return false; |
| 73 | |
| 74 | case Type: |
| 75 | return getAsType()->isDependentType(); |
| 76 | |
| 77 | case Template: |
| 78 | return getAsTemplate().isDependent(); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 79 | |
| 80 | case TemplateExpansion: |
| 81 | return true; |
| 82 | |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 83 | case Declaration: |
| 84 | if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl())) |
| 85 | return DC->isDependentContext(); |
| 86 | return getAsDecl()->getDeclContext()->isDependentContext(); |
| 87 | |
| 88 | case Integral: |
| 89 | // Never dependent |
| 90 | return false; |
| 91 | |
| 92 | case Expression: |
| 93 | return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent()); |
| 94 | |
| 95 | case Pack: |
| 96 | for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) { |
| 97 | if (P->isDependent()) |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 107 | bool TemplateArgument::isPackExpansion() const { |
| 108 | switch (getKind()) { |
| 109 | case Null: |
| 110 | case Declaration: |
| 111 | case Integral: |
| 112 | case Pack: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 113 | case Template: |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 114 | return false; |
| 115 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 116 | case TemplateExpansion: |
| 117 | return true; |
| 118 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 119 | case Type: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 120 | return isa<PackExpansionType>(getAsType()); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 122 | case Expression: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 123 | return isa<PackExpansionExpr>(getAsExpr()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 129 | bool TemplateArgument::containsUnexpandedParameterPack() const { |
| 130 | switch (getKind()) { |
| 131 | case Null: |
| 132 | case Declaration: |
| 133 | case Integral: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 134 | case TemplateExpansion: |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case Type: |
| 138 | if (getAsType()->containsUnexpandedParameterPack()) |
| 139 | return true; |
| 140 | break; |
| 141 | |
| 142 | case Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 143 | if (getAsTemplate().containsUnexpandedParameterPack()) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 144 | return true; |
| 145 | break; |
| 146 | |
| 147 | case Expression: |
| 148 | if (getAsExpr()->containsUnexpandedParameterPack()) |
| 149 | return true; |
| 150 | break; |
| 151 | |
| 152 | case Pack: |
| 153 | for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) |
| 154 | if (P->containsUnexpandedParameterPack()) |
| 155 | return true; |
| 156 | |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 163 | llvm::Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const { |
| 164 | assert(Kind == TemplateExpansion); |
| 165 | if (TemplateArg.NumExpansions) |
| 166 | return TemplateArg.NumExpansions - 1; |
| 167 | |
| 168 | return llvm::Optional<unsigned>(); |
| 169 | } |
| 170 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 171 | void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 172 | const ASTContext &Context) const { |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 173 | ID.AddInteger(Kind); |
| 174 | switch (Kind) { |
| 175 | case Null: |
| 176 | break; |
| 177 | |
| 178 | case Type: |
| 179 | getAsType().Profile(ID); |
| 180 | break; |
| 181 | |
| 182 | case Declaration: |
| 183 | ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0); |
| 184 | break; |
| 185 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 186 | case Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 187 | case TemplateExpansion: { |
| 188 | TemplateName Template = getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 189 | if (TemplateTemplateParmDecl *TTP |
| 190 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 191 | Template.getAsTemplateDecl())) { |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 192 | ID.AddBoolean(true); |
| 193 | ID.AddInteger(TTP->getDepth()); |
| 194 | ID.AddInteger(TTP->getPosition()); |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 195 | ID.AddBoolean(TTP->isParameterPack()); |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 196 | } else { |
| 197 | ID.AddBoolean(false); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 198 | ID.AddPointer(Context.getCanonicalTemplateName(Template) |
| 199 | .getAsVoidPointer()); |
Douglas Gregor | 74295b3 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 200 | } |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 201 | break; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 202 | } |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 203 | |
John McCall | 275c10a | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 204 | case Integral: |
| 205 | getAsIntegral()->Profile(ID); |
| 206 | getIntegralType().Profile(ID); |
| 207 | break; |
| 208 | |
| 209 | case Expression: |
| 210 | getAsExpr()->Profile(ID, Context, true); |
| 211 | break; |
| 212 | |
| 213 | case Pack: |
| 214 | ID.AddInteger(Args.NumArgs); |
| 215 | for (unsigned I = 0; I != Args.NumArgs; ++I) |
| 216 | Args.Args[I].Profile(ID, Context); |
| 217 | } |
| 218 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 219 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 220 | bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const { |
| 221 | if (getKind() != Other.getKind()) return false; |
| 222 | |
| 223 | switch (getKind()) { |
| 224 | case Null: |
| 225 | case Type: |
| 226 | case Declaration: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 227 | case Expression: |
| 228 | case Template: |
| 229 | case TemplateExpansion: |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 230 | return TypeOrValue == Other.TypeOrValue; |
| 231 | |
| 232 | case Integral: |
| 233 | return getIntegralType() == Other.getIntegralType() && |
| 234 | *getAsIntegral() == *Other.getAsIntegral(); |
| 235 | |
| 236 | case Pack: |
| 237 | if (Args.NumArgs != Other.Args.NumArgs) return false; |
| 238 | for (unsigned I = 0, E = Args.NumArgs; I != E; ++I) |
| 239 | if (!Args.Args[I].structurallyEquals(Other.Args.Args[I])) |
| 240 | return false; |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | // Suppress warnings. |
| 245 | return false; |
| 246 | } |
| 247 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 248 | TemplateArgument TemplateArgument::getPackExpansionPattern() const { |
| 249 | assert(isPackExpansion()); |
| 250 | |
| 251 | switch (getKind()) { |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 252 | case Type: |
| 253 | return getAsType()->getAs<PackExpansionType>()->getPattern(); |
| 254 | |
| 255 | case Expression: |
| 256 | return cast<PackExpansionExpr>(getAsExpr())->getPattern(); |
| 257 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 258 | case TemplateExpansion: |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 259 | return TemplateArgument(getAsTemplateOrTemplatePattern()); |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 260 | |
| 261 | case Declaration: |
| 262 | case Integral: |
| 263 | case Pack: |
| 264 | case Null: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 265 | case Template: |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 266 | return TemplateArgument(); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return TemplateArgument(); |
| 270 | } |
| 271 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 272 | void TemplateArgument::print(const PrintingPolicy &Policy, |
| 273 | llvm::raw_ostream &Out) const { |
| 274 | switch (getKind()) { |
| 275 | case Null: |
| 276 | Out << "<no value>"; |
| 277 | break; |
| 278 | |
| 279 | case Type: { |
| 280 | std::string TypeStr; |
| 281 | getAsType().getAsStringInternal(TypeStr, Policy); |
| 282 | Out << TypeStr; |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | case Declaration: { |
| 287 | bool Unnamed = true; |
| 288 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getAsDecl())) { |
| 289 | if (ND->getDeclName()) { |
| 290 | Unnamed = false; |
| 291 | Out << ND->getNameAsString(); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (Unnamed) { |
| 296 | Out << "<anonymous>"; |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 301 | case Template: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 302 | getAsTemplate().print(Out, Policy); |
| 303 | break; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 304 | |
| 305 | case TemplateExpansion: |
| 306 | getAsTemplateOrTemplatePattern().print(Out, Policy); |
| 307 | Out << "..."; |
| 308 | break; |
| 309 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 310 | case Integral: { |
Chandler Carruth | 781701c | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 311 | printIntegral(*this, Out); |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 315 | case Expression: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 316 | getAsExpr()->printPretty(Out, 0, Policy); |
| 317 | break; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 318 | |
| 319 | case Pack: |
| 320 | Out << "<"; |
| 321 | bool First = true; |
| 322 | for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end(); |
| 323 | P != PEnd; ++P) { |
| 324 | if (First) |
| 325 | First = false; |
| 326 | else |
| 327 | Out << ", "; |
| 328 | |
| 329 | P->print(Policy, Out); |
| 330 | } |
| 331 | Out << ">"; |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 336 | //===----------------------------------------------------------------------===// |
| 337 | // TemplateArgumentLoc Implementation |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | |
Douglas Gregor | b0ddf3a | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 340 | TemplateArgumentLocInfo::TemplateArgumentLocInfo() { |
| 341 | memset(this, 0, sizeof(TemplateArgumentLocInfo)); |
| 342 | } |
| 343 | |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 344 | SourceRange TemplateArgumentLoc::getSourceRange() const { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 345 | switch (Argument.getKind()) { |
| 346 | case TemplateArgument::Expression: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 347 | return getSourceExpression()->getSourceRange(); |
Zhanyong Wan | f38ef0c | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 348 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 349 | case TemplateArgument::Declaration: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 350 | return getSourceDeclExpression()->getSourceRange(); |
Zhanyong Wan | f38ef0c | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 351 | |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 352 | case TemplateArgument::Type: |
Zhanyong Wan | f38ef0c | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 353 | if (TypeSourceInfo *TSI = getTypeSourceInfo()) |
| 354 | return TSI->getTypeLoc().getSourceRange(); |
| 355 | else |
| 356 | return SourceRange(); |
| 357 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 358 | case TemplateArgument::Template: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 359 | if (getTemplateQualifierLoc()) |
| 360 | return SourceRange(getTemplateQualifierLoc().getBeginLoc(), |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 361 | getTemplateNameLoc()); |
| 362 | return SourceRange(getTemplateNameLoc()); |
| 363 | |
| 364 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 365 | if (getTemplateQualifierLoc()) |
| 366 | return SourceRange(getTemplateQualifierLoc().getBeginLoc(), |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 367 | getTemplateEllipsisLoc()); |
| 368 | return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc()); |
| 369 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 370 | case TemplateArgument::Integral: |
| 371 | case TemplateArgument::Pack: |
| 372 | case TemplateArgument::Null: |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 373 | return SourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | // Silence bonus gcc warning. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 377 | return SourceRange(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 378 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 380 | TemplateArgumentLoc |
| 381 | TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 382 | llvm::Optional<unsigned> &NumExpansions, |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 383 | ASTContext &Context) const { |
| 384 | assert(Argument.isPackExpansion()); |
| 385 | |
| 386 | switch (Argument.getKind()) { |
| 387 | case TemplateArgument::Type: { |
Douglas Gregor | 03491de | 2010-12-21 22:10:26 +0000 | [diff] [blame] | 388 | // FIXME: We shouldn't ever have to worry about missing |
| 389 | // type-source info! |
| 390 | TypeSourceInfo *ExpansionTSInfo = getTypeSourceInfo(); |
| 391 | if (!ExpansionTSInfo) |
| 392 | ExpansionTSInfo = Context.getTrivialTypeSourceInfo( |
| 393 | getArgument().getAsType(), |
| 394 | Ellipsis); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 395 | PackExpansionTypeLoc Expansion |
Douglas Gregor | 03491de | 2010-12-21 22:10:26 +0000 | [diff] [blame] | 396 | = cast<PackExpansionTypeLoc>(ExpansionTSInfo->getTypeLoc()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 397 | Ellipsis = Expansion.getEllipsisLoc(); |
| 398 | |
| 399 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 400 | NumExpansions = Expansion.getTypePtr()->getNumExpansions(); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 401 | |
| 402 | // FIXME: This is horrible. We know where the source location data is for |
| 403 | // the pattern, and we have the pattern's type, but we are forced to copy |
| 404 | // them into an ASTContext because TypeSourceInfo bundles them together |
| 405 | // and TemplateArgumentLoc traffics in TypeSourceInfo pointers. |
| 406 | TypeSourceInfo *PatternTSInfo |
| 407 | = Context.CreateTypeSourceInfo(Pattern.getType(), |
| 408 | Pattern.getFullDataSize()); |
| 409 | memcpy(PatternTSInfo->getTypeLoc().getOpaqueData(), |
| 410 | Pattern.getOpaqueData(), Pattern.getFullDataSize()); |
| 411 | return TemplateArgumentLoc(TemplateArgument(Pattern.getType()), |
| 412 | PatternTSInfo); |
| 413 | } |
| 414 | |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 415 | case TemplateArgument::Expression: { |
Douglas Gregor | b0ddf3a | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 416 | PackExpansionExpr *Expansion |
| 417 | = cast<PackExpansionExpr>(Argument.getAsExpr()); |
| 418 | Expr *Pattern = Expansion->getPattern(); |
| 419 | Ellipsis = Expansion->getEllipsisLoc(); |
Douglas Gregor | 67fd125 | 2011-01-14 21:20:45 +0000 | [diff] [blame] | 420 | NumExpansions = Expansion->getNumExpansions(); |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 421 | return TemplateArgumentLoc(Pattern, Pattern); |
| 422 | } |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 423 | |
| 424 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b0ddf3a | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 425 | Ellipsis = getTemplateEllipsisLoc(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 426 | NumExpansions = Argument.getNumTemplateExpansions(); |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 427 | return TemplateArgumentLoc(Argument.getPackExpansionPattern(), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 428 | getTemplateQualifierLoc(), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 429 | getTemplateNameLoc()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 430 | |
| 431 | case TemplateArgument::Declaration: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 432 | case TemplateArgument::Template: |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 433 | case TemplateArgument::Integral: |
| 434 | case TemplateArgument::Pack: |
| 435 | case TemplateArgument::Null: |
| 436 | return TemplateArgumentLoc(); |
| 437 | } |
| 438 | |
| 439 | return TemplateArgumentLoc(); |
| 440 | } |
| 441 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 442 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 443 | const TemplateArgument &Arg) { |
| 444 | switch (Arg.getKind()) { |
| 445 | case TemplateArgument::Null: |
John McCall | 67c4a0c | 2010-08-05 04:58:04 +0000 | [diff] [blame] | 446 | // This is bad, but not as bad as crashing because of argument |
| 447 | // count mismatches. |
| 448 | return DB << "(null template argument)"; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 449 | |
| 450 | case TemplateArgument::Type: |
| 451 | return DB << Arg.getAsType(); |
| 452 | |
| 453 | case TemplateArgument::Declaration: |
| 454 | return DB << Arg.getAsDecl(); |
| 455 | |
| 456 | case TemplateArgument::Integral: |
| 457 | return DB << Arg.getAsIntegral()->toString(10); |
| 458 | |
| 459 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 460 | return DB << Arg.getAsTemplate(); |
| 461 | |
| 462 | case TemplateArgument::TemplateExpansion: |
| 463 | return DB << Arg.getAsTemplateOrTemplatePattern() << "..."; |
| 464 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 465 | case TemplateArgument::Expression: { |
| 466 | // This shouldn't actually ever happen, so it's okay that we're |
| 467 | // regurgitating an expression here. |
| 468 | // FIXME: We're guessing at LangOptions! |
| 469 | llvm::SmallString<32> Str; |
| 470 | llvm::raw_svector_ostream OS(Str); |
| 471 | LangOptions LangOpts; |
| 472 | LangOpts.CPlusPlus = true; |
| 473 | PrintingPolicy Policy(LangOpts); |
| 474 | Arg.getAsExpr()->printPretty(OS, 0, Policy); |
| 475 | return DB << OS.str(); |
| 476 | } |
| 477 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 478 | case TemplateArgument::Pack: { |
| 479 | // FIXME: We're guessing at LangOptions! |
| 480 | llvm::SmallString<32> Str; |
| 481 | llvm::raw_svector_ostream OS(Str); |
| 482 | LangOptions LangOpts; |
| 483 | LangOpts.CPlusPlus = true; |
| 484 | PrintingPolicy Policy(LangOpts); |
| 485 | Arg.print(Policy, OS); |
| 486 | return DB << OS.str(); |
| 487 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | return DB; |
| 491 | } |