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