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