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