DeclTemplate.h 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DeclTemplate.h - Classes for representing C++ templates --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// Defines the C++ template declaration subclasses.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
  19. #define LLVM_CLANG_AST_DECLTEMPLATE_H
  20. #include "clang/AST/ASTConcept.h"
  21. #include "clang/AST/Decl.h"
  22. #include "clang/AST/DeclBase.h"
  23. #include "clang/AST/DeclCXX.h"
  24. #include "clang/AST/DeclarationName.h"
  25. #include "clang/AST/Redeclarable.h"
  26. #include "clang/AST/TemplateBase.h"
  27. #include "clang/AST/Type.h"
  28. #include "clang/Basic/LLVM.h"
  29. #include "clang/Basic/SourceLocation.h"
  30. #include "clang/Basic/Specifiers.h"
  31. #include "llvm/ADT/ArrayRef.h"
  32. #include "llvm/ADT/FoldingSet.h"
  33. #include "llvm/ADT/PointerIntPair.h"
  34. #include "llvm/ADT/PointerUnion.h"
  35. #include "llvm/ADT/iterator.h"
  36. #include "llvm/ADT/iterator_range.h"
  37. #include "llvm/Support/Casting.h"
  38. #include "llvm/Support/Compiler.h"
  39. #include "llvm/Support/TrailingObjects.h"
  40. #include <cassert>
  41. #include <cstddef>
  42. #include <cstdint>
  43. #include <iterator>
  44. #include <utility>
  45. namespace clang {
  46. enum BuiltinTemplateKind : int;
  47. class ClassTemplateDecl;
  48. class ClassTemplatePartialSpecializationDecl;
  49. class Expr;
  50. class FunctionTemplateDecl;
  51. class IdentifierInfo;
  52. class NonTypeTemplateParmDecl;
  53. class TemplateDecl;
  54. class TemplateTemplateParmDecl;
  55. class TemplateTypeParmDecl;
  56. class ConceptDecl;
  57. class UnresolvedSetImpl;
  58. class VarTemplateDecl;
  59. class VarTemplatePartialSpecializationDecl;
  60. /// Stores a template parameter of any kind.
  61. using TemplateParameter =
  62. llvm::PointerUnion<TemplateTypeParmDecl *, NonTypeTemplateParmDecl *,
  63. TemplateTemplateParmDecl *>;
  64. NamedDecl *getAsNamedDecl(TemplateParameter P);
  65. /// Stores a list of template parameters for a TemplateDecl and its
  66. /// derived classes.
  67. class TemplateParameterList final
  68. : private llvm::TrailingObjects<TemplateParameterList, NamedDecl *,
  69. Expr *> {
  70. /// The location of the 'template' keyword.
  71. SourceLocation TemplateLoc;
  72. /// The locations of the '<' and '>' angle brackets.
  73. SourceLocation LAngleLoc, RAngleLoc;
  74. /// The number of template parameters in this template
  75. /// parameter list.
  76. unsigned NumParams : 29;
  77. /// Whether this template parameter list contains an unexpanded parameter
  78. /// pack.
  79. unsigned ContainsUnexpandedParameterPack : 1;
  80. /// Whether this template parameter list has a requires clause.
  81. unsigned HasRequiresClause : 1;
  82. /// Whether any of the template parameters has constrained-parameter
  83. /// constraint-expression.
  84. unsigned HasConstrainedParameters : 1;
  85. protected:
  86. TemplateParameterList(const ASTContext& C, SourceLocation TemplateLoc,
  87. SourceLocation LAngleLoc, ArrayRef<NamedDecl *> Params,
  88. SourceLocation RAngleLoc, Expr *RequiresClause);
  89. size_t numTrailingObjects(OverloadToken<NamedDecl *>) const {
  90. return NumParams;
  91. }
  92. size_t numTrailingObjects(OverloadToken<Expr *>) const {
  93. return HasRequiresClause ? 1 : 0;
  94. }
  95. public:
  96. template <size_t N, bool HasRequiresClause>
  97. friend class FixedSizeTemplateParameterListStorage;
  98. friend TrailingObjects;
  99. static TemplateParameterList *Create(const ASTContext &C,
  100. SourceLocation TemplateLoc,
  101. SourceLocation LAngleLoc,
  102. ArrayRef<NamedDecl *> Params,
  103. SourceLocation RAngleLoc,
  104. Expr *RequiresClause);
  105. /// Iterates through the template parameters in this list.
  106. using iterator = NamedDecl **;
  107. /// Iterates through the template parameters in this list.
  108. using const_iterator = NamedDecl * const *;
  109. iterator begin() { return getTrailingObjects<NamedDecl *>(); }
  110. const_iterator begin() const { return getTrailingObjects<NamedDecl *>(); }
  111. iterator end() { return begin() + NumParams; }
  112. const_iterator end() const { return begin() + NumParams; }
  113. unsigned size() const { return NumParams; }
  114. ArrayRef<NamedDecl*> asArray() {
  115. return llvm::makeArrayRef(begin(), end());
  116. }
  117. ArrayRef<const NamedDecl*> asArray() const {
  118. return llvm::makeArrayRef(begin(), size());
  119. }
  120. NamedDecl* getParam(unsigned Idx) {
  121. assert(Idx < size() && "Template parameter index out-of-range");
  122. return begin()[Idx];
  123. }
  124. const NamedDecl* getParam(unsigned Idx) const {
  125. assert(Idx < size() && "Template parameter index out-of-range");
  126. return begin()[Idx];
  127. }
  128. /// Returns the minimum number of arguments needed to form a
  129. /// template specialization.
  130. ///
  131. /// This may be fewer than the number of template parameters, if some of
  132. /// the parameters have default arguments or if there is a parameter pack.
  133. unsigned getMinRequiredArguments() const;
  134. /// Get the depth of this template parameter list in the set of
  135. /// template parameter lists.
  136. ///
  137. /// The first template parameter list in a declaration will have depth 0,
  138. /// the second template parameter list will have depth 1, etc.
  139. unsigned getDepth() const;
  140. /// Determine whether this template parameter list contains an
  141. /// unexpanded parameter pack.
  142. bool containsUnexpandedParameterPack() const;
  143. /// Determine whether this template parameter list contains a parameter pack.
  144. bool hasParameterPack() const {
  145. for (const NamedDecl *P : asArray())
  146. if (P->isParameterPack())
  147. return true;
  148. return false;
  149. }
  150. /// The constraint-expression of the associated requires-clause.
  151. Expr *getRequiresClause() {
  152. return HasRequiresClause ? getTrailingObjects<Expr *>()[0] : nullptr;
  153. }
  154. /// The constraint-expression of the associated requires-clause.
  155. const Expr *getRequiresClause() const {
  156. return HasRequiresClause ? getTrailingObjects<Expr *>()[0] : nullptr;
  157. }
  158. /// \brief All associated constraints derived from this template parameter
  159. /// list, including the requires clause and any constraints derived from
  160. /// constrained-parameters.
  161. ///
  162. /// The constraints in the resulting list are to be treated as if in a
  163. /// conjunction ("and").
  164. void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const;
  165. bool hasAssociatedConstraints() const;
  166. SourceLocation getTemplateLoc() const { return TemplateLoc; }
  167. SourceLocation getLAngleLoc() const { return LAngleLoc; }
  168. SourceLocation getRAngleLoc() const { return RAngleLoc; }
  169. SourceRange getSourceRange() const LLVM_READONLY {
  170. return SourceRange(TemplateLoc, RAngleLoc);
  171. }
  172. void print(raw_ostream &Out, const ASTContext &Context,
  173. bool OmitTemplateKW = false) const;
  174. void print(raw_ostream &Out, const ASTContext &Context,
  175. const PrintingPolicy &Policy, bool OmitTemplateKW = false) const;
  176. static bool shouldIncludeTypeForArgument(const PrintingPolicy &Policy,
  177. const TemplateParameterList *TPL,
  178. unsigned Idx);
  179. };
  180. /// Stores a list of template parameters and the associated
  181. /// requires-clause (if any) for a TemplateDecl and its derived classes.
  182. /// Suitable for creating on the stack.
  183. template <size_t N, bool HasRequiresClause>
  184. class FixedSizeTemplateParameterListStorage
  185. : public TemplateParameterList::FixedSizeStorageOwner {
  186. typename TemplateParameterList::FixedSizeStorage<
  187. NamedDecl *, Expr *>::with_counts<
  188. N, HasRequiresClause ? 1u : 0u
  189. >::type storage;
  190. public:
  191. FixedSizeTemplateParameterListStorage(const ASTContext &C,
  192. SourceLocation TemplateLoc,
  193. SourceLocation LAngleLoc,
  194. ArrayRef<NamedDecl *> Params,
  195. SourceLocation RAngleLoc,
  196. Expr *RequiresClause)
  197. : FixedSizeStorageOwner(
  198. (assert(N == Params.size()),
  199. assert(HasRequiresClause == (RequiresClause != nullptr)),
  200. new (static_cast<void *>(&storage)) TemplateParameterList(C,
  201. TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause))) {}
  202. };
  203. /// A template argument list.
  204. class TemplateArgumentList final
  205. : private llvm::TrailingObjects<TemplateArgumentList, TemplateArgument> {
  206. /// The template argument list.
  207. const TemplateArgument *Arguments;
  208. /// The number of template arguments in this template
  209. /// argument list.
  210. unsigned NumArguments;
  211. // Constructs an instance with an internal Argument list, containing
  212. // a copy of the Args array. (Called by CreateCopy)
  213. TemplateArgumentList(ArrayRef<TemplateArgument> Args);
  214. public:
  215. friend TrailingObjects;
  216. TemplateArgumentList(const TemplateArgumentList &) = delete;
  217. TemplateArgumentList &operator=(const TemplateArgumentList &) = delete;
  218. /// Type used to indicate that the template argument list itself is a
  219. /// stack object. It does not own its template arguments.
  220. enum OnStackType { OnStack };
  221. /// Create a new template argument list that copies the given set of
  222. /// template arguments.
  223. static TemplateArgumentList *CreateCopy(ASTContext &Context,
  224. ArrayRef<TemplateArgument> Args);
  225. /// Construct a new, temporary template argument list on the stack.
  226. ///
  227. /// The template argument list does not own the template arguments
  228. /// provided.
  229. explicit TemplateArgumentList(OnStackType, ArrayRef<TemplateArgument> Args)
  230. : Arguments(Args.data()), NumArguments(Args.size()) {}
  231. /// Produces a shallow copy of the given template argument list.
  232. ///
  233. /// This operation assumes that the input argument list outlives it.
  234. /// This takes the list as a pointer to avoid looking like a copy
  235. /// constructor, since this really really isn't safe to use that
  236. /// way.
  237. explicit TemplateArgumentList(const TemplateArgumentList *Other)
  238. : Arguments(Other->data()), NumArguments(Other->size()) {}
  239. /// Retrieve the template argument at a given index.
  240. const TemplateArgument &get(unsigned Idx) const {
  241. assert(Idx < NumArguments && "Invalid template argument index");
  242. return data()[Idx];
  243. }
  244. /// Retrieve the template argument at a given index.
  245. const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
  246. /// Produce this as an array ref.
  247. ArrayRef<TemplateArgument> asArray() const {
  248. return llvm::makeArrayRef(data(), size());
  249. }
  250. /// Retrieve the number of template arguments in this
  251. /// template argument list.
  252. unsigned size() const { return NumArguments; }
  253. /// Retrieve a pointer to the template argument list.
  254. const TemplateArgument *data() const { return Arguments; }
  255. };
  256. void *allocateDefaultArgStorageChain(const ASTContext &C);
  257. /// Storage for a default argument. This is conceptually either empty, or an
  258. /// argument value, or a pointer to a previous declaration that had a default
  259. /// argument.
  260. ///
  261. /// However, this is complicated by modules: while we require all the default
  262. /// arguments for a template to be equivalent, there may be more than one, and
  263. /// we need to track all the originating parameters to determine if the default
  264. /// argument is visible.
  265. template<typename ParmDecl, typename ArgType>
  266. class DefaultArgStorage {
  267. /// Storage for both the value *and* another parameter from which we inherit
  268. /// the default argument. This is used when multiple default arguments for a
  269. /// parameter are merged together from different modules.
  270. struct Chain {
  271. ParmDecl *PrevDeclWithDefaultArg;
  272. ArgType Value;
  273. };
  274. static_assert(sizeof(Chain) == sizeof(void *) * 2,
  275. "non-pointer argument type?");
  276. llvm::PointerUnion<ArgType, ParmDecl*, Chain*> ValueOrInherited;
  277. static ParmDecl *getParmOwningDefaultArg(ParmDecl *Parm) {
  278. const DefaultArgStorage &Storage = Parm->getDefaultArgStorage();
  279. if (auto *Prev = Storage.ValueOrInherited.template dyn_cast<ParmDecl *>())
  280. Parm = Prev;
  281. assert(!Parm->getDefaultArgStorage()
  282. .ValueOrInherited.template is<ParmDecl *>() &&
  283. "should only be one level of indirection");
  284. return Parm;
  285. }
  286. public:
  287. DefaultArgStorage() : ValueOrInherited(ArgType()) {}
  288. /// Determine whether there is a default argument for this parameter.
  289. bool isSet() const { return !ValueOrInherited.isNull(); }
  290. /// Determine whether the default argument for this parameter was inherited
  291. /// from a previous declaration of the same entity.
  292. bool isInherited() const { return ValueOrInherited.template is<ParmDecl*>(); }
  293. /// Get the default argument's value. This does not consider whether the
  294. /// default argument is visible.
  295. ArgType get() const {
  296. const DefaultArgStorage *Storage = this;
  297. if (const auto *Prev = ValueOrInherited.template dyn_cast<ParmDecl *>())
  298. Storage = &Prev->getDefaultArgStorage();
  299. if (const auto *C = Storage->ValueOrInherited.template dyn_cast<Chain *>())
  300. return C->Value;
  301. return Storage->ValueOrInherited.template get<ArgType>();
  302. }
  303. /// Get the parameter from which we inherit the default argument, if any.
  304. /// This is the parameter on which the default argument was actually written.
  305. const ParmDecl *getInheritedFrom() const {
  306. if (const auto *D = ValueOrInherited.template dyn_cast<ParmDecl *>())
  307. return D;
  308. if (const auto *C = ValueOrInherited.template dyn_cast<Chain *>())
  309. return C->PrevDeclWithDefaultArg;
  310. return nullptr;
  311. }
  312. /// Set the default argument.
  313. void set(ArgType Arg) {
  314. assert(!isSet() && "default argument already set");
  315. ValueOrInherited = Arg;
  316. }
  317. /// Set that the default argument was inherited from another parameter.
  318. void setInherited(const ASTContext &C, ParmDecl *InheritedFrom) {
  319. assert(!isInherited() && "default argument already inherited");
  320. InheritedFrom = getParmOwningDefaultArg(InheritedFrom);
  321. if (!isSet())
  322. ValueOrInherited = InheritedFrom;
  323. else
  324. ValueOrInherited = new (allocateDefaultArgStorageChain(C))
  325. Chain{InheritedFrom, ValueOrInherited.template get<ArgType>()};
  326. }
  327. /// Remove the default argument, even if it was inherited.
  328. void clear() {
  329. ValueOrInherited = ArgType();
  330. }
  331. };
  332. //===----------------------------------------------------------------------===//
  333. // Kinds of Templates
  334. //===----------------------------------------------------------------------===//
  335. /// \brief The base class of all kinds of template declarations (e.g.,
  336. /// class, function, etc.).
  337. ///
  338. /// The TemplateDecl class stores the list of template parameters and a
  339. /// reference to the templated scoped declaration: the underlying AST node.
  340. class TemplateDecl : public NamedDecl {
  341. void anchor() override;
  342. protected:
  343. // Construct a template decl with name, parameters, and templated element.
  344. TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name,
  345. TemplateParameterList *Params, NamedDecl *Decl);
  346. // Construct a template decl with the given name and parameters.
  347. // Used when there is no templated element (e.g., for tt-params).
  348. TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name,
  349. TemplateParameterList *Params)
  350. : TemplateDecl(DK, DC, L, Name, Params, nullptr) {}
  351. public:
  352. friend class ASTDeclReader;
  353. friend class ASTDeclWriter;
  354. /// Get the list of template parameters
  355. TemplateParameterList *getTemplateParameters() const {
  356. return TemplateParams;
  357. }
  358. /// \brief Get the total constraint-expression associated with this template,
  359. /// including constraint-expressions derived from the requires-clause,
  360. /// trailing requires-clause (for functions and methods) and constrained
  361. /// template parameters.
  362. void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const;
  363. bool hasAssociatedConstraints() const;
  364. /// Get the underlying, templated declaration.
  365. NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
  366. // Implement isa/cast/dyncast/etc.
  367. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  368. static bool classofKind(Kind K) {
  369. return K >= firstTemplate && K <= lastTemplate;
  370. }
  371. SourceRange getSourceRange() const override LLVM_READONLY {
  372. return SourceRange(getTemplateParameters()->getTemplateLoc(),
  373. TemplatedDecl->getSourceRange().getEnd());
  374. }
  375. protected:
  376. NamedDecl *TemplatedDecl;
  377. TemplateParameterList *TemplateParams;
  378. void setTemplateParameters(TemplateParameterList *TParams) {
  379. TemplateParams = TParams;
  380. }
  381. public:
  382. /// Initialize the underlying templated declaration and
  383. /// template parameters.
  384. void init(NamedDecl *templatedDecl, TemplateParameterList* templateParams) {
  385. assert(!TemplatedDecl && "TemplatedDecl already set!");
  386. assert(!TemplateParams && "TemplateParams already set!");
  387. TemplatedDecl = templatedDecl;
  388. TemplateParams = templateParams;
  389. }
  390. };
  391. /// Provides information about a function template specialization,
  392. /// which is a FunctionDecl that has been explicitly specialization or
  393. /// instantiated from a function template.
  394. class FunctionTemplateSpecializationInfo final
  395. : public llvm::FoldingSetNode,
  396. private llvm::TrailingObjects<FunctionTemplateSpecializationInfo,
  397. MemberSpecializationInfo *> {
  398. /// The function template specialization that this structure describes and a
  399. /// flag indicating if the function is a member specialization.
  400. llvm::PointerIntPair<FunctionDecl *, 1, bool> Function;
  401. /// The function template from which this function template
  402. /// specialization was generated.
  403. ///
  404. /// The two bits contain the top 4 values of TemplateSpecializationKind.
  405. llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
  406. public:
  407. /// The template arguments used to produce the function template
  408. /// specialization from the function template.
  409. const TemplateArgumentList *TemplateArguments;
  410. /// The template arguments as written in the sources, if provided.
  411. /// FIXME: Normally null; tail-allocate this.
  412. const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten;
  413. /// The point at which this function template specialization was
  414. /// first instantiated.
  415. SourceLocation PointOfInstantiation;
  416. private:
  417. FunctionTemplateSpecializationInfo(
  418. FunctionDecl *FD, FunctionTemplateDecl *Template,
  419. TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
  420. const ASTTemplateArgumentListInfo *TemplateArgsAsWritten,
  421. SourceLocation POI, MemberSpecializationInfo *MSInfo)
  422. : Function(FD, MSInfo ? true : false), Template(Template, TSK - 1),
  423. TemplateArguments(TemplateArgs),
  424. TemplateArgumentsAsWritten(TemplateArgsAsWritten),
  425. PointOfInstantiation(POI) {
  426. if (MSInfo)
  427. getTrailingObjects<MemberSpecializationInfo *>()[0] = MSInfo;
  428. }
  429. size_t numTrailingObjects(OverloadToken<MemberSpecializationInfo*>) const {
  430. return Function.getInt();
  431. }
  432. public:
  433. friend TrailingObjects;
  434. static FunctionTemplateSpecializationInfo *
  435. Create(ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
  436. TemplateSpecializationKind TSK,
  437. const TemplateArgumentList *TemplateArgs,
  438. const TemplateArgumentListInfo *TemplateArgsAsWritten,
  439. SourceLocation POI, MemberSpecializationInfo *MSInfo);
  440. /// Retrieve the declaration of the function template specialization.
  441. FunctionDecl *getFunction() const { return Function.getPointer(); }
  442. /// Retrieve the template from which this function was specialized.
  443. FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
  444. /// Determine what kind of template specialization this is.
  445. TemplateSpecializationKind getTemplateSpecializationKind() const {
  446. return (TemplateSpecializationKind)(Template.getInt() + 1);
  447. }
  448. bool isExplicitSpecialization() const {
  449. return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
  450. }
  451. /// True if this declaration is an explicit specialization,
  452. /// explicit instantiation declaration, or explicit instantiation
  453. /// definition.
  454. bool isExplicitInstantiationOrSpecialization() const {
  455. return isTemplateExplicitInstantiationOrSpecialization(
  456. getTemplateSpecializationKind());
  457. }
  458. /// Set the template specialization kind.
  459. void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
  460. assert(TSK != TSK_Undeclared &&
  461. "Cannot encode TSK_Undeclared for a function template specialization");
  462. Template.setInt(TSK - 1);
  463. }
  464. /// Retrieve the first point of instantiation of this function
  465. /// template specialization.
  466. ///
  467. /// The point of instantiation may be an invalid source location if this
  468. /// function has yet to be instantiated.
  469. SourceLocation getPointOfInstantiation() const {
  470. return PointOfInstantiation;
  471. }
  472. /// Set the (first) point of instantiation of this function template
  473. /// specialization.
  474. void setPointOfInstantiation(SourceLocation POI) {
  475. PointOfInstantiation = POI;
  476. }
  477. /// Get the specialization info if this function template specialization is
  478. /// also a member specialization:
  479. ///
  480. /// \code
  481. /// template<typename> struct A {
  482. /// template<typename> void f();
  483. /// template<> void f<int>(); // ClassScopeFunctionSpecializationDecl
  484. /// };
  485. /// \endcode
  486. ///
  487. /// Here, A<int>::f<int> is a function template specialization that is
  488. /// an explicit specialization of A<int>::f, but it's also a member
  489. /// specialization (an implicit instantiation in this case) of A::f<int>.
  490. /// Further:
  491. ///
  492. /// \code
  493. /// template<> template<> void A<int>::f<int>() {}
  494. /// \endcode
  495. ///
  496. /// ... declares a function template specialization that is an explicit
  497. /// specialization of A<int>::f, and is also an explicit member
  498. /// specialization of A::f<int>.
  499. ///
  500. /// Note that the TemplateSpecializationKind of the MemberSpecializationInfo
  501. /// need not be the same as that returned by getTemplateSpecializationKind(),
  502. /// and represents the relationship between the function and the class-scope
  503. /// explicit specialization in the original templated class -- whereas our
  504. /// TemplateSpecializationKind represents the relationship between the
  505. /// function and the function template, and should always be
  506. /// TSK_ExplicitSpecialization whenever we have MemberSpecializationInfo.
  507. MemberSpecializationInfo *getMemberSpecializationInfo() const {
  508. return numTrailingObjects(OverloadToken<MemberSpecializationInfo *>())
  509. ? getTrailingObjects<MemberSpecializationInfo *>()[0]
  510. : nullptr;
  511. }
  512. void Profile(llvm::FoldingSetNodeID &ID) {
  513. Profile(ID, TemplateArguments->asArray(), getFunction()->getASTContext());
  514. }
  515. static void
  516. Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
  517. ASTContext &Context) {
  518. ID.AddInteger(TemplateArgs.size());
  519. for (const TemplateArgument &TemplateArg : TemplateArgs)
  520. TemplateArg.Profile(ID, Context);
  521. }
  522. };
  523. /// Provides information a specialization of a member of a class
  524. /// template, which may be a member function, static data member,
  525. /// member class or member enumeration.
  526. class MemberSpecializationInfo {
  527. // The member declaration from which this member was instantiated, and the
  528. // manner in which the instantiation occurred (in the lower two bits).
  529. llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
  530. // The point at which this member was first instantiated.
  531. SourceLocation PointOfInstantiation;
  532. public:
  533. explicit
  534. MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK,
  535. SourceLocation POI = SourceLocation())
  536. : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) {
  537. assert(TSK != TSK_Undeclared &&
  538. "Cannot encode undeclared template specializations for members");
  539. }
  540. /// Retrieve the member declaration from which this member was
  541. /// instantiated.
  542. NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
  543. /// Determine what kind of template specialization this is.
  544. TemplateSpecializationKind getTemplateSpecializationKind() const {
  545. return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
  546. }
  547. bool isExplicitSpecialization() const {
  548. return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
  549. }
  550. /// Set the template specialization kind.
  551. void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
  552. assert(TSK != TSK_Undeclared &&
  553. "Cannot encode undeclared template specializations for members");
  554. MemberAndTSK.setInt(TSK - 1);
  555. }
  556. /// Retrieve the first point of instantiation of this member.
  557. /// If the point of instantiation is an invalid location, then this member
  558. /// has not yet been instantiated.
  559. SourceLocation getPointOfInstantiation() const {
  560. return PointOfInstantiation;
  561. }
  562. /// Set the first point of instantiation.
  563. void setPointOfInstantiation(SourceLocation POI) {
  564. PointOfInstantiation = POI;
  565. }
  566. };
  567. /// Provides information about a dependent function-template
  568. /// specialization declaration.
  569. ///
  570. /// Since explicit function template specialization and instantiation
  571. /// declarations can only appear in namespace scope, and you can only
  572. /// specialize a member of a fully-specialized class, the only way to
  573. /// get one of these is in a friend declaration like the following:
  574. ///
  575. /// \code
  576. /// template \<class T> void foo(T);
  577. /// template \<class T> class A {
  578. /// friend void foo<>(T);
  579. /// };
  580. /// \endcode
  581. class DependentFunctionTemplateSpecializationInfo final
  582. : private llvm::TrailingObjects<DependentFunctionTemplateSpecializationInfo,
  583. TemplateArgumentLoc,
  584. FunctionTemplateDecl *> {
  585. /// The number of potential template candidates.
  586. unsigned NumTemplates;
  587. /// The number of template arguments.
  588. unsigned NumArgs;
  589. /// The locations of the left and right angle brackets.
  590. SourceRange AngleLocs;
  591. size_t numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
  592. return NumArgs;
  593. }
  594. size_t numTrailingObjects(OverloadToken<FunctionTemplateDecl *>) const {
  595. return NumTemplates;
  596. }
  597. DependentFunctionTemplateSpecializationInfo(
  598. const UnresolvedSetImpl &Templates,
  599. const TemplateArgumentListInfo &TemplateArgs);
  600. public:
  601. friend TrailingObjects;
  602. static DependentFunctionTemplateSpecializationInfo *
  603. Create(ASTContext &Context, const UnresolvedSetImpl &Templates,
  604. const TemplateArgumentListInfo &TemplateArgs);
  605. /// Returns the number of function templates that this might
  606. /// be a specialization of.
  607. unsigned getNumTemplates() const { return NumTemplates; }
  608. /// Returns the i'th template candidate.
  609. FunctionTemplateDecl *getTemplate(unsigned I) const {
  610. assert(I < getNumTemplates() && "template index out of range");
  611. return getTrailingObjects<FunctionTemplateDecl *>()[I];
  612. }
  613. /// Returns the explicit template arguments that were given.
  614. const TemplateArgumentLoc *getTemplateArgs() const {
  615. return getTrailingObjects<TemplateArgumentLoc>();
  616. }
  617. /// Returns the number of explicit template arguments that were given.
  618. unsigned getNumTemplateArgs() const { return NumArgs; }
  619. llvm::ArrayRef<TemplateArgumentLoc> arguments() const {
  620. return llvm::makeArrayRef(getTemplateArgs(), getNumTemplateArgs());
  621. }
  622. /// Returns the nth template argument.
  623. const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
  624. assert(I < getNumTemplateArgs() && "template arg index out of range");
  625. return getTemplateArgs()[I];
  626. }
  627. SourceLocation getLAngleLoc() const {
  628. return AngleLocs.getBegin();
  629. }
  630. SourceLocation getRAngleLoc() const {
  631. return AngleLocs.getEnd();
  632. }
  633. };
  634. /// Declaration of a redeclarable template.
  635. class RedeclarableTemplateDecl : public TemplateDecl,
  636. public Redeclarable<RedeclarableTemplateDecl>
  637. {
  638. using redeclarable_base = Redeclarable<RedeclarableTemplateDecl>;
  639. RedeclarableTemplateDecl *getNextRedeclarationImpl() override {
  640. return getNextRedeclaration();
  641. }
  642. RedeclarableTemplateDecl *getPreviousDeclImpl() override {
  643. return getPreviousDecl();
  644. }
  645. RedeclarableTemplateDecl *getMostRecentDeclImpl() override {
  646. return getMostRecentDecl();
  647. }
  648. void anchor() override;
  649. protected:
  650. template <typename EntryType> struct SpecEntryTraits {
  651. using DeclType = EntryType;
  652. static DeclType *getDecl(EntryType *D) {
  653. return D;
  654. }
  655. static ArrayRef<TemplateArgument> getTemplateArgs(EntryType *D) {
  656. return D->getTemplateArgs().asArray();
  657. }
  658. };
  659. template <typename EntryType, typename SETraits = SpecEntryTraits<EntryType>,
  660. typename DeclType = typename SETraits::DeclType>
  661. struct SpecIterator
  662. : llvm::iterator_adaptor_base<
  663. SpecIterator<EntryType, SETraits, DeclType>,
  664. typename llvm::FoldingSetVector<EntryType>::iterator,
  665. typename std::iterator_traits<typename llvm::FoldingSetVector<
  666. EntryType>::iterator>::iterator_category,
  667. DeclType *, ptrdiff_t, DeclType *, DeclType *> {
  668. SpecIterator() = default;
  669. explicit SpecIterator(
  670. typename llvm::FoldingSetVector<EntryType>::iterator SetIter)
  671. : SpecIterator::iterator_adaptor_base(std::move(SetIter)) {}
  672. DeclType *operator*() const {
  673. return SETraits::getDecl(&*this->I)->getMostRecentDecl();
  674. }
  675. DeclType *operator->() const { return **this; }
  676. };
  677. template <typename EntryType>
  678. static SpecIterator<EntryType>
  679. makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) {
  680. return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin());
  681. }
  682. void loadLazySpecializationsImpl() const;
  683. template <class EntryType, typename ...ProfileArguments>
  684. typename SpecEntryTraits<EntryType>::DeclType*
  685. findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
  686. void *&InsertPos, ProfileArguments &&...ProfileArgs);
  687. template <class Derived, class EntryType>
  688. void addSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
  689. EntryType *Entry, void *InsertPos);
  690. struct CommonBase {
  691. CommonBase() : InstantiatedFromMember(nullptr, false) {}
  692. /// The template from which this was most
  693. /// directly instantiated (or null).
  694. ///
  695. /// The boolean value indicates whether this template
  696. /// was explicitly specialized.
  697. llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
  698. InstantiatedFromMember;
  699. /// If non-null, points to an array of specializations (including
  700. /// partial specializations) known only by their external declaration IDs.
  701. ///
  702. /// The first value in the array is the number of specializations/partial
  703. /// specializations that follow.
  704. uint32_t *LazySpecializations = nullptr;
  705. };
  706. /// Pointer to the common data shared by all declarations of this
  707. /// template.
  708. mutable CommonBase *Common = nullptr;
  709. /// Retrieves the "common" pointer shared by all (re-)declarations of
  710. /// the same template. Calling this routine may implicitly allocate memory
  711. /// for the common pointer.
  712. CommonBase *getCommonPtr() const;
  713. virtual CommonBase *newCommon(ASTContext &C) const = 0;
  714. // Construct a template decl with name, parameters, and templated element.
  715. RedeclarableTemplateDecl(Kind DK, ASTContext &C, DeclContext *DC,
  716. SourceLocation L, DeclarationName Name,
  717. TemplateParameterList *Params, NamedDecl *Decl)
  718. : TemplateDecl(DK, DC, L, Name, Params, Decl), redeclarable_base(C) {}
  719. public:
  720. friend class ASTDeclReader;
  721. friend class ASTDeclWriter;
  722. friend class ASTReader;
  723. template <class decl_type> friend class RedeclarableTemplate;
  724. /// Retrieves the canonical declaration of this template.
  725. RedeclarableTemplateDecl *getCanonicalDecl() override {
  726. return getFirstDecl();
  727. }
  728. const RedeclarableTemplateDecl *getCanonicalDecl() const {
  729. return getFirstDecl();
  730. }
  731. /// Determines whether this template was a specialization of a
  732. /// member template.
  733. ///
  734. /// In the following example, the function template \c X<int>::f and the
  735. /// member template \c X<int>::Inner are member specializations.
  736. ///
  737. /// \code
  738. /// template<typename T>
  739. /// struct X {
  740. /// template<typename U> void f(T, U);
  741. /// template<typename U> struct Inner;
  742. /// };
  743. ///
  744. /// template<> template<typename T>
  745. /// void X<int>::f(int, T);
  746. /// template<> template<typename T>
  747. /// struct X<int>::Inner { /* ... */ };
  748. /// \endcode
  749. bool isMemberSpecialization() const {
  750. return getCommonPtr()->InstantiatedFromMember.getInt();
  751. }
  752. /// Note that this member template is a specialization.
  753. void setMemberSpecialization() {
  754. assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
  755. "Only member templates can be member template specializations");
  756. getCommonPtr()->InstantiatedFromMember.setInt(true);
  757. }
  758. /// Retrieve the member template from which this template was
  759. /// instantiated, or nullptr if this template was not instantiated from a
  760. /// member template.
  761. ///
  762. /// A template is instantiated from a member template when the member
  763. /// template itself is part of a class template (or member thereof). For
  764. /// example, given
  765. ///
  766. /// \code
  767. /// template<typename T>
  768. /// struct X {
  769. /// template<typename U> void f(T, U);
  770. /// };
  771. ///
  772. /// void test(X<int> x) {
  773. /// x.f(1, 'a');
  774. /// };
  775. /// \endcode
  776. ///
  777. /// \c X<int>::f is a FunctionTemplateDecl that describes the function
  778. /// template
  779. ///
  780. /// \code
  781. /// template<typename U> void X<int>::f(int, U);
  782. /// \endcode
  783. ///
  784. /// which was itself created during the instantiation of \c X<int>. Calling
  785. /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will
  786. /// retrieve the FunctionTemplateDecl for the original template \c f within
  787. /// the class template \c X<T>, i.e.,
  788. ///
  789. /// \code
  790. /// template<typename T>
  791. /// template<typename U>
  792. /// void X<T>::f(T, U);
  793. /// \endcode
  794. RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const {
  795. return getCommonPtr()->InstantiatedFromMember.getPointer();
  796. }
  797. void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) {
  798. assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
  799. getCommonPtr()->InstantiatedFromMember.setPointer(TD);
  800. }
  801. using redecl_range = redeclarable_base::redecl_range;
  802. using redecl_iterator = redeclarable_base::redecl_iterator;
  803. using redeclarable_base::redecls_begin;
  804. using redeclarable_base::redecls_end;
  805. using redeclarable_base::redecls;
  806. using redeclarable_base::getPreviousDecl;
  807. using redeclarable_base::getMostRecentDecl;
  808. using redeclarable_base::isFirstDecl;
  809. // Implement isa/cast/dyncast/etc.
  810. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  811. static bool classofKind(Kind K) {
  812. return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate;
  813. }
  814. };
  815. template <> struct RedeclarableTemplateDecl::
  816. SpecEntryTraits<FunctionTemplateSpecializationInfo> {
  817. using DeclType = FunctionDecl;
  818. static DeclType *getDecl(FunctionTemplateSpecializationInfo *I) {
  819. return I->getFunction();
  820. }
  821. static ArrayRef<TemplateArgument>
  822. getTemplateArgs(FunctionTemplateSpecializationInfo *I) {
  823. return I->TemplateArguments->asArray();
  824. }
  825. };
  826. /// Declaration of a template function.
  827. class FunctionTemplateDecl : public RedeclarableTemplateDecl {
  828. protected:
  829. friend class FunctionDecl;
  830. /// Data that is common to all of the declarations of a given
  831. /// function template.
  832. struct Common : CommonBase {
  833. /// The function template specializations for this function
  834. /// template, including explicit specializations and instantiations.
  835. llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations;
  836. /// The set of "injected" template arguments used within this
  837. /// function template.
  838. ///
  839. /// This pointer refers to the template arguments (there are as
  840. /// many template arguments as template parameaters) for the function
  841. /// template, and is allocated lazily, since most function templates do not
  842. /// require the use of this information.
  843. TemplateArgument *InjectedArgs = nullptr;
  844. Common() = default;
  845. };
  846. FunctionTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
  847. DeclarationName Name, TemplateParameterList *Params,
  848. NamedDecl *Decl)
  849. : RedeclarableTemplateDecl(FunctionTemplate, C, DC, L, Name, Params,
  850. Decl) {}
  851. CommonBase *newCommon(ASTContext &C) const override;
  852. Common *getCommonPtr() const {
  853. return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
  854. }
  855. /// Retrieve the set of function template specializations of this
  856. /// function template.
  857. llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
  858. getSpecializations() const;
  859. /// Add a specialization of this function template.
  860. ///
  861. /// \param InsertPos Insert position in the FoldingSetVector, must have been
  862. /// retrieved by an earlier call to findSpecialization().
  863. void addSpecialization(FunctionTemplateSpecializationInfo* Info,
  864. void *InsertPos);
  865. public:
  866. friend class ASTDeclReader;
  867. friend class ASTDeclWriter;
  868. /// Load any lazily-loaded specializations from the external source.
  869. void LoadLazySpecializations() const;
  870. /// Get the underlying function declaration of the template.
  871. FunctionDecl *getTemplatedDecl() const {
  872. return static_cast<FunctionDecl *>(TemplatedDecl);
  873. }
  874. /// Returns whether this template declaration defines the primary
  875. /// pattern.
  876. bool isThisDeclarationADefinition() const {
  877. return getTemplatedDecl()->isThisDeclarationADefinition();
  878. }
  879. /// Return the specialization with the provided arguments if it exists,
  880. /// otherwise return the insertion point.
  881. FunctionDecl *findSpecialization(ArrayRef<TemplateArgument> Args,
  882. void *&InsertPos);
  883. FunctionTemplateDecl *getCanonicalDecl() override {
  884. return cast<FunctionTemplateDecl>(
  885. RedeclarableTemplateDecl::getCanonicalDecl());
  886. }
  887. const FunctionTemplateDecl *getCanonicalDecl() const {
  888. return cast<FunctionTemplateDecl>(
  889. RedeclarableTemplateDecl::getCanonicalDecl());
  890. }
  891. /// Retrieve the previous declaration of this function template, or
  892. /// nullptr if no such declaration exists.
  893. FunctionTemplateDecl *getPreviousDecl() {
  894. return cast_or_null<FunctionTemplateDecl>(
  895. static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
  896. }
  897. const FunctionTemplateDecl *getPreviousDecl() const {
  898. return cast_or_null<FunctionTemplateDecl>(
  899. static_cast<const RedeclarableTemplateDecl *>(this)->getPreviousDecl());
  900. }
  901. FunctionTemplateDecl *getMostRecentDecl() {
  902. return cast<FunctionTemplateDecl>(
  903. static_cast<RedeclarableTemplateDecl *>(this)
  904. ->getMostRecentDecl());
  905. }
  906. const FunctionTemplateDecl *getMostRecentDecl() const {
  907. return const_cast<FunctionTemplateDecl*>(this)->getMostRecentDecl();
  908. }
  909. FunctionTemplateDecl *getInstantiatedFromMemberTemplate() const {
  910. return cast_or_null<FunctionTemplateDecl>(
  911. RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
  912. }
  913. using spec_iterator = SpecIterator<FunctionTemplateSpecializationInfo>;
  914. using spec_range = llvm::iterator_range<spec_iterator>;
  915. spec_range specializations() const {
  916. return spec_range(spec_begin(), spec_end());
  917. }
  918. spec_iterator spec_begin() const {
  919. return makeSpecIterator(getSpecializations(), false);
  920. }
  921. spec_iterator spec_end() const {
  922. return makeSpecIterator(getSpecializations(), true);
  923. }
  924. /// Retrieve the "injected" template arguments that correspond to the
  925. /// template parameters of this function template.
  926. ///
  927. /// Although the C++ standard has no notion of the "injected" template
  928. /// arguments for a function template, the notion is convenient when
  929. /// we need to perform substitutions inside the definition of a function
  930. /// template.
  931. ArrayRef<TemplateArgument> getInjectedTemplateArgs();
  932. /// Return whether this function template is an abbreviated function template,
  933. /// e.g. `void foo(auto x)` or `template<typename T> void foo(auto x)`
  934. bool isAbbreviated() const {
  935. // Since the invented template parameters generated from 'auto' parameters
  936. // are either appended to the end of the explicit template parameter list or
  937. // form a new template paramter list, we can simply observe the last
  938. // parameter to determine if such a thing happened.
  939. const TemplateParameterList *TPL = getTemplateParameters();
  940. return TPL->getParam(TPL->size() - 1)->isImplicit();
  941. }
  942. /// Merge \p Prev with our RedeclarableTemplateDecl::Common.
  943. void mergePrevDecl(FunctionTemplateDecl *Prev);
  944. /// Create a function template node.
  945. static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
  946. SourceLocation L,
  947. DeclarationName Name,
  948. TemplateParameterList *Params,
  949. NamedDecl *Decl);
  950. /// Create an empty function template node.
  951. static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  952. // Implement isa/cast/dyncast support
  953. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  954. static bool classofKind(Kind K) { return K == FunctionTemplate; }
  955. };
  956. //===----------------------------------------------------------------------===//
  957. // Kinds of Template Parameters
  958. //===----------------------------------------------------------------------===//
  959. /// Defines the position of a template parameter within a template
  960. /// parameter list.
  961. ///
  962. /// Because template parameter can be listed
  963. /// sequentially for out-of-line template members, each template parameter is
  964. /// given a Depth - the nesting of template parameter scopes - and a Position -
  965. /// the occurrence within the parameter list.
  966. /// This class is inheritedly privately by different kinds of template
  967. /// parameters and is not part of the Decl hierarchy. Just a facility.
  968. class TemplateParmPosition {
  969. protected:
  970. // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
  971. // position? Maybe?
  972. unsigned Depth;
  973. unsigned Position;
  974. TemplateParmPosition(unsigned D, unsigned P) : Depth(D), Position(P) {}
  975. public:
  976. TemplateParmPosition() = delete;
  977. /// Get the nesting depth of the template parameter.
  978. unsigned getDepth() const { return Depth; }
  979. void setDepth(unsigned D) { Depth = D; }
  980. /// Get the position of the template parameter within its parameter list.
  981. unsigned getPosition() const { return Position; }
  982. void setPosition(unsigned P) { Position = P; }
  983. /// Get the index of the template parameter within its parameter list.
  984. unsigned getIndex() const { return Position; }
  985. };
  986. /// Declaration of a template type parameter.
  987. ///
  988. /// For example, "T" in
  989. /// \code
  990. /// template<typename T> class vector;
  991. /// \endcode
  992. class TemplateTypeParmDecl final : public TypeDecl,
  993. private llvm::TrailingObjects<TemplateTypeParmDecl, TypeConstraint> {
  994. /// Sema creates these on the stack during auto type deduction.
  995. friend class Sema;
  996. friend TrailingObjects;
  997. friend class ASTDeclReader;
  998. /// Whether this template type parameter was declaration with
  999. /// the 'typename' keyword.
  1000. ///
  1001. /// If false, it was declared with the 'class' keyword.
  1002. bool Typename : 1;
  1003. /// Whether this template type parameter has a type-constraint construct.
  1004. bool HasTypeConstraint : 1;
  1005. /// Whether the type constraint has been initialized. This can be false if the
  1006. /// constraint was not initialized yet or if there was an error forming the
  1007. /// type constraint.
  1008. bool TypeConstraintInitialized : 1;
  1009. /// Whether this non-type template parameter is an "expanded"
  1010. /// parameter pack, meaning that its type is a pack expansion and we
  1011. /// already know the set of types that expansion expands to.
  1012. bool ExpandedParameterPack : 1;
  1013. /// The number of type parameters in an expanded parameter pack.
  1014. unsigned NumExpanded = 0;
  1015. /// The default template argument, if any.
  1016. using DefArgStorage =
  1017. DefaultArgStorage<TemplateTypeParmDecl, TypeSourceInfo *>;
  1018. DefArgStorage DefaultArgument;
  1019. TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
  1020. SourceLocation IdLoc, IdentifierInfo *Id, bool Typename,
  1021. bool HasTypeConstraint, Optional<unsigned> NumExpanded)
  1022. : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
  1023. HasTypeConstraint(HasTypeConstraint), TypeConstraintInitialized(false),
  1024. ExpandedParameterPack(NumExpanded),
  1025. NumExpanded(NumExpanded.getValueOr(0)) {}
  1026. public:
  1027. static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC,
  1028. SourceLocation KeyLoc,
  1029. SourceLocation NameLoc,
  1030. unsigned D, unsigned P,
  1031. IdentifierInfo *Id, bool Typename,
  1032. bool ParameterPack,
  1033. bool HasTypeConstraint = false,
  1034. Optional<unsigned> NumExpanded = None);
  1035. static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
  1036. unsigned ID);
  1037. static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
  1038. unsigned ID,
  1039. bool HasTypeConstraint);
  1040. /// Whether this template type parameter was declared with
  1041. /// the 'typename' keyword.
  1042. ///
  1043. /// If not, it was either declared with the 'class' keyword or with a
  1044. /// type-constraint (see hasTypeConstraint()).
  1045. bool wasDeclaredWithTypename() const {
  1046. return Typename && !HasTypeConstraint;
  1047. }
  1048. const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
  1049. /// Determine whether this template parameter has a default
  1050. /// argument.
  1051. bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
  1052. /// Retrieve the default argument, if any.
  1053. QualType getDefaultArgument() const {
  1054. return DefaultArgument.get()->getType();
  1055. }
  1056. /// Retrieves the default argument's source information, if any.
  1057. TypeSourceInfo *getDefaultArgumentInfo() const {
  1058. return DefaultArgument.get();
  1059. }
  1060. /// Retrieves the location of the default argument declaration.
  1061. SourceLocation getDefaultArgumentLoc() const;
  1062. /// Determines whether the default argument was inherited
  1063. /// from a previous declaration of this template.
  1064. bool defaultArgumentWasInherited() const {
  1065. return DefaultArgument.isInherited();
  1066. }
  1067. /// Set the default argument for this template parameter.
  1068. void setDefaultArgument(TypeSourceInfo *DefArg) {
  1069. DefaultArgument.set(DefArg);
  1070. }
  1071. /// Set that this default argument was inherited from another
  1072. /// parameter.
  1073. void setInheritedDefaultArgument(const ASTContext &C,
  1074. TemplateTypeParmDecl *Prev) {
  1075. DefaultArgument.setInherited(C, Prev);
  1076. }
  1077. /// Removes the default argument of this template parameter.
  1078. void removeDefaultArgument() {
  1079. DefaultArgument.clear();
  1080. }
  1081. /// Set whether this template type parameter was declared with
  1082. /// the 'typename' or 'class' keyword.
  1083. void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; }
  1084. /// Retrieve the depth of the template parameter.
  1085. unsigned getDepth() const;
  1086. /// Retrieve the index of the template parameter.
  1087. unsigned getIndex() const;
  1088. /// Returns whether this is a parameter pack.
  1089. bool isParameterPack() const;
  1090. /// Whether this parameter pack is a pack expansion.
  1091. ///
  1092. /// A template type template parameter pack can be a pack expansion if its
  1093. /// type-constraint contains an unexpanded parameter pack.
  1094. bool isPackExpansion() const {
  1095. if (!isParameterPack())
  1096. return false;
  1097. if (const TypeConstraint *TC = getTypeConstraint())
  1098. if (TC->hasExplicitTemplateArgs())
  1099. for (const auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments())
  1100. if (ArgLoc.getArgument().containsUnexpandedParameterPack())
  1101. return true;
  1102. return false;
  1103. }
  1104. /// Whether this parameter is a template type parameter pack that has a known
  1105. /// list of different type-constraints at different positions.
  1106. ///
  1107. /// A parameter pack is an expanded parameter pack when the original
  1108. /// parameter pack's type-constraint was itself a pack expansion, and that
  1109. /// expansion has already been expanded. For example, given:
  1110. ///
  1111. /// \code
  1112. /// template<typename ...Types>
  1113. /// struct X {
  1114. /// template<convertible_to<Types> ...Convertibles>
  1115. /// struct Y { /* ... */ };
  1116. /// };
  1117. /// \endcode
  1118. ///
  1119. /// The parameter pack \c Convertibles has (convertible_to<Types> && ...) as
  1120. /// its type-constraint. When \c Types is supplied with template arguments by
  1121. /// instantiating \c X, the instantiation of \c Convertibles becomes an
  1122. /// expanded parameter pack. For example, instantiating
  1123. /// \c X<int, unsigned int> results in \c Convertibles being an expanded
  1124. /// parameter pack of size 2 (use getNumExpansionTypes() to get this number).
  1125. bool isExpandedParameterPack() const { return ExpandedParameterPack; }
  1126. /// Retrieves the number of parameters in an expanded parameter pack.
  1127. unsigned getNumExpansionParameters() const {
  1128. assert(ExpandedParameterPack && "Not an expansion parameter pack");
  1129. return NumExpanded;
  1130. }
  1131. /// Returns the type constraint associated with this template parameter (if
  1132. /// any).
  1133. const TypeConstraint *getTypeConstraint() const {
  1134. return TypeConstraintInitialized ? getTrailingObjects<TypeConstraint>() :
  1135. nullptr;
  1136. }
  1137. void setTypeConstraint(NestedNameSpecifierLoc NNS,
  1138. DeclarationNameInfo NameInfo, NamedDecl *FoundDecl,
  1139. ConceptDecl *CD,
  1140. const ASTTemplateArgumentListInfo *ArgsAsWritten,
  1141. Expr *ImmediatelyDeclaredConstraint);
  1142. /// Determine whether this template parameter has a type-constraint.
  1143. bool hasTypeConstraint() const {
  1144. return HasTypeConstraint;
  1145. }
  1146. /// \brief Get the associated-constraints of this template parameter.
  1147. /// This will either be the immediately-introduced constraint or empty.
  1148. ///
  1149. /// Use this instead of getConstraintExpression for concepts APIs that
  1150. /// accept an ArrayRef of constraint expressions.
  1151. void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  1152. if (HasTypeConstraint)
  1153. AC.push_back(getTypeConstraint()->getImmediatelyDeclaredConstraint());
  1154. }
  1155. SourceRange getSourceRange() const override LLVM_READONLY;
  1156. // Implement isa/cast/dyncast/etc.
  1157. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1158. static bool classofKind(Kind K) { return K == TemplateTypeParm; }
  1159. };
  1160. /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
  1161. /// e.g., "Size" in
  1162. /// @code
  1163. /// template<int Size> class array { };
  1164. /// @endcode
  1165. class NonTypeTemplateParmDecl final
  1166. : public DeclaratorDecl,
  1167. protected TemplateParmPosition,
  1168. private llvm::TrailingObjects<NonTypeTemplateParmDecl,
  1169. std::pair<QualType, TypeSourceInfo *>,
  1170. Expr *> {
  1171. friend class ASTDeclReader;
  1172. friend TrailingObjects;
  1173. /// The default template argument, if any, and whether or not
  1174. /// it was inherited.
  1175. using DefArgStorage = DefaultArgStorage<NonTypeTemplateParmDecl, Expr *>;
  1176. DefArgStorage DefaultArgument;
  1177. // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
  1178. // down here to save memory.
  1179. /// Whether this non-type template parameter is a parameter pack.
  1180. bool ParameterPack;
  1181. /// Whether this non-type template parameter is an "expanded"
  1182. /// parameter pack, meaning that its type is a pack expansion and we
  1183. /// already know the set of types that expansion expands to.
  1184. bool ExpandedParameterPack = false;
  1185. /// The number of types in an expanded parameter pack.
  1186. unsigned NumExpandedTypes = 0;
  1187. size_t numTrailingObjects(
  1188. OverloadToken<std::pair<QualType, TypeSourceInfo *>>) const {
  1189. return NumExpandedTypes;
  1190. }
  1191. NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
  1192. SourceLocation IdLoc, unsigned D, unsigned P,
  1193. IdentifierInfo *Id, QualType T,
  1194. bool ParameterPack, TypeSourceInfo *TInfo)
  1195. : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
  1196. TemplateParmPosition(D, P), ParameterPack(ParameterPack) {}
  1197. NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
  1198. SourceLocation IdLoc, unsigned D, unsigned P,
  1199. IdentifierInfo *Id, QualType T,
  1200. TypeSourceInfo *TInfo,
  1201. ArrayRef<QualType> ExpandedTypes,
  1202. ArrayRef<TypeSourceInfo *> ExpandedTInfos);
  1203. public:
  1204. static NonTypeTemplateParmDecl *
  1205. Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  1206. SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
  1207. QualType T, bool ParameterPack, TypeSourceInfo *TInfo);
  1208. static NonTypeTemplateParmDecl *
  1209. Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  1210. SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
  1211. QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
  1212. ArrayRef<TypeSourceInfo *> ExpandedTInfos);
  1213. static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
  1214. unsigned ID,
  1215. bool HasTypeConstraint);
  1216. static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
  1217. unsigned ID,
  1218. unsigned NumExpandedTypes,
  1219. bool HasTypeConstraint);
  1220. using TemplateParmPosition::getDepth;
  1221. using TemplateParmPosition::setDepth;
  1222. using TemplateParmPosition::getPosition;
  1223. using TemplateParmPosition::setPosition;
  1224. using TemplateParmPosition::getIndex;
  1225. SourceRange getSourceRange() const override LLVM_READONLY;
  1226. const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
  1227. /// Determine whether this template parameter has a default
  1228. /// argument.
  1229. bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
  1230. /// Retrieve the default argument, if any.
  1231. Expr *getDefaultArgument() const { return DefaultArgument.get(); }
  1232. /// Retrieve the location of the default argument, if any.
  1233. SourceLocation getDefaultArgumentLoc() const;
  1234. /// Determines whether the default argument was inherited
  1235. /// from a previous declaration of this template.
  1236. bool defaultArgumentWasInherited() const {
  1237. return DefaultArgument.isInherited();
  1238. }
  1239. /// Set the default argument for this template parameter, and
  1240. /// whether that default argument was inherited from another
  1241. /// declaration.
  1242. void setDefaultArgument(Expr *DefArg) { DefaultArgument.set(DefArg); }
  1243. void setInheritedDefaultArgument(const ASTContext &C,
  1244. NonTypeTemplateParmDecl *Parm) {
  1245. DefaultArgument.setInherited(C, Parm);
  1246. }
  1247. /// Removes the default argument of this template parameter.
  1248. void removeDefaultArgument() { DefaultArgument.clear(); }
  1249. /// Whether this parameter is a non-type template parameter pack.
  1250. ///
  1251. /// If the parameter is a parameter pack, the type may be a
  1252. /// \c PackExpansionType. In the following example, the \c Dims parameter
  1253. /// is a parameter pack (whose type is 'unsigned').
  1254. ///
  1255. /// \code
  1256. /// template<typename T, unsigned ...Dims> struct multi_array;
  1257. /// \endcode
  1258. bool isParameterPack() const { return ParameterPack; }
  1259. /// Whether this parameter pack is a pack expansion.
  1260. ///
  1261. /// A non-type template parameter pack is a pack expansion if its type
  1262. /// contains an unexpanded parameter pack. In this case, we will have
  1263. /// built a PackExpansionType wrapping the type.
  1264. bool isPackExpansion() const {
  1265. return ParameterPack && getType()->getAs<PackExpansionType>();
  1266. }
  1267. /// Whether this parameter is a non-type template parameter pack
  1268. /// that has a known list of different types at different positions.
  1269. ///
  1270. /// A parameter pack is an expanded parameter pack when the original
  1271. /// parameter pack's type was itself a pack expansion, and that expansion
  1272. /// has already been expanded. For example, given:
  1273. ///
  1274. /// \code
  1275. /// template<typename ...Types>
  1276. /// struct X {
  1277. /// template<Types ...Values>
  1278. /// struct Y { /* ... */ };
  1279. /// };
  1280. /// \endcode
  1281. ///
  1282. /// The parameter pack \c Values has a \c PackExpansionType as its type,
  1283. /// which expands \c Types. When \c Types is supplied with template arguments
  1284. /// by instantiating \c X, the instantiation of \c Values becomes an
  1285. /// expanded parameter pack. For example, instantiating
  1286. /// \c X<int, unsigned int> results in \c Values being an expanded parameter
  1287. /// pack with expansion types \c int and \c unsigned int.
  1288. ///
  1289. /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions
  1290. /// return the expansion types.
  1291. bool isExpandedParameterPack() const { return ExpandedParameterPack; }
  1292. /// Retrieves the number of expansion types in an expanded parameter
  1293. /// pack.
  1294. unsigned getNumExpansionTypes() const {
  1295. assert(ExpandedParameterPack && "Not an expansion parameter pack");
  1296. return NumExpandedTypes;
  1297. }
  1298. /// Retrieve a particular expansion type within an expanded parameter
  1299. /// pack.
  1300. QualType getExpansionType(unsigned I) const {
  1301. assert(I < NumExpandedTypes && "Out-of-range expansion type index");
  1302. auto TypesAndInfos =
  1303. getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
  1304. return TypesAndInfos[I].first;
  1305. }
  1306. /// Retrieve a particular expansion type source info within an
  1307. /// expanded parameter pack.
  1308. TypeSourceInfo *getExpansionTypeSourceInfo(unsigned I) const {
  1309. assert(I < NumExpandedTypes && "Out-of-range expansion type index");
  1310. auto TypesAndInfos =
  1311. getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
  1312. return TypesAndInfos[I].second;
  1313. }
  1314. /// Return the constraint introduced by the placeholder type of this non-type
  1315. /// template parameter (if any).
  1316. Expr *getPlaceholderTypeConstraint() const {
  1317. return hasPlaceholderTypeConstraint() ? *getTrailingObjects<Expr *>() :
  1318. nullptr;
  1319. }
  1320. void setPlaceholderTypeConstraint(Expr *E) {
  1321. *getTrailingObjects<Expr *>() = E;
  1322. }
  1323. /// Determine whether this non-type template parameter's type has a
  1324. /// placeholder with a type-constraint.
  1325. bool hasPlaceholderTypeConstraint() const {
  1326. auto *AT = getType()->getContainedAutoType();
  1327. return AT && AT->isConstrained();
  1328. }
  1329. /// \brief Get the associated-constraints of this template parameter.
  1330. /// This will either be a vector of size 1 containing the immediately-declared
  1331. /// constraint introduced by the placeholder type, or an empty vector.
  1332. ///
  1333. /// Use this instead of getPlaceholderImmediatelyDeclaredConstraint for
  1334. /// concepts APIs that accept an ArrayRef of constraint expressions.
  1335. void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  1336. if (Expr *E = getPlaceholderTypeConstraint())
  1337. AC.push_back(E);
  1338. }
  1339. // Implement isa/cast/dyncast/etc.
  1340. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1341. static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
  1342. };
  1343. /// TemplateTemplateParmDecl - Declares a template template parameter,
  1344. /// e.g., "T" in
  1345. /// @code
  1346. /// template <template <typename> class T> class container { };
  1347. /// @endcode
  1348. /// A template template parameter is a TemplateDecl because it defines the
  1349. /// name of a template and the template parameters allowable for substitution.
  1350. class TemplateTemplateParmDecl final
  1351. : public TemplateDecl,
  1352. protected TemplateParmPosition,
  1353. private llvm::TrailingObjects<TemplateTemplateParmDecl,
  1354. TemplateParameterList *> {
  1355. /// The default template argument, if any.
  1356. using DefArgStorage =
  1357. DefaultArgStorage<TemplateTemplateParmDecl, TemplateArgumentLoc *>;
  1358. DefArgStorage DefaultArgument;
  1359. /// Whether this parameter is a parameter pack.
  1360. bool ParameterPack;
  1361. /// Whether this template template parameter is an "expanded"
  1362. /// parameter pack, meaning that it is a pack expansion and we
  1363. /// already know the set of template parameters that expansion expands to.
  1364. bool ExpandedParameterPack = false;
  1365. /// The number of parameters in an expanded parameter pack.
  1366. unsigned NumExpandedParams = 0;
  1367. TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
  1368. unsigned D, unsigned P, bool ParameterPack,
  1369. IdentifierInfo *Id, TemplateParameterList *Params)
  1370. : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
  1371. TemplateParmPosition(D, P), ParameterPack(ParameterPack) {}
  1372. TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
  1373. unsigned D, unsigned P,
  1374. IdentifierInfo *Id, TemplateParameterList *Params,
  1375. ArrayRef<TemplateParameterList *> Expansions);
  1376. void anchor() override;
  1377. public:
  1378. friend class ASTDeclReader;
  1379. friend class ASTDeclWriter;
  1380. friend TrailingObjects;
  1381. static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
  1382. SourceLocation L, unsigned D,
  1383. unsigned P, bool ParameterPack,
  1384. IdentifierInfo *Id,
  1385. TemplateParameterList *Params);
  1386. static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
  1387. SourceLocation L, unsigned D,
  1388. unsigned P,
  1389. IdentifierInfo *Id,
  1390. TemplateParameterList *Params,
  1391. ArrayRef<TemplateParameterList *> Expansions);
  1392. static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
  1393. unsigned ID);
  1394. static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
  1395. unsigned ID,
  1396. unsigned NumExpansions);
  1397. using TemplateParmPosition::getDepth;
  1398. using TemplateParmPosition::setDepth;
  1399. using TemplateParmPosition::getPosition;
  1400. using TemplateParmPosition::setPosition;
  1401. using TemplateParmPosition::getIndex;
  1402. /// Whether this template template parameter is a template
  1403. /// parameter pack.
  1404. ///
  1405. /// \code
  1406. /// template<template <class T> ...MetaFunctions> struct Apply;
  1407. /// \endcode
  1408. bool isParameterPack() const { return ParameterPack; }
  1409. /// Whether this parameter pack is a pack expansion.
  1410. ///
  1411. /// A template template parameter pack is a pack expansion if its template
  1412. /// parameter list contains an unexpanded parameter pack.
  1413. bool isPackExpansion() const {
  1414. return ParameterPack &&
  1415. getTemplateParameters()->containsUnexpandedParameterPack();
  1416. }
  1417. /// Whether this parameter is a template template parameter pack that
  1418. /// has a known list of different template parameter lists at different
  1419. /// positions.
  1420. ///
  1421. /// A parameter pack is an expanded parameter pack when the original parameter
  1422. /// pack's template parameter list was itself a pack expansion, and that
  1423. /// expansion has already been expanded. For exampe, given:
  1424. ///
  1425. /// \code
  1426. /// template<typename...Types> struct Outer {
  1427. /// template<template<Types> class...Templates> struct Inner;
  1428. /// };
  1429. /// \endcode
  1430. ///
  1431. /// The parameter pack \c Templates is a pack expansion, which expands the
  1432. /// pack \c Types. When \c Types is supplied with template arguments by
  1433. /// instantiating \c Outer, the instantiation of \c Templates is an expanded
  1434. /// parameter pack.
  1435. bool isExpandedParameterPack() const { return ExpandedParameterPack; }
  1436. /// Retrieves the number of expansion template parameters in
  1437. /// an expanded parameter pack.
  1438. unsigned getNumExpansionTemplateParameters() const {
  1439. assert(ExpandedParameterPack && "Not an expansion parameter pack");
  1440. return NumExpandedParams;
  1441. }
  1442. /// Retrieve a particular expansion type within an expanded parameter
  1443. /// pack.
  1444. TemplateParameterList *getExpansionTemplateParameters(unsigned I) const {
  1445. assert(I < NumExpandedParams && "Out-of-range expansion type index");
  1446. return getTrailingObjects<TemplateParameterList *>()[I];
  1447. }
  1448. const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
  1449. /// Determine whether this template parameter has a default
  1450. /// argument.
  1451. bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
  1452. /// Retrieve the default argument, if any.
  1453. const TemplateArgumentLoc &getDefaultArgument() const {
  1454. static const TemplateArgumentLoc NoneLoc;
  1455. return DefaultArgument.isSet() ? *DefaultArgument.get() : NoneLoc;
  1456. }
  1457. /// Retrieve the location of the default argument, if any.
  1458. SourceLocation getDefaultArgumentLoc() const;
  1459. /// Determines whether the default argument was inherited
  1460. /// from a previous declaration of this template.
  1461. bool defaultArgumentWasInherited() const {
  1462. return DefaultArgument.isInherited();
  1463. }
  1464. /// Set the default argument for this template parameter, and
  1465. /// whether that default argument was inherited from another
  1466. /// declaration.
  1467. void setDefaultArgument(const ASTContext &C,
  1468. const TemplateArgumentLoc &DefArg);
  1469. void setInheritedDefaultArgument(const ASTContext &C,
  1470. TemplateTemplateParmDecl *Prev) {
  1471. DefaultArgument.setInherited(C, Prev);
  1472. }
  1473. /// Removes the default argument of this template parameter.
  1474. void removeDefaultArgument() { DefaultArgument.clear(); }
  1475. SourceRange getSourceRange() const override LLVM_READONLY {
  1476. SourceLocation End = getLocation();
  1477. if (hasDefaultArgument() && !defaultArgumentWasInherited())
  1478. End = getDefaultArgument().getSourceRange().getEnd();
  1479. return SourceRange(getTemplateParameters()->getTemplateLoc(), End);
  1480. }
  1481. // Implement isa/cast/dyncast/etc.
  1482. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1483. static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
  1484. };
  1485. /// Represents the builtin template declaration which is used to
  1486. /// implement __make_integer_seq and other builtin templates. It serves
  1487. /// no real purpose beyond existing as a place to hold template parameters.
  1488. class BuiltinTemplateDecl : public TemplateDecl {
  1489. BuiltinTemplateKind BTK;
  1490. BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
  1491. DeclarationName Name, BuiltinTemplateKind BTK);
  1492. void anchor() override;
  1493. public:
  1494. // Implement isa/cast/dyncast support
  1495. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1496. static bool classofKind(Kind K) { return K == BuiltinTemplate; }
  1497. static BuiltinTemplateDecl *Create(const ASTContext &C, DeclContext *DC,
  1498. DeclarationName Name,
  1499. BuiltinTemplateKind BTK) {
  1500. return new (C, DC) BuiltinTemplateDecl(C, DC, Name, BTK);
  1501. }
  1502. SourceRange getSourceRange() const override LLVM_READONLY {
  1503. return {};
  1504. }
  1505. BuiltinTemplateKind getBuiltinTemplateKind() const { return BTK; }
  1506. };
  1507. /// Represents a class template specialization, which refers to
  1508. /// a class template with a given set of template arguments.
  1509. ///
  1510. /// Class template specializations represent both explicit
  1511. /// specialization of class templates, as in the example below, and
  1512. /// implicit instantiations of class templates.
  1513. ///
  1514. /// \code
  1515. /// template<typename T> class array;
  1516. ///
  1517. /// template<>
  1518. /// class array<bool> { }; // class template specialization array<bool>
  1519. /// \endcode
  1520. class ClassTemplateSpecializationDecl
  1521. : public CXXRecordDecl, public llvm::FoldingSetNode {
  1522. /// Structure that stores information about a class template
  1523. /// specialization that was instantiated from a class template partial
  1524. /// specialization.
  1525. struct SpecializedPartialSpecialization {
  1526. /// The class template partial specialization from which this
  1527. /// class template specialization was instantiated.
  1528. ClassTemplatePartialSpecializationDecl *PartialSpecialization;
  1529. /// The template argument list deduced for the class template
  1530. /// partial specialization itself.
  1531. const TemplateArgumentList *TemplateArgs;
  1532. };
  1533. /// The template that this specialization specializes
  1534. llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
  1535. SpecializedTemplate;
  1536. /// Further info for explicit template specialization/instantiation.
  1537. struct ExplicitSpecializationInfo {
  1538. /// The type-as-written.
  1539. TypeSourceInfo *TypeAsWritten = nullptr;
  1540. /// The location of the extern keyword.
  1541. SourceLocation ExternLoc;
  1542. /// The location of the template keyword.
  1543. SourceLocation TemplateKeywordLoc;
  1544. ExplicitSpecializationInfo() = default;
  1545. };
  1546. /// Further info for explicit template specialization/instantiation.
  1547. /// Does not apply to implicit specializations.
  1548. ExplicitSpecializationInfo *ExplicitInfo = nullptr;
  1549. /// The template arguments used to describe this specialization.
  1550. const TemplateArgumentList *TemplateArgs;
  1551. /// The point where this template was instantiated (if any)
  1552. SourceLocation PointOfInstantiation;
  1553. /// The kind of specialization this declaration refers to.
  1554. /// Really a value of type TemplateSpecializationKind.
  1555. unsigned SpecializationKind : 3;
  1556. protected:
  1557. ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
  1558. DeclContext *DC, SourceLocation StartLoc,
  1559. SourceLocation IdLoc,
  1560. ClassTemplateDecl *SpecializedTemplate,
  1561. ArrayRef<TemplateArgument> Args,
  1562. ClassTemplateSpecializationDecl *PrevDecl);
  1563. explicit ClassTemplateSpecializationDecl(ASTContext &C, Kind DK);
  1564. public:
  1565. friend class ASTDeclReader;
  1566. friend class ASTDeclWriter;
  1567. static ClassTemplateSpecializationDecl *
  1568. Create(ASTContext &Context, TagKind TK, DeclContext *DC,
  1569. SourceLocation StartLoc, SourceLocation IdLoc,
  1570. ClassTemplateDecl *SpecializedTemplate,
  1571. ArrayRef<TemplateArgument> Args,
  1572. ClassTemplateSpecializationDecl *PrevDecl);
  1573. static ClassTemplateSpecializationDecl *
  1574. CreateDeserialized(ASTContext &C, unsigned ID);
  1575. void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
  1576. bool Qualified) const override;
  1577. // FIXME: This is broken. CXXRecordDecl::getMostRecentDecl() returns a
  1578. // different "most recent" declaration from this function for the same
  1579. // declaration, because we don't override getMostRecentDeclImpl(). But
  1580. // it's not clear that we should override that, because the most recent
  1581. // declaration as a CXXRecordDecl sometimes is the injected-class-name.
  1582. ClassTemplateSpecializationDecl *getMostRecentDecl() {
  1583. return cast<ClassTemplateSpecializationDecl>(
  1584. getMostRecentNonInjectedDecl());
  1585. }
  1586. /// Retrieve the template that this specialization specializes.
  1587. ClassTemplateDecl *getSpecializedTemplate() const;
  1588. /// Retrieve the template arguments of the class template
  1589. /// specialization.
  1590. const TemplateArgumentList &getTemplateArgs() const {
  1591. return *TemplateArgs;
  1592. }
  1593. void setTemplateArgs(TemplateArgumentList *Args) {
  1594. TemplateArgs = Args;
  1595. }
  1596. /// Determine the kind of specialization that this
  1597. /// declaration represents.
  1598. TemplateSpecializationKind getSpecializationKind() const {
  1599. return static_cast<TemplateSpecializationKind>(SpecializationKind);
  1600. }
  1601. bool isExplicitSpecialization() const {
  1602. return getSpecializationKind() == TSK_ExplicitSpecialization;
  1603. }
  1604. /// Is this an explicit specialization at class scope (within the class that
  1605. /// owns the primary template)? For example:
  1606. ///
  1607. /// \code
  1608. /// template<typename T> struct Outer {
  1609. /// template<typename U> struct Inner;
  1610. /// template<> struct Inner; // class-scope explicit specialization
  1611. /// };
  1612. /// \endcode
  1613. bool isClassScopeExplicitSpecialization() const {
  1614. return isExplicitSpecialization() &&
  1615. isa<CXXRecordDecl>(getLexicalDeclContext());
  1616. }
  1617. /// True if this declaration is an explicit specialization,
  1618. /// explicit instantiation declaration, or explicit instantiation
  1619. /// definition.
  1620. bool isExplicitInstantiationOrSpecialization() const {
  1621. return isTemplateExplicitInstantiationOrSpecialization(
  1622. getTemplateSpecializationKind());
  1623. }
  1624. void setSpecializedTemplate(ClassTemplateDecl *Specialized) {
  1625. SpecializedTemplate = Specialized;
  1626. }
  1627. void setSpecializationKind(TemplateSpecializationKind TSK) {
  1628. SpecializationKind = TSK;
  1629. }
  1630. /// Get the point of instantiation (if any), or null if none.
  1631. SourceLocation getPointOfInstantiation() const {
  1632. return PointOfInstantiation;
  1633. }
  1634. void setPointOfInstantiation(SourceLocation Loc) {
  1635. assert(Loc.isValid() && "point of instantiation must be valid!");
  1636. PointOfInstantiation = Loc;
  1637. }
  1638. /// If this class template specialization is an instantiation of
  1639. /// a template (rather than an explicit specialization), return the
  1640. /// class template or class template partial specialization from which it
  1641. /// was instantiated.
  1642. llvm::PointerUnion<ClassTemplateDecl *,
  1643. ClassTemplatePartialSpecializationDecl *>
  1644. getInstantiatedFrom() const {
  1645. if (!isTemplateInstantiation(getSpecializationKind()))
  1646. return llvm::PointerUnion<ClassTemplateDecl *,
  1647. ClassTemplatePartialSpecializationDecl *>();
  1648. return getSpecializedTemplateOrPartial();
  1649. }
  1650. /// Retrieve the class template or class template partial
  1651. /// specialization which was specialized by this.
  1652. llvm::PointerUnion<ClassTemplateDecl *,
  1653. ClassTemplatePartialSpecializationDecl *>
  1654. getSpecializedTemplateOrPartial() const {
  1655. if (const auto *PartialSpec =
  1656. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
  1657. return PartialSpec->PartialSpecialization;
  1658. return SpecializedTemplate.get<ClassTemplateDecl*>();
  1659. }
  1660. /// Retrieve the set of template arguments that should be used
  1661. /// to instantiate members of the class template or class template partial
  1662. /// specialization from which this class template specialization was
  1663. /// instantiated.
  1664. ///
  1665. /// \returns For a class template specialization instantiated from the primary
  1666. /// template, this function will return the same template arguments as
  1667. /// getTemplateArgs(). For a class template specialization instantiated from
  1668. /// a class template partial specialization, this function will return the
  1669. /// deduced template arguments for the class template partial specialization
  1670. /// itself.
  1671. const TemplateArgumentList &getTemplateInstantiationArgs() const {
  1672. if (const auto *PartialSpec =
  1673. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
  1674. return *PartialSpec->TemplateArgs;
  1675. return getTemplateArgs();
  1676. }
  1677. /// Note that this class template specialization is actually an
  1678. /// instantiation of the given class template partial specialization whose
  1679. /// template arguments have been deduced.
  1680. void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
  1681. const TemplateArgumentList *TemplateArgs) {
  1682. assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
  1683. "Already set to a class template partial specialization!");
  1684. auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
  1685. PS->PartialSpecialization = PartialSpec;
  1686. PS->TemplateArgs = TemplateArgs;
  1687. SpecializedTemplate = PS;
  1688. }
  1689. /// Note that this class template specialization is an instantiation
  1690. /// of the given class template.
  1691. void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
  1692. assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
  1693. "Previously set to a class template partial specialization!");
  1694. SpecializedTemplate = TemplDecl;
  1695. }
  1696. /// Sets the type of this specialization as it was written by
  1697. /// the user. This will be a class template specialization type.
  1698. void setTypeAsWritten(TypeSourceInfo *T) {
  1699. if (!ExplicitInfo)
  1700. ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
  1701. ExplicitInfo->TypeAsWritten = T;
  1702. }
  1703. /// Gets the type of this specialization as it was written by
  1704. /// the user, if it was so written.
  1705. TypeSourceInfo *getTypeAsWritten() const {
  1706. return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
  1707. }
  1708. /// Gets the location of the extern keyword, if present.
  1709. SourceLocation getExternLoc() const {
  1710. return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
  1711. }
  1712. /// Sets the location of the extern keyword.
  1713. void setExternLoc(SourceLocation Loc) {
  1714. if (!ExplicitInfo)
  1715. ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
  1716. ExplicitInfo->ExternLoc = Loc;
  1717. }
  1718. /// Sets the location of the template keyword.
  1719. void setTemplateKeywordLoc(SourceLocation Loc) {
  1720. if (!ExplicitInfo)
  1721. ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
  1722. ExplicitInfo->TemplateKeywordLoc = Loc;
  1723. }
  1724. /// Gets the location of the template keyword, if present.
  1725. SourceLocation getTemplateKeywordLoc() const {
  1726. return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
  1727. }
  1728. SourceRange getSourceRange() const override LLVM_READONLY;
  1729. void Profile(llvm::FoldingSetNodeID &ID) const {
  1730. Profile(ID, TemplateArgs->asArray(), getASTContext());
  1731. }
  1732. static void
  1733. Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
  1734. ASTContext &Context) {
  1735. ID.AddInteger(TemplateArgs.size());
  1736. for (const TemplateArgument &TemplateArg : TemplateArgs)
  1737. TemplateArg.Profile(ID, Context);
  1738. }
  1739. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1740. static bool classofKind(Kind K) {
  1741. return K >= firstClassTemplateSpecialization &&
  1742. K <= lastClassTemplateSpecialization;
  1743. }
  1744. };
  1745. class ClassTemplatePartialSpecializationDecl
  1746. : public ClassTemplateSpecializationDecl {
  1747. /// The list of template parameters
  1748. TemplateParameterList* TemplateParams = nullptr;
  1749. /// The source info for the template arguments as written.
  1750. /// FIXME: redundant with TypeAsWritten?
  1751. const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
  1752. /// The class template partial specialization from which this
  1753. /// class template partial specialization was instantiated.
  1754. ///
  1755. /// The boolean value will be true to indicate that this class template
  1756. /// partial specialization was specialized at this level.
  1757. llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
  1758. InstantiatedFromMember;
  1759. ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
  1760. DeclContext *DC,
  1761. SourceLocation StartLoc,
  1762. SourceLocation IdLoc,
  1763. TemplateParameterList *Params,
  1764. ClassTemplateDecl *SpecializedTemplate,
  1765. ArrayRef<TemplateArgument> Args,
  1766. const ASTTemplateArgumentListInfo *ArgsAsWritten,
  1767. ClassTemplatePartialSpecializationDecl *PrevDecl);
  1768. ClassTemplatePartialSpecializationDecl(ASTContext &C)
  1769. : ClassTemplateSpecializationDecl(C, ClassTemplatePartialSpecialization),
  1770. InstantiatedFromMember(nullptr, false) {}
  1771. void anchor() override;
  1772. public:
  1773. friend class ASTDeclReader;
  1774. friend class ASTDeclWriter;
  1775. static ClassTemplatePartialSpecializationDecl *
  1776. Create(ASTContext &Context, TagKind TK, DeclContext *DC,
  1777. SourceLocation StartLoc, SourceLocation IdLoc,
  1778. TemplateParameterList *Params,
  1779. ClassTemplateDecl *SpecializedTemplate,
  1780. ArrayRef<TemplateArgument> Args,
  1781. const TemplateArgumentListInfo &ArgInfos,
  1782. QualType CanonInjectedType,
  1783. ClassTemplatePartialSpecializationDecl *PrevDecl);
  1784. static ClassTemplatePartialSpecializationDecl *
  1785. CreateDeserialized(ASTContext &C, unsigned ID);
  1786. ClassTemplatePartialSpecializationDecl *getMostRecentDecl() {
  1787. return cast<ClassTemplatePartialSpecializationDecl>(
  1788. static_cast<ClassTemplateSpecializationDecl *>(
  1789. this)->getMostRecentDecl());
  1790. }
  1791. /// Get the list of template parameters
  1792. TemplateParameterList *getTemplateParameters() const {
  1793. return TemplateParams;
  1794. }
  1795. /// \brief All associated constraints of this partial specialization,
  1796. /// including the requires clause and any constraints derived from
  1797. /// constrained-parameters.
  1798. ///
  1799. /// The constraints in the resulting list are to be treated as if in a
  1800. /// conjunction ("and").
  1801. void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  1802. TemplateParams->getAssociatedConstraints(AC);
  1803. }
  1804. bool hasAssociatedConstraints() const {
  1805. return TemplateParams->hasAssociatedConstraints();
  1806. }
  1807. /// Get the template arguments as written.
  1808. const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
  1809. return ArgsAsWritten;
  1810. }
  1811. /// Retrieve the member class template partial specialization from
  1812. /// which this particular class template partial specialization was
  1813. /// instantiated.
  1814. ///
  1815. /// \code
  1816. /// template<typename T>
  1817. /// struct Outer {
  1818. /// template<typename U> struct Inner;
  1819. /// template<typename U> struct Inner<U*> { }; // #1
  1820. /// };
  1821. ///
  1822. /// Outer<float>::Inner<int*> ii;
  1823. /// \endcode
  1824. ///
  1825. /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
  1826. /// end up instantiating the partial specialization
  1827. /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
  1828. /// template partial specialization \c Outer<T>::Inner<U*>. Given
  1829. /// \c Outer<float>::Inner<U*>, this function would return
  1830. /// \c Outer<T>::Inner<U*>.
  1831. ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() const {
  1832. const auto *First =
  1833. cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
  1834. return First->InstantiatedFromMember.getPointer();
  1835. }
  1836. ClassTemplatePartialSpecializationDecl *
  1837. getInstantiatedFromMemberTemplate() const {
  1838. return getInstantiatedFromMember();
  1839. }
  1840. void setInstantiatedFromMember(
  1841. ClassTemplatePartialSpecializationDecl *PartialSpec) {
  1842. auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
  1843. First->InstantiatedFromMember.setPointer(PartialSpec);
  1844. }
  1845. /// Determines whether this class template partial specialization
  1846. /// template was a specialization of a member partial specialization.
  1847. ///
  1848. /// In the following example, the member template partial specialization
  1849. /// \c X<int>::Inner<T*> is a member specialization.
  1850. ///
  1851. /// \code
  1852. /// template<typename T>
  1853. /// struct X {
  1854. /// template<typename U> struct Inner;
  1855. /// template<typename U> struct Inner<U*>;
  1856. /// };
  1857. ///
  1858. /// template<> template<typename T>
  1859. /// struct X<int>::Inner<T*> { /* ... */ };
  1860. /// \endcode
  1861. bool isMemberSpecialization() {
  1862. const auto *First =
  1863. cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
  1864. return First->InstantiatedFromMember.getInt();
  1865. }
  1866. /// Note that this member template is a specialization.
  1867. void setMemberSpecialization() {
  1868. auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
  1869. assert(First->InstantiatedFromMember.getPointer() &&
  1870. "Only member templates can be member template specializations");
  1871. return First->InstantiatedFromMember.setInt(true);
  1872. }
  1873. /// Retrieves the injected specialization type for this partial
  1874. /// specialization. This is not the same as the type-decl-type for
  1875. /// this partial specialization, which is an InjectedClassNameType.
  1876. QualType getInjectedSpecializationType() const {
  1877. assert(getTypeForDecl() && "partial specialization has no type set!");
  1878. return cast<InjectedClassNameType>(getTypeForDecl())
  1879. ->getInjectedSpecializationType();
  1880. }
  1881. void Profile(llvm::FoldingSetNodeID &ID) const {
  1882. Profile(ID, getTemplateArgs().asArray(), getTemplateParameters(),
  1883. getASTContext());
  1884. }
  1885. static void
  1886. Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
  1887. TemplateParameterList *TPL, ASTContext &Context);
  1888. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  1889. static bool classofKind(Kind K) {
  1890. return K == ClassTemplatePartialSpecialization;
  1891. }
  1892. };
  1893. /// Declaration of a class template.
  1894. class ClassTemplateDecl : public RedeclarableTemplateDecl {
  1895. protected:
  1896. /// Data that is common to all of the declarations of a given
  1897. /// class template.
  1898. struct Common : CommonBase {
  1899. /// The class template specializations for this class
  1900. /// template, including explicit specializations and instantiations.
  1901. llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations;
  1902. /// The class template partial specializations for this class
  1903. /// template.
  1904. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
  1905. PartialSpecializations;
  1906. /// The injected-class-name type for this class template.
  1907. QualType InjectedClassNameType;
  1908. Common() = default;
  1909. };
  1910. /// Retrieve the set of specializations of this class template.
  1911. llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
  1912. getSpecializations() const;
  1913. /// Retrieve the set of partial specializations of this class
  1914. /// template.
  1915. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
  1916. getPartialSpecializations() const;
  1917. ClassTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
  1918. DeclarationName Name, TemplateParameterList *Params,
  1919. NamedDecl *Decl)
  1920. : RedeclarableTemplateDecl(ClassTemplate, C, DC, L, Name, Params, Decl) {}
  1921. CommonBase *newCommon(ASTContext &C) const override;
  1922. Common *getCommonPtr() const {
  1923. return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
  1924. }
  1925. public:
  1926. friend class ASTDeclReader;
  1927. friend class ASTDeclWriter;
  1928. /// Load any lazily-loaded specializations from the external source.
  1929. void LoadLazySpecializations() const;
  1930. /// Get the underlying class declarations of the template.
  1931. CXXRecordDecl *getTemplatedDecl() const {
  1932. return static_cast<CXXRecordDecl *>(TemplatedDecl);
  1933. }
  1934. /// Returns whether this template declaration defines the primary
  1935. /// class pattern.
  1936. bool isThisDeclarationADefinition() const {
  1937. return getTemplatedDecl()->isThisDeclarationADefinition();
  1938. }
  1939. /// \brief Create a class template node.
  1940. static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
  1941. SourceLocation L,
  1942. DeclarationName Name,
  1943. TemplateParameterList *Params,
  1944. NamedDecl *Decl);
  1945. /// Create an empty class template node.
  1946. static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  1947. /// Return the specialization with the provided arguments if it exists,
  1948. /// otherwise return the insertion point.
  1949. ClassTemplateSpecializationDecl *
  1950. findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
  1951. /// Insert the specified specialization knowing that it is not already
  1952. /// in. InsertPos must be obtained from findSpecialization.
  1953. void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
  1954. ClassTemplateDecl *getCanonicalDecl() override {
  1955. return cast<ClassTemplateDecl>(
  1956. RedeclarableTemplateDecl::getCanonicalDecl());
  1957. }
  1958. const ClassTemplateDecl *getCanonicalDecl() const {
  1959. return cast<ClassTemplateDecl>(
  1960. RedeclarableTemplateDecl::getCanonicalDecl());
  1961. }
  1962. /// Retrieve the previous declaration of this class template, or
  1963. /// nullptr if no such declaration exists.
  1964. ClassTemplateDecl *getPreviousDecl() {
  1965. return cast_or_null<ClassTemplateDecl>(
  1966. static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
  1967. }
  1968. const ClassTemplateDecl *getPreviousDecl() const {
  1969. return cast_or_null<ClassTemplateDecl>(
  1970. static_cast<const RedeclarableTemplateDecl *>(
  1971. this)->getPreviousDecl());
  1972. }
  1973. ClassTemplateDecl *getMostRecentDecl() {
  1974. return cast<ClassTemplateDecl>(
  1975. static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
  1976. }
  1977. const ClassTemplateDecl *getMostRecentDecl() const {
  1978. return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl();
  1979. }
  1980. ClassTemplateDecl *getInstantiatedFromMemberTemplate() const {
  1981. return cast_or_null<ClassTemplateDecl>(
  1982. RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
  1983. }
  1984. /// Return the partial specialization with the provided arguments if it
  1985. /// exists, otherwise return the insertion point.
  1986. ClassTemplatePartialSpecializationDecl *
  1987. findPartialSpecialization(ArrayRef<TemplateArgument> Args,
  1988. TemplateParameterList *TPL, void *&InsertPos);
  1989. /// Insert the specified partial specialization knowing that it is not
  1990. /// already in. InsertPos must be obtained from findPartialSpecialization.
  1991. void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
  1992. void *InsertPos);
  1993. /// Retrieve the partial specializations as an ordered list.
  1994. void getPartialSpecializations(
  1995. SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const;
  1996. /// Find a class template partial specialization with the given
  1997. /// type T.
  1998. ///
  1999. /// \param T a dependent type that names a specialization of this class
  2000. /// template.
  2001. ///
  2002. /// \returns the class template partial specialization that exactly matches
  2003. /// the type \p T, or nullptr if no such partial specialization exists.
  2004. ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
  2005. /// Find a class template partial specialization which was instantiated
  2006. /// from the given member partial specialization.
  2007. ///
  2008. /// \param D a member class template partial specialization.
  2009. ///
  2010. /// \returns the class template partial specialization which was instantiated
  2011. /// from the given member partial specialization, or nullptr if no such
  2012. /// partial specialization exists.
  2013. ClassTemplatePartialSpecializationDecl *
  2014. findPartialSpecInstantiatedFromMember(
  2015. ClassTemplatePartialSpecializationDecl *D);
  2016. /// Retrieve the template specialization type of the
  2017. /// injected-class-name for this class template.
  2018. ///
  2019. /// The injected-class-name for a class template \c X is \c
  2020. /// X<template-args>, where \c template-args is formed from the
  2021. /// template arguments that correspond to the template parameters of
  2022. /// \c X. For example:
  2023. ///
  2024. /// \code
  2025. /// template<typename T, int N>
  2026. /// struct array {
  2027. /// typedef array this_type; // "array" is equivalent to "array<T, N>"
  2028. /// };
  2029. /// \endcode
  2030. QualType getInjectedClassNameSpecialization();
  2031. using spec_iterator = SpecIterator<ClassTemplateSpecializationDecl>;
  2032. using spec_range = llvm::iterator_range<spec_iterator>;
  2033. spec_range specializations() const {
  2034. return spec_range(spec_begin(), spec_end());
  2035. }
  2036. spec_iterator spec_begin() const {
  2037. return makeSpecIterator(getSpecializations(), false);
  2038. }
  2039. spec_iterator spec_end() const {
  2040. return makeSpecIterator(getSpecializations(), true);
  2041. }
  2042. // Implement isa/cast/dyncast support
  2043. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2044. static bool classofKind(Kind K) { return K == ClassTemplate; }
  2045. };
  2046. /// Declaration of a friend template.
  2047. ///
  2048. /// For example:
  2049. /// \code
  2050. /// template \<typename T> class A {
  2051. /// friend class MyVector<T>; // not a friend template
  2052. /// template \<typename U> friend class B; // not a friend template
  2053. /// template \<typename U> friend class Foo<T>::Nested; // friend template
  2054. /// };
  2055. /// \endcode
  2056. ///
  2057. /// \note This class is not currently in use. All of the above
  2058. /// will yield a FriendDecl, not a FriendTemplateDecl.
  2059. class FriendTemplateDecl : public Decl {
  2060. virtual void anchor();
  2061. public:
  2062. using FriendUnion = llvm::PointerUnion<NamedDecl *,TypeSourceInfo *>;
  2063. private:
  2064. // The number of template parameters; always non-zero.
  2065. unsigned NumParams = 0;
  2066. // The parameter list.
  2067. TemplateParameterList **Params = nullptr;
  2068. // The declaration that's a friend of this class.
  2069. FriendUnion Friend;
  2070. // Location of the 'friend' specifier.
  2071. SourceLocation FriendLoc;
  2072. FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
  2073. TemplateParameterList **Params, unsigned NumParams,
  2074. FriendUnion Friend, SourceLocation FriendLoc)
  2075. : Decl(Decl::FriendTemplate, DC, Loc), NumParams(NumParams),
  2076. Params(Params), Friend(Friend), FriendLoc(FriendLoc) {}
  2077. FriendTemplateDecl(EmptyShell Empty) : Decl(Decl::FriendTemplate, Empty) {}
  2078. public:
  2079. friend class ASTDeclReader;
  2080. static FriendTemplateDecl *
  2081. Create(ASTContext &Context, DeclContext *DC, SourceLocation Loc,
  2082. MutableArrayRef<TemplateParameterList *> Params, FriendUnion Friend,
  2083. SourceLocation FriendLoc);
  2084. static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2085. /// If this friend declaration names a templated type (or
  2086. /// a dependent member type of a templated type), return that
  2087. /// type; otherwise return null.
  2088. TypeSourceInfo *getFriendType() const {
  2089. return Friend.dyn_cast<TypeSourceInfo*>();
  2090. }
  2091. /// If this friend declaration names a templated function (or
  2092. /// a member function of a templated type), return that type;
  2093. /// otherwise return null.
  2094. NamedDecl *getFriendDecl() const {
  2095. return Friend.dyn_cast<NamedDecl*>();
  2096. }
  2097. /// Retrieves the location of the 'friend' keyword.
  2098. SourceLocation getFriendLoc() const {
  2099. return FriendLoc;
  2100. }
  2101. TemplateParameterList *getTemplateParameterList(unsigned i) const {
  2102. assert(i <= NumParams);
  2103. return Params[i];
  2104. }
  2105. unsigned getNumTemplateParameters() const {
  2106. return NumParams;
  2107. }
  2108. // Implement isa/cast/dyncast/etc.
  2109. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2110. static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
  2111. };
  2112. /// Declaration of an alias template.
  2113. ///
  2114. /// For example:
  2115. /// \code
  2116. /// template \<typename T> using V = std::map<T*, int, MyCompare<T>>;
  2117. /// \endcode
  2118. class TypeAliasTemplateDecl : public RedeclarableTemplateDecl {
  2119. protected:
  2120. using Common = CommonBase;
  2121. TypeAliasTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
  2122. DeclarationName Name, TemplateParameterList *Params,
  2123. NamedDecl *Decl)
  2124. : RedeclarableTemplateDecl(TypeAliasTemplate, C, DC, L, Name, Params,
  2125. Decl) {}
  2126. CommonBase *newCommon(ASTContext &C) const override;
  2127. Common *getCommonPtr() {
  2128. return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
  2129. }
  2130. public:
  2131. friend class ASTDeclReader;
  2132. friend class ASTDeclWriter;
  2133. /// Get the underlying function declaration of the template.
  2134. TypeAliasDecl *getTemplatedDecl() const {
  2135. return static_cast<TypeAliasDecl *>(TemplatedDecl);
  2136. }
  2137. TypeAliasTemplateDecl *getCanonicalDecl() override {
  2138. return cast<TypeAliasTemplateDecl>(
  2139. RedeclarableTemplateDecl::getCanonicalDecl());
  2140. }
  2141. const TypeAliasTemplateDecl *getCanonicalDecl() const {
  2142. return cast<TypeAliasTemplateDecl>(
  2143. RedeclarableTemplateDecl::getCanonicalDecl());
  2144. }
  2145. /// Retrieve the previous declaration of this function template, or
  2146. /// nullptr if no such declaration exists.
  2147. TypeAliasTemplateDecl *getPreviousDecl() {
  2148. return cast_or_null<TypeAliasTemplateDecl>(
  2149. static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
  2150. }
  2151. const TypeAliasTemplateDecl *getPreviousDecl() const {
  2152. return cast_or_null<TypeAliasTemplateDecl>(
  2153. static_cast<const RedeclarableTemplateDecl *>(
  2154. this)->getPreviousDecl());
  2155. }
  2156. TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() const {
  2157. return cast_or_null<TypeAliasTemplateDecl>(
  2158. RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
  2159. }
  2160. /// Create a function template node.
  2161. static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC,
  2162. SourceLocation L,
  2163. DeclarationName Name,
  2164. TemplateParameterList *Params,
  2165. NamedDecl *Decl);
  2166. /// Create an empty alias template node.
  2167. static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2168. // Implement isa/cast/dyncast support
  2169. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2170. static bool classofKind(Kind K) { return K == TypeAliasTemplate; }
  2171. };
  2172. /// Declaration of a function specialization at template class scope.
  2173. ///
  2174. /// For example:
  2175. /// \code
  2176. /// template <class T>
  2177. /// class A {
  2178. /// template <class U> void foo(U a) { }
  2179. /// template<> void foo(int a) { }
  2180. /// }
  2181. /// \endcode
  2182. ///
  2183. /// "template<> foo(int a)" will be saved in Specialization as a normal
  2184. /// CXXMethodDecl. Then during an instantiation of class A, it will be
  2185. /// transformed into an actual function specialization.
  2186. ///
  2187. /// FIXME: This is redundant; we could store the same information directly on
  2188. /// the CXXMethodDecl as a DependentFunctionTemplateSpecializationInfo.
  2189. class ClassScopeFunctionSpecializationDecl : public Decl {
  2190. CXXMethodDecl *Specialization;
  2191. const ASTTemplateArgumentListInfo *TemplateArgs;
  2192. ClassScopeFunctionSpecializationDecl(
  2193. DeclContext *DC, SourceLocation Loc, CXXMethodDecl *FD,
  2194. const ASTTemplateArgumentListInfo *TemplArgs)
  2195. : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc),
  2196. Specialization(FD), TemplateArgs(TemplArgs) {}
  2197. ClassScopeFunctionSpecializationDecl(EmptyShell Empty)
  2198. : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {}
  2199. virtual void anchor();
  2200. public:
  2201. friend class ASTDeclReader;
  2202. friend class ASTDeclWriter;
  2203. CXXMethodDecl *getSpecialization() const { return Specialization; }
  2204. bool hasExplicitTemplateArgs() const { return TemplateArgs; }
  2205. const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
  2206. return TemplateArgs;
  2207. }
  2208. static ClassScopeFunctionSpecializationDecl *
  2209. Create(ASTContext &C, DeclContext *DC, SourceLocation Loc, CXXMethodDecl *FD,
  2210. bool HasExplicitTemplateArgs,
  2211. const TemplateArgumentListInfo &TemplateArgs) {
  2212. return new (C, DC) ClassScopeFunctionSpecializationDecl(
  2213. DC, Loc, FD,
  2214. HasExplicitTemplateArgs
  2215. ? ASTTemplateArgumentListInfo::Create(C, TemplateArgs)
  2216. : nullptr);
  2217. }
  2218. static ClassScopeFunctionSpecializationDecl *
  2219. CreateDeserialized(ASTContext &Context, unsigned ID);
  2220. // Implement isa/cast/dyncast/etc.
  2221. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2222. static bool classofKind(Kind K) {
  2223. return K == Decl::ClassScopeFunctionSpecialization;
  2224. }
  2225. };
  2226. /// Represents a variable template specialization, which refers to
  2227. /// a variable template with a given set of template arguments.
  2228. ///
  2229. /// Variable template specializations represent both explicit
  2230. /// specializations of variable templates, as in the example below, and
  2231. /// implicit instantiations of variable templates.
  2232. ///
  2233. /// \code
  2234. /// template<typename T> constexpr T pi = T(3.1415926535897932385);
  2235. ///
  2236. /// template<>
  2237. /// constexpr float pi<float>; // variable template specialization pi<float>
  2238. /// \endcode
  2239. class VarTemplateSpecializationDecl : public VarDecl,
  2240. public llvm::FoldingSetNode {
  2241. /// Structure that stores information about a variable template
  2242. /// specialization that was instantiated from a variable template partial
  2243. /// specialization.
  2244. struct SpecializedPartialSpecialization {
  2245. /// The variable template partial specialization from which this
  2246. /// variable template specialization was instantiated.
  2247. VarTemplatePartialSpecializationDecl *PartialSpecialization;
  2248. /// The template argument list deduced for the variable template
  2249. /// partial specialization itself.
  2250. const TemplateArgumentList *TemplateArgs;
  2251. };
  2252. /// The template that this specialization specializes.
  2253. llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *>
  2254. SpecializedTemplate;
  2255. /// Further info for explicit template specialization/instantiation.
  2256. struct ExplicitSpecializationInfo {
  2257. /// The type-as-written.
  2258. TypeSourceInfo *TypeAsWritten = nullptr;
  2259. /// The location of the extern keyword.
  2260. SourceLocation ExternLoc;
  2261. /// The location of the template keyword.
  2262. SourceLocation TemplateKeywordLoc;
  2263. ExplicitSpecializationInfo() = default;
  2264. };
  2265. /// Further info for explicit template specialization/instantiation.
  2266. /// Does not apply to implicit specializations.
  2267. ExplicitSpecializationInfo *ExplicitInfo = nullptr;
  2268. /// The template arguments used to describe this specialization.
  2269. const TemplateArgumentList *TemplateArgs;
  2270. TemplateArgumentListInfo TemplateArgsInfo;
  2271. /// The point where this template was instantiated (if any).
  2272. SourceLocation PointOfInstantiation;
  2273. /// The kind of specialization this declaration refers to.
  2274. /// Really a value of type TemplateSpecializationKind.
  2275. unsigned SpecializationKind : 3;
  2276. /// Whether this declaration is a complete definition of the
  2277. /// variable template specialization. We can't otherwise tell apart
  2278. /// an instantiated declaration from an instantiated definition with
  2279. /// no initializer.
  2280. unsigned IsCompleteDefinition : 1;
  2281. protected:
  2282. VarTemplateSpecializationDecl(Kind DK, ASTContext &Context, DeclContext *DC,
  2283. SourceLocation StartLoc, SourceLocation IdLoc,
  2284. VarTemplateDecl *SpecializedTemplate,
  2285. QualType T, TypeSourceInfo *TInfo,
  2286. StorageClass S,
  2287. ArrayRef<TemplateArgument> Args);
  2288. explicit VarTemplateSpecializationDecl(Kind DK, ASTContext &Context);
  2289. public:
  2290. friend class ASTDeclReader;
  2291. friend class ASTDeclWriter;
  2292. friend class VarDecl;
  2293. static VarTemplateSpecializationDecl *
  2294. Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  2295. SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
  2296. TypeSourceInfo *TInfo, StorageClass S,
  2297. ArrayRef<TemplateArgument> Args);
  2298. static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C,
  2299. unsigned ID);
  2300. void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
  2301. bool Qualified) const override;
  2302. VarTemplateSpecializationDecl *getMostRecentDecl() {
  2303. VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl();
  2304. return cast<VarTemplateSpecializationDecl>(Recent);
  2305. }
  2306. /// Retrieve the template that this specialization specializes.
  2307. VarTemplateDecl *getSpecializedTemplate() const;
  2308. /// Retrieve the template arguments of the variable template
  2309. /// specialization.
  2310. const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; }
  2311. // TODO: Always set this when creating the new specialization?
  2312. void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo);
  2313. const TemplateArgumentListInfo &getTemplateArgsInfo() const {
  2314. return TemplateArgsInfo;
  2315. }
  2316. /// Determine the kind of specialization that this
  2317. /// declaration represents.
  2318. TemplateSpecializationKind getSpecializationKind() const {
  2319. return static_cast<TemplateSpecializationKind>(SpecializationKind);
  2320. }
  2321. bool isExplicitSpecialization() const {
  2322. return getSpecializationKind() == TSK_ExplicitSpecialization;
  2323. }
  2324. bool isClassScopeExplicitSpecialization() const {
  2325. return isExplicitSpecialization() &&
  2326. isa<CXXRecordDecl>(getLexicalDeclContext());
  2327. }
  2328. /// True if this declaration is an explicit specialization,
  2329. /// explicit instantiation declaration, or explicit instantiation
  2330. /// definition.
  2331. bool isExplicitInstantiationOrSpecialization() const {
  2332. return isTemplateExplicitInstantiationOrSpecialization(
  2333. getTemplateSpecializationKind());
  2334. }
  2335. void setSpecializationKind(TemplateSpecializationKind TSK) {
  2336. SpecializationKind = TSK;
  2337. }
  2338. /// Get the point of instantiation (if any), or null if none.
  2339. SourceLocation getPointOfInstantiation() const {
  2340. return PointOfInstantiation;
  2341. }
  2342. void setPointOfInstantiation(SourceLocation Loc) {
  2343. assert(Loc.isValid() && "point of instantiation must be valid!");
  2344. PointOfInstantiation = Loc;
  2345. }
  2346. void setCompleteDefinition() { IsCompleteDefinition = true; }
  2347. /// If this variable template specialization is an instantiation of
  2348. /// a template (rather than an explicit specialization), return the
  2349. /// variable template or variable template partial specialization from which
  2350. /// it was instantiated.
  2351. llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
  2352. getInstantiatedFrom() const {
  2353. if (!isTemplateInstantiation(getSpecializationKind()))
  2354. return llvm::PointerUnion<VarTemplateDecl *,
  2355. VarTemplatePartialSpecializationDecl *>();
  2356. return getSpecializedTemplateOrPartial();
  2357. }
  2358. /// Retrieve the variable template or variable template partial
  2359. /// specialization which was specialized by this.
  2360. llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
  2361. getSpecializedTemplateOrPartial() const {
  2362. if (const auto *PartialSpec =
  2363. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
  2364. return PartialSpec->PartialSpecialization;
  2365. return SpecializedTemplate.get<VarTemplateDecl *>();
  2366. }
  2367. /// Retrieve the set of template arguments that should be used
  2368. /// to instantiate the initializer of the variable template or variable
  2369. /// template partial specialization from which this variable template
  2370. /// specialization was instantiated.
  2371. ///
  2372. /// \returns For a variable template specialization instantiated from the
  2373. /// primary template, this function will return the same template arguments
  2374. /// as getTemplateArgs(). For a variable template specialization instantiated
  2375. /// from a variable template partial specialization, this function will the
  2376. /// return deduced template arguments for the variable template partial
  2377. /// specialization itself.
  2378. const TemplateArgumentList &getTemplateInstantiationArgs() const {
  2379. if (const auto *PartialSpec =
  2380. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
  2381. return *PartialSpec->TemplateArgs;
  2382. return getTemplateArgs();
  2383. }
  2384. /// Note that this variable template specialization is actually an
  2385. /// instantiation of the given variable template partial specialization whose
  2386. /// template arguments have been deduced.
  2387. void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec,
  2388. const TemplateArgumentList *TemplateArgs) {
  2389. assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
  2390. "Already set to a variable template partial specialization!");
  2391. auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
  2392. PS->PartialSpecialization = PartialSpec;
  2393. PS->TemplateArgs = TemplateArgs;
  2394. SpecializedTemplate = PS;
  2395. }
  2396. /// Note that this variable template specialization is an instantiation
  2397. /// of the given variable template.
  2398. void setInstantiationOf(VarTemplateDecl *TemplDecl) {
  2399. assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
  2400. "Previously set to a variable template partial specialization!");
  2401. SpecializedTemplate = TemplDecl;
  2402. }
  2403. /// Sets the type of this specialization as it was written by
  2404. /// the user.
  2405. void setTypeAsWritten(TypeSourceInfo *T) {
  2406. if (!ExplicitInfo)
  2407. ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
  2408. ExplicitInfo->TypeAsWritten = T;
  2409. }
  2410. /// Gets the type of this specialization as it was written by
  2411. /// the user, if it was so written.
  2412. TypeSourceInfo *getTypeAsWritten() const {
  2413. return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
  2414. }
  2415. /// Gets the location of the extern keyword, if present.
  2416. SourceLocation getExternLoc() const {
  2417. return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
  2418. }
  2419. /// Sets the location of the extern keyword.
  2420. void setExternLoc(SourceLocation Loc) {
  2421. if (!ExplicitInfo)
  2422. ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
  2423. ExplicitInfo->ExternLoc = Loc;
  2424. }
  2425. /// Sets the location of the template keyword.
  2426. void setTemplateKeywordLoc(SourceLocation Loc) {
  2427. if (!ExplicitInfo)
  2428. ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
  2429. ExplicitInfo->TemplateKeywordLoc = Loc;
  2430. }
  2431. /// Gets the location of the template keyword, if present.
  2432. SourceLocation getTemplateKeywordLoc() const {
  2433. return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
  2434. }
  2435. void Profile(llvm::FoldingSetNodeID &ID) const {
  2436. Profile(ID, TemplateArgs->asArray(), getASTContext());
  2437. }
  2438. static void Profile(llvm::FoldingSetNodeID &ID,
  2439. ArrayRef<TemplateArgument> TemplateArgs,
  2440. ASTContext &Context) {
  2441. ID.AddInteger(TemplateArgs.size());
  2442. for (const TemplateArgument &TemplateArg : TemplateArgs)
  2443. TemplateArg.Profile(ID, Context);
  2444. }
  2445. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2446. static bool classofKind(Kind K) {
  2447. return K >= firstVarTemplateSpecialization &&
  2448. K <= lastVarTemplateSpecialization;
  2449. }
  2450. };
  2451. class VarTemplatePartialSpecializationDecl
  2452. : public VarTemplateSpecializationDecl {
  2453. /// The list of template parameters
  2454. TemplateParameterList *TemplateParams = nullptr;
  2455. /// The source info for the template arguments as written.
  2456. /// FIXME: redundant with TypeAsWritten?
  2457. const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
  2458. /// The variable template partial specialization from which this
  2459. /// variable template partial specialization was instantiated.
  2460. ///
  2461. /// The boolean value will be true to indicate that this variable template
  2462. /// partial specialization was specialized at this level.
  2463. llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool>
  2464. InstantiatedFromMember;
  2465. VarTemplatePartialSpecializationDecl(
  2466. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  2467. SourceLocation IdLoc, TemplateParameterList *Params,
  2468. VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
  2469. StorageClass S, ArrayRef<TemplateArgument> Args,
  2470. const ASTTemplateArgumentListInfo *ArgInfos);
  2471. VarTemplatePartialSpecializationDecl(ASTContext &Context)
  2472. : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization,
  2473. Context),
  2474. InstantiatedFromMember(nullptr, false) {}
  2475. void anchor() override;
  2476. public:
  2477. friend class ASTDeclReader;
  2478. friend class ASTDeclWriter;
  2479. static VarTemplatePartialSpecializationDecl *
  2480. Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  2481. SourceLocation IdLoc, TemplateParameterList *Params,
  2482. VarTemplateDecl *SpecializedTemplate, QualType T,
  2483. TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args,
  2484. const TemplateArgumentListInfo &ArgInfos);
  2485. static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C,
  2486. unsigned ID);
  2487. VarTemplatePartialSpecializationDecl *getMostRecentDecl() {
  2488. return cast<VarTemplatePartialSpecializationDecl>(
  2489. static_cast<VarTemplateSpecializationDecl *>(
  2490. this)->getMostRecentDecl());
  2491. }
  2492. /// Get the list of template parameters
  2493. TemplateParameterList *getTemplateParameters() const {
  2494. return TemplateParams;
  2495. }
  2496. /// Get the template arguments as written.
  2497. const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
  2498. return ArgsAsWritten;
  2499. }
  2500. /// \brief All associated constraints of this partial specialization,
  2501. /// including the requires clause and any constraints derived from
  2502. /// constrained-parameters.
  2503. ///
  2504. /// The constraints in the resulting list are to be treated as if in a
  2505. /// conjunction ("and").
  2506. void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  2507. TemplateParams->getAssociatedConstraints(AC);
  2508. }
  2509. bool hasAssociatedConstraints() const {
  2510. return TemplateParams->hasAssociatedConstraints();
  2511. }
  2512. /// \brief Retrieve the member variable template partial specialization from
  2513. /// which this particular variable template partial specialization was
  2514. /// instantiated.
  2515. ///
  2516. /// \code
  2517. /// template<typename T>
  2518. /// struct Outer {
  2519. /// template<typename U> U Inner;
  2520. /// template<typename U> U* Inner<U*> = (U*)(0); // #1
  2521. /// };
  2522. ///
  2523. /// template int* Outer<float>::Inner<int*>;
  2524. /// \endcode
  2525. ///
  2526. /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
  2527. /// end up instantiating the partial specialization
  2528. /// \c Outer<float>::Inner<U*>, which itself was instantiated from the
  2529. /// variable template partial specialization \c Outer<T>::Inner<U*>. Given
  2530. /// \c Outer<float>::Inner<U*>, this function would return
  2531. /// \c Outer<T>::Inner<U*>.
  2532. VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() const {
  2533. const auto *First =
  2534. cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
  2535. return First->InstantiatedFromMember.getPointer();
  2536. }
  2537. void
  2538. setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) {
  2539. auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
  2540. First->InstantiatedFromMember.setPointer(PartialSpec);
  2541. }
  2542. /// Determines whether this variable template partial specialization
  2543. /// was a specialization of a member partial specialization.
  2544. ///
  2545. /// In the following example, the member template partial specialization
  2546. /// \c X<int>::Inner<T*> is a member specialization.
  2547. ///
  2548. /// \code
  2549. /// template<typename T>
  2550. /// struct X {
  2551. /// template<typename U> U Inner;
  2552. /// template<typename U> U* Inner<U*> = (U*)(0);
  2553. /// };
  2554. ///
  2555. /// template<> template<typename T>
  2556. /// U* X<int>::Inner<T*> = (T*)(0) + 1;
  2557. /// \endcode
  2558. bool isMemberSpecialization() {
  2559. const auto *First =
  2560. cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
  2561. return First->InstantiatedFromMember.getInt();
  2562. }
  2563. /// Note that this member template is a specialization.
  2564. void setMemberSpecialization() {
  2565. auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
  2566. assert(First->InstantiatedFromMember.getPointer() &&
  2567. "Only member templates can be member template specializations");
  2568. return First->InstantiatedFromMember.setInt(true);
  2569. }
  2570. void Profile(llvm::FoldingSetNodeID &ID) const {
  2571. Profile(ID, getTemplateArgs().asArray(), getTemplateParameters(),
  2572. getASTContext());
  2573. }
  2574. static void
  2575. Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
  2576. TemplateParameterList *TPL, ASTContext &Context);
  2577. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2578. static bool classofKind(Kind K) {
  2579. return K == VarTemplatePartialSpecialization;
  2580. }
  2581. };
  2582. /// Declaration of a variable template.
  2583. class VarTemplateDecl : public RedeclarableTemplateDecl {
  2584. protected:
  2585. /// Data that is common to all of the declarations of a given
  2586. /// variable template.
  2587. struct Common : CommonBase {
  2588. /// The variable template specializations for this variable
  2589. /// template, including explicit specializations and instantiations.
  2590. llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations;
  2591. /// The variable template partial specializations for this variable
  2592. /// template.
  2593. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
  2594. PartialSpecializations;
  2595. Common() = default;
  2596. };
  2597. /// Retrieve the set of specializations of this variable template.
  2598. llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
  2599. getSpecializations() const;
  2600. /// Retrieve the set of partial specializations of this class
  2601. /// template.
  2602. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
  2603. getPartialSpecializations() const;
  2604. VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
  2605. DeclarationName Name, TemplateParameterList *Params,
  2606. NamedDecl *Decl)
  2607. : RedeclarableTemplateDecl(VarTemplate, C, DC, L, Name, Params, Decl) {}
  2608. CommonBase *newCommon(ASTContext &C) const override;
  2609. Common *getCommonPtr() const {
  2610. return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
  2611. }
  2612. public:
  2613. friend class ASTDeclReader;
  2614. friend class ASTDeclWriter;
  2615. /// Load any lazily-loaded specializations from the external source.
  2616. void LoadLazySpecializations() const;
  2617. /// Get the underlying variable declarations of the template.
  2618. VarDecl *getTemplatedDecl() const {
  2619. return static_cast<VarDecl *>(TemplatedDecl);
  2620. }
  2621. /// Returns whether this template declaration defines the primary
  2622. /// variable pattern.
  2623. bool isThisDeclarationADefinition() const {
  2624. return getTemplatedDecl()->isThisDeclarationADefinition();
  2625. }
  2626. VarTemplateDecl *getDefinition();
  2627. /// Create a variable template node.
  2628. static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC,
  2629. SourceLocation L, DeclarationName Name,
  2630. TemplateParameterList *Params,
  2631. VarDecl *Decl);
  2632. /// Create an empty variable template node.
  2633. static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2634. /// Return the specialization with the provided arguments if it exists,
  2635. /// otherwise return the insertion point.
  2636. VarTemplateSpecializationDecl *
  2637. findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
  2638. /// Insert the specified specialization knowing that it is not already
  2639. /// in. InsertPos must be obtained from findSpecialization.
  2640. void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos);
  2641. VarTemplateDecl *getCanonicalDecl() override {
  2642. return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
  2643. }
  2644. const VarTemplateDecl *getCanonicalDecl() const {
  2645. return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
  2646. }
  2647. /// Retrieve the previous declaration of this variable template, or
  2648. /// nullptr if no such declaration exists.
  2649. VarTemplateDecl *getPreviousDecl() {
  2650. return cast_or_null<VarTemplateDecl>(
  2651. static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
  2652. }
  2653. const VarTemplateDecl *getPreviousDecl() const {
  2654. return cast_or_null<VarTemplateDecl>(
  2655. static_cast<const RedeclarableTemplateDecl *>(
  2656. this)->getPreviousDecl());
  2657. }
  2658. VarTemplateDecl *getMostRecentDecl() {
  2659. return cast<VarTemplateDecl>(
  2660. static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
  2661. }
  2662. const VarTemplateDecl *getMostRecentDecl() const {
  2663. return const_cast<VarTemplateDecl *>(this)->getMostRecentDecl();
  2664. }
  2665. VarTemplateDecl *getInstantiatedFromMemberTemplate() const {
  2666. return cast_or_null<VarTemplateDecl>(
  2667. RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
  2668. }
  2669. /// Return the partial specialization with the provided arguments if it
  2670. /// exists, otherwise return the insertion point.
  2671. VarTemplatePartialSpecializationDecl *
  2672. findPartialSpecialization(ArrayRef<TemplateArgument> Args,
  2673. TemplateParameterList *TPL, void *&InsertPos);
  2674. /// Insert the specified partial specialization knowing that it is not
  2675. /// already in. InsertPos must be obtained from findPartialSpecialization.
  2676. void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D,
  2677. void *InsertPos);
  2678. /// Retrieve the partial specializations as an ordered list.
  2679. void getPartialSpecializations(
  2680. SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const;
  2681. /// Find a variable template partial specialization which was
  2682. /// instantiated
  2683. /// from the given member partial specialization.
  2684. ///
  2685. /// \param D a member variable template partial specialization.
  2686. ///
  2687. /// \returns the variable template partial specialization which was
  2688. /// instantiated
  2689. /// from the given member partial specialization, or nullptr if no such
  2690. /// partial specialization exists.
  2691. VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember(
  2692. VarTemplatePartialSpecializationDecl *D);
  2693. using spec_iterator = SpecIterator<VarTemplateSpecializationDecl>;
  2694. using spec_range = llvm::iterator_range<spec_iterator>;
  2695. spec_range specializations() const {
  2696. return spec_range(spec_begin(), spec_end());
  2697. }
  2698. spec_iterator spec_begin() const {
  2699. return makeSpecIterator(getSpecializations(), false);
  2700. }
  2701. spec_iterator spec_end() const {
  2702. return makeSpecIterator(getSpecializations(), true);
  2703. }
  2704. // Implement isa/cast/dyncast support
  2705. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2706. static bool classofKind(Kind K) { return K == VarTemplate; }
  2707. };
  2708. /// Declaration of a C++2a concept.
  2709. class ConceptDecl : public TemplateDecl, public Mergeable<ConceptDecl> {
  2710. protected:
  2711. Expr *ConstraintExpr;
  2712. ConceptDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
  2713. TemplateParameterList *Params, Expr *ConstraintExpr)
  2714. : TemplateDecl(Concept, DC, L, Name, Params),
  2715. ConstraintExpr(ConstraintExpr) {};
  2716. public:
  2717. static ConceptDecl *Create(ASTContext &C, DeclContext *DC,
  2718. SourceLocation L, DeclarationName Name,
  2719. TemplateParameterList *Params,
  2720. Expr *ConstraintExpr);
  2721. static ConceptDecl *CreateDeserialized(ASTContext &C, unsigned ID);
  2722. Expr *getConstraintExpr() const {
  2723. return ConstraintExpr;
  2724. }
  2725. SourceRange getSourceRange() const override LLVM_READONLY {
  2726. return SourceRange(getTemplateParameters()->getTemplateLoc(),
  2727. ConstraintExpr->getEndLoc());
  2728. }
  2729. bool isTypeConcept() const {
  2730. return isa<TemplateTypeParmDecl>(getTemplateParameters()->getParam(0));
  2731. }
  2732. ConceptDecl *getCanonicalDecl() override { return getFirstDecl(); }
  2733. const ConceptDecl *getCanonicalDecl() const { return getFirstDecl(); }
  2734. // Implement isa/cast/dyncast/etc.
  2735. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2736. static bool classofKind(Kind K) { return K == Concept; }
  2737. friend class ASTReader;
  2738. friend class ASTDeclReader;
  2739. friend class ASTDeclWriter;
  2740. };
  2741. /// A template parameter object.
  2742. ///
  2743. /// Template parameter objects represent values of class type used as template
  2744. /// arguments. There is one template parameter object for each such distinct
  2745. /// value used as a template argument across the program.
  2746. ///
  2747. /// \code
  2748. /// struct A { int x, y; };
  2749. /// template<A> struct S;
  2750. /// S<A{1, 2}> s1;
  2751. /// S<A{1, 2}> s2; // same type, argument is same TemplateParamObjectDecl.
  2752. /// \endcode
  2753. class TemplateParamObjectDecl : public ValueDecl,
  2754. public Mergeable<TemplateParamObjectDecl>,
  2755. public llvm::FoldingSetNode {
  2756. private:
  2757. /// The value of this template parameter object.
  2758. APValue Value;
  2759. TemplateParamObjectDecl(DeclContext *DC, QualType T, const APValue &V)
  2760. : ValueDecl(TemplateParamObject, DC, SourceLocation(), DeclarationName(),
  2761. T),
  2762. Value(V) {}
  2763. static TemplateParamObjectDecl *Create(const ASTContext &C, QualType T,
  2764. const APValue &V);
  2765. static TemplateParamObjectDecl *CreateDeserialized(ASTContext &C,
  2766. unsigned ID);
  2767. /// Only ASTContext::getTemplateParamObjectDecl and deserialization
  2768. /// create these.
  2769. friend class ASTContext;
  2770. friend class ASTReader;
  2771. friend class ASTDeclReader;
  2772. public:
  2773. /// Print this template parameter object in a human-readable format.
  2774. void printName(llvm::raw_ostream &OS) const override;
  2775. /// Print this object as an equivalent expression.
  2776. void printAsExpr(llvm::raw_ostream &OS) const;
  2777. /// Print this object as an initializer suitable for a variable of the
  2778. /// object's type.
  2779. void printAsInit(llvm::raw_ostream &OS) const;
  2780. const APValue &getValue() const { return Value; }
  2781. static void Profile(llvm::FoldingSetNodeID &ID, QualType T,
  2782. const APValue &V) {
  2783. ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
  2784. V.Profile(ID);
  2785. }
  2786. void Profile(llvm::FoldingSetNodeID &ID) {
  2787. Profile(ID, getType(), getValue());
  2788. }
  2789. TemplateParamObjectDecl *getCanonicalDecl() override {
  2790. return getFirstDecl();
  2791. }
  2792. const TemplateParamObjectDecl *getCanonicalDecl() const {
  2793. return getFirstDecl();
  2794. }
  2795. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
  2796. static bool classofKind(Kind K) { return K == TemplateParamObject; }
  2797. };
  2798. inline NamedDecl *getAsNamedDecl(TemplateParameter P) {
  2799. if (auto *PD = P.dyn_cast<TemplateTypeParmDecl *>())
  2800. return PD;
  2801. if (auto *PD = P.dyn_cast<NonTypeTemplateParmDecl *>())
  2802. return PD;
  2803. return P.get<TemplateTemplateParmDecl *>();
  2804. }
  2805. inline TemplateDecl *getAsTypeTemplateDecl(Decl *D) {
  2806. auto *TD = dyn_cast<TemplateDecl>(D);
  2807. return TD && (isa<ClassTemplateDecl>(TD) ||
  2808. isa<ClassTemplatePartialSpecializationDecl>(TD) ||
  2809. isa<TypeAliasTemplateDecl>(TD) ||
  2810. isa<TemplateTemplateParmDecl>(TD))
  2811. ? TD
  2812. : nullptr;
  2813. }
  2814. /// Check whether the template parameter is a pack expansion, and if so,
  2815. /// determine the number of parameters produced by that expansion. For instance:
  2816. ///
  2817. /// \code
  2818. /// template<typename ...Ts> struct A {
  2819. /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B;
  2820. /// };
  2821. /// \endcode
  2822. ///
  2823. /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us
  2824. /// is not a pack expansion, so returns an empty Optional.
  2825. inline Optional<unsigned> getExpandedPackSize(const NamedDecl *Param) {
  2826. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
  2827. if (TTP->isExpandedParameterPack())
  2828. return TTP->getNumExpansionParameters();
  2829. }
  2830. if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
  2831. if (NTTP->isExpandedParameterPack())
  2832. return NTTP->getNumExpansionTypes();
  2833. }
  2834. if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) {
  2835. if (TTP->isExpandedParameterPack())
  2836. return TTP->getNumExpansionTemplateParameters();
  2837. }
  2838. return None;
  2839. }
  2840. } // namespace clang
  2841. #endif // LLVM_CLANG_AST_DECLTEMPLATE_H
  2842. #ifdef __GNUC__
  2843. #pragma GCC diagnostic pop
  2844. #endif