Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1 | //===--- ParseTemplate.cpp - Template Parsing -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements parsing of C++ templates. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/DeclSpec.h" |
| 17 | #include "clang/Sema/ParsedTemplate.h" |
| 18 | #include "clang/Sema/Scope.h" |
Chris Lattner | b2434d5 | 2009-12-10 00:45:15 +0000 | [diff] [blame] | 19 | #include "RAIIObjectsForParser.h" |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 22 | /// \brief Parse a template declaration, explicit instantiation, or |
| 23 | /// explicit specialization. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 24 | Decl * |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 25 | Parser::ParseDeclarationStartingWithTemplate(unsigned Context, |
| 26 | SourceLocation &DeclEnd, |
| 27 | AccessSpecifier AS) { |
| 28 | if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | return ParseExplicitInstantiation(SourceLocation(), ConsumeToken(), |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 30 | DeclEnd); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 31 | |
| 32 | return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS); |
| 33 | } |
| 34 | |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 35 | /// \brief RAII class that manages the template parameter depth. |
| 36 | namespace { |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 37 | class TemplateParameterDepthCounter { |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 38 | unsigned &Depth; |
| 39 | unsigned AddedLevels; |
| 40 | |
| 41 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | explicit TemplateParameterDepthCounter(unsigned &Depth) |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 43 | : Depth(Depth), AddedLevels(0) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 45 | ~TemplateParameterDepthCounter() { |
| 46 | Depth -= AddedLevels; |
| 47 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
| 49 | void operator++() { |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 50 | ++Depth; |
| 51 | ++AddedLevels; |
| 52 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 54 | operator unsigned() const { return Depth; } |
| 55 | }; |
| 56 | } |
| 57 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 58 | /// \brief Parse a template declaration or an explicit specialization. |
| 59 | /// |
| 60 | /// Template declarations include one or more template parameter lists |
| 61 | /// and either the function or class template declaration. Explicit |
| 62 | /// specializations contain one or more 'template < >' prefixes |
| 63 | /// followed by a (possibly templated) declaration. Since the |
| 64 | /// syntactic form of both features is nearly identical, we parse all |
| 65 | /// of the template headers together and let semantic analysis sort |
| 66 | /// the declarations from the explicit specializations. |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 67 | /// |
| 68 | /// template-declaration: [C++ temp] |
| 69 | /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 70 | /// |
| 71 | /// explicit-specialization: [ C++ temp.expl.spec] |
| 72 | /// 'template' '<' '>' declaration |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 73 | Decl * |
Anders Carlsson | dfbbdf6 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 74 | Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context, |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 75 | SourceLocation &DeclEnd, |
Anders Carlsson | dfbbdf6 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 76 | AccessSpecifier AS) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && |
| 78 | "Token does not start a template declaration."); |
| 79 | |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 80 | // Enter template-parameter scope. |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 81 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 82 | |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 83 | // Tell the action that names should be checked in the context of |
| 84 | // the declaration to come. |
| 85 | ParsingDeclRAIIObject ParsingTemplateParams(*this); |
| 86 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 87 | // Parse multiple levels of template headers within this template |
| 88 | // parameter scope, e.g., |
| 89 | // |
| 90 | // template<typename T> |
| 91 | // template<typename U> |
| 92 | // class A<T>::B { ... }; |
| 93 | // |
| 94 | // We parse multiple levels non-recursively so that we can build a |
| 95 | // single data structure containing all of the template parameter |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 96 | // lists to easily differentiate between the case above and: |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 97 | // |
| 98 | // template<typename T> |
| 99 | // class A { |
| 100 | // template<typename U> class B; |
| 101 | // }; |
| 102 | // |
| 103 | // In the first case, the action for declaring A<T>::B receives |
| 104 | // both template parameter lists. In the second case, the action for |
| 105 | // defining A<T>::B receives just the inner template parameter list |
| 106 | // (and retrieves the outer template parameter list from its |
| 107 | // context). |
Douglas Gregor | 468535e | 2009-08-20 18:46:05 +0000 | [diff] [blame] | 108 | bool isSpecialization = true; |
Douglas Gregor | 1d0015f | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 109 | bool LastParamListWasEmpty = false; |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 110 | TemplateParameterLists ParamLists; |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 111 | TemplateParameterDepthCounter Depth(TemplateParameterDepth); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 112 | do { |
| 113 | // Consume the 'export', if any. |
| 114 | SourceLocation ExportLoc; |
| 115 | if (Tok.is(tok::kw_export)) { |
| 116 | ExportLoc = ConsumeToken(); |
| 117 | } |
| 118 | |
| 119 | // Consume the 'template', which should be here. |
| 120 | SourceLocation TemplateLoc; |
| 121 | if (Tok.is(tok::kw_template)) { |
| 122 | TemplateLoc = ConsumeToken(); |
| 123 | } else { |
| 124 | Diag(Tok.getLocation(), diag::err_expected_template); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 125 | return 0; |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 126 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 128 | // Parse the '<' template-parameter-list '>' |
| 129 | SourceLocation LAngleLoc, RAngleLoc; |
John McCall | 572ccbc | 2010-08-23 06:53:58 +0000 | [diff] [blame] | 130 | llvm::SmallVector<Decl*, 4> TemplateParams; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | if (ParseTemplateParameters(Depth, TemplateParams, LAngleLoc, |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 132 | RAngleLoc)) { |
| 133 | // Skip until the semi-colon or a }. |
| 134 | SkipUntil(tok::r_brace, true, true); |
| 135 | if (Tok.is(tok::semi)) |
| 136 | ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 137 | return 0; |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 138 | } |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 139 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 140 | ParamLists.push_back( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | Actions.ActOnTemplateParameterList(Depth, ExportLoc, |
| 142 | TemplateLoc, LAngleLoc, |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 143 | TemplateParams.data(), |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 144 | TemplateParams.size(), RAngleLoc)); |
Douglas Gregor | a3dff8e | 2009-08-24 23:03:25 +0000 | [diff] [blame] | 145 | |
| 146 | if (!TemplateParams.empty()) { |
| 147 | isSpecialization = false; |
| 148 | ++Depth; |
Douglas Gregor | 1d0015f | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 149 | } else { |
| 150 | LastParamListWasEmpty = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 152 | } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); |
| 153 | |
| 154 | // Parse the actual template declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | return ParseSingleDeclarationAfterTemplate(Context, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 156 | ParsedTemplateInfo(&ParamLists, |
Douglas Gregor | 1d0015f | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 157 | isSpecialization, |
| 158 | LastParamListWasEmpty), |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 159 | ParsingTemplateParams, |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 160 | DeclEnd, AS); |
| 161 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 162 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 163 | /// \brief Parse a single declaration that declares a template, |
| 164 | /// template specialization, or explicit instantiation of a template. |
| 165 | /// |
| 166 | /// \param TemplateParams if non-NULL, the template parameter lists |
| 167 | /// that preceded this declaration. In this case, the declaration is a |
| 168 | /// template declaration, out-of-line definition of a template, or an |
| 169 | /// explicit template specialization. When NULL, the declaration is an |
| 170 | /// explicit template instantiation. |
| 171 | /// |
| 172 | /// \param TemplateLoc when TemplateParams is NULL, the location of |
| 173 | /// the 'template' keyword that indicates that we have an explicit |
| 174 | /// template instantiation. |
| 175 | /// |
| 176 | /// \param DeclEnd will receive the source location of the last token |
| 177 | /// within this declaration. |
| 178 | /// |
| 179 | /// \param AS the access specifier associated with this |
| 180 | /// declaration. Will be AS_none for namespace-scope declarations. |
| 181 | /// |
| 182 | /// \returns the new declaration. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 183 | Decl * |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 184 | Parser::ParseSingleDeclarationAfterTemplate( |
| 185 | unsigned Context, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 186 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 187 | ParsingDeclRAIIObject &DiagsFromTParams, |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 188 | SourceLocation &DeclEnd, |
| 189 | AccessSpecifier AS) { |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 190 | assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
| 191 | "Template information required"); |
| 192 | |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 193 | if (Context == Declarator::MemberContext) { |
| 194 | // We are parsing a member template. |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 195 | ParseCXXClassMemberDeclaration(AS, TemplateInfo, &DiagsFromTParams); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 196 | return 0; |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 197 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame^] | 199 | ParsedAttributesWithRange prefixAttrs; |
| 200 | MaybeParseCXX0XAttributes(prefixAttrs); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 201 | |
| 202 | if (Tok.is(tok::kw_using)) |
| 203 | return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame^] | 204 | prefixAttrs); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 205 | |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 206 | // Parse the declaration specifiers, stealing the accumulated |
| 207 | // diagnostics from the template parameters. |
| 208 | ParsingDeclSpec DS(DiagsFromTParams); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 209 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame^] | 210 | DS.takeAttributesFrom(prefixAttrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 212 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, |
| 213 | getDeclSpecContextFromDeclaratorContext(Context)); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 214 | |
| 215 | if (Tok.is(tok::semi)) { |
| 216 | DeclEnd = ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 217 | Decl *Decl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 218 | DS.complete(Decl); |
| 219 | return Decl; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // Parse the declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 223 | ParsingDeclarator DeclaratorInfo(*this, DS, (Declarator::TheContext)Context); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 224 | ParseDeclarator(DeclaratorInfo); |
| 225 | // Error parsing the declarator? |
| 226 | if (!DeclaratorInfo.hasName()) { |
| 227 | // If so, skip until the semi-colon or a }. |
| 228 | SkipUntil(tok::r_brace, true, true); |
| 229 | if (Tok.is(tok::semi)) |
| 230 | ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 231 | return 0; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 232 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 234 | // If we have a declaration or declarator list, handle it. |
| 235 | if (isDeclarationAfterDeclarator()) { |
| 236 | // Parse this declaration. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 237 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, |
| 238 | TemplateInfo); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 239 | |
| 240 | if (Tok.is(tok::comma)) { |
| 241 | Diag(Tok, diag::err_multiple_template_declarators) |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 242 | << (int)TemplateInfo.Kind; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 243 | SkipUntil(tok::semi, true, false); |
| 244 | return ThisDecl; |
| 245 | } |
| 246 | |
| 247 | // Eat the semi colon after the declaration. |
John McCall | ef50e99 | 2009-07-31 02:20:35 +0000 | [diff] [blame] | 248 | ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 249 | DS.complete(ThisDecl); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 250 | return ThisDecl; |
| 251 | } |
| 252 | |
| 253 | if (DeclaratorInfo.isFunctionDeclarator() && |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 254 | isStartOfFunctionDefinition(DeclaratorInfo)) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 255 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 256 | Diag(Tok, diag::err_function_declared_typedef); |
| 257 | |
| 258 | if (Tok.is(tok::l_brace)) { |
| 259 | // This recovery skips the entire function body. It would be nice |
| 260 | // to simply call ParseFunctionDefinition() below, however Sema |
| 261 | // assumes the declarator represents a function, not a typedef. |
| 262 | ConsumeBrace(); |
| 263 | SkipUntil(tok::r_brace, true); |
| 264 | } else { |
| 265 | SkipUntil(tok::semi); |
| 266 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 267 | return 0; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 268 | } |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 269 | return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (DeclaratorInfo.isFunctionDeclarator()) |
| 273 | Diag(Tok, diag::err_expected_fn_body); |
| 274 | else |
| 275 | Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); |
| 276 | SkipUntil(tok::semi); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 277 | return 0; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /// ParseTemplateParameters - Parses a template-parameter-list enclosed in |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 281 | /// angle brackets. Depth is the depth of this template-parameter-list, which |
| 282 | /// is the number of template headers directly enclosing this template header. |
| 283 | /// TemplateParams is the current list of template parameters we're building. |
| 284 | /// The template parameter we parse will be added to this list. LAngleLoc and |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | /// RAngleLoc will receive the positions of the '<' and '>', respectively, |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 286 | /// that enclose this template parameter list. |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 287 | /// |
| 288 | /// \returns true if an error occurred, false otherwise. |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 289 | bool Parser::ParseTemplateParameters(unsigned Depth, |
John McCall | 572ccbc | 2010-08-23 06:53:58 +0000 | [diff] [blame] | 290 | llvm::SmallVectorImpl<Decl*> &TemplateParams, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 291 | SourceLocation &LAngleLoc, |
| 292 | SourceLocation &RAngleLoc) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 293 | // Get the template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | if (!Tok.is(tok::less)) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 295 | Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 296 | return true; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 297 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 298 | LAngleLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 300 | // Try to parse the template parameter list. |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 301 | if (Tok.is(tok::greater)) |
| 302 | RAngleLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | else if (ParseTemplateParameterList(Depth, TemplateParams)) { |
| 304 | if (!Tok.is(tok::greater)) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 305 | Diag(Tok.getLocation(), diag::err_expected_greater); |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 306 | return true; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 307 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 308 | RAngleLoc = ConsumeToken(); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 309 | } |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 310 | return false; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /// ParseTemplateParameterList - Parse a template parameter list. If |
| 314 | /// the parsing fails badly (i.e., closing bracket was left out), this |
| 315 | /// will try to put the token stream in a reasonable position (closing |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | /// a statement, etc.) and return false. |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 317 | /// |
| 318 | /// template-parameter-list: [C++ temp] |
| 319 | /// template-parameter |
| 320 | /// template-parameter-list ',' template-parameter |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | bool |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 322 | Parser::ParseTemplateParameterList(unsigned Depth, |
John McCall | 572ccbc | 2010-08-23 06:53:58 +0000 | [diff] [blame] | 323 | llvm::SmallVectorImpl<Decl*> &TemplateParams) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | while (1) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 325 | if (Decl *TmpParam |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 326 | = ParseTemplateParameter(Depth, TemplateParams.size())) { |
| 327 | TemplateParams.push_back(TmpParam); |
| 328 | } else { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 329 | // If we failed to parse a template parameter, skip until we find |
| 330 | // a comma or closing brace. |
| 331 | SkipUntil(tok::comma, tok::greater, true, true); |
| 332 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 334 | // Did we find a comma or the end of the template parmeter list? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | if (Tok.is(tok::comma)) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 336 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | } else if (Tok.is(tok::greater)) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 338 | // Don't consume this... that's done by template parser. |
| 339 | break; |
| 340 | } else { |
| 341 | // Somebody probably forgot to close the template. Skip ahead and |
| 342 | // try to get out of the expression. This error is currently |
| 343 | // subsumed by whatever goes on in ParseTemplateParameter. |
| 344 | // TODO: This could match >>, and it would be nice to avoid those |
| 345 | // silly errors with template <vec<T>>. |
Douglas Gregor | b048402 | 2010-10-15 01:15:58 +0000 | [diff] [blame] | 346 | Diag(Tok.getLocation(), diag::err_expected_comma_greater); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 347 | SkipUntil(tok::greater, true, true); |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
Douglas Gregor | 26aedb7 | 2009-11-21 02:07:55 +0000 | [diff] [blame] | 354 | /// \brief Determine whether the parser is at the start of a template |
| 355 | /// type parameter. |
| 356 | bool Parser::isStartOfTemplateTypeParameter() { |
Douglas Gregor | 71b209d | 2010-06-04 07:30:15 +0000 | [diff] [blame] | 357 | if (Tok.is(tok::kw_class)) { |
| 358 | // "class" may be the start of an elaborated-type-specifier or a |
| 359 | // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter. |
| 360 | switch (NextToken().getKind()) { |
| 361 | case tok::equal: |
| 362 | case tok::comma: |
| 363 | case tok::greater: |
| 364 | case tok::greatergreater: |
| 365 | case tok::ellipsis: |
| 366 | return true; |
| 367 | |
| 368 | case tok::identifier: |
| 369 | // This may be either a type-parameter or an elaborated-type-specifier. |
| 370 | // We have to look further. |
| 371 | break; |
| 372 | |
| 373 | default: |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | switch (GetLookAheadToken(2).getKind()) { |
| 378 | case tok::equal: |
| 379 | case tok::comma: |
| 380 | case tok::greater: |
| 381 | case tok::greatergreater: |
| 382 | return true; |
| 383 | |
| 384 | default: |
| 385 | return false; |
| 386 | } |
| 387 | } |
Douglas Gregor | 26aedb7 | 2009-11-21 02:07:55 +0000 | [diff] [blame] | 388 | |
| 389 | if (Tok.isNot(tok::kw_typename)) |
| 390 | return false; |
| 391 | |
| 392 | // C++ [temp.param]p2: |
| 393 | // There is no semantic difference between class and typename in a |
| 394 | // template-parameter. typename followed by an unqualified-id |
| 395 | // names a template type parameter. typename followed by a |
| 396 | // qualified-id denotes the type in a non-type |
| 397 | // parameter-declaration. |
| 398 | Token Next = NextToken(); |
| 399 | |
| 400 | // If we have an identifier, skip over it. |
| 401 | if (Next.getKind() == tok::identifier) |
| 402 | Next = GetLookAheadToken(2); |
| 403 | |
| 404 | switch (Next.getKind()) { |
| 405 | case tok::equal: |
| 406 | case tok::comma: |
| 407 | case tok::greater: |
| 408 | case tok::greatergreater: |
| 409 | case tok::ellipsis: |
| 410 | return true; |
| 411 | |
| 412 | default: |
| 413 | return false; |
| 414 | } |
| 415 | } |
| 416 | |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 417 | /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). |
| 418 | /// |
| 419 | /// template-parameter: [C++ temp.param] |
| 420 | /// type-parameter |
| 421 | /// parameter-declaration |
| 422 | /// |
| 423 | /// type-parameter: (see below) |
Anders Carlsson | f986ba7 | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 424 | /// 'class' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 425 | /// 'class' identifier[opt] '=' type-id |
Anders Carlsson | f986ba7 | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 426 | /// 'typename' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 427 | /// 'typename' identifier[opt] '=' type-id |
Anders Carlsson | f986ba7 | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 428 | /// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 429 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 430 | Decl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | 26aedb7 | 2009-11-21 02:07:55 +0000 | [diff] [blame] | 431 | if (isStartOfTemplateTypeParameter()) |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 432 | return ParseTypeParameter(Depth, Position); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
| 434 | if (Tok.is(tok::kw_template)) |
Chris Lattner | a21db61 | 2009-01-04 23:51:17 +0000 | [diff] [blame] | 435 | return ParseTemplateTemplateParameter(Depth, Position); |
| 436 | |
| 437 | // If it's none of the above, then it must be a parameter declaration. |
| 438 | // NOTE: This will pick up errors in the closure of the template parameter |
| 439 | // list (e.g., template < ; Check here to implement >> style closures. |
| 440 | return ParseNonTypeTemplateParameter(Depth, Position); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). |
| 444 | /// Other kinds of template parameters are parsed in |
| 445 | /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. |
| 446 | /// |
| 447 | /// type-parameter: [C++ temp.param] |
Anders Carlsson | f986ba7 | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 448 | /// 'class' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 449 | /// 'class' identifier[opt] '=' type-id |
Anders Carlsson | f986ba7 | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 450 | /// 'typename' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 451 | /// 'typename' identifier[opt] '=' type-id |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 452 | Decl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 453 | assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | "A type-parameter starts with 'class' or 'typename'"); |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 455 | |
| 456 | // Consume the 'class' or 'typename' keyword. |
| 457 | bool TypenameKeyword = Tok.is(tok::kw_typename); |
| 458 | SourceLocation KeyLoc = ConsumeToken(); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 459 | |
Anders Carlsson | 01e9e93 | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 460 | // Grab the ellipsis (if given). |
| 461 | bool Ellipsis = false; |
| 462 | SourceLocation EllipsisLoc; |
Anders Carlsson | f986ba7 | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 463 | if (Tok.is(tok::ellipsis)) { |
Anders Carlsson | 01e9e93 | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 464 | Ellipsis = true; |
| 465 | EllipsisLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
| 467 | if (!getLang().CPlusPlus0x) |
Anders Carlsson | f986ba7 | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 468 | Diag(EllipsisLoc, diag::err_variadic_templates); |
Anders Carlsson | 01e9e93 | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 469 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 471 | // Grab the template parameter name (if given) |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 472 | SourceLocation NameLoc; |
| 473 | IdentifierInfo* ParamName = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 475 | ParamName = Tok.getIdentifierInfo(); |
| 476 | NameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || |
| 478 | Tok.is(tok::greater)) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 479 | // Unnamed template parameter. Don't have to do anything here, just |
| 480 | // don't consume this token. |
| 481 | } else { |
| 482 | Diag(Tok.getLocation(), diag::err_expected_ident); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 483 | return 0; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 484 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 486 | // Grab a default argument (if available). |
| 487 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before |
| 488 | // we introduce the type parameter into the local scope. |
| 489 | SourceLocation EqualLoc; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 490 | ParsedType DefaultArg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | if (Tok.is(tok::equal)) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 492 | EqualLoc = ConsumeToken(); |
| 493 | DefaultArg = ParseTypeName().get(); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 494 | } |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 496 | return Actions.ActOnTypeParameter(getCurScope(), TypenameKeyword, Ellipsis, |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 497 | EllipsisLoc, KeyLoc, ParamName, NameLoc, |
| 498 | Depth, Position, EqualLoc, DefaultArg); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /// ParseTemplateTemplateParameter - Handle the parsing of template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | /// template parameters. |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 503 | /// |
| 504 | /// type-parameter: [C++ temp.param] |
| 505 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] |
| 506 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 507 | Decl * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 508 | Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 509 | assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); |
| 510 | |
| 511 | // Handle the template <...> part. |
| 512 | SourceLocation TemplateLoc = ConsumeToken(); |
John McCall | 572ccbc | 2010-08-23 06:53:58 +0000 | [diff] [blame] | 513 | llvm::SmallVector<Decl*,8> TemplateParams; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 514 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 85e8b3e | 2009-02-10 19:52:54 +0000 | [diff] [blame] | 515 | { |
| 516 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | if (ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 518 | RAngleLoc)) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 519 | return 0; |
Douglas Gregor | 85e8b3e | 2009-02-10 19:52:54 +0000 | [diff] [blame] | 520 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Generate a meaningful error if the user forgot to put class before the |
| 524 | // identifier, comma, or greater. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | if (!Tok.is(tok::kw_class)) { |
| 526 | Diag(Tok.getLocation(), diag::err_expected_class_before) |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 527 | << PP.getSpelling(Tok); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 528 | return 0; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 529 | } |
| 530 | SourceLocation ClassLoc = ConsumeToken(); |
| 531 | |
| 532 | // Get the identifier, if given. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 533 | SourceLocation NameLoc; |
| 534 | IdentifierInfo* ParamName = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 536 | ParamName = Tok.getIdentifierInfo(); |
| 537 | NameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | } else if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 539 | // Unnamed template parameter. Don't have to do anything here, just |
| 540 | // don't consume this token. |
| 541 | } else { |
| 542 | Diag(Tok.getLocation(), diag::err_expected_ident); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 543 | return 0; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | TemplateParamsTy *ParamList = |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 547 | Actions.ActOnTemplateParameterList(Depth, SourceLocation(), |
| 548 | TemplateLoc, LAngleLoc, |
Douglas Gregor | a02bb37 | 2010-10-21 17:26:49 +0000 | [diff] [blame] | 549 | TemplateParams.data(), |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 550 | TemplateParams.size(), |
| 551 | RAngleLoc); |
| 552 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 553 | // Grab a default argument (if available). |
| 554 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before |
| 555 | // we introduce the template parameter into the local scope. |
| 556 | SourceLocation EqualLoc; |
| 557 | ParsedTemplateArgument DefaultArg; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 558 | if (Tok.is(tok::equal)) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 559 | EqualLoc = ConsumeToken(); |
| 560 | DefaultArg = ParseTemplateTemplateArgument(); |
| 561 | if (DefaultArg.isInvalid()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 562 | Diag(Tok.getLocation(), |
| 563 | diag::err_default_template_template_parameter_not_template); |
Nuno Lopes | 221c1fd | 2009-12-10 00:07:02 +0000 | [diff] [blame] | 564 | static const tok::TokenKind EndToks[] = { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 565 | tok::comma, tok::greater, tok::greatergreater |
| 566 | }; |
| 567 | SkipUntil(EndToks, 3, true, true); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 568 | } |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 569 | } |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 571 | return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc, |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 572 | ParamList, ParamName, |
| 573 | NameLoc, Depth, Position, |
| 574 | EqualLoc, DefaultArg); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /// ParseNonTypeTemplateParameter - Handle the parsing of non-type |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | /// template parameters (e.g., in "template<int Size> class array;"). |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 579 | /// |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 580 | /// template-parameter: |
| 581 | /// ... |
| 582 | /// parameter-declaration |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 583 | Decl * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 584 | Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 585 | SourceLocation StartLoc = Tok.getLocation(); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 586 | |
| 587 | // Parse the declaration-specifiers (i.e., the type). |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 588 | // FIXME: The type should probably be restricted in some way... Not all |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 589 | // declarators (parts of declarators?) are accepted for parameters. |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 590 | DeclSpec DS; |
| 591 | ParseDeclarationSpecifiers(DS); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 592 | |
| 593 | // Parse this as a typename. |
Douglas Gregor | f558618 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 594 | Declarator ParamDecl(DS, Declarator::TemplateParamContext); |
| 595 | ParseDeclarator(ParamDecl); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 596 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 597 | // This probably shouldn't happen - and it's more of a Sema thing, but |
| 598 | // basically we didn't parse the type name because we couldn't associate |
| 599 | // it with an AST node. we should just skip to the comma or greater. |
| 600 | // TODO: This is currently a placeholder for some kind of Sema Error. |
| 601 | Diag(Tok.getLocation(), diag::err_parse_error); |
| 602 | SkipUntil(tok::comma, tok::greater, true, true); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 603 | return 0; |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 606 | // If there is a default value, parse it. |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 607 | // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before |
| 608 | // we introduce the template parameter into the local scope. |
| 609 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 610 | ExprResult DefaultArg; |
Chris Lattner | b5134c0 | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 611 | if (Tok.is(tok::equal)) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 612 | EqualLoc = ConsumeToken(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 613 | |
| 614 | // C++ [temp.param]p15: |
| 615 | // When parsing a default template-argument for a non-type |
| 616 | // template-parameter, the first non-nested > is taken as the |
| 617 | // end of the template-parameter-list rather than a greater-than |
| 618 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 620 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 621 | DefaultArg = ParseAssignmentExpression(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 622 | if (DefaultArg.isInvalid()) |
| 623 | SkipUntil(tok::comma, tok::greater, true, true); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 624 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 626 | // Create the parameter. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 627 | return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl, |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 628 | Depth, Position, EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 629 | DefaultArg.take()); |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 630 | } |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 631 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 632 | /// \brief Parses a template-id that after the template name has |
| 633 | /// already been parsed. |
| 634 | /// |
| 635 | /// This routine takes care of parsing the enclosed template argument |
| 636 | /// list ('<' template-parameter-list [opt] '>') and placing the |
| 637 | /// results into a form that can be transferred to semantic analysis. |
| 638 | /// |
| 639 | /// \param Template the template declaration produced by isTemplateName |
| 640 | /// |
| 641 | /// \param TemplateNameLoc the source location of the template name |
| 642 | /// |
| 643 | /// \param SS if non-NULL, the nested-name-specifier preceding the |
| 644 | /// template name. |
| 645 | /// |
| 646 | /// \param ConsumeLastToken if true, then we will consume the last |
| 647 | /// token that forms the template-id. Otherwise, we will leave the |
| 648 | /// last token in the stream (e.g., so that it can be replaced with an |
| 649 | /// annotation token). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | bool |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 651 | Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | SourceLocation TemplateNameLoc, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 653 | const CXXScopeSpec *SS, |
| 654 | bool ConsumeLastToken, |
| 655 | SourceLocation &LAngleLoc, |
| 656 | TemplateArgList &TemplateArgs, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 657 | SourceLocation &RAngleLoc) { |
| 658 | assert(Tok.is(tok::less) && "Must have already parsed the template-name"); |
| 659 | |
| 660 | // Consume the '<'. |
| 661 | LAngleLoc = ConsumeToken(); |
| 662 | |
| 663 | // Parse the optional template-argument-list. |
| 664 | bool Invalid = false; |
| 665 | { |
| 666 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
| 667 | if (Tok.isNot(tok::greater)) |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 668 | Invalid = ParseTemplateArgumentList(TemplateArgs); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 669 | |
| 670 | if (Invalid) { |
| 671 | // Try to find the closing '>'. |
| 672 | SkipUntil(tok::greater, true, !ConsumeLastToken); |
| 673 | |
| 674 | return true; |
| 675 | } |
| 676 | } |
| 677 | |
Eli Friedman | affd5fd | 2009-12-27 22:31:18 +0000 | [diff] [blame] | 678 | if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) { |
| 679 | Diag(Tok.getLocation(), diag::err_expected_greater); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 680 | return true; |
Eli Friedman | affd5fd | 2009-12-27 22:31:18 +0000 | [diff] [blame] | 681 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | cbb45d0 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 683 | // Determine the location of the '>' or '>>'. Only consume this |
| 684 | // token if the caller asked us to. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 685 | RAngleLoc = Tok.getLocation(); |
| 686 | |
Douglas Gregor | cbb45d0 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 687 | if (Tok.is(tok::greatergreater)) { |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 688 | if (!getLang().CPlusPlus0x) { |
| 689 | const char *ReplaceStr = "> >"; |
| 690 | if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater)) |
| 691 | ReplaceStr = "> > "; |
| 692 | |
| 693 | Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 694 | << FixItHint::CreateReplacement( |
Douglas Gregor | 96977da | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 695 | SourceRange(Tok.getLocation()), ReplaceStr); |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 696 | } |
Douglas Gregor | cbb45d0 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 697 | |
| 698 | Tok.setKind(tok::greater); |
| 699 | if (!ConsumeLastToken) { |
| 700 | // Since we're not supposed to consume the '>>' token, we need |
| 701 | // to insert a second '>' token after the first. |
| 702 | PP.EnterToken(Tok); |
| 703 | } |
| 704 | } else if (ConsumeLastToken) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 705 | ConsumeToken(); |
| 706 | |
| 707 | return false; |
| 708 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 710 | /// \brief Replace the tokens that form a simple-template-id with an |
| 711 | /// annotation token containing the complete template-id. |
| 712 | /// |
| 713 | /// The first token in the stream must be the name of a template that |
| 714 | /// is followed by a '<'. This routine will parse the complete |
| 715 | /// simple-template-id and replace the tokens with a single annotation |
| 716 | /// token with one of two different kinds: if the template-id names a |
| 717 | /// type (and \p AllowTypeAnnotation is true), the annotation token is |
| 718 | /// a type annotation that includes the optional nested-name-specifier |
| 719 | /// (\p SS). Otherwise, the annotation token is a template-id |
| 720 | /// annotation that does not include the optional |
| 721 | /// nested-name-specifier. |
| 722 | /// |
| 723 | /// \param Template the declaration of the template named by the first |
| 724 | /// token (an identifier), as returned from \c Action::isTemplateName(). |
| 725 | /// |
| 726 | /// \param TemplateNameKind the kind of template that \p Template |
| 727 | /// refers to, as returned from \c Action::isTemplateName(). |
| 728 | /// |
| 729 | /// \param SS if non-NULL, the nested-name-specifier that precedes |
| 730 | /// this template name. |
| 731 | /// |
| 732 | /// \param TemplateKWLoc if valid, specifies that this template-id |
| 733 | /// annotation was preceded by the 'template' keyword and gives the |
| 734 | /// location of that keyword. If invalid (the default), then this |
| 735 | /// template-id was not preceded by a 'template' keyword. |
| 736 | /// |
| 737 | /// \param AllowTypeAnnotation if true (the default), then a |
| 738 | /// simple-template-id that refers to a class template, template |
| 739 | /// template parameter, or other template that produces a type will be |
| 740 | /// replaced with a type annotation token. Otherwise, the |
| 741 | /// simple-template-id is always replaced with a template-id |
| 742 | /// annotation token. |
Chris Lattner | 5558e9f | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 743 | /// |
| 744 | /// If an unrecoverable parse error occurs and no annotation token can be |
| 745 | /// formed, this function returns true. |
| 746 | /// |
| 747 | bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | const CXXScopeSpec *SS, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 749 | UnqualifiedId &TemplateName, |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 750 | SourceLocation TemplateKWLoc, |
| 751 | bool AllowTypeAnnotation) { |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 752 | assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 753 | assert(Template && Tok.is(tok::less) && |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 754 | "Parser isn't at the beginning of a template-id"); |
| 755 | |
| 756 | // Consume the template-name. |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 757 | SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin(); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 759 | // Parse the enclosed template argument list. |
| 760 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 67b556a | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 761 | TemplateArgList TemplateArgs; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 762 | bool Invalid = ParseTemplateIdAfterTemplateName(Template, |
| 763 | TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | SS, false, LAngleLoc, |
| 765 | TemplateArgs, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 766 | RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Chris Lattner | 5558e9f | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 768 | if (Invalid) { |
| 769 | // If we failed to parse the template ID but skipped ahead to a >, we're not |
| 770 | // going to be able to form a token annotation. Eat the '>' if present. |
| 771 | if (Tok.is(tok::greater)) |
| 772 | ConsumeToken(); |
| 773 | return true; |
| 774 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 775 | |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 776 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 777 | TemplateArgs.size()); |
Douglas Gregor | 0db4ccd | 2009-02-09 21:04:56 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 779 | // Build the annotation token. |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 780 | if (TNK == TNK_Type_template && AllowTypeAnnotation) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 781 | TypeResult Type |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 782 | = Actions.ActOnTemplateIdType(Template, TemplateNameLoc, |
| 783 | LAngleLoc, TemplateArgsPtr, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 784 | RAngleLoc); |
Chris Lattner | 5558e9f | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 785 | if (Type.isInvalid()) { |
| 786 | // If we failed to parse the template ID but skipped ahead to a >, we're not |
| 787 | // going to be able to form a token annotation. Eat the '>' if present. |
| 788 | if (Tok.is(tok::greater)) |
| 789 | ConsumeToken(); |
| 790 | return true; |
| 791 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 792 | |
| 793 | Tok.setKind(tok::annot_typename); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 794 | setTypeAnnotation(Tok, Type.get()); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 795 | if (SS && SS->isNotEmpty()) |
| 796 | Tok.setLocation(SS->getBeginLoc()); |
| 797 | else if (TemplateKWLoc.isValid()) |
| 798 | Tok.setLocation(TemplateKWLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | else |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 800 | Tok.setLocation(TemplateNameLoc); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 801 | } else { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 802 | // Build a template-id annotation token that can be processed |
| 803 | // later. |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 804 | Tok.setKind(tok::annot_template_id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 806 | = TemplateIdAnnotation::Allocate(TemplateArgs.size()); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 807 | TemplateId->TemplateNameLoc = TemplateNameLoc; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 808 | if (TemplateName.getKind() == UnqualifiedId::IK_Identifier) { |
| 809 | TemplateId->Name = TemplateName.Identifier; |
| 810 | TemplateId->Operator = OO_None; |
| 811 | } else { |
| 812 | TemplateId->Name = 0; |
| 813 | TemplateId->Operator = TemplateName.OperatorFunctionId.Operator; |
| 814 | } |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 815 | TemplateId->Template = Template; |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 816 | TemplateId->Kind = TNK; |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 817 | TemplateId->LAngleLoc = LAngleLoc; |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 818 | TemplateId->RAngleLoc = RAngleLoc; |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 819 | ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); |
| 820 | for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 821 | Args[Arg] = TemplateArgs[Arg]; |
| 822 | Tok.setAnnotationValue(TemplateId); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 823 | if (TemplateKWLoc.isValid()) |
| 824 | Tok.setLocation(TemplateKWLoc); |
| 825 | else |
| 826 | Tok.setLocation(TemplateNameLoc); |
| 827 | |
| 828 | TemplateArgsPtr.release(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | // Common fields for the annotation token |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 832 | Tok.setAnnotationEndLoc(RAngleLoc); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 833 | |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 834 | // In case the tokens were cached, have Preprocessor replace them with the |
| 835 | // annotation token. |
| 836 | PP.AnnotateCachedTokens(Tok); |
Chris Lattner | 5558e9f | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 837 | return false; |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 840 | /// \brief Replaces a template-id annotation token with a type |
| 841 | /// annotation token. |
| 842 | /// |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 843 | /// If there was a failure when forming the type from the template-id, |
| 844 | /// a type annotation token will still be created, but will have a |
| 845 | /// NULL type pointer to signify an error. |
| 846 | void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 847 | assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); |
| 848 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 850 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 851 | assert((TemplateId->Kind == TNK_Type_template || |
| 852 | TemplateId->Kind == TNK_Dependent_template_name) && |
| 853 | "Only works for type and dependent templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
| 855 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 856 | TemplateId->getTemplateArgs(), |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 857 | TemplateId->NumArgs); |
| 858 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 859 | TypeResult Type |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 860 | = Actions.ActOnTemplateIdType(TemplateId->Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 861 | TemplateId->TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | TemplateId->LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 863 | TemplateArgsPtr, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 864 | TemplateId->RAngleLoc); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 865 | // Create the new "type" annotation token. |
| 866 | Tok.setKind(tok::annot_typename); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 867 | setTypeAnnotation(Tok, Type.isInvalid() ? ParsedType() : Type.get()); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 868 | if (SS && SS->isNotEmpty()) // it was a C++ qualified type name. |
| 869 | Tok.setLocation(SS->getBeginLoc()); |
Sebastian Redl | b0e3e1b | 2010-02-08 19:35:18 +0000 | [diff] [blame] | 870 | // End location stays the same |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 871 | |
Douglas Gregor | 3552259 | 2009-11-04 18:18:19 +0000 | [diff] [blame] | 872 | // Replace the template-id annotation token, and possible the scope-specifier |
| 873 | // that precedes it, with the typename annotation token. |
| 874 | PP.AnnotateCachedTokens(Tok); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 875 | TemplateId->Destroy(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 878 | /// \brief Determine whether the given token can end a template argument. |
Benjamin Kramer | 2c9a91c | 2009-11-10 21:29:56 +0000 | [diff] [blame] | 879 | static bool isEndOfTemplateArgument(Token Tok) { |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 880 | return Tok.is(tok::comma) || Tok.is(tok::greater) || |
| 881 | Tok.is(tok::greatergreater); |
| 882 | } |
| 883 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 884 | /// \brief Parse a C++ template template argument. |
| 885 | ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { |
| 886 | if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) && |
| 887 | !Tok.is(tok::annot_cxxscope)) |
| 888 | return ParsedTemplateArgument(); |
| 889 | |
| 890 | // C++0x [temp.arg.template]p1: |
| 891 | // A template-argument for a template template-parameter shall be the name |
| 892 | // of a class template or a template alias, expressed as id-expression. |
| 893 | // |
Douglas Gregor | c998409 | 2009-11-12 00:03:40 +0000 | [diff] [blame] | 894 | // We parse an id-expression that refers to a class template or template |
| 895 | // alias. The grammar we parse is: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 896 | // |
| 897 | // nested-name-specifier[opt] template[opt] identifier |
| 898 | // |
| 899 | // followed by a token that terminates a template argument, such as ',', |
| 900 | // '>', or (in some cases) '>>'. |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 901 | CXXScopeSpec SS; // nested-name-specifier, if present |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 902 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 903 | /*EnteringContext=*/false); |
| 904 | |
| 905 | if (SS.isSet() && Tok.is(tok::kw_template)) { |
| 906 | // Parse the optional 'template' keyword following the |
| 907 | // nested-name-specifier. |
| 908 | SourceLocation TemplateLoc = ConsumeToken(); |
| 909 | |
| 910 | if (Tok.is(tok::identifier)) { |
| 911 | // We appear to have a dependent template name. |
| 912 | UnqualifiedId Name; |
| 913 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 914 | ConsumeToken(); // the identifier |
| 915 | |
| 916 | // If the next token signals the end of a template argument, |
| 917 | // then we have a dependent template name that could be a template |
| 918 | // template argument. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 919 | TemplateTy Template; |
| 920 | if (isEndOfTemplateArgument(Tok) && |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 921 | Actions.ActOnDependentTemplateName(getCurScope(), TemplateLoc, |
| 922 | SS, Name, |
| 923 | /*ObjectType=*/ ParsedType(), |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 924 | /*EnteringContext=*/false, |
| 925 | Template)) |
| 926 | return ParsedTemplateArgument(SS, Template, Name.StartLocation); |
| 927 | } |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 928 | } else if (Tok.is(tok::identifier)) { |
| 929 | // We may have a (non-dependent) template name. |
| 930 | TemplateTy Template; |
| 931 | UnqualifiedId Name; |
| 932 | Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 933 | ConsumeToken(); // the identifier |
| 934 | |
| 935 | if (isEndOfTemplateArgument(Tok)) { |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 936 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 937 | TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, |
| 938 | /*hasTemplateKeyword=*/false, |
| 939 | Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 940 | /*ObjectType=*/ ParsedType(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 941 | /*EnteringContext=*/false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 942 | Template, |
| 943 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 944 | if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) { |
| 945 | // We have an id-expression that refers to a class template or |
| 946 | // (C++0x) template alias. |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 947 | return ParsedTemplateArgument(SS, Template, Name.StartLocation); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Douglas Gregor | c998409 | 2009-11-12 00:03:40 +0000 | [diff] [blame] | 952 | // We don't have a template template argument. |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 953 | return ParsedTemplateArgument(); |
| 954 | } |
| 955 | |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 956 | /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). |
| 957 | /// |
| 958 | /// template-argument: [C++ 14.2] |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 959 | /// constant-expression |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 960 | /// type-id |
| 961 | /// id-expression |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 962 | ParsedTemplateArgument Parser::ParseTemplateArgument() { |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 963 | // C++ [temp.arg]p2: |
| 964 | // In a template-argument, an ambiguity between a type-id and an |
| 965 | // expression is resolved to a type-id, regardless of the form of |
| 966 | // the corresponding template-parameter. |
| 967 | // |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 968 | // Therefore, we initially try to parse a type-id. |
Douglas Gregor | 97f3457 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 969 | if (isCXXTypeId(TypeIdAsTemplateArgument)) { |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 970 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 971 | TypeResult TypeArg = ParseTypeName(); |
| 972 | if (TypeArg.isInvalid()) |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 973 | return ParsedTemplateArgument(); |
| 974 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 975 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
| 976 | TypeArg.get().getAsOpaquePtr(), |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 977 | Loc); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 978 | } |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 979 | |
| 980 | // Try to parse a template template argument. |
Douglas Gregor | c998409 | 2009-11-12 00:03:40 +0000 | [diff] [blame] | 981 | { |
| 982 | TentativeParsingAction TPA(*this); |
| 983 | |
| 984 | ParsedTemplateArgument TemplateTemplateArgument |
| 985 | = ParseTemplateTemplateArgument(); |
| 986 | if (!TemplateTemplateArgument.isInvalid()) { |
| 987 | TPA.Commit(); |
| 988 | return TemplateTemplateArgument; |
| 989 | } |
| 990 | |
| 991 | // Revert this tentative parse to parse a non-type template argument. |
| 992 | TPA.Revert(); |
| 993 | } |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 994 | |
| 995 | // Parse a non-type template argument. |
| 996 | SourceLocation Loc = Tok.getLocation(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 997 | ExprResult ExprArg = ParseConstantExpression(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 998 | if (ExprArg.isInvalid() || !ExprArg.get()) |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 999 | return ParsedTemplateArgument(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1000 | |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1001 | return ParsedTemplateArgument(ParsedTemplateArgument::NonType, |
| 1002 | ExprArg.release(), Loc); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1005 | /// \brief Determine whether the current tokens can only be parsed as a |
| 1006 | /// template argument list (starting with the '<') and never as a '<' |
| 1007 | /// expression. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 1008 | bool Parser::IsTemplateArgumentList(unsigned Skip) { |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1009 | struct AlwaysRevertAction : TentativeParsingAction { |
| 1010 | AlwaysRevertAction(Parser &P) : TentativeParsingAction(P) { } |
| 1011 | ~AlwaysRevertAction() { Revert(); } |
| 1012 | } Tentative(*this); |
| 1013 | |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 1014 | while (Skip) { |
| 1015 | ConsumeToken(); |
| 1016 | --Skip; |
| 1017 | } |
| 1018 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1019 | // '<' |
| 1020 | if (!Tok.is(tok::less)) |
| 1021 | return false; |
| 1022 | ConsumeToken(); |
| 1023 | |
| 1024 | // An empty template argument list. |
| 1025 | if (Tok.is(tok::greater)) |
| 1026 | return true; |
| 1027 | |
| 1028 | // See whether we have declaration specifiers, which indicate a type. |
| 1029 | while (isCXXDeclarationSpecifier() == TPResult::True()) |
| 1030 | ConsumeToken(); |
| 1031 | |
| 1032 | // If we have a '>' or a ',' then this is a template argument list. |
| 1033 | return Tok.is(tok::greater) || Tok.is(tok::comma); |
| 1034 | } |
| 1035 | |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1036 | /// ParseTemplateArgumentList - Parse a C++ template-argument-list |
| 1037 | /// (C++ [temp.names]). Returns true if there was an error. |
| 1038 | /// |
| 1039 | /// template-argument-list: [C++ 14.2] |
| 1040 | /// template-argument |
| 1041 | /// template-argument-list ',' template-argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | bool |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1043 | Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) { |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1044 | while (true) { |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1045 | ParsedTemplateArgument Arg = ParseTemplateArgument(); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1046 | if (Tok.is(tok::ellipsis)) { |
| 1047 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 1048 | Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc); |
| 1049 | } |
| 1050 | |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1051 | if (Arg.isInvalid()) { |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1052 | SkipUntil(tok::comma, tok::greater, true, true); |
| 1053 | return true; |
| 1054 | } |
Douglas Gregor | 67b556a | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1056 | // Save this template argument. |
| 1057 | TemplateArgs.push_back(Arg); |
| 1058 | |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1059 | // If the next token is a comma, consume it and keep reading |
| 1060 | // arguments. |
| 1061 | if (Tok.isNot(tok::comma)) break; |
| 1062 | |
| 1063 | // Consume the comma. |
| 1064 | ConsumeToken(); |
| 1065 | } |
| 1066 | |
Eli Friedman | affd5fd | 2009-12-27 22:31:18 +0000 | [diff] [blame] | 1067 | return false; |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | /// \brief Parse a C++ explicit template instantiation |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1071 | /// (C++ [temp.explicit]). |
| 1072 | /// |
| 1073 | /// explicit-instantiation: |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1074 | /// 'extern' [opt] 'template' declaration |
| 1075 | /// |
| 1076 | /// Note that the 'extern' is a GNU extension and C++0x feature. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1077 | Decl *Parser::ParseExplicitInstantiation(SourceLocation ExternLoc, |
| 1078 | SourceLocation TemplateLoc, |
| 1079 | SourceLocation &DeclEnd) { |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1080 | // This isn't really required here. |
| 1081 | ParsingDeclRAIIObject ParsingTemplateParams(*this); |
| 1082 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1084 | ParsedTemplateInfo(ExternLoc, |
| 1085 | TemplateLoc), |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1086 | ParsingTemplateParams, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1087 | DeclEnd, AS_none); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1088 | } |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1089 | |
| 1090 | SourceRange Parser::ParsedTemplateInfo::getSourceRange() const { |
| 1091 | if (TemplateParams) |
| 1092 | return getTemplateParamsRange(TemplateParams->data(), |
| 1093 | TemplateParams->size()); |
| 1094 | |
| 1095 | SourceRange R(TemplateLoc); |
| 1096 | if (ExternLoc.isValid()) |
| 1097 | R.setBegin(ExternLoc); |
| 1098 | return R; |
| 1099 | } |