TemplateBase.cpp 20 KB

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