ParseTemplate.cpp revision 194711
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" 16193326Sed#include "clang/Parse/DeclSpec.h" 17193326Sed#include "clang/Parse/Scope.h" 18193326Sedusing namespace clang; 19193326Sed 20193326Sed/// \brief Parse a template declaration, explicit instantiation, or 21193326Sed/// explicit specialization. 22193326SedParser::DeclPtrTy 23193326SedParser::ParseDeclarationStartingWithTemplate(unsigned Context, 24193326Sed SourceLocation &DeclEnd, 25193326Sed AccessSpecifier AS) { 26193326Sed if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) 27193326Sed return ParseExplicitInstantiation(ConsumeToken(), DeclEnd); 28193326Sed 29193326Sed return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS); 30193326Sed} 31193326Sed 32193326Sed/// \brief Parse a template declaration or an explicit specialization. 33193326Sed/// 34193326Sed/// Template declarations include one or more template parameter lists 35193326Sed/// and either the function or class template declaration. Explicit 36193326Sed/// specializations contain one or more 'template < >' prefixes 37193326Sed/// followed by a (possibly templated) declaration. Since the 38193326Sed/// syntactic form of both features is nearly identical, we parse all 39193326Sed/// of the template headers together and let semantic analysis sort 40193326Sed/// the declarations from the explicit specializations. 41193326Sed/// 42193326Sed/// template-declaration: [C++ temp] 43193326Sed/// 'export'[opt] 'template' '<' template-parameter-list '>' declaration 44193326Sed/// 45193326Sed/// explicit-specialization: [ C++ temp.expl.spec] 46193326Sed/// 'template' '<' '>' declaration 47193326SedParser::DeclPtrTy 48193326SedParser::ParseTemplateDeclarationOrSpecialization(unsigned Context, 49193326Sed SourceLocation &DeclEnd, 50193326Sed AccessSpecifier AS) { 51193326Sed assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && 52193326Sed "Token does not start a template declaration."); 53193326Sed 54193326Sed // Enter template-parameter scope. 55193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 56193326Sed 57193326Sed // Parse multiple levels of template headers within this template 58193326Sed // parameter scope, e.g., 59193326Sed // 60193326Sed // template<typename T> 61193326Sed // template<typename U> 62193326Sed // class A<T>::B { ... }; 63193326Sed // 64193326Sed // We parse multiple levels non-recursively so that we can build a 65193326Sed // single data structure containing all of the template parameter 66193326Sed // lists to easily differentiate between the case above and: 67193326Sed // 68193326Sed // template<typename T> 69193326Sed // class A { 70193326Sed // template<typename U> class B; 71193326Sed // }; 72193326Sed // 73193326Sed // In the first case, the action for declaring A<T>::B receives 74193326Sed // both template parameter lists. In the second case, the action for 75193326Sed // defining A<T>::B receives just the inner template parameter list 76193326Sed // (and retrieves the outer template parameter list from its 77193326Sed // context). 78193326Sed bool isSpecialiation = true; 79193326Sed TemplateParameterLists ParamLists; 80193326Sed do { 81193326Sed // Consume the 'export', if any. 82193326Sed SourceLocation ExportLoc; 83193326Sed if (Tok.is(tok::kw_export)) { 84193326Sed ExportLoc = ConsumeToken(); 85193326Sed } 86193326Sed 87193326Sed // Consume the 'template', which should be here. 88193326Sed SourceLocation TemplateLoc; 89193326Sed if (Tok.is(tok::kw_template)) { 90193326Sed TemplateLoc = ConsumeToken(); 91193326Sed } else { 92193326Sed Diag(Tok.getLocation(), diag::err_expected_template); 93193326Sed return DeclPtrTy(); 94193326Sed } 95193326Sed 96193326Sed // Parse the '<' template-parameter-list '>' 97193326Sed SourceLocation LAngleLoc, RAngleLoc; 98193326Sed TemplateParameterList TemplateParams; 99193326Sed ParseTemplateParameters(ParamLists.size(), TemplateParams, LAngleLoc, 100193326Sed RAngleLoc); 101193326Sed 102193326Sed if (!TemplateParams.empty()) 103193326Sed isSpecialiation = false; 104193326Sed 105193326Sed ParamLists.push_back( 106193326Sed Actions.ActOnTemplateParameterList(ParamLists.size(), ExportLoc, 107193326Sed TemplateLoc, LAngleLoc, 108193326Sed TemplateParams.data(), 109193326Sed TemplateParams.size(), RAngleLoc)); 110193326Sed } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); 111193326Sed 112193326Sed // Parse the actual template declaration. 113193326Sed return ParseSingleDeclarationAfterTemplate(Context, 114193326Sed ParsedTemplateInfo(&ParamLists, 115193326Sed isSpecialiation), 116193326Sed DeclEnd, AS); 117193326Sed} 118193326Sed 119193326Sed/// \brief Parse a single declaration that declares a template, 120193326Sed/// template specialization, or explicit instantiation of a template. 121193326Sed/// 122193326Sed/// \param TemplateParams if non-NULL, the template parameter lists 123193326Sed/// that preceded this declaration. In this case, the declaration is a 124193326Sed/// template declaration, out-of-line definition of a template, or an 125193326Sed/// explicit template specialization. When NULL, the declaration is an 126193326Sed/// explicit template instantiation. 127193326Sed/// 128193326Sed/// \param TemplateLoc when TemplateParams is NULL, the location of 129193326Sed/// the 'template' keyword that indicates that we have an explicit 130193326Sed/// template instantiation. 131193326Sed/// 132193326Sed/// \param DeclEnd will receive the source location of the last token 133193326Sed/// within this declaration. 134193326Sed/// 135193326Sed/// \param AS the access specifier associated with this 136193326Sed/// declaration. Will be AS_none for namespace-scope declarations. 137193326Sed/// 138193326Sed/// \returns the new declaration. 139193326SedParser::DeclPtrTy 140193326SedParser::ParseSingleDeclarationAfterTemplate( 141193326Sed unsigned Context, 142193326Sed const ParsedTemplateInfo &TemplateInfo, 143193326Sed SourceLocation &DeclEnd, 144193326Sed AccessSpecifier AS) { 145193326Sed assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 146193326Sed "Template information required"); 147193326Sed 148193326Sed // Parse the declaration specifiers. 149193326Sed DeclSpec DS; 150193326Sed // FIXME: Pass TemplateLoc through for explicit template instantiations 151193326Sed ParseDeclarationSpecifiers(DS, TemplateInfo, AS); 152193326Sed 153193326Sed if (Tok.is(tok::semi)) { 154193326Sed DeclEnd = ConsumeToken(); 155193326Sed return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 156193326Sed } 157193326Sed 158193326Sed // Parse the declarator. 159193326Sed Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); 160193326Sed ParseDeclarator(DeclaratorInfo); 161193326Sed // Error parsing the declarator? 162193326Sed if (!DeclaratorInfo.hasName()) { 163193326Sed // If so, skip until the semi-colon or a }. 164193326Sed SkipUntil(tok::r_brace, true, true); 165193326Sed if (Tok.is(tok::semi)) 166193326Sed ConsumeToken(); 167193326Sed return DeclPtrTy(); 168193326Sed } 169193326Sed 170193326Sed // If we have a declaration or declarator list, handle it. 171193326Sed if (isDeclarationAfterDeclarator()) { 172193326Sed // Parse this declaration. 173193326Sed DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo); 174193326Sed 175193326Sed if (Tok.is(tok::comma)) { 176193326Sed Diag(Tok, diag::err_multiple_template_declarators) 177193326Sed << (int)TemplateInfo.Kind; 178193326Sed SkipUntil(tok::semi, true, false); 179193326Sed return ThisDecl; 180193326Sed } 181193326Sed 182193326Sed // Eat the semi colon after the declaration. 183193326Sed ExpectAndConsume(tok::semi, diag::err_expected_semi_declation); 184193326Sed return ThisDecl; 185193326Sed } 186193326Sed 187193326Sed if (DeclaratorInfo.isFunctionDeclarator() && 188193326Sed isStartOfFunctionDefinition()) { 189193326Sed if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 190193326Sed Diag(Tok, diag::err_function_declared_typedef); 191193326Sed 192193326Sed if (Tok.is(tok::l_brace)) { 193193326Sed // This recovery skips the entire function body. It would be nice 194193326Sed // to simply call ParseFunctionDefinition() below, however Sema 195193326Sed // assumes the declarator represents a function, not a typedef. 196193326Sed ConsumeBrace(); 197193326Sed SkipUntil(tok::r_brace, true); 198193326Sed } else { 199193326Sed SkipUntil(tok::semi); 200193326Sed } 201193326Sed return DeclPtrTy(); 202193326Sed } 203193326Sed return ParseFunctionDefinition(DeclaratorInfo); 204193326Sed } 205193326Sed 206193326Sed if (DeclaratorInfo.isFunctionDeclarator()) 207193326Sed Diag(Tok, diag::err_expected_fn_body); 208193326Sed else 209193326Sed Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); 210193326Sed SkipUntil(tok::semi); 211193326Sed return DeclPtrTy(); 212193326Sed} 213193326Sed 214193326Sed/// ParseTemplateParameters - Parses a template-parameter-list enclosed in 215193326Sed/// angle brackets. Depth is the depth of this template-parameter-list, which 216193326Sed/// is the number of template headers directly enclosing this template header. 217193326Sed/// TemplateParams is the current list of template parameters we're building. 218193326Sed/// The template parameter we parse will be added to this list. LAngleLoc and 219193326Sed/// RAngleLoc will receive the positions of the '<' and '>', respectively, 220193326Sed/// that enclose this template parameter list. 221193326Sedbool Parser::ParseTemplateParameters(unsigned Depth, 222193326Sed TemplateParameterList &TemplateParams, 223193326Sed SourceLocation &LAngleLoc, 224193326Sed SourceLocation &RAngleLoc) { 225193326Sed // Get the template parameter list. 226193326Sed if(!Tok.is(tok::less)) { 227193326Sed Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; 228193326Sed return false; 229193326Sed } 230193326Sed LAngleLoc = ConsumeToken(); 231193326Sed 232193326Sed // Try to parse the template parameter list. 233193326Sed if (Tok.is(tok::greater)) 234193326Sed RAngleLoc = ConsumeToken(); 235193326Sed else if(ParseTemplateParameterList(Depth, TemplateParams)) { 236193326Sed if(!Tok.is(tok::greater)) { 237193326Sed Diag(Tok.getLocation(), diag::err_expected_greater); 238193326Sed return false; 239193326Sed } 240193326Sed RAngleLoc = ConsumeToken(); 241193326Sed } 242193326Sed return true; 243193326Sed} 244193326Sed 245193326Sed/// ParseTemplateParameterList - Parse a template parameter list. If 246193326Sed/// the parsing fails badly (i.e., closing bracket was left out), this 247193326Sed/// will try to put the token stream in a reasonable position (closing 248193326Sed/// a statement, etc.) and return false. 249193326Sed/// 250193326Sed/// template-parameter-list: [C++ temp] 251193326Sed/// template-parameter 252193326Sed/// template-parameter-list ',' template-parameter 253193326Sedbool 254193326SedParser::ParseTemplateParameterList(unsigned Depth, 255193326Sed TemplateParameterList &TemplateParams) { 256193326Sed while(1) { 257193326Sed if (DeclPtrTy TmpParam 258193326Sed = ParseTemplateParameter(Depth, TemplateParams.size())) { 259193326Sed TemplateParams.push_back(TmpParam); 260193326Sed } else { 261193326Sed // If we failed to parse a template parameter, skip until we find 262193326Sed // a comma or closing brace. 263193326Sed SkipUntil(tok::comma, tok::greater, true, true); 264193326Sed } 265193326Sed 266193326Sed // Did we find a comma or the end of the template parmeter list? 267193326Sed if(Tok.is(tok::comma)) { 268193326Sed ConsumeToken(); 269193326Sed } else if(Tok.is(tok::greater)) { 270193326Sed // Don't consume this... that's done by template parser. 271193326Sed break; 272193326Sed } else { 273193326Sed // Somebody probably forgot to close the template. Skip ahead and 274193326Sed // try to get out of the expression. This error is currently 275193326Sed // subsumed by whatever goes on in ParseTemplateParameter. 276193326Sed // TODO: This could match >>, and it would be nice to avoid those 277193326Sed // silly errors with template <vec<T>>. 278193326Sed // Diag(Tok.getLocation(), diag::err_expected_comma_greater); 279193326Sed SkipUntil(tok::greater, true, true); 280193326Sed return false; 281193326Sed } 282193326Sed } 283193326Sed return true; 284193326Sed} 285193326Sed 286193326Sed/// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). 287193326Sed/// 288193326Sed/// template-parameter: [C++ temp.param] 289193326Sed/// type-parameter 290193326Sed/// parameter-declaration 291193326Sed/// 292193326Sed/// type-parameter: (see below) 293194179Sed/// 'class' ...[opt][C++0x] identifier[opt] 294193326Sed/// 'class' identifier[opt] '=' type-id 295194179Sed/// 'typename' ...[opt][C++0x] identifier[opt] 296193326Sed/// 'typename' identifier[opt] '=' type-id 297194179Sed/// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt] 298193326Sed/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression 299193326SedParser::DeclPtrTy 300193326SedParser::ParseTemplateParameter(unsigned Depth, unsigned Position) { 301193326Sed if(Tok.is(tok::kw_class) || 302193326Sed (Tok.is(tok::kw_typename) && 303193326Sed // FIXME: Next token has not been annotated! 304193326Sed NextToken().isNot(tok::annot_typename))) { 305193326Sed return ParseTypeParameter(Depth, Position); 306193326Sed } 307193326Sed 308193326Sed if(Tok.is(tok::kw_template)) 309193326Sed return ParseTemplateTemplateParameter(Depth, Position); 310193326Sed 311193326Sed // If it's none of the above, then it must be a parameter declaration. 312193326Sed // NOTE: This will pick up errors in the closure of the template parameter 313193326Sed // list (e.g., template < ; Check here to implement >> style closures. 314193326Sed return ParseNonTypeTemplateParameter(Depth, Position); 315193326Sed} 316193326Sed 317193326Sed/// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). 318193326Sed/// Other kinds of template parameters are parsed in 319193326Sed/// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. 320193326Sed/// 321193326Sed/// type-parameter: [C++ temp.param] 322194179Sed/// 'class' ...[opt][C++0x] identifier[opt] 323193326Sed/// 'class' identifier[opt] '=' type-id 324194179Sed/// 'typename' ...[opt][C++0x] identifier[opt] 325193326Sed/// 'typename' identifier[opt] '=' type-id 326193326SedParser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){ 327193326Sed assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && 328193326Sed "A type-parameter starts with 'class' or 'typename'"); 329193326Sed 330193326Sed // Consume the 'class' or 'typename' keyword. 331193326Sed bool TypenameKeyword = Tok.is(tok::kw_typename); 332193326Sed SourceLocation KeyLoc = ConsumeToken(); 333193326Sed 334194179Sed // Grab the ellipsis (if given). 335194179Sed bool Ellipsis = false; 336194179Sed SourceLocation EllipsisLoc; 337194179Sed if (Tok.is(tok::ellipsis)) { 338194179Sed Ellipsis = true; 339194179Sed EllipsisLoc = ConsumeToken(); 340194179Sed 341194179Sed if (!getLang().CPlusPlus0x) 342194179Sed Diag(EllipsisLoc, diag::err_variadic_templates); 343194179Sed } 344194179Sed 345193326Sed // Grab the template parameter name (if given) 346193326Sed SourceLocation NameLoc; 347193326Sed IdentifierInfo* ParamName = 0; 348193326Sed if(Tok.is(tok::identifier)) { 349193326Sed ParamName = Tok.getIdentifierInfo(); 350193326Sed NameLoc = ConsumeToken(); 351193326Sed } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || 352193326Sed Tok.is(tok::greater)) { 353193326Sed // Unnamed template parameter. Don't have to do anything here, just 354193326Sed // don't consume this token. 355193326Sed } else { 356193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 357193326Sed return DeclPtrTy(); 358193326Sed } 359193326Sed 360193326Sed DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword, 361194179Sed Ellipsis, EllipsisLoc, 362193326Sed KeyLoc, ParamName, NameLoc, 363193326Sed Depth, Position); 364193326Sed 365193326Sed // Grab a default type id (if given). 366193326Sed if(Tok.is(tok::equal)) { 367193326Sed SourceLocation EqualLoc = ConsumeToken(); 368193326Sed SourceLocation DefaultLoc = Tok.getLocation(); 369193326Sed TypeResult DefaultType = ParseTypeName(); 370193326Sed if (!DefaultType.isInvalid()) 371193326Sed Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc, 372193326Sed DefaultType.get()); 373193326Sed } 374193326Sed 375193326Sed return TypeParam; 376193326Sed} 377193326Sed 378193326Sed/// ParseTemplateTemplateParameter - Handle the parsing of template 379193326Sed/// template parameters. 380193326Sed/// 381193326Sed/// type-parameter: [C++ temp.param] 382193326Sed/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] 383193326Sed/// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression 384193326SedParser::DeclPtrTy 385193326SedParser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { 386193326Sed assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); 387193326Sed 388193326Sed // Handle the template <...> part. 389193326Sed SourceLocation TemplateLoc = ConsumeToken(); 390193326Sed TemplateParameterList TemplateParams; 391193326Sed SourceLocation LAngleLoc, RAngleLoc; 392193326Sed { 393193326Sed ParseScope TemplateParmScope(this, Scope::TemplateParamScope); 394193326Sed if(!ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, 395193326Sed RAngleLoc)) { 396193326Sed return DeclPtrTy(); 397193326Sed } 398193326Sed } 399193326Sed 400193326Sed // Generate a meaningful error if the user forgot to put class before the 401193326Sed // identifier, comma, or greater. 402193326Sed if(!Tok.is(tok::kw_class)) { 403193326Sed Diag(Tok.getLocation(), diag::err_expected_class_before) 404193326Sed << PP.getSpelling(Tok); 405193326Sed return DeclPtrTy(); 406193326Sed } 407193326Sed SourceLocation ClassLoc = ConsumeToken(); 408193326Sed 409193326Sed // Get the identifier, if given. 410193326Sed SourceLocation NameLoc; 411193326Sed IdentifierInfo* ParamName = 0; 412193326Sed if(Tok.is(tok::identifier)) { 413193326Sed ParamName = Tok.getIdentifierInfo(); 414193326Sed NameLoc = ConsumeToken(); 415193326Sed } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { 416193326Sed // Unnamed template parameter. Don't have to do anything here, just 417193326Sed // don't consume this token. 418193326Sed } else { 419193326Sed Diag(Tok.getLocation(), diag::err_expected_ident); 420193326Sed return DeclPtrTy(); 421193326Sed } 422193326Sed 423193326Sed TemplateParamsTy *ParamList = 424193326Sed Actions.ActOnTemplateParameterList(Depth, SourceLocation(), 425193326Sed TemplateLoc, LAngleLoc, 426193326Sed &TemplateParams[0], 427193326Sed TemplateParams.size(), 428193326Sed RAngleLoc); 429193326Sed 430193326Sed Parser::DeclPtrTy Param 431193326Sed = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc, 432193326Sed ParamList, ParamName, 433193326Sed NameLoc, Depth, Position); 434193326Sed 435193326Sed // Get the a default value, if given. 436193326Sed if (Tok.is(tok::equal)) { 437193326Sed SourceLocation EqualLoc = ConsumeToken(); 438193326Sed OwningExprResult DefaultExpr = ParseCXXIdExpression(); 439193326Sed if (DefaultExpr.isInvalid()) 440193326Sed return Param; 441193326Sed else if (Param) 442193326Sed Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, 443193326Sed move(DefaultExpr)); 444193326Sed } 445193326Sed 446193326Sed return Param; 447193326Sed} 448193326Sed 449193326Sed/// ParseNonTypeTemplateParameter - Handle the parsing of non-type 450193326Sed/// template parameters (e.g., in "template<int Size> class array;"). 451193326Sed/// 452193326Sed/// template-parameter: 453193326Sed/// ... 454193326Sed/// parameter-declaration 455193326Sed/// 456193326Sed/// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(), 457193326Sed/// but that didn't work out to well. Instead, this tries to recrate the basic 458193326Sed/// parsing of parameter declarations, but tries to constrain it for template 459193326Sed/// parameters. 460193326Sed/// FIXME: We need to make a ParseParameterDeclaration that works for 461193326Sed/// non-type template parameters and normal function parameters. 462193326SedParser::DeclPtrTy 463193326SedParser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { 464193326Sed SourceLocation StartLoc = Tok.getLocation(); 465193326Sed 466193326Sed // Parse the declaration-specifiers (i.e., the type). 467193326Sed // FIXME: The type should probably be restricted in some way... Not all 468193326Sed // declarators (parts of declarators?) are accepted for parameters. 469193326Sed DeclSpec DS; 470193326Sed ParseDeclarationSpecifiers(DS); 471193326Sed 472193326Sed // Parse this as a typename. 473193326Sed Declarator ParamDecl(DS, Declarator::TemplateParamContext); 474193326Sed ParseDeclarator(ParamDecl); 475193326Sed if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) { 476193326Sed // This probably shouldn't happen - and it's more of a Sema thing, but 477193326Sed // basically we didn't parse the type name because we couldn't associate 478193326Sed // it with an AST node. we should just skip to the comma or greater. 479193326Sed // TODO: This is currently a placeholder for some kind of Sema Error. 480193326Sed Diag(Tok.getLocation(), diag::err_parse_error); 481193326Sed SkipUntil(tok::comma, tok::greater, true, true); 482193326Sed return DeclPtrTy(); 483193326Sed } 484193326Sed 485193326Sed // Create the parameter. 486193326Sed DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl, 487193326Sed Depth, Position); 488193326Sed 489193326Sed // If there is a default value, parse it. 490193326Sed if (Tok.is(tok::equal)) { 491193326Sed SourceLocation EqualLoc = ConsumeToken(); 492193326Sed 493193326Sed // C++ [temp.param]p15: 494193326Sed // When parsing a default template-argument for a non-type 495193326Sed // template-parameter, the first non-nested > is taken as the 496193326Sed // end of the template-parameter-list rather than a greater-than 497193326Sed // operator. 498193326Sed GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 499193326Sed 500193326Sed OwningExprResult DefaultArg = ParseAssignmentExpression(); 501193326Sed if (DefaultArg.isInvalid()) 502193326Sed SkipUntil(tok::comma, tok::greater, true, true); 503193326Sed else if (Param) 504193326Sed Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc, 505193326Sed move(DefaultArg)); 506193326Sed } 507193326Sed 508193326Sed return Param; 509193326Sed} 510193326Sed 511193326Sed/// \brief Parses a template-id that after the template name has 512193326Sed/// already been parsed. 513193326Sed/// 514193326Sed/// This routine takes care of parsing the enclosed template argument 515193326Sed/// list ('<' template-parameter-list [opt] '>') and placing the 516193326Sed/// results into a form that can be transferred to semantic analysis. 517193326Sed/// 518193326Sed/// \param Template the template declaration produced by isTemplateName 519193326Sed/// 520193326Sed/// \param TemplateNameLoc the source location of the template name 521193326Sed/// 522193326Sed/// \param SS if non-NULL, the nested-name-specifier preceding the 523193326Sed/// template name. 524193326Sed/// 525193326Sed/// \param ConsumeLastToken if true, then we will consume the last 526193326Sed/// token that forms the template-id. Otherwise, we will leave the 527193326Sed/// last token in the stream (e.g., so that it can be replaced with an 528193326Sed/// annotation token). 529193326Sedbool 530193326SedParser::ParseTemplateIdAfterTemplateName(TemplateTy Template, 531193326Sed SourceLocation TemplateNameLoc, 532193326Sed const CXXScopeSpec *SS, 533193326Sed bool ConsumeLastToken, 534193326Sed SourceLocation &LAngleLoc, 535193326Sed TemplateArgList &TemplateArgs, 536193326Sed TemplateArgIsTypeList &TemplateArgIsType, 537193326Sed TemplateArgLocationList &TemplateArgLocations, 538193326Sed SourceLocation &RAngleLoc) { 539193326Sed assert(Tok.is(tok::less) && "Must have already parsed the template-name"); 540193326Sed 541193326Sed // Consume the '<'. 542193326Sed LAngleLoc = ConsumeToken(); 543193326Sed 544193326Sed // Parse the optional template-argument-list. 545193326Sed bool Invalid = false; 546193326Sed { 547193326Sed GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); 548193326Sed if (Tok.isNot(tok::greater)) 549193326Sed Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType, 550193326Sed TemplateArgLocations); 551193326Sed 552193326Sed if (Invalid) { 553193326Sed // Try to find the closing '>'. 554193326Sed SkipUntil(tok::greater, true, !ConsumeLastToken); 555193326Sed 556193326Sed return true; 557193326Sed } 558193326Sed } 559193326Sed 560193326Sed if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) 561193326Sed return true; 562193326Sed 563193326Sed // Determine the location of the '>' or '>>'. Only consume this 564193326Sed // token if the caller asked us to. 565193326Sed RAngleLoc = Tok.getLocation(); 566193326Sed 567193326Sed if (Tok.is(tok::greatergreater)) { 568193326Sed if (!getLang().CPlusPlus0x) { 569193326Sed const char *ReplaceStr = "> >"; 570193326Sed if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater)) 571193326Sed ReplaceStr = "> > "; 572193326Sed 573193326Sed Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space) 574193326Sed << CodeModificationHint::CreateReplacement( 575193326Sed SourceRange(Tok.getLocation()), ReplaceStr); 576193326Sed } 577193326Sed 578193326Sed Tok.setKind(tok::greater); 579193326Sed if (!ConsumeLastToken) { 580193326Sed // Since we're not supposed to consume the '>>' token, we need 581193326Sed // to insert a second '>' token after the first. 582193326Sed PP.EnterToken(Tok); 583193326Sed } 584193326Sed } else if (ConsumeLastToken) 585193326Sed ConsumeToken(); 586193326Sed 587193326Sed return false; 588193326Sed} 589193326Sed 590193326Sed/// \brief Replace the tokens that form a simple-template-id with an 591193326Sed/// annotation token containing the complete template-id. 592193326Sed/// 593193326Sed/// The first token in the stream must be the name of a template that 594193326Sed/// is followed by a '<'. This routine will parse the complete 595193326Sed/// simple-template-id and replace the tokens with a single annotation 596193326Sed/// token with one of two different kinds: if the template-id names a 597193326Sed/// type (and \p AllowTypeAnnotation is true), the annotation token is 598193326Sed/// a type annotation that includes the optional nested-name-specifier 599193326Sed/// (\p SS). Otherwise, the annotation token is a template-id 600193326Sed/// annotation that does not include the optional 601193326Sed/// nested-name-specifier. 602193326Sed/// 603193326Sed/// \param Template the declaration of the template named by the first 604193326Sed/// token (an identifier), as returned from \c Action::isTemplateName(). 605193326Sed/// 606193326Sed/// \param TemplateNameKind the kind of template that \p Template 607193326Sed/// refers to, as returned from \c Action::isTemplateName(). 608193326Sed/// 609193326Sed/// \param SS if non-NULL, the nested-name-specifier that precedes 610193326Sed/// this template name. 611193326Sed/// 612193326Sed/// \param TemplateKWLoc if valid, specifies that this template-id 613193326Sed/// annotation was preceded by the 'template' keyword and gives the 614193326Sed/// location of that keyword. If invalid (the default), then this 615193326Sed/// template-id was not preceded by a 'template' keyword. 616193326Sed/// 617193326Sed/// \param AllowTypeAnnotation if true (the default), then a 618193326Sed/// simple-template-id that refers to a class template, template 619193326Sed/// template parameter, or other template that produces a type will be 620193326Sed/// replaced with a type annotation token. Otherwise, the 621193326Sed/// simple-template-id is always replaced with a template-id 622193326Sed/// annotation token. 623193326Sedvoid Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, 624193326Sed const CXXScopeSpec *SS, 625193326Sed SourceLocation TemplateKWLoc, 626193326Sed bool AllowTypeAnnotation) { 627193326Sed assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); 628193326Sed assert(Template && Tok.is(tok::identifier) && NextToken().is(tok::less) && 629193326Sed "Parser isn't at the beginning of a template-id"); 630193326Sed 631193326Sed // Consume the template-name. 632193326Sed IdentifierInfo *Name = Tok.getIdentifierInfo(); 633193326Sed SourceLocation TemplateNameLoc = ConsumeToken(); 634193326Sed 635193326Sed // Parse the enclosed template argument list. 636193326Sed SourceLocation LAngleLoc, RAngleLoc; 637193326Sed TemplateArgList TemplateArgs; 638193326Sed TemplateArgIsTypeList TemplateArgIsType; 639193326Sed TemplateArgLocationList TemplateArgLocations; 640193326Sed bool Invalid = ParseTemplateIdAfterTemplateName(Template, TemplateNameLoc, 641193326Sed SS, false, LAngleLoc, 642193326Sed TemplateArgs, 643193326Sed TemplateArgIsType, 644193326Sed TemplateArgLocations, 645193326Sed RAngleLoc); 646193326Sed 647193326Sed ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), 648193326Sed TemplateArgIsType.data(), 649193326Sed TemplateArgs.size()); 650193326Sed 651193326Sed if (Invalid) // FIXME: How to recover from a broken template-id? 652193326Sed return; 653193326Sed 654193326Sed // Build the annotation token. 655193326Sed if (TNK == TNK_Type_template && AllowTypeAnnotation) { 656193326Sed Action::TypeResult Type 657193326Sed = Actions.ActOnTemplateIdType(Template, TemplateNameLoc, 658193326Sed LAngleLoc, TemplateArgsPtr, 659193326Sed &TemplateArgLocations[0], 660193326Sed RAngleLoc); 661193326Sed if (Type.isInvalid()) // FIXME: better recovery? 662193326Sed return; 663193326Sed 664193326Sed Tok.setKind(tok::annot_typename); 665193326Sed Tok.setAnnotationValue(Type.get()); 666193326Sed if (SS && SS->isNotEmpty()) 667193326Sed Tok.setLocation(SS->getBeginLoc()); 668193326Sed else if (TemplateKWLoc.isValid()) 669193326Sed Tok.setLocation(TemplateKWLoc); 670193326Sed else 671193326Sed Tok.setLocation(TemplateNameLoc); 672193326Sed } else { 673193326Sed // Build a template-id annotation token that can be processed 674193326Sed // later. 675193326Sed Tok.setKind(tok::annot_template_id); 676193326Sed TemplateIdAnnotation *TemplateId 677193326Sed = TemplateIdAnnotation::Allocate(TemplateArgs.size()); 678193326Sed TemplateId->TemplateNameLoc = TemplateNameLoc; 679193326Sed TemplateId->Name = Name; 680193326Sed TemplateId->Template = Template.getAs<void*>(); 681193326Sed TemplateId->Kind = TNK; 682193326Sed TemplateId->LAngleLoc = LAngleLoc; 683193326Sed TemplateId->RAngleLoc = RAngleLoc; 684193326Sed void **Args = TemplateId->getTemplateArgs(); 685193326Sed bool *ArgIsType = TemplateId->getTemplateArgIsType(); 686193326Sed SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations(); 687193326Sed for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) { 688193326Sed Args[Arg] = TemplateArgs[Arg]; 689193326Sed ArgIsType[Arg] = TemplateArgIsType[Arg]; 690193326Sed ArgLocs[Arg] = TemplateArgLocations[Arg]; 691193326Sed } 692193326Sed Tok.setAnnotationValue(TemplateId); 693193326Sed if (TemplateKWLoc.isValid()) 694193326Sed Tok.setLocation(TemplateKWLoc); 695193326Sed else 696193326Sed Tok.setLocation(TemplateNameLoc); 697193326Sed 698193326Sed TemplateArgsPtr.release(); 699193326Sed } 700193326Sed 701193326Sed // Common fields for the annotation token 702193326Sed Tok.setAnnotationEndLoc(RAngleLoc); 703193326Sed 704193326Sed // In case the tokens were cached, have Preprocessor replace them with the 705193326Sed // annotation token. 706193326Sed PP.AnnotateCachedTokens(Tok); 707193326Sed} 708193326Sed 709193326Sed/// \brief Replaces a template-id annotation token with a type 710193326Sed/// annotation token. 711193326Sed/// 712193326Sed/// If there was a failure when forming the type from the template-id, 713193326Sed/// a type annotation token will still be created, but will have a 714193326Sed/// NULL type pointer to signify an error. 715193326Sedvoid Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) { 716193326Sed assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); 717193326Sed 718193326Sed TemplateIdAnnotation *TemplateId 719193326Sed = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 720193326Sed assert((TemplateId->Kind == TNK_Type_template || 721193326Sed TemplateId->Kind == TNK_Dependent_template_name) && 722193326Sed "Only works for type and dependent templates"); 723193326Sed 724193326Sed ASTTemplateArgsPtr TemplateArgsPtr(Actions, 725193326Sed TemplateId->getTemplateArgs(), 726193326Sed TemplateId->getTemplateArgIsType(), 727193326Sed TemplateId->NumArgs); 728193326Sed 729193326Sed Action::TypeResult Type 730193326Sed = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template), 731193326Sed TemplateId->TemplateNameLoc, 732193326Sed TemplateId->LAngleLoc, 733193326Sed TemplateArgsPtr, 734193326Sed TemplateId->getTemplateArgLocations(), 735193326Sed TemplateId->RAngleLoc); 736193326Sed // Create the new "type" annotation token. 737193326Sed Tok.setKind(tok::annot_typename); 738193326Sed Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get()); 739193326Sed if (SS && SS->isNotEmpty()) // it was a C++ qualified type name. 740193326Sed Tok.setLocation(SS->getBeginLoc()); 741193326Sed 742193326Sed // We might be backtracking, in which case we need to replace the 743193326Sed // template-id annotation token with the type annotation within the 744193326Sed // set of cached tokens. That way, we won't try to form the same 745193326Sed // class template specialization again. 746193326Sed PP.ReplaceLastTokenWithAnnotation(Tok); 747193326Sed TemplateId->Destroy(); 748193326Sed} 749193326Sed 750193326Sed/// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). 751193326Sed/// 752193326Sed/// template-argument: [C++ 14.2] 753194711Sed/// constant-expression 754193326Sed/// type-id 755193326Sed/// id-expression 756193326Sedvoid *Parser::ParseTemplateArgument(bool &ArgIsType) { 757193326Sed // C++ [temp.arg]p2: 758193326Sed // In a template-argument, an ambiguity between a type-id and an 759193326Sed // expression is resolved to a type-id, regardless of the form of 760193326Sed // the corresponding template-parameter. 761193326Sed // 762193326Sed // Therefore, we initially try to parse a type-id. 763193326Sed if (isCXXTypeId(TypeIdAsTemplateArgument)) { 764193326Sed ArgIsType = true; 765193326Sed TypeResult TypeArg = ParseTypeName(); 766193326Sed if (TypeArg.isInvalid()) 767193326Sed return 0; 768193326Sed return TypeArg.get(); 769193326Sed } 770193326Sed 771194711Sed OwningExprResult ExprArg = ParseConstantExpression(); 772193326Sed if (ExprArg.isInvalid() || !ExprArg.get()) 773193326Sed return 0; 774193326Sed 775193326Sed ArgIsType = false; 776193326Sed return ExprArg.release(); 777193326Sed} 778193326Sed 779193326Sed/// ParseTemplateArgumentList - Parse a C++ template-argument-list 780193326Sed/// (C++ [temp.names]). Returns true if there was an error. 781193326Sed/// 782193326Sed/// template-argument-list: [C++ 14.2] 783193326Sed/// template-argument 784193326Sed/// template-argument-list ',' template-argument 785193326Sedbool 786193326SedParser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs, 787193326Sed TemplateArgIsTypeList &TemplateArgIsType, 788193326Sed TemplateArgLocationList &TemplateArgLocations) { 789193326Sed while (true) { 790193326Sed bool IsType = false; 791193326Sed SourceLocation Loc = Tok.getLocation(); 792193326Sed void *Arg = ParseTemplateArgument(IsType); 793193326Sed if (Arg) { 794193326Sed TemplateArgs.push_back(Arg); 795193326Sed TemplateArgIsType.push_back(IsType); 796193326Sed TemplateArgLocations.push_back(Loc); 797193326Sed } else { 798193326Sed SkipUntil(tok::comma, tok::greater, true, true); 799193326Sed return true; 800193326Sed } 801193326Sed 802193326Sed // If the next token is a comma, consume it and keep reading 803193326Sed // arguments. 804193326Sed if (Tok.isNot(tok::comma)) break; 805193326Sed 806193326Sed // Consume the comma. 807193326Sed ConsumeToken(); 808193326Sed } 809193326Sed 810193326Sed return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater); 811193326Sed} 812193326Sed 813193326Sed/// \brief Parse a C++ explicit template instantiation 814193326Sed/// (C++ [temp.explicit]). 815193326Sed/// 816193326Sed/// explicit-instantiation: 817193326Sed/// 'template' declaration 818193326SedParser::DeclPtrTy 819193326SedParser::ParseExplicitInstantiation(SourceLocation TemplateLoc, 820193326Sed SourceLocation &DeclEnd) { 821193326Sed return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, 822193326Sed ParsedTemplateInfo(TemplateLoc), 823193326Sed DeclEnd, AS_none); 824193326Sed} 825