ParseTemplate.cpp 56 KB

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