Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1 | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anders Carlsson | 0c6139d | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 14 | #include "clang/Basic/OperatorKinds.h" |
Douglas Gregor | 1b7f898 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
| 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 20 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 21 | #include "RAIIObjectsForParser.h" |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 25 | /// may either be a top level namespace or a block-level namespace alias. If |
| 26 | /// there was an inline keyword, it has already been parsed. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 27 | /// |
| 28 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 29 | /// named-namespace-definition |
| 30 | /// unnamed-namespace-definition |
| 31 | /// |
| 32 | /// unnamed-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 33 | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// named-namespace-definition: |
| 36 | /// original-namespace-definition |
| 37 | /// extension-namespace-definition |
| 38 | /// |
| 39 | /// original-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 40 | /// 'inline'[opt] 'namespace' identifier attributes[opt] |
| 41 | /// '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 42 | /// |
| 43 | /// extension-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 44 | /// 'inline'[opt] 'namespace' original-namespace-name |
| 45 | /// '{' namespace-body '}' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | /// |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 47 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 48 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 49 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 50 | Decl *Parser::ParseNamespace(unsigned Context, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 51 | SourceLocation &DeclEnd, |
| 52 | SourceLocation InlineLoc) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 53 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 54 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 56 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 57 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 58 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 59 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 61 | SourceLocation IdentLoc; |
| 62 | IdentifierInfo *Ident = 0; |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 63 | |
| 64 | Token attrTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 66 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 67 | Ident = Tok.getIdentifierInfo(); |
| 68 | IdentLoc = ConsumeToken(); // eat the identifier. |
| 69 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 71 | // Read label attributes, if present. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 72 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 73 | if (Tok.is(tok::kw___attribute)) { |
| 74 | attrTok = Tok; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 75 | ParseGNUAttributes(attrs); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 78 | if (Tok.is(tok::equal)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 79 | if (!attrs.empty()) |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 80 | Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 81 | if (InlineLoc.isValid()) |
| 82 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
| 83 | << FixItHint::CreateRemoval(InlineLoc); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 85 | return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 86 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 88 | if (Tok.isNot(tok::l_brace)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 90 | diag::err_expected_ident_lbrace); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 91 | return 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 92 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 94 | SourceLocation LBrace = ConsumeBrace(); |
| 95 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 96 | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
| 97 | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
| 98 | getCurScope()->getFnParent()) { |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 99 | Diag(LBrace, diag::err_namespace_nonnamespace_scope); |
| 100 | SkipUntil(tok::r_brace, false); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 101 | return 0; |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 104 | // If we're still good, complain about inline namespaces in non-C++0x now. |
| 105 | if (!getLang().CPlusPlus0x && InlineLoc.isValid()) |
| 106 | Diag(InlineLoc, diag::ext_inline_namespace); |
| 107 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 108 | // Enter a scope for the namespace. |
| 109 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 110 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 111 | Decl *NamespcDecl = |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 112 | Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc, |
| 113 | IdentLoc, Ident, LBrace, attrs.getList()); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 114 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 115 | PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc, |
| 116 | "parsing namespace"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 118 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 119 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 120 | MaybeParseCXX0XAttributes(attrs); |
| 121 | MaybeParseMicrosoftAttributes(attrs); |
| 122 | ParseExternalDeclaration(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 125 | // Leave the namespace scope. |
| 126 | NamespaceScope.Exit(); |
| 127 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 128 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace); |
| 129 | Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 131 | DeclEnd = RBraceLoc; |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 132 | return NamespcDecl; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 134 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 135 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 136 | /// alias definition. |
| 137 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 138 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 139 | SourceLocation AliasLoc, |
| 140 | IdentifierInfo *Alias, |
| 141 | SourceLocation &DeclEnd) { |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 142 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 144 | ConsumeToken(); // eat the '='. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 146 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 147 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 148 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 149 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 150 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 151 | CXXScopeSpec SS; |
| 152 | // Parse (optional) nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 153 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 154 | |
| 155 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
| 156 | Diag(Tok, diag::err_expected_namespace_name); |
| 157 | // Skip to end of the definition and eat the ';'. |
| 158 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 159 | return 0; |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Parse identifier. |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 163 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 164 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 166 | // Eat the ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 167 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 168 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, |
| 169 | "", tok::semi); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 171 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 172 | SS, IdentLoc, Ident); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 175 | /// ParseLinkage - We know that the current token is a string_literal |
| 176 | /// and just before that, that extern was seen. |
| 177 | /// |
| 178 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 179 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 180 | /// 'extern' string-literal declaration |
| 181 | /// |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 182 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) { |
Douglas Gregor | c19923d | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 183 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 184 | llvm::SmallString<8> LangBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 185 | bool Invalid = false; |
| 186 | llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid); |
| 187 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 188 | return 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 189 | |
| 190 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 192 | ParseScope LinkageScope(this, Scope::DeclScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 193 | Decl *LinkageSpec |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 194 | = Actions.ActOnStartLinkageSpecification(getCurScope(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 195 | DS.getSourceRange().getBegin(), |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 196 | Loc, Lang, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 197 | Tok.is(tok::l_brace) ? Tok.getLocation() |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 198 | : SourceLocation()); |
| 199 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 200 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 201 | MaybeParseCXX0XAttributes(attrs); |
| 202 | MaybeParseMicrosoftAttributes(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 204 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 205 | DS.setExternInLinkageSpec(true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 206 | ParseExternalDeclaration(attrs, &DS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 207 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 208 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 63a0113 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 211 | DS.abort(); |
| 212 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 213 | ProhibitAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 215 | SourceLocation LBrace = ConsumeBrace(); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 216 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 217 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 218 | MaybeParseCXX0XAttributes(attrs); |
| 219 | MaybeParseMicrosoftAttributes(attrs); |
| 220 | ParseExternalDeclaration(attrs); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 223 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 224 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
| 225 | RBrace); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 226 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 227 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 228 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 229 | /// using-directive. Assumes that current token is 'using'. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 230 | Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 231 | const ParsedTemplateInfo &TemplateInfo, |
| 232 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 233 | ParsedAttributesWithRange &attrs) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 234 | assert(Tok.is(tok::kw_using) && "Not using token"); |
| 235 | |
| 236 | // Eat 'using'. |
| 237 | SourceLocation UsingLoc = ConsumeToken(); |
| 238 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 239 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 240 | Actions.CodeCompleteUsing(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 241 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 242 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 243 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 244 | // 'using namespace' means this is a using-directive. |
| 245 | if (Tok.is(tok::kw_namespace)) { |
| 246 | // Template parameters are always an error here. |
| 247 | if (TemplateInfo.Kind) { |
| 248 | SourceRange R = TemplateInfo.getSourceRange(); |
| 249 | Diag(UsingLoc, diag::err_templated_using_directive) |
| 250 | << R << FixItHint::CreateRemoval(R); |
| 251 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 252 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 253 | return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 256 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 257 | |
| 258 | // Using declarations can't have attributes. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 259 | ProhibitAttributes(attrs); |
Chris Lattner | 2f27477 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 260 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 261 | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 265 | /// that current token is 'namespace' and 'using' was already parsed. |
| 266 | /// |
| 267 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 268 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 269 | /// namespace-name ; |
| 270 | /// [GNU] using-directive: |
| 271 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 272 | /// namespace-name attributes[opt] ; |
| 273 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 274 | Decl *Parser::ParseUsingDirective(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 275 | SourceLocation UsingLoc, |
| 276 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 277 | ParsedAttributes &attrs) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 278 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 279 | |
| 280 | // Eat 'namespace'. |
| 281 | SourceLocation NamespcLoc = ConsumeToken(); |
| 282 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 283 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 284 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 285 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 286 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 288 | CXXScopeSpec SS; |
| 289 | // Parse (optional) nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 290 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 291 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 292 | IdentifierInfo *NamespcName = 0; |
| 293 | SourceLocation IdentLoc = SourceLocation(); |
| 294 | |
| 295 | // Parse namespace-name. |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 296 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 297 | Diag(Tok, diag::err_expected_namespace_name); |
| 298 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 299 | SkipUntil(tok::semi); |
| 300 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 301 | return 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 304 | // Parse identifier. |
| 305 | NamespcName = Tok.getIdentifierInfo(); |
| 306 | IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 308 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 309 | bool GNUAttr = false; |
| 310 | if (Tok.is(tok::kw___attribute)) { |
| 311 | GNUAttr = true; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 312 | ParseGNUAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 315 | // Eat ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 316 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 317 | ExpectAndConsume(tok::semi, |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 318 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
| 319 | : diag::err_expected_semi_after_namespace_name, |
| 320 | "", tok::semi); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 321 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 322 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 323 | IdentLoc, NamespcName, attrs.getList()); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 326 | /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration. |
| 327 | /// Assumes that 'using' was already seen. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 328 | /// |
| 329 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 330 | /// 'using' 'typename'[opt] ::[opt] nested-name-specifier |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 331 | /// unqualified-id |
| 332 | /// 'using' :: unqualified-id |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 333 | /// |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 334 | /// alias-declaration: C++0x [decl.typedef]p2 |
| 335 | /// 'using' identifier = type-id ; |
| 336 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 337 | Decl *Parser::ParseUsingDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 338 | const ParsedTemplateInfo &TemplateInfo, |
| 339 | SourceLocation UsingLoc, |
| 340 | SourceLocation &DeclEnd, |
| 341 | AccessSpecifier AS) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 342 | CXXScopeSpec SS; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 343 | SourceLocation TypenameLoc; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 344 | bool IsTypeName; |
| 345 | |
| 346 | // Ignore optional 'typename'. |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 347 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 348 | if (Tok.is(tok::kw_typename)) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 349 | TypenameLoc = Tok.getLocation(); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 350 | ConsumeToken(); |
| 351 | IsTypeName = true; |
| 352 | } |
| 353 | else |
| 354 | IsTypeName = false; |
| 355 | |
| 356 | // Parse nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 357 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 359 | // Check nested-name specifier. |
| 360 | if (SS.isInvalid()) { |
| 361 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 362 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 363 | } |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 364 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 365 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 366 | // destructor names and allow the action module to diagnose any semantic |
| 367 | // errors. |
| 368 | UnqualifiedId Name; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 369 | if (ParseUnqualifiedId(SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 370 | /*EnteringContext=*/false, |
| 371 | /*AllowDestructorName=*/true, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 372 | /*AllowConstructorName=*/true, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 373 | ParsedType(), |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 374 | Name)) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 375 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 376 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 377 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 378 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 379 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 380 | |
| 381 | // Maybe this is an alias-declaration. |
| 382 | bool IsAliasDecl = Tok.is(tok::equal); |
| 383 | TypeResult TypeAlias; |
| 384 | if (IsAliasDecl) { |
| 385 | // TODO: Do we want to support attributes somewhere in an alias declaration? |
| 386 | // Can't follow GCC since it doesn't support them yet! |
| 387 | ConsumeToken(); |
| 388 | |
| 389 | if (!getLang().CPlusPlus0x) |
| 390 | Diag(Tok.getLocation(), diag::ext_alias_declaration); |
| 391 | |
| 392 | // Name must be an identifier. |
| 393 | if (Name.getKind() != UnqualifiedId::IK_Identifier) { |
| 394 | Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier); |
| 395 | // No removal fixit: can't recover from this. |
| 396 | SkipUntil(tok::semi); |
| 397 | return 0; |
| 398 | } else if (IsTypeName) |
| 399 | Diag(TypenameLoc, diag::err_alias_declaration_not_identifier) |
| 400 | << FixItHint::CreateRemoval(SourceRange(TypenameLoc, |
| 401 | SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc)); |
| 402 | else if (SS.isNotEmpty()) |
| 403 | Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier) |
| 404 | << FixItHint::CreateRemoval(SS.getRange()); |
| 405 | |
| 406 | TypeAlias = ParseTypeName(0, Declarator::AliasDeclContext); |
| 407 | } else |
| 408 | // Parse (optional) attributes (most likely GNU strong-using extension). |
| 409 | MaybeParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 411 | // Eat ';'. |
| 412 | DeclEnd = Tok.getLocation(); |
| 413 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 414 | !attrs.empty() ? "attributes list" : |
| 415 | IsAliasDecl ? "alias declaration" : "using declaration", |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 416 | tok::semi); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 417 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 418 | // Diagnose an attempt to declare a templated using-declaration. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 419 | // TODO: in C++0x, alias-declarations can be templates: |
| 420 | // template <...> using id = type; |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 421 | if (TemplateInfo.Kind) { |
| 422 | SourceRange R = TemplateInfo.getSourceRange(); |
| 423 | Diag(UsingLoc, diag::err_templated_using_declaration) |
| 424 | << R << FixItHint::CreateRemoval(R); |
| 425 | |
| 426 | // Unfortunately, we have to bail out instead of recovering by |
| 427 | // ignoring the parameters, just in case the nested name specifier |
| 428 | // depends on the parameters. |
| 429 | return 0; |
| 430 | } |
| 431 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 432 | if (IsAliasDecl) |
| 433 | return Actions.ActOnAliasDeclaration(getCurScope(), AS, UsingLoc, Name, |
| 434 | TypeAlias); |
| 435 | |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 436 | return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 437 | Name, attrs.getList(), |
| 438 | IsTypeName, TypenameLoc); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 441 | /// ParseStaticAssertDeclaration - Parse C++0x or C1X static_assert-declaration. |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 442 | /// |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 443 | /// [C++0x] static_assert-declaration: |
| 444 | /// static_assert ( constant-expression , string-literal ) ; |
| 445 | /// |
| 446 | /// [C1X] static_assert-declaration: |
| 447 | /// _Static_assert ( constant-expression , string-literal ) ; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 448 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 449 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 450 | assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) && |
| 451 | "Not a static_assert declaration"); |
| 452 | |
| 453 | if (Tok.is(tok::kw__Static_assert) && !getLang().C1X) |
| 454 | Diag(Tok, diag::ext_c1x_static_assert); |
| 455 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 456 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 458 | if (Tok.isNot(tok::l_paren)) { |
| 459 | Diag(Tok, diag::err_expected_lparen); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 460 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 463 | SourceLocation LParenLoc = ConsumeParen(); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 464 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 465 | ExprResult AssertExpr(ParseConstantExpression()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 466 | if (AssertExpr.isInvalid()) { |
| 467 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 468 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 471 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 472 | return 0; |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 473 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 474 | if (Tok.isNot(tok::string_literal)) { |
| 475 | Diag(Tok, diag::err_expected_string_literal); |
| 476 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 477 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 478 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 480 | ExprResult AssertMessage(ParseStringLiteralExpression()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | if (AssertMessage.isInvalid()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 482 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 483 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 484 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 486 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 487 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 488 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 489 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
| 490 | AssertExpr.take(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 491 | AssertMessage.take(), |
| 492 | RParenLoc); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 495 | /// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. |
| 496 | /// |
| 497 | /// 'decltype' ( expression ) |
| 498 | /// |
| 499 | void Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
| 500 | assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier"); |
| 501 | |
| 502 | SourceLocation StartLoc = ConsumeToken(); |
| 503 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
| 505 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 506 | "decltype")) { |
| 507 | SkipUntil(tok::r_paren); |
| 508 | return; |
| 509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 511 | // Parse the expression |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 513 | // C++0x [dcl.type.simple]p4: |
| 514 | // The operand of the decltype specifier is an unevaluated operand. |
| 515 | EnterExpressionEvaluationContext Unevaluated(Actions, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 516 | Sema::Unevaluated); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 517 | ExprResult Result = ParseExpression(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 518 | if (Result.isInvalid()) { |
| 519 | SkipUntil(tok::r_paren); |
| 520 | return; |
| 521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 523 | // Match the ')' |
| 524 | SourceLocation RParenLoc; |
| 525 | if (Tok.is(tok::r_paren)) |
| 526 | RParenLoc = ConsumeParen(); |
| 527 | else |
| 528 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 530 | if (RParenLoc.isInvalid()) |
| 531 | return; |
| 532 | |
| 533 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 534 | unsigned DiagID; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 535 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 537 | DiagID, Result.release())) |
| 538 | Diag(StartLoc, DiagID) << PrevSpec; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 541 | /// ParseClassName - Parse a C++ class-name, which names a class. Note |
| 542 | /// that we only check that the result names a type; semantic analysis |
| 543 | /// will need to verify that the type names a class. The result is |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 544 | /// either a type or NULL, depending on whether a type name was |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 545 | /// found. |
| 546 | /// |
| 547 | /// class-name: [C++ 9.1] |
| 548 | /// identifier |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 549 | /// simple-template-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | /// |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 551 | Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 552 | CXXScopeSpec &SS) { |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 553 | // Check whether we have a template-id that names a type. |
| 554 | if (Tok.is(tok::annot_template_id)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 556 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 557 | if (TemplateId->Kind == TNK_Type_template || |
| 558 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 559 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 560 | |
| 561 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 562 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 563 | EndLocation = Tok.getAnnotationEndLoc(); |
| 564 | ConsumeToken(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 565 | |
| 566 | if (Type) |
| 567 | return Type; |
| 568 | return true; |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // Fall through to produce an error below. |
| 572 | } |
| 573 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 574 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 575 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 576 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 579 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 580 | SourceLocation IdLoc = ConsumeToken(); |
| 581 | |
| 582 | if (Tok.is(tok::less)) { |
| 583 | // It looks the user intended to write a template-id here, but the |
| 584 | // template-name was wrong. Try to fix that. |
| 585 | TemplateNameKind TNK = TNK_Type_template; |
| 586 | TemplateTy Template; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 587 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 588 | &SS, Template, TNK)) { |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 589 | Diag(IdLoc, diag::err_unknown_template_name) |
| 590 | << Id; |
| 591 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 593 | if (!Template) |
| 594 | return true; |
| 595 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 596 | // Form the template name |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 597 | UnqualifiedId TemplateName; |
| 598 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 600 | // Parse the full template-id, then turn it into a type. |
| 601 | if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName, |
| 602 | SourceLocation(), true)) |
| 603 | return true; |
| 604 | if (TNK == TNK_Dependent_template_name) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 605 | AnnotateTemplateIdTokenAsType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 607 | // If we didn't end up with a typename token, there's nothing more we |
| 608 | // can do. |
| 609 | if (Tok.isNot(tok::annot_typename)) |
| 610 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 612 | // Retrieve the type from the annotation token, consume that token, and |
| 613 | // return. |
| 614 | EndLocation = Tok.getAnnotationEndLoc(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 615 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 616 | ConsumeToken(); |
| 617 | return Type; |
| 618 | } |
| 619 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 620 | // We have an identifier; check whether it is actually a type. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 621 | ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 622 | false, ParsedType(), |
| 623 | /*NonTrivialTypeSourceInfo=*/true); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 624 | if (!Type) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 625 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 626 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // Consume the identifier. |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 630 | EndLocation = IdLoc; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 631 | |
| 632 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 633 | DeclSpec DS(AttrFactory); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 634 | DS.SetRangeStart(IdLoc); |
| 635 | DS.SetRangeEnd(EndLocation); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 636 | DS.getTypeSpecScope() = SS; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 637 | |
| 638 | const char *PrevSpec = 0; |
| 639 | unsigned DiagID; |
| 640 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type); |
| 641 | |
| 642 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 643 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 646 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 647 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 648 | /// until we reach the start of a definition or see a token that |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 649 | /// cannot start a definition. If SuppressDeclarations is true, we do know. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 650 | /// |
| 651 | /// class-specifier: [C++ class] |
| 652 | /// class-head '{' member-specification[opt] '}' |
| 653 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 654 | /// class-head: |
| 655 | /// class-key identifier[opt] base-clause[opt] |
| 656 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 657 | /// class-key nested-name-specifier[opt] simple-template-id |
| 658 | /// base-clause[opt] |
| 659 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 661 | /// identifier base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 663 | /// simple-template-id base-clause[opt] |
| 664 | /// class-key: |
| 665 | /// 'class' |
| 666 | /// 'struct' |
| 667 | /// 'union' |
| 668 | /// |
| 669 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 671 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 672 | /// simple-template-id |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 673 | /// |
| 674 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 675 | /// together, subsume the C99 struct-or-union-specifier: |
| 676 | /// |
| 677 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 678 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 679 | /// struct-or-union identifier |
| 680 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 681 | /// '}' attributes[opt] |
| 682 | /// [GNU] struct-or-union attributes[opt] identifier |
| 683 | /// struct-or-union: |
| 684 | /// 'struct' |
| 685 | /// 'union' |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 686 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 687 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 688 | const ParsedTemplateInfo &TemplateInfo, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 689 | AccessSpecifier AS, bool SuppressDeclarations){ |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 690 | DeclSpec::TST TagType; |
| 691 | if (TagTokKind == tok::kw_struct) |
| 692 | TagType = DeclSpec::TST_struct; |
| 693 | else if (TagTokKind == tok::kw_class) |
| 694 | TagType = DeclSpec::TST_class; |
| 695 | else { |
| 696 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 697 | TagType = DeclSpec::TST_union; |
| 698 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 699 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 700 | if (Tok.is(tok::code_completion)) { |
| 701 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 702 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 703 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 704 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 705 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 706 | // C++03 [temp.explicit] 14.7.2/8: |
| 707 | // The usual access checking rules do not apply to names used to specify |
| 708 | // explicit instantiations. |
| 709 | // |
| 710 | // As an extension we do not perform access checking on the names used to |
| 711 | // specify explicit specializations either. This is important to allow |
| 712 | // specializing traits classes for private types. |
| 713 | bool SuppressingAccessChecks = false; |
| 714 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 715 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) { |
| 716 | Actions.ActOnStartSuppressingAccessChecks(); |
| 717 | SuppressingAccessChecks = true; |
| 718 | } |
| 719 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 720 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 721 | // If attributes exist after tag, parse them. |
| 722 | if (Tok.is(tok::kw___attribute)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 723 | ParseGNUAttributes(attrs); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 724 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 725 | // If declspecs exist after tag, parse them. |
John McCall | b1d397c | 2010-08-05 17:13:11 +0000 | [diff] [blame] | 726 | while (Tok.is(tok::kw___declspec)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 727 | ParseMicrosoftDeclSpec(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 728 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 729 | // If C++0x attributes exist here, parse them. |
| 730 | // FIXME: Are we consistent with the ordering of parsing of different |
| 731 | // styles of attributes? |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 732 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 734 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) { |
| 735 | // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but |
| 736 | // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | // token sequence "struct __is_pod", make __is_pod into a normal |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 738 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 739 | // properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 740 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 741 | Tok.setKind(tok::identifier); |
| 742 | } |
| 743 | |
| 744 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) { |
| 745 | // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but |
| 746 | // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | // token sequence "struct __is_empty", make __is_empty into a normal |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 748 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 749 | // properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 750 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 751 | Tok.setKind(tok::identifier); |
| 752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 754 | // Parse the (optional) nested-name-specifier. |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 755 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 756 | if (getLang().CPlusPlus) { |
| 757 | // "FOO : BAR" is not a potential typo for "FOO::BAR". |
| 758 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 759 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 760 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 761 | DS.SetTypeSpecError(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 762 | if (SS.isSet()) |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 763 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
| 764 | Diag(Tok, diag::err_expected_ident); |
| 765 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 767 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 768 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 769 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 770 | IdentifierInfo *Name = 0; |
| 771 | SourceLocation NameLoc; |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 772 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 773 | if (Tok.is(tok::identifier)) { |
| 774 | Name = Tok.getIdentifierInfo(); |
| 775 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 776 | |
Douglas Gregor | 5ee3734 | 2010-05-30 22:30:21 +0000 | [diff] [blame] | 777 | if (Tok.is(tok::less) && getLang().CPlusPlus) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 778 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 779 | // Eat the template argument list and try to continue parsing this as |
| 780 | // a class (or template thereof). |
| 781 | TemplateArgList TemplateArgs; |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 782 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 783 | if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS, |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 784 | true, LAngleLoc, |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 785 | TemplateArgs, RAngleLoc)) { |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 786 | // We couldn't parse the template argument list at all, so don't |
| 787 | // try to give any location information for the list. |
| 788 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 789 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 791 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 792 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 793 | << (TagType == DeclSpec::TST_class? 0 |
| 794 | : TagType == DeclSpec::TST_struct? 1 |
| 795 | : 2) |
| 796 | << Name |
| 797 | << SourceRange(LAngleLoc, RAngleLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 798 | |
| 799 | // Strip off the last template parameter list if it was empty, since |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 800 | // we've removed its template argument list. |
| 801 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
| 802 | if (TemplateParams && TemplateParams->size() > 1) { |
| 803 | TemplateParams->pop_back(); |
| 804 | } else { |
| 805 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 806 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 807 | = ParsedTemplateInfo::NonTemplate; |
| 808 | } |
| 809 | } else if (TemplateInfo.Kind |
| 810 | == ParsedTemplateInfo::ExplicitInstantiation) { |
| 811 | // Pretend this is just a forward declaration. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 812 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 813 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 814 | = ParsedTemplateInfo::NonTemplate; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 815 | const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 816 | = SourceLocation(); |
| 817 | const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc |
| 818 | = SourceLocation(); |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 819 | } |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 820 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 821 | } else if (Tok.is(tok::annot_template_id)) { |
| 822 | TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 823 | NameLoc = ConsumeToken(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 825 | if (TemplateId->Kind != TNK_Type_template && |
| 826 | TemplateId->Kind != TNK_Dependent_template_name) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 827 | // The template-name in the simple-template-id refers to |
| 828 | // something other than a class template. Give an appropriate |
| 829 | // error message and skip to the ';'. |
| 830 | SourceRange Range(NameLoc); |
| 831 | if (SS.isNotEmpty()) |
| 832 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 833 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 834 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
| 835 | << Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 837 | DS.SetTypeSpecError(); |
| 838 | SkipUntil(tok::semi, false, true); |
| 839 | TemplateId->Destroy(); |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 840 | if (SuppressingAccessChecks) |
| 841 | Actions.ActOnStopSuppressingAccessChecks(); |
| 842 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 843 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 844 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 847 | // As soon as we're finished parsing the class's template-id, turn access |
| 848 | // checking back on. |
| 849 | if (SuppressingAccessChecks) |
| 850 | Actions.ActOnStopSuppressingAccessChecks(); |
| 851 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 852 | // There are four options here. If we have 'struct foo;', then this |
| 853 | // is either a forward declaration or a friend declaration, which |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 854 | // have to be treated differently. If we have 'struct foo {...', |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 855 | // 'struct foo :...' or 'struct foo final[opt]' then this is a |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 856 | // definition. Otherwise we have something like 'struct foo xyz', a reference. |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 857 | // However, in some contexts, things look like declarations but are just |
| 858 | // references, e.g. |
| 859 | // new struct s; |
| 860 | // or |
| 861 | // &T::operator struct s; |
| 862 | // For these, SuppressDeclarations is true. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 863 | Sema::TagUseKind TUK; |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 864 | if (SuppressDeclarations) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 865 | TUK = Sema::TUK_Reference; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 866 | else if (Tok.is(tok::l_brace) || |
| 867 | (getLang().CPlusPlus && Tok.is(tok::colon)) || |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 868 | isCXX0XFinalKeyword()) { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 869 | if (DS.isFriendSpecified()) { |
| 870 | // C++ [class.friend]p2: |
| 871 | // A class shall not be defined in a friend declaration. |
| 872 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_class) |
| 873 | << SourceRange(DS.getFriendSpecLoc()); |
| 874 | |
| 875 | // Skip everything up to the semicolon, so that this looks like a proper |
| 876 | // friend class (or template thereof) declaration. |
| 877 | SkipUntil(tok::semi, true, true); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 878 | TUK = Sema::TUK_Friend; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 879 | } else { |
| 880 | // Okay, this is a class definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 881 | TUK = Sema::TUK_Definition; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 882 | } |
| 883 | } else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 884 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 885 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 886 | TUK = Sema::TUK_Reference; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 887 | |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 888 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 889 | TUK != Sema::TUK_Definition)) { |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 890 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 891 | // We have a declaration or reference to an anonymous class. |
| 892 | Diag(StartLoc, diag::err_anon_type_definition) |
| 893 | << DeclSpec::getSpecifierName(TagType); |
| 894 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 896 | SkipUntil(tok::comma, true); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 897 | |
| 898 | if (TemplateId) |
| 899 | TemplateId->Destroy(); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 903 | // Create the tag portion of the class or class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 904 | DeclResult TagOrTempResult = true; // invalid |
| 905 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 907 | bool Owned = false; |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 908 | if (TemplateId) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 909 | // Explicit specialization, class template partial specialization, |
| 910 | // or explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 912 | TemplateId->getTemplateArgs(), |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 913 | TemplateId->NumArgs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 914 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 915 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 916 | // This is an explicit instantiation of a class template. |
| 917 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 918 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 919 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | TemplateInfo.TemplateLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 921 | TagType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | StartLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 923 | SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 924 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | TemplateId->TemplateNameLoc, |
| 926 | TemplateId->LAngleLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 927 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 929 | attrs.getList()); |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 930 | |
| 931 | // Friend template-ids are treated as references unless |
| 932 | // they have template headers, in which case they're ill-formed |
| 933 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 934 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 935 | } else if (TUK == Sema::TUK_Reference || |
| 936 | (TUK == Sema::TUK_Friend && |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 937 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 938 | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, |
| 939 | StartLoc, |
| 940 | TemplateId->SS, |
| 941 | TemplateId->Template, |
| 942 | TemplateId->TemplateNameLoc, |
| 943 | TemplateId->LAngleLoc, |
| 944 | TemplateArgsPtr, |
| 945 | TemplateId->RAngleLoc); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 946 | } else { |
| 947 | // This is an explicit specialization or a class template |
| 948 | // partial specialization. |
| 949 | TemplateParameterLists FakedParamLists; |
| 950 | |
| 951 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 952 | // This looks like an explicit instantiation, because we have |
| 953 | // something like |
| 954 | // |
| 955 | // template class Foo<X> |
| 956 | // |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 957 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 958 | // meant to be an explicit specialization, but the user forgot |
| 959 | // the '<>' after 'template'. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 960 | assert(TUK == Sema::TUK_Definition && "Expected a definition here"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 961 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | SourceLocation LAngleLoc |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 963 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | Diag(TemplateId->TemplateNameLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 965 | diag::err_explicit_instantiation_with_definition) |
| 966 | << SourceRange(TemplateInfo.TemplateLoc) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 967 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 968 | |
| 969 | // Create a fake template parameter list that contains only |
| 970 | // "template<>", so that we treat this construct as a class |
| 971 | // template specialization. |
| 972 | FakedParamLists.push_back( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | Actions.ActOnTemplateParameterList(0, SourceLocation(), |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 974 | TemplateInfo.TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | LAngleLoc, |
| 976 | 0, 0, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 977 | LAngleLoc)); |
| 978 | TemplateParams = &FakedParamLists; |
| 979 | } |
| 980 | |
| 981 | // Build the class template specialization. |
| 982 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 983 | = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 984 | StartLoc, SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 985 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | TemplateId->TemplateNameLoc, |
| 987 | TemplateId->LAngleLoc, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 988 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 990 | attrs.getList(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 991 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 992 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 993 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 994 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 995 | TemplateId->Destroy(); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 996 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 997 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 998 | // Explicit instantiation of a member of a class template |
| 999 | // specialization, e.g., |
| 1000 | // |
| 1001 | // template struct Outer<int>::Inner; |
| 1002 | // |
| 1003 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1004 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1005 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | TemplateInfo.TemplateLoc, |
| 1007 | TagType, StartLoc, SS, Name, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1008 | NameLoc, attrs.getList()); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1009 | } else if (TUK == Sema::TUK_Friend && |
| 1010 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
| 1011 | TagOrTempResult = |
| 1012 | Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(), |
| 1013 | TagType, StartLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1014 | Name, NameLoc, attrs.getList(), |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1015 | MultiTemplateParamsArg(Actions, |
| 1016 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 1017 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1018 | } else { |
| 1019 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1020 | TUK == Sema::TUK_Definition) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1021 | // FIXME: Diagnose this particular error. |
| 1022 | } |
| 1023 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1024 | bool IsDependent = false; |
| 1025 | |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 1026 | // Don't pass down template parameter lists if this is just a tag |
| 1027 | // reference. For example, we don't need the template parameters here: |
| 1028 | // template <class T> class A *makeA(T t); |
| 1029 | MultiTemplateParamsArg TParams; |
| 1030 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 1031 | TParams = |
| 1032 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 1033 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1034 | // Declaration or definition of a class type |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1035 | TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1036 | SS, Name, NameLoc, attrs.getList(), AS, |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 1037 | TParams, Owned, IsDependent, false, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 1038 | false, clang::TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1039 | |
| 1040 | // If ActOnTag said the type was dependent, try again with the |
| 1041 | // less common call. |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1042 | if (IsDependent) { |
| 1043 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1044 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1045 | SS, Name, StartLoc, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1046 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1047 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1049 | // If there is a body, parse it and inform the actions module. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1050 | if (TUK == Sema::TUK_Definition) { |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1051 | assert(Tok.is(tok::l_brace) || |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1052 | (getLang().CPlusPlus && Tok.is(tok::colon)) || |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1053 | isCXX0XFinalKeyword()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1054 | if (getLang().CPlusPlus) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1055 | ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1056 | else |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1057 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1060 | const char *PrevSpec = 0; |
| 1061 | unsigned DiagID; |
| 1062 | bool Result; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1063 | if (!TypeResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1064 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 1065 | NameLoc.isValid() ? NameLoc : StartLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1066 | PrevSpec, DiagID, TypeResult.get()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1067 | } else if (!TagOrTempResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1068 | Result = DS.SetTypeSpecType(TagType, StartLoc, |
| 1069 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 1070 | PrevSpec, DiagID, TagOrTempResult.get(), Owned); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1071 | } else { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1072 | DS.SetTypeSpecError(); |
Anders Carlsson | 66e9977 | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1076 | if (Result) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1077 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1078 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1079 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 1080 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 1081 | // going to look at what comes after it to improve error recovery. If an |
| 1082 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 1083 | // the end of the declaration and recover that way. |
| 1084 | // |
| 1085 | // This switch enumerates the valid "follow" set for definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1086 | if (TUK == Sema::TUK_Definition) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1087 | bool ExpectedSemi = true; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1088 | switch (Tok.getKind()) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1089 | default: break; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1090 | case tok::semi: // struct foo {...} ; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1091 | case tok::star: // struct foo {...} * P; |
| 1092 | case tok::amp: // struct foo {...} & R = ... |
| 1093 | case tok::identifier: // struct foo {...} V ; |
| 1094 | case tok::r_paren: //(struct foo {...} ) {4} |
| 1095 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1096 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1097 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
Chris Lattner | c2e1c1a | 2010-02-03 20:41:24 +0000 | [diff] [blame] | 1098 | case tok::l_paren: // struct foo {...} ( x); |
Chris Lattner | 16acfee | 2010-02-03 01:45:03 +0000 | [diff] [blame] | 1099 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1100 | ExpectedSemi = false; |
| 1101 | break; |
| 1102 | // Type qualifiers |
| 1103 | case tok::kw_const: // struct foo {...} const x; |
| 1104 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1105 | case tok::kw_restrict: // struct foo {...} restrict x; |
| 1106 | case tok::kw_inline: // struct foo {...} inline foo() {}; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1107 | // Storage-class specifiers |
| 1108 | case tok::kw_static: // struct foo {...} static x; |
| 1109 | case tok::kw_extern: // struct foo {...} extern x; |
| 1110 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1111 | case tok::kw_register: // struct foo {...} register x; |
| 1112 | case tok::kw_auto: // struct foo {...} auto x; |
Douglas Gregor | 33f9924 | 2010-05-17 18:19:56 +0000 | [diff] [blame] | 1113 | case tok::kw_mutable: // struct foo {...} mutable x; |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1114 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1115 | // can occur after class specifiers according to the grammar. However, |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1116 | // almost no one actually writes code like this. If we see one of these, |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1117 | // it is much more likely that someone missed a semi colon and the |
| 1118 | // type/storage class specifier we're seeing is part of the *next* |
| 1119 | // intended declaration, as in: |
| 1120 | // |
| 1121 | // struct foo { ... } |
| 1122 | // typedef int X; |
| 1123 | // |
| 1124 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1125 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1126 | // the same declaration of X. Because of this, we look ahead past this |
| 1127 | // token to see if it's a type specifier. If so, we know the code is |
| 1128 | // otherwise invalid, so we can produce the expected semi error. |
| 1129 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1130 | ExpectedSemi = false; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1131 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1132 | |
| 1133 | case tok::r_brace: // struct bar { struct foo {...} } |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1134 | // Missing ';' at end of struct is accepted as an extension in C mode. |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1135 | if (!getLang().CPlusPlus) |
| 1136 | ExpectedSemi = false; |
| 1137 | break; |
| 1138 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1140 | if (ExpectedSemi) { |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1141 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1142 | TagType == DeclSpec::TST_class ? "class" |
| 1143 | : TagType == DeclSpec::TST_struct? "struct" : "union"); |
| 1144 | // Push this token back into the preprocessor and change our current token |
| 1145 | // to ';' so that the rest of the code recovers as though there were an |
| 1146 | // ';' after the definition. |
| 1147 | PP.EnterToken(Tok); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1148 | Tok.setKind(tok::semi); |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1154 | /// |
| 1155 | /// base-clause : [C++ class.derived] |
| 1156 | /// ':' base-specifier-list |
| 1157 | /// base-specifier-list: |
| 1158 | /// base-specifier '...'[opt] |
| 1159 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1160 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1161 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 1162 | ConsumeToken(); |
| 1163 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1164 | // Build up an array of parsed base specifiers. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1165 | llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1166 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1167 | while (true) { |
| 1168 | // Parse a base-specifier. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1169 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1170 | if (Result.isInvalid()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1171 | // Skip the rest of this base specifier, up until the comma or |
| 1172 | // opening brace. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1173 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 1174 | } else { |
| 1175 | // Add this to our array of base specifiers. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1176 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | // If the next token is a comma, consume it and keep reading |
| 1180 | // base-specifiers. |
| 1181 | if (Tok.isNot(tok::comma)) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1183 | // Consume the comma. |
| 1184 | ConsumeToken(); |
| 1185 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1186 | |
| 1187 | // Attach the base specifiers |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1188 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 1192 | /// one entry in the base class list of a class specifier, for example: |
| 1193 | /// class foo : public bar, virtual private baz { |
| 1194 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 1195 | /// |
| 1196 | /// base-specifier: [C++ class.derived] |
| 1197 | /// ::[opt] nested-name-specifier[opt] class-name |
| 1198 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
| 1199 | /// class-name |
| 1200 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
| 1201 | /// class-name |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1202 | Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1203 | bool IsVirtual = false; |
| 1204 | SourceLocation StartLoc = Tok.getLocation(); |
| 1205 | |
| 1206 | // Parse the 'virtual' keyword. |
| 1207 | if (Tok.is(tok::kw_virtual)) { |
| 1208 | ConsumeToken(); |
| 1209 | IsVirtual = true; |
| 1210 | } |
| 1211 | |
| 1212 | // Parse an (optional) access specifier. |
| 1213 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1214 | if (Access != AS_none) |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1215 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1217 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 1218 | // access specifier. |
| 1219 | if (Tok.is(tok::kw_virtual)) { |
| 1220 | SourceLocation VirtualLoc = ConsumeToken(); |
| 1221 | if (IsVirtual) { |
| 1222 | // Complain about duplicate 'virtual' |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1223 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1224 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | IsVirtual = true; |
| 1228 | } |
| 1229 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1230 | // Parse optional '::' and optional nested-name-specifier. |
| 1231 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1232 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1233 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1234 | // The location of the base class itself. |
| 1235 | SourceLocation BaseLoc = Tok.getLocation(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1236 | |
| 1237 | // Parse the class-name. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1238 | SourceLocation EndLocation; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1239 | TypeResult BaseType = ParseClassName(EndLocation, SS); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1240 | if (BaseType.isInvalid()) |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1241 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1243 | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
| 1244 | // actually part of the base-specifier-list grammar productions, but we |
| 1245 | // parse it here for convenience. |
| 1246 | SourceLocation EllipsisLoc; |
| 1247 | if (Tok.is(tok::ellipsis)) |
| 1248 | EllipsisLoc = ConsumeToken(); |
| 1249 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1251 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1253 | // Notify semantic analysis that we have parsed a complete |
| 1254 | // base-specifier. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1255 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1256 | BaseType.get(), BaseLoc, EllipsisLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 1260 | /// a C++ access-specifier. |
| 1261 | /// |
| 1262 | /// access-specifier: [C++ class.derived] |
| 1263 | /// 'private' |
| 1264 | /// 'protected' |
| 1265 | /// 'public' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1267 | switch (Tok.getKind()) { |
| 1268 | default: return AS_none; |
| 1269 | case tok::kw_private: return AS_private; |
| 1270 | case tok::kw_protected: return AS_protected; |
| 1271 | case tok::kw_public: return AS_public; |
| 1272 | } |
| 1273 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1274 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1275 | void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1276 | Decl *ThisDecl) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1277 | // We just declared a member function. If this member function |
| 1278 | // has any default arguments, we'll need to parse them later. |
| 1279 | LateParsedMethodDeclaration *LateMethod = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | DeclaratorChunk::FunctionTypeInfo &FTI |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1281 | = DeclaratorInfo.getFunctionTypeInfo(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1282 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 1283 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 1284 | if (!LateMethod) { |
| 1285 | // Push this method onto the stack of late-parsed method |
| 1286 | // declarations. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1287 | LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
| 1288 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1289 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1290 | |
| 1291 | // Add all of the parameters prior to this one (they don't |
| 1292 | // have default arguments). |
| 1293 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 1294 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 1295 | LateMethod->DefaultArgs.push_back( |
Douglas Gregor | 8f8210c | 2010-03-02 01:29:43 +0000 | [diff] [blame] | 1296 | LateParsedDefaultArgument(FTI.ArgInfo[I].Param)); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // Add this parameter to the list of parameters (it or may |
| 1300 | // not have a default argument). |
| 1301 | LateMethod->DefaultArgs.push_back( |
| 1302 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 1303 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1308 | /// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x |
| 1309 | /// virt-specifier. |
| 1310 | /// |
| 1311 | /// virt-specifier: |
| 1312 | /// override |
| 1313 | /// final |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1314 | VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const { |
Anders Carlsson | ce93a7c | 2011-01-22 23:01:49 +0000 | [diff] [blame] | 1315 | if (!getLang().CPlusPlus) |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1316 | return VirtSpecifiers::VS_None; |
| 1317 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1318 | if (Tok.is(tok::identifier)) { |
| 1319 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1320 | |
Anders Carlsson | 7eeb4ec | 2011-01-20 03:47:08 +0000 | [diff] [blame] | 1321 | // Initialize the contextual keywords. |
| 1322 | if (!Ident_final) { |
| 1323 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1324 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1325 | } |
| 1326 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1327 | if (II == Ident_override) |
| 1328 | return VirtSpecifiers::VS_Override; |
| 1329 | |
| 1330 | if (II == Ident_final) |
| 1331 | return VirtSpecifiers::VS_Final; |
| 1332 | } |
| 1333 | |
| 1334 | return VirtSpecifiers::VS_None; |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq. |
| 1338 | /// |
| 1339 | /// virt-specifier-seq: |
| 1340 | /// virt-specifier |
| 1341 | /// virt-specifier-seq virt-specifier |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1342 | void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) { |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1343 | while (true) { |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1344 | VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier(); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1345 | if (Specifier == VirtSpecifiers::VS_None) |
| 1346 | return; |
| 1347 | |
| 1348 | // C++ [class.mem]p8: |
| 1349 | // A virt-specifier-seq shall contain at most one of each virt-specifier. |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1350 | const char *PrevSpec = 0; |
Anders Carlsson | 46127a9 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 1351 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1352 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
| 1353 | << PrevSpec |
| 1354 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1355 | |
Anders Carlsson | ce93a7c | 2011-01-22 23:01:49 +0000 | [diff] [blame] | 1356 | if (!getLang().CPlusPlus0x) |
| 1357 | Diag(Tok.getLocation(), diag::ext_override_control_keyword) |
| 1358 | << VirtSpecifiers::getSpecifierName(Specifier); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1359 | ConsumeToken(); |
| 1360 | } |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1363 | /// isCXX0XFinalKeyword - Determine whether the next token is a C++0x |
| 1364 | /// contextual 'final' keyword. |
| 1365 | bool Parser::isCXX0XFinalKeyword() const { |
Anders Carlsson | ce93a7c | 2011-01-22 23:01:49 +0000 | [diff] [blame] | 1366 | if (!getLang().CPlusPlus) |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1367 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1368 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1369 | if (!Tok.is(tok::identifier)) |
| 1370 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1371 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1372 | // Initialize the contextual keywords. |
| 1373 | if (!Ident_final) { |
| 1374 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1375 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1376 | } |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1377 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1378 | return Tok.getIdentifierInfo() == Ident_final; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1381 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 1382 | /// |
| 1383 | /// member-declaration: |
| 1384 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 1385 | /// function-definition ';'[opt] |
| 1386 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 1387 | /// using-declaration [TODO] |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1388 | /// [C++0x] static_assert-declaration |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1389 | /// template-declaration |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1390 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1391 | /// |
| 1392 | /// member-declarator-list: |
| 1393 | /// member-declarator |
| 1394 | /// member-declarator-list ',' member-declarator |
| 1395 | /// |
| 1396 | /// member-declarator: |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1397 | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1398 | /// declarator constant-initializer[opt] |
| 1399 | /// identifier[opt] ':' constant-expression |
| 1400 | /// |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1401 | /// virt-specifier-seq: |
| 1402 | /// virt-specifier |
| 1403 | /// virt-specifier-seq virt-specifier |
| 1404 | /// |
| 1405 | /// virt-specifier: |
| 1406 | /// override |
| 1407 | /// final |
| 1408 | /// new |
| 1409 | /// |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1410 | /// pure-specifier: |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1411 | /// '= 0' |
| 1412 | /// |
| 1413 | /// constant-initializer: |
| 1414 | /// '=' constant-expression |
| 1415 | /// |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1416 | void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1417 | const ParsedTemplateInfo &TemplateInfo, |
| 1418 | ParsingDeclRAIIObject *TemplateDiags) { |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1419 | if (Tok.is(tok::at)) { |
| 1420 | if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs)) |
| 1421 | Diag(Tok, diag::err_at_defs_cxx); |
| 1422 | else |
| 1423 | Diag(Tok, diag::err_at_in_class); |
| 1424 | |
| 1425 | ConsumeToken(); |
| 1426 | SkipUntil(tok::r_brace); |
| 1427 | return; |
| 1428 | } |
| 1429 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1430 | // Access declarations. |
| 1431 | if (!TemplateInfo.Kind && |
| 1432 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1433 | !TryAnnotateCXXScopeToken() && |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1434 | Tok.is(tok::annot_cxxscope)) { |
| 1435 | bool isAccessDecl = false; |
| 1436 | if (NextToken().is(tok::identifier)) |
| 1437 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 1438 | else |
| 1439 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 1440 | |
| 1441 | if (isAccessDecl) { |
| 1442 | // Collect the scope specifier token we annotated earlier. |
| 1443 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1444 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Try to parse an unqualified-id. |
| 1447 | UnqualifiedId Name; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1448 | if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1449 | SkipUntil(tok::semi); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | // TODO: recover from mistakenly-qualified operator declarations. |
| 1454 | if (ExpectAndConsume(tok::semi, |
| 1455 | diag::err_expected_semi_after, |
| 1456 | "access declaration", |
| 1457 | tok::semi)) |
| 1458 | return; |
| 1459 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1460 | Actions.ActOnUsingDeclaration(getCurScope(), AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1461 | false, SourceLocation(), |
| 1462 | SS, Name, |
| 1463 | /* AttrList */ 0, |
| 1464 | /* IsTypeName */ false, |
| 1465 | SourceLocation()); |
| 1466 | return; |
| 1467 | } |
| 1468 | } |
| 1469 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1470 | // static_assert-declaration |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1471 | if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1472 | // FIXME: Check for templates |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1473 | SourceLocation DeclEnd; |
| 1474 | ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1475 | return; |
| 1476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1478 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1480 | "Nested template improperly parsed?"); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1481 | SourceLocation DeclEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1483 | AS); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1484 | return; |
| 1485 | } |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1487 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 1488 | if (Tok.is(tok::kw___extension__)) { |
| 1489 | // __extension__ silences extension warnings in the subexpression. |
| 1490 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 1491 | ConsumeToken(); |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1492 | return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags); |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1493 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1495 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it |
| 1496 | // is a bitfield. |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1497 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1498 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1499 | ParsedAttributesWithRange attrs(AttrFactory); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1500 | // Optional C++0x attribute-specifier |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1501 | MaybeParseCXX0XAttributes(attrs); |
| 1502 | MaybeParseMicrosoftAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1503 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1504 | if (Tok.is(tok::kw_using)) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1505 | // FIXME: Check for template aliases |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1506 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1507 | ProhibitAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1509 | // Eat 'using'. |
| 1510 | SourceLocation UsingLoc = ConsumeToken(); |
| 1511 | |
| 1512 | if (Tok.is(tok::kw_namespace)) { |
| 1513 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
| 1514 | SkipUntil(tok::semi, true, true); |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1515 | } else { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1516 | SourceLocation DeclEnd; |
| 1517 | // Otherwise, it must be using-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1518 | ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo, |
| 1519 | UsingLoc, DeclEnd, AS); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1520 | } |
| 1521 | return; |
| 1522 | } |
| 1523 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1524 | // decl-specifier-seq: |
| 1525 | // Parse the common declaration-specifiers piece. |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1526 | ParsingDeclSpec DS(*this, TemplateDiags); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1527 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1528 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1529 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1530 | MultiTemplateParamsArg TemplateParams(Actions, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 1531 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, |
| 1532 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 1533 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1534 | if (Tok.is(tok::semi)) { |
| 1535 | ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1536 | Decl *TheDecl = |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1537 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); |
| 1538 | DS.complete(TheDecl); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1539 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1540 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1541 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1542 | ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1543 | VirtSpecifiers VS; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1544 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1545 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1546 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 1547 | ColonProtectionRAIIObject X(*this); |
| 1548 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1549 | // Parse the first declarator. |
| 1550 | ParseDeclarator(DeclaratorInfo); |
| 1551 | // Error parsing the declarator? |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1552 | if (!DeclaratorInfo.hasName()) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1553 | // If so, skip until the semi-colon or a }. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1554 | SkipUntil(tok::r_brace, true); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1555 | if (Tok.is(tok::semi)) |
| 1556 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1557 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1560 | ParseOptionalCXX0XVirtSpecifierSeq(VS); |
| 1561 | |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 1562 | // If attributes exist after the declarator, but before an '{', parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1563 | MaybeParseGNUAttributes(DeclaratorInfo); |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 1564 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1565 | // function-definition: |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1566 | if (Tok.is(tok::l_brace) |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1567 | || (DeclaratorInfo.isFunctionDeclarator() && |
| 1568 | (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1569 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
| 1570 | Diag(Tok, diag::err_func_def_no_params); |
| 1571 | ConsumeBrace(); |
| 1572 | SkipUntil(tok::r_brace, true); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1573 | |
| 1574 | // Consume the optional ';' |
| 1575 | if (Tok.is(tok::semi)) |
| 1576 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1577 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1578 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1579 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1580 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1581 | Diag(Tok, diag::err_function_declared_typedef); |
| 1582 | // This recovery skips the entire function body. It would be nice |
| 1583 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 1584 | // assumes the declarator represents a function, not a typedef. |
| 1585 | ConsumeBrace(); |
| 1586 | SkipUntil(tok::r_brace, true); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1587 | |
| 1588 | // Consume the optional ';' |
| 1589 | if (Tok.is(tok::semi)) |
| 1590 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1591 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1594 | ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1595 | // Consume the optional ';' |
| 1596 | if (Tok.is(tok::semi)) |
| 1597 | ConsumeToken(); |
| 1598 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1599 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1600 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | // member-declarator-list: |
| 1604 | // member-declarator |
| 1605 | // member-declarator-list ',' member-declarator |
| 1606 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1607 | llvm::SmallVector<Decl *, 8> DeclsInGroup; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1608 | ExprResult BitfieldSize; |
| 1609 | ExprResult Init; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1610 | bool Deleted = false; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1611 | |
| 1612 | while (1) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1613 | // member-declarator: |
| 1614 | // declarator pure-specifier[opt] |
| 1615 | // declarator constant-initializer[opt] |
| 1616 | // identifier[opt] ':' constant-expression |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1617 | if (Tok.is(tok::colon)) { |
| 1618 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1619 | BitfieldSize = ParseConstantExpression(); |
| 1620 | if (BitfieldSize.isInvalid()) |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1621 | SkipUntil(tok::comma, true, true); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1622 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1624 | ParseOptionalCXX0XVirtSpecifierSeq(VS); |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1625 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1626 | // pure-specifier: |
| 1627 | // '= 0' |
| 1628 | // |
| 1629 | // constant-initializer: |
| 1630 | // '=' constant-expression |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1631 | // |
| 1632 | // defaulted/deleted function-definition: |
| 1633 | // '=' 'default' [TODO] |
| 1634 | // '=' 'delete' |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1635 | if (Tok.is(tok::equal)) { |
| 1636 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1637 | if (Tok.is(tok::kw_delete)) { |
| 1638 | if (!getLang().CPlusPlus0x) |
| 1639 | Diag(Tok, diag::warn_deleted_function_accepted_as_extension); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1640 | ConsumeToken(); |
| 1641 | Deleted = true; |
| 1642 | } else { |
| 1643 | Init = ParseInitializer(); |
| 1644 | if (Init.isInvalid()) |
| 1645 | SkipUntil(tok::comma, true, true); |
| 1646 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1649 | // If a simple-asm-expr is present, parse it. |
| 1650 | if (Tok.is(tok::kw_asm)) { |
| 1651 | SourceLocation Loc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1652 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1653 | if (AsmLabel.isInvalid()) |
| 1654 | SkipUntil(tok::comma, true, true); |
| 1655 | |
| 1656 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
| 1657 | DeclaratorInfo.SetRangeEnd(Loc); |
| 1658 | } |
| 1659 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1660 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1661 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1662 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1663 | // NOTE: If Sema is the Action module and declarator is an instance field, |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1664 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1665 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1666 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1667 | Decl *ThisDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1668 | if (DS.isFriendSpecified()) { |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1669 | // TODO: handle initializers, bitfields, 'delete' |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1670 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1671 | /*IsDefinition*/ false, |
| 1672 | move(TemplateParams)); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1673 | } else { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1674 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1675 | DeclaratorInfo, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1676 | move(TemplateParams), |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1677 | BitfieldSize.release(), |
Anders Carlsson | 69a8735 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 1678 | VS, Init.release(), |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 1679 | /*IsDefinition*/Deleted, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1680 | Deleted); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1681 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1682 | if (ThisDecl) |
| 1683 | DeclsInGroup.push_back(ThisDecl); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1684 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1685 | if (DeclaratorInfo.isFunctionDeclarator() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1687 | != DeclSpec::SCS_typedef) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1688 | HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1691 | DeclaratorInfo.complete(ThisDecl); |
| 1692 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1693 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1694 | // or an error, bail out. |
| 1695 | if (Tok.isNot(tok::comma)) |
| 1696 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1698 | // Consume the comma. |
| 1699 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1701 | // Parse the next declarator. |
| 1702 | DeclaratorInfo.clear(); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1703 | VS.clear(); |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1704 | BitfieldSize = 0; |
| 1705 | Init = 0; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1706 | Deleted = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1708 | // Attributes are only allowed on the second declarator. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1709 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1710 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1711 | if (Tok.isNot(tok::colon)) |
| 1712 | ParseDeclarator(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1715 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
| 1716 | // Skip to end of block or statement. |
| 1717 | SkipUntil(tok::r_brace, true, true); |
| 1718 | // If we stopped at a ';', eat it. |
| 1719 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1720 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1723 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(), |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1724 | DeclsInGroup.size()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 1728 | /// |
| 1729 | /// member-specification: |
| 1730 | /// member-declaration member-specification[opt] |
| 1731 | /// access-specifier ':' member-specification[opt] |
| 1732 | /// |
| 1733 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1734 | unsigned TagType, Decl *TagDecl) { |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1735 | assert((TagType == DeclSpec::TST_struct || |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1736 | TagType == DeclSpec::TST_union || |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1737 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1738 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1739 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 1740 | "parsing struct/union/class body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1742 | // Determine whether this is a non-nested class. Note that local |
| 1743 | // classes are *not* considered to be nested classes. |
| 1744 | bool NonNestedClass = true; |
| 1745 | if (!ClassStack.empty()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1746 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1747 | if (S->isClassScope()) { |
| 1748 | // We're inside a class scope, so this is a nested class. |
| 1749 | NonNestedClass = false; |
| 1750 | break; |
| 1751 | } |
| 1752 | |
| 1753 | if ((S->getFlags() & Scope::FnScope)) { |
| 1754 | // If we're in a function or function template declared in the |
| 1755 | // body of a class, then this is a local class rather than a |
| 1756 | // nested class. |
| 1757 | const Scope *Parent = S->getParent(); |
| 1758 | if (Parent->isTemplateParamScope()) |
| 1759 | Parent = Parent->getParent(); |
| 1760 | if (Parent->isClassScope()) |
| 1761 | break; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1765 | |
| 1766 | // Enter a scope for the class. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1767 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1768 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1769 | // Note that we are parsing a new (potentially-nested) class definition. |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1770 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1772 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1773 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1774 | |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 1775 | SourceLocation FinalLoc; |
| 1776 | |
| 1777 | // Parse the optional 'final' keyword. |
| 1778 | if (getLang().CPlusPlus && Tok.is(tok::identifier)) { |
| 1779 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1780 | |
| 1781 | // Initialize the contextual keywords. |
| 1782 | if (!Ident_final) { |
| 1783 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1784 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1785 | } |
| 1786 | |
| 1787 | if (II == Ident_final) |
| 1788 | FinalLoc = ConsumeToken(); |
| 1789 | |
| 1790 | if (!getLang().CPlusPlus0x) |
| 1791 | Diag(FinalLoc, diag::ext_override_control_keyword) << "final"; |
| 1792 | } |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1793 | |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1794 | if (Tok.is(tok::colon)) { |
| 1795 | ParseBaseClause(TagDecl); |
| 1796 | |
| 1797 | if (!Tok.is(tok::l_brace)) { |
| 1798 | Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 1799 | |
| 1800 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1801 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1802 | return; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | assert(Tok.is(tok::l_brace)); |
| 1807 | |
| 1808 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1809 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1810 | if (TagDecl) |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 1811 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1812 | LBraceLoc); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 1813 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1814 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 1815 | // by default. Members of a class defined with the keywords struct or union |
| 1816 | // are public by default. |
| 1817 | AccessSpecifier CurAS; |
| 1818 | if (TagType == DeclSpec::TST_class) |
| 1819 | CurAS = AS_private; |
| 1820 | else |
| 1821 | CurAS = AS_public; |
| 1822 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1823 | SourceLocation RBraceLoc; |
| 1824 | if (TagDecl) { |
| 1825 | // While we still have something to read, read the member-declarations. |
| 1826 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 1827 | // Each iteration of this loop reads one member-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1829 | // Check for extraneous top-level semicolon. |
| 1830 | if (Tok.is(tok::semi)) { |
| 1831 | Diag(Tok, diag::ext_extra_struct_semi) |
| 1832 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
| 1833 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1834 | ConsumeToken(); |
| 1835 | continue; |
| 1836 | } |
| 1837 | |
| 1838 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 1839 | if (AS != AS_none) { |
| 1840 | // Current token is a C++ access specifier. |
| 1841 | CurAS = AS; |
| 1842 | SourceLocation ASLoc = Tok.getLocation(); |
| 1843 | ConsumeToken(); |
| 1844 | if (Tok.is(tok::colon)) |
| 1845 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation()); |
| 1846 | else |
| 1847 | Diag(Tok, diag::err_expected_colon); |
| 1848 | ConsumeToken(); |
| 1849 | continue; |
| 1850 | } |
| 1851 | |
| 1852 | // FIXME: Make sure we don't have a template here. |
| 1853 | |
| 1854 | // Parse all the comma separated declarators. |
| 1855 | ParseCXXClassMemberDeclaration(CurAS); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1858 | RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 1859 | } else { |
| 1860 | SkipUntil(tok::r_brace, false, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1861 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1863 | // If attributes exist after class contents, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1864 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1865 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1866 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1867 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1868 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1869 | LBraceLoc, RBraceLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1870 | attrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1871 | |
| 1872 | // C++ 9.2p2: Within the class member-specification, the class is regarded as |
| 1873 | // complete within function bodies, default arguments, |
| 1874 | // exception-specifications, and constructor ctor-initializers (including |
| 1875 | // such things in nested classes). |
| 1876 | // |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1877 | // FIXME: Only function bodies and constructor ctor-initializers are |
| 1878 | // parsed correctly, fix the rest. |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1879 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1880 | // We are not inside a nested class. This class and its nested classes |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1881 | // are complete and we can parse the delayed portions of method |
| 1882 | // declarations and the lexed inline method definitions. |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 1883 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1884 | ParseLexedMethodDeclarations(getCurrentClass()); |
| 1885 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 1886 | PrevTokLocation = SavedPrevTokLocation; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1889 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1890 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 1891 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1892 | // Leave the class scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1893 | ParsingDef.Pop(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1894 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1895 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1896 | |
| 1897 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 1898 | /// which explicitly initializes the members or base classes of a |
| 1899 | /// class (C++ [class.base.init]). For example, the three initializers |
| 1900 | /// after the ':' in the Derived constructor below: |
| 1901 | /// |
| 1902 | /// @code |
| 1903 | /// class Base { }; |
| 1904 | /// class Derived : Base { |
| 1905 | /// int x; |
| 1906 | /// float f; |
| 1907 | /// public: |
| 1908 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 1909 | /// }; |
| 1910 | /// @endcode |
| 1911 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | /// [C++] ctor-initializer: |
| 1913 | /// ':' mem-initializer-list |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1914 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | /// [C++] mem-initializer-list: |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1916 | /// mem-initializer ...[opt] |
| 1917 | /// mem-initializer ...[opt] , mem-initializer-list |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1918 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1919 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 1920 | |
| 1921 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1922 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1923 | llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1924 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1925 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1926 | do { |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 1927 | if (Tok.is(tok::code_completion)) { |
| 1928 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 1929 | MemInitializers.data(), |
| 1930 | MemInitializers.size()); |
| 1931 | ConsumeCodeCompletionToken(); |
| 1932 | } else { |
| 1933 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 1934 | if (!MemInit.isInvalid()) |
| 1935 | MemInitializers.push_back(MemInit.get()); |
| 1936 | else |
| 1937 | AnyErrors = true; |
| 1938 | } |
| 1939 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1940 | if (Tok.is(tok::comma)) |
| 1941 | ConsumeToken(); |
| 1942 | else if (Tok.is(tok::l_brace)) |
| 1943 | break; |
Douglas Gregor | b1f6fa4 | 2010-09-07 14:35:10 +0000 | [diff] [blame] | 1944 | // If the next token looks like a base or member initializer, assume that |
| 1945 | // we're just missing a comma. |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 1946 | else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { |
| 1947 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 1948 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 1949 | << FixItHint::CreateInsertion(Loc, ", "); |
| 1950 | } else { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1951 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1952 | Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1953 | SkipUntil(tok::l_brace, true, true); |
| 1954 | break; |
| 1955 | } |
| 1956 | } while (true); |
| 1957 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1959 | MemInitializers.data(), MemInitializers.size(), |
| 1960 | AnyErrors); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 1964 | /// part of a constructor initializer that explicitly initializes one |
| 1965 | /// member or base class (C++ [class.base.init]). See |
| 1966 | /// ParseConstructorInitializer for an example. |
| 1967 | /// |
| 1968 | /// [C++] mem-initializer: |
| 1969 | /// mem-initializer-id '(' expression-list[opt] ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | /// |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1971 | /// [C++] mem-initializer-id: |
| 1972 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 1973 | /// identifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1974 | Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1975 | // parse '::'[opt] nested-name-specifier[opt] |
| 1976 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1977 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
| 1978 | ParsedType TemplateTypeTy; |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1979 | if (Tok.is(tok::annot_template_id)) { |
| 1980 | TemplateIdAnnotation *TemplateId |
| 1981 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 1982 | if (TemplateId->Kind == TNK_Type_template || |
| 1983 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1984 | AnnotateTemplateIdTokenAsType(); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1985 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1986 | TemplateTypeTy = getTypeAnnotation(Tok); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1987 | } |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1988 | } |
| 1989 | if (!TemplateTypeTy && Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1990 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1991 | return true; |
| 1992 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1994 | // Get the identifier. This may be a member name or a class name, |
| 1995 | // but we'll let the semantic analysis determine which it is. |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1996 | IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1997 | SourceLocation IdLoc = ConsumeToken(); |
| 1998 | |
| 1999 | // Parse the '('. |
| 2000 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2001 | Diag(Tok, diag::err_expected_lparen); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2002 | return true; |
| 2003 | } |
| 2004 | SourceLocation LParenLoc = ConsumeParen(); |
| 2005 | |
| 2006 | // Parse the optional expression-list. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 2007 | ExprVector ArgExprs(Actions); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2008 | CommaLocsTy CommaLocs; |
| 2009 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 2010 | SkipUntil(tok::r_paren); |
| 2011 | return true; |
| 2012 | } |
| 2013 | |
| 2014 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2015 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2016 | SourceLocation EllipsisLoc; |
| 2017 | if (Tok.is(tok::ellipsis)) |
| 2018 | EllipsisLoc = ConsumeToken(); |
| 2019 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2020 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2021 | TemplateTypeTy, IdLoc, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 2022 | LParenLoc, ArgExprs.take(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2023 | ArgExprs.size(), RParenLoc, |
| 2024 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2025 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2026 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2027 | /// \brief Parse a C++ exception-specification if present (C++0x [except.spec]). |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2028 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2029 | /// exception-specification: |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2030 | /// dynamic-exception-specification |
| 2031 | /// noexcept-specification |
| 2032 | /// |
| 2033 | /// noexcept-specification: |
| 2034 | /// 'noexcept' |
| 2035 | /// 'noexcept' '(' constant-expression ')' |
| 2036 | ExceptionSpecificationType |
| 2037 | Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange, |
| 2038 | llvm::SmallVectorImpl<ParsedType> &DynamicExceptions, |
| 2039 | llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
| 2040 | ExprResult &NoexceptExpr) { |
| 2041 | ExceptionSpecificationType Result = EST_None; |
| 2042 | |
| 2043 | // See if there's a dynamic specification. |
| 2044 | if (Tok.is(tok::kw_throw)) { |
| 2045 | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
| 2046 | DynamicExceptions, |
| 2047 | DynamicExceptionRanges); |
| 2048 | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
| 2049 | "Produced different number of exception types and ranges."); |
| 2050 | } |
| 2051 | |
| 2052 | // If there's no noexcept specification, we're done. |
| 2053 | if (Tok.isNot(tok::kw_noexcept)) |
| 2054 | return Result; |
| 2055 | |
| 2056 | // If we already had a dynamic specification, parse the noexcept for, |
| 2057 | // recovery, but emit a diagnostic and don't store the results. |
| 2058 | SourceRange NoexceptRange; |
| 2059 | ExceptionSpecificationType NoexceptType = EST_None; |
| 2060 | |
| 2061 | SourceLocation KeywordLoc = ConsumeToken(); |
| 2062 | if (Tok.is(tok::l_paren)) { |
| 2063 | // There is an argument. |
| 2064 | SourceLocation LParenLoc = ConsumeParen(); |
| 2065 | NoexceptType = EST_ComputedNoexcept; |
| 2066 | NoexceptExpr = ParseConstantExpression(); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2067 | // The argument must be contextually convertible to bool. We use |
| 2068 | // ActOnBooleanCondition for this purpose. |
| 2069 | if (!NoexceptExpr.isInvalid()) |
| 2070 | NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc, |
| 2071 | NoexceptExpr.get()); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2072 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2073 | NoexceptRange = SourceRange(KeywordLoc, RParenLoc); |
| 2074 | } else { |
| 2075 | // There is no argument. |
| 2076 | NoexceptType = EST_BasicNoexcept; |
| 2077 | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
| 2078 | } |
| 2079 | |
| 2080 | if (Result == EST_None) { |
| 2081 | SpecificationRange = NoexceptRange; |
| 2082 | Result = NoexceptType; |
| 2083 | |
| 2084 | // If there's a dynamic specification after a noexcept specification, |
| 2085 | // parse that and ignore the results. |
| 2086 | if (Tok.is(tok::kw_throw)) { |
| 2087 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2088 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
| 2089 | DynamicExceptionRanges); |
| 2090 | } |
| 2091 | } else { |
| 2092 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2093 | } |
| 2094 | |
| 2095 | return Result; |
| 2096 | } |
| 2097 | |
| 2098 | /// ParseDynamicExceptionSpecification - Parse a C++ |
| 2099 | /// dynamic-exception-specification (C++ [except.spec]). |
| 2100 | /// |
| 2101 | /// dynamic-exception-specification: |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2102 | /// 'throw' '(' type-id-list [opt] ')' |
| 2103 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2105 | /// type-id-list: |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2106 | /// type-id ... [opt] |
| 2107 | /// type-id-list ',' type-id ... [opt] |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2108 | /// |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2109 | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
| 2110 | SourceRange &SpecificationRange, |
| 2111 | llvm::SmallVectorImpl<ParsedType> &Exceptions, |
| 2112 | llvm::SmallVectorImpl<SourceRange> &Ranges) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2113 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2115 | SpecificationRange.setBegin(ConsumeToken()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2117 | if (!Tok.is(tok::l_paren)) { |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2118 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 2119 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2120 | return EST_DynamicNone; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2121 | } |
| 2122 | SourceLocation LParenLoc = ConsumeParen(); |
| 2123 | |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2124 | // Parse throw(...), a Microsoft extension that means "this function |
| 2125 | // can throw anything". |
| 2126 | if (Tok.is(tok::ellipsis)) { |
| 2127 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 2128 | if (!getLang().Microsoft) |
| 2129 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2130 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2131 | SpecificationRange.setEnd(RParenLoc); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2132 | return EST_MSAny; |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2135 | // Parse the sequence of type-ids. |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2136 | SourceRange Range; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2137 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2138 | TypeResult Res(ParseTypeName(&Range)); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2139 | |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2140 | if (Tok.is(tok::ellipsis)) { |
| 2141 | // C++0x [temp.variadic]p5: |
| 2142 | // - In a dynamic-exception-specification (15.4); the pattern is a |
| 2143 | // type-id. |
| 2144 | SourceLocation Ellipsis = ConsumeToken(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2145 | Range.setEnd(Ellipsis); |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2146 | if (!Res.isInvalid()) |
| 2147 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
| 2148 | } |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2149 | |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2150 | if (!Res.isInvalid()) { |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2151 | Exceptions.push_back(Res.get()); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2152 | Ranges.push_back(Range); |
| 2153 | } |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2154 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2155 | if (Tok.is(tok::comma)) |
| 2156 | ConsumeToken(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2157 | else |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2158 | break; |
| 2159 | } |
| 2160 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2161 | SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc)); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2162 | return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2163 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 2165 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 2166 | /// function declaration. |
| 2167 | TypeResult Parser::ParseTrailingReturnType() { |
| 2168 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 2169 | |
| 2170 | ConsumeToken(); |
| 2171 | |
| 2172 | // FIXME: Need to suppress declarations when parsing this typename. |
| 2173 | // Otherwise in this function definition: |
| 2174 | // |
| 2175 | // auto f() -> struct X {} |
| 2176 | // |
| 2177 | // struct X is parsed as class definition because of the trailing |
| 2178 | // brace. |
| 2179 | |
| 2180 | SourceRange Range; |
| 2181 | return ParseTypeName(&Range); |
| 2182 | } |
| 2183 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2184 | /// \brief We have just started parsing the definition of a new class, |
| 2185 | /// so push that class onto our stack of classes that is currently |
| 2186 | /// being parsed. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2187 | Sema::ParsingClassState |
| 2188 | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2189 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2190 | "Nested class without outer class"); |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2191 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass)); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2192 | return Actions.PushParsingClass(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | /// \brief Deallocate the given parsed class and all of its nested |
| 2196 | /// classes. |
| 2197 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2198 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 2199 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2200 | delete Class; |
| 2201 | } |
| 2202 | |
| 2203 | /// \brief Pop the top class of the stack of classes that are |
| 2204 | /// currently being parsed. |
| 2205 | /// |
| 2206 | /// This routine should be called when we have finished parsing the |
| 2207 | /// definition of a class, but have not yet popped the Scope |
| 2208 | /// associated with the class's definition. |
| 2209 | /// |
| 2210 | /// \returns true if the class we've popped is a top-level class, |
| 2211 | /// false otherwise. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2212 | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2213 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2215 | Actions.PopParsingClass(state); |
| 2216 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2217 | ParsingClass *Victim = ClassStack.top(); |
| 2218 | ClassStack.pop(); |
| 2219 | if (Victim->TopLevelClass) { |
| 2220 | // Deallocate all of the nested classes of this class, |
| 2221 | // recursively: we don't need to keep any of this information. |
| 2222 | DeallocateParsedClasses(Victim); |
| 2223 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2225 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 2226 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2227 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2228 | // The victim is a nested class, but we will not need to perform |
| 2229 | // any processing after the definition of this class since it has |
| 2230 | // no members whose handling was delayed. Therefore, we can just |
| 2231 | // remove this nested class. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2232 | DeallocateParsedClasses(Victim); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2233 | return; |
| 2234 | } |
| 2235 | |
| 2236 | // This nested class has some members that will need to be processed |
| 2237 | // after the top-level class is completely defined. Therefore, add |
| 2238 | // it to the list of nested classes within its parent. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2239 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2240 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2241 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2242 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2243 | |
| 2244 | /// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only |
| 2245 | /// parses standard attributes. |
| 2246 | /// |
| 2247 | /// [C++0x] attribute-specifier: |
| 2248 | /// '[' '[' attribute-list ']' ']' |
| 2249 | /// |
| 2250 | /// [C++0x] attribute-list: |
| 2251 | /// attribute[opt] |
| 2252 | /// attribute-list ',' attribute[opt] |
| 2253 | /// |
| 2254 | /// [C++0x] attribute: |
| 2255 | /// attribute-token attribute-argument-clause[opt] |
| 2256 | /// |
| 2257 | /// [C++0x] attribute-token: |
| 2258 | /// identifier |
| 2259 | /// attribute-scoped-token |
| 2260 | /// |
| 2261 | /// [C++0x] attribute-scoped-token: |
| 2262 | /// attribute-namespace '::' identifier |
| 2263 | /// |
| 2264 | /// [C++0x] attribute-namespace: |
| 2265 | /// identifier |
| 2266 | /// |
| 2267 | /// [C++0x] attribute-argument-clause: |
| 2268 | /// '(' balanced-token-seq ')' |
| 2269 | /// |
| 2270 | /// [C++0x] balanced-token-seq: |
| 2271 | /// balanced-token |
| 2272 | /// balanced-token-seq balanced-token |
| 2273 | /// |
| 2274 | /// [C++0x] balanced-token: |
| 2275 | /// '(' balanced-token-seq ')' |
| 2276 | /// '[' balanced-token-seq ']' |
| 2277 | /// '{' balanced-token-seq '}' |
| 2278 | /// any token but '(', ')', '[', ']', '{', or '}' |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2279 | void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs, |
| 2280 | SourceLocation *endLoc) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2281 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) |
| 2282 | && "Not a C++0x attribute list"); |
| 2283 | |
| 2284 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2285 | |
| 2286 | ConsumeBracket(); |
| 2287 | ConsumeBracket(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2288 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2289 | if (Tok.is(tok::comma)) { |
| 2290 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2291 | ConsumeToken(); |
| 2292 | } |
| 2293 | |
| 2294 | while (Tok.is(tok::identifier) || Tok.is(tok::comma)) { |
| 2295 | // attribute not present |
| 2296 | if (Tok.is(tok::comma)) { |
| 2297 | ConsumeToken(); |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
| 2301 | IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo(); |
| 2302 | SourceLocation ScopeLoc, AttrLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2303 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2304 | // scoped attribute |
| 2305 | if (Tok.is(tok::coloncolon)) { |
| 2306 | ConsumeToken(); |
| 2307 | |
| 2308 | if (!Tok.is(tok::identifier)) { |
| 2309 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2310 | SkipUntil(tok::r_square, tok::comma, true, true); |
| 2311 | continue; |
| 2312 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2313 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2314 | ScopeName = AttrName; |
| 2315 | ScopeLoc = AttrLoc; |
| 2316 | |
| 2317 | AttrName = Tok.getIdentifierInfo(); |
| 2318 | AttrLoc = ConsumeToken(); |
| 2319 | } |
| 2320 | |
| 2321 | bool AttrParsed = false; |
| 2322 | // No scoped names are supported; ideally we could put all non-standard |
| 2323 | // attributes into namespaces. |
| 2324 | if (!ScopeName) { |
| 2325 | switch(AttributeList::getKind(AttrName)) |
| 2326 | { |
| 2327 | // No arguments |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2328 | case AttributeList::AT_carries_dependency: |
Anders Carlsson | 15e14a2 | 2011-01-23 21:33:18 +0000 | [diff] [blame] | 2329 | case AttributeList::AT_noreturn: { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2330 | if (Tok.is(tok::l_paren)) { |
| 2331 | Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments) |
| 2332 | << AttrName->getName(); |
| 2333 | break; |
| 2334 | } |
| 2335 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2336 | attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0, |
| 2337 | SourceLocation(), 0, 0, false, true); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2338 | AttrParsed = true; |
| 2339 | break; |
| 2340 | } |
| 2341 | |
| 2342 | // One argument; must be a type-id or assignment-expression |
| 2343 | case AttributeList::AT_aligned: { |
| 2344 | if (Tok.isNot(tok::l_paren)) { |
| 2345 | Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments) |
| 2346 | << AttrName->getName(); |
| 2347 | break; |
| 2348 | } |
| 2349 | SourceLocation ParamLoc = ConsumeParen(); |
| 2350 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2351 | ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2352 | |
| 2353 | MatchRHSPunctuation(tok::r_paren, ParamLoc); |
| 2354 | |
| 2355 | ExprVector ArgExprs(Actions); |
| 2356 | ArgExprs.push_back(ArgExpr.release()); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2357 | attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, |
| 2358 | 0, ParamLoc, ArgExprs.take(), 1, |
| 2359 | false, true); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2360 | |
| 2361 | AttrParsed = true; |
| 2362 | break; |
| 2363 | } |
| 2364 | |
| 2365 | // Silence warnings |
| 2366 | default: break; |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | // Skip the entire parameter clause, if any |
| 2371 | if (!AttrParsed && Tok.is(tok::l_paren)) { |
| 2372 | ConsumeParen(); |
| 2373 | // SkipUntil maintains the balancedness of tokens. |
| 2374 | SkipUntil(tok::r_paren, false); |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2379 | SkipUntil(tok::r_square, false); |
| 2380 | Loc = Tok.getLocation(); |
| 2381 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2382 | SkipUntil(tok::r_square, false); |
| 2383 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2384 | attrs.Range = SourceRange(StartLoc, Loc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | /// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]] |
| 2388 | /// attribute. |
| 2389 | /// |
| 2390 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2391 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2392 | /// |
| 2393 | /// [C++0x] 'align' '(' type-id ')' |
| 2394 | /// [C++0x] 'align' '(' assignment-expression ')' |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2395 | ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2396 | if (isTypeIdInParens()) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2397 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2398 | SourceLocation TypeLoc = Tok.getLocation(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2399 | ParsedType Ty = ParseTypeName().get(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2400 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2401 | return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2402 | Ty.getAsOpaquePtr(), TypeRange); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2403 | } else |
| 2404 | return ParseConstantExpression(); |
| 2405 | } |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2406 | |
| 2407 | /// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr] |
| 2408 | /// |
| 2409 | /// [MS] ms-attribute: |
| 2410 | /// '[' token-seq ']' |
| 2411 | /// |
| 2412 | /// [MS] ms-attribute-seq: |
| 2413 | /// ms-attribute[opt] |
| 2414 | /// ms-attribute ms-attribute-seq |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2415 | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
| 2416 | SourceLocation *endLoc) { |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2417 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 2418 | |
| 2419 | while (Tok.is(tok::l_square)) { |
| 2420 | ConsumeBracket(); |
| 2421 | SkipUntil(tok::r_square, true, true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2422 | if (endLoc) *endLoc = Tok.getLocation(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2423 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); |
| 2424 | } |
| 2425 | } |