John McCall | 588d2d5 | 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 | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 15 | #include "clang/AST/TemplateBase.h" |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclBase.h" |
Douglas Gregor | bd866c2 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 4c4f8de | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/FoldingSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 74c6d19 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 27 | #include <algorithm> |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | |
Chandler Carruth | 4c4f8de | 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 | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 37 | raw_ostream &Out) { |
Chandler Carruth | 4c4f8de | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 38 | const ::clang::Type *T = TemplArg.getIntegralType().getTypePtr(); |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 39 | const llvm::APSInt &Val = TemplArg.getAsIntegral(); |
Chandler Carruth | 4c4f8de | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 40 | |
| 41 | if (T->isBooleanType()) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 42 | Out << (Val.getBoolValue() ? "true" : "false"); |
Chandler Carruth | 4c4f8de | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 43 | } else if (T->isCharType()) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 44 | const char Ch = Val.getZExtValue(); |
Chandler Carruth | 20d0fee | 2011-02-25 20:09:13 +0000 | [diff] [blame] | 45 | Out << ((Ch == '\'') ? "'\\" : "'"); |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 46 | Out.write_escaped(StringRef(&Ch, 1), /*UseHexEscapes=*/ true); |
Chandler Carruth | 20d0fee | 2011-02-25 20:09:13 +0000 | [diff] [blame] | 47 | Out << "'"; |
Chandler Carruth | 4c4f8de | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 48 | } else { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 49 | Out << Val; |
Chandler Carruth | 4c4f8de | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
| 54 | // TemplateArgument Implementation |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 57 | TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value, |
Eli Friedman | 2d1611b | 2013-08-21 23:05:56 +0000 | [diff] [blame] | 58 | QualType Type) { |
| 59 | Integer.Kind = Integral; |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 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 |
Benjamin Kramer | 5d4cff7 | 2012-06-07 15:54:03 +0000 | [diff] [blame] | 64 | unsigned NumWords = Value.getNumWords(); |
| 65 | if (NumWords > 1) { |
| 66 | void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t)); |
| 67 | std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t)); |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 68 | Integer.pVal = static_cast<uint64_t *>(Mem); |
| 69 | } else { |
| 70 | Integer.VAL = Value.getZExtValue(); |
| 71 | } |
| 72 | |
| 73 | Integer.Type = Type.getAsOpaquePtr(); |
| 74 | } |
| 75 | |
Douglas Gregor | 74c6d19 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 76 | TemplateArgument TemplateArgument::CreatePackCopy(ASTContext &Context, |
| 77 | const TemplateArgument *Args, |
| 78 | unsigned NumArgs) { |
| 79 | if (NumArgs == 0) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 80 | return getEmptyPack(); |
Douglas Gregor | 74c6d19 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 81 | |
| 82 | TemplateArgument *Storage = new (Context) TemplateArgument [NumArgs]; |
| 83 | std::copy(Args, Args + NumArgs, Storage); |
| 84 | return TemplateArgument(Storage, NumArgs); |
| 85 | } |
| 86 | |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 87 | bool TemplateArgument::isDependent() const { |
| 88 | switch (getKind()) { |
| 89 | case Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 90 | llvm_unreachable("Should not have a NULL template argument"); |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 91 | |
| 92 | case Type: |
| 93 | return getAsType()->isDependentType(); |
| 94 | |
| 95 | case Template: |
| 96 | return getAsTemplate().isDependent(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 97 | |
| 98 | case TemplateExpansion: |
| 99 | return true; |
| 100 | |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 101 | case Declaration: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 102 | if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl())) |
| 103 | return DC->isDependentContext(); |
| 104 | return getAsDecl()->getDeclContext()->isDependentContext(); |
| 105 | |
| 106 | case NullPtr: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 107 | return false; |
Douglas Gregor | a6e053e | 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 | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 125 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 128 | bool TemplateArgument::isInstantiationDependent() const { |
| 129 | switch (getKind()) { |
| 130 | case Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 131 | llvm_unreachable("Should not have a NULL template argument"); |
Douglas Gregor | 678d76c | 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: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 143 | if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl())) |
| 144 | return DC->isDependentContext(); |
| 145 | return getAsDecl()->getDeclContext()->isDependentContext(); |
| 146 | |
| 147 | case NullPtr: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 148 | return false; |
| 149 | |
Douglas Gregor | 678d76c | 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 | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 165 | |
| 166 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Douglas Gregor | 840bd6c | 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 | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 175 | case Template: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 176 | case NullPtr: |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 177 | return false; |
| 178 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 179 | case TemplateExpansion: |
| 180 | return true; |
| 181 | |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 182 | case Type: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 183 | return isa<PackExpansionType>(getAsType()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 184 | |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 185 | case Expression: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 186 | return isa<PackExpansionExpr>(getAsExpr()); |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 187 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 188 | |
| 189 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 192 | bool TemplateArgument::containsUnexpandedParameterPack() const { |
| 193 | switch (getKind()) { |
| 194 | case Null: |
| 195 | case Declaration: |
| 196 | case Integral: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 197 | case TemplateExpansion: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 198 | case NullPtr: |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 199 | break; |
| 200 | |
| 201 | case Type: |
| 202 | if (getAsType()->containsUnexpandedParameterPack()) |
| 203 | return true; |
| 204 | break; |
| 205 | |
| 206 | case Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 207 | if (getAsTemplate().containsUnexpandedParameterPack()) |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 208 | return true; |
| 209 | break; |
| 210 | |
| 211 | case Expression: |
| 212 | if (getAsExpr()->containsUnexpandedParameterPack()) |
| 213 | return true; |
| 214 | break; |
| 215 | |
| 216 | case Pack: |
| 217 | for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) |
| 218 | if (P->containsUnexpandedParameterPack()) |
| 219 | return true; |
| 220 | |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | return false; |
| 225 | } |
| 226 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 227 | Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const { |
Eli Friedman | 2d1611b | 2013-08-21 23:05:56 +0000 | [diff] [blame] | 228 | assert(getKind() == TemplateExpansion); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 229 | if (TemplateArg.NumExpansions) |
| 230 | return TemplateArg.NumExpansions - 1; |
| 231 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 232 | return None; |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 233 | } |
| 234 | |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 235 | void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 236 | const ASTContext &Context) const { |
Eli Friedman | 2d1611b | 2013-08-21 23:05:56 +0000 | [diff] [blame] | 237 | ID.AddInteger(getKind()); |
| 238 | switch (getKind()) { |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 239 | case Null: |
| 240 | break; |
| 241 | |
| 242 | case Type: |
| 243 | getAsType().Profile(ID); |
| 244 | break; |
| 245 | |
Eli Friedman | 2d1611b | 2013-08-21 23:05:56 +0000 | [diff] [blame] | 246 | case NullPtr: |
| 247 | getNullPtrType().Profile(ID); |
| 248 | break; |
| 249 | |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 250 | case Declaration: |
| 251 | ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0); |
| 252 | break; |
| 253 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 254 | case Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 255 | case TemplateExpansion: { |
| 256 | TemplateName Template = getAsTemplateOrTemplatePattern(); |
Douglas Gregor | bd866c2 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 257 | if (TemplateTemplateParmDecl *TTP |
| 258 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 259 | Template.getAsTemplateDecl())) { |
Douglas Gregor | bd866c2 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 260 | ID.AddBoolean(true); |
| 261 | ID.AddInteger(TTP->getDepth()); |
| 262 | ID.AddInteger(TTP->getPosition()); |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 263 | ID.AddBoolean(TTP->isParameterPack()); |
Douglas Gregor | bd866c2 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 264 | } else { |
| 265 | ID.AddBoolean(false); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 266 | ID.AddPointer(Context.getCanonicalTemplateName(Template) |
| 267 | .getAsVoidPointer()); |
Douglas Gregor | bd866c2 | 2009-11-23 12:52:47 +0000 | [diff] [blame] | 268 | } |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 269 | break; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 270 | } |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 271 | |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 272 | case Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 273 | getAsIntegral().Profile(ID); |
John McCall | 588d2d5 | 2009-10-29 07:48:15 +0000 | [diff] [blame] | 274 | getIntegralType().Profile(ID); |
| 275 | break; |
| 276 | |
| 277 | case Expression: |
| 278 | getAsExpr()->Profile(ID, Context, true); |
| 279 | break; |
| 280 | |
| 281 | case Pack: |
| 282 | ID.AddInteger(Args.NumArgs); |
| 283 | for (unsigned I = 0; I != Args.NumArgs; ++I) |
| 284 | Args.Args[I].Profile(ID, Context); |
| 285 | } |
| 286 | } |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 287 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 288 | bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const { |
| 289 | if (getKind() != Other.getKind()) return false; |
| 290 | |
| 291 | switch (getKind()) { |
| 292 | case Null: |
| 293 | case Type: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 294 | case Expression: |
| 295 | case Template: |
| 296 | case TemplateExpansion: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 297 | case NullPtr: |
Eli Friedman | 2d1611b | 2013-08-21 23:05:56 +0000 | [diff] [blame] | 298 | return TypeOrValue.V == Other.TypeOrValue.V; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 299 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 300 | case Declaration: |
| 301 | return getAsDecl() == Other.getAsDecl() && |
| 302 | isDeclForReferenceParam() && Other.isDeclForReferenceParam(); |
| 303 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 304 | case Integral: |
| 305 | return getIntegralType() == Other.getIntegralType() && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 306 | getAsIntegral() == Other.getAsIntegral(); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 307 | |
| 308 | case Pack: |
| 309 | if (Args.NumArgs != Other.Args.NumArgs) return false; |
| 310 | for (unsigned I = 0, E = Args.NumArgs; I != E; ++I) |
| 311 | if (!Args.Args[I].structurallyEquals(Other.Args.Args[I])) |
| 312 | return false; |
| 313 | return true; |
| 314 | } |
| 315 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 316 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 319 | TemplateArgument TemplateArgument::getPackExpansionPattern() const { |
| 320 | assert(isPackExpansion()); |
| 321 | |
| 322 | switch (getKind()) { |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 323 | case Type: |
| 324 | return getAsType()->getAs<PackExpansionType>()->getPattern(); |
| 325 | |
| 326 | case Expression: |
| 327 | return cast<PackExpansionExpr>(getAsExpr())->getPattern(); |
| 328 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 329 | case TemplateExpansion: |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 330 | return TemplateArgument(getAsTemplateOrTemplatePattern()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 332 | case Declaration: |
| 333 | case Integral: |
| 334 | case Pack: |
| 335 | case Null: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 336 | case Template: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 337 | case NullPtr: |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 338 | return TemplateArgument(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 339 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 340 | |
| 341 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 344 | void TemplateArgument::print(const PrintingPolicy &Policy, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 345 | raw_ostream &Out) const { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 346 | switch (getKind()) { |
| 347 | case Null: |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame^] | 348 | Out << "(no value)"; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | case Type: { |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 352 | PrintingPolicy SubPolicy(Policy); |
| 353 | SubPolicy.SuppressStrongLifetime = true; |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 354 | getAsType().print(Out, SubPolicy); |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | |
| 358 | case Declaration: { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 359 | NamedDecl *ND = cast<NamedDecl>(getAsDecl()); |
David Blaikie | b8f6a8a | 2013-05-09 22:43:45 +0000 | [diff] [blame] | 360 | Out << '&'; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 361 | if (ND->getDeclName()) { |
| 362 | // FIXME: distinguish between pointer and reference args? |
David Blaikie | b8f6a8a | 2013-05-09 22:43:45 +0000 | [diff] [blame] | 363 | ND->printQualifiedName(Out); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 364 | } else { |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame^] | 365 | Out << "(anonymous)"; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 366 | } |
| 367 | break; |
| 368 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 369 | |
Abramo Bagnara | 4910701 | 2012-10-05 04:43:29 +0000 | [diff] [blame] | 370 | case NullPtr: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 371 | Out << "nullptr"; |
Abramo Bagnara | 4910701 | 2012-10-05 04:43:29 +0000 | [diff] [blame] | 372 | break; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 374 | case Template: |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 375 | getAsTemplate().print(Out, Policy); |
| 376 | break; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 377 | |
| 378 | case TemplateExpansion: |
| 379 | getAsTemplateOrTemplatePattern().print(Out, Policy); |
| 380 | Out << "..."; |
| 381 | break; |
| 382 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 383 | case Integral: { |
Chandler Carruth | 4c4f8de | 2011-02-19 00:21:00 +0000 | [diff] [blame] | 384 | printIntegral(*this, Out); |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 388 | case Expression: |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 389 | getAsExpr()->printPretty(Out, 0, Policy); |
| 390 | break; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 391 | |
| 392 | case Pack: |
| 393 | Out << "<"; |
| 394 | bool First = true; |
| 395 | for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end(); |
| 396 | P != PEnd; ++P) { |
| 397 | if (First) |
| 398 | First = false; |
| 399 | else |
| 400 | Out << ", "; |
| 401 | |
| 402 | P->print(Policy, Out); |
| 403 | } |
| 404 | Out << ">"; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 409 | //===----------------------------------------------------------------------===// |
| 410 | // TemplateArgumentLoc Implementation |
| 411 | //===----------------------------------------------------------------------===// |
| 412 | |
Douglas Gregor | df50b84 | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 413 | TemplateArgumentLocInfo::TemplateArgumentLocInfo() { |
Chandler Carruth | 6e1f9ba | 2011-04-28 08:19:45 +0000 | [diff] [blame] | 414 | memset((void*)this, 0, sizeof(TemplateArgumentLocInfo)); |
Douglas Gregor | df50b84 | 2011-01-06 00:33:28 +0000 | [diff] [blame] | 415 | } |
| 416 | |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 417 | SourceRange TemplateArgumentLoc::getSourceRange() const { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 418 | switch (Argument.getKind()) { |
| 419 | case TemplateArgument::Expression: |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 420 | return getSourceExpression()->getSourceRange(); |
Zhanyong Wan | 18ca8bf | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 421 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 422 | case TemplateArgument::Declaration: |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 423 | return getSourceDeclExpression()->getSourceRange(); |
Zhanyong Wan | 18ca8bf | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 424 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 425 | case TemplateArgument::NullPtr: |
| 426 | return getSourceNullPtrExpression()->getSourceRange(); |
| 427 | |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 428 | case TemplateArgument::Type: |
Zhanyong Wan | 18ca8bf | 2010-09-03 23:50:56 +0000 | [diff] [blame] | 429 | if (TypeSourceInfo *TSI = getTypeSourceInfo()) |
| 430 | return TSI->getTypeLoc().getSourceRange(); |
| 431 | else |
| 432 | return SourceRange(); |
| 433 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 434 | case TemplateArgument::Template: |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 435 | if (getTemplateQualifierLoc()) |
| 436 | return SourceRange(getTemplateQualifierLoc().getBeginLoc(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 437 | getTemplateNameLoc()); |
| 438 | return SourceRange(getTemplateNameLoc()); |
| 439 | |
| 440 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 441 | if (getTemplateQualifierLoc()) |
| 442 | return SourceRange(getTemplateQualifierLoc().getBeginLoc(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 443 | getTemplateEllipsisLoc()); |
| 444 | return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc()); |
| 445 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 446 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 447 | return getSourceIntegralExpression()->getSourceRange(); |
| 448 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 449 | case TemplateArgument::Pack: |
| 450 | case TemplateArgument::Null: |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 451 | return SourceRange(); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 452 | } |
| 453 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 454 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 455 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 456 | |
| 457 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 458 | const TemplateArgument &Arg) { |
| 459 | switch (Arg.getKind()) { |
| 460 | case TemplateArgument::Null: |
John McCall | 3b3a5ed | 2010-08-05 04:58:04 +0000 | [diff] [blame] | 461 | // This is bad, but not as bad as crashing because of argument |
| 462 | // count mismatches. |
| 463 | return DB << "(null template argument)"; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 464 | |
| 465 | case TemplateArgument::Type: |
| 466 | return DB << Arg.getAsType(); |
| 467 | |
| 468 | case TemplateArgument::Declaration: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 469 | return DB << Arg.getAsDecl(); |
| 470 | |
| 471 | case TemplateArgument::NullPtr: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 472 | return DB << "nullptr"; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 473 | |
| 474 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 475 | return DB << Arg.getAsIntegral().toString(10); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 476 | |
| 477 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 478 | return DB << Arg.getAsTemplate(); |
| 479 | |
| 480 | case TemplateArgument::TemplateExpansion: |
| 481 | return DB << Arg.getAsTemplateOrTemplatePattern() << "..."; |
| 482 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 483 | case TemplateArgument::Expression: { |
| 484 | // This shouldn't actually ever happen, so it's okay that we're |
| 485 | // regurgitating an expression here. |
| 486 | // FIXME: We're guessing at LangOptions! |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 487 | SmallString<32> Str; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 488 | llvm::raw_svector_ostream OS(Str); |
| 489 | LangOptions LangOpts; |
| 490 | LangOpts.CPlusPlus = true; |
| 491 | PrintingPolicy Policy(LangOpts); |
| 492 | Arg.getAsExpr()->printPretty(OS, 0, Policy); |
| 493 | return DB << OS.str(); |
| 494 | } |
| 495 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 496 | case TemplateArgument::Pack: { |
| 497 | // FIXME: We're guessing at LangOptions! |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 498 | SmallString<32> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 499 | llvm::raw_svector_ostream OS(Str); |
| 500 | LangOptions LangOpts; |
| 501 | LangOpts.CPlusPlus = true; |
| 502 | PrintingPolicy Policy(LangOpts); |
| 503 | Arg.print(Policy, OS); |
| 504 | return DB << OS.str(); |
| 505 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 506 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 507 | |
| 508 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 509 | } |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 510 | |
| 511 | const ASTTemplateArgumentListInfo * |
| 512 | ASTTemplateArgumentListInfo::Create(ASTContext &C, |
| 513 | const TemplateArgumentListInfo &List) { |
Richard Smith | fdf496c | 2014-03-28 23:32:39 +0000 | [diff] [blame] | 514 | assert(llvm::alignOf<ASTTemplateArgumentListInfo>() >= |
| 515 | llvm::alignOf<TemplateArgumentLoc>()); |
Richard Smith | f3bb0b2 | 2012-08-15 01:22:58 +0000 | [diff] [blame] | 516 | std::size_t size = ASTTemplateArgumentListInfo::sizeFor(List.size()); |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 517 | void *Mem = C.Allocate(size, llvm::alignOf<ASTTemplateArgumentListInfo>()); |
| 518 | ASTTemplateArgumentListInfo *TAI = new (Mem) ASTTemplateArgumentListInfo(); |
| 519 | TAI->initializeFrom(List); |
| 520 | return TAI; |
| 521 | } |
| 522 | |
| 523 | void ASTTemplateArgumentListInfo::initializeFrom( |
| 524 | const TemplateArgumentListInfo &Info) { |
| 525 | LAngleLoc = Info.getLAngleLoc(); |
| 526 | RAngleLoc = Info.getRAngleLoc(); |
| 527 | NumTemplateArgs = Info.size(); |
| 528 | |
| 529 | TemplateArgumentLoc *ArgBuffer = getTemplateArgs(); |
| 530 | for (unsigned i = 0; i != NumTemplateArgs; ++i) |
| 531 | new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]); |
| 532 | } |
| 533 | |
| 534 | void ASTTemplateArgumentListInfo::initializeFrom( |
| 535 | const TemplateArgumentListInfo &Info, |
| 536 | bool &Dependent, |
| 537 | bool &InstantiationDependent, |
| 538 | bool &ContainsUnexpandedParameterPack) { |
| 539 | LAngleLoc = Info.getLAngleLoc(); |
| 540 | RAngleLoc = Info.getRAngleLoc(); |
| 541 | NumTemplateArgs = Info.size(); |
| 542 | |
| 543 | TemplateArgumentLoc *ArgBuffer = getTemplateArgs(); |
| 544 | for (unsigned i = 0; i != NumTemplateArgs; ++i) { |
| 545 | Dependent = Dependent || Info[i].getArgument().isDependent(); |
| 546 | InstantiationDependent = InstantiationDependent || |
| 547 | Info[i].getArgument().isInstantiationDependent(); |
| 548 | ContainsUnexpandedParameterPack |
| 549 | = ContainsUnexpandedParameterPack || |
| 550 | Info[i].getArgument().containsUnexpandedParameterPack(); |
| 551 | |
| 552 | new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void ASTTemplateArgumentListInfo::copyInto( |
| 557 | TemplateArgumentListInfo &Info) const { |
| 558 | Info.setLAngleLoc(LAngleLoc); |
| 559 | Info.setRAngleLoc(RAngleLoc); |
| 560 | for (unsigned I = 0; I != NumTemplateArgs; ++I) |
| 561 | Info.addArgument(getTemplateArgs()[I]); |
| 562 | } |
| 563 | |
| 564 | std::size_t ASTTemplateArgumentListInfo::sizeFor(unsigned NumTemplateArgs) { |
| 565 | return sizeof(ASTTemplateArgumentListInfo) + |
| 566 | sizeof(TemplateArgumentLoc) * NumTemplateArgs; |
| 567 | } |
| 568 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 569 | void |
| 570 | ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc, |
| 571 | const TemplateArgumentListInfo &Info) { |
| 572 | Base::initializeFrom(Info); |
| 573 | setTemplateKeywordLoc(TemplateKWLoc); |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 574 | } |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 575 | |
| 576 | void |
| 577 | ASTTemplateKWAndArgsInfo |
| 578 | ::initializeFrom(SourceLocation TemplateKWLoc, |
| 579 | const TemplateArgumentListInfo &Info, |
| 580 | bool &Dependent, |
| 581 | bool &InstantiationDependent, |
| 582 | bool &ContainsUnexpandedParameterPack) { |
| 583 | Base::initializeFrom(Info, Dependent, InstantiationDependent, |
| 584 | ContainsUnexpandedParameterPack); |
| 585 | setTemplateKeywordLoc(TemplateKWLoc); |
| 586 | } |
| 587 | |
| 588 | void |
| 589 | ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) { |
| 590 | // No explicit template arguments, but template keyword loc is valid. |
| 591 | assert(TemplateKWLoc.isValid()); |
| 592 | LAngleLoc = SourceLocation(); |
| 593 | RAngleLoc = SourceLocation(); |
| 594 | NumTemplateArgs = 0; |
| 595 | setTemplateKeywordLoc(TemplateKWLoc); |
| 596 | } |
| 597 | |
| 598 | std::size_t |
| 599 | ASTTemplateKWAndArgsInfo::sizeFor(unsigned NumTemplateArgs) { |
| 600 | // Add space for the template keyword location. |
Richard Smith | f3bb0b2 | 2012-08-15 01:22:58 +0000 | [diff] [blame] | 601 | // FIXME: There's room for this in the padding before the template args in |
| 602 | // 64-bit builds. |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 603 | return Base::sizeFor(NumTemplateArgs) + sizeof(SourceLocation); |
| 604 | } |