TemplateBase.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. //===- TemplateBase.cpp - Common template AST class implementation --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements common classes used throughout C++ template
  10. // representations.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/TemplateBase.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclBase.h"
  17. #include "clang/AST/DeclTemplate.h"
  18. #include "clang/AST/DependenceFlags.h"
  19. #include "clang/AST/Expr.h"
  20. #include "clang/AST/ExprCXX.h"
  21. #include "clang/AST/PrettyPrinter.h"
  22. #include "clang/AST/TemplateName.h"
  23. #include "clang/AST/Type.h"
  24. #include "clang/AST/TypeLoc.h"
  25. #include "clang/Basic/Diagnostic.h"
  26. #include "clang/Basic/LLVM.h"
  27. #include "clang/Basic/LangOptions.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "llvm/ADT/APSInt.h"
  30. #include "llvm/ADT/FoldingSet.h"
  31. #include "llvm/ADT/None.h"
  32. #include "llvm/ADT/SmallString.h"
  33. #include "llvm/ADT/StringExtras.h"
  34. #include "llvm/ADT/StringRef.h"
  35. #include "llvm/Support/Casting.h"
  36. #include "llvm/Support/Compiler.h"
  37. #include "llvm/Support/ErrorHandling.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include <cassert>
  40. #include <cstddef>
  41. #include <cstdint>
  42. #include <cstring>
  43. using namespace clang;
  44. /// Print a template integral argument value.
  45. ///
  46. /// \param TemplArg the TemplateArgument instance to print.
  47. ///
  48. /// \param Out the raw_ostream instance to use for printing.
  49. ///
  50. /// \param Policy the printing policy for EnumConstantDecl printing.
  51. ///
  52. /// \param IncludeType If set, ensure that the type of the expression printed
  53. /// matches the type of the template argument.
  54. static void printIntegral(const TemplateArgument &TemplArg, raw_ostream &Out,
  55. const PrintingPolicy &Policy, bool IncludeType) {
  56. const Type *T = TemplArg.getIntegralType().getTypePtr();
  57. const llvm::APSInt &Val = TemplArg.getAsIntegral();
  58. if (const EnumType *ET = T->getAs<EnumType>()) {
  59. for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) {
  60. // In Sema::CheckTemplateArugment, enum template arguments value are
  61. // extended to the size of the integer underlying the enum type. This
  62. // may create a size difference between the enum value and template
  63. // argument value, requiring isSameValue here instead of operator==.
  64. if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
  65. ECD->printQualifiedName(Out, Policy);
  66. return;
  67. }
  68. }
  69. }
  70. if (Policy.MSVCFormatting)
  71. IncludeType = false;
  72. if (T->isBooleanType()) {
  73. if (!Policy.MSVCFormatting)
  74. Out << (Val.getBoolValue() ? "true" : "false");
  75. else
  76. Out << Val;
  77. } else if (T->isCharType()) {
  78. if (IncludeType) {
  79. if (T->isSpecificBuiltinType(BuiltinType::SChar))
  80. Out << "(signed char)";
  81. else if (T->isSpecificBuiltinType(BuiltinType::UChar))
  82. Out << "(unsigned char)";
  83. }
  84. CharacterLiteral::print(Val.getZExtValue(), CharacterLiteral::Ascii, Out);
  85. } else if (T->isAnyCharacterType() && !Policy.MSVCFormatting) {
  86. CharacterLiteral::CharacterKind Kind;
  87. if (T->isWideCharType())
  88. Kind = CharacterLiteral::Wide;
  89. else if (T->isChar8Type())
  90. Kind = CharacterLiteral::UTF8;
  91. else if (T->isChar16Type())
  92. Kind = CharacterLiteral::UTF16;
  93. else if (T->isChar32Type())
  94. Kind = CharacterLiteral::UTF32;
  95. else
  96. Kind = CharacterLiteral::Ascii;
  97. CharacterLiteral::print(Val.getExtValue(), Kind, Out);
  98. } else if (IncludeType) {
  99. if (const auto *BT = T->getAs<BuiltinType>()) {
  100. switch (BT->getKind()) {
  101. case BuiltinType::ULongLong:
  102. Out << Val << "ULL";
  103. break;
  104. case BuiltinType::LongLong:
  105. Out << Val << "LL";
  106. break;
  107. case BuiltinType::ULong:
  108. Out << Val << "UL";
  109. break;
  110. case BuiltinType::Long:
  111. Out << Val << "L";
  112. break;
  113. case BuiltinType::UInt:
  114. Out << Val << "U";
  115. break;
  116. case BuiltinType::Int:
  117. Out << Val;
  118. break;
  119. default:
  120. Out << "(" << T->getCanonicalTypeInternal().getAsString(Policy) << ")"
  121. << Val;
  122. break;
  123. }
  124. } else
  125. Out << "(" << T->getCanonicalTypeInternal().getAsString(Policy) << ")"
  126. << Val;
  127. } else
  128. Out << Val;
  129. }
  130. static unsigned getArrayDepth(QualType type) {
  131. unsigned count = 0;
  132. while (const auto *arrayType = type->getAsArrayTypeUnsafe()) {
  133. count++;
  134. type = arrayType->getElementType();
  135. }
  136. return count;
  137. }
  138. static bool needsAmpersandOnTemplateArg(QualType paramType, QualType argType) {
  139. // Generally, if the parameter type is a pointer, we must be taking the
  140. // address of something and need a &. However, if the argument is an array,
  141. // this could be implicit via array-to-pointer decay.
  142. if (!paramType->isPointerType())
  143. return paramType->isMemberPointerType();
  144. if (argType->isArrayType())
  145. return getArrayDepth(argType) == getArrayDepth(paramType->getPointeeType());
  146. return true;
  147. }
  148. //===----------------------------------------------------------------------===//
  149. // TemplateArgument Implementation
  150. //===----------------------------------------------------------------------===//
  151. TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value,
  152. QualType Type) {
  153. Integer.Kind = Integral;
  154. // Copy the APSInt value into our decomposed form.
  155. Integer.BitWidth = Value.getBitWidth();
  156. Integer.IsUnsigned = Value.isUnsigned();
  157. // If the value is large, we have to get additional memory from the ASTContext
  158. unsigned NumWords = Value.getNumWords();
  159. if (NumWords > 1) {
  160. void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t));
  161. std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t));
  162. Integer.pVal = static_cast<uint64_t *>(Mem);
  163. } else {
  164. Integer.VAL = Value.getZExtValue();
  165. }
  166. Integer.Type = Type.getAsOpaquePtr();
  167. }
  168. TemplateArgument
  169. TemplateArgument::CreatePackCopy(ASTContext &Context,
  170. ArrayRef<TemplateArgument> Args) {
  171. if (Args.empty())
  172. return getEmptyPack();
  173. return TemplateArgument(Args.copy(Context));
  174. }
  175. TemplateArgumentDependence TemplateArgument::getDependence() const {
  176. auto Deps = TemplateArgumentDependence::None;
  177. switch (getKind()) {
  178. case Null:
  179. llvm_unreachable("Should not have a NULL template argument");
  180. case Type:
  181. Deps = toTemplateArgumentDependence(getAsType()->getDependence());
  182. if (isa<PackExpansionType>(getAsType()))
  183. Deps |= TemplateArgumentDependence::Dependent;
  184. return Deps;
  185. case Template:
  186. return toTemplateArgumentDependence(getAsTemplate().getDependence());
  187. case TemplateExpansion:
  188. return TemplateArgumentDependence::Dependent |
  189. TemplateArgumentDependence::Instantiation;
  190. case Declaration: {
  191. auto *DC = dyn_cast<DeclContext>(getAsDecl());
  192. if (!DC)
  193. DC = getAsDecl()->getDeclContext();
  194. if (DC->isDependentContext())
  195. Deps = TemplateArgumentDependence::Dependent |
  196. TemplateArgumentDependence::Instantiation;
  197. return Deps;
  198. }
  199. case NullPtr:
  200. case Integral:
  201. return TemplateArgumentDependence::None;
  202. case Expression:
  203. Deps = toTemplateArgumentDependence(getAsExpr()->getDependence());
  204. if (isa<PackExpansionExpr>(getAsExpr()))
  205. Deps |= TemplateArgumentDependence::Dependent |
  206. TemplateArgumentDependence::Instantiation;
  207. return Deps;
  208. case Pack:
  209. for (const auto &P : pack_elements())
  210. Deps |= P.getDependence();
  211. return Deps;
  212. }
  213. llvm_unreachable("unhandled ArgKind");
  214. }
  215. bool TemplateArgument::isDependent() const {
  216. return getDependence() & TemplateArgumentDependence::Dependent;
  217. }
  218. bool TemplateArgument::isInstantiationDependent() const {
  219. return getDependence() & TemplateArgumentDependence::Instantiation;
  220. }
  221. bool TemplateArgument::isPackExpansion() const {
  222. switch (getKind()) {
  223. case Null:
  224. case Declaration:
  225. case Integral:
  226. case Pack:
  227. case Template:
  228. case NullPtr:
  229. return false;
  230. case TemplateExpansion:
  231. return true;
  232. case Type:
  233. return isa<PackExpansionType>(getAsType());
  234. case Expression:
  235. return isa<PackExpansionExpr>(getAsExpr());
  236. }
  237. llvm_unreachable("Invalid TemplateArgument Kind!");
  238. }
  239. bool TemplateArgument::containsUnexpandedParameterPack() const {
  240. return getDependence() & TemplateArgumentDependence::UnexpandedPack;
  241. }
  242. Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
  243. assert(getKind() == TemplateExpansion);
  244. if (TemplateArg.NumExpansions)
  245. return TemplateArg.NumExpansions - 1;
  246. return None;
  247. }
  248. QualType TemplateArgument::getNonTypeTemplateArgumentType() const {
  249. switch (getKind()) {
  250. case TemplateArgument::Null:
  251. case TemplateArgument::Type:
  252. case TemplateArgument::Template:
  253. case TemplateArgument::TemplateExpansion:
  254. case TemplateArgument::Pack:
  255. return QualType();
  256. case TemplateArgument::Integral:
  257. return getIntegralType();
  258. case TemplateArgument::Expression:
  259. return getAsExpr()->getType();
  260. case TemplateArgument::Declaration:
  261. return getParamTypeForDecl();
  262. case TemplateArgument::NullPtr:
  263. return getNullPtrType();
  264. }
  265. llvm_unreachable("Invalid TemplateArgument Kind!");
  266. }
  267. void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
  268. const ASTContext &Context) const {
  269. ID.AddInteger(getKind());
  270. switch (getKind()) {
  271. case Null:
  272. break;
  273. case Type:
  274. getAsType().Profile(ID);
  275. break;
  276. case NullPtr:
  277. getNullPtrType().Profile(ID);
  278. break;
  279. case Declaration:
  280. getParamTypeForDecl().Profile(ID);
  281. ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : nullptr);
  282. break;
  283. case Template:
  284. case TemplateExpansion: {
  285. TemplateName Template = getAsTemplateOrTemplatePattern();
  286. if (TemplateTemplateParmDecl *TTP
  287. = dyn_cast_or_null<TemplateTemplateParmDecl>(
  288. Template.getAsTemplateDecl())) {
  289. ID.AddBoolean(true);
  290. ID.AddInteger(TTP->getDepth());
  291. ID.AddInteger(TTP->getPosition());
  292. ID.AddBoolean(TTP->isParameterPack());
  293. } else {
  294. ID.AddBoolean(false);
  295. ID.AddPointer(Context.getCanonicalTemplateName(Template)
  296. .getAsVoidPointer());
  297. }
  298. break;
  299. }
  300. case Integral:
  301. getAsIntegral().Profile(ID);
  302. getIntegralType().Profile(ID);
  303. break;
  304. case Expression:
  305. getAsExpr()->Profile(ID, Context, true);
  306. break;
  307. case Pack:
  308. ID.AddInteger(Args.NumArgs);
  309. for (unsigned I = 0; I != Args.NumArgs; ++I)
  310. Args.Args[I].Profile(ID, Context);
  311. }
  312. }
  313. bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
  314. if (getKind() != Other.getKind()) return false;
  315. switch (getKind()) {
  316. case Null:
  317. case Type:
  318. case Expression:
  319. case NullPtr:
  320. return TypeOrValue.V == Other.TypeOrValue.V;
  321. case Template:
  322. case TemplateExpansion:
  323. return TemplateArg.Name == Other.TemplateArg.Name &&
  324. TemplateArg.NumExpansions == Other.TemplateArg.NumExpansions;
  325. case Declaration:
  326. return getAsDecl() == Other.getAsDecl();
  327. case Integral:
  328. return getIntegralType() == Other.getIntegralType() &&
  329. getAsIntegral() == Other.getAsIntegral();
  330. case Pack:
  331. if (Args.NumArgs != Other.Args.NumArgs) return false;
  332. for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
  333. if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
  334. return false;
  335. return true;
  336. }
  337. llvm_unreachable("Invalid TemplateArgument Kind!");
  338. }
  339. TemplateArgument TemplateArgument::getPackExpansionPattern() const {
  340. assert(isPackExpansion());
  341. switch (getKind()) {
  342. case Type:
  343. return getAsType()->castAs<PackExpansionType>()->getPattern();
  344. case Expression:
  345. return cast<PackExpansionExpr>(getAsExpr())->getPattern();
  346. case TemplateExpansion:
  347. return TemplateArgument(getAsTemplateOrTemplatePattern());
  348. case Declaration:
  349. case Integral:
  350. case Pack:
  351. case Null:
  352. case Template:
  353. case NullPtr:
  354. return TemplateArgument();
  355. }
  356. llvm_unreachable("Invalid TemplateArgument Kind!");
  357. }
  358. void TemplateArgument::print(const PrintingPolicy &Policy, raw_ostream &Out,
  359. bool IncludeType) const {
  360. switch (getKind()) {
  361. case Null:
  362. Out << "(no value)";
  363. break;
  364. case Type: {
  365. PrintingPolicy SubPolicy(Policy);
  366. SubPolicy.SuppressStrongLifetime = true;
  367. getAsType().print(Out, SubPolicy);
  368. break;
  369. }
  370. case Declaration: {
  371. // FIXME: Include the type if it's not obvious from the context.
  372. NamedDecl *ND = getAsDecl();
  373. if (getParamTypeForDecl()->isRecordType()) {
  374. if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
  375. TPO->printAsInit(Out);
  376. break;
  377. }
  378. }
  379. if (auto *VD = dyn_cast<ValueDecl>(ND)) {
  380. if (needsAmpersandOnTemplateArg(getParamTypeForDecl(), VD->getType()))
  381. Out << "&";
  382. }
  383. ND->printQualifiedName(Out);
  384. break;
  385. }
  386. case NullPtr:
  387. // FIXME: Include the type if it's not obvious from the context.
  388. Out << "nullptr";
  389. break;
  390. case Template:
  391. getAsTemplate().print(Out, Policy, TemplateName::Qualified::Fully);
  392. break;
  393. case TemplateExpansion:
  394. getAsTemplateOrTemplatePattern().print(Out, Policy);
  395. Out << "...";
  396. break;
  397. case Integral:
  398. printIntegral(*this, Out, Policy, IncludeType);
  399. break;
  400. case Expression:
  401. getAsExpr()->printPretty(Out, nullptr, Policy);
  402. break;
  403. case Pack:
  404. Out << "<";
  405. bool First = true;
  406. for (const auto &P : pack_elements()) {
  407. if (First)
  408. First = false;
  409. else
  410. Out << ", ";
  411. P.print(Policy, Out, IncludeType);
  412. }
  413. Out << ">";
  414. break;
  415. }
  416. }
  417. void TemplateArgument::dump(raw_ostream &Out) const {
  418. LangOptions LO; // FIXME! see also TemplateName::dump().
  419. LO.CPlusPlus = true;
  420. LO.Bool = true;
  421. print(PrintingPolicy(LO), Out, /*IncludeType*/ true);
  422. }
  423. LLVM_DUMP_METHOD void TemplateArgument::dump() const { dump(llvm::errs()); }
  424. //===----------------------------------------------------------------------===//
  425. // TemplateArgumentLoc Implementation
  426. //===----------------------------------------------------------------------===//
  427. SourceRange TemplateArgumentLoc::getSourceRange() const {
  428. switch (Argument.getKind()) {
  429. case TemplateArgument::Expression:
  430. return getSourceExpression()->getSourceRange();
  431. case TemplateArgument::Declaration:
  432. return getSourceDeclExpression()->getSourceRange();
  433. case TemplateArgument::NullPtr:
  434. return getSourceNullPtrExpression()->getSourceRange();
  435. case TemplateArgument::Type:
  436. if (TypeSourceInfo *TSI = getTypeSourceInfo())
  437. return TSI->getTypeLoc().getSourceRange();
  438. else
  439. return SourceRange();
  440. case TemplateArgument::Template:
  441. if (getTemplateQualifierLoc())
  442. return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
  443. getTemplateNameLoc());
  444. return SourceRange(getTemplateNameLoc());
  445. case TemplateArgument::TemplateExpansion:
  446. if (getTemplateQualifierLoc())
  447. return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
  448. getTemplateEllipsisLoc());
  449. return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
  450. case TemplateArgument::Integral:
  451. return getSourceIntegralExpression()->getSourceRange();
  452. case TemplateArgument::Pack:
  453. case TemplateArgument::Null:
  454. return SourceRange();
  455. }
  456. llvm_unreachable("Invalid TemplateArgument Kind!");
  457. }
  458. template <typename T>
  459. static const T &DiagTemplateArg(const T &DB, const TemplateArgument &Arg) {
  460. switch (Arg.getKind()) {
  461. case TemplateArgument::Null:
  462. // This is bad, but not as bad as crashing because of argument
  463. // count mismatches.
  464. return DB << "(null template argument)";
  465. case TemplateArgument::Type:
  466. return DB << Arg.getAsType();
  467. case TemplateArgument::Declaration:
  468. return DB << Arg.getAsDecl();
  469. case TemplateArgument::NullPtr:
  470. return DB << "nullptr";
  471. case TemplateArgument::Integral:
  472. return DB << toString(Arg.getAsIntegral(), 10);
  473. case TemplateArgument::Template:
  474. return DB << Arg.getAsTemplate();
  475. case TemplateArgument::TemplateExpansion:
  476. return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
  477. case TemplateArgument::Expression: {
  478. // This shouldn't actually ever happen, so it's okay that we're
  479. // regurgitating an expression here.
  480. // FIXME: We're guessing at LangOptions!
  481. SmallString<32> Str;
  482. llvm::raw_svector_ostream OS(Str);
  483. LangOptions LangOpts;
  484. LangOpts.CPlusPlus = true;
  485. PrintingPolicy Policy(LangOpts);
  486. Arg.getAsExpr()->printPretty(OS, nullptr, Policy);
  487. return DB << OS.str();
  488. }
  489. case TemplateArgument::Pack: {
  490. // FIXME: We're guessing at LangOptions!
  491. SmallString<32> Str;
  492. llvm::raw_svector_ostream OS(Str);
  493. LangOptions LangOpts;
  494. LangOpts.CPlusPlus = true;
  495. PrintingPolicy Policy(LangOpts);
  496. Arg.print(Policy, OS, /*IncludeType*/ true);
  497. return DB << OS.str();
  498. }
  499. }
  500. llvm_unreachable("Invalid TemplateArgument Kind!");
  501. }
  502. const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
  503. const TemplateArgument &Arg) {
  504. return DiagTemplateArg(DB, Arg);
  505. }
  506. clang::TemplateArgumentLocInfo::TemplateArgumentLocInfo(
  507. ASTContext &Ctx, NestedNameSpecifierLoc QualifierLoc,
  508. SourceLocation TemplateNameLoc, SourceLocation EllipsisLoc) {
  509. TemplateTemplateArgLocInfo *Template = new (Ctx) TemplateTemplateArgLocInfo;
  510. Template->Qualifier = QualifierLoc.getNestedNameSpecifier();
  511. Template->QualifierLocData = QualifierLoc.getOpaqueData();
  512. Template->TemplateNameLoc = TemplateNameLoc;
  513. Template->EllipsisLoc = EllipsisLoc;
  514. Pointer = Template;
  515. }
  516. const ASTTemplateArgumentListInfo *
  517. ASTTemplateArgumentListInfo::Create(const ASTContext &C,
  518. const TemplateArgumentListInfo &List) {
  519. std::size_t size = totalSizeToAlloc<TemplateArgumentLoc>(List.size());
  520. void *Mem = C.Allocate(size, alignof(ASTTemplateArgumentListInfo));
  521. return new (Mem) ASTTemplateArgumentListInfo(List);
  522. }
  523. ASTTemplateArgumentListInfo::ASTTemplateArgumentListInfo(
  524. const TemplateArgumentListInfo &Info) {
  525. LAngleLoc = Info.getLAngleLoc();
  526. RAngleLoc = Info.getRAngleLoc();
  527. NumTemplateArgs = Info.size();
  528. TemplateArgumentLoc *ArgBuffer = getTrailingObjects<TemplateArgumentLoc>();
  529. for (unsigned i = 0; i != NumTemplateArgs; ++i)
  530. new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
  531. }
  532. void ASTTemplateKWAndArgsInfo::initializeFrom(
  533. SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
  534. TemplateArgumentLoc *OutArgArray) {
  535. this->TemplateKWLoc = TemplateKWLoc;
  536. LAngleLoc = Info.getLAngleLoc();
  537. RAngleLoc = Info.getRAngleLoc();
  538. NumTemplateArgs = Info.size();
  539. for (unsigned i = 0; i != NumTemplateArgs; ++i)
  540. new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
  541. }
  542. void ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) {
  543. assert(TemplateKWLoc.isValid());
  544. LAngleLoc = SourceLocation();
  545. RAngleLoc = SourceLocation();
  546. this->TemplateKWLoc = TemplateKWLoc;
  547. NumTemplateArgs = 0;
  548. }
  549. void ASTTemplateKWAndArgsInfo::initializeFrom(
  550. SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
  551. TemplateArgumentLoc *OutArgArray, TemplateArgumentDependence &Deps) {
  552. this->TemplateKWLoc = TemplateKWLoc;
  553. LAngleLoc = Info.getLAngleLoc();
  554. RAngleLoc = Info.getRAngleLoc();
  555. NumTemplateArgs = Info.size();
  556. for (unsigned i = 0; i != NumTemplateArgs; ++i) {
  557. Deps |= Info[i].getArgument().getDependence();
  558. new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
  559. }
  560. }
  561. void ASTTemplateKWAndArgsInfo::copyInto(const TemplateArgumentLoc *ArgArray,
  562. TemplateArgumentListInfo &Info) const {
  563. Info.setLAngleLoc(LAngleLoc);
  564. Info.setRAngleLoc(RAngleLoc);
  565. for (unsigned I = 0; I != NumTemplateArgs; ++I)
  566. Info.addArgument(ArgArray[I]);
  567. }