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