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