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