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