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