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