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