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