ParseTemplate.cpp revision 212904
1193326Sed//===--- ParseTemplate.cpp - Template Parsing -----------------------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements parsing of C++ templates. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#include "clang/Parse/Parser.h" 15193326Sed#include "clang/Parse/ParseDiagnostic.h" 16212904Sdim#include "clang/Sema/DeclSpec.h" 17212904Sdim#include "clang/Sema/ParsedTemplate.h" 18212904Sdim#include "clang/Sema/Scope.h" 19200583Srdivacky#include "RAIIObjectsForParser.h" 20193326Sedusing namespace clang; 21193326Sed 22193326Sed/// \brief Parse a template declaration, explicit instantiation, or 23193326Sed/// explicit specialization. 24212904SdimDecl * 25193326SedParser::ParseDeclarationStartingWithTemplate(unsigned Context, 26193326Sed SourceLocation &DeclEnd, 27193326Sed AccessSpecifier AS) { 28193326Sed if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) 29198092Srdivacky return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(), 30198092Srdivacky DeclEnd); 31193326Sed 32193326Sed return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS); 33193326Sed} 34193326Sed 35198092Srdivacky/// \brief RAII class that manages the template parameter depth. 36198092Srdivackynamespace { 37199990Srdivacky class TemplateParameterDepthCounter { 38198092Srdivacky unsigned &Depth; 39198092Srdivacky unsigned AddedLevels; 40198092Srdivacky 41198092Srdivacky public: 42198092Srdivacky explicit TemplateParameterDepthCounter(unsigned &Depth) 43198092Srdivacky : Depth(Depth), AddedLevels(0) { } 44198092Srdivacky 45198092Srdivacky ~TemplateParameterDepthCounter() { 46198092Srdivacky Depth -= AddedLevels; 47198092Srdivacky } 48198092Srdivacky 49198092Srdivacky void operator++() { 50198092Srdivacky ++Depth; 51198092Srdivacky ++AddedLevels; 52198092Srdivacky } 53198092Srdivacky 54198092Srdivacky operator unsigned() const { return Depth; } 55198092Srdivacky }; 56198092Srdivacky} 57198092Srdivacky 58193326Sed/// \brief Parse a template declaration or an explicit specialization. 59193326Sed/// 60193326Sed/// Template declarations include one or more template parameter lists 61193326Sed/// and either the function or class template declaration. Explicit 62193326Sed/// specializations contain one or more 'template < >' prefixes 63193326Sed/// followed by a (possibly templated) declaration. Since the 64193326Sed/// syntactic form of both features is nearly identical, we parse all 65193326Sed/// of the template headers together and let semantic analysis sort 66193326Sed/// the declarations from the explicit specializations. 67193326Sed/// 68193326Sed/// template-declaration: [C++ temp] 69193326Sed/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration 70193326Sed/// 71193326Sed/// explicit-specialization: [ C++ temp.expl.spec] 72193326Sed/// 'template' '<' '>' declaration 73212904SdimDecl * 74193326SedParser::ParseTemplateDeclarationOrSpecialization(unsigned Context, 75193326Sed SourceLocation &DeclEnd, 76193326Sed AccessSpecifier AS) { 77198092Srdivacky assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && 78198092Srdivacky "Token does not start a template declaration."); 79198092Srdivacky 80193326Sed // Enter template-parameter scope. 81193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 82193326Sed 83212904Sdim // Tell the action that names should be checked in the context of 84212904Sdim // the declaration to come. 85212904Sdim ParsingDeclRAIIObject ParsingTemplateParams(*this); 86212904Sdim 87193326Sed // Parse multiple levels of template headers within this template 88193326Sed // parameter scope, e.g., 89193326Sed // 90193326Sed // template<typename T> 91193326Sed // template<typename U> 92193326Sed // class A<T>::B { ... }; 93193326Sed // 94193326Sed // We parse multiple levels non-recursively so that we can build a 95193326Sed // single data structure containing all of the template parameter 96193326Sed // lists to easily differentiate between the case above and: 97193326Sed // 98193326Sed // template<typename T> 99193326Sed // class A { 100193326Sed // template<typename U> class B; 101193326Sed // }; 102193326Sed // 103193326Sed // In the first case, the action for declaring A<T>::B receives 104193326Sed // both template parameter lists. In the second case, the action for 105193326Sed // defining A<T>::B receives just the inner template parameter list 106193326Sed // (and retrieves the outer template parameter list from its 107193326Sed // context). 108198092Srdivacky bool isSpecialization = true; 109198893Srdivacky bool LastParamListWasEmpty = false; 110193326Sed TemplateParameterLists ParamLists; 111198092Srdivacky TemplateParameterDepthCounter Depth(TemplateParameterDepth); 112193326Sed do { 113193326Sed // Consume the 'export', if any. 114193326Sed SourceLocation ExportLoc; 115193326Sed if (Tok.is(tok::kw_export)) { 116193326Sed ExportLoc = ConsumeToken(); 117193326Sed } 118193326Sed 119193326Sed // Consume the 'template', which should be here. 120193326Sed SourceLocation TemplateLoc; 121193326Sed if (Tok.is(tok::kw_template)) { 122193326Sed TemplateLoc = ConsumeToken(); 123193326Sed } else { 124193326Sed Diag(Tok.getLocation(), diag::err_expected_template); 125212904Sdim return 0; 126193326Sed } 127198092Srdivacky 128193326Sed // Parse the '<' template-parameter-list '>' 129193326Sed SourceLocation LAngleLoc, RAngleLoc; 130212904Sdim llvm::SmallVector<Decl*, 4> TemplateParams; 131198092Srdivacky if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc, 132198092Srdivacky RAngleLoc)) { 133198092Srdivacky // Skip until the semi-colon or a }. 134198092Srdivacky SkipUntil(tok::r_brace, true, true); 135198092Srdivacky if (Tok.is(tok::semi)) 136198092Srdivacky ConsumeToken(); 137212904Sdim return 0; 138198092Srdivacky } 139193326Sed 140193326Sed ParamLists.push_back( 141198092Srdivacky Actions.ActOnTemplateParameterList(Depth, ExportLoc, 142198092Srdivacky TemplateLoc, LAngleLoc, 143193326Sed TemplateParams.data(), 144193326Sed TemplateParams.size(), RAngleLoc)); 145198092Srdivacky 146198092Srdivacky if (!TemplateParams.empty()) { 147198092Srdivacky isSpecialization = false; 148198092Srdivacky ++Depth; 149198893Srdivacky } else { 150198893Srdivacky LastParamListWasEmpty = true; 151198092Srdivacky } 152193326Sed } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); 153193326Sed 154193326Sed // Parse the actual template declaration. 155198092Srdivacky return ParseSingleDeclarationAfterTemplate(Context, 156193326Sed ParsedTemplateInfo(&ParamLists, 157198893Srdivacky isSpecialization, 158198893Srdivacky LastParamListWasEmpty), 159212904Sdim ParsingTemplateParams, 160193326Sed DeclEnd, AS); 161193326Sed} 162193326Sed 163193326Sed/// \brief Parse a single declaration that declares a template, 164193326Sed/// template specialization, or explicit instantiation of a template. 165193326Sed/// 166193326Sed/// \param TemplateParams if non-NULL, the template parameter lists 167193326Sed/// that preceded this declaration. In this case, the declaration is a 168193326Sed/// template declaration, out-of-line definition of a template, or an 169193326Sed/// explicit template specialization. When NULL, the declaration is an 170193326Sed/// explicit template instantiation. 171193326Sed/// 172193326Sed/// \param TemplateLoc when TemplateParams is NULL, the location of 173193326Sed/// the 'template' keyword that indicates that we have an explicit 174193326Sed/// template instantiation. 175193326Sed/// 176193326Sed/// \param DeclEnd will receive the source location of the last token 177193326Sed/// within this declaration. 178193326Sed/// 179193326Sed/// \param AS the access specifier associated with this 180193326Sed/// declaration. Will be AS_none for namespace-scope declarations. 181193326Sed/// 182193326Sed/// \returns the new declaration. 183212904SdimDecl * 184193326SedParser::ParseSingleDeclarationAfterTemplate( 185193326Sed unsigned Context, 186193326Sed const ParsedTemplateInfo &TemplateInfo, 187212904Sdim ParsingDeclRAIIObject &DiagsFromTParams, 188193326Sed SourceLocation &DeclEnd, 189193326Sed AccessSpecifier AS) { 190193326Sed assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 191193326Sed "Template information required"); 192193326Sed 193198092Srdivacky if (Context == Declarator::MemberContext) { 194198092Srdivacky // We are parsing a member template. 195212904Sdim ParseCXXClassMemberDeclaration(AS, TemplateInfo, &DiagsFromTParams); 196212904Sdim return 0; 197198092Srdivacky } 198198092Srdivacky 199212904Sdim // Parse the declaration specifiers, stealing the accumulated 200212904Sdim // diagnostics from the template parameters. 201212904Sdim ParsingDeclSpec DS(DiagsFromTParams); 202199990Srdivacky 203199990Srdivacky if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 204199990Srdivacky DS.AddAttributes(ParseCXX0XAttributes().AttrList); 205199990Srdivacky 206202379Srdivacky ParseDeclarationSpecifiers(DS, TemplateInfo, AS, 207202379Srdivacky getDeclSpecContextFromDeclaratorContext(Context)); 208193326Sed 209193326Sed if (Tok.is(tok::semi)) { 210193326Sed DeclEnd = ConsumeToken(); 211212904Sdim Decl *Decl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 212198893Srdivacky DS.complete(Decl); 213198893Srdivacky return Decl; 214193326Sed } 215193326Sed 216193326Sed // Parse the declarator. 217198893Srdivacky ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context); 218193326Sed ParseDeclarator(DeclaratorInfo); 219193326Sed // Error parsing the declarator? 220193326Sed if (!DeclaratorInfo.hasName()) { 221193326Sed // If so, skip until the semi-colon or a }. 222193326Sed SkipUntil(tok::r_brace, true, true); 223193326Sed if (Tok.is(tok::semi)) 224193326Sed ConsumeToken(); 225212904Sdim return 0; 226193326Sed } 227198092Srdivacky 228193326Sed // If we have a declaration or declarator list, handle it. 229193326Sed if (isDeclarationAfterDeclarator()) { 230193326Sed // Parse this declaration. 231212904Sdim Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, 232212904Sdim TemplateInfo); 233193326Sed 234193326Sed if (Tok.is(tok::comma)) { 235193326Sed Diag(Tok, diag::err_multiple_template_declarators) 236193326Sed << (int)TemplateInfo.Kind; 237193326Sed SkipUntil(tok::semi, true, false); 238193326Sed return ThisDecl; 239193326Sed } 240193326Sed 241193326Sed // Eat the semi colon after the declaration. 242198092Srdivacky ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration); 243198893Srdivacky DS.complete(ThisDecl); 244193326Sed return ThisDecl; 245193326Sed } 246193326Sed 247193326Sed if (DeclaratorInfo.isFunctionDeclarator() && 248210299Sed isStartOfFunctionDefinition(DeclaratorInfo)) { 249193326Sed if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 250193326Sed Diag(Tok, diag::err_function_declared_typedef); 251193326Sed 252193326Sed if (Tok.is(tok::l_brace)) { 253193326Sed // This recovery skips the entire function body. It would be nice 254193326Sed // to simply call ParseFunctionDefinition() below, however Sema 255193326Sed // assumes the declarator represents a function, not a typedef. 256193326Sed ConsumeBrace(); 257193326Sed SkipUntil(tok::r_brace, true); 258193326Sed } else { 259193326Sed SkipUntil(tok::semi); 260193326Sed } 261212904Sdim return 0; 262193326Sed } 263195099Sed return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo); 264193326Sed } 265193326Sed 266193326Sed if (DeclaratorInfo.isFunctionDeclarator()) 267193326Sed Diag(Tok, diag::err_expected_fn_body); 268193326Sed else 269193326Sed Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); 270193326Sed SkipUntil(tok::semi); 271212904Sdim return 0; 272193326Sed} 273193326Sed 274193326Sed/// ParseTemplateParameters - Parses a template-parameter-list enclosed in 275193326Sed/// angle brackets. Depth is the depth of this template-parameter-list, which 276193326Sed/// is the number of template headers directly enclosing this template header. 277193326Sed/// TemplateParams is the current list of template parameters we're building. 278193326Sed/// The template parameter we parse will be added to this list. LAngleLoc and 279198092Srdivacky/// RAngleLoc will receive the positions of the '<' and '>', respectively, 280193326Sed/// that enclose this template parameter list. 281198092Srdivacky/// 282198092Srdivacky/// \returns true if an error occurred, false otherwise. 283193326Sedbool Parser::ParseTemplateParameters(unsigned Depth, 284212904Sdim llvm::SmallVectorImpl<Decl*> &TemplateParams, 285193326Sed SourceLocation &LAngleLoc, 286193326Sed SourceLocation &RAngleLoc) { 287193326Sed // Get the template parameter list. 288198092Srdivacky if (!Tok.is(tok::less)) { 289193326Sed Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; 290198092Srdivacky return true; 291193326Sed } 292193326Sed LAngleLoc = ConsumeToken(); 293198092Srdivacky 294193326Sed // Try to parse the template parameter list. 295193326Sed if (Tok.is(tok::greater)) 296193326Sed RAngleLoc = ConsumeToken(); 297198092Srdivacky else if (ParseTemplateParameterList(Depth, TemplateParams)) { 298198092Srdivacky if (!Tok.is(tok::greater)) { 299193326Sed Diag(Tok.getLocation(), diag::err_expected_greater); 300198092Srdivacky return true; 301193326Sed } 302193326Sed RAngleLoc = ConsumeToken(); 303193326Sed } 304198092Srdivacky return false; 305193326Sed} 306193326Sed 307193326Sed/// ParseTemplateParameterList - Parse a template parameter list. If 308193326Sed/// the parsing fails badly (i.e., closing bracket was left out), this 309193326Sed/// will try to put the token stream in a reasonable position (closing 310198092Srdivacky/// a statement, etc.) and return false. 311193326Sed/// 312193326Sed/// template-parameter-list: [C++ temp] 313193326Sed/// template-parameter 314193326Sed/// template-parameter-list ',' template-parameter 315198092Srdivackybool 316193326SedParser::ParseTemplateParameterList(unsigned Depth, 317212904Sdim llvm::SmallVectorImpl<Decl*> &TemplateParams) { 318198092Srdivacky while (1) { 319212904Sdim if (Decl *TmpParam 320193326Sed = ParseTemplateParameter(Depth, TemplateParams.size())) { 321193326Sed TemplateParams.push_back(TmpParam); 322193326Sed } else { 323193326Sed // If we failed to parse a template parameter, skip until we find 324193326Sed // a comma or closing brace. 325193326Sed SkipUntil(tok::comma, tok::greater, true, true); 326193326Sed } 327198092Srdivacky 328193326Sed // Did we find a comma or the end of the template parmeter list? 329198092Srdivacky if (Tok.is(tok::comma)) { 330193326Sed ConsumeToken(); 331198092Srdivacky } else if (Tok.is(tok::greater)) { 332193326Sed // Don't consume this... that's done by template parser. 333193326Sed break; 334193326Sed } else { 335193326Sed // Somebody probably forgot to close the template. Skip ahead and 336193326Sed // try to get out of the expression. This error is currently 337193326Sed // subsumed by whatever goes on in ParseTemplateParameter. 338193326Sed // TODO: This could match >>, and it would be nice to avoid those 339193326Sed // silly errors with template <vec<T>>. 340193326Sed // Diag(Tok.getLocation(), diag::err_expected_comma_greater); 341193326Sed SkipUntil(tok::greater, true, true); 342193326Sed return false; 343193326Sed } 344193326Sed } 345193326Sed return true; 346193326Sed} 347193326Sed 348199990Srdivacky/// \brief Determine whether the parser is at the start of a template 349199990Srdivacky/// type parameter. 350199990Srdivackybool Parser::isStartOfTemplateTypeParameter() { 351210299Sed if (Tok.is(tok::kw_class)) { 352210299Sed // "class" may be the start of an elaborated-type-specifier or a 353210299Sed // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. 354210299Sed switch (NextToken().getKind()) { 355210299Sed case tok::equal: 356210299Sed case tok::comma: 357210299Sed case tok::greater: 358210299Sed case tok::greatergreater: 359210299Sed case tok::ellipsis: 360210299Sed return true; 361210299Sed 362210299Sed case tok::identifier: 363210299Sed // This may be either a type-parameter or an elaborated-type-specifier. 364210299Sed // We have to look further. 365210299Sed break; 366210299Sed 367210299Sed default: 368210299Sed return false; 369210299Sed } 370210299Sed 371210299Sed switch (GetLookAheadToken(2).getKind()) { 372210299Sed case tok::equal: 373210299Sed case tok::comma: 374210299Sed case tok::greater: 375210299Sed case tok::greatergreater: 376210299Sed return true; 377210299Sed 378210299Sed default: 379210299Sed return false; 380210299Sed } 381210299Sed } 382199990Srdivacky 383199990Srdivacky if (Tok.isNot(tok::kw_typename)) 384199990Srdivacky return false; 385199990Srdivacky 386199990Srdivacky // C++ [temp.param]p2: 387199990Srdivacky // There is no semantic difference between class and typename in a 388199990Srdivacky // template-parameter. typename followed by an unqualified-id 389199990Srdivacky // names a template type parameter. typename followed by a 390199990Srdivacky // qualified-id denotes the type in a non-type 391199990Srdivacky // parameter-declaration. 392199990Srdivacky Token Next = NextToken(); 393199990Srdivacky 394199990Srdivacky // If we have an identifier, skip over it. 395199990Srdivacky if (Next.getKind() == tok::identifier) 396199990Srdivacky Next = GetLookAheadToken(2); 397199990Srdivacky 398199990Srdivacky switch (Next.getKind()) { 399199990Srdivacky case tok::equal: 400199990Srdivacky case tok::comma: 401199990Srdivacky case tok::greater: 402199990Srdivacky case tok::greatergreater: 403199990Srdivacky case tok::ellipsis: 404199990Srdivacky return true; 405199990Srdivacky 406199990Srdivacky default: 407199990Srdivacky return false; 408199990Srdivacky } 409199990Srdivacky} 410199990Srdivacky 411193326Sed/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). 412193326Sed/// 413193326Sed/// template-parameter: [C++ temp.param] 414193326Sed/// type-parameter 415193326Sed/// parameter-declaration 416193326Sed/// 417193326Sed/// type-parameter: (see below) 418194179Sed/// 'class' ...[opt][C++0x] identifier[opt] 419193326Sed/// 'class' identifier[opt] '=' type-id 420194179Sed/// 'typename' ...[opt][C++0x] identifier[opt] 421193326Sed/// 'typename' identifier[opt] '=' type-id 422194179Sed/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt] 423193326Sed/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression 424212904SdimDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { 425199990Srdivacky if (isStartOfTemplateTypeParameter()) 426193326Sed return ParseTypeParameter(Depth, Position); 427198092Srdivacky 428198092Srdivacky if (Tok.is(tok::kw_template)) 429193326Sed return ParseTemplateTemplateParameter(Depth, Position); 430193326Sed 431193326Sed // If it's none of the above, then it must be a parameter declaration. 432193326Sed // NOTE: This will pick up errors in the closure of the template parameter 433193326Sed // list (e.g., template < ; Check here to implement >> style closures. 434193326Sed return ParseNonTypeTemplateParameter(Depth, Position); 435193326Sed} 436193326Sed 437193326Sed/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). 438193326Sed/// Other kinds of template parameters are parsed in 439193326Sed/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. 440193326Sed/// 441193326Sed/// type-parameter: [C++ temp.param] 442194179Sed/// 'class' ...[opt][C++0x] identifier[opt] 443193326Sed/// 'class' identifier[opt] '=' type-id 444194179Sed/// 'typename' ...[opt][C++0x] identifier[opt] 445193326Sed/// 'typename' identifier[opt] '=' type-id 446212904SdimDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { 447193326Sed assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && 448198092Srdivacky "A type-parameter starts with 'class' or 'typename'"); 449193326Sed 450193326Sed // Consume the 'class' or 'typename' keyword. 451193326Sed bool TypenameKeyword = Tok.is(tok::kw_typename); 452193326Sed SourceLocation KeyLoc = ConsumeToken(); 453193326Sed 454194179Sed // Grab the ellipsis (if given). 455194179Sed bool Ellipsis = false; 456194179Sed SourceLocation EllipsisLoc; 457194179Sed if (Tok.is(tok::ellipsis)) { 458194179Sed Ellipsis = true; 459194179Sed EllipsisLoc = ConsumeToken(); 460198092Srdivacky 461198092Srdivacky if (!getLang().CPlusPlus0x) 462194179Sed Diag(EllipsisLoc, diag::err_variadic_templates); 463194179Sed } 464198092Srdivacky 465193326Sed // Grab the template parameter name (if given) 466193326Sed SourceLocation NameLoc; 467193326Sed IdentifierInfo* ParamName = 0; 468198092Srdivacky if (Tok.is(tok::identifier)) { 469193326Sed ParamName = Tok.getIdentifierInfo(); 470193326Sed NameLoc = ConsumeToken(); 471198092Srdivacky } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || 472198092Srdivacky Tok.is(tok::greater)) { 473193326Sed // Unnamed template parameter. Don't have to do anything here, just 474193326Sed // don't consume this token. 475193326Sed } else { 476193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 477212904Sdim return 0; 478193326Sed } 479198092Srdivacky 480210299Sed // Grab a default argument (if available). 481210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 482210299Sed // we introduce the type parameter into the local scope. 483210299Sed SourceLocation EqualLoc; 484212904Sdim ParsedType DefaultArg; 485198092Srdivacky if (Tok.is(tok::equal)) { 486210299Sed EqualLoc = ConsumeToken(); 487210299Sed DefaultArg = ParseTypeName().get(); 488193326Sed } 489210299Sed 490210299Sed return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis, 491210299Sed EllipsisLoc, KeyLoc, ParamName, NameLoc, 492210299Sed Depth, Position, EqualLoc, DefaultArg); 493193326Sed} 494193326Sed 495193326Sed/// ParseTemplateTemplateParameter - Handle the parsing of template 496198092Srdivacky/// template parameters. 497193326Sed/// 498193326Sed/// type-parameter: [C++ temp.param] 499193326Sed/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 500193326Sed/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression 501212904SdimDecl * 502193326SedParser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { 503193326Sed assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); 504193326Sed 505193326Sed // Handle the template <...> part. 506193326Sed SourceLocation TemplateLoc = ConsumeToken(); 507212904Sdim llvm::SmallVector<Decl*,8> TemplateParams; 508193326Sed SourceLocation LAngleLoc, RAngleLoc; 509193326Sed { 510193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 511198092Srdivacky if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, 512198092Srdivacky RAngleLoc)) { 513212904Sdim return 0; 514193326Sed } 515193326Sed } 516193326Sed 517193326Sed // Generate a meaningful error if the user forgot to put class before the 518193326Sed // identifier, comma, or greater. 519198092Srdivacky if (!Tok.is(tok::kw_class)) { 520198092Srdivacky Diag(Tok.getLocation(), diag::err_expected_class_before) 521193326Sed << PP.getSpelling(Tok); 522212904Sdim return 0; 523193326Sed } 524193326Sed SourceLocation ClassLoc = ConsumeToken(); 525193326Sed 526193326Sed // Get the identifier, if given. 527193326Sed SourceLocation NameLoc; 528193326Sed IdentifierInfo* ParamName = 0; 529198092Srdivacky if (Tok.is(tok::identifier)) { 530193326Sed ParamName = Tok.getIdentifierInfo(); 531193326Sed NameLoc = ConsumeToken(); 532198092Srdivacky } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { 533193326Sed // Unnamed template parameter. Don't have to do anything here, just 534193326Sed // don't consume this token. 535193326Sed } else { 536193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 537212904Sdim return 0; 538193326Sed } 539193326Sed 540198092Srdivacky TemplateParamsTy *ParamList = 541193326Sed Actions.ActOnTemplateParameterList(Depth, SourceLocation(), 542193326Sed TemplateLoc, LAngleLoc, 543198092Srdivacky &TemplateParams[0], 544193326Sed TemplateParams.size(), 545193326Sed RAngleLoc); 546193326Sed 547210299Sed // Grab a default argument (if available). 548210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 549210299Sed // we introduce the template parameter into the local scope. 550210299Sed SourceLocation EqualLoc; 551210299Sed ParsedTemplateArgument DefaultArg; 552193326Sed if (Tok.is(tok::equal)) { 553210299Sed EqualLoc = ConsumeToken(); 554210299Sed DefaultArg = ParseTemplateTemplateArgument(); 555210299Sed if (DefaultArg.isInvalid()) { 556199482Srdivacky Diag(Tok.getLocation(), 557199482Srdivacky diag::err_default_template_template_parameter_not_template); 558200583Srdivacky static const tok::TokenKind EndToks[] = { 559199482Srdivacky tok::comma, tok::greater, tok::greatergreater 560199482Srdivacky }; 561199482Srdivacky SkipUntil(EndToks, 3, true, true); 562210299Sed } 563193326Sed } 564210299Sed 565210299Sed return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, 566210299Sed ParamList, ParamName, 567210299Sed NameLoc, Depth, Position, 568210299Sed EqualLoc, DefaultArg); 569193326Sed} 570193326Sed 571193326Sed/// ParseNonTypeTemplateParameter - Handle the parsing of non-type 572198092Srdivacky/// template parameters (e.g., in "template<int Size> class array;"). 573193326Sed/// 574193326Sed/// template-parameter: 575193326Sed/// ... 576193326Sed/// parameter-declaration 577212904SdimDecl * 578193326SedParser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { 579193326Sed SourceLocation StartLoc = Tok.getLocation(); 580193326Sed 581193326Sed // Parse the declaration-specifiers (i.e., the type). 582193326Sed // FIXME: The type should probably be restricted in some way... Not all 583193326Sed // declarators (parts of declarators?) are accepted for parameters. 584193326Sed DeclSpec DS; 585193326Sed ParseDeclarationSpecifiers(DS); 586193326Sed 587193326Sed // Parse this as a typename. 588193326Sed Declarator ParamDecl(DS, Declarator::TemplateParamContext); 589193326Sed ParseDeclarator(ParamDecl); 590212904Sdim if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { 591193326Sed // This probably shouldn't happen - and it's more of a Sema thing, but 592193326Sed // basically we didn't parse the type name because we couldn't associate 593193326Sed // it with an AST node. we should just skip to the comma or greater. 594193326Sed // TODO: This is currently a placeholder for some kind of Sema Error. 595193326Sed Diag(Tok.getLocation(), diag::err_parse_error); 596193326Sed SkipUntil(tok::comma, tok::greater, true, true); 597212904Sdim return 0; 598193326Sed } 599193326Sed 600193326Sed // If there is a default value, parse it. 601210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 602210299Sed // we introduce the template parameter into the local scope. 603210299Sed SourceLocation EqualLoc; 604212904Sdim ExprResult DefaultArg; 605193326Sed if (Tok.is(tok::equal)) { 606210299Sed EqualLoc = ConsumeToken(); 607193326Sed 608193326Sed // C++ [temp.param]p15: 609193326Sed // When parsing a default template-argument for a non-type 610193326Sed // template-parameter, the first non-nested > is taken as the 611193326Sed // end of the template-parameter-list rather than a greater-than 612193326Sed // operator. 613198092Srdivacky GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 614193326Sed 615210299Sed DefaultArg = ParseAssignmentExpression(); 616193326Sed if (DefaultArg.isInvalid()) 617193326Sed SkipUntil(tok::comma, tok::greater, true, true); 618193326Sed } 619198092Srdivacky 620210299Sed // Create the parameter. 621210299Sed return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, 622210299Sed Depth, Position, EqualLoc, 623212904Sdim DefaultArg.take()); 624193326Sed} 625193326Sed 626193326Sed/// \brief Parses a template-id that after the template name has 627193326Sed/// already been parsed. 628193326Sed/// 629193326Sed/// This routine takes care of parsing the enclosed template argument 630193326Sed/// list ('<' template-parameter-list [opt] '>') and placing the 631193326Sed/// results into a form that can be transferred to semantic analysis. 632193326Sed/// 633193326Sed/// \param Template the template declaration produced by isTemplateName 634193326Sed/// 635193326Sed/// \param TemplateNameLoc the source location of the template name 636193326Sed/// 637193326Sed/// \param SS if non-NULL, the nested-name-specifier preceding the 638193326Sed/// template name. 639193326Sed/// 640193326Sed/// \param ConsumeLastToken if true, then we will consume the last 641193326Sed/// token that forms the template-id. Otherwise, we will leave the 642193326Sed/// last token in the stream (e.g., so that it can be replaced with an 643193326Sed/// annotation token). 644198092Srdivackybool 645193326SedParser::ParseTemplateIdAfterTemplateName(TemplateTy Template, 646198092Srdivacky SourceLocation TemplateNameLoc, 647193326Sed const CXXScopeSpec *SS, 648193326Sed bool ConsumeLastToken, 649193326Sed SourceLocation &LAngleLoc, 650193326Sed TemplateArgList &TemplateArgs, 651193326Sed SourceLocation &RAngleLoc) { 652193326Sed assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 653193326Sed 654193326Sed // Consume the '<'. 655193326Sed LAngleLoc = ConsumeToken(); 656193326Sed 657193326Sed // Parse the optional template-argument-list. 658193326Sed bool Invalid = false; 659193326Sed { 660193326Sed GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 661193326Sed if (Tok.isNot(tok::greater)) 662199482Srdivacky Invalid = ParseTemplateArgumentList(TemplateArgs); 663193326Sed 664193326Sed if (Invalid) { 665193326Sed // Try to find the closing '>'. 666193326Sed SkipUntil(tok::greater, true, !ConsumeLastToken); 667193326Sed 668193326Sed return true; 669193326Sed } 670193326Sed } 671193326Sed 672201361Srdivacky if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) { 673201361Srdivacky Diag(Tok.getLocation(), diag::err_expected_greater); 674193326Sed return true; 675201361Srdivacky } 676193326Sed 677193326Sed // Determine the location of the '>' or '>>'. Only consume this 678193326Sed // token if the caller asked us to. 679193326Sed RAngleLoc = Tok.getLocation(); 680193326Sed 681193326Sed if (Tok.is(tok::greatergreater)) { 682193326Sed if (!getLang().CPlusPlus0x) { 683193326Sed const char *ReplaceStr = "> >"; 684193326Sed if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater)) 685193326Sed ReplaceStr = "> > "; 686193326Sed 687193326Sed Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space) 688206084Srdivacky << FixItHint::CreateReplacement( 689193326Sed SourceRange(Tok.getLocation()), ReplaceStr); 690193326Sed } 691193326Sed 692193326Sed Tok.setKind(tok::greater); 693193326Sed if (!ConsumeLastToken) { 694193326Sed // Since we're not supposed to consume the '>>' token, we need 695193326Sed // to insert a second '>' token after the first. 696193326Sed PP.EnterToken(Tok); 697193326Sed } 698193326Sed } else if (ConsumeLastToken) 699193326Sed ConsumeToken(); 700193326Sed 701193326Sed return false; 702193326Sed} 703198092Srdivacky 704193326Sed/// \brief Replace the tokens that form a simple-template-id with an 705193326Sed/// annotation token containing the complete template-id. 706193326Sed/// 707193326Sed/// The first token in the stream must be the name of a template that 708193326Sed/// is followed by a '<'. This routine will parse the complete 709193326Sed/// simple-template-id and replace the tokens with a single annotation 710193326Sed/// token with one of two different kinds: if the template-id names a 711193326Sed/// type (and \p AllowTypeAnnotation is true), the annotation token is 712193326Sed/// a type annotation that includes the optional nested-name-specifier 713193326Sed/// (\p SS). Otherwise, the annotation token is a template-id 714193326Sed/// annotation that does not include the optional 715193326Sed/// nested-name-specifier. 716193326Sed/// 717193326Sed/// \param Template the declaration of the template named by the first 718193326Sed/// token (an identifier), as returned from \c Action::isTemplateName(). 719193326Sed/// 720193326Sed/// \param TemplateNameKind the kind of template that \p Template 721193326Sed/// refers to, as returned from \c Action::isTemplateName(). 722193326Sed/// 723193326Sed/// \param SS if non-NULL, the nested-name-specifier that precedes 724193326Sed/// this template name. 725193326Sed/// 726193326Sed/// \param TemplateKWLoc if valid, specifies that this template-id 727193326Sed/// annotation was preceded by the 'template' keyword and gives the 728193326Sed/// location of that keyword. If invalid (the default), then this 729193326Sed/// template-id was not preceded by a 'template' keyword. 730193326Sed/// 731193326Sed/// \param AllowTypeAnnotation if true (the default), then a 732193326Sed/// simple-template-id that refers to a class template, template 733193326Sed/// template parameter, or other template that produces a type will be 734193326Sed/// replaced with a type annotation token. Otherwise, the 735193326Sed/// simple-template-id is always replaced with a template-id 736193326Sed/// annotation token. 737195099Sed/// 738195099Sed/// If an unrecoverable parse error occurs and no annotation token can be 739195099Sed/// formed, this function returns true. 740195099Sed/// 741195099Sedbool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, 742198092Srdivacky const CXXScopeSpec *SS, 743198893Srdivacky UnqualifiedId &TemplateName, 744193326Sed SourceLocation TemplateKWLoc, 745193326Sed bool AllowTypeAnnotation) { 746193326Sed assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); 747198893Srdivacky assert(Template && Tok.is(tok::less) && 748193326Sed "Parser isn't at the beginning of a template-id"); 749193326Sed 750193326Sed // Consume the template-name. 751198893Srdivacky SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); 752193326Sed 753193326Sed // Parse the enclosed template argument list. 754193326Sed SourceLocation LAngleLoc, RAngleLoc; 755193326Sed TemplateArgList TemplateArgs; 756198893Srdivacky bool Invalid = ParseTemplateIdAfterTemplateName(Template, 757198893Srdivacky TemplateNameLoc, 758198092Srdivacky SS, false, LAngleLoc, 759198092Srdivacky TemplateArgs, 760193326Sed RAngleLoc); 761198092Srdivacky 762195099Sed if (Invalid) { 763195099Sed // If we failed to parse the template ID but skipped ahead to a >, we're not 764195099Sed // going to be able to form a token annotation. Eat the '>' if present. 765195099Sed if (Tok.is(tok::greater)) 766195099Sed ConsumeToken(); 767195099Sed return true; 768195099Sed } 769193326Sed 770193326Sed ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), 771193326Sed TemplateArgs.size()); 772193326Sed 773193326Sed // Build the annotation token. 774193326Sed if (TNK == TNK_Type_template && AllowTypeAnnotation) { 775212904Sdim TypeResult Type 776193326Sed = Actions.ActOnTemplateIdType(Template, TemplateNameLoc, 777193326Sed LAngleLoc, TemplateArgsPtr, 778193326Sed RAngleLoc); 779195099Sed if (Type.isInvalid()) { 780195099Sed // If we failed to parse the template ID but skipped ahead to a >, we're not 781195099Sed // going to be able to form a token annotation. Eat the '>' if present. 782195099Sed if (Tok.is(tok::greater)) 783195099Sed ConsumeToken(); 784195099Sed return true; 785195099Sed } 786193326Sed 787193326Sed Tok.setKind(tok::annot_typename); 788212904Sdim setTypeAnnotation(Tok, Type.get()); 789193326Sed if (SS && SS->isNotEmpty()) 790193326Sed Tok.setLocation(SS->getBeginLoc()); 791193326Sed else if (TemplateKWLoc.isValid()) 792193326Sed Tok.setLocation(TemplateKWLoc); 793198092Srdivacky else 794193326Sed Tok.setLocation(TemplateNameLoc); 795193326Sed } else { 796193326Sed // Build a template-id annotation token that can be processed 797193326Sed // later. 798193326Sed Tok.setKind(tok::annot_template_id); 799198092Srdivacky TemplateIdAnnotation *TemplateId 800193326Sed = TemplateIdAnnotation::Allocate(TemplateArgs.size()); 801193326Sed TemplateId->TemplateNameLoc = TemplateNameLoc; 802198893Srdivacky if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) { 803198893Srdivacky TemplateId->Name = TemplateName.Identifier; 804198893Srdivacky TemplateId->Operator = OO_None; 805198893Srdivacky } else { 806198893Srdivacky TemplateId->Name = 0; 807198893Srdivacky TemplateId->Operator = TemplateName.OperatorFunctionId.Operator; 808198893Srdivacky } 809212904Sdim TemplateId->Template = Template; 810193326Sed TemplateId->Kind = TNK; 811193326Sed TemplateId->LAngleLoc = LAngleLoc; 812193326Sed TemplateId->RAngleLoc = RAngleLoc; 813199482Srdivacky ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); 814199482Srdivacky for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) 815193326Sed Args[Arg] = TemplateArgs[Arg]; 816193326Sed Tok.setAnnotationValue(TemplateId); 817193326Sed if (TemplateKWLoc.isValid()) 818193326Sed Tok.setLocation(TemplateKWLoc); 819193326Sed else 820193326Sed Tok.setLocation(TemplateNameLoc); 821193326Sed 822193326Sed TemplateArgsPtr.release(); 823193326Sed } 824193326Sed 825193326Sed // Common fields for the annotation token 826193326Sed Tok.setAnnotationEndLoc(RAngleLoc); 827193326Sed 828193326Sed // In case the tokens were cached, have Preprocessor replace them with the 829193326Sed // annotation token. 830193326Sed PP.AnnotateCachedTokens(Tok); 831195099Sed return false; 832193326Sed} 833193326Sed 834193326Sed/// \brief Replaces a template-id annotation token with a type 835193326Sed/// annotation token. 836193326Sed/// 837193326Sed/// If there was a failure when forming the type from the template-id, 838193326Sed/// a type annotation token will still be created, but will have a 839193326Sed/// NULL type pointer to signify an error. 840193326Sedvoid Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) { 841193326Sed assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); 842193326Sed 843198092Srdivacky TemplateIdAnnotation *TemplateId 844193326Sed = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 845193326Sed assert((TemplateId->Kind == TNK_Type_template || 846193326Sed TemplateId->Kind == TNK_Dependent_template_name) && 847193326Sed "Only works for type and dependent templates"); 848198092Srdivacky 849198092Srdivacky ASTTemplateArgsPtr TemplateArgsPtr(Actions, 850193326Sed TemplateId->getTemplateArgs(), 851193326Sed TemplateId->NumArgs); 852193326Sed 853212904Sdim TypeResult Type 854212904Sdim = Actions.ActOnTemplateIdType(TemplateId->Template, 855193326Sed TemplateId->TemplateNameLoc, 856198092Srdivacky TemplateId->LAngleLoc, 857193326Sed TemplateArgsPtr, 858193326Sed TemplateId->RAngleLoc); 859193326Sed // Create the new "type" annotation token. 860193326Sed Tok.setKind(tok::annot_typename); 861212904Sdim setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get()); 862193326Sed if (SS && SS->isNotEmpty()) // it was a C++ qualified type name. 863193326Sed Tok.setLocation(SS->getBeginLoc()); 864203955Srdivacky // End location stays the same 865193326Sed 866198954Srdivacky // Replace the template-id annotation token, and possible the scope-specifier 867198954Srdivacky // that precedes it, with the typename annotation token. 868198954Srdivacky PP.AnnotateCachedTokens(Tok); 869193326Sed TemplateId->Destroy(); 870193326Sed} 871193326Sed 872199482Srdivacky/// \brief Determine whether the given token can end a template argument. 873199482Srdivackystatic bool isEndOfTemplateArgument(Token Tok) { 874199482Srdivacky return Tok.is(tok::comma) || Tok.is(tok::greater) || 875199482Srdivacky Tok.is(tok::greatergreater); 876199482Srdivacky} 877199482Srdivacky 878199482Srdivacky/// \brief Parse a C++ template template argument. 879199482SrdivackyParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { 880199482Srdivacky if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && 881199482Srdivacky !Tok.is(tok::annot_cxxscope)) 882199482Srdivacky return ParsedTemplateArgument(); 883199482Srdivacky 884199482Srdivacky // C++0x [temp.arg.template]p1: 885199482Srdivacky // A template-argument for a template template-parameter shall be the name 886199482Srdivacky // of a class template or a template alias, expressed as id-expression. 887199482Srdivacky // 888199482Srdivacky // We parse an id-expression that refers to a class template or template 889199482Srdivacky // alias. The grammar we parse is: 890199482Srdivacky // 891199482Srdivacky // nested-name-specifier[opt] template[opt] identifier 892199482Srdivacky // 893199482Srdivacky // followed by a token that terminates a template argument, such as ',', 894199482Srdivacky // '>', or (in some cases) '>>'. 895199482Srdivacky CXXScopeSpec SS; // nested-name-specifier, if present 896212904Sdim ParseOptionalCXXScopeSpecifier(SS, ParsedType(), 897199482Srdivacky /*EnteringContext=*/false); 898199482Srdivacky 899199482Srdivacky if (SS.isSet() && Tok.is(tok::kw_template)) { 900199482Srdivacky // Parse the optional 'template' keyword following the 901199482Srdivacky // nested-name-specifier. 902199482Srdivacky SourceLocation TemplateLoc = ConsumeToken(); 903199482Srdivacky 904199482Srdivacky if (Tok.is(tok::identifier)) { 905199482Srdivacky // We appear to have a dependent template name. 906199482Srdivacky UnqualifiedId Name; 907199482Srdivacky Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 908199482Srdivacky ConsumeToken(); // the identifier 909199482Srdivacky 910199482Srdivacky // If the next token signals the end of a template argument, 911199482Srdivacky // then we have a dependent template name that could be a template 912199482Srdivacky // template argument. 913210299Sed TemplateTy Template; 914210299Sed if (isEndOfTemplateArgument(Tok) && 915212904Sdim Actions.ActOnDependentTemplateName(getCurScope(), TemplateLoc, 916212904Sdim SS, Name, 917212904Sdim /*ObjectType=*/ ParsedType(), 918210299Sed /*EnteringContext=*/false, 919210299Sed Template)) 920210299Sed return ParsedTemplateArgument(SS, Template, Name.StartLocation); 921210299Sed } 922199482Srdivacky } else if (Tok.is(tok::identifier)) { 923199482Srdivacky // We may have a (non-dependent) template name. 924199482Srdivacky TemplateTy Template; 925199482Srdivacky UnqualifiedId Name; 926199482Srdivacky Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 927199482Srdivacky ConsumeToken(); // the identifier 928199482Srdivacky 929199482Srdivacky if (isEndOfTemplateArgument(Tok)) { 930208600Srdivacky bool MemberOfUnknownSpecialization; 931212904Sdim TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, 932212904Sdim /*hasTemplateKeyword=*/false, 933212904Sdim Name, 934212904Sdim /*ObjectType=*/ ParsedType(), 935199482Srdivacky /*EnteringContext=*/false, 936208600Srdivacky Template, 937208600Srdivacky MemberOfUnknownSpecialization); 938199482Srdivacky if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { 939199482Srdivacky // We have an id-expression that refers to a class template or 940199482Srdivacky // (C++0x) template alias. 941199482Srdivacky return ParsedTemplateArgument(SS, Template, Name.StartLocation); 942199482Srdivacky } 943199482Srdivacky } 944199482Srdivacky } 945199482Srdivacky 946199482Srdivacky // We don't have a template template argument. 947199482Srdivacky return ParsedTemplateArgument(); 948199482Srdivacky} 949199482Srdivacky 950193326Sed/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). 951193326Sed/// 952193326Sed/// template-argument: [C++ 14.2] 953194711Sed/// constant-expression 954193326Sed/// type-id 955193326Sed/// id-expression 956199482SrdivackyParsedTemplateArgument Parser::ParseTemplateArgument() { 957193326Sed // C++ [temp.arg]p2: 958193326Sed // In a template-argument, an ambiguity between a type-id and an 959193326Sed // expression is resolved to a type-id, regardless of the form of 960193326Sed // the corresponding template-parameter. 961193326Sed // 962199482Srdivacky // Therefore, we initially try to parse a type-id. 963193326Sed if (isCXXTypeId(TypeIdAsTemplateArgument)) { 964199482Srdivacky SourceLocation Loc = Tok.getLocation(); 965193326Sed TypeResult TypeArg = ParseTypeName(); 966193326Sed if (TypeArg.isInvalid()) 967199482Srdivacky return ParsedTemplateArgument(); 968199482Srdivacky 969212904Sdim return ParsedTemplateArgument(ParsedTemplateArgument::Type, 970212904Sdim TypeArg.get().getAsOpaquePtr(), 971199482Srdivacky Loc); 972193326Sed } 973199482Srdivacky 974199482Srdivacky // Try to parse a template template argument. 975199482Srdivacky { 976199482Srdivacky TentativeParsingAction TPA(*this); 977193326Sed 978199482Srdivacky ParsedTemplateArgument TemplateTemplateArgument 979199482Srdivacky = ParseTemplateTemplateArgument(); 980199482Srdivacky if (!TemplateTemplateArgument.isInvalid()) { 981199482Srdivacky TPA.Commit(); 982199482Srdivacky return TemplateTemplateArgument; 983199482Srdivacky } 984199482Srdivacky 985199482Srdivacky // Revert this tentative parse to parse a non-type template argument. 986199482Srdivacky TPA.Revert(); 987199482Srdivacky } 988199482Srdivacky 989199482Srdivacky // Parse a non-type template argument. 990199482Srdivacky SourceLocation Loc = Tok.getLocation(); 991212904Sdim ExprResult ExprArg = ParseConstantExpression(); 992193326Sed if (ExprArg.isInvalid() || !ExprArg.get()) 993199482Srdivacky return ParsedTemplateArgument(); 994193326Sed 995199482Srdivacky return ParsedTemplateArgument(ParsedTemplateArgument::NonType, 996199482Srdivacky ExprArg.release(), Loc); 997193326Sed} 998193326Sed 999208600Srdivacky/// \brief Determine whether the current tokens can only be parsed as a 1000208600Srdivacky/// template argument list (starting with the '<') and never as a '<' 1001208600Srdivacky/// expression. 1002208600Srdivackybool Parser::IsTemplateArgumentList(unsigned Skip) { 1003208600Srdivacky struct AlwaysRevertAction : TentativeParsingAction { 1004208600Srdivacky AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { } 1005208600Srdivacky ~AlwaysRevertAction() { Revert(); } 1006208600Srdivacky } Tentative(*this); 1007208600Srdivacky 1008208600Srdivacky while (Skip) { 1009208600Srdivacky ConsumeToken(); 1010208600Srdivacky --Skip; 1011208600Srdivacky } 1012208600Srdivacky 1013208600Srdivacky // '<' 1014208600Srdivacky if (!Tok.is(tok::less)) 1015208600Srdivacky return false; 1016208600Srdivacky ConsumeToken(); 1017208600Srdivacky 1018208600Srdivacky // An empty template argument list. 1019208600Srdivacky if (Tok.is(tok::greater)) 1020208600Srdivacky return true; 1021208600Srdivacky 1022208600Srdivacky // See whether we have declaration specifiers, which indicate a type. 1023208600Srdivacky while (isCXXDeclarationSpecifier() == TPResult::True()) 1024208600Srdivacky ConsumeToken(); 1025208600Srdivacky 1026208600Srdivacky // If we have a '>' or a ',' then this is a template argument list. 1027208600Srdivacky return Tok.is(tok::greater) || Tok.is(tok::comma); 1028208600Srdivacky} 1029208600Srdivacky 1030193326Sed/// ParseTemplateArgumentList - Parse a C++ template-argument-list 1031193326Sed/// (C++ [temp.names]). Returns true if there was an error. 1032193326Sed/// 1033193326Sed/// template-argument-list: [C++ 14.2] 1034193326Sed/// template-argument 1035193326Sed/// template-argument-list ',' template-argument 1036198092Srdivackybool 1037199482SrdivackyParser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { 1038193326Sed while (true) { 1039199482Srdivacky ParsedTemplateArgument Arg = ParseTemplateArgument(); 1040199482Srdivacky if (Arg.isInvalid()) { 1041193326Sed SkipUntil(tok::comma, tok::greater, true, true); 1042193326Sed return true; 1043193326Sed } 1044193326Sed 1045199482Srdivacky // Save this template argument. 1046199482Srdivacky TemplateArgs.push_back(Arg); 1047199482Srdivacky 1048193326Sed // If the next token is a comma, consume it and keep reading 1049193326Sed // arguments. 1050193326Sed if (Tok.isNot(tok::comma)) break; 1051193326Sed 1052193326Sed // Consume the comma. 1053193326Sed ConsumeToken(); 1054193326Sed } 1055193326Sed 1056201361Srdivacky return false; 1057193326Sed} 1058193326Sed 1059198092Srdivacky/// \brief Parse a C++ explicit template instantiation 1060193326Sed/// (C++ [temp.explicit]). 1061193326Sed/// 1062193326Sed/// explicit-instantiation: 1063198092Srdivacky/// 'extern' [opt] 'template' declaration 1064198092Srdivacky/// 1065198092Srdivacky/// Note that the 'extern' is a GNU extension and C++0x feature. 1066212904SdimDecl *Parser::ParseExplicitInstantiation(SourceLocation ExternLoc, 1067212904Sdim SourceLocation TemplateLoc, 1068212904Sdim SourceLocation &DeclEnd) { 1069212904Sdim // This isn't really required here. 1070212904Sdim ParsingDeclRAIIObject ParsingTemplateParams(*this); 1071212904Sdim 1072198092Srdivacky return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, 1073198092Srdivacky ParsedTemplateInfo(ExternLoc, 1074198092Srdivacky TemplateLoc), 1075212904Sdim ParsingTemplateParams, 1076193326Sed DeclEnd, AS_none); 1077193326Sed} 1078