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