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