ParseTemplate.cpp revision 221345
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" 20221345Sdim#include "clang/AST/DeclTemplate.h" 21221345Sdim#include "clang/AST/ASTConsumer.h" 22193326Sedusing namespace clang; 23193326Sed 24193326Sed/// \brief Parse a template declaration, explicit instantiation, or 25193326Sed/// explicit specialization. 26212904SdimDecl * 27193326SedParser::ParseDeclarationStartingWithTemplate(unsigned Context, 28193326Sed SourceLocation &DeclEnd, 29193326Sed AccessSpecifier AS) { 30193326Sed if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) 31198092Srdivacky return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(), 32198092Srdivacky DeclEnd); 33193326Sed 34193326Sed return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS); 35193326Sed} 36193326Sed 37198092Srdivacky/// \brief RAII class that manages the template parameter depth. 38198092Srdivackynamespace { 39199990Srdivacky class TemplateParameterDepthCounter { 40198092Srdivacky unsigned &Depth; 41198092Srdivacky unsigned AddedLevels; 42198092Srdivacky 43198092Srdivacky public: 44198092Srdivacky explicit TemplateParameterDepthCounter(unsigned &Depth) 45198092Srdivacky : Depth(Depth), AddedLevels(0) { } 46198092Srdivacky 47198092Srdivacky ~TemplateParameterDepthCounter() { 48198092Srdivacky Depth -= AddedLevels; 49198092Srdivacky } 50198092Srdivacky 51198092Srdivacky void operator++() { 52198092Srdivacky ++Depth; 53198092Srdivacky ++AddedLevels; 54198092Srdivacky } 55198092Srdivacky 56198092Srdivacky operator unsigned() const { return Depth; } 57198092Srdivacky }; 58198092Srdivacky} 59198092Srdivacky 60193326Sed/// \brief Parse a template declaration or an explicit specialization. 61193326Sed/// 62193326Sed/// Template declarations include one or more template parameter lists 63193326Sed/// and either the function or class template declaration. Explicit 64193326Sed/// specializations contain one or more 'template < >' prefixes 65193326Sed/// followed by a (possibly templated) declaration. Since the 66193326Sed/// syntactic form of both features is nearly identical, we parse all 67193326Sed/// of the template headers together and let semantic analysis sort 68193326Sed/// the declarations from the explicit specializations. 69193326Sed/// 70193326Sed/// template-declaration: [C++ temp] 71193326Sed/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration 72193326Sed/// 73193326Sed/// explicit-specialization: [ C++ temp.expl.spec] 74193326Sed/// 'template' '<' '>' declaration 75212904SdimDecl * 76193326SedParser::ParseTemplateDeclarationOrSpecialization(unsigned Context, 77193326Sed SourceLocation &DeclEnd, 78193326Sed AccessSpecifier AS) { 79198092Srdivacky assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && 80198092Srdivacky "Token does not start a template declaration."); 81198092Srdivacky 82193326Sed // Enter template-parameter scope. 83193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 84193326Sed 85212904Sdim // Tell the action that names should be checked in the context of 86212904Sdim // the declaration to come. 87212904Sdim ParsingDeclRAIIObject ParsingTemplateParams(*this); 88212904Sdim 89193326Sed // Parse multiple levels of template headers within this template 90193326Sed // parameter scope, e.g., 91193326Sed // 92193326Sed // template<typename T> 93193326Sed // template<typename U> 94193326Sed // class A<T>::B { ... }; 95193326Sed // 96193326Sed // We parse multiple levels non-recursively so that we can build a 97193326Sed // single data structure containing all of the template parameter 98193326Sed // lists to easily differentiate between the case above and: 99193326Sed // 100193326Sed // template<typename T> 101193326Sed // class A { 102193326Sed // template<typename U> class B; 103193326Sed // }; 104193326Sed // 105193326Sed // In the first case, the action for declaring A<T>::B receives 106193326Sed // both template parameter lists. In the second case, the action for 107193326Sed // defining A<T>::B receives just the inner template parameter list 108193326Sed // (and retrieves the outer template parameter list from its 109193326Sed // context). 110198092Srdivacky bool isSpecialization = true; 111198893Srdivacky bool LastParamListWasEmpty = false; 112193326Sed TemplateParameterLists ParamLists; 113198092Srdivacky TemplateParameterDepthCounter Depth(TemplateParameterDepth); 114193326Sed do { 115193326Sed // Consume the 'export', if any. 116193326Sed SourceLocation ExportLoc; 117193326Sed if (Tok.is(tok::kw_export)) { 118193326Sed ExportLoc = ConsumeToken(); 119193326Sed } 120193326Sed 121193326Sed // Consume the 'template', which should be here. 122193326Sed SourceLocation TemplateLoc; 123193326Sed if (Tok.is(tok::kw_template)) { 124193326Sed TemplateLoc = ConsumeToken(); 125193326Sed } else { 126193326Sed Diag(Tok.getLocation(), diag::err_expected_template); 127212904Sdim return 0; 128193326Sed } 129198092Srdivacky 130193326Sed // Parse the '<' template-parameter-list '>' 131193326Sed SourceLocation LAngleLoc, RAngleLoc; 132212904Sdim llvm::SmallVector<Decl*, 4> TemplateParams; 133198092Srdivacky if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc, 134198092Srdivacky RAngleLoc)) { 135198092Srdivacky // Skip until the semi-colon or a }. 136198092Srdivacky SkipUntil(tok::r_brace, true, true); 137198092Srdivacky if (Tok.is(tok::semi)) 138198092Srdivacky ConsumeToken(); 139212904Sdim return 0; 140198092Srdivacky } 141193326Sed 142193326Sed ParamLists.push_back( 143198092Srdivacky Actions.ActOnTemplateParameterList(Depth, ExportLoc, 144198092Srdivacky TemplateLoc, LAngleLoc, 145193326Sed TemplateParams.data(), 146193326Sed TemplateParams.size(), RAngleLoc)); 147198092Srdivacky 148198092Srdivacky if (!TemplateParams.empty()) { 149198092Srdivacky isSpecialization = false; 150198092Srdivacky ++Depth; 151198893Srdivacky } else { 152198893Srdivacky LastParamListWasEmpty = true; 153198092Srdivacky } 154193326Sed } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); 155193326Sed 156193326Sed // Parse the actual template declaration. 157198092Srdivacky return ParseSingleDeclarationAfterTemplate(Context, 158193326Sed ParsedTemplateInfo(&ParamLists, 159198893Srdivacky isSpecialization, 160198893Srdivacky LastParamListWasEmpty), 161212904Sdim ParsingTemplateParams, 162193326Sed DeclEnd, AS); 163193326Sed} 164193326Sed 165193326Sed/// \brief Parse a single declaration that declares a template, 166193326Sed/// template specialization, or explicit instantiation of a template. 167193326Sed/// 168193326Sed/// \param TemplateParams if non-NULL, the template parameter lists 169193326Sed/// that preceded this declaration. In this case, the declaration is a 170193326Sed/// template declaration, out-of-line definition of a template, or an 171193326Sed/// explicit template specialization. When NULL, the declaration is an 172193326Sed/// explicit template instantiation. 173193326Sed/// 174193326Sed/// \param TemplateLoc when TemplateParams is NULL, the location of 175193326Sed/// the 'template' keyword that indicates that we have an explicit 176193326Sed/// template instantiation. 177193326Sed/// 178193326Sed/// \param DeclEnd will receive the source location of the last token 179193326Sed/// within this declaration. 180193326Sed/// 181193326Sed/// \param AS the access specifier associated with this 182193326Sed/// declaration. Will be AS_none for namespace-scope declarations. 183193326Sed/// 184193326Sed/// \returns the new declaration. 185212904SdimDecl * 186193326SedParser::ParseSingleDeclarationAfterTemplate( 187193326Sed unsigned Context, 188193326Sed const ParsedTemplateInfo &TemplateInfo, 189212904Sdim ParsingDeclRAIIObject &DiagsFromTParams, 190193326Sed SourceLocation &DeclEnd, 191193326Sed AccessSpecifier AS) { 192193326Sed assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 193193326Sed "Template information required"); 194193326Sed 195198092Srdivacky if (Context == Declarator::MemberContext) { 196198092Srdivacky // We are parsing a member template. 197212904Sdim ParseCXXClassMemberDeclaration(AS, TemplateInfo, &DiagsFromTParams); 198212904Sdim return 0; 199198092Srdivacky } 200198092Srdivacky 201221345Sdim ParsedAttributesWithRange prefixAttrs(AttrFactory); 202218893Sdim MaybeParseCXX0XAttributes(prefixAttrs); 203218893Sdim 204218893Sdim if (Tok.is(tok::kw_using)) 205218893Sdim return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, 206218893Sdim prefixAttrs); 207218893Sdim 208212904Sdim // Parse the declaration specifiers, stealing the accumulated 209212904Sdim // diagnostics from the template parameters. 210221345Sdim ParsingDeclSpec DS(*this, &DiagsFromTParams); 211199990Srdivacky 212218893Sdim DS.takeAttributesFrom(prefixAttrs); 213199990Srdivacky 214202379Srdivacky ParseDeclarationSpecifiers(DS, TemplateInfo, AS, 215202379Srdivacky getDeclSpecContextFromDeclaratorContext(Context)); 216193326Sed 217193326Sed if (Tok.is(tok::semi)) { 218193326Sed DeclEnd = ConsumeToken(); 219212904Sdim Decl *Decl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); 220198893Srdivacky DS.complete(Decl); 221198893Srdivacky return Decl; 222193326Sed } 223193326Sed 224193326Sed // Parse the declarator. 225198893Srdivacky ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context); 226193326Sed ParseDeclarator(DeclaratorInfo); 227193326Sed // Error parsing the declarator? 228193326Sed if (!DeclaratorInfo.hasName()) { 229193326Sed // If so, skip until the semi-colon or a }. 230193326Sed SkipUntil(tok::r_brace, true, true); 231193326Sed if (Tok.is(tok::semi)) 232193326Sed ConsumeToken(); 233212904Sdim return 0; 234193326Sed } 235198092Srdivacky 236193326Sed // If we have a declaration or declarator list, handle it. 237193326Sed if (isDeclarationAfterDeclarator()) { 238193326Sed // Parse this declaration. 239212904Sdim Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, 240212904Sdim TemplateInfo); 241193326Sed 242193326Sed if (Tok.is(tok::comma)) { 243193326Sed Diag(Tok, diag::err_multiple_template_declarators) 244193326Sed << (int)TemplateInfo.Kind; 245193326Sed SkipUntil(tok::semi, true, false); 246193326Sed return ThisDecl; 247193326Sed } 248193326Sed 249193326Sed // Eat the semi colon after the declaration. 250198092Srdivacky ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration); 251218893Sdim DeclaratorInfo.complete(ThisDecl); 252193326Sed return ThisDecl; 253193326Sed } 254193326Sed 255193326Sed if (DeclaratorInfo.isFunctionDeclarator() && 256210299Sed isStartOfFunctionDefinition(DeclaratorInfo)) { 257193326Sed if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 258193326Sed Diag(Tok, diag::err_function_declared_typedef); 259193326Sed 260193326Sed if (Tok.is(tok::l_brace)) { 261193326Sed // This recovery skips the entire function body. It would be nice 262193326Sed // to simply call ParseFunctionDefinition() below, however Sema 263193326Sed // assumes the declarator represents a function, not a typedef. 264193326Sed ConsumeBrace(); 265193326Sed SkipUntil(tok::r_brace, true); 266193326Sed } else { 267193326Sed SkipUntil(tok::semi); 268193326Sed } 269212904Sdim return 0; 270193326Sed } 271195099Sed return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo); 272193326Sed } 273193326Sed 274193326Sed if (DeclaratorInfo.isFunctionDeclarator()) 275193326Sed Diag(Tok, diag::err_expected_fn_body); 276193326Sed else 277193326Sed Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); 278193326Sed SkipUntil(tok::semi); 279212904Sdim return 0; 280193326Sed} 281193326Sed 282193326Sed/// ParseTemplateParameters - Parses a template-parameter-list enclosed in 283193326Sed/// angle brackets. Depth is the depth of this template-parameter-list, which 284193326Sed/// is the number of template headers directly enclosing this template header. 285193326Sed/// TemplateParams is the current list of template parameters we're building. 286193326Sed/// The template parameter we parse will be added to this list. LAngleLoc and 287198092Srdivacky/// RAngleLoc will receive the positions of the '<' and '>', respectively, 288193326Sed/// that enclose this template parameter list. 289198092Srdivacky/// 290198092Srdivacky/// \returns true if an error occurred, false otherwise. 291193326Sedbool Parser::ParseTemplateParameters(unsigned Depth, 292212904Sdim llvm::SmallVectorImpl<Decl*> &TemplateParams, 293193326Sed SourceLocation &LAngleLoc, 294193326Sed SourceLocation &RAngleLoc) { 295193326Sed // Get the template parameter list. 296198092Srdivacky if (!Tok.is(tok::less)) { 297193326Sed Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; 298198092Srdivacky return true; 299193326Sed } 300193326Sed LAngleLoc = ConsumeToken(); 301198092Srdivacky 302193326Sed // Try to parse the template parameter list. 303193326Sed if (Tok.is(tok::greater)) 304193326Sed RAngleLoc = ConsumeToken(); 305198092Srdivacky else if (ParseTemplateParameterList(Depth, TemplateParams)) { 306198092Srdivacky if (!Tok.is(tok::greater)) { 307193326Sed Diag(Tok.getLocation(), diag::err_expected_greater); 308198092Srdivacky return true; 309193326Sed } 310193326Sed RAngleLoc = ConsumeToken(); 311193326Sed } 312198092Srdivacky return false; 313193326Sed} 314193326Sed 315193326Sed/// ParseTemplateParameterList - Parse a template parameter list. If 316193326Sed/// the parsing fails badly (i.e., closing bracket was left out), this 317193326Sed/// will try to put the token stream in a reasonable position (closing 318198092Srdivacky/// a statement, etc.) and return false. 319193326Sed/// 320193326Sed/// template-parameter-list: [C++ temp] 321193326Sed/// template-parameter 322193326Sed/// template-parameter-list ',' template-parameter 323198092Srdivackybool 324193326SedParser::ParseTemplateParameterList(unsigned Depth, 325212904Sdim llvm::SmallVectorImpl<Decl*> &TemplateParams) { 326198092Srdivacky while (1) { 327212904Sdim if (Decl *TmpParam 328193326Sed = ParseTemplateParameter(Depth, TemplateParams.size())) { 329193326Sed TemplateParams.push_back(TmpParam); 330193326Sed } else { 331193326Sed // If we failed to parse a template parameter, skip until we find 332193326Sed // a comma or closing brace. 333193326Sed SkipUntil(tok::comma, tok::greater, true, true); 334193326Sed } 335198092Srdivacky 336193326Sed // Did we find a comma or the end of the template parmeter list? 337198092Srdivacky if (Tok.is(tok::comma)) { 338193326Sed ConsumeToken(); 339198092Srdivacky } else if (Tok.is(tok::greater)) { 340193326Sed // Don't consume this... that's done by template parser. 341193326Sed break; 342193326Sed } else { 343193326Sed // Somebody probably forgot to close the template. Skip ahead and 344193326Sed // try to get out of the expression. This error is currently 345193326Sed // subsumed by whatever goes on in ParseTemplateParameter. 346193326Sed // TODO: This could match >>, and it would be nice to avoid those 347193326Sed // silly errors with template <vec<T>>. 348218893Sdim Diag(Tok.getLocation(), diag::err_expected_comma_greater); 349193326Sed SkipUntil(tok::greater, true, true); 350193326Sed return false; 351193326Sed } 352193326Sed } 353193326Sed return true; 354193326Sed} 355193326Sed 356199990Srdivacky/// \brief Determine whether the parser is at the start of a template 357199990Srdivacky/// type parameter. 358199990Srdivackybool Parser::isStartOfTemplateTypeParameter() { 359210299Sed if (Tok.is(tok::kw_class)) { 360210299Sed // "class" may be the start of an elaborated-type-specifier or a 361210299Sed // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. 362210299Sed switch (NextToken().getKind()) { 363210299Sed case tok::equal: 364210299Sed case tok::comma: 365210299Sed case tok::greater: 366210299Sed case tok::greatergreater: 367210299Sed case tok::ellipsis: 368210299Sed return true; 369210299Sed 370210299Sed case tok::identifier: 371210299Sed // This may be either a type-parameter or an elaborated-type-specifier. 372210299Sed // We have to look further. 373210299Sed break; 374210299Sed 375210299Sed default: 376210299Sed return false; 377210299Sed } 378210299Sed 379210299Sed switch (GetLookAheadToken(2).getKind()) { 380210299Sed case tok::equal: 381210299Sed case tok::comma: 382210299Sed case tok::greater: 383210299Sed case tok::greatergreater: 384210299Sed return true; 385210299Sed 386210299Sed default: 387210299Sed return false; 388210299Sed } 389210299Sed } 390199990Srdivacky 391199990Srdivacky if (Tok.isNot(tok::kw_typename)) 392199990Srdivacky return false; 393199990Srdivacky 394199990Srdivacky // C++ [temp.param]p2: 395199990Srdivacky // There is no semantic difference between class and typename in a 396199990Srdivacky // template-parameter. typename followed by an unqualified-id 397199990Srdivacky // names a template type parameter. typename followed by a 398199990Srdivacky // qualified-id denotes the type in a non-type 399199990Srdivacky // parameter-declaration. 400199990Srdivacky Token Next = NextToken(); 401199990Srdivacky 402199990Srdivacky // If we have an identifier, skip over it. 403199990Srdivacky if (Next.getKind() == tok::identifier) 404199990Srdivacky Next = GetLookAheadToken(2); 405199990Srdivacky 406199990Srdivacky switch (Next.getKind()) { 407199990Srdivacky case tok::equal: 408199990Srdivacky case tok::comma: 409199990Srdivacky case tok::greater: 410199990Srdivacky case tok::greatergreater: 411199990Srdivacky case tok::ellipsis: 412199990Srdivacky return true; 413199990Srdivacky 414199990Srdivacky default: 415199990Srdivacky return false; 416199990Srdivacky } 417199990Srdivacky} 418199990Srdivacky 419193326Sed/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). 420193326Sed/// 421193326Sed/// template-parameter: [C++ temp.param] 422193326Sed/// type-parameter 423193326Sed/// parameter-declaration 424193326Sed/// 425193326Sed/// type-parameter: (see below) 426218893Sdim/// 'class' ...[opt] identifier[opt] 427193326Sed/// 'class' identifier[opt] '=' type-id 428218893Sdim/// 'typename' ...[opt] identifier[opt] 429193326Sed/// 'typename' identifier[opt] '=' type-id 430218893Sdim/// 'template' '<' template-parameter-list '>' 431218893Sdim/// 'class' ...[opt] identifier[opt] 432218893Sdim/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 433218893Sdim/// = id-expression 434212904SdimDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { 435199990Srdivacky if (isStartOfTemplateTypeParameter()) 436193326Sed return ParseTypeParameter(Depth, Position); 437198092Srdivacky 438198092Srdivacky if (Tok.is(tok::kw_template)) 439193326Sed return ParseTemplateTemplateParameter(Depth, Position); 440193326Sed 441193326Sed // If it's none of the above, then it must be a parameter declaration. 442193326Sed // NOTE: This will pick up errors in the closure of the template parameter 443193326Sed // list (e.g., template < ; Check here to implement >> style closures. 444193326Sed return ParseNonTypeTemplateParameter(Depth, Position); 445193326Sed} 446193326Sed 447193326Sed/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). 448193326Sed/// Other kinds of template parameters are parsed in 449193326Sed/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. 450193326Sed/// 451193326Sed/// type-parameter: [C++ temp.param] 452194179Sed/// 'class' ...[opt][C++0x] identifier[opt] 453193326Sed/// 'class' identifier[opt] '=' type-id 454194179Sed/// 'typename' ...[opt][C++0x] identifier[opt] 455193326Sed/// 'typename' identifier[opt] '=' type-id 456212904SdimDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { 457193326Sed assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && 458198092Srdivacky "A type-parameter starts with 'class' or 'typename'"); 459193326Sed 460193326Sed // Consume the 'class' or 'typename' keyword. 461193326Sed bool TypenameKeyword = Tok.is(tok::kw_typename); 462193326Sed SourceLocation KeyLoc = ConsumeToken(); 463193326Sed 464194179Sed // Grab the ellipsis (if given). 465194179Sed bool Ellipsis = false; 466194179Sed SourceLocation EllipsisLoc; 467194179Sed if (Tok.is(tok::ellipsis)) { 468194179Sed Ellipsis = true; 469194179Sed EllipsisLoc = ConsumeToken(); 470198092Srdivacky 471198092Srdivacky if (!getLang().CPlusPlus0x) 472218893Sdim Diag(EllipsisLoc, diag::ext_variadic_templates); 473194179Sed } 474198092Srdivacky 475193326Sed // Grab the template parameter name (if given) 476193326Sed SourceLocation NameLoc; 477193326Sed IdentifierInfo* ParamName = 0; 478198092Srdivacky if (Tok.is(tok::identifier)) { 479193326Sed ParamName = Tok.getIdentifierInfo(); 480193326Sed NameLoc = ConsumeToken(); 481198092Srdivacky } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || 482198092Srdivacky Tok.is(tok::greater)) { 483193326Sed // Unnamed template parameter. Don't have to do anything here, just 484193326Sed // don't consume this token. 485193326Sed } else { 486193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 487212904Sdim return 0; 488193326Sed } 489198092Srdivacky 490210299Sed // Grab a default argument (if available). 491210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 492210299Sed // we introduce the type parameter into the local scope. 493210299Sed SourceLocation EqualLoc; 494212904Sdim ParsedType DefaultArg; 495198092Srdivacky if (Tok.is(tok::equal)) { 496210299Sed EqualLoc = ConsumeToken(); 497210299Sed DefaultArg = ParseTypeName().get(); 498193326Sed } 499210299Sed 500210299Sed return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis, 501210299Sed EllipsisLoc, KeyLoc, ParamName, NameLoc, 502210299Sed Depth, Position, EqualLoc, DefaultArg); 503193326Sed} 504193326Sed 505193326Sed/// ParseTemplateTemplateParameter - Handle the parsing of template 506198092Srdivacky/// template parameters. 507193326Sed/// 508193326Sed/// type-parameter: [C++ temp.param] 509218893Sdim/// 'template' '<' template-parameter-list '>' 'class' 510218893Sdim/// ...[opt] identifier[opt] 511218893Sdim/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 512218893Sdim/// = id-expression 513212904SdimDecl * 514193326SedParser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { 515193326Sed assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); 516193326Sed 517193326Sed // Handle the template <...> part. 518193326Sed SourceLocation TemplateLoc = ConsumeToken(); 519212904Sdim llvm::SmallVector<Decl*,8> TemplateParams; 520193326Sed SourceLocation LAngleLoc, RAngleLoc; 521193326Sed { 522193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 523198092Srdivacky if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, 524198092Srdivacky RAngleLoc)) { 525212904Sdim return 0; 526193326Sed } 527193326Sed } 528193326Sed 529193326Sed // Generate a meaningful error if the user forgot to put class before the 530193326Sed // identifier, comma, or greater. 531198092Srdivacky if (!Tok.is(tok::kw_class)) { 532198092Srdivacky Diag(Tok.getLocation(), diag::err_expected_class_before) 533193326Sed << PP.getSpelling(Tok); 534212904Sdim return 0; 535193326Sed } 536218893Sdim ConsumeToken(); 537193326Sed 538218893Sdim // Parse the ellipsis, if given. 539218893Sdim SourceLocation EllipsisLoc; 540218893Sdim if (Tok.is(tok::ellipsis)) { 541218893Sdim EllipsisLoc = ConsumeToken(); 542218893Sdim 543218893Sdim if (!getLang().CPlusPlus0x) 544218893Sdim Diag(EllipsisLoc, diag::ext_variadic_templates); 545218893Sdim } 546218893Sdim 547193326Sed // Get the identifier, if given. 548193326Sed SourceLocation NameLoc; 549193326Sed IdentifierInfo* ParamName = 0; 550198092Srdivacky if (Tok.is(tok::identifier)) { 551193326Sed ParamName = Tok.getIdentifierInfo(); 552193326Sed NameLoc = ConsumeToken(); 553198092Srdivacky } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { 554193326Sed // Unnamed template parameter. Don't have to do anything here, just 555193326Sed // don't consume this token. 556193326Sed } else { 557193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 558212904Sdim return 0; 559193326Sed } 560193326Sed 561198092Srdivacky TemplateParamsTy *ParamList = 562193326Sed Actions.ActOnTemplateParameterList(Depth, SourceLocation(), 563193326Sed TemplateLoc, LAngleLoc, 564218893Sdim TemplateParams.data(), 565193326Sed TemplateParams.size(), 566193326Sed RAngleLoc); 567193326Sed 568210299Sed // Grab a default argument (if available). 569210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 570210299Sed // we introduce the template parameter into the local scope. 571210299Sed SourceLocation EqualLoc; 572210299Sed ParsedTemplateArgument DefaultArg; 573193326Sed if (Tok.is(tok::equal)) { 574210299Sed EqualLoc = ConsumeToken(); 575210299Sed DefaultArg = ParseTemplateTemplateArgument(); 576210299Sed if (DefaultArg.isInvalid()) { 577199482Srdivacky Diag(Tok.getLocation(), 578199482Srdivacky diag::err_default_template_template_parameter_not_template); 579200583Srdivacky static const tok::TokenKind EndToks[] = { 580199482Srdivacky tok::comma, tok::greater, tok::greatergreater 581199482Srdivacky }; 582199482Srdivacky SkipUntil(EndToks, 3, true, true); 583210299Sed } 584193326Sed } 585210299Sed 586210299Sed return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, 587218893Sdim ParamList, EllipsisLoc, 588218893Sdim ParamName, NameLoc, Depth, 589218893Sdim Position, EqualLoc, DefaultArg); 590193326Sed} 591193326Sed 592193326Sed/// ParseNonTypeTemplateParameter - Handle the parsing of non-type 593198092Srdivacky/// template parameters (e.g., in "template<int Size> class array;"). 594193326Sed/// 595193326Sed/// template-parameter: 596193326Sed/// ... 597193326Sed/// parameter-declaration 598212904SdimDecl * 599193326SedParser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { 600193326Sed // Parse the declaration-specifiers (i.e., the type). 601193326Sed // FIXME: The type should probably be restricted in some way... Not all 602193326Sed // declarators (parts of declarators?) are accepted for parameters. 603221345Sdim DeclSpec DS(AttrFactory); 604193326Sed ParseDeclarationSpecifiers(DS); 605193326Sed 606193326Sed // Parse this as a typename. 607193326Sed Declarator ParamDecl(DS, Declarator::TemplateParamContext); 608193326Sed ParseDeclarator(ParamDecl); 609212904Sdim if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { 610193326Sed // This probably shouldn't happen - and it's more of a Sema thing, but 611193326Sed // basically we didn't parse the type name because we couldn't associate 612193326Sed // it with an AST node. we should just skip to the comma or greater. 613193326Sed // TODO: This is currently a placeholder for some kind of Sema Error. 614193326Sed Diag(Tok.getLocation(), diag::err_parse_error); 615193326Sed SkipUntil(tok::comma, tok::greater, true, true); 616212904Sdim return 0; 617193326Sed } 618193326Sed 619193326Sed // If there is a default value, parse it. 620210299Sed // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before 621210299Sed // we introduce the template parameter into the local scope. 622210299Sed SourceLocation EqualLoc; 623212904Sdim ExprResult DefaultArg; 624193326Sed if (Tok.is(tok::equal)) { 625210299Sed EqualLoc = ConsumeToken(); 626193326Sed 627193326Sed // C++ [temp.param]p15: 628193326Sed // When parsing a default template-argument for a non-type 629193326Sed // template-parameter, the first non-nested > is taken as the 630193326Sed // end of the template-parameter-list rather than a greater-than 631193326Sed // operator. 632198092Srdivacky GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 633193326Sed 634210299Sed DefaultArg = ParseAssignmentExpression(); 635193326Sed if (DefaultArg.isInvalid()) 636193326Sed SkipUntil(tok::comma, tok::greater, true, true); 637193326Sed } 638198092Srdivacky 639210299Sed // Create the parameter. 640210299Sed return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, 641210299Sed Depth, Position, EqualLoc, 642212904Sdim DefaultArg.take()); 643193326Sed} 644193326Sed 645193326Sed/// \brief Parses a template-id that after the template name has 646193326Sed/// already been parsed. 647193326Sed/// 648193326Sed/// This routine takes care of parsing the enclosed template argument 649193326Sed/// list ('<' template-parameter-list [opt] '>') and placing the 650193326Sed/// results into a form that can be transferred to semantic analysis. 651193326Sed/// 652193326Sed/// \param Template the template declaration produced by isTemplateName 653193326Sed/// 654193326Sed/// \param TemplateNameLoc the source location of the template name 655193326Sed/// 656193326Sed/// \param SS if non-NULL, the nested-name-specifier preceding the 657193326Sed/// template name. 658193326Sed/// 659193326Sed/// \param ConsumeLastToken if true, then we will consume the last 660193326Sed/// token that forms the template-id. Otherwise, we will leave the 661193326Sed/// last token in the stream (e.g., so that it can be replaced with an 662193326Sed/// annotation token). 663198092Srdivackybool 664193326SedParser::ParseTemplateIdAfterTemplateName(TemplateTy Template, 665198092Srdivacky SourceLocation TemplateNameLoc, 666221345Sdim const CXXScopeSpec &SS, 667193326Sed bool ConsumeLastToken, 668193326Sed SourceLocation &LAngleLoc, 669193326Sed TemplateArgList &TemplateArgs, 670193326Sed SourceLocation &RAngleLoc) { 671193326Sed assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 672193326Sed 673193326Sed // Consume the '<'. 674193326Sed LAngleLoc = ConsumeToken(); 675193326Sed 676193326Sed // Parse the optional template-argument-list. 677193326Sed bool Invalid = false; 678193326Sed { 679193326Sed GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 680218893Sdim if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) 681199482Srdivacky Invalid = ParseTemplateArgumentList(TemplateArgs); 682193326Sed 683193326Sed if (Invalid) { 684193326Sed // Try to find the closing '>'. 685193326Sed SkipUntil(tok::greater, true, !ConsumeLastToken); 686193326Sed 687193326Sed return true; 688193326Sed } 689193326Sed } 690193326Sed 691201361Srdivacky if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) { 692201361Srdivacky Diag(Tok.getLocation(), diag::err_expected_greater); 693193326Sed return true; 694201361Srdivacky } 695193326Sed 696193326Sed // Determine the location of the '>' or '>>'. Only consume this 697193326Sed // token if the caller asked us to. 698193326Sed RAngleLoc = Tok.getLocation(); 699193326Sed 700193326Sed if (Tok.is(tok::greatergreater)) { 701193326Sed if (!getLang().CPlusPlus0x) { 702193326Sed const char *ReplaceStr = "> >"; 703193326Sed if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater)) 704193326Sed ReplaceStr = "> > "; 705193326Sed 706193326Sed Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space) 707206084Srdivacky << FixItHint::CreateReplacement( 708193326Sed SourceRange(Tok.getLocation()), ReplaceStr); 709193326Sed } 710193326Sed 711193326Sed Tok.setKind(tok::greater); 712193326Sed if (!ConsumeLastToken) { 713193326Sed // Since we're not supposed to consume the '>>' token, we need 714193326Sed // to insert a second '>' token after the first. 715193326Sed PP.EnterToken(Tok); 716193326Sed } 717193326Sed } else if (ConsumeLastToken) 718193326Sed ConsumeToken(); 719193326Sed 720193326Sed return false; 721193326Sed} 722198092Srdivacky 723193326Sed/// \brief Replace the tokens that form a simple-template-id with an 724193326Sed/// annotation token containing the complete template-id. 725193326Sed/// 726193326Sed/// The first token in the stream must be the name of a template that 727193326Sed/// is followed by a '<'. This routine will parse the complete 728193326Sed/// simple-template-id and replace the tokens with a single annotation 729193326Sed/// token with one of two different kinds: if the template-id names a 730193326Sed/// type (and \p AllowTypeAnnotation is true), the annotation token is 731193326Sed/// a type annotation that includes the optional nested-name-specifier 732193326Sed/// (\p SS). Otherwise, the annotation token is a template-id 733193326Sed/// annotation that does not include the optional 734193326Sed/// nested-name-specifier. 735193326Sed/// 736193326Sed/// \param Template the declaration of the template named by the first 737193326Sed/// token (an identifier), as returned from \c Action::isTemplateName(). 738193326Sed/// 739193326Sed/// \param TemplateNameKind the kind of template that \p Template 740193326Sed/// refers to, as returned from \c Action::isTemplateName(). 741193326Sed/// 742193326Sed/// \param SS if non-NULL, the nested-name-specifier that precedes 743193326Sed/// this template name. 744193326Sed/// 745193326Sed/// \param TemplateKWLoc if valid, specifies that this template-id 746193326Sed/// annotation was preceded by the 'template' keyword and gives the 747193326Sed/// location of that keyword. If invalid (the default), then this 748193326Sed/// template-id was not preceded by a 'template' keyword. 749193326Sed/// 750193326Sed/// \param AllowTypeAnnotation if true (the default), then a 751193326Sed/// simple-template-id that refers to a class template, template 752193326Sed/// template parameter, or other template that produces a type will be 753193326Sed/// replaced with a type annotation token. Otherwise, the 754193326Sed/// simple-template-id is always replaced with a template-id 755193326Sed/// annotation token. 756195099Sed/// 757195099Sed/// If an unrecoverable parse error occurs and no annotation token can be 758195099Sed/// formed, this function returns true. 759195099Sed/// 760195099Sedbool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, 761221345Sdim CXXScopeSpec &SS, 762198893Srdivacky UnqualifiedId &TemplateName, 763193326Sed SourceLocation TemplateKWLoc, 764193326Sed bool AllowTypeAnnotation) { 765193326Sed assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); 766198893Srdivacky assert(Template && Tok.is(tok::less) && 767193326Sed "Parser isn't at the beginning of a template-id"); 768193326Sed 769193326Sed // Consume the template-name. 770198893Srdivacky SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); 771193326Sed 772193326Sed // Parse the enclosed template argument list. 773193326Sed SourceLocation LAngleLoc, RAngleLoc; 774193326Sed TemplateArgList TemplateArgs; 775198893Srdivacky bool Invalid = ParseTemplateIdAfterTemplateName(Template, 776198893Srdivacky TemplateNameLoc, 777198092Srdivacky SS, false, LAngleLoc, 778198092Srdivacky TemplateArgs, 779193326Sed RAngleLoc); 780198092Srdivacky 781195099Sed if (Invalid) { 782195099Sed // If we failed to parse the template ID but skipped ahead to a >, we're not 783195099Sed // going to be able to form a token annotation. Eat the '>' if present. 784195099Sed if (Tok.is(tok::greater)) 785195099Sed ConsumeToken(); 786195099Sed return true; 787195099Sed } 788193326Sed 789193326Sed ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), 790193326Sed TemplateArgs.size()); 791193326Sed 792193326Sed // Build the annotation token. 793193326Sed if (TNK == TNK_Type_template && AllowTypeAnnotation) { 794212904Sdim TypeResult Type 795221345Sdim = Actions.ActOnTemplateIdType(SS, 796221345Sdim Template, TemplateNameLoc, 797193326Sed LAngleLoc, TemplateArgsPtr, 798193326Sed RAngleLoc); 799195099Sed if (Type.isInvalid()) { 800195099Sed // If we failed to parse the template ID but skipped ahead to a >, we're not 801195099Sed // going to be able to form a token annotation. Eat the '>' if present. 802195099Sed if (Tok.is(tok::greater)) 803195099Sed ConsumeToken(); 804195099Sed return true; 805195099Sed } 806193326Sed 807193326Sed Tok.setKind(tok::annot_typename); 808212904Sdim setTypeAnnotation(Tok, Type.get()); 809221345Sdim if (SS.isNotEmpty()) 810221345Sdim Tok.setLocation(SS.getBeginLoc()); 811193326Sed else if (TemplateKWLoc.isValid()) 812193326Sed Tok.setLocation(TemplateKWLoc); 813198092Srdivacky else 814193326Sed Tok.setLocation(TemplateNameLoc); 815193326Sed } else { 816193326Sed // Build a template-id annotation token that can be processed 817193326Sed // later. 818193326Sed Tok.setKind(tok::annot_template_id); 819198092Srdivacky TemplateIdAnnotation *TemplateId 820193326Sed = TemplateIdAnnotation::Allocate(TemplateArgs.size()); 821193326Sed TemplateId->TemplateNameLoc = TemplateNameLoc; 822198893Srdivacky if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) { 823198893Srdivacky TemplateId->Name = TemplateName.Identifier; 824198893Srdivacky TemplateId->Operator = OO_None; 825198893Srdivacky } else { 826198893Srdivacky TemplateId->Name = 0; 827198893Srdivacky TemplateId->Operator = TemplateName.OperatorFunctionId.Operator; 828198893Srdivacky } 829221345Sdim TemplateId->SS = SS; 830212904Sdim TemplateId->Template = Template; 831193326Sed TemplateId->Kind = TNK; 832193326Sed TemplateId->LAngleLoc = LAngleLoc; 833193326Sed TemplateId->RAngleLoc = RAngleLoc; 834199482Srdivacky ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); 835199482Srdivacky for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) 836219077Sdim Args[Arg] = ParsedTemplateArgument(TemplateArgs[Arg]); 837193326Sed Tok.setAnnotationValue(TemplateId); 838193326Sed if (TemplateKWLoc.isValid()) 839193326Sed Tok.setLocation(TemplateKWLoc); 840193326Sed else 841193326Sed Tok.setLocation(TemplateNameLoc); 842193326Sed 843193326Sed TemplateArgsPtr.release(); 844193326Sed } 845193326Sed 846193326Sed // Common fields for the annotation token 847193326Sed Tok.setAnnotationEndLoc(RAngleLoc); 848193326Sed 849193326Sed // In case the tokens were cached, have Preprocessor replace them with the 850193326Sed // annotation token. 851193326Sed PP.AnnotateCachedTokens(Tok); 852195099Sed return false; 853193326Sed} 854193326Sed 855193326Sed/// \brief Replaces a template-id annotation token with a type 856193326Sed/// annotation token. 857193326Sed/// 858193326Sed/// If there was a failure when forming the type from the template-id, 859193326Sed/// a type annotation token will still be created, but will have a 860193326Sed/// NULL type pointer to signify an error. 861221345Sdimvoid Parser::AnnotateTemplateIdTokenAsType() { 862193326Sed assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); 863193326Sed 864198092Srdivacky TemplateIdAnnotation *TemplateId 865193326Sed = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 866193326Sed assert((TemplateId->Kind == TNK_Type_template || 867193326Sed TemplateId->Kind == TNK_Dependent_template_name) && 868193326Sed "Only works for type and dependent templates"); 869198092Srdivacky 870198092Srdivacky ASTTemplateArgsPtr TemplateArgsPtr(Actions, 871193326Sed TemplateId->getTemplateArgs(), 872193326Sed TemplateId->NumArgs); 873193326Sed 874212904Sdim TypeResult Type 875221345Sdim = Actions.ActOnTemplateIdType(TemplateId->SS, 876221345Sdim TemplateId->Template, 877193326Sed TemplateId->TemplateNameLoc, 878198092Srdivacky TemplateId->LAngleLoc, 879193326Sed TemplateArgsPtr, 880193326Sed TemplateId->RAngleLoc); 881193326Sed // Create the new "type" annotation token. 882193326Sed Tok.setKind(tok::annot_typename); 883212904Sdim setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get()); 884221345Sdim if (TemplateId->SS.isNotEmpty()) // it was a C++ qualified type name. 885221345Sdim Tok.setLocation(TemplateId->SS.getBeginLoc()); 886203955Srdivacky // End location stays the same 887193326Sed 888198954Srdivacky // Replace the template-id annotation token, and possible the scope-specifier 889198954Srdivacky // that precedes it, with the typename annotation token. 890198954Srdivacky PP.AnnotateCachedTokens(Tok); 891193326Sed TemplateId->Destroy(); 892193326Sed} 893193326Sed 894199482Srdivacky/// \brief Determine whether the given token can end a template argument. 895199482Srdivackystatic bool isEndOfTemplateArgument(Token Tok) { 896199482Srdivacky return Tok.is(tok::comma) || Tok.is(tok::greater) || 897199482Srdivacky Tok.is(tok::greatergreater); 898199482Srdivacky} 899199482Srdivacky 900199482Srdivacky/// \brief Parse a C++ template template argument. 901199482SrdivackyParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { 902199482Srdivacky if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && 903199482Srdivacky !Tok.is(tok::annot_cxxscope)) 904199482Srdivacky return ParsedTemplateArgument(); 905199482Srdivacky 906199482Srdivacky // C++0x [temp.arg.template]p1: 907199482Srdivacky // A template-argument for a template template-parameter shall be the name 908199482Srdivacky // of a class template or a template alias, expressed as id-expression. 909199482Srdivacky // 910199482Srdivacky // We parse an id-expression that refers to a class template or template 911199482Srdivacky // alias. The grammar we parse is: 912199482Srdivacky // 913218893Sdim // nested-name-specifier[opt] template[opt] identifier ...[opt] 914199482Srdivacky // 915199482Srdivacky // followed by a token that terminates a template argument, such as ',', 916199482Srdivacky // '>', or (in some cases) '>>'. 917199482Srdivacky CXXScopeSpec SS; // nested-name-specifier, if present 918212904Sdim ParseOptionalCXXScopeSpecifier(SS, ParsedType(), 919199482Srdivacky /*EnteringContext=*/false); 920199482Srdivacky 921218893Sdim ParsedTemplateArgument Result; 922218893Sdim SourceLocation EllipsisLoc; 923199482Srdivacky if (SS.isSet() && Tok.is(tok::kw_template)) { 924199482Srdivacky // Parse the optional 'template' keyword following the 925199482Srdivacky // nested-name-specifier. 926199482Srdivacky SourceLocation TemplateLoc = ConsumeToken(); 927199482Srdivacky 928199482Srdivacky if (Tok.is(tok::identifier)) { 929199482Srdivacky // We appear to have a dependent template name. 930199482Srdivacky UnqualifiedId Name; 931199482Srdivacky Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 932199482Srdivacky ConsumeToken(); // the identifier 933199482Srdivacky 934218893Sdim // Parse the ellipsis. 935218893Sdim if (Tok.is(tok::ellipsis)) 936218893Sdim EllipsisLoc = ConsumeToken(); 937218893Sdim 938199482Srdivacky // If the next token signals the end of a template argument, 939199482Srdivacky // then we have a dependent template name that could be a template 940199482Srdivacky // template argument. 941210299Sed TemplateTy Template; 942210299Sed if (isEndOfTemplateArgument(Tok) && 943212904Sdim Actions.ActOnDependentTemplateName(getCurScope(), TemplateLoc, 944212904Sdim SS, Name, 945212904Sdim /*ObjectType=*/ ParsedType(), 946210299Sed /*EnteringContext=*/false, 947210299Sed Template)) 948218893Sdim Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 949210299Sed } 950199482Srdivacky } else if (Tok.is(tok::identifier)) { 951199482Srdivacky // We may have a (non-dependent) template name. 952199482Srdivacky TemplateTy Template; 953199482Srdivacky UnqualifiedId Name; 954199482Srdivacky Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 955199482Srdivacky ConsumeToken(); // the identifier 956199482Srdivacky 957218893Sdim // Parse the ellipsis. 958218893Sdim if (Tok.is(tok::ellipsis)) 959218893Sdim EllipsisLoc = ConsumeToken(); 960218893Sdim 961199482Srdivacky if (isEndOfTemplateArgument(Tok)) { 962208600Srdivacky bool MemberOfUnknownSpecialization; 963212904Sdim TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, 964212904Sdim /*hasTemplateKeyword=*/false, 965212904Sdim Name, 966212904Sdim /*ObjectType=*/ ParsedType(), 967199482Srdivacky /*EnteringContext=*/false, 968208600Srdivacky Template, 969208600Srdivacky MemberOfUnknownSpecialization); 970199482Srdivacky if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { 971199482Srdivacky // We have an id-expression that refers to a class template or 972199482Srdivacky // (C++0x) template alias. 973218893Sdim Result = ParsedTemplateArgument(SS, Template, Name.StartLocation); 974199482Srdivacky } 975199482Srdivacky } 976199482Srdivacky } 977199482Srdivacky 978218893Sdim // If this is a pack expansion, build it as such. 979218893Sdim if (EllipsisLoc.isValid() && !Result.isInvalid()) 980218893Sdim Result = Actions.ActOnPackExpansion(Result, EllipsisLoc); 981218893Sdim 982218893Sdim return Result; 983199482Srdivacky} 984199482Srdivacky 985193326Sed/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). 986193326Sed/// 987193326Sed/// template-argument: [C++ 14.2] 988194711Sed/// constant-expression 989193326Sed/// type-id 990193326Sed/// id-expression 991199482SrdivackyParsedTemplateArgument Parser::ParseTemplateArgument() { 992193326Sed // C++ [temp.arg]p2: 993193326Sed // In a template-argument, an ambiguity between a type-id and an 994193326Sed // expression is resolved to a type-id, regardless of the form of 995193326Sed // the corresponding template-parameter. 996193326Sed // 997199482Srdivacky // Therefore, we initially try to parse a type-id. 998193326Sed if (isCXXTypeId(TypeIdAsTemplateArgument)) { 999199482Srdivacky SourceLocation Loc = Tok.getLocation(); 1000218893Sdim TypeResult TypeArg = ParseTypeName(/*Range=*/0, 1001218893Sdim Declarator::TemplateTypeArgContext); 1002193326Sed if (TypeArg.isInvalid()) 1003199482Srdivacky return ParsedTemplateArgument(); 1004199482Srdivacky 1005212904Sdim return ParsedTemplateArgument(ParsedTemplateArgument::Type, 1006212904Sdim TypeArg.get().getAsOpaquePtr(), 1007199482Srdivacky Loc); 1008193326Sed } 1009199482Srdivacky 1010199482Srdivacky // Try to parse a template template argument. 1011199482Srdivacky { 1012199482Srdivacky TentativeParsingAction TPA(*this); 1013193326Sed 1014199482Srdivacky ParsedTemplateArgument TemplateTemplateArgument 1015199482Srdivacky = ParseTemplateTemplateArgument(); 1016199482Srdivacky if (!TemplateTemplateArgument.isInvalid()) { 1017199482Srdivacky TPA.Commit(); 1018199482Srdivacky return TemplateTemplateArgument; 1019199482Srdivacky } 1020199482Srdivacky 1021199482Srdivacky // Revert this tentative parse to parse a non-type template argument. 1022199482Srdivacky TPA.Revert(); 1023199482Srdivacky } 1024199482Srdivacky 1025199482Srdivacky // Parse a non-type template argument. 1026199482Srdivacky SourceLocation Loc = Tok.getLocation(); 1027212904Sdim ExprResult ExprArg = ParseConstantExpression(); 1028193326Sed if (ExprArg.isInvalid() || !ExprArg.get()) 1029199482Srdivacky return ParsedTemplateArgument(); 1030193326Sed 1031199482Srdivacky return ParsedTemplateArgument(ParsedTemplateArgument::NonType, 1032199482Srdivacky ExprArg.release(), Loc); 1033193326Sed} 1034193326Sed 1035208600Srdivacky/// \brief Determine whether the current tokens can only be parsed as a 1036208600Srdivacky/// template argument list (starting with the '<') and never as a '<' 1037208600Srdivacky/// expression. 1038208600Srdivackybool Parser::IsTemplateArgumentList(unsigned Skip) { 1039208600Srdivacky struct AlwaysRevertAction : TentativeParsingAction { 1040208600Srdivacky AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { } 1041208600Srdivacky ~AlwaysRevertAction() { Revert(); } 1042208600Srdivacky } Tentative(*this); 1043208600Srdivacky 1044208600Srdivacky while (Skip) { 1045208600Srdivacky ConsumeToken(); 1046208600Srdivacky --Skip; 1047208600Srdivacky } 1048208600Srdivacky 1049208600Srdivacky // '<' 1050208600Srdivacky if (!Tok.is(tok::less)) 1051208600Srdivacky return false; 1052208600Srdivacky ConsumeToken(); 1053208600Srdivacky 1054208600Srdivacky // An empty template argument list. 1055208600Srdivacky if (Tok.is(tok::greater)) 1056208600Srdivacky return true; 1057208600Srdivacky 1058208600Srdivacky // See whether we have declaration specifiers, which indicate a type. 1059208600Srdivacky while (isCXXDeclarationSpecifier() == TPResult::True()) 1060208600Srdivacky ConsumeToken(); 1061208600Srdivacky 1062208600Srdivacky // If we have a '>' or a ',' then this is a template argument list. 1063208600Srdivacky return Tok.is(tok::greater) || Tok.is(tok::comma); 1064208600Srdivacky} 1065208600Srdivacky 1066193326Sed/// ParseTemplateArgumentList - Parse a C++ template-argument-list 1067193326Sed/// (C++ [temp.names]). Returns true if there was an error. 1068193326Sed/// 1069193326Sed/// template-argument-list: [C++ 14.2] 1070193326Sed/// template-argument 1071193326Sed/// template-argument-list ',' template-argument 1072198092Srdivackybool 1073199482SrdivackyParser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { 1074193326Sed while (true) { 1075199482Srdivacky ParsedTemplateArgument Arg = ParseTemplateArgument(); 1076218893Sdim if (Tok.is(tok::ellipsis)) { 1077218893Sdim SourceLocation EllipsisLoc = ConsumeToken(); 1078218893Sdim Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); 1079218893Sdim } 1080218893Sdim 1081199482Srdivacky if (Arg.isInvalid()) { 1082193326Sed SkipUntil(tok::comma, tok::greater, true, true); 1083193326Sed return true; 1084193326Sed } 1085193326Sed 1086199482Srdivacky // Save this template argument. 1087199482Srdivacky TemplateArgs.push_back(Arg); 1088199482Srdivacky 1089193326Sed // If the next token is a comma, consume it and keep reading 1090193326Sed // arguments. 1091193326Sed if (Tok.isNot(tok::comma)) break; 1092193326Sed 1093193326Sed // Consume the comma. 1094193326Sed ConsumeToken(); 1095193326Sed } 1096193326Sed 1097201361Srdivacky return false; 1098193326Sed} 1099193326Sed 1100198092Srdivacky/// \brief Parse a C++ explicit template instantiation 1101193326Sed/// (C++ [temp.explicit]). 1102193326Sed/// 1103193326Sed/// explicit-instantiation: 1104198092Srdivacky/// 'extern' [opt] 'template' declaration 1105198092Srdivacky/// 1106198092Srdivacky/// Note that the 'extern' is a GNU extension and C++0x feature. 1107212904SdimDecl *Parser::ParseExplicitInstantiation(SourceLocation ExternLoc, 1108212904Sdim SourceLocation TemplateLoc, 1109212904Sdim SourceLocation &DeclEnd) { 1110212904Sdim // This isn't really required here. 1111212904Sdim ParsingDeclRAIIObject ParsingTemplateParams(*this); 1112212904Sdim 1113198092Srdivacky return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, 1114198092Srdivacky ParsedTemplateInfo(ExternLoc, 1115198092Srdivacky TemplateLoc), 1116212904Sdim ParsingTemplateParams, 1117193326Sed DeclEnd, AS_none); 1118193326Sed} 1119218893Sdim 1120218893SdimSourceRange Parser::ParsedTemplateInfo::getSourceRange() const { 1121218893Sdim if (TemplateParams) 1122218893Sdim return getTemplateParamsRange(TemplateParams->data(), 1123218893Sdim TemplateParams->size()); 1124218893Sdim 1125218893Sdim SourceRange R(TemplateLoc); 1126218893Sdim if (ExternLoc.isValid()) 1127218893Sdim R.setBegin(ExternLoc); 1128218893Sdim return R; 1129218893Sdim} 1130221345Sdim 1131221345Sdimvoid Parser::LateTemplateParserCallback(void *P, const FunctionDecl *FD) { 1132221345Sdim ((Parser*)P)->LateTemplateParser(FD); 1133221345Sdim} 1134221345Sdim 1135221345Sdim 1136221345Sdimvoid Parser::LateTemplateParser(const FunctionDecl *FD) { 1137221345Sdim LateParsedTemplatedFunction *LPT = LateParsedTemplateMap[FD]; 1138221345Sdim if (LPT) { 1139221345Sdim ParseLateTemplatedFuncDef(*LPT); 1140221345Sdim return; 1141221345Sdim } 1142221345Sdim 1143221345Sdim llvm_unreachable("Late templated function without associated lexed tokens"); 1144221345Sdim} 1145221345Sdim 1146221345Sdim/// \brief Late parse a C++ function template in Microsoft mode. 1147221345Sdimvoid Parser::ParseLateTemplatedFuncDef(LateParsedTemplatedFunction &LMT) { 1148221345Sdim if(!LMT.D) 1149221345Sdim return; 1150221345Sdim 1151221345Sdim // If this is a member template, introduce the template parameter scope. 1152221345Sdim ParseScope TemplateScope(this, Scope::TemplateParamScope); 1153221345Sdim 1154221345Sdim // Get the FunctionDecl. 1155221345Sdim FunctionDecl *FD = 0; 1156221345Sdim if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(LMT.D)) 1157221345Sdim FD = FunTmpl->getTemplatedDecl(); 1158221345Sdim else 1159221345Sdim FD = cast<FunctionDecl>(LMT.D); 1160221345Sdim 1161221345Sdim // Reinject the template parameters. 1162221345Sdim DeclaratorDecl* Declarator = dyn_cast<DeclaratorDecl>(FD); 1163221345Sdim if (Declarator && Declarator->getNumTemplateParameterLists() != 0) { 1164221345Sdim Actions.ActOnReenterDeclaratorTemplateScope(getCurScope(), Declarator); 1165221345Sdim Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D); 1166221345Sdim } else { 1167221345Sdim Actions.ActOnReenterTemplateScope(getCurScope(), LMT.D); 1168221345Sdim 1169221345Sdim DeclContext *DD = FD->getLexicalParent(); 1170221345Sdim while (DD && DD->isRecord()) { 1171221345Sdim if (ClassTemplatePartialSpecializationDecl* MD = 1172221345Sdim dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>(DD)) 1173221345Sdim Actions.ActOnReenterTemplateScope(getCurScope(), MD); 1174221345Sdim else if (CXXRecordDecl* MD = dyn_cast_or_null<CXXRecordDecl>(DD)) 1175221345Sdim Actions.ActOnReenterTemplateScope(getCurScope(), 1176221345Sdim MD->getDescribedClassTemplate()); 1177221345Sdim 1178221345Sdim DD = DD->getLexicalParent(); 1179221345Sdim } 1180221345Sdim } 1181221345Sdim assert(!LMT.Toks.empty() && "Empty body!"); 1182221345Sdim 1183221345Sdim // Append the current token at the end of the new token stream so that it 1184221345Sdim // doesn't get lost. 1185221345Sdim LMT.Toks.push_back(Tok); 1186221345Sdim PP.EnterTokenStream(LMT.Toks.data(), LMT.Toks.size(), true, false); 1187221345Sdim 1188221345Sdim // Consume the previously pushed token. 1189221345Sdim ConsumeAnyToken(); 1190221345Sdim assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) 1191221345Sdim && "Inline method not starting with '{', ':' or 'try'"); 1192221345Sdim 1193221345Sdim // Parse the method body. Function body parsing code is similar enough 1194221345Sdim // to be re-used for method bodies as well. 1195221345Sdim ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); 1196221345Sdim 1197221345Sdim // Recreate the DeclContext. 1198221345Sdim Sema::ContextRAII SavedContext(Actions, Actions.getContainingDC(FD)); 1199221345Sdim 1200221345Sdim if (FunctionTemplateDecl *FunctionTemplate 1201221345Sdim = dyn_cast_or_null<FunctionTemplateDecl>(LMT.D)) 1202221345Sdim Actions.ActOnStartOfFunctionDef(getCurScope(), 1203221345Sdim FunctionTemplate->getTemplatedDecl()); 1204221345Sdim if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(LMT.D)) 1205221345Sdim Actions.ActOnStartOfFunctionDef(getCurScope(), Function); 1206221345Sdim 1207221345Sdim 1208221345Sdim if (Tok.is(tok::kw_try)) { 1209221345Sdim ParseFunctionTryBlock(LMT.D, FnScope); 1210221345Sdim return; 1211221345Sdim } 1212221345Sdim if (Tok.is(tok::colon)) { 1213221345Sdim ParseConstructorInitializer(LMT.D); 1214221345Sdim 1215221345Sdim // Error recovery. 1216221345Sdim if (!Tok.is(tok::l_brace)) { 1217221345Sdim Actions.ActOnFinishFunctionBody(LMT.D, 0); 1218221345Sdim return; 1219221345Sdim } 1220221345Sdim } else 1221221345Sdim Actions.ActOnDefaultCtorInitializers(LMT.D); 1222221345Sdim 1223221345Sdim ParseFunctionStatementBody(LMT.D, FnScope); 1224221345Sdim Actions.MarkAsLateParsedTemplate(FD, false); 1225221345Sdim 1226221345Sdim DeclGroupPtrTy grp = Actions.ConvertDeclToDeclGroup(LMT.D); 1227221345Sdim if (grp) 1228221345Sdim Actions.getASTConsumer().HandleTopLevelDecl(grp.get()); 1229221345Sdim} 1230221345Sdim 1231221345Sdim/// \brief Lex a delayed template function for late parsing. 1232221345Sdimvoid Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { 1233221345Sdim tok::TokenKind kind = Tok.getKind(); 1234221345Sdim // We may have a constructor initializer or function-try-block here. 1235221345Sdim if (kind == tok::colon || kind == tok::kw_try) 1236221345Sdim ConsumeAndStoreUntil(tok::l_brace, Toks); 1237221345Sdim else { 1238221345Sdim Toks.push_back(Tok); 1239221345Sdim ConsumeBrace(); 1240221345Sdim } 1241221345Sdim // Consume everything up to (and including) the matching right brace. 1242221345Sdim ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1243221345Sdim 1244221345Sdim // If we're in a function-try-block, we need to store all the catch blocks. 1245221345Sdim if (kind == tok::kw_try) { 1246221345Sdim while (Tok.is(tok::kw_catch)) { 1247221345Sdim ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 1248221345Sdim ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1249221345Sdim } 1250221345Sdim } 1251221345Sdim} 1252