Douglas Gregor | b3bec71 | 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 | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
| 17 | #include "clang/Parse/Scope.h" |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 20 | /// \brief Parse a template declaration, explicit instantiation, or |
| 21 | /// explicit specialization. |
| 22 | Parser::DeclPtrTy |
| 23 | Parser::ParseDeclarationStartingWithTemplate(unsigned Context, |
| 24 | SourceLocation &DeclEnd, |
| 25 | AccessSpecifier AS) { |
| 26 | if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) |
| 27 | return ParseExplicitInstantiation(ConsumeToken(), DeclEnd); |
| 28 | |
| 29 | return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AS); |
| 30 | } |
| 31 | |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 32 | /// \brief Parse a template declaration or an explicit specialization. |
| 33 | /// |
| 34 | /// Template declarations include one or more template parameter lists |
| 35 | /// and either the function or class template declaration. Explicit |
| 36 | /// specializations contain one or more 'template < >' prefixes |
| 37 | /// followed by a (possibly templated) declaration. Since the |
| 38 | /// syntactic form of both features is nearly identical, we parse all |
| 39 | /// of the template headers together and let semantic analysis sort |
| 40 | /// the declarations from the explicit specializations. |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 41 | /// |
| 42 | /// template-declaration: [C++ temp] |
| 43 | /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 44 | /// |
| 45 | /// explicit-specialization: [ C++ temp.expl.spec] |
| 46 | /// 'template' '<' '>' declaration |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 47 | Parser::DeclPtrTy |
Anders Carlsson | ed20fb9 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 48 | Parser::ParseTemplateDeclarationOrSpecialization(unsigned Context, |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 49 | SourceLocation &DeclEnd, |
Anders Carlsson | ed20fb9 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 50 | AccessSpecifier AS) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 51 | assert((Tok.is(tok::kw_export) || Tok.is(tok::kw_template)) && |
| 52 | "Token does not start a template declaration."); |
| 53 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 54 | // Enter template-parameter scope. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 55 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 56 | |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 57 | // Parse multiple levels of template headers within this template |
| 58 | // parameter scope, e.g., |
| 59 | // |
| 60 | // template<typename T> |
| 61 | // template<typename U> |
| 62 | // class A<T>::B { ... }; |
| 63 | // |
| 64 | // We parse multiple levels non-recursively so that we can build a |
| 65 | // single data structure containing all of the template parameter |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 66 | // lists to easily differentiate between the case above and: |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 67 | // |
| 68 | // template<typename T> |
| 69 | // class A { |
| 70 | // template<typename U> class B; |
| 71 | // }; |
| 72 | // |
| 73 | // In the first case, the action for declaring A<T>::B receives |
| 74 | // both template parameter lists. In the second case, the action for |
| 75 | // defining A<T>::B receives just the inner template parameter list |
| 76 | // (and retrieves the outer template parameter list from its |
| 77 | // context). |
Douglas Gregor | d022d71 | 2009-08-20 18:46:05 +0000 | [diff] [blame] | 78 | bool isSpecialization = true; |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 79 | TemplateParameterLists ParamLists; |
| 80 | do { |
| 81 | // Consume the 'export', if any. |
| 82 | SourceLocation ExportLoc; |
| 83 | if (Tok.is(tok::kw_export)) { |
| 84 | ExportLoc = ConsumeToken(); |
| 85 | } |
| 86 | |
| 87 | // Consume the 'template', which should be here. |
| 88 | SourceLocation TemplateLoc; |
| 89 | if (Tok.is(tok::kw_template)) { |
| 90 | TemplateLoc = ConsumeToken(); |
| 91 | } else { |
| 92 | Diag(Tok.getLocation(), diag::err_expected_template); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 93 | return DeclPtrTy(); |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Parse the '<' template-parameter-list '>' |
| 97 | SourceLocation LAngleLoc, RAngleLoc; |
| 98 | TemplateParameterList TemplateParams; |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 99 | if (ParseTemplateParameters(ParamLists.size(), TemplateParams, LAngleLoc, |
| 100 | RAngleLoc)) { |
| 101 | // Skip until the semi-colon or a }. |
| 102 | SkipUntil(tok::r_brace, true, true); |
| 103 | if (Tok.is(tok::semi)) |
| 104 | ConsumeToken(); |
| 105 | return DeclPtrTy(); |
| 106 | } |
| 107 | |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 108 | if (!TemplateParams.empty()) |
Douglas Gregor | d022d71 | 2009-08-20 18:46:05 +0000 | [diff] [blame] | 109 | isSpecialization = false; |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 111 | ParamLists.push_back( |
| 112 | Actions.ActOnTemplateParameterList(ParamLists.size(), ExportLoc, |
| 113 | TemplateLoc, LAngleLoc, |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 114 | TemplateParams.data(), |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 115 | TemplateParams.size(), RAngleLoc)); |
| 116 | } while (Tok.is(tok::kw_export) || Tok.is(tok::kw_template)); |
| 117 | |
| 118 | // Parse the actual template declaration. |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 119 | return ParseSingleDeclarationAfterTemplate(Context, |
| 120 | ParsedTemplateInfo(&ParamLists, |
Douglas Gregor | d022d71 | 2009-08-20 18:46:05 +0000 | [diff] [blame] | 121 | isSpecialization), |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 122 | DeclEnd, AS); |
| 123 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 125 | /// \brief Parse a single declaration that declares a template, |
| 126 | /// template specialization, or explicit instantiation of a template. |
| 127 | /// |
| 128 | /// \param TemplateParams if non-NULL, the template parameter lists |
| 129 | /// that preceded this declaration. In this case, the declaration is a |
| 130 | /// template declaration, out-of-line definition of a template, or an |
| 131 | /// explicit template specialization. When NULL, the declaration is an |
| 132 | /// explicit template instantiation. |
| 133 | /// |
| 134 | /// \param TemplateLoc when TemplateParams is NULL, the location of |
| 135 | /// the 'template' keyword that indicates that we have an explicit |
| 136 | /// template instantiation. |
| 137 | /// |
| 138 | /// \param DeclEnd will receive the source location of the last token |
| 139 | /// within this declaration. |
| 140 | /// |
| 141 | /// \param AS the access specifier associated with this |
| 142 | /// declaration. Will be AS_none for namespace-scope declarations. |
| 143 | /// |
| 144 | /// \returns the new declaration. |
| 145 | Parser::DeclPtrTy |
| 146 | Parser::ParseSingleDeclarationAfterTemplate( |
| 147 | unsigned Context, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 148 | const ParsedTemplateInfo &TemplateInfo, |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 149 | SourceLocation &DeclEnd, |
| 150 | AccessSpecifier AS) { |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 151 | assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
| 152 | "Template information required"); |
| 153 | |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame^] | 154 | if (Context == Declarator::MemberContext) { |
| 155 | // We are parsing a member template. |
| 156 | ParseCXXClassMemberDeclaration(AS, TemplateInfo); |
| 157 | return DeclPtrTy::make((void*)0); |
| 158 | } |
| 159 | |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 160 | // Parse the declaration specifiers. |
| 161 | DeclSpec DS; |
| 162 | // FIXME: Pass TemplateLoc through for explicit template instantiations |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 163 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS); |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 164 | |
| 165 | if (Tok.is(tok::semi)) { |
| 166 | DeclEnd = ConsumeToken(); |
| 167 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 168 | } |
| 169 | |
| 170 | // Parse the declarator. |
| 171 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 172 | ParseDeclarator(DeclaratorInfo); |
| 173 | // Error parsing the declarator? |
| 174 | if (!DeclaratorInfo.hasName()) { |
| 175 | // If so, skip until the semi-colon or a }. |
| 176 | SkipUntil(tok::r_brace, true, true); |
| 177 | if (Tok.is(tok::semi)) |
| 178 | ConsumeToken(); |
| 179 | return DeclPtrTy(); |
| 180 | } |
| 181 | |
| 182 | // If we have a declaration or declarator list, handle it. |
| 183 | if (isDeclarationAfterDeclarator()) { |
| 184 | // Parse this declaration. |
Douglas Gregor | 2ae1d77 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 185 | DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo, |
| 186 | TemplateInfo); |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 187 | |
| 188 | if (Tok.is(tok::comma)) { |
| 189 | Diag(Tok, diag::err_multiple_template_declarators) |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 190 | << (int)TemplateInfo.Kind; |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 191 | SkipUntil(tok::semi, true, false); |
| 192 | return ThisDecl; |
| 193 | } |
| 194 | |
| 195 | // Eat the semi colon after the declaration. |
John McCall | fcb32f4 | 2009-07-31 02:20:35 +0000 | [diff] [blame] | 196 | ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration); |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 197 | return ThisDecl; |
| 198 | } |
| 199 | |
| 200 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 201 | isStartOfFunctionDefinition()) { |
| 202 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 203 | Diag(Tok, diag::err_function_declared_typedef); |
| 204 | |
| 205 | if (Tok.is(tok::l_brace)) { |
| 206 | // This recovery skips the entire function body. It would be nice |
| 207 | // to simply call ParseFunctionDefinition() below, however Sema |
| 208 | // assumes the declarator represents a function, not a typedef. |
| 209 | ConsumeBrace(); |
| 210 | SkipUntil(tok::r_brace, true); |
| 211 | } else { |
| 212 | SkipUntil(tok::semi); |
| 213 | } |
| 214 | return DeclPtrTy(); |
| 215 | } |
Douglas Gregor | 19d1065 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 216 | return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo); |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (DeclaratorInfo.isFunctionDeclarator()) |
| 220 | Diag(Tok, diag::err_expected_fn_body); |
| 221 | else |
| 222 | Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); |
| 223 | SkipUntil(tok::semi); |
| 224 | return DeclPtrTy(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /// ParseTemplateParameters - Parses a template-parameter-list enclosed in |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 228 | /// angle brackets. Depth is the depth of this template-parameter-list, which |
| 229 | /// is the number of template headers directly enclosing this template header. |
| 230 | /// TemplateParams is the current list of template parameters we're building. |
| 231 | /// The template parameter we parse will be added to this list. LAngleLoc and |
| 232 | /// RAngleLoc will receive the positions of the '<' and '>', respectively, |
| 233 | /// that enclose this template parameter list. |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 234 | /// |
| 235 | /// \returns true if an error occurred, false otherwise. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 236 | bool Parser::ParseTemplateParameters(unsigned Depth, |
| 237 | TemplateParameterList &TemplateParams, |
| 238 | SourceLocation &LAngleLoc, |
| 239 | SourceLocation &RAngleLoc) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 240 | // Get the template parameter list. |
| 241 | if(!Tok.is(tok::less)) { |
| 242 | Diag(Tok.getLocation(), diag::err_expected_less_after) << "template"; |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 243 | return true; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 244 | } |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 245 | LAngleLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 246 | |
| 247 | // Try to parse the template parameter list. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 248 | if (Tok.is(tok::greater)) |
| 249 | RAngleLoc = ConsumeToken(); |
| 250 | else if(ParseTemplateParameterList(Depth, TemplateParams)) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 251 | if(!Tok.is(tok::greater)) { |
| 252 | Diag(Tok.getLocation(), diag::err_expected_greater); |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 253 | return true; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 254 | } |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 255 | RAngleLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 256 | } |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 257 | return false; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /// ParseTemplateParameterList - Parse a template parameter list. If |
| 261 | /// the parsing fails badly (i.e., closing bracket was left out), this |
| 262 | /// will try to put the token stream in a reasonable position (closing |
| 263 | /// a statement, etc.) and return false. |
| 264 | /// |
| 265 | /// template-parameter-list: [C++ temp] |
| 266 | /// template-parameter |
| 267 | /// template-parameter-list ',' template-parameter |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 268 | bool |
| 269 | Parser::ParseTemplateParameterList(unsigned Depth, |
| 270 | TemplateParameterList &TemplateParams) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 271 | while(1) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 272 | if (DeclPtrTy TmpParam |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 273 | = ParseTemplateParameter(Depth, TemplateParams.size())) { |
| 274 | TemplateParams.push_back(TmpParam); |
| 275 | } else { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 276 | // If we failed to parse a template parameter, skip until we find |
| 277 | // a comma or closing brace. |
| 278 | SkipUntil(tok::comma, tok::greater, true, true); |
| 279 | } |
| 280 | |
| 281 | // Did we find a comma or the end of the template parmeter list? |
| 282 | if(Tok.is(tok::comma)) { |
| 283 | ConsumeToken(); |
| 284 | } else if(Tok.is(tok::greater)) { |
| 285 | // Don't consume this... that's done by template parser. |
| 286 | break; |
| 287 | } else { |
| 288 | // Somebody probably forgot to close the template. Skip ahead and |
| 289 | // try to get out of the expression. This error is currently |
| 290 | // subsumed by whatever goes on in ParseTemplateParameter. |
| 291 | // TODO: This could match >>, and it would be nice to avoid those |
| 292 | // silly errors with template <vec<T>>. |
| 293 | // Diag(Tok.getLocation(), diag::err_expected_comma_greater); |
| 294 | SkipUntil(tok::greater, true, true); |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). |
| 302 | /// |
| 303 | /// template-parameter: [C++ temp.param] |
| 304 | /// type-parameter |
| 305 | /// parameter-declaration |
| 306 | /// |
| 307 | /// type-parameter: (see below) |
Anders Carlsson | 647d1ab | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 308 | /// 'class' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 309 | /// 'class' identifier[opt] '=' type-id |
Anders Carlsson | 647d1ab | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 310 | /// 'typename' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 311 | /// 'typename' identifier[opt] '=' type-id |
Anders Carlsson | 647d1ab | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 312 | /// 'template' ...[opt][C++0x] '<' template-parameter-list '>' 'class' identifier[opt] |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 313 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 314 | Parser::DeclPtrTy |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 315 | Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { |
Chris Lattner | 4e7a420 | 2009-01-04 23:51:17 +0000 | [diff] [blame] | 316 | if(Tok.is(tok::kw_class) || |
| 317 | (Tok.is(tok::kw_typename) && |
| 318 | // FIXME: Next token has not been annotated! |
Chris Lattner | 5d7eace | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 319 | NextToken().isNot(tok::annot_typename))) { |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 320 | return ParseTypeParameter(Depth, Position); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 321 | } |
Chris Lattner | 4e7a420 | 2009-01-04 23:51:17 +0000 | [diff] [blame] | 322 | |
| 323 | if(Tok.is(tok::kw_template)) |
| 324 | return ParseTemplateTemplateParameter(Depth, Position); |
| 325 | |
| 326 | // If it's none of the above, then it must be a parameter declaration. |
| 327 | // NOTE: This will pick up errors in the closure of the template parameter |
| 328 | // list (e.g., template < ; Check here to implement >> style closures. |
| 329 | return ParseNonTypeTemplateParameter(Depth, Position); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). |
| 333 | /// Other kinds of template parameters are parsed in |
| 334 | /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. |
| 335 | /// |
| 336 | /// type-parameter: [C++ temp.param] |
Anders Carlsson | 647d1ab | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 337 | /// 'class' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 338 | /// 'class' identifier[opt] '=' type-id |
Anders Carlsson | 647d1ab | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 339 | /// 'typename' ...[opt][C++0x] identifier[opt] |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 340 | /// 'typename' identifier[opt] '=' type-id |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 341 | Parser::DeclPtrTy Parser::ParseTypeParameter(unsigned Depth, unsigned Position){ |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 342 | assert((Tok.is(tok::kw_class) || Tok.is(tok::kw_typename)) && |
| 343 | "A type-parameter starts with 'class' or 'typename'"); |
| 344 | |
| 345 | // Consume the 'class' or 'typename' keyword. |
| 346 | bool TypenameKeyword = Tok.is(tok::kw_typename); |
| 347 | SourceLocation KeyLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 348 | |
Anders Carlsson | 9c85fa0 | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 349 | // Grab the ellipsis (if given). |
| 350 | bool Ellipsis = false; |
| 351 | SourceLocation EllipsisLoc; |
Anders Carlsson | 647d1ab | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 352 | if (Tok.is(tok::ellipsis)) { |
Anders Carlsson | 9c85fa0 | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 353 | Ellipsis = true; |
| 354 | EllipsisLoc = ConsumeToken(); |
Anders Carlsson | 647d1ab | 2009-06-12 23:09:56 +0000 | [diff] [blame] | 355 | |
| 356 | if (!getLang().CPlusPlus0x) |
| 357 | Diag(EllipsisLoc, diag::err_variadic_templates); |
Anders Carlsson | 9c85fa0 | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 360 | // Grab the template parameter name (if given) |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 361 | SourceLocation NameLoc; |
| 362 | IdentifierInfo* ParamName = 0; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 363 | if(Tok.is(tok::identifier)) { |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 364 | ParamName = Tok.getIdentifierInfo(); |
| 365 | NameLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 366 | } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || |
| 367 | Tok.is(tok::greater)) { |
| 368 | // Unnamed template parameter. Don't have to do anything here, just |
| 369 | // don't consume this token. |
| 370 | } else { |
| 371 | Diag(Tok.getLocation(), diag::err_expected_ident); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 372 | return DeclPtrTy(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 375 | DeclPtrTy TypeParam = Actions.ActOnTypeParameter(CurScope, TypenameKeyword, |
Anders Carlsson | 9c85fa0 | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 376 | Ellipsis, EllipsisLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 377 | KeyLoc, ParamName, NameLoc, |
| 378 | Depth, Position); |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 380 | // Grab a default type id (if given). |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 381 | if(Tok.is(tok::equal)) { |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 382 | SourceLocation EqualLoc = ConsumeToken(); |
Douglas Gregor | 9225a7e | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 383 | SourceLocation DefaultLoc = Tok.getLocation(); |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 384 | TypeResult DefaultType = ParseTypeName(); |
| 385 | if (!DefaultType.isInvalid()) |
Douglas Gregor | 9225a7e | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 386 | Actions.ActOnTypeParameterDefault(TypeParam, EqualLoc, DefaultLoc, |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 387 | DefaultType.get()); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 390 | return TypeParam; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /// ParseTemplateTemplateParameter - Handle the parsing of template |
| 394 | /// template parameters. |
| 395 | /// |
| 396 | /// type-parameter: [C++ temp.param] |
| 397 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] |
| 398 | /// 'template' '<' template-parameter-list '>' 'class' identifier[opt] = id-expression |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 399 | Parser::DeclPtrTy |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 400 | Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 401 | assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); |
| 402 | |
| 403 | // Handle the template <...> part. |
| 404 | SourceLocation TemplateLoc = ConsumeToken(); |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 405 | TemplateParameterList TemplateParams; |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 406 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 77f7ced | 2009-02-10 19:52:54 +0000 | [diff] [blame] | 407 | { |
| 408 | ParseScope TemplateParmScope(this, Scope::TemplateParamScope); |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 409 | if(ParseTemplateParameters(Depth + 1, TemplateParams, LAngleLoc, |
| 410 | RAngleLoc)) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 411 | return DeclPtrTy(); |
Douglas Gregor | 77f7ced | 2009-02-10 19:52:54 +0000 | [diff] [blame] | 412 | } |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // Generate a meaningful error if the user forgot to put class before the |
| 416 | // identifier, comma, or greater. |
| 417 | if(!Tok.is(tok::kw_class)) { |
| 418 | Diag(Tok.getLocation(), diag::err_expected_class_before) |
| 419 | << PP.getSpelling(Tok); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 420 | return DeclPtrTy(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 421 | } |
| 422 | SourceLocation ClassLoc = ConsumeToken(); |
| 423 | |
| 424 | // Get the identifier, if given. |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 425 | SourceLocation NameLoc; |
| 426 | IdentifierInfo* ParamName = 0; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 427 | if(Tok.is(tok::identifier)) { |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 428 | ParamName = Tok.getIdentifierInfo(); |
| 429 | NameLoc = ConsumeToken(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 430 | } else if(Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::greater)) { |
| 431 | // Unnamed template parameter. Don't have to do anything here, just |
| 432 | // don't consume this token. |
| 433 | } else { |
| 434 | Diag(Tok.getLocation(), diag::err_expected_ident); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 435 | return DeclPtrTy(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 438 | TemplateParamsTy *ParamList = |
| 439 | Actions.ActOnTemplateParameterList(Depth, SourceLocation(), |
| 440 | TemplateLoc, LAngleLoc, |
| 441 | &TemplateParams[0], |
| 442 | TemplateParams.size(), |
| 443 | RAngleLoc); |
| 444 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 445 | Parser::DeclPtrTy Param |
Douglas Gregor | 9225a7e | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 446 | = Actions.ActOnTemplateTemplateParameter(CurScope, TemplateLoc, |
| 447 | ParamList, ParamName, |
| 448 | NameLoc, Depth, Position); |
| 449 | |
| 450 | // Get the a default value, if given. |
| 451 | if (Tok.is(tok::equal)) { |
| 452 | SourceLocation EqualLoc = ConsumeToken(); |
| 453 | OwningExprResult DefaultExpr = ParseCXXIdExpression(); |
| 454 | if (DefaultExpr.isInvalid()) |
| 455 | return Param; |
| 456 | else if (Param) |
| 457 | Actions.ActOnTemplateTemplateParameterDefault(Param, EqualLoc, |
| 458 | move(DefaultExpr)); |
| 459 | } |
| 460 | |
| 461 | return Param; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /// ParseNonTypeTemplateParameter - Handle the parsing of non-type |
| 465 | /// template parameters (e.g., in "template<int Size> class array;"). |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 466 | /// |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 467 | /// template-parameter: |
| 468 | /// ... |
| 469 | /// parameter-declaration |
| 470 | /// |
| 471 | /// NOTE: It would be ideal to simply call out to ParseParameterDeclaration(), |
| 472 | /// but that didn't work out to well. Instead, this tries to recrate the basic |
| 473 | /// parsing of parameter declarations, but tries to constrain it for template |
| 474 | /// parameters. |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 475 | /// FIXME: We need to make a ParseParameterDeclaration that works for |
| 476 | /// non-type template parameters and normal function parameters. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 477 | Parser::DeclPtrTy |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 478 | Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 479 | SourceLocation StartLoc = Tok.getLocation(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 480 | |
| 481 | // Parse the declaration-specifiers (i.e., the type). |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 482 | // FIXME: The type should probably be restricted in some way... Not all |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 483 | // declarators (parts of declarators?) are accepted for parameters. |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 484 | DeclSpec DS; |
| 485 | ParseDeclarationSpecifiers(DS); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 486 | |
| 487 | // Parse this as a typename. |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 488 | Declarator ParamDecl(DS, Declarator::TemplateParamContext); |
| 489 | ParseDeclarator(ParamDecl); |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 490 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified && !DS.getTypeRep()) { |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 491 | // This probably shouldn't happen - and it's more of a Sema thing, but |
| 492 | // basically we didn't parse the type name because we couldn't associate |
| 493 | // it with an AST node. we should just skip to the comma or greater. |
| 494 | // TODO: This is currently a placeholder for some kind of Sema Error. |
| 495 | Diag(Tok.getLocation(), diag::err_parse_error); |
| 496 | SkipUntil(tok::comma, tok::greater, true, true); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 497 | return DeclPtrTy(); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 500 | // Create the parameter. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 501 | DeclPtrTy Param = Actions.ActOnNonTypeTemplateParameter(CurScope, ParamDecl, |
| 502 | Depth, Position); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 9225a7e | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 504 | // If there is a default value, parse it. |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 505 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 9225a7e | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 506 | SourceLocation EqualLoc = ConsumeToken(); |
| 507 | |
| 508 | // C++ [temp.param]p15: |
| 509 | // When parsing a default template-argument for a non-type |
| 510 | // template-parameter, the first non-nested > is taken as the |
| 511 | // end of the template-parameter-list rather than a greater-than |
| 512 | // operator. |
| 513 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
| 514 | |
| 515 | OwningExprResult DefaultArg = ParseAssignmentExpression(); |
| 516 | if (DefaultArg.isInvalid()) |
| 517 | SkipUntil(tok::comma, tok::greater, true, true); |
| 518 | else if (Param) |
| 519 | Actions.ActOnNonTypeTemplateParameterDefault(Param, EqualLoc, |
| 520 | move(DefaultArg)); |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Douglas Gregor | 8e7f957 | 2008-12-02 00:41:28 +0000 | [diff] [blame] | 523 | return Param; |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 524 | } |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 526 | /// \brief Parses a template-id that after the template name has |
| 527 | /// already been parsed. |
| 528 | /// |
| 529 | /// This routine takes care of parsing the enclosed template argument |
| 530 | /// list ('<' template-parameter-list [opt] '>') and placing the |
| 531 | /// results into a form that can be transferred to semantic analysis. |
| 532 | /// |
| 533 | /// \param Template the template declaration produced by isTemplateName |
| 534 | /// |
| 535 | /// \param TemplateNameLoc the source location of the template name |
| 536 | /// |
| 537 | /// \param SS if non-NULL, the nested-name-specifier preceding the |
| 538 | /// template name. |
| 539 | /// |
| 540 | /// \param ConsumeLastToken if true, then we will consume the last |
| 541 | /// token that forms the template-id. Otherwise, we will leave the |
| 542 | /// last token in the stream (e.g., so that it can be replaced with an |
| 543 | /// annotation token). |
| 544 | bool |
Douglas Gregor | dd13e84 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 545 | Parser::ParseTemplateIdAfterTemplateName(TemplateTy Template, |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 546 | SourceLocation TemplateNameLoc, |
| 547 | const CXXScopeSpec *SS, |
| 548 | bool ConsumeLastToken, |
| 549 | SourceLocation &LAngleLoc, |
| 550 | TemplateArgList &TemplateArgs, |
| 551 | TemplateArgIsTypeList &TemplateArgIsType, |
| 552 | TemplateArgLocationList &TemplateArgLocations, |
| 553 | SourceLocation &RAngleLoc) { |
| 554 | assert(Tok.is(tok::less) && "Must have already parsed the template-name"); |
| 555 | |
| 556 | // Consume the '<'. |
| 557 | LAngleLoc = ConsumeToken(); |
| 558 | |
| 559 | // Parse the optional template-argument-list. |
| 560 | bool Invalid = false; |
| 561 | { |
| 562 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, false); |
| 563 | if (Tok.isNot(tok::greater)) |
| 564 | Invalid = ParseTemplateArgumentList(TemplateArgs, TemplateArgIsType, |
| 565 | TemplateArgLocations); |
| 566 | |
| 567 | if (Invalid) { |
| 568 | // Try to find the closing '>'. |
| 569 | SkipUntil(tok::greater, true, !ConsumeLastToken); |
| 570 | |
| 571 | return true; |
| 572 | } |
| 573 | } |
| 574 | |
Douglas Gregor | f2d8739 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 575 | if (Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater)) |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 576 | return true; |
| 577 | |
Douglas Gregor | f2d8739 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 578 | // Determine the location of the '>' or '>>'. Only consume this |
| 579 | // token if the caller asked us to. |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 580 | RAngleLoc = Tok.getLocation(); |
| 581 | |
Douglas Gregor | f2d8739 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 582 | if (Tok.is(tok::greatergreater)) { |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 583 | if (!getLang().CPlusPlus0x) { |
| 584 | const char *ReplaceStr = "> >"; |
| 585 | if (NextToken().is(tok::greater) || NextToken().is(tok::greatergreater)) |
| 586 | ReplaceStr = "> > "; |
| 587 | |
| 588 | Diag(Tok.getLocation(), diag::err_two_right_angle_brackets_need_space) |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 589 | << CodeModificationHint::CreateReplacement( |
| 590 | SourceRange(Tok.getLocation()), ReplaceStr); |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 591 | } |
Douglas Gregor | f2d8739 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 592 | |
| 593 | Tok.setKind(tok::greater); |
| 594 | if (!ConsumeLastToken) { |
| 595 | // Since we're not supposed to consume the '>>' token, we need |
| 596 | // to insert a second '>' token after the first. |
| 597 | PP.EnterToken(Tok); |
| 598 | } |
| 599 | } else if (ConsumeLastToken) |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 600 | ConsumeToken(); |
| 601 | |
| 602 | return false; |
| 603 | } |
| 604 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 605 | /// \brief Replace the tokens that form a simple-template-id with an |
| 606 | /// annotation token containing the complete template-id. |
| 607 | /// |
| 608 | /// The first token in the stream must be the name of a template that |
| 609 | /// is followed by a '<'. This routine will parse the complete |
| 610 | /// simple-template-id and replace the tokens with a single annotation |
| 611 | /// token with one of two different kinds: if the template-id names a |
| 612 | /// type (and \p AllowTypeAnnotation is true), the annotation token is |
| 613 | /// a type annotation that includes the optional nested-name-specifier |
| 614 | /// (\p SS). Otherwise, the annotation token is a template-id |
| 615 | /// annotation that does not include the optional |
| 616 | /// nested-name-specifier. |
| 617 | /// |
| 618 | /// \param Template the declaration of the template named by the first |
| 619 | /// token (an identifier), as returned from \c Action::isTemplateName(). |
| 620 | /// |
| 621 | /// \param TemplateNameKind the kind of template that \p Template |
| 622 | /// refers to, as returned from \c Action::isTemplateName(). |
| 623 | /// |
| 624 | /// \param SS if non-NULL, the nested-name-specifier that precedes |
| 625 | /// this template name. |
| 626 | /// |
| 627 | /// \param TemplateKWLoc if valid, specifies that this template-id |
| 628 | /// annotation was preceded by the 'template' keyword and gives the |
| 629 | /// location of that keyword. If invalid (the default), then this |
| 630 | /// template-id was not preceded by a 'template' keyword. |
| 631 | /// |
| 632 | /// \param AllowTypeAnnotation if true (the default), then a |
| 633 | /// simple-template-id that refers to a class template, template |
| 634 | /// template parameter, or other template that produces a type will be |
| 635 | /// replaced with a type annotation token. Otherwise, the |
| 636 | /// simple-template-id is always replaced with a template-id |
| 637 | /// annotation token. |
Chris Lattner | 8eabc06 | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 638 | /// |
| 639 | /// If an unrecoverable parse error occurs and no annotation token can be |
| 640 | /// formed, this function returns true. |
| 641 | /// |
| 642 | bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 643 | const CXXScopeSpec *SS, |
| 644 | SourceLocation TemplateKWLoc, |
| 645 | bool AllowTypeAnnotation) { |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 646 | assert(getLang().CPlusPlus && "Can only annotate template-ids in C++"); |
| 647 | assert(Template && Tok.is(tok::identifier) && NextToken().is(tok::less) && |
| 648 | "Parser isn't at the beginning of a template-id"); |
| 649 | |
| 650 | // Consume the template-name. |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 651 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 652 | SourceLocation TemplateNameLoc = ConsumeToken(); |
| 653 | |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 654 | // Parse the enclosed template argument list. |
| 655 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 656 | TemplateArgList TemplateArgs; |
| 657 | TemplateArgIsTypeList TemplateArgIsType; |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 658 | TemplateArgLocationList TemplateArgLocations; |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 659 | bool Invalid = ParseTemplateIdAfterTemplateName(Template, TemplateNameLoc, |
| 660 | SS, false, LAngleLoc, |
| 661 | TemplateArgs, |
| 662 | TemplateArgIsType, |
| 663 | TemplateArgLocations, |
| 664 | RAngleLoc); |
Chris Lattner | 8eabc06 | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 665 | |
| 666 | if (Invalid) { |
| 667 | // If we failed to parse the template ID but skipped ahead to a >, we're not |
| 668 | // going to be able to form a token annotation. Eat the '>' if present. |
| 669 | if (Tok.is(tok::greater)) |
| 670 | ConsumeToken(); |
| 671 | return true; |
| 672 | } |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 673 | |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 674 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), |
| 675 | TemplateArgIsType.data(), |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 676 | TemplateArgs.size()); |
Douglas Gregor | af0d009 | 2009-02-09 21:04:56 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 678 | // Build the annotation token. |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 679 | if (TNK == TNK_Type_template && AllowTypeAnnotation) { |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 680 | Action::TypeResult Type |
Douglas Gregor | dd13e84 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 681 | = Actions.ActOnTemplateIdType(Template, TemplateNameLoc, |
| 682 | LAngleLoc, TemplateArgsPtr, |
| 683 | &TemplateArgLocations[0], |
| 684 | RAngleLoc); |
Chris Lattner | 8eabc06 | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 685 | if (Type.isInvalid()) { |
| 686 | // If we failed to parse the template ID but skipped ahead to a >, we're not |
| 687 | // going to be able to form a token annotation. Eat the '>' if present. |
| 688 | if (Tok.is(tok::greater)) |
| 689 | ConsumeToken(); |
| 690 | return true; |
| 691 | } |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 692 | |
| 693 | Tok.setKind(tok::annot_typename); |
| 694 | Tok.setAnnotationValue(Type.get()); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 695 | if (SS && SS->isNotEmpty()) |
| 696 | Tok.setLocation(SS->getBeginLoc()); |
| 697 | else if (TemplateKWLoc.isValid()) |
| 698 | Tok.setLocation(TemplateKWLoc); |
| 699 | else |
| 700 | Tok.setLocation(TemplateNameLoc); |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 701 | } else { |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 702 | // Build a template-id annotation token that can be processed |
| 703 | // later. |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 704 | Tok.setKind(tok::annot_template_id); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 705 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 706 | = TemplateIdAnnotation::Allocate(TemplateArgs.size()); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 707 | TemplateId->TemplateNameLoc = TemplateNameLoc; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 708 | TemplateId->Name = Name; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 709 | TemplateId->Template = Template.getAs<void*>(); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 710 | TemplateId->Kind = TNK; |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 711 | TemplateId->LAngleLoc = LAngleLoc; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 712 | TemplateId->RAngleLoc = RAngleLoc; |
| 713 | void **Args = TemplateId->getTemplateArgs(); |
| 714 | bool *ArgIsType = TemplateId->getTemplateArgIsType(); |
| 715 | SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations(); |
| 716 | for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); Arg != ArgEnd; ++Arg) { |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 717 | Args[Arg] = TemplateArgs[Arg]; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 718 | ArgIsType[Arg] = TemplateArgIsType[Arg]; |
| 719 | ArgLocs[Arg] = TemplateArgLocations[Arg]; |
| 720 | } |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 721 | Tok.setAnnotationValue(TemplateId); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 722 | if (TemplateKWLoc.isValid()) |
| 723 | Tok.setLocation(TemplateKWLoc); |
| 724 | else |
| 725 | Tok.setLocation(TemplateNameLoc); |
| 726 | |
| 727 | TemplateArgsPtr.release(); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // Common fields for the annotation token |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 731 | Tok.setAnnotationEndLoc(RAngleLoc); |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 733 | // In case the tokens were cached, have Preprocessor replace them with the |
| 734 | // annotation token. |
| 735 | PP.AnnotateCachedTokens(Tok); |
Chris Lattner | 8eabc06 | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 736 | return false; |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 739 | /// \brief Replaces a template-id annotation token with a type |
| 740 | /// annotation token. |
| 741 | /// |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 742 | /// If there was a failure when forming the type from the template-id, |
| 743 | /// a type annotation token will still be created, but will have a |
| 744 | /// NULL type pointer to signify an error. |
| 745 | void Parser::AnnotateTemplateIdTokenAsType(const CXXScopeSpec *SS) { |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 746 | assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens"); |
| 747 | |
| 748 | TemplateIdAnnotation *TemplateId |
| 749 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 750 | assert((TemplateId->Kind == TNK_Type_template || |
| 751 | TemplateId->Kind == TNK_Dependent_template_name) && |
| 752 | "Only works for type and dependent templates"); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 753 | |
| 754 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
| 755 | TemplateId->getTemplateArgs(), |
| 756 | TemplateId->getTemplateArgIsType(), |
| 757 | TemplateId->NumArgs); |
| 758 | |
| 759 | Action::TypeResult Type |
Douglas Gregor | dd13e84 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 760 | = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template), |
| 761 | TemplateId->TemplateNameLoc, |
| 762 | TemplateId->LAngleLoc, |
| 763 | TemplateArgsPtr, |
| 764 | TemplateId->getTemplateArgLocations(), |
| 765 | TemplateId->RAngleLoc); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 766 | // Create the new "type" annotation token. |
| 767 | Tok.setKind(tok::annot_typename); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 768 | Tok.setAnnotationValue(Type.isInvalid()? 0 : Type.get()); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 769 | if (SS && SS->isNotEmpty()) // it was a C++ qualified type name. |
| 770 | Tok.setLocation(SS->getBeginLoc()); |
| 771 | |
| 772 | // We might be backtracking, in which case we need to replace the |
| 773 | // template-id annotation token with the type annotation within the |
| 774 | // set of cached tokens. That way, we won't try to form the same |
| 775 | // class template specialization again. |
| 776 | PP.ReplaceLastTokenWithAnnotation(Tok); |
| 777 | TemplateId->Destroy(); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 780 | /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). |
| 781 | /// |
| 782 | /// template-argument: [C++ 14.2] |
Douglas Gregor | a8b2fbf | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 783 | /// constant-expression |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 784 | /// type-id |
| 785 | /// id-expression |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 786 | void *Parser::ParseTemplateArgument(bool &ArgIsType) { |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 787 | // C++ [temp.arg]p2: |
| 788 | // In a template-argument, an ambiguity between a type-id and an |
| 789 | // expression is resolved to a type-id, regardless of the form of |
| 790 | // the corresponding template-parameter. |
| 791 | // |
| 792 | // Therefore, we initially try to parse a type-id. |
Douglas Gregor | 341ac79 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 793 | if (isCXXTypeId(TypeIdAsTemplateArgument)) { |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 794 | ArgIsType = true; |
Douglas Gregor | 6c0f406 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 795 | TypeResult TypeArg = ParseTypeName(); |
| 796 | if (TypeArg.isInvalid()) |
| 797 | return 0; |
| 798 | return TypeArg.get(); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Douglas Gregor | a8b2fbf | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 801 | OwningExprResult ExprArg = ParseConstantExpression(); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 802 | if (ExprArg.isInvalid() || !ExprArg.get()) |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 803 | return 0; |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 804 | |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 805 | ArgIsType = false; |
| 806 | return ExprArg.release(); |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /// ParseTemplateArgumentList - Parse a C++ template-argument-list |
| 810 | /// (C++ [temp.names]). Returns true if there was an error. |
| 811 | /// |
| 812 | /// template-argument-list: [C++ 14.2] |
| 813 | /// template-argument |
| 814 | /// template-argument-list ',' template-argument |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 815 | bool |
| 816 | Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs, |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 817 | TemplateArgIsTypeList &TemplateArgIsType, |
| 818 | TemplateArgLocationList &TemplateArgLocations) { |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 819 | while (true) { |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 820 | bool IsType = false; |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 821 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 822 | void *Arg = ParseTemplateArgument(IsType); |
| 823 | if (Arg) { |
| 824 | TemplateArgs.push_back(Arg); |
| 825 | TemplateArgIsType.push_back(IsType); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 826 | TemplateArgLocations.push_back(Loc); |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 827 | } else { |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 828 | SkipUntil(tok::comma, tok::greater, true, true); |
| 829 | return true; |
| 830 | } |
Douglas Gregor | 6f37b58 | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 832 | // If the next token is a comma, consume it and keep reading |
| 833 | // arguments. |
| 834 | if (Tok.isNot(tok::comma)) break; |
| 835 | |
| 836 | // Consume the comma. |
| 837 | ConsumeToken(); |
| 838 | } |
| 839 | |
Douglas Gregor | f2d8739 | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 840 | return Tok.isNot(tok::greater) && Tok.isNot(tok::greatergreater); |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 843 | /// \brief Parse a C++ explicit template instantiation |
| 844 | /// (C++ [temp.explicit]). |
| 845 | /// |
| 846 | /// explicit-instantiation: |
| 847 | /// 'template' declaration |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 848 | Parser::DeclPtrTy |
| 849 | Parser::ParseExplicitInstantiation(SourceLocation TemplateLoc, |
| 850 | SourceLocation &DeclEnd) { |
| 851 | return ParseSingleDeclarationAfterTemplate(Declarator::FileContext, |
| 852 | ParsedTemplateInfo(TemplateLoc), |
| 853 | DeclEnd, AS_none); |
Douglas Gregor | e3298aa | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 854 | } |