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