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