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