Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1 | //===--- ParseTemplate.cpp - Template Parsing -----------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements parsing of C++ templates. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclTemplate.h" |
| 15 | #include "clang/Parse/ParseDiagnostic.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Parser.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 17 | #include "clang/Parse/RAIIObjectsForParser.h" |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 18 | #include "clang/Sema/DeclSpec.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
| 20 | #include "clang/Sema/Scope.h" |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/TimeProfiler.h" |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 24 | /// Parse a template declaration, explicit instantiation, or |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 25 | /// explicit specialization. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 26 | Decl *Parser::ParseDeclarationStartingWithTemplate( |
| 27 | DeclaratorContext Context, SourceLocation &DeclEnd, |
| 28 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 29 | ObjCDeclContextSwitch ObjCDC(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 30 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 31 | if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 32 | return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(), |
| 33 | DeclEnd, AccessAttrs, AS); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 34 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 35 | return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs, |
| 36 | AS); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 39 | /// Parse a template declaration or an explicit specialization. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 40 | /// |
| 41 | /// Template declarations include one or more template parameter lists |
| 42 | /// and either the function or class template declaration. Explicit |
| 43 | /// specializations contain one or more 'template < >' prefixes |
| 44 | /// followed by a (possibly templated) declaration. Since the |
| 45 | /// syntactic form of both features is nearly identical, we parse all |
| 46 | /// of the template headers together and let semantic analysis sort |
| 47 | /// the declarations from the explicit specializations. |
| 48 | /// |
| 49 | /// template-declaration: [C++ temp] |
| 50 | /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration |
| 51 | /// |
| 52 | /// explicit-specialization: [ C++ temp.expl.spec] |
| 53 | /// 'template' '<' '>' declaration |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 54 | Decl *Parser::ParseTemplateDeclarationOrSpecialization( |
| 55 | DeclaratorContext Context, SourceLocation &DeclEnd, |
| 56 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 57 | assert(Tok.isOneOf(tok::kw_export, tok::kw_template) && |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 58 | "Token does not start a template declaration."); |
| 59 | |
| 60 | // Enter template-parameter scope. |
| 61 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
| 62 | |
| 63 | // Tell the action that names should be checked in the context of |
| 64 | // the declaration to come. |
| 65 | ParsingDeclRAIIObject |
| 66 | ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); |
| 67 | |
| 68 | // Parse multiple levels of template headers within this template |
| 69 | // parameter scope, e.g., |
| 70 | // |
| 71 | // template<typename T> |
| 72 | // template<typename U> |
| 73 | // class A<T>::B { ... }; |
| 74 | // |
| 75 | // We parse multiple levels non-recursively so that we can build a |
| 76 | // single data structure containing all of the template parameter |
| 77 | // lists to easily differentiate between the case above and: |
| 78 | // |
| 79 | // template<typename T> |
| 80 | // class A { |
| 81 | // template<typename U> class B; |
| 82 | // }; |
| 83 | // |
| 84 | // In the first case, the action for declaring A<T>::B receives |
| 85 | // both template parameter lists. In the second case, the action for |
| 86 | // defining A<T>::B receives just the inner template parameter list |
| 87 | // (and retrieves the outer template parameter list from its |
| 88 | // context). |
| 89 | bool isSpecialization = true; |
| 90 | bool LastParamListWasEmpty = false; |
| 91 | TemplateParameterLists ParamLists; |
| 92 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 93 | |
| 94 | do { |
| 95 | // Consume the 'export', if any. |
| 96 | SourceLocation ExportLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 97 | TryConsumeToken(tok::kw_export, ExportLoc); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 98 | |
| 99 | // Consume the 'template', which should be here. |
| 100 | SourceLocation TemplateLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 101 | if (!TryConsumeToken(tok::kw_template, TemplateLoc)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 102 | Diag(Tok.getLocation(), diag::err_expected_template); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 103 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Parse the '<' template-parameter-list '>' |
| 107 | SourceLocation LAngleLoc, RAngleLoc; |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 108 | SmallVector<NamedDecl*, 4> TemplateParams; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 109 | if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(), |
| 110 | TemplateParams, LAngleLoc, RAngleLoc)) { |
Hubert Tong | ec3cb57 | 2015-06-25 00:23:39 +0000 | [diff] [blame] | 111 | // Skip until the semi-colon or a '}'. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 112 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 113 | TryConsumeToken(tok::semi); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 114 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 117 | ExprResult OptionalRequiresClauseConstraintER; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 118 | if (!TemplateParams.empty()) { |
| 119 | isSpecialization = false; |
| 120 | ++CurTemplateDepthTracker; |
Hubert Tong | ec3cb57 | 2015-06-25 00:23:39 +0000 | [diff] [blame] | 121 | |
| 122 | if (TryConsumeToken(tok::kw_requires)) { |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 123 | OptionalRequiresClauseConstraintER = |
Hubert Tong | ec3cb57 | 2015-06-25 00:23:39 +0000 | [diff] [blame] | 124 | Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression()); |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 125 | if (!OptionalRequiresClauseConstraintER.isUsable()) { |
Hubert Tong | ec3cb57 | 2015-06-25 00:23:39 +0000 | [diff] [blame] | 126 | // Skip until the semi-colon or a '}'. |
| 127 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 128 | TryConsumeToken(tok::semi); |
| 129 | return nullptr; |
| 130 | } |
| 131 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 132 | } else { |
| 133 | LastParamListWasEmpty = true; |
| 134 | } |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 135 | |
| 136 | ParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 137 | CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc, |
| 138 | TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get())); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 139 | } while (Tok.isOneOf(tok::kw_export, tok::kw_template)); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 140 | |
Akira Hatanaka | 10aced8 | 2016-04-29 02:24:14 +0000 | [diff] [blame] | 141 | unsigned NewFlags = getCurScope()->getFlags() & ~Scope::TemplateParamScope; |
| 142 | ParseScopeFlags TemplateScopeFlags(this, NewFlags, isSpecialization); |
| 143 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 144 | // Parse the actual template declaration. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 145 | return ParseSingleDeclarationAfterTemplate( |
| 146 | Context, |
| 147 | ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty), |
| 148 | ParsingTemplateParams, DeclEnd, AccessAttrs, AS); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 151 | /// Parse a single declaration that declares a template, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 152 | /// template specialization, or explicit instantiation of a template. |
| 153 | /// |
| 154 | /// \param DeclEnd will receive the source location of the last token |
| 155 | /// within this declaration. |
| 156 | /// |
| 157 | /// \param AS the access specifier associated with this |
| 158 | /// declaration. Will be AS_none for namespace-scope declarations. |
| 159 | /// |
| 160 | /// \returns the new declaration. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 161 | Decl *Parser::ParseSingleDeclarationAfterTemplate( |
| 162 | DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, |
| 163 | ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd, |
| 164 | ParsedAttributes &AccessAttrs, AccessSpecifier AS) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 165 | assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
| 166 | "Template information required"); |
| 167 | |
Aaron Ballman | e7c544d | 2014-08-04 20:28:35 +0000 | [diff] [blame] | 168 | if (Tok.is(tok::kw_static_assert)) { |
| 169 | // A static_assert declaration may not be templated. |
| 170 | Diag(Tok.getLocation(), diag::err_templated_invalid_declaration) |
| 171 | << TemplateInfo.getSourceRange(); |
| 172 | // Parse the static_assert declaration to improve error recovery. |
| 173 | return ParseStaticAssertDeclaration(DeclEnd); |
| 174 | } |
| 175 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 176 | if (Context == DeclaratorContext::MemberContext) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 177 | // We are parsing a member template. |
| 178 | ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo, |
| 179 | &DiagsFromTParams); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 180 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | ParsedAttributesWithRange prefixAttrs(AttrFactory); |
| 184 | MaybeParseCXX11Attributes(prefixAttrs); |
| 185 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 186 | if (Tok.is(tok::kw_using)) { |
Erik Verbruggen | 51ee12a | 2017-09-08 09:31:13 +0000 | [diff] [blame] | 187 | auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, |
| 188 | prefixAttrs); |
| 189 | if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl()) |
| 190 | return nullptr; |
| 191 | return usingDeclPtr.get().getSingleDecl(); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 192 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 193 | |
| 194 | // Parse the declaration specifiers, stealing any diagnostics from |
| 195 | // the template parameters. |
| 196 | ParsingDeclSpec DS(*this, &DiagsFromTParams); |
| 197 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 198 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 199 | getDeclSpecContextFromDeclaratorContext(Context)); |
| 200 | |
| 201 | if (Tok.is(tok::semi)) { |
| 202 | ProhibitAttributes(prefixAttrs); |
| 203 | DeclEnd = ConsumeToken(); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 204 | RecordDecl *AnonRecord = nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 205 | Decl *Decl = Actions.ParsedFreeStandingDeclSpec( |
| 206 | getCurScope(), AS, DS, |
| 207 | TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams |
| 208 | : MultiTemplateParamsArg(), |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 209 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation, |
| 210 | AnonRecord); |
| 211 | assert(!AnonRecord && |
| 212 | "Anonymous unions/structs should not be valid with template"); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 213 | DS.complete(Decl); |
| 214 | return Decl; |
| 215 | } |
| 216 | |
| 217 | // Move the attributes from the prefix into the DS. |
| 218 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 219 | ProhibitAttributes(prefixAttrs); |
| 220 | else |
| 221 | DS.takeAttributesFrom(prefixAttrs); |
| 222 | |
| 223 | // Parse the declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 224 | ParsingDeclarator DeclaratorInfo(*this, DS, (DeclaratorContext)Context); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 225 | ParseDeclarator(DeclaratorInfo); |
| 226 | // Error parsing the declarator? |
| 227 | if (!DeclaratorInfo.hasName()) { |
| 228 | // If so, skip until the semi-colon or a }. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 229 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 230 | if (Tok.is(tok::semi)) |
| 231 | ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 232 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 235 | llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() { |
| 236 | return DeclaratorInfo.getIdentifier() != nullptr |
| 237 | ? DeclaratorInfo.getIdentifier()->getName() |
| 238 | : "<unknown>"; |
| 239 | }); |
| 240 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 241 | LateParsedAttrList LateParsedAttrs(true); |
| 242 | if (DeclaratorInfo.isFunctionDeclarator()) |
| 243 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
| 244 | |
| 245 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 246 | isStartOfFunctionDefinition(DeclaratorInfo)) { |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 247 | |
| 248 | // Function definitions are only allowed at file scope and in C++ classes. |
| 249 | // The C++ inline method definition case is handled elsewhere, so we only |
| 250 | // need to handle the file scope definition case. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 251 | if (Context != DeclaratorContext::FileContext) { |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 252 | Diag(Tok, diag::err_function_definition_not_allowed); |
| 253 | SkipMalformedDecl(); |
| 254 | return nullptr; |
| 255 | } |
| 256 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 257 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 258 | // Recover by ignoring the 'typedef'. This was probably supposed to be |
| 259 | // the 'typename' keyword, which we should have already suggested adding |
| 260 | // if it's appropriate. |
| 261 | Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef) |
| 262 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
| 263 | DS.ClearStorageClassSpecs(); |
| 264 | } |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 265 | |
| 266 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 267 | if (DeclaratorInfo.getName().getKind() != |
| 268 | UnqualifiedIdKind::IK_TemplateId) { |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 269 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 270 | // recover by ignoring the 'template' keyword. |
| 271 | Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 272 | return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(), |
| 273 | &LateParsedAttrs); |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 274 | } else { |
| 275 | SourceLocation LAngleLoc |
| 276 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 277 | Diag(DeclaratorInfo.getIdentifierLoc(), |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 278 | diag::err_explicit_instantiation_with_definition) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 279 | << SourceRange(TemplateInfo.TemplateLoc) |
| 280 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 281 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 282 | // Recover as if it were an explicit specialization. |
Larisse Voufo | b9bbaba | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 283 | TemplateParameterLists FakedParamLists; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 284 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 285 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 286 | LAngleLoc, nullptr)); |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 287 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 288 | return ParseFunctionDefinition( |
| 289 | DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists, |
| 290 | /*isSpecialization=*/true, |
| 291 | /*LastParamListWasEmpty=*/true), |
| 292 | &LateParsedAttrs); |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 295 | return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 296 | &LateParsedAttrs); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // Parse this declaration. |
| 300 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, |
| 301 | TemplateInfo); |
| 302 | |
| 303 | if (Tok.is(tok::comma)) { |
| 304 | Diag(Tok, diag::err_multiple_template_declarators) |
| 305 | << (int)TemplateInfo.Kind; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 306 | SkipUntil(tok::semi); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 307 | return ThisDecl; |
| 308 | } |
| 309 | |
| 310 | // Eat the semi colon after the declaration. |
| 311 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); |
| 312 | if (LateParsedAttrs.size() > 0) |
| 313 | ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false); |
| 314 | DeclaratorInfo.complete(ThisDecl); |
| 315 | return ThisDecl; |
| 316 | } |
| 317 | |
| 318 | /// ParseTemplateParameters - Parses a template-parameter-list enclosed in |
| 319 | /// angle brackets. Depth is the depth of this template-parameter-list, which |
| 320 | /// is the number of template headers directly enclosing this template header. |
| 321 | /// TemplateParams is the current list of template parameters we're building. |
| 322 | /// The template parameter we parse will be added to this list. LAngleLoc and |
| 323 | /// RAngleLoc will receive the positions of the '<' and '>', respectively, |
| 324 | /// that enclose this template parameter list. |
| 325 | /// |
| 326 | /// \returns true if an error occurred, false otherwise. |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 327 | bool Parser::ParseTemplateParameters( |
| 328 | unsigned Depth, SmallVectorImpl<NamedDecl *> &TemplateParams, |
| 329 | SourceLocation &LAngleLoc, SourceLocation &RAngleLoc) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 330 | // Get the template parameter list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 331 | if (!TryConsumeToken(tok::less, LAngleLoc)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 332 | Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; |
| 333 | return true; |
| 334 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 335 | |
| 336 | // Try to parse the template parameter list. |
| 337 | bool Failed = false; |
| 338 | if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) |
| 339 | Failed = ParseTemplateParameterList(Depth, TemplateParams); |
| 340 | |
| 341 | if (Tok.is(tok::greatergreater)) { |
| 342 | // No diagnostic required here: a template-parameter-list can only be |
| 343 | // followed by a declaration or, for a template template parameter, the |
| 344 | // 'class' keyword. Therefore, the second '>' will be diagnosed later. |
| 345 | // This matters for elegant diagnosis of: |
| 346 | // template<template<typename>> struct S; |
| 347 | Tok.setKind(tok::greater); |
| 348 | RAngleLoc = Tok.getLocation(); |
| 349 | Tok.setLocation(Tok.getLocation().getLocWithOffset(1)); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 350 | } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) { |
| 351 | Diag(Tok.getLocation(), diag::err_expected) << tok::greater; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | /// ParseTemplateParameterList - Parse a template parameter list. If |
| 358 | /// the parsing fails badly (i.e., closing bracket was left out), this |
| 359 | /// will try to put the token stream in a reasonable position (closing |
| 360 | /// a statement, etc.) and return false. |
| 361 | /// |
| 362 | /// template-parameter-list: [C++ temp] |
| 363 | /// template-parameter |
| 364 | /// template-parameter-list ',' template-parameter |
| 365 | bool |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 366 | Parser::ParseTemplateParameterList(const unsigned Depth, |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 367 | SmallVectorImpl<NamedDecl*> &TemplateParams) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 368 | while (1) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 369 | |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 370 | if (NamedDecl *TmpParam |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 371 | = ParseTemplateParameter(Depth, TemplateParams.size())) { |
Faisal Vali | d9548c3 | 2017-12-23 19:27:07 +0000 | [diff] [blame] | 372 | TemplateParams.push_back(TmpParam); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 373 | } else { |
| 374 | // If we failed to parse a template parameter, skip until we find |
| 375 | // a comma or closing brace. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 376 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, |
| 377 | StopAtSemi | StopBeforeMatch); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Did we find a comma or the end of the template parameter list? |
| 381 | if (Tok.is(tok::comma)) { |
| 382 | ConsumeToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 383 | } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 384 | // Don't consume this... that's done by template parser. |
| 385 | break; |
| 386 | } else { |
| 387 | // Somebody probably forgot to close the template. Skip ahead and |
| 388 | // try to get out of the expression. This error is currently |
| 389 | // subsumed by whatever goes on in ParseTemplateParameter. |
| 390 | Diag(Tok.getLocation(), diag::err_expected_comma_greater); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 391 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, |
| 392 | StopAtSemi | StopBeforeMatch); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | } |
| 396 | return true; |
| 397 | } |
| 398 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 399 | /// Determine whether the parser is at the start of a template |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 400 | /// type parameter. |
| 401 | bool Parser::isStartOfTemplateTypeParameter() { |
| 402 | if (Tok.is(tok::kw_class)) { |
| 403 | // "class" may be the start of an elaborated-type-specifier or a |
| 404 | // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. |
| 405 | switch (NextToken().getKind()) { |
| 406 | case tok::equal: |
| 407 | case tok::comma: |
| 408 | case tok::greater: |
| 409 | case tok::greatergreater: |
| 410 | case tok::ellipsis: |
| 411 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 412 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 413 | case tok::identifier: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 414 | // This may be either a type-parameter or an elaborated-type-specifier. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 415 | // We have to look further. |
| 416 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 417 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 418 | default: |
| 419 | return false; |
| 420 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 421 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 422 | switch (GetLookAheadToken(2).getKind()) { |
| 423 | case tok::equal: |
| 424 | case tok::comma: |
| 425 | case tok::greater: |
| 426 | case tok::greatergreater: |
| 427 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 428 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 429 | default: |
| 430 | return false; |
| 431 | } |
| 432 | } |
| 433 | |
Richard Smith | f9a5d5f | 2018-08-17 19:43:40 +0000 | [diff] [blame] | 434 | // 'typedef' is a reasonably-common typo/thinko for 'typename', and is |
| 435 | // ill-formed otherwise. |
| 436 | if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef)) |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 437 | return false; |
| 438 | |
| 439 | // C++ [temp.param]p2: |
| 440 | // There is no semantic difference between class and typename in a |
| 441 | // template-parameter. typename followed by an unqualified-id |
| 442 | // names a template type parameter. typename followed by a |
| 443 | // qualified-id denotes the type in a non-type |
| 444 | // parameter-declaration. |
| 445 | Token Next = NextToken(); |
| 446 | |
| 447 | // If we have an identifier, skip over it. |
| 448 | if (Next.getKind() == tok::identifier) |
| 449 | Next = GetLookAheadToken(2); |
| 450 | |
| 451 | switch (Next.getKind()) { |
| 452 | case tok::equal: |
| 453 | case tok::comma: |
| 454 | case tok::greater: |
| 455 | case tok::greatergreater: |
| 456 | case tok::ellipsis: |
| 457 | return true; |
| 458 | |
Richard Smith | f9a5d5f | 2018-08-17 19:43:40 +0000 | [diff] [blame] | 459 | case tok::kw_typename: |
| 460 | case tok::kw_typedef: |
| 461 | case tok::kw_class: |
| 462 | // These indicate that a comma was missed after a type parameter, not that |
| 463 | // we have found a non-type parameter. |
| 464 | return true; |
| 465 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 466 | default: |
| 467 | return false; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). |
| 472 | /// |
| 473 | /// template-parameter: [C++ temp.param] |
| 474 | /// type-parameter |
| 475 | /// parameter-declaration |
| 476 | /// |
| 477 | /// type-parameter: (see below) |
| 478 | /// 'class' ...[opt] identifier[opt] |
| 479 | /// 'class' identifier[opt] '=' type-id |
| 480 | /// 'typename' ...[opt] identifier[opt] |
| 481 | /// 'typename' identifier[opt] '=' type-id |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 482 | /// 'template' '<' template-parameter-list '>' |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 483 | /// 'class' ...[opt] identifier[opt] |
| 484 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] |
| 485 | /// = id-expression |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 486 | NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { |
Richard Smith | f9a5d5f | 2018-08-17 19:43:40 +0000 | [diff] [blame] | 487 | if (isStartOfTemplateTypeParameter()) { |
| 488 | // Is there just a typo in the input code? ('typedef' instead of 'typename') |
| 489 | if (Tok.is(tok::kw_typedef)) { |
| 490 | Diag(Tok.getLocation(), diag::err_expected_template_parameter); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 491 | |
Richard Smith | f9a5d5f | 2018-08-17 19:43:40 +0000 | [diff] [blame] | 492 | Diag(Tok.getLocation(), diag::note_meant_to_use_typename) |
| 493 | << FixItHint::CreateReplacement(CharSourceRange::getCharRange( |
| 494 | Tok.getLocation(), Tok.getEndLoc()), |
| 495 | "typename"); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 496 | |
Richard Smith | f9a5d5f | 2018-08-17 19:43:40 +0000 | [diff] [blame] | 497 | Tok.setKind(tok::kw_typename); |
| 498 | } |
Jan Korous | 3a98e51 | 2018-02-08 14:37:58 +0000 | [diff] [blame] | 499 | |
| 500 | return ParseTypeParameter(Depth, Position); |
| 501 | } |
| 502 | |
Richard Smith | f9a5d5f | 2018-08-17 19:43:40 +0000 | [diff] [blame] | 503 | if (Tok.is(tok::kw_template)) |
| 504 | return ParseTemplateTemplateParameter(Depth, Position); |
| 505 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 506 | // If it's none of the above, then it must be a parameter declaration. |
| 507 | // NOTE: This will pick up errors in the closure of the template parameter |
| 508 | // list (e.g., template < ; Check here to implement >> style closures. |
| 509 | return ParseNonTypeTemplateParameter(Depth, Position); |
| 510 | } |
| 511 | |
| 512 | /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). |
| 513 | /// Other kinds of template parameters are parsed in |
| 514 | /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. |
| 515 | /// |
| 516 | /// type-parameter: [C++ temp.param] |
| 517 | /// 'class' ...[opt][C++0x] identifier[opt] |
| 518 | /// 'class' identifier[opt] '=' type-id |
| 519 | /// 'typename' ...[opt][C++0x] identifier[opt] |
| 520 | /// 'typename' identifier[opt] '=' type-id |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 521 | NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 522 | assert(Tok.isOneOf(tok::kw_class, tok::kw_typename) && |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 523 | "A type-parameter starts with 'class' or 'typename'"); |
| 524 | |
| 525 | // Consume the 'class' or 'typename' keyword. |
| 526 | bool TypenameKeyword = Tok.is(tok::kw_typename); |
| 527 | SourceLocation KeyLoc = ConsumeToken(); |
| 528 | |
| 529 | // Grab the ellipsis (if given). |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 530 | SourceLocation EllipsisLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 531 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 532 | Diag(EllipsisLoc, |
| 533 | getLangOpts().CPlusPlus11 |
| 534 | ? diag::warn_cxx98_compat_variadic_templates |
| 535 | : diag::ext_variadic_templates); |
| 536 | } |
| 537 | |
| 538 | // Grab the template parameter name (if given) |
| 539 | SourceLocation NameLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 540 | IdentifierInfo *ParamName = nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 541 | if (Tok.is(tok::identifier)) { |
| 542 | ParamName = Tok.getIdentifierInfo(); |
| 543 | NameLoc = ConsumeToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 544 | } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, |
| 545 | tok::greatergreater)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 546 | // Unnamed template parameter. Don't have to do anything here, just |
| 547 | // don't consume this token. |
| 548 | } else { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 549 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 550 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 553 | // Recover from misplaced ellipsis. |
| 554 | bool AlreadyHasEllipsis = EllipsisLoc.isValid(); |
| 555 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
| 556 | DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); |
| 557 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 558 | // Grab a default argument (if available). |
| 559 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before |
| 560 | // we introduce the type parameter into the local scope. |
| 561 | SourceLocation EqualLoc; |
| 562 | ParsedType DefaultArg; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 563 | if (TryConsumeToken(tok::equal, EqualLoc)) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 564 | DefaultArg = ParseTypeName(/*Range=*/nullptr, |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 565 | DeclaratorContext::TemplateTypeArgContext).get(); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 566 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 567 | return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, EllipsisLoc, |
| 568 | KeyLoc, ParamName, NameLoc, Depth, Position, |
| 569 | EqualLoc, DefaultArg); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /// ParseTemplateTemplateParameter - Handle the parsing of template |
| 573 | /// template parameters. |
| 574 | /// |
| 575 | /// type-parameter: [C++ temp.param] |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 576 | /// 'template' '<' template-parameter-list '>' type-parameter-key |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 577 | /// ...[opt] identifier[opt] |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 578 | /// 'template' '<' template-parameter-list '>' type-parameter-key |
| 579 | /// identifier[opt] = id-expression |
| 580 | /// type-parameter-key: |
| 581 | /// 'class' |
| 582 | /// 'typename' [C++1z] |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 583 | NamedDecl * |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 584 | Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { |
| 585 | assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); |
| 586 | |
| 587 | // Handle the template <...> part. |
| 588 | SourceLocation TemplateLoc = ConsumeToken(); |
Faisal Vali | f241b0d | 2017-08-25 18:24:20 +0000 | [diff] [blame] | 589 | SmallVector<NamedDecl*,8> TemplateParams; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 590 | SourceLocation LAngleLoc, RAngleLoc; |
| 591 | { |
| 592 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
| 593 | if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, |
| 594 | RAngleLoc)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 595 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 599 | // Provide an ExtWarn if the C++1z feature of using 'typename' here is used. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 600 | // Generate a meaningful error if the user forgot to put class before the |
| 601 | // identifier, comma, or greater. Provide a fixit if the identifier, comma, |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 602 | // or greater appear immediately or after 'struct'. In the latter case, |
| 603 | // replace the keyword with 'class'. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 604 | if (!TryConsumeToken(tok::kw_class)) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 605 | bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct); |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 606 | const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok; |
| 607 | if (Tok.is(tok::kw_typename)) { |
| 608 | Diag(Tok.getLocation(), |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 609 | getLangOpts().CPlusPlus17 |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 610 | ? diag::warn_cxx14_compat_template_template_param_typename |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 611 | : diag::ext_template_template_param_typename) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 612 | << (!getLangOpts().CPlusPlus17 |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 613 | ? FixItHint::CreateReplacement(Tok.getLocation(), "class") |
| 614 | : FixItHint()); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 615 | } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater, |
| 616 | tok::greatergreater, tok::ellipsis)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 617 | Diag(Tok.getLocation(), diag::err_class_on_template_template_param) |
| 618 | << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class") |
| 619 | : FixItHint::CreateInsertion(Tok.getLocation(), "class ")); |
Richard Smith | 78e1ca6 | 2014-06-16 15:51:22 +0000 | [diff] [blame] | 620 | } else |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 621 | Diag(Tok.getLocation(), diag::err_class_on_template_template_param); |
| 622 | |
| 623 | if (Replace) |
| 624 | ConsumeToken(); |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 625 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 626 | |
| 627 | // Parse the ellipsis, if given. |
| 628 | SourceLocation EllipsisLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 629 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 630 | Diag(EllipsisLoc, |
| 631 | getLangOpts().CPlusPlus11 |
| 632 | ? diag::warn_cxx98_compat_variadic_templates |
| 633 | : diag::ext_variadic_templates); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 634 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 635 | // Get the identifier, if given. |
| 636 | SourceLocation NameLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 637 | IdentifierInfo *ParamName = nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 638 | if (Tok.is(tok::identifier)) { |
| 639 | ParamName = Tok.getIdentifierInfo(); |
| 640 | NameLoc = ConsumeToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 641 | } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater, |
| 642 | tok::greatergreater)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 643 | // Unnamed template parameter. Don't have to do anything here, just |
| 644 | // don't consume this token. |
| 645 | } else { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 646 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 647 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 650 | // Recover from misplaced ellipsis. |
| 651 | bool AlreadyHasEllipsis = EllipsisLoc.isValid(); |
| 652 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
| 653 | DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); |
| 654 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 655 | TemplateParameterList *ParamList = |
| 656 | Actions.ActOnTemplateParameterList(Depth, SourceLocation(), |
| 657 | TemplateLoc, LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 658 | TemplateParams, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 659 | RAngleLoc, nullptr); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 660 | |
| 661 | // Grab a default argument (if available). |
| 662 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before |
| 663 | // we introduce the template parameter into the local scope. |
| 664 | SourceLocation EqualLoc; |
| 665 | ParsedTemplateArgument DefaultArg; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 666 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 667 | DefaultArg = ParseTemplateTemplateArgument(); |
| 668 | if (DefaultArg.isInvalid()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 669 | Diag(Tok.getLocation(), |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 670 | diag::err_default_template_template_parameter_not_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 671 | SkipUntil(tok::comma, tok::greater, tok::greatergreater, |
| 672 | StopAtSemi | StopBeforeMatch); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 675 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 676 | return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 677 | ParamList, EllipsisLoc, |
| 678 | ParamName, NameLoc, Depth, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 679 | Position, EqualLoc, DefaultArg); |
| 680 | } |
| 681 | |
| 682 | /// ParseNonTypeTemplateParameter - Handle the parsing of non-type |
| 683 | /// template parameters (e.g., in "template<int Size> class array;"). |
| 684 | /// |
| 685 | /// template-parameter: |
| 686 | /// ... |
| 687 | /// parameter-declaration |
Faisal Vali | be29403 | 2017-12-23 18:56:34 +0000 | [diff] [blame] | 688 | NamedDecl * |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 689 | Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { |
| 690 | // Parse the declaration-specifiers (i.e., the type). |
| 691 | // FIXME: The type should probably be restricted in some way... Not all |
| 692 | // declarators (parts of declarators?) are accepted for parameters. |
| 693 | DeclSpec DS(AttrFactory); |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 694 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
| 695 | DeclSpecContext::DSC_template_param); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 696 | |
| 697 | // Parse this as a typename. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 698 | Declarator ParamDecl(DS, DeclaratorContext::TemplateParamContext); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 699 | ParseDeclarator(ParamDecl); |
| 700 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
| 701 | Diag(Tok.getLocation(), diag::err_expected_template_parameter); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 702 | return nullptr; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 705 | // Recover from misplaced ellipsis. |
| 706 | SourceLocation EllipsisLoc; |
| 707 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
| 708 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl); |
| 709 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 710 | // If there is a default value, parse it. |
| 711 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before |
| 712 | // we introduce the template parameter into the local scope. |
| 713 | SourceLocation EqualLoc; |
| 714 | ExprResult DefaultArg; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 715 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 716 | // C++ [temp.param]p15: |
| 717 | // When parsing a default template-argument for a non-type |
| 718 | // template-parameter, the first non-nested > is taken as the |
| 719 | // end of the template-parameter-list rather than a greater-than |
| 720 | // operator. |
| 721 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 722 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 723 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 724 | |
Kaelyn Takata | 999dd85 | 2014-12-02 23:32:20 +0000 | [diff] [blame] | 725 | DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 726 | if (DefaultArg.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 727 | SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // Create the parameter. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 731 | return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, |
| 732 | Depth, Position, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 733 | DefaultArg.get()); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 736 | void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc, |
| 737 | SourceLocation CorrectLoc, |
| 738 | bool AlreadyHasEllipsis, |
| 739 | bool IdentifierHasName) { |
| 740 | FixItHint Insertion; |
| 741 | if (!AlreadyHasEllipsis) |
| 742 | Insertion = FixItHint::CreateInsertion(CorrectLoc, "..."); |
| 743 | Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 744 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion |
| 745 | << !IdentifierHasName; |
| 746 | } |
| 747 | |
| 748 | void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc, |
| 749 | Declarator &D) { |
| 750 | assert(EllipsisLoc.isValid()); |
| 751 | bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid(); |
| 752 | if (!AlreadyHasEllipsis) |
| 753 | D.setEllipsisLoc(EllipsisLoc); |
| 754 | DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(), |
| 755 | AlreadyHasEllipsis, D.hasName()); |
| 756 | } |
| 757 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 758 | /// Parses a '>' at the end of a template list. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 759 | /// |
| 760 | /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries |
| 761 | /// to determine if these tokens were supposed to be a '>' followed by |
| 762 | /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary. |
| 763 | /// |
| 764 | /// \param RAngleLoc the location of the consumed '>'. |
| 765 | /// |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 766 | /// \param ConsumeLastToken if true, the '>' is consumed. |
| 767 | /// |
| 768 | /// \param ObjCGenericList if true, this is the '>' closing an Objective-C |
| 769 | /// type parameter or type argument list, rather than a C++ template parameter |
| 770 | /// or argument list. |
Serge Pavlov | b716b3c | 2013-08-10 05:54:47 +0000 | [diff] [blame] | 771 | /// |
| 772 | /// \returns true, if current token does not start with '>', false otherwise. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 773 | bool Parser::ParseGreaterThanInTemplateList(SourceLocation &RAngleLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 774 | bool ConsumeLastToken, |
| 775 | bool ObjCGenericList) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 776 | // What will be left once we've consumed the '>'. |
| 777 | tok::TokenKind RemainingToken; |
| 778 | const char *ReplacementStr = "> >"; |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 779 | bool MergeWithNextToken = false; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 780 | |
| 781 | switch (Tok.getKind()) { |
| 782 | default: |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 783 | Diag(Tok.getLocation(), diag::err_expected) << tok::greater; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 784 | return true; |
| 785 | |
| 786 | case tok::greater: |
| 787 | // Determine the location of the '>' token. Only consume this token |
| 788 | // if the caller asked us to. |
| 789 | RAngleLoc = Tok.getLocation(); |
| 790 | if (ConsumeLastToken) |
| 791 | ConsumeToken(); |
| 792 | return false; |
| 793 | |
| 794 | case tok::greatergreater: |
| 795 | RemainingToken = tok::greater; |
| 796 | break; |
| 797 | |
| 798 | case tok::greatergreatergreater: |
| 799 | RemainingToken = tok::greatergreater; |
| 800 | break; |
| 801 | |
| 802 | case tok::greaterequal: |
| 803 | RemainingToken = tok::equal; |
| 804 | ReplacementStr = "> ="; |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 805 | |
| 806 | // Join two adjacent '=' tokens into one, for cases like: |
| 807 | // void (*p)() = f<int>; |
| 808 | // return f<int>==p; |
| 809 | if (NextToken().is(tok::equal) && |
| 810 | areTokensAdjacent(Tok, NextToken())) { |
| 811 | RemainingToken = tok::equalequal; |
| 812 | MergeWithNextToken = true; |
| 813 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 814 | break; |
| 815 | |
| 816 | case tok::greatergreaterequal: |
| 817 | RemainingToken = tok::greaterequal; |
| 818 | break; |
| 819 | } |
| 820 | |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 821 | // This template-id is terminated by a token that starts with a '>'. |
| 822 | // Outside C++11 and Objective-C, this is now error recovery. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 823 | // |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 824 | // C++11 allows this when the token is '>>', and in CUDA + C++11 mode, we |
| 825 | // extend that treatment to also apply to the '>>>' token. |
| 826 | // |
| 827 | // Objective-C allows this in its type parameter / argument lists. |
| 828 | |
| 829 | SourceLocation TokBeforeGreaterLoc = PrevTokLocation; |
| 830 | SourceLocation TokLoc = Tok.getLocation(); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 831 | Token Next = NextToken(); |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 832 | |
| 833 | // Whether splitting the current token after the '>' would undesirably result |
| 834 | // in the remaining token pasting with the token after it. This excludes the |
| 835 | // MergeWithNextToken cases, which we've already handled. |
| 836 | bool PreventMergeWithNextToken = |
| 837 | (RemainingToken == tok::greater || |
| 838 | RemainingToken == tok::greatergreater) && |
| 839 | (Next.isOneOf(tok::greater, tok::greatergreater, |
| 840 | tok::greatergreatergreater, tok::equal, tok::greaterequal, |
| 841 | tok::greatergreaterequal, tok::equalequal)) && |
| 842 | areTokensAdjacent(Tok, Next); |
| 843 | |
| 844 | // Diagnose this situation as appropriate. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 845 | if (!ObjCGenericList) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 846 | // The source range of the replaced token(s). |
| 847 | CharSourceRange ReplacementRange = CharSourceRange::getCharRange( |
| 848 | TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(), |
| 849 | getLangOpts())); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 851 | // A hint to put a space between the '>>'s. In order to make the hint as |
| 852 | // clear as possible, we include the characters either side of the space in |
| 853 | // the replacement, rather than just inserting a space at SecondCharLoc. |
| 854 | FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange, |
| 855 | ReplacementStr); |
| 856 | |
| 857 | // A hint to put another space after the token, if it would otherwise be |
| 858 | // lexed differently. |
| 859 | FixItHint Hint2; |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 860 | if (PreventMergeWithNextToken) |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 861 | Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " "); |
| 862 | |
| 863 | unsigned DiagId = diag::err_two_right_angle_brackets_need_space; |
| 864 | if (getLangOpts().CPlusPlus11 && |
| 865 | (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater))) |
| 866 | DiagId = diag::warn_cxx98_compat_two_right_angle_brackets; |
| 867 | else if (Tok.is(tok::greaterequal)) |
| 868 | DiagId = diag::err_right_angle_bracket_equal_needs_space; |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 869 | Diag(TokLoc, DiagId) << Hint1 << Hint2; |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 870 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 871 | |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 872 | // Find the "length" of the resulting '>' token. This is not always 1, as it |
| 873 | // can contain escaped newlines. |
| 874 | unsigned GreaterLength = Lexer::getTokenPrefixLength( |
| 875 | TokLoc, 1, PP.getSourceManager(), getLangOpts()); |
| 876 | |
| 877 | // Annotate the source buffer to indicate that we split the token after the |
| 878 | // '>'. This allows us to properly find the end of, and extract the spelling |
| 879 | // of, the '>' token later. |
| 880 | RAngleLoc = PP.SplitToken(TokLoc, GreaterLength); |
| 881 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 882 | // Strip the initial '>' from the token. |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 883 | bool CachingTokens = PP.IsPreviousCachedToken(Tok); |
| 884 | |
| 885 | Token Greater = Tok; |
| 886 | Greater.setLocation(RAngleLoc); |
| 887 | Greater.setKind(tok::greater); |
| 888 | Greater.setLength(GreaterLength); |
| 889 | |
| 890 | unsigned OldLength = Tok.getLength(); |
| 891 | if (MergeWithNextToken) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 892 | ConsumeToken(); |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 893 | OldLength += Tok.getLength(); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 894 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 895 | |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 896 | Tok.setKind(RemainingToken); |
| 897 | Tok.setLength(OldLength - GreaterLength); |
| 898 | |
| 899 | // Split the second token if lexing it normally would lex a different token |
| 900 | // (eg, the fifth token in 'A<B>>>' should re-lex as '>', not '>>'). |
| 901 | SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength); |
| 902 | if (PreventMergeWithNextToken) |
| 903 | AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength()); |
| 904 | Tok.setLocation(AfterGreaterLoc); |
| 905 | |
| 906 | // Update the token cache to match what we just did if necessary. |
| 907 | if (CachingTokens) { |
| 908 | // If the previous cached token is being merged, delete it. |
| 909 | if (MergeWithNextToken) |
| 910 | PP.ReplacePreviousCachedToken({}); |
| 911 | |
Bruno Cardoso Lopes | fb9b6cd | 2016-02-05 19:36:39 +0000 | [diff] [blame] | 912 | if (ConsumeLastToken) |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 913 | PP.ReplacePreviousCachedToken({Greater, Tok}); |
Bruno Cardoso Lopes | fb9b6cd | 2016-02-05 19:36:39 +0000 | [diff] [blame] | 914 | else |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 915 | PP.ReplacePreviousCachedToken({Greater}); |
Bruno Cardoso Lopes | 428a5aa | 2016-01-31 00:47:51 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 918 | if (ConsumeLastToken) { |
| 919 | PrevTokLocation = RAngleLoc; |
| 920 | } else { |
| 921 | PrevTokLocation = TokBeforeGreaterLoc; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 922 | PP.EnterToken(Tok); |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 923 | Tok = Greater; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 924 | } |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 925 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
| 929 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 930 | /// Parses a template-id that after the template name has |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 931 | /// already been parsed. |
| 932 | /// |
| 933 | /// This routine takes care of parsing the enclosed template argument |
| 934 | /// list ('<' template-parameter-list [opt] '>') and placing the |
| 935 | /// results into a form that can be transferred to semantic analysis. |
| 936 | /// |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 937 | /// \param ConsumeLastToken if true, then we will consume the last |
| 938 | /// token that forms the template-id. Otherwise, we will leave the |
| 939 | /// last token in the stream (e.g., so that it can be replaced with an |
| 940 | /// annotation token). |
| 941 | bool |
Richard Smith | 9a420f9 | 2017-05-10 21:47:30 +0000 | [diff] [blame] | 942 | Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 943 | SourceLocation &LAngleLoc, |
| 944 | TemplateArgList &TemplateArgs, |
| 945 | SourceLocation &RAngleLoc) { |
| 946 | assert(Tok.is(tok::less) && "Must have already parsed the template-name"); |
| 947 | |
| 948 | // Consume the '<'. |
| 949 | LAngleLoc = ConsumeToken(); |
| 950 | |
| 951 | // Parse the optional template-argument-list. |
| 952 | bool Invalid = false; |
| 953 | { |
| 954 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
Artem Belevich | 2eeb048 | 2018-09-21 17:46:28 +0000 | [diff] [blame] | 955 | if (!Tok.isOneOf(tok::greater, tok::greatergreater, |
| 956 | tok::greatergreatergreater, tok::greaterequal, |
| 957 | tok::greatergreaterequal)) |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 958 | Invalid = ParseTemplateArgumentList(TemplateArgs); |
| 959 | |
| 960 | if (Invalid) { |
| 961 | // Try to find the closing '>'. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 962 | if (ConsumeLastToken) |
| 963 | SkipUntil(tok::greater, StopAtSemi); |
| 964 | else |
| 965 | SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | } |
| 969 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 970 | return ParseGreaterThanInTemplateList(RAngleLoc, ConsumeLastToken, |
| 971 | /*ObjCGenericList=*/false); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 974 | /// Replace the tokens that form a simple-template-id with an |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 975 | /// annotation token containing the complete template-id. |
| 976 | /// |
| 977 | /// The first token in the stream must be the name of a template that |
| 978 | /// is followed by a '<'. This routine will parse the complete |
| 979 | /// simple-template-id and replace the tokens with a single annotation |
| 980 | /// token with one of two different kinds: if the template-id names a |
| 981 | /// type (and \p AllowTypeAnnotation is true), the annotation token is |
| 982 | /// a type annotation that includes the optional nested-name-specifier |
| 983 | /// (\p SS). Otherwise, the annotation token is a template-id |
| 984 | /// annotation that does not include the optional |
| 985 | /// nested-name-specifier. |
| 986 | /// |
| 987 | /// \param Template the declaration of the template named by the first |
| 988 | /// token (an identifier), as returned from \c Action::isTemplateName(). |
| 989 | /// |
| 990 | /// \param TNK the kind of template that \p Template |
| 991 | /// refers to, as returned from \c Action::isTemplateName(). |
| 992 | /// |
| 993 | /// \param SS if non-NULL, the nested-name-specifier that precedes |
| 994 | /// this template name. |
| 995 | /// |
| 996 | /// \param TemplateKWLoc if valid, specifies that this template-id |
| 997 | /// annotation was preceded by the 'template' keyword and gives the |
| 998 | /// location of that keyword. If invalid (the default), then this |
| 999 | /// template-id was not preceded by a 'template' keyword. |
| 1000 | /// |
| 1001 | /// \param AllowTypeAnnotation if true (the default), then a |
| 1002 | /// simple-template-id that refers to a class template, template |
| 1003 | /// template parameter, or other template that produces a type will be |
| 1004 | /// replaced with a type annotation token. Otherwise, the |
| 1005 | /// simple-template-id is always replaced with a template-id |
| 1006 | /// annotation token. |
| 1007 | /// |
| 1008 | /// If an unrecoverable parse error occurs and no annotation token can be |
| 1009 | /// formed, this function returns true. |
| 1010 | /// |
| 1011 | bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, |
| 1012 | CXXScopeSpec &SS, |
| 1013 | SourceLocation TemplateKWLoc, |
| 1014 | UnqualifiedId &TemplateName, |
| 1015 | bool AllowTypeAnnotation) { |
| 1016 | assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++"); |
| 1017 | assert(Template && Tok.is(tok::less) && |
| 1018 | "Parser isn't at the beginning of a template-id"); |
| 1019 | |
| 1020 | // Consume the template-name. |
| 1021 | SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); |
| 1022 | |
| 1023 | // Parse the enclosed template argument list. |
| 1024 | SourceLocation LAngleLoc, RAngleLoc; |
| 1025 | TemplateArgList TemplateArgs; |
Richard Smith | 9a420f9 | 2017-05-10 21:47:30 +0000 | [diff] [blame] | 1026 | bool Invalid = ParseTemplateIdAfterTemplateName(false, LAngleLoc, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1027 | TemplateArgs, |
| 1028 | RAngleLoc); |
| 1029 | |
| 1030 | if (Invalid) { |
| 1031 | // If we failed to parse the template ID but skipped ahead to a >, we're not |
| 1032 | // going to be able to form a token annotation. Eat the '>' if present. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 1033 | TryConsumeToken(tok::greater); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); |
| 1038 | |
| 1039 | // Build the annotation token. |
| 1040 | if (TNK == TNK_Type_template && AllowTypeAnnotation) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 1041 | TypeResult Type = Actions.ActOnTemplateIdType( |
| 1042 | SS, TemplateKWLoc, Template, TemplateName.Identifier, |
| 1043 | TemplateNameLoc, LAngleLoc, TemplateArgsPtr, RAngleLoc); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1044 | if (Type.isInvalid()) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 1045 | // If we failed to parse the template ID but skipped ahead to a >, we're |
| 1046 | // not going to be able to form a token annotation. Eat the '>' if |
| 1047 | // present. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 1048 | TryConsumeToken(tok::greater); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1049 | return true; |
| 1050 | } |
| 1051 | |
| 1052 | Tok.setKind(tok::annot_typename); |
| 1053 | setTypeAnnotation(Tok, Type.get()); |
| 1054 | if (SS.isNotEmpty()) |
| 1055 | Tok.setLocation(SS.getBeginLoc()); |
| 1056 | else if (TemplateKWLoc.isValid()) |
| 1057 | Tok.setLocation(TemplateKWLoc); |
| 1058 | else |
| 1059 | Tok.setLocation(TemplateNameLoc); |
| 1060 | } else { |
| 1061 | // Build a template-id annotation token that can be processed |
| 1062 | // later. |
| 1063 | Tok.setKind(tok::annot_template_id); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1064 | |
Faisal Vali | 43caf67 | 2017-05-23 01:07:12 +0000 | [diff] [blame] | 1065 | IdentifierInfo *TemplateII = |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 1066 | TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier |
Faisal Vali | 43caf67 | 2017-05-23 01:07:12 +0000 | [diff] [blame] | 1067 | ? TemplateName.Identifier |
| 1068 | : nullptr; |
| 1069 | |
| 1070 | OverloadedOperatorKind OpKind = |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 1071 | TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier |
Faisal Vali | 43caf67 | 2017-05-23 01:07:12 +0000 | [diff] [blame] | 1072 | ? OO_None |
| 1073 | : TemplateName.OperatorFunctionId.Operator; |
| 1074 | |
Dimitry Andric | e4f5d01 | 2017-12-18 19:46:56 +0000 | [diff] [blame] | 1075 | TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create( |
| 1076 | SS, TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK, |
Faisal Vali | 43caf67 | 2017-05-23 01:07:12 +0000 | [diff] [blame] | 1077 | LAngleLoc, RAngleLoc, TemplateArgs, TemplateIds); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1078 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1079 | Tok.setAnnotationValue(TemplateId); |
| 1080 | if (TemplateKWLoc.isValid()) |
| 1081 | Tok.setLocation(TemplateKWLoc); |
| 1082 | else |
| 1083 | Tok.setLocation(TemplateNameLoc); |
| 1084 | } |
| 1085 | |
| 1086 | // Common fields for the annotation token |
| 1087 | Tok.setAnnotationEndLoc(RAngleLoc); |
| 1088 | |
| 1089 | // In case the tokens were cached, have Preprocessor replace them with the |
| 1090 | // annotation token. |
| 1091 | PP.AnnotateCachedTokens(Tok); |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1095 | /// Replaces a template-id annotation token with a type |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1096 | /// annotation token. |
| 1097 | /// |
| 1098 | /// If there was a failure when forming the type from the template-id, |
| 1099 | /// a type annotation token will still be created, but will have a |
| 1100 | /// NULL type pointer to signify an error. |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 1101 | /// |
| 1102 | /// \param IsClassName Is this template-id appearing in a context where we |
| 1103 | /// know it names a class, such as in an elaborated-type-specifier or |
| 1104 | /// base-specifier? ('typename' and 'template' are unneeded and disallowed |
| 1105 | /// in those contexts.) |
| 1106 | void Parser::AnnotateTemplateIdTokenAsType(bool IsClassName) { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1107 | assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); |
| 1108 | |
| 1109 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
| 1110 | assert((TemplateId->Kind == TNK_Type_template || |
| 1111 | TemplateId->Kind == TNK_Dependent_template_name) && |
| 1112 | "Only works for type and dependent templates"); |
| 1113 | |
| 1114 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
| 1115 | TemplateId->NumArgs); |
| 1116 | |
| 1117 | TypeResult Type |
| 1118 | = Actions.ActOnTemplateIdType(TemplateId->SS, |
| 1119 | TemplateId->TemplateKWLoc, |
| 1120 | TemplateId->Template, |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 1121 | TemplateId->Name, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1122 | TemplateId->TemplateNameLoc, |
| 1123 | TemplateId->LAngleLoc, |
| 1124 | TemplateArgsPtr, |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 1125 | TemplateId->RAngleLoc, |
| 1126 | /*IsCtorOrDtorName*/false, |
| 1127 | IsClassName); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1128 | // Create the new "type" annotation token. |
| 1129 | Tok.setKind(tok::annot_typename); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1130 | setTypeAnnotation(Tok, Type.isInvalid() ? nullptr : Type.get()); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1131 | if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name. |
| 1132 | Tok.setLocation(TemplateId->SS.getBeginLoc()); |
| 1133 | // End location stays the same |
| 1134 | |
| 1135 | // Replace the template-id annotation token, and possible the scope-specifier |
| 1136 | // that precedes it, with the typename annotation token. |
| 1137 | PP.AnnotateCachedTokens(Tok); |
| 1138 | } |
| 1139 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1140 | /// Determine whether the given token can end a template argument. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1141 | static bool isEndOfTemplateArgument(Token Tok) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1142 | return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1145 | /// Parse a C++ template template argument. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1146 | ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { |
| 1147 | if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && |
| 1148 | !Tok.is(tok::annot_cxxscope)) |
| 1149 | return ParsedTemplateArgument(); |
| 1150 | |
| 1151 | // C++0x [temp.arg.template]p1: |
| 1152 | // A template-argument for a template template-parameter shall be the name |
| 1153 | // of a class template or an alias template, expressed as id-expression. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1154 | // |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1155 | // We parse an id-expression that refers to a class template or alias |
| 1156 | // template. The grammar we parse is: |
| 1157 | // |
| 1158 | // nested-name-specifier[opt] template[opt] identifier ...[opt] |
| 1159 | // |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1160 | // followed by a token that terminates a template argument, such as ',', |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1161 | // '>', or (in some cases) '>>'. |
| 1162 | CXXScopeSpec SS; // nested-name-specifier, if present |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1163 | ParseOptionalCXXScopeSpecifier(SS, nullptr, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1164 | /*EnteringContext=*/false); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1165 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1166 | ParsedTemplateArgument Result; |
| 1167 | SourceLocation EllipsisLoc; |
| 1168 | if (SS.isSet() && Tok.is(tok::kw_template)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1169 | // Parse the optional 'template' keyword following the |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1170 | // nested-name-specifier. |
| 1171 | SourceLocation TemplateKWLoc = ConsumeToken(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1172 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1173 | if (Tok.is(tok::identifier)) { |
| 1174 | // We appear to have a dependent template name. |
| 1175 | UnqualifiedId Name; |
| 1176 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1177 | ConsumeToken(); // the identifier |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1178 | |
| 1179 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
| 1180 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1181 | // If the next token signals the end of a template argument, |
| 1182 | // then we have a dependent template name that could be a template |
| 1183 | // template argument. |
| 1184 | TemplateTy Template; |
| 1185 | if (isEndOfTemplateArgument(Tok) && |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1186 | Actions.ActOnDependentTemplateName( |
| 1187 | getCurScope(), SS, TemplateKWLoc, Name, |
| 1188 | /*ObjectType=*/nullptr, |
| 1189 | /*EnteringContext=*/false, Template)) |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1190 | Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); |
| 1191 | } |
| 1192 | } else if (Tok.is(tok::identifier)) { |
| 1193 | // We may have a (non-dependent) template name. |
| 1194 | TemplateTy Template; |
| 1195 | UnqualifiedId Name; |
| 1196 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1197 | ConsumeToken(); // the identifier |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 1198 | |
| 1199 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (isEndOfTemplateArgument(Tok)) { |
| 1202 | bool MemberOfUnknownSpecialization; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1203 | TemplateNameKind TNK = Actions.isTemplateName( |
| 1204 | getCurScope(), SS, |
| 1205 | /*hasTemplateKeyword=*/false, Name, |
| 1206 | /*ObjectType=*/nullptr, |
| 1207 | /*EnteringContext=*/false, Template, MemberOfUnknownSpecialization); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1208 | if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { |
| 1209 | // We have an id-expression that refers to a class template or |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1210 | // (C++0x) alias template. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1211 | Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1215 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1216 | // If this is a pack expansion, build it as such. |
| 1217 | if (EllipsisLoc.isValid() && !Result.isInvalid()) |
| 1218 | Result = Actions.ActOnPackExpansion(Result, EllipsisLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1219 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1220 | return Result; |
| 1221 | } |
| 1222 | |
| 1223 | /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). |
| 1224 | /// |
| 1225 | /// template-argument: [C++ 14.2] |
| 1226 | /// constant-expression |
| 1227 | /// type-id |
| 1228 | /// id-expression |
| 1229 | ParsedTemplateArgument Parser::ParseTemplateArgument() { |
| 1230 | // C++ [temp.arg]p2: |
| 1231 | // In a template-argument, an ambiguity between a type-id and an |
| 1232 | // expression is resolved to a type-id, regardless of the form of |
| 1233 | // the corresponding template-parameter. |
| 1234 | // |
Faisal Vali | 56f1de4 | 2017-05-20 19:58:04 +0000 | [diff] [blame] | 1235 | // Therefore, we initially try to parse a type-id - and isCXXTypeId might look |
| 1236 | // up and annotate an identifier as an id-expression during disambiguation, |
| 1237 | // so enter the appropriate context for a constant expression template |
| 1238 | // argument before trying to disambiguate. |
| 1239 | |
| 1240 | EnterExpressionEvaluationContext EnterConstantEvaluated( |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 1241 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated, |
| 1242 | /*LambdaContextDecl=*/nullptr, |
| 1243 | /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_TemplateArgument); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1244 | if (isCXXTypeId(TypeIdAsTemplateArgument)) { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1245 | TypeResult TypeArg = ParseTypeName( |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 1246 | /*Range=*/nullptr, DeclaratorContext::TemplateArgContext); |
| 1247 | return Actions.ActOnTemplateTypeArgument(TypeArg); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1248 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1249 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1250 | // Try to parse a template template argument. |
| 1251 | { |
| 1252 | TentativeParsingAction TPA(*this); |
| 1253 | |
| 1254 | ParsedTemplateArgument TemplateTemplateArgument |
| 1255 | = ParseTemplateTemplateArgument(); |
| 1256 | if (!TemplateTemplateArgument.isInvalid()) { |
| 1257 | TPA.Commit(); |
| 1258 | return TemplateTemplateArgument; |
| 1259 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1260 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1261 | // Revert this tentative parse to parse a non-type template argument. |
| 1262 | TPA.Revert(); |
| 1263 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1264 | |
| 1265 | // Parse a non-type template argument. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1266 | SourceLocation Loc = Tok.getLocation(); |
Faisal Vali | 56f1de4 | 2017-05-20 19:58:04 +0000 | [diff] [blame] | 1267 | ExprResult ExprArg = ParseConstantExpressionInExprEvalContext(MaybeTypeCast); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1268 | if (ExprArg.isInvalid() || !ExprArg.get()) |
| 1269 | return ParsedTemplateArgument(); |
| 1270 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1271 | return ParsedTemplateArgument(ParsedTemplateArgument::NonType, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1272 | ExprArg.get(), Loc); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1275 | /// Determine whether the current tokens can only be parsed as a |
| 1276 | /// template argument list (starting with the '<') and never as a '<' |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1277 | /// expression. |
| 1278 | bool Parser::IsTemplateArgumentList(unsigned Skip) { |
| 1279 | struct AlwaysRevertAction : TentativeParsingAction { |
| 1280 | AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { } |
| 1281 | ~AlwaysRevertAction() { Revert(); } |
| 1282 | } Tentative(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1283 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1284 | while (Skip) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1285 | ConsumeAnyToken(); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1286 | --Skip; |
| 1287 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1288 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1289 | // '<' |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 1290 | if (!TryConsumeToken(tok::less)) |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1291 | return false; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1292 | |
| 1293 | // An empty template argument list. |
| 1294 | if (Tok.is(tok::greater)) |
| 1295 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1296 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1297 | // See whether we have declaration specifiers, which indicate a type. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 1298 | while (isCXXDeclarationSpecifier() == TPResult::True) |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1299 | ConsumeAnyToken(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1300 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1301 | // If we have a '>' or a ',' then this is a template argument list. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1302 | return Tok.isOneOf(tok::greater, tok::comma); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /// ParseTemplateArgumentList - Parse a C++ template-argument-list |
| 1306 | /// (C++ [temp.names]). Returns true if there was an error. |
| 1307 | /// |
| 1308 | /// template-argument-list: [C++ 14.2] |
| 1309 | /// template-argument |
| 1310 | /// template-argument-list ',' template-argument |
| 1311 | bool |
| 1312 | Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1313 | |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 1314 | ColonProtectionRAIIObject ColonProtection(*this, false); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1315 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 1316 | do { |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1317 | ParsedTemplateArgument Arg = ParseTemplateArgument(); |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 1318 | SourceLocation EllipsisLoc; |
| 1319 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1320 | Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1321 | |
| 1322 | if (Arg.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1323 | SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1324 | return true; |
| 1325 | } |
| 1326 | |
| 1327 | // Save this template argument. |
| 1328 | TemplateArgs.push_back(Arg); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1329 | |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1330 | // If the next token is a comma, consume it and keep reading |
| 1331 | // arguments. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 1332 | } while (TryConsumeToken(tok::comma)); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1333 | |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1337 | /// Parse a C++ explicit template instantiation |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1338 | /// (C++ [temp.explicit]). |
| 1339 | /// |
| 1340 | /// explicit-instantiation: |
| 1341 | /// 'extern' [opt] 'template' declaration |
| 1342 | /// |
| 1343 | /// Note that the 'extern' is a GNU extension and C++11 feature. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1344 | Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1345 | SourceLocation ExternLoc, |
| 1346 | SourceLocation TemplateLoc, |
| 1347 | SourceLocation &DeclEnd, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1348 | ParsedAttributes &AccessAttrs, |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1349 | AccessSpecifier AS) { |
| 1350 | // This isn't really required here. |
| 1351 | ParsingDeclRAIIObject |
| 1352 | ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent); |
| 1353 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1354 | return ParseSingleDeclarationAfterTemplate( |
| 1355 | Context, ParsedTemplateInfo(ExternLoc, TemplateLoc), |
| 1356 | ParsingTemplateParams, DeclEnd, AccessAttrs, AS); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | SourceRange Parser::ParsedTemplateInfo::getSourceRange() const { |
| 1360 | if (TemplateParams) |
| 1361 | return getTemplateParamsRange(TemplateParams->data(), |
| 1362 | TemplateParams->size()); |
| 1363 | |
| 1364 | SourceRange R(TemplateLoc); |
| 1365 | if (ExternLoc.isValid()) |
| 1366 | R.setBegin(ExternLoc); |
| 1367 | return R; |
| 1368 | } |
| 1369 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1370 | void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) { |
| 1371 | ((Parser *)P)->ParseLateTemplatedFuncDef(LPT); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1374 | /// Late parse a C++ function template in Microsoft mode. |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1375 | void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { |
David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 1376 | if (!LPT.D) |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1377 | return; |
| 1378 | |
| 1379 | // Get the FunctionDecl. |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1380 | FunctionDecl *FunD = LPT.D->getAsFunction(); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1381 | // Track template parameter depth. |
| 1382 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 1383 | |
| 1384 | // To restore the context after late parsing. |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1385 | Sema::ContextRAII GlobalSavedContext( |
| 1386 | Actions, Actions.Context.getTranslationUnitDecl()); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1387 | |
| 1388 | SmallVector<ParseScope*, 4> TemplateParamScopeStack; |
| 1389 | |
Reid Kleckner | 229eee4 | 2018-11-27 21:20:42 +0000 | [diff] [blame] | 1390 | // Get the list of DeclContexts to reenter. For inline methods, we only want |
| 1391 | // to push the DeclContext of the outermost class. This matches the way the |
| 1392 | // parser normally parses bodies of inline methods when the outermost class is |
| 1393 | // complete. |
| 1394 | struct ContainingDC { |
| 1395 | ContainingDC(DeclContext *DC, bool ShouldPush) : Pair(DC, ShouldPush) {} |
| 1396 | llvm::PointerIntPair<DeclContext *, 1, bool> Pair; |
| 1397 | DeclContext *getDC() { return Pair.getPointer(); } |
| 1398 | bool shouldPushDC() { return Pair.getInt(); } |
| 1399 | }; |
| 1400 | SmallVector<ContainingDC, 4> DeclContextsToReenter; |
Hans Wennborg | b6d4e8c | 2014-05-02 02:01:07 +0000 | [diff] [blame] | 1401 | DeclContext *DD = FunD; |
Reid Kleckner | 229eee4 | 2018-11-27 21:20:42 +0000 | [diff] [blame] | 1402 | DeclContext *NextContaining = Actions.getContainingDC(DD); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1403 | while (DD && !DD->isTranslationUnit()) { |
Reid Kleckner | 229eee4 | 2018-11-27 21:20:42 +0000 | [diff] [blame] | 1404 | bool ShouldPush = DD == NextContaining; |
| 1405 | DeclContextsToReenter.push_back({DD, ShouldPush}); |
| 1406 | if (ShouldPush) |
| 1407 | NextContaining = Actions.getContainingDC(DD); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1408 | DD = DD->getLexicalParent(); |
| 1409 | } |
| 1410 | |
| 1411 | // Reenter template scopes from outermost to innermost. |
Reid Kleckner | 229eee4 | 2018-11-27 21:20:42 +0000 | [diff] [blame] | 1412 | for (ContainingDC CDC : reverse(DeclContextsToReenter)) { |
| 1413 | TemplateParamScopeStack.push_back( |
| 1414 | new ParseScope(this, Scope::TemplateParamScope)); |
| 1415 | unsigned NumParamLists = Actions.ActOnReenterTemplateScope( |
| 1416 | getCurScope(), cast<Decl>(CDC.getDC())); |
Hans Wennborg | b6d4e8c | 2014-05-02 02:01:07 +0000 | [diff] [blame] | 1417 | CurTemplateDepthTracker.addDepth(NumParamLists); |
Reid Kleckner | 229eee4 | 2018-11-27 21:20:42 +0000 | [diff] [blame] | 1418 | if (CDC.shouldPushDC()) { |
Hans Wennborg | b6d4e8c | 2014-05-02 02:01:07 +0000 | [diff] [blame] | 1419 | TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope)); |
Reid Kleckner | 229eee4 | 2018-11-27 21:20:42 +0000 | [diff] [blame] | 1420 | Actions.PushDeclContext(Actions.getCurScope(), CDC.getDC()); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1421 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1422 | } |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1423 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1424 | assert(!LPT.Toks.empty() && "Empty body!"); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1425 | |
| 1426 | // Append the current token at the end of the new token stream so that it |
| 1427 | // doesn't get lost. |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1428 | LPT.Toks.push_back(Tok); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1429 | PP.EnterTokenStream(LPT.Toks, true); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1430 | |
| 1431 | // Consume the previously pushed token. |
| 1432 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1433 | assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) && |
| 1434 | "Inline method not starting with '{', ':' or 'try'"); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Parse the method body. Function body parsing code is similar enough |
| 1437 | // to be re-used for method bodies as well. |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 1438 | ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope | |
| 1439 | Scope::CompoundStmtScope); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1440 | |
| 1441 | // Recreate the containing function DeclContext. |
Nico Weber | 55048cf | 2014-08-15 22:15:00 +0000 | [diff] [blame] | 1442 | Sema::ContextRAII FunctionSavedContext(Actions, |
| 1443 | Actions.getContainingDC(FunD)); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1444 | |
| 1445 | Actions.ActOnStartOfFunctionDef(getCurScope(), FunD); |
| 1446 | |
| 1447 | if (Tok.is(tok::kw_try)) { |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1448 | ParseFunctionTryBlock(LPT.D, FnScope); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1449 | } else { |
| 1450 | if (Tok.is(tok::colon)) |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1451 | ParseConstructorInitializer(LPT.D); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1452 | else |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1453 | Actions.ActOnDefaultCtorInitializers(LPT.D); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (Tok.is(tok::l_brace)) { |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1456 | assert((!isa<FunctionTemplateDecl>(LPT.D) || |
| 1457 | cast<FunctionTemplateDecl>(LPT.D) |
| 1458 | ->getTemplateParameters() |
Hans Wennborg | b6d4e8c | 2014-05-02 02:01:07 +0000 | [diff] [blame] | 1459 | ->getDepth() == TemplateParameterDepth - 1) && |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1460 | "TemplateParameterDepth should be greater than the depth of " |
| 1461 | "current template being instantiated!"); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 1462 | ParseFunctionStatementBody(LPT.D, FnScope); |
| 1463 | Actions.UnmarkAsLateParsedTemplate(FunD); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1464 | } else |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1465 | Actions.ActOnFinishFunctionBody(LPT.D, nullptr); |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | // Exit scopes. |
| 1469 | FnScope.Exit(); |
Craig Topper | 61ac906 | 2013-07-08 03:55:09 +0000 | [diff] [blame] | 1470 | SmallVectorImpl<ParseScope *>::reverse_iterator I = |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1471 | TemplateParamScopeStack.rbegin(); |
| 1472 | for (; I != TemplateParamScopeStack.rend(); ++I) |
| 1473 | delete *I; |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1476 | /// Lex a delayed template function for late parsing. |
Faisal Vali | 6a79ca1 | 2013-06-08 19:39:00 +0000 | [diff] [blame] | 1477 | void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { |
| 1478 | tok::TokenKind kind = Tok.getKind(); |
| 1479 | if (!ConsumeAndStoreFunctionPrologue(Toks)) { |
| 1480 | // Consume everything up to (and including) the matching right brace. |
| 1481 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 1482 | } |
| 1483 | |
| 1484 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 1485 | if (kind == tok::kw_try) { |
| 1486 | while (Tok.is(tok::kw_catch)) { |
| 1487 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 1488 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 1489 | } |
| 1490 | } |
| 1491 | } |
Richard Smith | c2dead4 | 2018-06-27 01:32:04 +0000 | [diff] [blame] | 1492 | |
| 1493 | /// We've parsed something that could plausibly be intended to be a template |
| 1494 | /// name (\p LHS) followed by a '<' token, and the following code can't possibly |
| 1495 | /// be an expression. Determine if this is likely to be a template-id and if so, |
| 1496 | /// diagnose it. |
| 1497 | bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) { |
| 1498 | TentativeParsingAction TPA(*this); |
| 1499 | // FIXME: We could look at the token sequence in a lot more detail here. |
| 1500 | if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater, |
| 1501 | StopAtSemi | StopBeforeMatch)) { |
| 1502 | TPA.Commit(); |
| 1503 | |
| 1504 | SourceLocation Greater; |
| 1505 | ParseGreaterThanInTemplateList(Greater, true, false); |
| 1506 | Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS, |
| 1507 | Less, Greater); |
| 1508 | return true; |
| 1509 | } |
| 1510 | |
| 1511 | // There's no matching '>' token, this probably isn't supposed to be |
| 1512 | // interpreted as a template-id. Parse it as an (ill-formed) comparison. |
| 1513 | TPA.Revert(); |
| 1514 | return false; |
| 1515 | } |
| 1516 | |
| 1517 | void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) { |
| 1518 | assert(Tok.is(tok::less) && "not at a potential angle bracket"); |
| 1519 | |
| 1520 | bool DependentTemplateName = false; |
| 1521 | if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName, |
| 1522 | DependentTemplateName)) |
| 1523 | return; |
| 1524 | |
| 1525 | // OK, this might be a name that the user intended to be parsed as a |
| 1526 | // template-name, followed by a '<' token. Check for some easy cases. |
| 1527 | |
| 1528 | // If we have potential_template<>, then it's supposed to be a template-name. |
| 1529 | if (NextToken().is(tok::greater) || |
| 1530 | (getLangOpts().CPlusPlus11 && |
| 1531 | NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) { |
| 1532 | SourceLocation Less = ConsumeToken(); |
| 1533 | SourceLocation Greater; |
| 1534 | ParseGreaterThanInTemplateList(Greater, true, false); |
| 1535 | Actions.diagnoseExprIntendedAsTemplateName( |
| 1536 | getCurScope(), PotentialTemplateName, Less, Greater); |
| 1537 | // FIXME: Perform error recovery. |
| 1538 | PotentialTemplateName = ExprError(); |
| 1539 | return; |
| 1540 | } |
| 1541 | |
| 1542 | // If we have 'potential_template<type-id', assume it's supposed to be a |
| 1543 | // template-name if there's a matching '>' later on. |
| 1544 | { |
| 1545 | // FIXME: Avoid the tentative parse when NextToken() can't begin a type. |
| 1546 | TentativeParsingAction TPA(*this); |
| 1547 | SourceLocation Less = ConsumeToken(); |
| 1548 | if (isTypeIdUnambiguously() && |
| 1549 | diagnoseUnknownTemplateId(PotentialTemplateName, Less)) { |
| 1550 | TPA.Commit(); |
| 1551 | // FIXME: Perform error recovery. |
| 1552 | PotentialTemplateName = ExprError(); |
| 1553 | return; |
| 1554 | } |
| 1555 | TPA.Revert(); |
| 1556 | } |
| 1557 | |
| 1558 | // Otherwise, remember that we saw this in case we see a potentially-matching |
| 1559 | // '>' token later on. |
| 1560 | AngleBracketTracker::Priority Priority = |
| 1561 | (DependentTemplateName ? AngleBracketTracker::DependentName |
| 1562 | : AngleBracketTracker::PotentialTypo) | |
| 1563 | (Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess |
| 1564 | : AngleBracketTracker::NoSpaceBeforeLess); |
| 1565 | AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(), |
| 1566 | Priority); |
| 1567 | } |
| 1568 | |
| 1569 | bool Parser::checkPotentialAngleBracketDelimiter( |
| 1570 | const AngleBracketTracker::Loc &LAngle, const Token &OpToken) { |
| 1571 | // If a comma in an expression context is followed by a type that can be a |
| 1572 | // template argument and cannot be an expression, then this is ill-formed, |
| 1573 | // but might be intended to be part of a template-id. |
| 1574 | if (OpToken.is(tok::comma) && isTypeIdUnambiguously() && |
| 1575 | diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) { |
| 1576 | AngleBrackets.clear(*this); |
| 1577 | return true; |
| 1578 | } |
| 1579 | |
| 1580 | // If a context that looks like a template-id is followed by '()', then |
| 1581 | // this is ill-formed, but might be intended to be a template-id |
| 1582 | // followed by '()'. |
| 1583 | if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) && |
| 1584 | NextToken().is(tok::r_paren)) { |
| 1585 | Actions.diagnoseExprIntendedAsTemplateName( |
| 1586 | getCurScope(), LAngle.TemplateName, LAngle.LessLoc, |
| 1587 | OpToken.getLocation()); |
| 1588 | AngleBrackets.clear(*this); |
| 1589 | return true; |
| 1590 | } |
| 1591 | |
| 1592 | // After a '>' (etc), we're no longer potentially in a construct that's |
| 1593 | // intended to be treated as a template-id. |
| 1594 | if (OpToken.is(tok::greater) || |
| 1595 | (getLangOpts().CPlusPlus11 && |
| 1596 | OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater))) |
| 1597 | AngleBrackets.clear(*this); |
| 1598 | return false; |
| 1599 | } |