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