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