ParseTemplate.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. //===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements parsing of C++ templates.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Parse/Parser.h"
  14. #include "RAIIObjectsForParser.h"
  15. #include "clang/AST/ASTConsumer.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/DeclTemplate.h"
  18. #include "clang/Parse/ParseDiagnostic.h"
  19. #include "clang/Sema/DeclSpec.h"
  20. #include "clang/Sema/ParsedTemplate.h"
  21. #include "clang/Sema/Scope.h"
  22. using namespace clang;
  23. /// \brief Parse a template declaration, explicit instantiation, or
  24. /// explicit specialization.
  25. Decl *
  26. Parser::ParseDeclarationStartingWithTemplate(unsigned Context,
  27. SourceLocation &DeclEnd,
  28. AccessSpecifier AS,
  29. AttributeList *AccessAttrs) {
  30. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  31. ObjCDeclContextSwitch ObjCDC(*this);
  32. if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
  33. return ParseExplicitInstantiation(Context,
  34. SourceLocation(), ConsumeToken(),
  35. DeclEnd, AS);
  36. }
  37. return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS,
  38. AccessAttrs);
  39. }
  40. /// \brief Parse a template declaration or an explicit specialization.
  41. ///
  42. /// Template declarations include one or more template parameter lists
  43. /// and either the function or class template declaration. Explicit
  44. /// specializations contain one or more 'template < >' prefixes
  45. /// followed by a (possibly templated) declaration. Since the
  46. /// syntactic form of both features is nearly identical, we parse all
  47. /// of the template headers together and let semantic analysis sort
  48. /// the declarations from the explicit specializations.
  49. ///
  50. /// template-declaration: [C++ temp]
  51. /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
  52. ///
  53. /// explicit-specialization: [ C++ temp.expl.spec]
  54. /// 'template' '<' '>' declaration
  55. Decl *
  56. Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context,
  57. SourceLocation &DeclEnd,
  58. AccessSpecifier AS,
  59. AttributeList *AccessAttrs) {
  60. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  61. assert(Tok.isOneOf(tok::kw_export, tok::kw_template) &&
  62. "Token does not start a template declaration.");
  63. // Enter template-parameter scope.
  64. ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
  65. // Tell the action that names should be checked in the context of
  66. // the declaration to come.
  67. ParsingDeclRAIIObject
  68. ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
  69. // Parse multiple levels of template headers within this template
  70. // parameter scope, e.g.,
  71. //
  72. // template<typename T>
  73. // template<typename U>
  74. // class A<T>::B { ... };
  75. //
  76. // We parse multiple levels non-recursively so that we can build a
  77. // single data structure containing all of the template parameter
  78. // lists to easily differentiate between the case above and:
  79. //
  80. // template<typename T>
  81. // class A {
  82. // template<typename U> class B;
  83. // };
  84. //
  85. // In the first case, the action for declaring A<T>::B receives
  86. // both template parameter lists. In the second case, the action for
  87. // defining A<T>::B receives just the inner template parameter list
  88. // (and retrieves the outer template parameter list from its
  89. // context).
  90. bool isSpecialization = true;
  91. bool LastParamListWasEmpty = false;
  92. TemplateParameterLists ParamLists;
  93. TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
  94. do {
  95. // Consume the 'export', if any.
  96. SourceLocation ExportLoc;
  97. TryConsumeToken(tok::kw_export, ExportLoc);
  98. // Consume the 'template', which should be here.
  99. SourceLocation TemplateLoc;
  100. if (!TryConsumeToken(tok::kw_template, TemplateLoc)) {
  101. Diag(Tok.getLocation(), diag::err_expected_template);
  102. return nullptr;
  103. }
  104. // Parse the '<' template-parameter-list '>'
  105. SourceLocation LAngleLoc, RAngleLoc;
  106. SmallVector<Decl*, 4> TemplateParams;
  107. if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(),
  108. TemplateParams, LAngleLoc, RAngleLoc)) {
  109. // Skip until the semi-colon or a '}'.
  110. SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
  111. TryConsumeToken(tok::semi);
  112. return nullptr;
  113. }
  114. ParamLists.push_back(
  115. Actions.ActOnTemplateParameterList(CurTemplateDepthTracker.getDepth(),
  116. ExportLoc,
  117. TemplateLoc, LAngleLoc,
  118. TemplateParams.data(),
  119. TemplateParams.size(), RAngleLoc));
  120. if (!TemplateParams.empty()) {
  121. isSpecialization = false;
  122. ++CurTemplateDepthTracker;
  123. if (TryConsumeToken(tok::kw_requires)) {
  124. ExprResult ER =
  125. Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression());
  126. if (!ER.isUsable()) {
  127. // Skip until the semi-colon or a '}'.
  128. SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
  129. TryConsumeToken(tok::semi);
  130. return nullptr;
  131. }
  132. }
  133. } else {
  134. LastParamListWasEmpty = true;
  135. }
  136. } while (Tok.isOneOf(tok::kw_export, tok::kw_template));
  137. // Parse the actual template declaration.
  138. return ParseSingleDeclarationAfterTemplate(Context,
  139. ParsedTemplateInfo(&ParamLists,
  140. isSpecialization,
  141. LastParamListWasEmpty),
  142. ParsingTemplateParams,
  143. DeclEnd, AS, AccessAttrs);
  144. }
  145. /// \brief Parse a single declaration that declares a template,
  146. /// template specialization, or explicit instantiation of a template.
  147. ///
  148. /// \param DeclEnd will receive the source location of the last token
  149. /// within this declaration.
  150. ///
  151. /// \param AS the access specifier associated with this
  152. /// declaration. Will be AS_none for namespace-scope declarations.
  153. ///
  154. /// \returns the new declaration.
  155. Decl *
  156. Parser::ParseSingleDeclarationAfterTemplate(
  157. unsigned Context,
  158. const ParsedTemplateInfo &TemplateInfo,
  159. ParsingDeclRAIIObject &DiagsFromTParams,
  160. SourceLocation &DeclEnd,
  161. AccessSpecifier AS,
  162. AttributeList *AccessAttrs) {
  163. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  164. assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
  165. "Template information required");
  166. if (Tok.is(tok::kw_static_assert)) {
  167. // A static_assert declaration may not be templated.
  168. Diag(Tok.getLocation(), diag::err_templated_invalid_declaration)
  169. << TemplateInfo.getSourceRange();
  170. // Parse the static_assert declaration to improve error recovery.
  171. return ParseStaticAssertDeclaration(DeclEnd);
  172. }
  173. if (Context == Declarator::MemberContext) {
  174. // We are parsing a member template.
  175. ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
  176. &DiagsFromTParams);
  177. return nullptr;
  178. }
  179. ParsedAttributesWithRange prefixAttrs(AttrFactory);
  180. MaybeParseCXX11Attributes(prefixAttrs);
  181. // HLSL Change: comment only - MaybeParseHLSLAttributes would go here if allowed at this point
  182. if (Tok.is(tok::kw_using))
  183. // HLSL Change Starts
  184. {
  185. if (getLangOpts().HLSL) {
  186. Diag(Tok, diag::err_hlsl_reserved_keyword) << "using";
  187. SkipMalformedDecl();
  188. }
  189. else {
  190. // HLSL Change Ends - succeeding statement is now conditional
  191. return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
  192. prefixAttrs);
  193. } // HLSL Change - close conditional
  194. } // HLSL Change - close conditional
  195. // Parse the declaration specifiers, stealing any diagnostics from
  196. // the template parameters.
  197. ParsingDeclSpec DS(*this, &DiagsFromTParams);
  198. ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
  199. getDeclSpecContextFromDeclaratorContext(Context));
  200. if (Tok.is(tok::semi)) {
  201. ProhibitAttributes(prefixAttrs);
  202. DeclEnd = ConsumeToken();
  203. Decl *Decl = Actions.ParsedFreeStandingDeclSpec(
  204. getCurScope(), AS, DS,
  205. TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams
  206. : MultiTemplateParamsArg(),
  207. TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation);
  208. DS.complete(Decl);
  209. return Decl;
  210. }
  211. // Move the attributes from the prefix into the DS.
  212. if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
  213. ProhibitAttributes(prefixAttrs);
  214. else
  215. DS.takeAttributesFrom(prefixAttrs);
  216. // Parse the declarator.
  217. ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context);
  218. ParseDeclarator(DeclaratorInfo);
  219. // Error parsing the declarator?
  220. if (!DeclaratorInfo.hasName()) {
  221. // If so, skip until the semi-colon or a }.
  222. SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
  223. if (Tok.is(tok::semi))
  224. ConsumeToken();
  225. return nullptr;
  226. }
  227. LateParsedAttrList LateParsedAttrs(true);
  228. if (DeclaratorInfo.isFunctionDeclarator())
  229. MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
  230. if (DeclaratorInfo.isFunctionDeclarator() &&
  231. isStartOfFunctionDefinition(DeclaratorInfo)) {
  232. // Function definitions are only allowed at file scope and in C++ classes.
  233. // The C++ inline method definition case is handled elsewhere, so we only
  234. // need to handle the file scope definition case.
  235. if (Context != Declarator::FileContext) {
  236. Diag(Tok, diag::err_function_definition_not_allowed);
  237. SkipMalformedDecl();
  238. return nullptr;
  239. }
  240. if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
  241. // Recover by ignoring the 'typedef'. This was probably supposed to be
  242. // the 'typename' keyword, which we should have already suggested adding
  243. // if it's appropriate.
  244. Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef)
  245. << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
  246. DS.ClearStorageClassSpecs();
  247. }
  248. if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
  249. if (DeclaratorInfo.getName().getKind() != UnqualifiedId::IK_TemplateId) {
  250. // If the declarator-id is not a template-id, issue a diagnostic and
  251. // recover by ignoring the 'template' keyword.
  252. Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0;
  253. return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(),
  254. &LateParsedAttrs);
  255. } else {
  256. SourceLocation LAngleLoc
  257. = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
  258. Diag(DeclaratorInfo.getIdentifierLoc(),
  259. diag::err_explicit_instantiation_with_definition)
  260. << SourceRange(TemplateInfo.TemplateLoc)
  261. << FixItHint::CreateInsertion(LAngleLoc, "<>");
  262. // Recover as if it were an explicit specialization.
  263. TemplateParameterLists FakedParamLists;
  264. FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
  265. 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
  266. 0, LAngleLoc));
  267. return ParseFunctionDefinition(
  268. DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists,
  269. /*isSpecialization=*/true,
  270. /*LastParamListWasEmpty=*/true),
  271. &LateParsedAttrs);
  272. }
  273. }
  274. return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo,
  275. &LateParsedAttrs);
  276. }
  277. // Parse this declaration.
  278. Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
  279. TemplateInfo);
  280. if (Tok.is(tok::comma)) {
  281. Diag(Tok, diag::err_multiple_template_declarators)
  282. << (int)TemplateInfo.Kind;
  283. SkipUntil(tok::semi);
  284. return ThisDecl;
  285. }
  286. // Eat the semi colon after the declaration.
  287. ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
  288. if (LateParsedAttrs.size() > 0)
  289. ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false);
  290. DeclaratorInfo.complete(ThisDecl);
  291. return ThisDecl;
  292. }
  293. /// ParseTemplateParameters - Parses a template-parameter-list enclosed in
  294. /// angle brackets. Depth is the depth of this template-parameter-list, which
  295. /// is the number of template headers directly enclosing this template header.
  296. /// TemplateParams is the current list of template parameters we're building.
  297. /// The template parameter we parse will be added to this list. LAngleLoc and
  298. /// RAngleLoc will receive the positions of the '<' and '>', respectively,
  299. /// that enclose this template parameter list.
  300. ///
  301. /// \returns true if an error occurred, false otherwise.
  302. bool Parser::ParseTemplateParameters(unsigned Depth,
  303. SmallVectorImpl<Decl*> &TemplateParams,
  304. SourceLocation &LAngleLoc,
  305. SourceLocation &RAngleLoc) {
  306. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  307. // Get the template parameter list.
  308. if (!TryConsumeToken(tok::less, LAngleLoc)) {
  309. Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
  310. return true;
  311. }
  312. // Try to parse the template parameter list.
  313. bool Failed = false;
  314. if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater))
  315. Failed = ParseTemplateParameterList(Depth, TemplateParams);
  316. if (Tok.is(tok::greatergreater)) {
  317. // No diagnostic required here: a template-parameter-list can only be
  318. // followed by a declaration or, for a template template parameter, the
  319. // 'class' keyword. Therefore, the second '>' will be diagnosed later.
  320. // This matters for elegant diagnosis of:
  321. // template<template<typename>> struct S;
  322. Tok.setKind(tok::greater);
  323. RAngleLoc = Tok.getLocation();
  324. Tok.setLocation(Tok.getLocation().getLocWithOffset(1));
  325. } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) {
  326. Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
  327. return true;
  328. }
  329. return false;
  330. }
  331. /// ParseTemplateParameterList - Parse a template parameter list. If
  332. /// the parsing fails badly (i.e., closing bracket was left out), this
  333. /// will try to put the token stream in a reasonable position (closing
  334. /// a statement, etc.) and return false.
  335. ///
  336. /// template-parameter-list: [C++ temp]
  337. /// template-parameter
  338. /// template-parameter-list ',' template-parameter
  339. bool
  340. Parser::ParseTemplateParameterList(unsigned Depth,
  341. SmallVectorImpl<Decl*> &TemplateParams) {
  342. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  343. while (1) {
  344. if (Decl *TmpParam
  345. = ParseTemplateParameter(Depth, TemplateParams.size())) {
  346. TemplateParams.push_back(TmpParam);
  347. } else {
  348. // If we failed to parse a template parameter, skip until we find
  349. // a comma or closing brace.
  350. SkipUntil(tok::comma, tok::greater, tok::greatergreater,
  351. StopAtSemi | StopBeforeMatch);
  352. }
  353. // Did we find a comma or the end of the template parameter list?
  354. if (Tok.is(tok::comma)) {
  355. ConsumeToken();
  356. } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) {
  357. // Don't consume this... that's done by template parser.
  358. break;
  359. } else {
  360. // Somebody probably forgot to close the template. Skip ahead and
  361. // try to get out of the expression. This error is currently
  362. // subsumed by whatever goes on in ParseTemplateParameter.
  363. Diag(Tok.getLocation(), diag::err_expected_comma_greater);
  364. SkipUntil(tok::comma, tok::greater, tok::greatergreater,
  365. StopAtSemi | StopBeforeMatch);
  366. return false;
  367. }
  368. }
  369. return true;
  370. }
  371. /// \brief Determine whether the parser is at the start of a template
  372. /// type parameter.
  373. bool Parser::isStartOfTemplateTypeParameter() {
  374. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  375. if (Tok.is(tok::kw_class)) {
  376. // "class" may be the start of an elaborated-type-specifier or a
  377. // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
  378. switch (NextToken().getKind()) {
  379. case tok::equal:
  380. case tok::comma:
  381. case tok::greater:
  382. case tok::greatergreater:
  383. case tok::ellipsis:
  384. return true;
  385. case tok::identifier:
  386. // This may be either a type-parameter or an elaborated-type-specifier.
  387. // We have to look further.
  388. break;
  389. default:
  390. return false;
  391. }
  392. switch (GetLookAheadToken(2).getKind()) {
  393. case tok::equal:
  394. case tok::comma:
  395. case tok::greater:
  396. case tok::greatergreater:
  397. return true;
  398. default:
  399. return false;
  400. }
  401. }
  402. if (Tok.isNot(tok::kw_typename))
  403. return false;
  404. // C++ [temp.param]p2:
  405. // There is no semantic difference between class and typename in a
  406. // template-parameter. typename followed by an unqualified-id
  407. // names a template type parameter. typename followed by a
  408. // qualified-id denotes the type in a non-type
  409. // parameter-declaration.
  410. Token Next = NextToken();
  411. // If we have an identifier, skip over it.
  412. if (Next.getKind() == tok::identifier)
  413. Next = GetLookAheadToken(2);
  414. switch (Next.getKind()) {
  415. case tok::equal:
  416. case tok::comma:
  417. case tok::greater:
  418. case tok::greatergreater:
  419. case tok::ellipsis:
  420. return true;
  421. default:
  422. return false;
  423. }
  424. }
  425. /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
  426. ///
  427. /// template-parameter: [C++ temp.param]
  428. /// type-parameter
  429. /// parameter-declaration
  430. ///
  431. /// type-parameter: (see below)
  432. /// 'class' ...[opt] identifier[opt]
  433. /// 'class' identifier[opt] '=' type-id
  434. /// 'typename' ...[opt] identifier[opt]
  435. /// 'typename' identifier[opt] '=' type-id
  436. /// 'template' '<' template-parameter-list '>'
  437. /// 'class' ...[opt] identifier[opt]
  438. /// 'template' '<' template-parameter-list '>' 'class' identifier[opt]
  439. /// = id-expression
  440. Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
  441. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  442. if (isStartOfTemplateTypeParameter())
  443. return ParseTypeParameter(Depth, Position);
  444. if (Tok.is(tok::kw_template))
  445. return ParseTemplateTemplateParameter(Depth, Position);
  446. // If it's none of the above, then it must be a parameter declaration.
  447. // NOTE: This will pick up errors in the closure of the template parameter
  448. // list (e.g., template < ; Check here to implement >> style closures.
  449. return ParseNonTypeTemplateParameter(Depth, Position);
  450. }
  451. /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
  452. /// Other kinds of template parameters are parsed in
  453. /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
  454. ///
  455. /// type-parameter: [C++ temp.param]
  456. /// 'class' ...[opt][C++0x] identifier[opt]
  457. /// 'class' identifier[opt] '=' type-id
  458. /// 'typename' ...[opt][C++0x] identifier[opt]
  459. /// 'typename' identifier[opt] '=' type-id
  460. Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
  461. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  462. assert(Tok.isOneOf(tok::kw_class, tok::kw_typename) &&
  463. "A type-parameter starts with 'class' or 'typename'");
  464. // Consume the 'class' or 'typename' keyword.
  465. bool TypenameKeyword = Tok.is(tok::kw_typename);
  466. SourceLocation KeyLoc = ConsumeToken();
  467. // Grab the ellipsis (if given).
  468. SourceLocation EllipsisLoc;
  469. if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
  470. Diag(EllipsisLoc,
  471. getLangOpts().CPlusPlus11
  472. ? diag::warn_cxx98_compat_variadic_templates
  473. : diag::ext_variadic_templates);
  474. }
  475. // Grab the template parameter name (if given)
  476. SourceLocation NameLoc;
  477. IdentifierInfo *ParamName = nullptr;
  478. if (Tok.is(tok::identifier)) {
  479. ParamName = Tok.getIdentifierInfo();
  480. NameLoc = ConsumeToken();
  481. } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
  482. tok::greatergreater)) {
  483. // Unnamed template parameter. Don't have to do anything here, just
  484. // don't consume this token.
  485. } else {
  486. Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
  487. return nullptr;
  488. }
  489. // Recover from misplaced ellipsis.
  490. bool AlreadyHasEllipsis = EllipsisLoc.isValid();
  491. if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
  492. DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
  493. // Grab a default argument (if available).
  494. // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
  495. // we introduce the type parameter into the local scope.
  496. SourceLocation EqualLoc;
  497. ParsedType DefaultArg;
  498. if (TryConsumeToken(tok::equal, EqualLoc))
  499. DefaultArg = ParseTypeName(/*Range=*/nullptr,
  500. Declarator::TemplateTypeArgContext).get();
  501. return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, EllipsisLoc,
  502. KeyLoc, ParamName, NameLoc, Depth, Position,
  503. EqualLoc, DefaultArg);
  504. }
  505. /// ParseTemplateTemplateParameter - Handle the parsing of template
  506. /// template parameters.
  507. ///
  508. /// type-parameter: [C++ temp.param]
  509. /// 'template' '<' template-parameter-list '>' type-parameter-key
  510. /// ...[opt] identifier[opt]
  511. /// 'template' '<' template-parameter-list '>' type-parameter-key
  512. /// identifier[opt] = id-expression
  513. /// type-parameter-key:
  514. /// 'class'
  515. /// 'typename' [C++1z]
  516. Decl *
  517. Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
  518. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  519. assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
  520. // Handle the template <...> part.
  521. SourceLocation TemplateLoc = ConsumeToken();
  522. SmallVector<Decl*,8> TemplateParams;
  523. SourceLocation LAngleLoc, RAngleLoc;
  524. {
  525. ParseScope TemplateParmScope(this, Scope::TemplateParamScope);
  526. if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc,
  527. RAngleLoc)) {
  528. return nullptr;
  529. }
  530. }
  531. // Provide an ExtWarn if the C++1z feature of using 'typename' here is used.
  532. // Generate a meaningful error if the user forgot to put class before the
  533. // identifier, comma, or greater. Provide a fixit if the identifier, comma,
  534. // or greater appear immediately or after 'struct'. In the latter case,
  535. // replace the keyword with 'class'.
  536. if (!TryConsumeToken(tok::kw_class)) {
  537. bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct);
  538. const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok;
  539. if (Tok.is(tok::kw_typename)) {
  540. Diag(Tok.getLocation(),
  541. getLangOpts().CPlusPlus1z
  542. ? diag::warn_cxx14_compat_template_template_param_typename
  543. : diag::ext_template_template_param_typename)
  544. << (!getLangOpts().CPlusPlus1z
  545. ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
  546. : FixItHint());
  547. } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater,
  548. tok::greatergreater, tok::ellipsis)) {
  549. Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
  550. << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
  551. : FixItHint::CreateInsertion(Tok.getLocation(), "class "));
  552. } else
  553. Diag(Tok.getLocation(), diag::err_class_on_template_template_param);
  554. if (Replace)
  555. ConsumeToken();
  556. }
  557. // Parse the ellipsis, if given.
  558. SourceLocation EllipsisLoc;
  559. if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
  560. Diag(EllipsisLoc,
  561. getLangOpts().CPlusPlus11
  562. ? diag::warn_cxx98_compat_variadic_templates
  563. : diag::ext_variadic_templates);
  564. // Get the identifier, if given.
  565. SourceLocation NameLoc;
  566. IdentifierInfo *ParamName = nullptr;
  567. if (Tok.is(tok::identifier)) {
  568. ParamName = Tok.getIdentifierInfo();
  569. NameLoc = ConsumeToken();
  570. } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
  571. tok::greatergreater)) {
  572. // Unnamed template parameter. Don't have to do anything here, just
  573. // don't consume this token.
  574. } else {
  575. Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
  576. return nullptr;
  577. }
  578. // Recover from misplaced ellipsis.
  579. bool AlreadyHasEllipsis = EllipsisLoc.isValid();
  580. if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
  581. DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
  582. TemplateParameterList *ParamList =
  583. Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
  584. TemplateLoc, LAngleLoc,
  585. TemplateParams.data(),
  586. TemplateParams.size(),
  587. RAngleLoc);
  588. // Grab a default argument (if available).
  589. // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
  590. // we introduce the template parameter into the local scope.
  591. SourceLocation EqualLoc;
  592. ParsedTemplateArgument DefaultArg;
  593. if (TryConsumeToken(tok::equal, EqualLoc)) {
  594. DefaultArg = ParseTemplateTemplateArgument();
  595. if (DefaultArg.isInvalid()) {
  596. Diag(Tok.getLocation(),
  597. diag::err_default_template_template_parameter_not_template);
  598. SkipUntil(tok::comma, tok::greater, tok::greatergreater,
  599. StopAtSemi | StopBeforeMatch);
  600. }
  601. }
  602. return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
  603. ParamList, EllipsisLoc,
  604. ParamName, NameLoc, Depth,
  605. Position, EqualLoc, DefaultArg);
  606. }
  607. /// ParseNonTypeTemplateParameter - Handle the parsing of non-type
  608. /// template parameters (e.g., in "template<int Size> class array;").
  609. ///
  610. /// template-parameter:
  611. /// ...
  612. /// parameter-declaration
  613. Decl *
  614. Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
  615. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  616. // Parse the declaration-specifiers (i.e., the type).
  617. // FIXME: The type should probably be restricted in some way... Not all
  618. // declarators (parts of declarators?) are accepted for parameters.
  619. DeclSpec DS(AttrFactory);
  620. ParseDeclarationSpecifiers(DS);
  621. // Parse this as a typename.
  622. Declarator ParamDecl(DS, Declarator::TemplateParamContext);
  623. ParseDeclarator(ParamDecl);
  624. if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
  625. Diag(Tok.getLocation(), diag::err_expected_template_parameter);
  626. return nullptr;
  627. }
  628. // Recover from misplaced ellipsis.
  629. SourceLocation EllipsisLoc;
  630. if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
  631. DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl);
  632. // If there is a default value, parse it.
  633. // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
  634. // we introduce the template parameter into the local scope.
  635. SourceLocation EqualLoc;
  636. ExprResult DefaultArg;
  637. if (TryConsumeToken(tok::equal, EqualLoc)) {
  638. // C++ [temp.param]p15:
  639. // When parsing a default template-argument for a non-type
  640. // template-parameter, the first non-nested > is taken as the
  641. // end of the template-parameter-list rather than a greater-than
  642. // operator.
  643. GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
  644. EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
  645. DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
  646. if (DefaultArg.isInvalid())
  647. SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
  648. }
  649. // Create the parameter.
  650. return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
  651. Depth, Position, EqualLoc,
  652. DefaultArg.get());
  653. }
  654. void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,
  655. SourceLocation CorrectLoc,
  656. bool AlreadyHasEllipsis,
  657. bool IdentifierHasName) {
  658. FixItHint Insertion;
  659. if (!AlreadyHasEllipsis)
  660. Insertion = FixItHint::CreateInsertion(CorrectLoc, "...");
  661. Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
  662. << FixItHint::CreateRemoval(EllipsisLoc) << Insertion
  663. << !IdentifierHasName;
  664. }
  665. void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,
  666. Declarator &D) {
  667. assert(EllipsisLoc.isValid());
  668. bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid();
  669. if (!AlreadyHasEllipsis)
  670. D.setEllipsisLoc(EllipsisLoc);
  671. DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(),
  672. AlreadyHasEllipsis, D.hasName());
  673. }
  674. /// \brief Parses a '>' at the end of a template list.
  675. ///
  676. /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries
  677. /// to determine if these tokens were supposed to be a '>' followed by
  678. /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary.
  679. ///
  680. /// \param RAngleLoc the location of the consumed '>'.
  681. ///
  682. /// \param ConsumeLastToken if true, the '>' is consumed.
  683. ///
  684. /// \param ObjCGenericList if true, this is the '>' closing an Objective-C
  685. /// type parameter or type argument list, rather than a C++ template parameter
  686. /// or argument list.
  687. ///
  688. /// \returns true, if current token does not start with '>', false otherwise.
  689. bool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc,
  690. bool ConsumeLastToken,
  691. bool ObjCGenericList) {
  692. // What will be left once we've consumed the '>'.
  693. tok::TokenKind RemainingToken;
  694. const char *ReplacementStr = "> >";
  695. switch (Tok.getKind()) {
  696. default:
  697. Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
  698. return true;
  699. case tok::greater:
  700. // Determine the location of the '>' token. Only consume this token
  701. // if the caller asked us to.
  702. RAngleLoc = Tok.getLocation();
  703. if (ConsumeLastToken)
  704. ConsumeToken();
  705. return false;
  706. case tok::greatergreater:
  707. RemainingToken = tok::greater;
  708. break;
  709. case tok::greatergreatergreater:
  710. RemainingToken = tok::greatergreater;
  711. break;
  712. case tok::greaterequal:
  713. RemainingToken = tok::equal;
  714. ReplacementStr = "> =";
  715. break;
  716. case tok::greatergreaterequal:
  717. RemainingToken = tok::greaterequal;
  718. break;
  719. }
  720. // This template-id is terminated by a token which starts with a '>'. Outside
  721. // C++11, this is now error recovery, and in C++11, this is error recovery if
  722. // the token isn't '>>' or '>>>'.
  723. // '>>>' is for CUDA, where this sequence of characters is parsed into
  724. // tok::greatergreatergreater, rather than two separate tokens.
  725. //
  726. // We always allow this for Objective-C type parameter and type argument
  727. // lists.
  728. RAngleLoc = Tok.getLocation();
  729. Token Next = NextToken();
  730. if (!ObjCGenericList) {
  731. // The source range of the '>>' or '>=' at the start of the token.
  732. CharSourceRange ReplacementRange =
  733. CharSourceRange::getCharRange(RAngleLoc,
  734. Lexer::AdvanceToTokenCharacter(RAngleLoc, 2, PP.getSourceManager(),
  735. getLangOpts()));
  736. // A hint to put a space between the '>>'s. In order to make the hint as
  737. // clear as possible, we include the characters either side of the space in
  738. // the replacement, rather than just inserting a space at SecondCharLoc.
  739. FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange,
  740. ReplacementStr);
  741. // A hint to put another space after the token, if it would otherwise be
  742. // lexed differently.
  743. FixItHint Hint2;
  744. if ((RemainingToken == tok::greater ||
  745. RemainingToken == tok::greatergreater) &&
  746. (Next.isOneOf(tok::greater, tok::greatergreater,
  747. tok::greatergreatergreater, tok::equal,
  748. tok::greaterequal, tok::greatergreaterequal,
  749. tok::equalequal)) &&
  750. areTokensAdjacent(Tok, Next))
  751. Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");
  752. unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
  753. if (getLangOpts().CPlusPlus11 &&
  754. (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
  755. DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
  756. else if (Tok.is(tok::greaterequal))
  757. DiagId = diag::err_right_angle_bracket_equal_needs_space;
  758. Diag(Tok.getLocation(), DiagId) << Hint1 << Hint2;
  759. }
  760. // Strip the initial '>' from the token.
  761. if (RemainingToken == tok::equal && Next.is(tok::equal) &&
  762. areTokensAdjacent(Tok, Next)) {
  763. // Join two adjacent '=' tokens into one, for cases like:
  764. // void (*p)() = f<int>;
  765. // return f<int>==p;
  766. ConsumeToken();
  767. Tok.setKind(tok::equalequal);
  768. Tok.setLength(Tok.getLength() + 1);
  769. } else {
  770. Tok.setKind(RemainingToken);
  771. Tok.setLength(Tok.getLength() - 1);
  772. }
  773. Tok.setLocation(Lexer::AdvanceToTokenCharacter(RAngleLoc, 1,
  774. PP.getSourceManager(),
  775. getLangOpts()));
  776. if (!ConsumeLastToken) {
  777. // Since we're not supposed to consume the '>' token, we need to push
  778. // this token and revert the current token back to the '>'.
  779. PP.EnterToken(Tok);
  780. Tok.setKind(tok::greater);
  781. Tok.setLength(1);
  782. Tok.setLocation(RAngleLoc);
  783. }
  784. return false;
  785. }
  786. /// \brief Parses a template-id that after the template name has
  787. /// already been parsed.
  788. ///
  789. /// This routine takes care of parsing the enclosed template argument
  790. /// list ('<' template-parameter-list [opt] '>') and placing the
  791. /// results into a form that can be transferred to semantic analysis.
  792. ///
  793. /// \param Template the template declaration produced by isTemplateName
  794. ///
  795. /// \param TemplateNameLoc the source location of the template name
  796. ///
  797. /// \param SS if non-NULL, the nested-name-specifier preceding the
  798. /// template name.
  799. ///
  800. /// \param ConsumeLastToken if true, then we will consume the last
  801. /// token that forms the template-id. Otherwise, we will leave the
  802. /// last token in the stream (e.g., so that it can be replaced with an
  803. /// annotation token).
  804. bool
  805. Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template,
  806. SourceLocation TemplateNameLoc,
  807. const CXXScopeSpec &SS,
  808. bool ConsumeLastToken,
  809. SourceLocation &LAngleLoc,
  810. TemplateArgList &TemplateArgs,
  811. SourceLocation &RAngleLoc) {
  812. assert(Tok.is(tok::less) && "Must have already parsed the template-name");
  813. // Consume the '<'.
  814. LAngleLoc = ConsumeToken();
  815. // Parse the optional template-argument-list.
  816. bool Invalid = false;
  817. {
  818. GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
  819. if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater))
  820. Invalid = ParseTemplateArgumentList(TemplateArgs);
  821. if (Invalid) {
  822. // Try to find the closing '>'.
  823. if (ConsumeLastToken)
  824. SkipUntil(tok::greater, StopAtSemi);
  825. else
  826. SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch);
  827. return true;
  828. }
  829. }
  830. return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken,
  831. /*ObjCGenericList=*/false);
  832. }
  833. /// \brief Replace the tokens that form a simple-template-id with an
  834. /// annotation token containing the complete template-id.
  835. ///
  836. /// The first token in the stream must be the name of a template that
  837. /// is followed by a '<'. This routine will parse the complete
  838. /// simple-template-id and replace the tokens with a single annotation
  839. /// token with one of two different kinds: if the template-id names a
  840. /// type (and \p AllowTypeAnnotation is true), the annotation token is
  841. /// a type annotation that includes the optional nested-name-specifier
  842. /// (\p SS). Otherwise, the annotation token is a template-id
  843. /// annotation that does not include the optional
  844. /// nested-name-specifier.
  845. ///
  846. /// \param Template the declaration of the template named by the first
  847. /// token (an identifier), as returned from \c Action::isTemplateName().
  848. ///
  849. /// \param TNK the kind of template that \p Template
  850. /// refers to, as returned from \c Action::isTemplateName().
  851. ///
  852. /// \param SS if non-NULL, the nested-name-specifier that precedes
  853. /// this template name.
  854. ///
  855. /// \param TemplateKWLoc if valid, specifies that this template-id
  856. /// annotation was preceded by the 'template' keyword and gives the
  857. /// location of that keyword. If invalid (the default), then this
  858. /// template-id was not preceded by a 'template' keyword.
  859. ///
  860. /// \param AllowTypeAnnotation if true (the default), then a
  861. /// simple-template-id that refers to a class template, template
  862. /// template parameter, or other template that produces a type will be
  863. /// replaced with a type annotation token. Otherwise, the
  864. /// simple-template-id is always replaced with a template-id
  865. /// annotation token.
  866. ///
  867. /// If an unrecoverable parse error occurs and no annotation token can be
  868. /// formed, this function returns true.
  869. ///
  870. bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
  871. CXXScopeSpec &SS,
  872. SourceLocation TemplateKWLoc,
  873. UnqualifiedId &TemplateName,
  874. bool AllowTypeAnnotation) {
  875. assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++");
  876. assert(Template && (Tok.is(tok::less) || getLangOpts().HLSL) && // HLSL Change
  877. "Parser isn't at the beginning of a template-id");
  878. // Consume the template-name.
  879. SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
  880. // Parse the enclosed template argument list.
  881. SourceLocation LAngleLoc, RAngleLoc;
  882. TemplateArgList TemplateArgs;
  883. // HLSL Change Starts - allow template names without '<>'
  884. bool Invalid;
  885. bool ShouldParse = Tok.is(tok::less) || !getLangOpts().HLSL;
  886. if (ShouldParse) { // HLSL Change - make following conditional
  887. Invalid = ParseTemplateIdAfterTemplateName(Template,
  888. TemplateNameLoc,
  889. SS, false, LAngleLoc,
  890. TemplateArgs,
  891. RAngleLoc);
  892. }
  893. else {
  894. Invalid = false;
  895. RAngleLoc = LAngleLoc = Tok.getLocation();
  896. }
  897. // HLSL Change Ends
  898. if (Invalid) {
  899. // If we failed to parse the template ID but skipped ahead to a >, we're not
  900. // going to be able to form a token annotation. Eat the '>' if present.
  901. TryConsumeToken(tok::greater);
  902. return true;
  903. }
  904. ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
  905. // Build the annotation token.
  906. if (TNK == TNK_Type_template && AllowTypeAnnotation) {
  907. TypeResult Type
  908. = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
  909. Template, TemplateNameLoc,
  910. LAngleLoc, TemplateArgsPtr, RAngleLoc);
  911. if (Type.isInvalid()) {
  912. // If we failed to parse the template ID but skipped ahead to a >, we're not
  913. // going to be able to form a token annotation. Eat the '>' if present.
  914. TryConsumeToken(tok::greater);
  915. return true;
  916. }
  917. Tok.setKind(tok::annot_typename);
  918. setTypeAnnotation(Tok, Type.get());
  919. if (SS.isNotEmpty())
  920. Tok.setLocation(SS.getBeginLoc());
  921. else if (TemplateKWLoc.isValid())
  922. Tok.setLocation(TemplateKWLoc);
  923. else
  924. Tok.setLocation(TemplateNameLoc);
  925. } else {
  926. // Build a template-id annotation token that can be processed
  927. // later.
  928. Tok.setKind(tok::annot_template_id);
  929. TemplateIdAnnotation *TemplateId
  930. = TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
  931. TemplateId->TemplateNameLoc = TemplateNameLoc;
  932. if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) {
  933. TemplateId->Name = TemplateName.Identifier;
  934. TemplateId->Operator = OO_None;
  935. } else {
  936. TemplateId->Name = nullptr;
  937. TemplateId->Operator = TemplateName.OperatorFunctionId.Operator;
  938. }
  939. TemplateId->SS = SS;
  940. TemplateId->TemplateKWLoc = TemplateKWLoc;
  941. TemplateId->Template = Template;
  942. TemplateId->Kind = TNK;
  943. TemplateId->LAngleLoc = LAngleLoc;
  944. TemplateId->RAngleLoc = RAngleLoc;
  945. ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
  946. for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg)
  947. Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]);
  948. Tok.setAnnotationValue(TemplateId);
  949. if (TemplateKWLoc.isValid())
  950. Tok.setLocation(TemplateKWLoc);
  951. else
  952. Tok.setLocation(TemplateNameLoc);
  953. }
  954. // Common fields for the annotation token
  955. Tok.setAnnotationEndLoc(RAngleLoc);
  956. // In case the tokens were cached, have Preprocessor replace them with the
  957. // annotation token.
  958. PP.AnnotateCachedTokens(Tok);
  959. return false;
  960. }
  961. /// \brief Replaces a template-id annotation token with a type
  962. /// annotation token.
  963. ///
  964. /// If there was a failure when forming the type from the template-id,
  965. /// a type annotation token will still be created, but will have a
  966. /// NULL type pointer to signify an error.
  967. void Parser::AnnotateTemplateIdTokenAsType() {
  968. assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
  969. TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
  970. assert((TemplateId->Kind == TNK_Type_template ||
  971. TemplateId->Kind == TNK_Dependent_template_name) &&
  972. "Only works for type and dependent templates");
  973. ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
  974. TemplateId->NumArgs);
  975. TypeResult Type
  976. = Actions.ActOnTemplateIdType(TemplateId->SS,
  977. TemplateId->TemplateKWLoc,
  978. TemplateId->Template,
  979. TemplateId->TemplateNameLoc,
  980. TemplateId->LAngleLoc,
  981. TemplateArgsPtr,
  982. TemplateId->RAngleLoc);
  983. // Create the new "type" annotation token.
  984. Tok.setKind(tok::annot_typename);
  985. setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get());
  986. if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name.
  987. Tok.setLocation(TemplateId->SS.getBeginLoc());
  988. // End location stays the same
  989. // Replace the template-id annotation token, and possible the scope-specifier
  990. // that precedes it, with the typename annotation token.
  991. PP.AnnotateCachedTokens(Tok);
  992. }
  993. /// \brief Determine whether the given token can end a template argument.
  994. static bool isEndOfTemplateArgument(Token Tok) {
  995. return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater);
  996. }
  997. /// \brief Parse a C++ template template argument.
  998. ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
  999. if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
  1000. !Tok.is(tok::annot_cxxscope))
  1001. return ParsedTemplateArgument();
  1002. // C++0x [temp.arg.template]p1:
  1003. // A template-argument for a template template-parameter shall be the name
  1004. // of a class template or an alias template, expressed as id-expression.
  1005. //
  1006. // We parse an id-expression that refers to a class template or alias
  1007. // template. The grammar we parse is:
  1008. //
  1009. // nested-name-specifier[opt] template[opt] identifier ...[opt]
  1010. //
  1011. // followed by a token that terminates a template argument, such as ',',
  1012. // '>', or (in some cases) '>>'.
  1013. CXXScopeSpec SS; // nested-name-specifier, if present
  1014. ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
  1015. /*EnteringContext=*/false);
  1016. ParsedTemplateArgument Result;
  1017. SourceLocation EllipsisLoc;
  1018. if (SS.isSet() && Tok.is(tok::kw_template)) {
  1019. // HLSL Change Starts
  1020. if (getLangOpts().HLSL) {
  1021. Diag(Tok, diag::err_hlsl_unsupported_construct) << "template template argument";
  1022. return Result;
  1023. }
  1024. // HLSL Change Ends
  1025. // Parse the optional 'template' keyword following the
  1026. // nested-name-specifier.
  1027. SourceLocation TemplateKWLoc = ConsumeToken();
  1028. if (Tok.is(tok::identifier)) {
  1029. // We appear to have a dependent template name.
  1030. UnqualifiedId Name;
  1031. Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
  1032. ConsumeToken(); // the identifier
  1033. TryConsumeToken(tok::ellipsis, EllipsisLoc);
  1034. // If the next token signals the end of a template argument,
  1035. // then we have a dependent template name that could be a template
  1036. // template argument.
  1037. TemplateTy Template;
  1038. if (isEndOfTemplateArgument(Tok) &&
  1039. Actions.ActOnDependentTemplateName(getCurScope(),
  1040. SS, TemplateKWLoc, Name,
  1041. /*ObjectType=*/ ParsedType(),
  1042. /*EnteringContext=*/false,
  1043. Template))
  1044. Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
  1045. }
  1046. } else if (Tok.is(tok::identifier)) {
  1047. // We may have a (non-dependent) template name.
  1048. TemplateTy Template;
  1049. UnqualifiedId Name;
  1050. Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
  1051. ConsumeToken(); // the identifier
  1052. TryConsumeToken(tok::ellipsis, EllipsisLoc);
  1053. if (isEndOfTemplateArgument(Tok)) {
  1054. bool MemberOfUnknownSpecialization;
  1055. TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
  1056. /*hasTemplateKeyword=*/false,
  1057. Name,
  1058. /*ObjectType=*/ ParsedType(),
  1059. /*EnteringContext=*/false,
  1060. Template,
  1061. MemberOfUnknownSpecialization);
  1062. if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
  1063. // We have an id-expression that refers to a class template or
  1064. // (C++0x) alias template.
  1065. Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
  1066. }
  1067. }
  1068. }
  1069. // If this is a pack expansion, build it as such.
  1070. if (EllipsisLoc.isValid() && !Result.isInvalid())
  1071. Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
  1072. return Result;
  1073. }
  1074. /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
  1075. ///
  1076. /// template-argument: [C++ 14.2]
  1077. /// constant-expression
  1078. /// type-id
  1079. /// id-expression
  1080. ParsedTemplateArgument Parser::ParseTemplateArgument() {
  1081. // C++ [temp.arg]p2:
  1082. // In a template-argument, an ambiguity between a type-id and an
  1083. // expression is resolved to a type-id, regardless of the form of
  1084. // the corresponding template-parameter.
  1085. //
  1086. // Therefore, we initially try to parse a type-id.
  1087. if (isCXXTypeId(TypeIdAsTemplateArgument)) {
  1088. SourceLocation Loc = Tok.getLocation();
  1089. TypeResult TypeArg = ParseTypeName(/*Range=*/nullptr,
  1090. Declarator::TemplateTypeArgContext);
  1091. if (TypeArg.isInvalid())
  1092. return ParsedTemplateArgument();
  1093. return ParsedTemplateArgument(ParsedTemplateArgument::Type,
  1094. TypeArg.get().getAsOpaquePtr(),
  1095. Loc);
  1096. }
  1097. // Try to parse a template template argument.
  1098. if (!getLangOpts().HLSL) // HLSL Change - HLSL does not support template template arguments
  1099. {
  1100. TentativeParsingAction TPA(*this);
  1101. ParsedTemplateArgument TemplateTemplateArgument
  1102. = ParseTemplateTemplateArgument();
  1103. if (!TemplateTemplateArgument.isInvalid()) {
  1104. TPA.Commit();
  1105. return TemplateTemplateArgument;
  1106. }
  1107. // Revert this tentative parse to parse a non-type template argument.
  1108. TPA.Revert();
  1109. }
  1110. // Parse a non-type template argument.
  1111. SourceLocation Loc = Tok.getLocation();
  1112. ExprResult ExprArg = ParseConstantExpression(MaybeTypeCast);
  1113. if (ExprArg.isInvalid() || !ExprArg.get())
  1114. return ParsedTemplateArgument();
  1115. return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
  1116. ExprArg.get(), Loc);
  1117. }
  1118. /// \brief Determine whether the current tokens can only be parsed as a
  1119. /// template argument list (starting with the '<') and never as a '<'
  1120. /// expression.
  1121. bool Parser::IsTemplateArgumentList(unsigned Skip) {
  1122. struct AlwaysRevertAction : TentativeParsingAction {
  1123. AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { }
  1124. ~AlwaysRevertAction() { Revert(); }
  1125. } Tentative(*this);
  1126. while (Skip) {
  1127. ConsumeToken();
  1128. --Skip;
  1129. }
  1130. // '<'
  1131. if (!TryConsumeToken(tok::less))
  1132. return false;
  1133. // An empty template argument list.
  1134. if (Tok.is(tok::greater))
  1135. return true;
  1136. // See whether we have declaration specifiers, which indicate a type.
  1137. while (isCXXDeclarationSpecifier() == TPResult::True)
  1138. ConsumeToken();
  1139. // If we have a '>' or a ',' then this is a template argument list.
  1140. return Tok.isOneOf(tok::greater, tok::comma);
  1141. }
  1142. /// ParseTemplateArgumentList - Parse a C++ template-argument-list
  1143. /// (C++ [temp.names]). Returns true if there was an error.
  1144. ///
  1145. /// template-argument-list: [C++ 14.2]
  1146. /// template-argument
  1147. /// template-argument-list ',' template-argument
  1148. bool
  1149. Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
  1150. // Template argument lists are constant-evaluation contexts.
  1151. EnterExpressionEvaluationContext EvalContext(Actions,Sema::ConstantEvaluated);
  1152. ColonProtectionRAIIObject ColonProtection(*this, false);
  1153. do {
  1154. ParsedTemplateArgument Arg = ParseTemplateArgument();
  1155. SourceLocation EllipsisLoc;
  1156. if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
  1157. { // HLSL Change Starts
  1158. if (getLangOpts().HLSL) {
  1159. Diag(EllipsisLoc, diag::err_hlsl_unsupported_construct) << "ellipsis";
  1160. SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
  1161. return true;
  1162. }
  1163. // HLSL Change Ends
  1164. Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
  1165. } // HLSL Change - end conditional block
  1166. if (Arg.isInvalid()) {
  1167. SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
  1168. return true;
  1169. }
  1170. // Save this template argument.
  1171. TemplateArgs.push_back(Arg);
  1172. // If the next token is a comma, consume it and keep reading
  1173. // arguments.
  1174. } while (TryConsumeToken(tok::comma));
  1175. return false;
  1176. }
  1177. /// \brief Parse a C++ explicit template instantiation
  1178. /// (C++ [temp.explicit]).
  1179. ///
  1180. /// explicit-instantiation:
  1181. /// 'extern' [opt] 'template' declaration
  1182. ///
  1183. /// Note that the 'extern' is a GNU extension and C++11 feature.
  1184. Decl *Parser::ParseExplicitInstantiation(unsigned Context,
  1185. SourceLocation ExternLoc,
  1186. SourceLocation TemplateLoc,
  1187. SourceLocation &DeclEnd,
  1188. AccessSpecifier AS) {
  1189. // This isn't really required here.
  1190. ParsingDeclRAIIObject
  1191. ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
  1192. return ParseSingleDeclarationAfterTemplate(Context,
  1193. ParsedTemplateInfo(ExternLoc,
  1194. TemplateLoc),
  1195. ParsingTemplateParams,
  1196. DeclEnd, AS);
  1197. }
  1198. SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
  1199. if (TemplateParams)
  1200. return getTemplateParamsRange(TemplateParams->data(),
  1201. TemplateParams->size());
  1202. SourceRange R(TemplateLoc);
  1203. if (ExternLoc.isValid())
  1204. R.setBegin(ExternLoc);
  1205. return R;
  1206. }
  1207. void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) {
  1208. ((Parser *)P)->ParseLateTemplatedFuncDef(LPT);
  1209. }
  1210. /// \brief Late parse a C++ function template in Microsoft mode.
  1211. void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
  1212. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  1213. if (!LPT.D)
  1214. return;
  1215. // Get the FunctionDecl.
  1216. FunctionDecl *FunD = LPT.D->getAsFunction();
  1217. // Track template parameter depth.
  1218. TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
  1219. // To restore the context after late parsing.
  1220. Sema::ContextRAII GlobalSavedContext(
  1221. Actions, Actions.Context.getTranslationUnitDecl());
  1222. SmallVector<ParseScope*, 4> TemplateParamScopeStack;
  1223. // Get the list of DeclContexts to reenter.
  1224. SmallVector<DeclContext*, 4> DeclContextsToReenter;
  1225. DeclContext *DD = FunD;
  1226. while (DD && !DD->isTranslationUnit()) {
  1227. DeclContextsToReenter.push_back(DD);
  1228. DD = DD->getLexicalParent();
  1229. }
  1230. // Reenter template scopes from outermost to innermost.
  1231. SmallVectorImpl<DeclContext *>::reverse_iterator II =
  1232. DeclContextsToReenter.rbegin();
  1233. for (; II != DeclContextsToReenter.rend(); ++II) {
  1234. TemplateParamScopeStack.push_back(new ParseScope(this,
  1235. Scope::TemplateParamScope));
  1236. unsigned NumParamLists =
  1237. Actions.ActOnReenterTemplateScope(getCurScope(), cast<Decl>(*II));
  1238. CurTemplateDepthTracker.addDepth(NumParamLists);
  1239. if (*II != FunD) {
  1240. TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope));
  1241. Actions.PushDeclContext(Actions.getCurScope(), *II);
  1242. }
  1243. }
  1244. assert(!LPT.Toks.empty() && "Empty body!");
  1245. // Append the current token at the end of the new token stream so that it
  1246. // doesn't get lost.
  1247. LPT.Toks.push_back(Tok);
  1248. PP.EnterTokenStream(LPT.Toks.data(), LPT.Toks.size(), true, false);
  1249. // Consume the previously pushed token.
  1250. ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
  1251. assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) &&
  1252. "Inline method not starting with '{', ':' or 'try'");
  1253. // Parse the method body. Function body parsing code is similar enough
  1254. // to be re-used for method bodies as well.
  1255. ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
  1256. // Recreate the containing function DeclContext.
  1257. Sema::ContextRAII FunctionSavedContext(Actions,
  1258. Actions.getContainingDC(FunD));
  1259. Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
  1260. if (Tok.is(tok::kw_try)) {
  1261. ParseFunctionTryBlock(LPT.D, FnScope);
  1262. } else {
  1263. if (Tok.is(tok::colon))
  1264. ParseConstructorInitializer(LPT.D);
  1265. else
  1266. Actions.ActOnDefaultCtorInitializers(LPT.D);
  1267. if (Tok.is(tok::l_brace)) {
  1268. assert((!isa<FunctionTemplateDecl>(LPT.D) ||
  1269. cast<FunctionTemplateDecl>(LPT.D)
  1270. ->getTemplateParameters()
  1271. ->getDepth() == TemplateParameterDepth - 1) &&
  1272. "TemplateParameterDepth should be greater than the depth of "
  1273. "current template being instantiated!");
  1274. ParseFunctionStatementBody(LPT.D, FnScope);
  1275. Actions.UnmarkAsLateParsedTemplate(FunD);
  1276. } else
  1277. Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
  1278. }
  1279. // Exit scopes.
  1280. FnScope.Exit();
  1281. SmallVectorImpl<ParseScope *>::reverse_iterator I =
  1282. TemplateParamScopeStack.rbegin();
  1283. for (; I != TemplateParamScopeStack.rend(); ++I)
  1284. delete *I;
  1285. }
  1286. /// \brief Lex a delayed template function for late parsing.
  1287. void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
  1288. assert(!getLangOpts().HLSL && "no template parsing is supported in HLSL"); // HLSL Change
  1289. tok::TokenKind kind = Tok.getKind();
  1290. if (!ConsumeAndStoreFunctionPrologue(Toks)) {
  1291. // Consume everything up to (and including) the matching right brace.
  1292. ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
  1293. }
  1294. // If we're in a function-try-block, we need to store all the catch blocks.
  1295. if (kind == tok::kw_try) {
  1296. while (Tok.is(tok::kw_catch)) {
  1297. ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
  1298. ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
  1299. }
  1300. }
  1301. }