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 | |
Douglas Gregor | 1b7f898 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 14 | #include "clang/Parse/Parser.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 16 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/OperatorKinds.h" |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 19 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 20 | #include "clang/Sema/DeclSpec.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 21 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 22 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Scope.h" |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 24 | #include "clang/Sema/SemaDiagnostic.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 29 | /// may either be a top level namespace or a block-level namespace alias. If |
| 30 | /// there was an inline keyword, it has already been parsed. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 31 | /// |
| 32 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 33 | /// named-namespace-definition |
| 34 | /// unnamed-namespace-definition |
| 35 | /// |
| 36 | /// unnamed-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 37 | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 38 | /// |
| 39 | /// named-namespace-definition: |
| 40 | /// original-namespace-definition |
| 41 | /// extension-namespace-definition |
| 42 | /// |
| 43 | /// original-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 44 | /// 'inline'[opt] 'namespace' identifier attributes[opt] |
| 45 | /// '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 46 | /// |
| 47 | /// extension-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 48 | /// 'inline'[opt] 'namespace' original-namespace-name |
| 49 | /// '{' namespace-body '}' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | /// |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 51 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 52 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 53 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 54 | Decl *Parser::ParseNamespace(unsigned Context, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 55 | SourceLocation &DeclEnd, |
| 56 | SourceLocation InlineLoc) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 57 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 58 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 59 | ObjCDeclContextSwitch ObjCDC(*this); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 61 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 62 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 63 | cutOffParsing(); |
| 64 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 65 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 67 | SourceLocation IdentLoc; |
| 68 | IdentifierInfo *Ident = 0; |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 69 | std::vector<SourceLocation> ExtraIdentLoc; |
| 70 | std::vector<IdentifierInfo*> ExtraIdent; |
| 71 | std::vector<SourceLocation> ExtraNamespaceLoc; |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 72 | |
| 73 | Token attrTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 75 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 76 | Ident = Tok.getIdentifierInfo(); |
| 77 | IdentLoc = ConsumeToken(); // eat the identifier. |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 78 | while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) { |
| 79 | ExtraNamespaceLoc.push_back(ConsumeToken()); |
| 80 | ExtraIdent.push_back(Tok.getIdentifierInfo()); |
| 81 | ExtraIdentLoc.push_back(ConsumeToken()); |
| 82 | } |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 83 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 85 | // Read label attributes, if present. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 86 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 87 | if (Tok.is(tok::kw___attribute)) { |
| 88 | attrTok = Tok; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 89 | ParseGNUAttributes(attrs); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 90 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 92 | if (Tok.is(tok::equal)) { |
Nico Weber | e1bb329 | 2012-10-27 23:44:27 +0000 | [diff] [blame] | 93 | if (Ident == 0) { |
| 94 | Diag(Tok, diag::err_expected_ident); |
| 95 | // Skip to end of the definition and eat the ';'. |
| 96 | SkipUntil(tok::semi); |
| 97 | return 0; |
| 98 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 99 | if (!attrs.empty()) |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 100 | Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 101 | if (InlineLoc.isValid()) |
| 102 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
| 103 | << FixItHint::CreateRemoval(InlineLoc); |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 104 | return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 105 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 108 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 109 | if (T.consumeOpen()) { |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 110 | if (!ExtraIdent.empty()) { |
| 111 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 112 | << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back()); |
| 113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 115 | diag::err_expected_ident_lbrace); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 116 | return 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 119 | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
| 120 | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
| 121 | getCurScope()->getFnParent()) { |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 122 | if (!ExtraIdent.empty()) { |
| 123 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 124 | << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back()); |
| 125 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 126 | Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 127 | SkipUntil(tok::r_brace); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 128 | return 0; |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 131 | if (!ExtraIdent.empty()) { |
| 132 | TentativeParsingAction TPA(*this); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 133 | SkipUntil(tok::r_brace, StopBeforeMatch); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 134 | Token rBraceToken = Tok; |
| 135 | TPA.Revert(); |
| 136 | |
| 137 | if (!rBraceToken.is(tok::r_brace)) { |
| 138 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 139 | << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back()); |
| 140 | } else { |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 141 | std::string NamespaceFix; |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 142 | for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(), |
| 143 | E = ExtraIdent.end(); I != E; ++I) { |
| 144 | NamespaceFix += " { namespace "; |
| 145 | NamespaceFix += (*I)->getName(); |
| 146 | } |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 147 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 148 | std::string RBraces; |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 149 | for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i) |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 150 | RBraces += "} "; |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 151 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 152 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 153 | << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(), |
| 154 | ExtraIdentLoc.back()), |
| 155 | NamespaceFix) |
| 156 | << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces); |
| 157 | } |
| 158 | } |
| 159 | |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 160 | // If we're still good, complain about inline namespaces in non-C++0x now. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 161 | if (InlineLoc.isValid()) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 162 | Diag(InlineLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 163 | diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace); |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 165 | // Enter a scope for the namespace. |
| 166 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 167 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 168 | Decl *NamespcDecl = |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 169 | Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 170 | IdentLoc, Ident, T.getOpenLocation(), |
| 171 | attrs.getList()); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 172 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 173 | PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc, |
| 174 | "parsing namespace"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 176 | // Parse the contents of the namespace. This includes parsing recovery on |
| 177 | // any improperly nested namespaces. |
| 178 | ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 179 | InlineLoc, attrs, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 181 | // Leave the namespace scope. |
| 182 | NamespaceScope.Exit(); |
| 183 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 184 | DeclEnd = T.getCloseLocation(); |
| 185 | Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 186 | |
| 187 | return NamespcDecl; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 189 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 190 | /// ParseInnerNamespace - Parse the contents of a namespace. |
| 191 | void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc, |
| 192 | std::vector<IdentifierInfo*>& Ident, |
| 193 | std::vector<SourceLocation>& NamespaceLoc, |
| 194 | unsigned int index, SourceLocation& InlineLoc, |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 195 | ParsedAttributes& attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 196 | BalancedDelimiterTracker &Tracker) { |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 197 | if (index == Ident.size()) { |
| 198 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 199 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 200 | MaybeParseCXX11Attributes(attrs); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 201 | MaybeParseMicrosoftAttributes(attrs); |
| 202 | ParseExternalDeclaration(attrs); |
| 203 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 204 | |
| 205 | // The caller is what called check -- we are simply calling |
| 206 | // the close for it. |
| 207 | Tracker.consumeClose(); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 208 | |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // Parse improperly nested namespaces. |
| 213 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 214 | Decl *NamespcDecl = |
| 215 | Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(), |
| 216 | NamespaceLoc[index], IdentLoc[index], |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 217 | Ident[index], Tracker.getOpenLocation(), |
| 218 | attrs.getList()); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 219 | |
| 220 | ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 221 | attrs, Tracker); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 222 | |
| 223 | NamespaceScope.Exit(); |
| 224 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 225 | Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation()); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 228 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 229 | /// alias definition. |
| 230 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 231 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 232 | SourceLocation AliasLoc, |
| 233 | IdentifierInfo *Alias, |
| 234 | SourceLocation &DeclEnd) { |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 235 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 237 | ConsumeToken(); // eat the '='. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 241 | cutOffParsing(); |
| 242 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 243 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 244 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 245 | CXXScopeSpec SS; |
| 246 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 247 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 248 | |
| 249 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
| 250 | Diag(Tok, diag::err_expected_namespace_name); |
| 251 | // Skip to end of the definition and eat the ';'. |
| 252 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 253 | return 0; |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Parse identifier. |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 257 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 258 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 260 | // Eat the ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 261 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 262 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, |
| 263 | "", tok::semi); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 265 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 266 | SS, IdentLoc, Ident); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 269 | /// ParseLinkage - We know that the current token is a string_literal |
| 270 | /// and just before that, that extern was seen. |
| 271 | /// |
| 272 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 273 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 274 | /// 'extern' string-literal declaration |
| 275 | /// |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 276 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) { |
Douglas Gregor | c19923d | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 277 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 278 | SmallString<8> LangBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 279 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 280 | StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 281 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 282 | return 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 283 | |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 284 | // FIXME: This is incorrect: linkage-specifiers are parsed in translation |
| 285 | // phase 7, so string-literal concatenation is supposed to occur. |
| 286 | // extern "" "C" "" "+" "+" { } is legal. |
| 287 | if (Tok.hasUDSuffix()) |
| 288 | Diag(Tok, diag::err_invalid_string_udl); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 289 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 291 | ParseScope LinkageScope(this, Scope::DeclScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 292 | Decl *LinkageSpec |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 293 | = Actions.ActOnStartLinkageSpecification(getCurScope(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 294 | DS.getSourceRange().getBegin(), |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 295 | Loc, Lang, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 296 | Tok.is(tok::l_brace) ? Tok.getLocation() |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 297 | : SourceLocation()); |
| 298 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 299 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 300 | MaybeParseCXX11Attributes(attrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 301 | MaybeParseMicrosoftAttributes(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 302 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 303 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | f41e33c | 2011-05-01 16:25:54 +0000 | [diff] [blame] | 304 | // Reset the source range in DS, as the leading "extern" |
| 305 | // does not really belong to the inner declaration ... |
| 306 | DS.SetRangeStart(SourceLocation()); |
| 307 | DS.SetRangeEnd(SourceLocation()); |
| 308 | // ... but anyway remember that such an "extern" was seen. |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 309 | DS.setExternInLinkageSpec(true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 310 | ParseExternalDeclaration(attrs, &DS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 311 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 312 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 314 | |
Douglas Gregor | 63a0113 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 315 | DS.abort(); |
| 316 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 317 | ProhibitAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 319 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 320 | T.consumeOpen(); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 321 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 322 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 323 | MaybeParseCXX11Attributes(attrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 324 | MaybeParseMicrosoftAttributes(attrs); |
| 325 | ParseExternalDeclaration(attrs); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 328 | T.consumeClose(); |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 329 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 330 | T.getCloseLocation()); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 331 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 332 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 333 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 334 | /// using-directive. Assumes that current token is 'using'. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 335 | Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 336 | const ParsedTemplateInfo &TemplateInfo, |
| 337 | SourceLocation &DeclEnd, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 338 | ParsedAttributesWithRange &attrs, |
| 339 | Decl **OwnedType) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 340 | assert(Tok.is(tok::kw_using) && "Not using token"); |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 341 | ObjCDeclContextSwitch ObjCDC(*this); |
| 342 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 343 | // Eat 'using'. |
| 344 | SourceLocation UsingLoc = ConsumeToken(); |
| 345 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 346 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 347 | Actions.CodeCompleteUsing(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 348 | cutOffParsing(); |
| 349 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 350 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 351 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 352 | // 'using namespace' means this is a using-directive. |
| 353 | if (Tok.is(tok::kw_namespace)) { |
| 354 | // Template parameters are always an error here. |
| 355 | if (TemplateInfo.Kind) { |
| 356 | SourceRange R = TemplateInfo.getSourceRange(); |
| 357 | Diag(UsingLoc, diag::err_templated_using_directive) |
| 358 | << R << FixItHint::CreateRemoval(R); |
| 359 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 360 | |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 361 | return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 364 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 365 | |
| 366 | // Using declarations can't have attributes. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 367 | ProhibitAttributes(attrs); |
Chris Lattner | 2f27477 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 368 | |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 369 | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 370 | AS_none, OwnedType); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 374 | /// that current token is 'namespace' and 'using' was already parsed. |
| 375 | /// |
| 376 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 377 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 378 | /// namespace-name ; |
| 379 | /// [GNU] using-directive: |
| 380 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 381 | /// namespace-name attributes[opt] ; |
| 382 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 383 | Decl *Parser::ParseUsingDirective(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 384 | SourceLocation UsingLoc, |
| 385 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 386 | ParsedAttributes &attrs) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 387 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 388 | |
| 389 | // Eat 'namespace'. |
| 390 | SourceLocation NamespcLoc = ConsumeToken(); |
| 391 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 392 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 393 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 394 | cutOffParsing(); |
| 395 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 396 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 398 | CXXScopeSpec SS; |
| 399 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 400 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 402 | IdentifierInfo *NamespcName = 0; |
| 403 | SourceLocation IdentLoc = SourceLocation(); |
| 404 | |
| 405 | // Parse namespace-name. |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 406 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 407 | Diag(Tok, diag::err_expected_namespace_name); |
| 408 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 409 | SkipUntil(tok::semi); |
| 410 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 411 | return 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 414 | // Parse identifier. |
| 415 | NamespcName = Tok.getIdentifierInfo(); |
| 416 | IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 418 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 419 | bool GNUAttr = false; |
| 420 | if (Tok.is(tok::kw___attribute)) { |
| 421 | GNUAttr = true; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 422 | ParseGNUAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 425 | // Eat ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 426 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 427 | ExpectAndConsume(tok::semi, |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 428 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
| 429 | : diag::err_expected_semi_after_namespace_name, |
| 430 | "", tok::semi); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 432 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 433 | IdentLoc, NamespcName, attrs.getList()); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 436 | /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration. |
| 437 | /// Assumes that 'using' was already seen. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 438 | /// |
| 439 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 440 | /// 'using' 'typename'[opt] ::[opt] nested-name-specifier |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 441 | /// unqualified-id |
| 442 | /// 'using' :: unqualified-id |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 443 | /// |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 444 | /// alias-declaration: C++11 [dcl.dcl]p1 |
| 445 | /// 'using' identifier attribute-specifier-seq[opt] = type-id ; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 446 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 447 | Decl *Parser::ParseUsingDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 448 | const ParsedTemplateInfo &TemplateInfo, |
| 449 | SourceLocation UsingLoc, |
| 450 | SourceLocation &DeclEnd, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 451 | AccessSpecifier AS, |
| 452 | Decl **OwnedType) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 453 | CXXScopeSpec SS; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 454 | SourceLocation TypenameLoc; |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 455 | bool HasTypenameKeyword = false; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 456 | |
Richard Smith | 5eed7e0 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 457 | // Check for misplaced attributes before the identifier in an |
| 458 | // alias-declaration. |
| 459 | ParsedAttributesWithRange MisplacedAttrs(AttrFactory); |
| 460 | MaybeParseCXX11Attributes(MisplacedAttrs); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 461 | |
| 462 | // Ignore optional 'typename'. |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 463 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 464 | if (Tok.is(tok::kw_typename)) { |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 465 | TypenameLoc = ConsumeToken(); |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 466 | HasTypenameKeyword = true; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 467 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 468 | |
| 469 | // Parse nested-name-specifier. |
Richard Smith | 2db075b | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 470 | IdentifierInfo *LastII = 0; |
| 471 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false, |
| 472 | /*MayBePseudoDtor=*/0, /*IsTypename=*/false, |
| 473 | /*LastII=*/&LastII); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 474 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 475 | // Check nested-name specifier. |
| 476 | if (SS.isInvalid()) { |
| 477 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 478 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 479 | } |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 480 | |
Richard Smith | 2db075b | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 481 | SourceLocation TemplateKWLoc; |
| 482 | UnqualifiedId Name; |
| 483 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 484 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 485 | // destructor names and allow the action module to diagnose any semantic |
| 486 | // errors. |
Richard Smith | 2db075b | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 487 | // |
| 488 | // C++11 [class.qual]p2: |
| 489 | // [...] in a using-declaration that is a member-declaration, if the name |
| 490 | // specified after the nested-name-specifier is the same as the identifier |
| 491 | // or the simple-template-id's template-name in the last component of the |
| 492 | // nested-name-specifier, the name is [...] considered to name the |
| 493 | // constructor. |
| 494 | if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext && |
| 495 | Tok.is(tok::identifier) && NextToken().is(tok::semi) && |
| 496 | SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() && |
| 497 | !SS.getScopeRep()->getAsNamespace() && |
| 498 | !SS.getScopeRep()->getAsNamespaceAlias()) { |
| 499 | SourceLocation IdLoc = ConsumeToken(); |
| 500 | ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII); |
| 501 | Name.setConstructorName(Type, IdLoc, IdLoc); |
| 502 | } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false, |
| 503 | /*AllowDestructorName=*/ true, |
| 504 | /*AllowConstructorName=*/ true, ParsedType(), |
| 505 | TemplateKWLoc, Name)) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 506 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 507 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 508 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 509 | |
Richard Smith | 5eed7e0 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 510 | ParsedAttributesWithRange Attrs(AttrFactory); |
Richard Smith | df1cce5 | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 511 | MaybeParseGNUAttributes(Attrs); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 512 | MaybeParseCXX11Attributes(Attrs); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 513 | |
| 514 | // Maybe this is an alias-declaration. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 515 | TypeResult TypeAlias; |
Richard Smith | 5eed7e0 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 516 | bool IsAliasDecl = Tok.is(tok::equal); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 517 | if (IsAliasDecl) { |
Richard Smith | 5eed7e0 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 518 | // If we had any misplaced attributes from earlier, this is where they |
| 519 | // should have been written. |
| 520 | if (MisplacedAttrs.Range.isValid()) { |
| 521 | Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 522 | << FixItHint::CreateInsertionFromRange( |
| 523 | Tok.getLocation(), |
| 524 | CharSourceRange::getTokenRange(MisplacedAttrs.Range)) |
| 525 | << FixItHint::CreateRemoval(MisplacedAttrs.Range); |
| 526 | Attrs.takeAllFrom(MisplacedAttrs); |
| 527 | } |
| 528 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 529 | ConsumeToken(); |
| 530 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 531 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 532 | diag::warn_cxx98_compat_alias_declaration : |
| 533 | diag::ext_alias_declaration); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 534 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 535 | // Type alias templates cannot be specialized. |
| 536 | int SpecKind = -1; |
Richard Smith | 536e9c1 | 2011-05-05 22:36:10 +0000 | [diff] [blame] | 537 | if (TemplateInfo.Kind == ParsedTemplateInfo::Template && |
| 538 | Name.getKind() == UnqualifiedId::IK_TemplateId) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 539 | SpecKind = 0; |
| 540 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) |
| 541 | SpecKind = 1; |
| 542 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 543 | SpecKind = 2; |
| 544 | if (SpecKind != -1) { |
| 545 | SourceRange Range; |
| 546 | if (SpecKind == 0) |
| 547 | Range = SourceRange(Name.TemplateId->LAngleLoc, |
| 548 | Name.TemplateId->RAngleLoc); |
| 549 | else |
| 550 | Range = TemplateInfo.getSourceRange(); |
| 551 | Diag(Range.getBegin(), diag::err_alias_declaration_specialization) |
| 552 | << SpecKind << Range; |
| 553 | SkipUntil(tok::semi); |
| 554 | return 0; |
| 555 | } |
| 556 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 557 | // Name must be an identifier. |
| 558 | if (Name.getKind() != UnqualifiedId::IK_Identifier) { |
| 559 | Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier); |
| 560 | // No removal fixit: can't recover from this. |
| 561 | SkipUntil(tok::semi); |
| 562 | return 0; |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 563 | } else if (HasTypenameKeyword) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 564 | Diag(TypenameLoc, diag::err_alias_declaration_not_identifier) |
| 565 | << FixItHint::CreateRemoval(SourceRange(TypenameLoc, |
| 566 | SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc)); |
| 567 | else if (SS.isNotEmpty()) |
| 568 | Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier) |
| 569 | << FixItHint::CreateRemoval(SS.getRange()); |
| 570 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 571 | TypeAlias = ParseTypeName(0, TemplateInfo.Kind ? |
| 572 | Declarator::AliasTemplateContext : |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 573 | Declarator::AliasDeclContext, AS, OwnedType, |
| 574 | &Attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 575 | } else { |
| 576 | // C++11 attributes are not allowed on a using-declaration, but GNU ones |
| 577 | // are. |
Richard Smith | 5eed7e0 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 578 | ProhibitAttributes(MisplacedAttrs); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 579 | ProhibitAttributes(Attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 580 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 581 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 582 | MaybeParseGNUAttributes(Attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 585 | // Eat ';'. |
| 586 | DeclEnd = Tok.getLocation(); |
| 587 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 588 | !Attrs.empty() ? "attributes list" : |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 589 | IsAliasDecl ? "alias declaration" : "using declaration", |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 590 | tok::semi); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 591 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 592 | // Diagnose an attempt to declare a templated using-declaration. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 593 | // In C++11, alias-declarations can be templates: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 594 | // template <...> using id = type; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 595 | if (TemplateInfo.Kind && !IsAliasDecl) { |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 596 | SourceRange R = TemplateInfo.getSourceRange(); |
| 597 | Diag(UsingLoc, diag::err_templated_using_declaration) |
| 598 | << R << FixItHint::CreateRemoval(R); |
| 599 | |
| 600 | // Unfortunately, we have to bail out instead of recovering by |
| 601 | // ignoring the parameters, just in case the nested name specifier |
| 602 | // depends on the parameters. |
| 603 | return 0; |
| 604 | } |
| 605 | |
Douglas Gregor | 480b53c | 2011-09-26 14:30:28 +0000 | [diff] [blame] | 606 | // "typename" keyword is allowed for identifiers only, |
| 607 | // because it may be a type definition. |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 608 | if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) { |
Douglas Gregor | 480b53c | 2011-09-26 14:30:28 +0000 | [diff] [blame] | 609 | Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only) |
| 610 | << FixItHint::CreateRemoval(SourceRange(TypenameLoc)); |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 611 | // Proceed parsing, but reset the HasTypenameKeyword flag. |
| 612 | HasTypenameKeyword = false; |
Douglas Gregor | 480b53c | 2011-09-26 14:30:28 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 615 | if (IsAliasDecl) { |
| 616 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 617 | MultiTemplateParamsArg TemplateParamsArg( |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 618 | TemplateParams ? TemplateParams->data() : 0, |
| 619 | TemplateParams ? TemplateParams->size() : 0); |
| 620 | return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 621 | UsingLoc, Name, Attrs.getList(), |
| 622 | TypeAlias); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 623 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 624 | |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 625 | return Actions.ActOnUsingDeclaration(getCurScope(), AS, |
| 626 | /* HasUsingKeyword */ true, UsingLoc, |
| 627 | SS, Name, Attrs.getList(), |
| 628 | HasTypenameKeyword, TypenameLoc); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 631 | /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 632 | /// |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 633 | /// [C++0x] static_assert-declaration: |
| 634 | /// static_assert ( constant-expression , string-literal ) ; |
| 635 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 636 | /// [C11] static_assert-declaration: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 637 | /// _Static_assert ( constant-expression , string-literal ) ; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 638 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 639 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 640 | assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) && |
| 641 | "Not a static_assert declaration"); |
| 642 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 643 | if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 644 | Diag(Tok, diag::ext_c11_static_assert); |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 645 | if (Tok.is(tok::kw_static_assert)) |
| 646 | Diag(Tok, diag::warn_cxx98_compat_static_assert); |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 647 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 648 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 650 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 651 | if (T.consumeOpen()) { |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 652 | Diag(Tok, diag::err_expected_lparen); |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 653 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 654 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 655 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 657 | ExprResult AssertExpr(ParseConstantExpression()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 658 | if (AssertExpr.isInvalid()) { |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 659 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 660 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 661 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 663 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 664 | return 0; |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 665 | |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 666 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 667 | Diag(Tok, diag::err_expected_string_literal) |
| 668 | << /*Source='static_assert'*/1; |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 669 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 670 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 671 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 673 | ExprResult AssertMessage(ParseStringLiteralExpression()); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 674 | if (AssertMessage.isInvalid()) { |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 675 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 676 | return 0; |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 677 | } |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 679 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 681 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 682 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 683 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 684 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
| 685 | AssertExpr.take(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 686 | AssertMessage.take(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 687 | T.getCloseLocation()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 690 | /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier. |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 691 | /// |
| 692 | /// 'decltype' ( expression ) |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 693 | /// 'decltype' ( 'auto' ) [C++1y] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 694 | /// |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 695 | SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
| 696 | assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) |
| 697 | && "Not a decltype specifier"); |
| 698 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 699 | ExprResult Result; |
| 700 | SourceLocation StartLoc = Tok.getLocation(); |
| 701 | SourceLocation EndLoc; |
| 702 | |
| 703 | if (Tok.is(tok::annot_decltype)) { |
| 704 | Result = getExprAnnotation(Tok); |
| 705 | EndLoc = Tok.getAnnotationEndLoc(); |
| 706 | ConsumeToken(); |
| 707 | if (Result.isInvalid()) { |
| 708 | DS.SetTypeSpecError(); |
| 709 | return EndLoc; |
| 710 | } |
| 711 | } else { |
Richard Smith | c7b5543 | 2012-02-24 22:30:04 +0000 | [diff] [blame] | 712 | if (Tok.getIdentifierInfo()->isStr("decltype")) |
| 713 | Diag(Tok, diag::warn_cxx98_compat_decltype); |
Richard Smith | 39304fa | 2012-02-24 18:10:23 +0000 | [diff] [blame] | 714 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 715 | ConsumeToken(); |
| 716 | |
| 717 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 718 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 719 | "decltype", tok::r_paren)) { |
| 720 | DS.SetTypeSpecError(); |
| 721 | return T.getOpenLocation() == Tok.getLocation() ? |
| 722 | StartLoc : T.getOpenLocation(); |
| 723 | } |
| 724 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 725 | // Check for C++1y 'decltype(auto)'. |
| 726 | if (Tok.is(tok::kw_auto)) { |
| 727 | // No need to disambiguate here: an expression can't start with 'auto', |
| 728 | // because the typename-specifier in a function-style cast operation can't |
| 729 | // be 'auto'. |
| 730 | Diag(Tok.getLocation(), |
| 731 | getLangOpts().CPlusPlus1y |
| 732 | ? diag::warn_cxx11_compat_decltype_auto_type_specifier |
| 733 | : diag::ext_decltype_auto_type_specifier); |
| 734 | ConsumeToken(); |
| 735 | } else { |
| 736 | // Parse the expression |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 737 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 738 | // C++11 [dcl.type.simple]p4: |
| 739 | // The operand of the decltype specifier is an unevaluated operand. |
| 740 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 741 | 0, /*IsDecltype=*/true); |
| 742 | Result = ParseExpression(); |
| 743 | if (Result.isInvalid()) { |
| 744 | DS.SetTypeSpecError(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 745 | if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 746 | EndLoc = ConsumeParen(); |
Argyrios Kyrtzidis | 1e58469 | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 747 | } else { |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 748 | if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) { |
| 749 | // Backtrack to get the location of the last token before the semi. |
| 750 | PP.RevertCachedTokens(2); |
| 751 | ConsumeToken(); // the semi. |
| 752 | EndLoc = ConsumeAnyToken(); |
| 753 | assert(Tok.is(tok::semi)); |
| 754 | } else { |
| 755 | EndLoc = Tok.getLocation(); |
| 756 | } |
Argyrios Kyrtzidis | 1e58469 | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 757 | } |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 758 | return EndLoc; |
Argyrios Kyrtzidis | 1e58469 | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 759 | } |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 760 | |
| 761 | Result = Actions.ActOnDecltypeExpression(Result.take()); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | // Match the ')' |
| 765 | T.consumeClose(); |
| 766 | if (T.getCloseLocation().isInvalid()) { |
| 767 | DS.SetTypeSpecError(); |
| 768 | // FIXME: this should return the location of the last token |
| 769 | // that was consumed (by "consumeClose()") |
| 770 | return T.getCloseLocation(); |
| 771 | } |
| 772 | |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 773 | if (Result.isInvalid()) { |
| 774 | DS.SetTypeSpecError(); |
| 775 | return T.getCloseLocation(); |
| 776 | } |
| 777 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 778 | EndLoc = T.getCloseLocation(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 779 | } |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 780 | assert(!Result.isInvalid()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 782 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 783 | unsigned DiagID; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 784 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 785 | if (Result.get() |
| 786 | ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
| 787 | DiagID, Result.release()) |
| 788 | : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec, |
| 789 | DiagID)) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 790 | Diag(StartLoc, DiagID) << PrevSpec; |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 791 | DS.SetTypeSpecError(); |
| 792 | } |
| 793 | return EndLoc; |
| 794 | } |
| 795 | |
| 796 | void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS, |
| 797 | SourceLocation StartLoc, |
| 798 | SourceLocation EndLoc) { |
| 799 | // make sure we have a token we can turn into an annotation token |
| 800 | if (PP.isBacktrackEnabled()) |
| 801 | PP.RevertCachedTokens(1); |
| 802 | else |
| 803 | PP.EnterToken(Tok); |
| 804 | |
| 805 | Tok.setKind(tok::annot_decltype); |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 806 | setExprAnnotation(Tok, |
| 807 | DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() : |
| 808 | DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() : |
| 809 | ExprError()); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 810 | Tok.setAnnotationEndLoc(EndLoc); |
| 811 | Tok.setLocation(StartLoc); |
| 812 | PP.AnnotateCachedTokens(Tok); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 815 | void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) { |
| 816 | assert(Tok.is(tok::kw___underlying_type) && |
| 817 | "Not an underlying type specifier"); |
| 818 | |
| 819 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 820 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 821 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 822 | "__underlying_type", tok::r_paren)) { |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 823 | return; |
| 824 | } |
| 825 | |
| 826 | TypeResult Result = ParseTypeName(); |
| 827 | if (Result.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 828 | SkipUntil(tok::r_paren, StopAtSemi); |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
| 832 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 833 | T.consumeClose(); |
| 834 | if (T.getCloseLocation().isInvalid()) |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 835 | return; |
| 836 | |
| 837 | const char *PrevSpec = 0; |
| 838 | unsigned DiagID; |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 839 | if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec, |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 840 | DiagID, Result.release())) |
| 841 | Diag(StartLoc, DiagID) << PrevSpec; |
Enea Zaffanella | 2d77634 | 2013-07-06 18:54:58 +0000 | [diff] [blame] | 842 | DS.setTypeofParensRange(T.getRange()); |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 843 | } |
| 844 | |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 845 | /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a |
| 846 | /// class name or decltype-specifier. Note that we only check that the result |
| 847 | /// names a type; semantic analysis will need to verify that the type names a |
| 848 | /// class. The result is either a type or null, depending on whether a type |
| 849 | /// name was found. |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 850 | /// |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 851 | /// base-type-specifier: [C++11 class.derived] |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 852 | /// class-or-decltype |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 853 | /// class-or-decltype: [C++11 class.derived] |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 854 | /// nested-name-specifier[opt] class-name |
| 855 | /// decltype-specifier |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 856 | /// class-name: [C++ class.name] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 857 | /// identifier |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 858 | /// simple-template-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | /// |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 860 | /// In C++98, instead of base-type-specifier, we have: |
| 861 | /// |
| 862 | /// ::[opt] nested-name-specifier[opt] class-name |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 863 | Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc, |
| 864 | SourceLocation &EndLocation) { |
David Blaikie | 7fe3878 | 2011-10-25 18:46:41 +0000 | [diff] [blame] | 865 | // Ignore attempts to use typename |
| 866 | if (Tok.is(tok::kw_typename)) { |
| 867 | Diag(Tok, diag::err_expected_class_name_not_template) |
| 868 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 869 | ConsumeToken(); |
| 870 | } |
| 871 | |
David Blaikie | 152aa4b | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 872 | // Parse optional nested-name-specifier |
| 873 | CXXScopeSpec SS; |
| 874 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
| 875 | |
| 876 | BaseLoc = Tok.getLocation(); |
| 877 | |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 878 | // Parse decltype-specifier |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 879 | // tok == kw_decltype is just error recovery, it can only happen when SS |
| 880 | // isn't empty |
| 881 | if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) { |
David Blaikie | 152aa4b | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 882 | if (SS.isNotEmpty()) |
| 883 | Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype) |
| 884 | << FixItHint::CreateRemoval(SS.getRange()); |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 885 | // Fake up a Declarator to use with ActOnTypeName. |
| 886 | DeclSpec DS(AttrFactory); |
| 887 | |
David Blaikie | b577757 | 2011-12-08 04:53:15 +0000 | [diff] [blame] | 888 | EndLocation = ParseDecltypeSpecifier(DS); |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 889 | |
| 890 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 891 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 892 | } |
| 893 | |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 894 | // Check whether we have a template-id that names a type. |
| 895 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 896 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 897 | if (TemplateId->Kind == TNK_Type_template || |
| 898 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 899 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 900 | |
| 901 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 902 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 903 | EndLocation = Tok.getAnnotationEndLoc(); |
| 904 | ConsumeToken(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 905 | |
| 906 | if (Type) |
| 907 | return Type; |
| 908 | return true; |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | // Fall through to produce an error below. |
| 912 | } |
| 913 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 914 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 915 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 916 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 919 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 920 | SourceLocation IdLoc = ConsumeToken(); |
| 921 | |
| 922 | if (Tok.is(tok::less)) { |
| 923 | // It looks the user intended to write a template-id here, but the |
| 924 | // template-name was wrong. Try to fix that. |
| 925 | TemplateNameKind TNK = TNK_Type_template; |
| 926 | TemplateTy Template; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 927 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 928 | &SS, Template, TNK)) { |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 929 | Diag(IdLoc, diag::err_unknown_template_name) |
| 930 | << Id; |
| 931 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 932 | |
Serge Pavlov | 62f675c | 2013-08-10 05:54:47 +0000 | [diff] [blame] | 933 | if (!Template) { |
| 934 | TemplateArgList TemplateArgs; |
| 935 | SourceLocation LAngleLoc, RAngleLoc; |
| 936 | ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS, |
| 937 | true, LAngleLoc, TemplateArgs, RAngleLoc); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 938 | return true; |
Serge Pavlov | 62f675c | 2013-08-10 05:54:47 +0000 | [diff] [blame] | 939 | } |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 940 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 941 | // Form the template name |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 942 | UnqualifiedId TemplateName; |
| 943 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 944 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 945 | // Parse the full template-id, then turn it into a type. |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 946 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
| 947 | TemplateName, true)) |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 948 | return true; |
| 949 | if (TNK == TNK_Dependent_template_name) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 950 | AnnotateTemplateIdTokenAsType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 951 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 952 | // If we didn't end up with a typename token, there's nothing more we |
| 953 | // can do. |
| 954 | if (Tok.isNot(tok::annot_typename)) |
| 955 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 957 | // Retrieve the type from the annotation token, consume that token, and |
| 958 | // return. |
| 959 | EndLocation = Tok.getAnnotationEndLoc(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 960 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 961 | ConsumeToken(); |
| 962 | return Type; |
| 963 | } |
| 964 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 965 | // We have an identifier; check whether it is actually a type. |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 966 | IdentifierInfo *CorrectedII = 0; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 967 | ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 968 | false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 969 | /*IsCtorOrDtorName=*/false, |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 970 | /*NonTrivialTypeSourceInfo=*/true, |
| 971 | &CorrectedII); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 972 | if (!Type) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 973 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 974 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | // Consume the identifier. |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 978 | EndLocation = IdLoc; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 979 | |
| 980 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 981 | DeclSpec DS(AttrFactory); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 982 | DS.SetRangeStart(IdLoc); |
| 983 | DS.SetRangeEnd(EndLocation); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 984 | DS.getTypeSpecScope() = SS; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 985 | |
| 986 | const char *PrevSpec = 0; |
| 987 | unsigned DiagID; |
| 988 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type); |
| 989 | |
| 990 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 991 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 992 | } |
| 993 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 994 | void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) { |
| 995 | while (Tok.is(tok::kw___single_inheritance) || |
| 996 | Tok.is(tok::kw___multiple_inheritance) || |
| 997 | Tok.is(tok::kw___virtual_inheritance)) { |
| 998 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 999 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1000 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 1001 | AttributeList::AS_GNU); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1005 | /// Determine whether the following tokens are valid after a type-specifier |
| 1006 | /// which could be a standalone declaration. This will conservatively return |
| 1007 | /// true if there's any doubt, and is appropriate for insert-';' fixits. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1008 | bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1009 | // This switch enumerates the valid "follow" set for type-specifiers. |
| 1010 | switch (Tok.getKind()) { |
| 1011 | default: break; |
| 1012 | case tok::semi: // struct foo {...} ; |
| 1013 | case tok::star: // struct foo {...} * P; |
| 1014 | case tok::amp: // struct foo {...} & R = ... |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1015 | case tok::ampamp: // struct foo {...} && R = ... |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1016 | case tok::identifier: // struct foo {...} V ; |
| 1017 | case tok::r_paren: //(struct foo {...} ) {4} |
| 1018 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1019 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1020 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
| 1021 | case tok::l_paren: // struct foo {...} ( x); |
| 1022 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1023 | case tok::kw_operator: // struct foo operator ++() {...} |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1024 | return true; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1025 | case tok::colon: |
| 1026 | return CouldBeBitfield; // enum E { ... } : 2; |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1027 | // Type qualifiers |
| 1028 | case tok::kw_const: // struct foo {...} const x; |
| 1029 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1030 | case tok::kw_restrict: // struct foo {...} restrict x; |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1031 | // Function specifiers |
| 1032 | // Note, no 'explicit'. An explicit function must be either a conversion |
| 1033 | // operator or a constructor. Either way, it can't have a return type. |
| 1034 | case tok::kw_inline: // struct foo inline f(); |
| 1035 | case tok::kw_virtual: // struct foo virtual f(); |
| 1036 | case tok::kw_friend: // struct foo friend f(); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1037 | // Storage-class specifiers |
| 1038 | case tok::kw_static: // struct foo {...} static x; |
| 1039 | case tok::kw_extern: // struct foo {...} extern x; |
| 1040 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1041 | case tok::kw_register: // struct foo {...} register x; |
| 1042 | case tok::kw_auto: // struct foo {...} auto x; |
| 1043 | case tok::kw_mutable: // struct foo {...} mutable x; |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1044 | case tok::kw_thread_local: // struct foo {...} thread_local x; |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1045 | case tok::kw_constexpr: // struct foo {...} constexpr x; |
| 1046 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1047 | // can occur after class specifiers according to the grammar. However, |
| 1048 | // almost no one actually writes code like this. If we see one of these, |
| 1049 | // it is much more likely that someone missed a semi colon and the |
| 1050 | // type/storage class specifier we're seeing is part of the *next* |
| 1051 | // intended declaration, as in: |
| 1052 | // |
| 1053 | // struct foo { ... } |
| 1054 | // typedef int X; |
| 1055 | // |
| 1056 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1057 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1058 | // the same declaration of X. Because of this, we look ahead past this |
| 1059 | // token to see if it's a type specifier. If so, we know the code is |
| 1060 | // otherwise invalid, so we can produce the expected semi error. |
| 1061 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1062 | return true; |
| 1063 | break; |
| 1064 | case tok::r_brace: // struct bar { struct foo {...} } |
| 1065 | // Missing ';' at end of struct is accepted as an extension in C mode. |
| 1066 | if (!getLangOpts().CPlusPlus) |
| 1067 | return true; |
| 1068 | break; |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1069 | // C++11 attributes |
| 1070 | case tok::l_square: // enum E [[]] x |
| 1071 | // Note, no tok::kw_alignas here; alignas cannot appertain to a type. |
| 1072 | return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square); |
Richard Smith | 8338a9d | 2013-01-29 04:13:32 +0000 | [diff] [blame] | 1073 | case tok::greater: |
| 1074 | // template<class T = class X> |
| 1075 | return getLangOpts().CPlusPlus; |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1076 | } |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1080 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 1081 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 1082 | /// until we reach the start of a definition or see a token that |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1083 | /// cannot start a definition. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1084 | /// |
| 1085 | /// class-specifier: [C++ class] |
| 1086 | /// class-head '{' member-specification[opt] '}' |
| 1087 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 1088 | /// class-head: |
| 1089 | /// class-key identifier[opt] base-clause[opt] |
| 1090 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 1091 | /// class-key nested-name-specifier[opt] simple-template-id |
| 1092 | /// base-clause[opt] |
| 1093 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1095 | /// identifier base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1097 | /// simple-template-id base-clause[opt] |
| 1098 | /// class-key: |
| 1099 | /// 'class' |
| 1100 | /// 'struct' |
| 1101 | /// 'union' |
| 1102 | /// |
| 1103 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 1105 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 1106 | /// simple-template-id |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1107 | /// |
| 1108 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 1109 | /// together, subsume the C99 struct-or-union-specifier: |
| 1110 | /// |
| 1111 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 1112 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 1113 | /// struct-or-union identifier |
| 1114 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 1115 | /// '}' attributes[opt] |
| 1116 | /// [GNU] struct-or-union attributes[opt] identifier |
| 1117 | /// struct-or-union: |
| 1118 | /// 'struct' |
| 1119 | /// 'union' |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1120 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 1121 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1122 | const ParsedTemplateInfo &TemplateInfo, |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1123 | AccessSpecifier AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1124 | bool EnteringContext, DeclSpecContext DSC, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1125 | ParsedAttributesWithRange &Attributes) { |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1126 | DeclSpec::TST TagType; |
| 1127 | if (TagTokKind == tok::kw_struct) |
| 1128 | TagType = DeclSpec::TST_struct; |
| 1129 | else if (TagTokKind == tok::kw___interface) |
| 1130 | TagType = DeclSpec::TST_interface; |
| 1131 | else if (TagTokKind == tok::kw_class) |
| 1132 | TagType = DeclSpec::TST_class; |
| 1133 | else { |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1134 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 1135 | TagType = DeclSpec::TST_union; |
| 1136 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1137 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1138 | if (Tok.is(tok::code_completion)) { |
| 1139 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1140 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1141 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1142 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1143 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1144 | // C++03 [temp.explicit] 14.7.2/8: |
| 1145 | // The usual access checking rules do not apply to names used to specify |
| 1146 | // explicit instantiations. |
| 1147 | // |
| 1148 | // As an extension we do not perform access checking on the names used to |
| 1149 | // specify explicit specializations either. This is important to allow |
| 1150 | // specializing traits classes for private types. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1151 | // |
| 1152 | // Note that we don't suppress if this turns out to be an elaborated |
| 1153 | // type specifier. |
| 1154 | bool shouldDelayDiagsInTag = |
| 1155 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 1156 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 1157 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1158 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1159 | ParsedAttributesWithRange attrs(AttrFactory); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1160 | // If attributes exist after tag, parse them. |
Richard Smith | df1cce5 | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 1161 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1162 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1163 | // If declspecs exist after tag, parse them. |
John McCall | b1d397c | 2010-08-05 17:13:11 +0000 | [diff] [blame] | 1164 | while (Tok.is(tok::kw___declspec)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1165 | ParseMicrosoftDeclSpec(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1166 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1167 | // Parse inheritance specifiers. |
| 1168 | if (Tok.is(tok::kw___single_inheritance) || |
| 1169 | Tok.is(tok::kw___multiple_inheritance) || |
| 1170 | Tok.is(tok::kw___virtual_inheritance)) |
Richard Smith | df1cce5 | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 1171 | ParseMicrosoftInheritanceClassAttributes(attrs); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1172 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1173 | // If C++0x attributes exist here, parse them. |
| 1174 | // FIXME: Are we consistent with the ordering of parsing of different |
| 1175 | // styles of attributes? |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1176 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1178 | // Source location used by FIXIT to insert misplaced |
| 1179 | // C++11 attributes |
| 1180 | SourceLocation AttrFixitLoc = Tok.getLocation(); |
| 1181 | |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1182 | if (TagType == DeclSpec::TST_struct && |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1183 | !Tok.is(tok::identifier) && |
| 1184 | Tok.getIdentifierInfo() && |
| 1185 | (Tok.is(tok::kw___is_arithmetic) || |
| 1186 | Tok.is(tok::kw___is_convertible) || |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1187 | Tok.is(tok::kw___is_empty) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1188 | Tok.is(tok::kw___is_floating_point) || |
| 1189 | Tok.is(tok::kw___is_function) || |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1190 | Tok.is(tok::kw___is_fundamental) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1191 | Tok.is(tok::kw___is_integral) || |
| 1192 | Tok.is(tok::kw___is_member_function_pointer) || |
| 1193 | Tok.is(tok::kw___is_member_pointer) || |
| 1194 | Tok.is(tok::kw___is_pod) || |
| 1195 | Tok.is(tok::kw___is_pointer) || |
| 1196 | Tok.is(tok::kw___is_same) || |
Douglas Gregor | 877222e | 2011-04-29 01:38:03 +0000 | [diff] [blame] | 1197 | Tok.is(tok::kw___is_scalar) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1198 | Tok.is(tok::kw___is_signed) || |
| 1199 | Tok.is(tok::kw___is_unsigned) || |
| 1200 | Tok.is(tok::kw___is_void))) { |
Douglas Gregor | 6887614 | 2011-07-30 07:01:49 +0000 | [diff] [blame] | 1201 | // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1202 | // name of struct templates, but some are keywords in GCC >= 4.3 |
| 1203 | // and Clang. Therefore, when we see the token sequence "struct |
| 1204 | // X", make X into a normal identifier rather than a keyword, to |
| 1205 | // allow libstdc++ 4.2 and libc++ to work properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 1206 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 1207 | Tok.setKind(tok::identifier); |
| 1208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1210 | // Parse the (optional) nested-name-specifier. |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1211 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1212 | if (getLangOpts().CPlusPlus) { |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1213 | // "FOO : BAR" is not a potential typo for "FOO::BAR". |
| 1214 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1215 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1216 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1217 | DS.SetTypeSpecError(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1218 | if (SS.isSet()) |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1219 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
| 1220 | Diag(Tok, diag::err_expected_ident); |
| 1221 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1222 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1223 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 1224 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1225 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1226 | IdentifierInfo *Name = 0; |
| 1227 | SourceLocation NameLoc; |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1228 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1229 | if (Tok.is(tok::identifier)) { |
| 1230 | Name = Tok.getIdentifierInfo(); |
| 1231 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1232 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1233 | if (Tok.is(tok::less) && getLangOpts().CPlusPlus) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1234 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1235 | // Eat the template argument list and try to continue parsing this as |
| 1236 | // a class (or template thereof). |
| 1237 | TemplateArgList TemplateArgs; |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1238 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1239 | if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS, |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1240 | true, LAngleLoc, |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1241 | TemplateArgs, RAngleLoc)) { |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1242 | // We couldn't parse the template argument list at all, so don't |
| 1243 | // try to give any location information for the list. |
| 1244 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 1245 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1247 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1248 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 1249 | << (TagType == DeclSpec::TST_class? 0 |
| 1250 | : TagType == DeclSpec::TST_struct? 1 |
Richard Smith | 0464e09 | 2013-11-08 21:51:24 +0000 | [diff] [blame] | 1251 | : TagType == DeclSpec::TST_union? 2 |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1252 | : 3) |
| 1253 | << Name |
| 1254 | << SourceRange(LAngleLoc, RAngleLoc); |
| 1255 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1256 | // Strip off the last template parameter list if it was empty, since |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1257 | // we've removed its template argument list. |
| 1258 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
| 1259 | if (TemplateParams && TemplateParams->size() > 1) { |
| 1260 | TemplateParams->pop_back(); |
| 1261 | } else { |
| 1262 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1263 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1264 | = ParsedTemplateInfo::NonTemplate; |
| 1265 | } |
| 1266 | } else if (TemplateInfo.Kind |
| 1267 | == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1268 | // Pretend this is just a forward declaration. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1269 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1270 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1271 | = ParsedTemplateInfo::NonTemplate; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1272 | const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1273 | = SourceLocation(); |
| 1274 | const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc |
| 1275 | = SourceLocation(); |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1276 | } |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1277 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1278 | } else if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1279 | TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1280 | NameLoc = ConsumeToken(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1282 | if (TemplateId->Kind != TNK_Type_template && |
| 1283 | TemplateId->Kind != TNK_Dependent_template_name) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1284 | // The template-name in the simple-template-id refers to |
| 1285 | // something other than a class template. Give an appropriate |
| 1286 | // error message and skip to the ';'. |
| 1287 | SourceRange Range(NameLoc); |
| 1288 | if (SS.isNotEmpty()) |
| 1289 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1290 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1291 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
Richard Trieu | 6e91f4b | 2013-06-19 22:25:01 +0000 | [diff] [blame] | 1292 | << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1294 | DS.SetTypeSpecError(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1295 | SkipUntil(tok::semi, StopBeforeMatch); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1296 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1297 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1300 | // There are four options here. |
| 1301 | // - If we are in a trailing return type, this is always just a reference, |
| 1302 | // and we must not try to parse a definition. For instance, |
| 1303 | // [] () -> struct S { }; |
| 1304 | // does not define a type. |
| 1305 | // - If we have 'struct foo {...', 'struct foo :...', |
| 1306 | // 'struct foo final :' or 'struct foo final {', then this is a definition. |
| 1307 | // - If we have 'struct foo;', then this is either a forward declaration |
| 1308 | // or a friend declaration, which have to be treated differently. |
| 1309 | // - Otherwise we have something like 'struct foo xyz', a reference. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1310 | // |
| 1311 | // We also detect these erroneous cases to provide better diagnostic for |
| 1312 | // C++11 attributes parsing. |
| 1313 | // - attributes follow class name: |
| 1314 | // struct foo [[]] {}; |
| 1315 | // - attributes appear before or after 'final': |
| 1316 | // struct foo [[]] final [[]] {}; |
| 1317 | // |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1318 | // However, in type-specifier-seq's, things look like declarations but are |
| 1319 | // just references, e.g. |
| 1320 | // new struct s; |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1321 | // or |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1322 | // &T::operator struct s; |
| 1323 | // For these, DSC is DSC_type_specifier. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1324 | |
| 1325 | // If there are attributes after class name, parse them. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1326 | MaybeParseCXX11Attributes(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1327 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1328 | Sema::TagUseKind TUK; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1329 | if (DSC == DSC_trailing) |
| 1330 | TUK = Sema::TUK_Reference; |
| 1331 | else if (Tok.is(tok::l_brace) || |
| 1332 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1333 | (isCXX11FinalKeyword() && |
David Blaikie | 6f42669 | 2012-03-12 15:39:49 +0000 | [diff] [blame] | 1334 | (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1335 | if (DS.isFriendSpecified()) { |
| 1336 | // C++ [class.friend]p2: |
| 1337 | // A class shall not be defined in a friend declaration. |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1338 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1339 | << SourceRange(DS.getFriendSpecLoc()); |
| 1340 | |
| 1341 | // Skip everything up to the semicolon, so that this looks like a proper |
| 1342 | // friend class (or template thereof) declaration. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1343 | SkipUntil(tok::semi, StopBeforeMatch); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1344 | TUK = Sema::TUK_Friend; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1345 | } else { |
| 1346 | // Okay, this is a class definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1347 | TUK = Sema::TUK_Definition; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1348 | } |
Richard Smith | 150d853 | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1349 | } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) || |
| 1350 | NextToken().is(tok::kw_alignas))) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1351 | // We can't tell if this is a definition or reference |
| 1352 | // until we skipped the 'final' and C++11 attribute specifiers. |
| 1353 | TentativeParsingAction PA(*this); |
| 1354 | |
| 1355 | // Skip the 'final' keyword. |
| 1356 | ConsumeToken(); |
| 1357 | |
| 1358 | // Skip C++11 attribute specifiers. |
| 1359 | while (true) { |
| 1360 | if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) { |
| 1361 | ConsumeBracket(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1362 | if (!SkipUntil(tok::r_square, StopAtSemi)) |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1363 | break; |
Richard Smith | 150d853 | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1364 | } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1365 | ConsumeToken(); |
| 1366 | ConsumeParen(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1367 | if (!SkipUntil(tok::r_paren, StopAtSemi)) |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1368 | break; |
| 1369 | } else { |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | if (Tok.is(tok::l_brace) || Tok.is(tok::colon)) |
| 1375 | TUK = Sema::TUK_Definition; |
| 1376 | else |
| 1377 | TUK = Sema::TUK_Reference; |
| 1378 | |
| 1379 | PA.Revert(); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1380 | } else if (DSC != DSC_type_specifier && |
| 1381 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1382 | (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1383 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1384 | if (Tok.isNot(tok::semi)) { |
| 1385 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 1386 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1387 | DeclSpec::getSpecifierName(TagType)); |
| 1388 | PP.EnterToken(Tok); |
| 1389 | Tok.setKind(tok::semi); |
| 1390 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1391 | } else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1392 | TUK = Sema::TUK_Reference; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1393 | |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1394 | // Forbid misplaced attributes. In cases of a reference, we pass attributes |
| 1395 | // to caller to handle. |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1396 | if (TUK != Sema::TUK_Reference) { |
| 1397 | // If this is not a reference, then the only possible |
| 1398 | // valid place for C++11 attributes to appear here |
| 1399 | // is between class-key and class-name. If there are |
| 1400 | // any attributes after class-name, we try a fixit to move |
| 1401 | // them to the right place. |
| 1402 | SourceRange AttrRange = Attributes.Range; |
| 1403 | if (AttrRange.isValid()) { |
| 1404 | Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed) |
| 1405 | << AttrRange |
| 1406 | << FixItHint::CreateInsertionFromRange(AttrFixitLoc, |
| 1407 | CharSourceRange(AttrRange, true)) |
| 1408 | << FixItHint::CreateRemoval(AttrRange); |
| 1409 | |
| 1410 | // Recover by adding misplaced attributes to the attribute list |
| 1411 | // of the class so they can be applied on the class later. |
| 1412 | attrs.takeAllFrom(Attributes); |
| 1413 | } |
| 1414 | } |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1415 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1416 | // If this is an elaborated type specifier, and we delayed |
| 1417 | // diagnostics before, just merge them into the current pool. |
| 1418 | if (shouldDelayDiagsInTag) { |
| 1419 | diagsFromTag.done(); |
| 1420 | if (TUK == Sema::TUK_Reference) |
| 1421 | diagsFromTag.redelay(); |
| 1422 | } |
| 1423 | |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1424 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1425 | TUK != Sema::TUK_Definition)) { |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1426 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 1427 | // We have a declaration or reference to an anonymous class. |
| 1428 | Diag(StartLoc, diag::err_anon_type_definition) |
| 1429 | << DeclSpec::getSpecifierName(TagType); |
| 1430 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1431 | |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1432 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1433 | return; |
| 1434 | } |
| 1435 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1436 | // Create the tag portion of the class or class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1437 | DeclResult TagOrTempResult = true; // invalid |
| 1438 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1439 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1440 | bool Owned = false; |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1441 | if (TemplateId) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1442 | // Explicit specialization, class template partial specialization, |
| 1443 | // or explicit instantiation. |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1444 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1445 | TemplateId->NumArgs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1446 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1447 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1448 | // This is an explicit instantiation of a class template. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1449 | ProhibitAttributes(attrs); |
| 1450 | |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1451 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1452 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1453 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | TemplateInfo.TemplateLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1455 | TagType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | StartLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1457 | SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1458 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | TemplateId->TemplateNameLoc, |
| 1460 | TemplateId->LAngleLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1461 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1463 | attrs.getList()); |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1464 | |
| 1465 | // Friend template-ids are treated as references unless |
| 1466 | // they have template headers, in which case they're ill-formed |
| 1467 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 1468 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1469 | } else if (TUK == Sema::TUK_Reference || |
| 1470 | (TUK == Sema::TUK_Friend && |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1471 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1472 | ProhibitAttributes(attrs); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1473 | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1474 | TemplateId->SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1475 | TemplateId->TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1476 | TemplateId->Template, |
| 1477 | TemplateId->TemplateNameLoc, |
| 1478 | TemplateId->LAngleLoc, |
| 1479 | TemplateArgsPtr, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1480 | TemplateId->RAngleLoc); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1481 | } else { |
| 1482 | // This is an explicit specialization or a class template |
| 1483 | // partial specialization. |
| 1484 | TemplateParameterLists FakedParamLists; |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1485 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1486 | // This looks like an explicit instantiation, because we have |
| 1487 | // something like |
| 1488 | // |
| 1489 | // template class Foo<X> |
| 1490 | // |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1491 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1492 | // meant to be an explicit specialization, but the user forgot |
| 1493 | // the '<>' after 'template'. |
Richard Smith | 61dfea9 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1494 | // It this is friend declaration however, since it cannot have a |
| 1495 | // template header, it is most likely that the user meant to |
| 1496 | // remove the 'template' keyword. |
Larisse Voufo | 4985429 | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 1497 | assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) && |
Richard Smith | 61dfea9 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1498 | "Expected a definition here"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1499 | |
Richard Smith | 61dfea9 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1500 | if (TUK == Sema::TUK_Friend) { |
| 1501 | Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation); |
| 1502 | TemplateParams = 0; |
| 1503 | } else { |
| 1504 | SourceLocation LAngleLoc = |
| 1505 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1506 | Diag(TemplateId->TemplateNameLoc, |
| 1507 | diag::err_explicit_instantiation_with_definition) |
| 1508 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1509 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1510 | |
| 1511 | // Create a fake template parameter list that contains only |
| 1512 | // "template<>", so that we treat this construct as a class |
| 1513 | // template specialization. |
| 1514 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1515 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1516 | LAngleLoc)); |
| 1517 | TemplateParams = &FakedParamLists; |
| 1518 | } |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | // Build the class template specialization. |
| 1522 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1523 | = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 1524 | StartLoc, DS.getModulePrivateSpecLoc(), SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1525 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | TemplateId->TemplateNameLoc, |
| 1527 | TemplateId->LAngleLoc, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1528 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1530 | attrs.getList(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1531 | MultiTemplateParamsArg( |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1532 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 1533 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1534 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1535 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1536 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1537 | // Explicit instantiation of a member of a class template |
| 1538 | // specialization, e.g., |
| 1539 | // |
| 1540 | // template struct Outer<int>::Inner; |
| 1541 | // |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1542 | ProhibitAttributes(attrs); |
| 1543 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1544 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1545 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1546 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | TemplateInfo.TemplateLoc, |
| 1548 | TagType, StartLoc, SS, Name, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1549 | NameLoc, attrs.getList()); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1550 | } else if (TUK == Sema::TUK_Friend && |
| 1551 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1552 | ProhibitAttributes(attrs); |
| 1553 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1554 | TagOrTempResult = |
| 1555 | Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(), |
| 1556 | TagType, StartLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1557 | Name, NameLoc, attrs.getList(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1558 | MultiTemplateParamsArg( |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1559 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 1560 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1561 | } else { |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1562 | if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition) |
| 1563 | ProhibitAttributes(attrs); |
Richard Smith | 61dfea9 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1564 | |
Larisse Voufo | 7c64ef0 | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1565 | if (TUK == Sema::TUK_Definition && |
| 1566 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1567 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1568 | // recover by ignoring the 'template' keyword. |
| 1569 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1570 | << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
Larisse Voufo | 4985429 | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 1571 | TemplateParams = 0; |
Larisse Voufo | 7c64ef0 | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1572 | } |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1573 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1574 | bool IsDependent = false; |
| 1575 | |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 1576 | // Don't pass down template parameter lists if this is just a tag |
| 1577 | // reference. For example, we don't need the template parameters here: |
| 1578 | // template <class T> class A *makeA(T t); |
| 1579 | MultiTemplateParamsArg TParams; |
| 1580 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 1581 | TParams = |
| 1582 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 1583 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1584 | // Declaration or definition of a class type |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1585 | TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1586 | SS, Name, NameLoc, attrs.getList(), AS, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 1587 | DS.getModulePrivateSpecLoc(), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1588 | TParams, Owned, IsDependent, |
| 1589 | SourceLocation(), false, |
| 1590 | clang::TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1591 | |
| 1592 | // If ActOnTag said the type was dependent, try again with the |
| 1593 | // less common call. |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1594 | if (IsDependent) { |
| 1595 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1596 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1597 | SS, Name, StartLoc, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1598 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1599 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1600 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1601 | // If there is a body, parse it and inform the actions module. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1602 | if (TUK == Sema::TUK_Definition) { |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1603 | assert(Tok.is(tok::l_brace) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1604 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1605 | isCXX11FinalKeyword()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1606 | if (getLangOpts().CPlusPlus) |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1607 | ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType, |
| 1608 | TagOrTempResult.get()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1609 | else |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1610 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1613 | const char *PrevSpec = 0; |
| 1614 | unsigned DiagID; |
| 1615 | bool Result; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1616 | if (!TypeResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1617 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 1618 | NameLoc.isValid() ? NameLoc : StartLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1619 | PrevSpec, DiagID, TypeResult.get()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1620 | } else if (!TagOrTempResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1621 | Result = DS.SetTypeSpecType(TagType, StartLoc, |
| 1622 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 1623 | PrevSpec, DiagID, TagOrTempResult.get(), Owned); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1624 | } else { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1625 | DS.SetTypeSpecError(); |
Anders Carlsson | 66e9977 | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1626 | return; |
| 1627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1629 | if (Result) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1630 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1632 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 1633 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 1634 | // going to look at what comes after it to improve error recovery. If an |
| 1635 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 1636 | // the end of the declaration and recover that way. |
| 1637 | // |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1638 | // Also enforce C++ [temp]p3: |
| 1639 | // In a template-declaration which defines a class, no declarator |
| 1640 | // is permitted. |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1641 | if (TUK == Sema::TUK_Definition && |
| 1642 | (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) { |
Argyrios Kyrtzidis | 7d033b2 | 2012-12-17 20:10:43 +0000 | [diff] [blame] | 1643 | if (Tok.isNot(tok::semi)) { |
| 1644 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1645 | DeclSpec::getSpecifierName(TagType)); |
| 1646 | // Push this token back into the preprocessor and change our current token |
| 1647 | // to ';' so that the rest of the code recovers as though there were an |
| 1648 | // ';' after the definition. |
| 1649 | PP.EnterToken(Tok); |
| 1650 | Tok.setKind(tok::semi); |
| 1651 | } |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1652 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1656 | /// |
| 1657 | /// base-clause : [C++ class.derived] |
| 1658 | /// ':' base-specifier-list |
| 1659 | /// base-specifier-list: |
| 1660 | /// base-specifier '...'[opt] |
| 1661 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1662 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1663 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 1664 | ConsumeToken(); |
| 1665 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1666 | // Build up an array of parsed base specifiers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1667 | SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1668 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1669 | while (true) { |
| 1670 | // Parse a base-specifier. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1671 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1672 | if (Result.isInvalid()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1673 | // Skip the rest of this base specifier, up until the comma or |
| 1674 | // opening brace. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1675 | SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1676 | } else { |
| 1677 | // Add this to our array of base specifiers. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1678 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | // If the next token is a comma, consume it and keep reading |
| 1682 | // base-specifiers. |
| 1683 | if (Tok.isNot(tok::comma)) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1685 | // Consume the comma. |
| 1686 | ConsumeToken(); |
| 1687 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1688 | |
| 1689 | // Attach the base specifiers |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1690 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 1694 | /// one entry in the base class list of a class specifier, for example: |
| 1695 | /// class foo : public bar, virtual private baz { |
| 1696 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 1697 | /// |
| 1698 | /// base-specifier: [C++ class.derived] |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1699 | /// attribute-specifier-seq[opt] base-type-specifier |
| 1700 | /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt] |
| 1701 | /// base-type-specifier |
| 1702 | /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt] |
| 1703 | /// base-type-specifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1704 | Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1705 | bool IsVirtual = false; |
| 1706 | SourceLocation StartLoc = Tok.getLocation(); |
| 1707 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1708 | ParsedAttributesWithRange Attributes(AttrFactory); |
| 1709 | MaybeParseCXX11Attributes(Attributes); |
| 1710 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1711 | // Parse the 'virtual' keyword. |
| 1712 | if (Tok.is(tok::kw_virtual)) { |
| 1713 | ConsumeToken(); |
| 1714 | IsVirtual = true; |
| 1715 | } |
| 1716 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1717 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 1718 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1719 | // Parse an (optional) access specifier. |
| 1720 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1721 | if (Access != AS_none) |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1722 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1724 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 1725 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1726 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 1727 | // access specifier. |
| 1728 | if (Tok.is(tok::kw_virtual)) { |
| 1729 | SourceLocation VirtualLoc = ConsumeToken(); |
| 1730 | if (IsVirtual) { |
| 1731 | // Complain about duplicate 'virtual' |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1732 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1733 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | IsVirtual = true; |
| 1737 | } |
| 1738 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1739 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 1740 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1741 | // Parse the class-name. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1742 | SourceLocation EndLocation; |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1743 | SourceLocation BaseLoc; |
| 1744 | TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1745 | if (BaseType.isInvalid()) |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1746 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1748 | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
| 1749 | // actually part of the base-specifier-list grammar productions, but we |
| 1750 | // parse it here for convenience. |
| 1751 | SourceLocation EllipsisLoc; |
| 1752 | if (Tok.is(tok::ellipsis)) |
| 1753 | EllipsisLoc = ConsumeToken(); |
| 1754 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1756 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1758 | // Notify semantic analysis that we have parsed a complete |
| 1759 | // base-specifier. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1760 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual, |
| 1761 | Access, BaseType.get(), BaseLoc, |
| 1762 | EllipsisLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 1766 | /// a C++ access-specifier. |
| 1767 | /// |
| 1768 | /// access-specifier: [C++ class.derived] |
| 1769 | /// 'private' |
| 1770 | /// 'protected' |
| 1771 | /// 'public' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1773 | switch (Tok.getKind()) { |
| 1774 | default: return AS_none; |
| 1775 | case tok::kw_private: return AS_private; |
| 1776 | case tok::kw_protected: return AS_protected; |
| 1777 | case tok::kw_public: return AS_public; |
| 1778 | } |
| 1779 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1781 | /// \brief If the given declarator has any parts for which parsing has to be |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 1782 | /// delayed, e.g., default arguments, create a late-parsed method declaration |
| 1783 | /// record to handle the parsing at the end of the class definition. |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1784 | void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo, |
| 1785 | Decl *ThisDecl) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1786 | // We just declared a member function. If this member function |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 1787 | // has any default arguments, we'll need to parse them later. |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1788 | LateParsedMethodDeclaration *LateMethod = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | DeclaratorChunk::FunctionTypeInfo &FTI |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1790 | = DeclaratorInfo.getFunctionTypeInfo(); |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1791 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1792 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 1793 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 1794 | if (!LateMethod) { |
| 1795 | // Push this method onto the stack of late-parsed method |
| 1796 | // declarations. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1797 | LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
| 1798 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1799 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1800 | |
| 1801 | // Add all of the parameters prior to this one (they don't |
| 1802 | // have default arguments). |
| 1803 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 1804 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 1805 | LateMethod->DefaultArgs.push_back( |
Douglas Gregor | 8f8210c | 2010-03-02 01:29:43 +0000 | [diff] [blame] | 1806 | LateParsedDefaultArgument(FTI.ArgInfo[I].Param)); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1809 | // Add this parameter to the list of parameters (it may or may |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1810 | // not have a default argument). |
| 1811 | LateMethod->DefaultArgs.push_back( |
| 1812 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 1813 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 1814 | } |
| 1815 | } |
| 1816 | } |
| 1817 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1818 | /// isCXX11VirtSpecifier - Determine whether the given token is a C++11 |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1819 | /// virt-specifier. |
| 1820 | /// |
| 1821 | /// virt-specifier: |
| 1822 | /// override |
| 1823 | /// final |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1824 | VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1825 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1826 | return VirtSpecifiers::VS_None; |
| 1827 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1828 | if (Tok.is(tok::identifier)) { |
| 1829 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1830 | |
Anders Carlsson | 7eeb4ec | 2011-01-20 03:47:08 +0000 | [diff] [blame] | 1831 | // Initialize the contextual keywords. |
| 1832 | if (!Ident_final) { |
| 1833 | Ident_final = &PP.getIdentifierTable().get("final"); |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1834 | if (getLangOpts().MicrosoftExt) |
| 1835 | Ident_sealed = &PP.getIdentifierTable().get("sealed"); |
Anders Carlsson | 7eeb4ec | 2011-01-20 03:47:08 +0000 | [diff] [blame] | 1836 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1837 | } |
| 1838 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1839 | if (II == Ident_override) |
| 1840 | return VirtSpecifiers::VS_Override; |
| 1841 | |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1842 | if (II == Ident_sealed) |
| 1843 | return VirtSpecifiers::VS_Sealed; |
| 1844 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1845 | if (II == Ident_final) |
| 1846 | return VirtSpecifiers::VS_Final; |
| 1847 | } |
| 1848 | |
| 1849 | return VirtSpecifiers::VS_None; |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1852 | /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq. |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1853 | /// |
| 1854 | /// virt-specifier-seq: |
| 1855 | /// virt-specifier |
| 1856 | /// virt-specifier-seq virt-specifier |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1857 | void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS, |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1858 | bool IsInterface) { |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1859 | while (true) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1860 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1861 | if (Specifier == VirtSpecifiers::VS_None) |
| 1862 | return; |
| 1863 | |
| 1864 | // C++ [class.mem]p8: |
| 1865 | // 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] | 1866 | const char *PrevSpec = 0; |
Anders Carlsson | 46127a9 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 1867 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1868 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
| 1869 | << PrevSpec |
| 1870 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1871 | |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1872 | if (IsInterface && (Specifier == VirtSpecifiers::VS_Final || |
| 1873 | Specifier == VirtSpecifiers::VS_Sealed)) { |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1874 | Diag(Tok.getLocation(), diag::err_override_control_interface) |
| 1875 | << VirtSpecifiers::getSpecifierName(Specifier); |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1876 | } else if (Specifier == VirtSpecifiers::VS_Sealed) { |
| 1877 | Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword); |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1878 | } else { |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1879 | Diag(Tok.getLocation(), |
| 1880 | getLangOpts().CPlusPlus11 |
| 1881 | ? diag::warn_cxx98_compat_override_control_keyword |
| 1882 | : diag::ext_override_control_keyword) |
| 1883 | << VirtSpecifiers::getSpecifierName(Specifier); |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1884 | } |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1885 | ConsumeToken(); |
| 1886 | } |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1889 | /// isCXX11FinalKeyword - Determine whether the next token is a C++11 |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1890 | /// contextual 'final' keyword. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1891 | bool Parser::isCXX11FinalKeyword() const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1892 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1893 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1894 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1895 | if (!Tok.is(tok::identifier)) |
| 1896 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1897 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1898 | // Initialize the contextual keywords. |
| 1899 | if (!Ident_final) { |
| 1900 | Ident_final = &PP.getIdentifierTable().get("final"); |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1901 | if (getLangOpts().MicrosoftExt) |
| 1902 | Ident_sealed = &PP.getIdentifierTable().get("sealed"); |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1903 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1904 | } |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1905 | |
| 1906 | return Tok.getIdentifierInfo() == Ident_final || |
| 1907 | Tok.getIdentifierInfo() == Ident_sealed; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1910 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 1911 | /// |
| 1912 | /// member-declaration: |
| 1913 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 1914 | /// function-definition ';'[opt] |
| 1915 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 1916 | /// using-declaration [TODO] |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1917 | /// [C++0x] static_assert-declaration |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1918 | /// template-declaration |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1919 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1920 | /// |
| 1921 | /// member-declarator-list: |
| 1922 | /// member-declarator |
| 1923 | /// member-declarator-list ',' member-declarator |
| 1924 | /// |
| 1925 | /// member-declarator: |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1926 | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1927 | /// declarator constant-initializer[opt] |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1928 | /// [C++11] declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1929 | /// identifier[opt] ':' constant-expression |
| 1930 | /// |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1931 | /// virt-specifier-seq: |
| 1932 | /// virt-specifier |
| 1933 | /// virt-specifier-seq virt-specifier |
| 1934 | /// |
| 1935 | /// virt-specifier: |
| 1936 | /// override |
| 1937 | /// final |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1938 | /// [MS] sealed |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1939 | /// |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1940 | /// pure-specifier: |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1941 | /// '= 0' |
| 1942 | /// |
| 1943 | /// constant-initializer: |
| 1944 | /// '=' constant-expression |
| 1945 | /// |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1946 | void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1947 | AttributeList *AccessAttrs, |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1948 | const ParsedTemplateInfo &TemplateInfo, |
| 1949 | ParsingDeclRAIIObject *TemplateDiags) { |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1950 | if (Tok.is(tok::at)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1951 | if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs)) |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1952 | Diag(Tok, diag::err_at_defs_cxx); |
| 1953 | else |
| 1954 | Diag(Tok, diag::err_at_in_class); |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1956 | ConsumeToken(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1957 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1958 | return; |
| 1959 | } |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 1960 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1961 | // Access declarations. |
Richard Smith | 83a22ec | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 1962 | bool MalformedTypeSpec = false; |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1963 | if (!TemplateInfo.Kind && |
Richard Smith | 83a22ec | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 1964 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) { |
| 1965 | if (TryAnnotateCXXScopeToken()) |
| 1966 | MalformedTypeSpec = true; |
| 1967 | |
| 1968 | bool isAccessDecl; |
| 1969 | if (Tok.isNot(tok::annot_cxxscope)) |
| 1970 | isAccessDecl = false; |
| 1971 | else if (NextToken().is(tok::identifier)) |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1972 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 1973 | else |
| 1974 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 1975 | |
| 1976 | if (isAccessDecl) { |
| 1977 | // Collect the scope specifier token we annotated earlier. |
| 1978 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1979 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 1980 | /*EnteringContext=*/false); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1981 | |
| 1982 | // Try to parse an unqualified-id. |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1983 | SourceLocation TemplateKWLoc; |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1984 | UnqualifiedId Name; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1985 | if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), |
| 1986 | TemplateKWLoc, Name)) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1987 | SkipUntil(tok::semi); |
| 1988 | return; |
| 1989 | } |
| 1990 | |
| 1991 | // TODO: recover from mistakenly-qualified operator declarations. |
| 1992 | if (ExpectAndConsume(tok::semi, |
| 1993 | diag::err_expected_semi_after, |
| 1994 | "access declaration", |
| 1995 | tok::semi)) |
| 1996 | return; |
| 1997 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1998 | Actions.ActOnUsingDeclaration(getCurScope(), AS, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 1999 | /* HasUsingKeyword */ false, |
| 2000 | SourceLocation(), |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2001 | SS, Name, |
| 2002 | /* AttrList */ 0, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2003 | /* HasTypenameKeyword */ false, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2004 | SourceLocation()); |
| 2005 | return; |
| 2006 | } |
| 2007 | } |
| 2008 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 2009 | // static_assert-declaration |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 2010 | 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] | 2011 | // FIXME: Check for templates |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 2012 | SourceLocation DeclEnd; |
| 2013 | ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2017 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2019 | "Nested template improperly parsed?"); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 2020 | SourceLocation DeclEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2022 | AS, AccessAttrs); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2023 | return; |
| 2024 | } |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 2025 | |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2026 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 2027 | if (Tok.is(tok::kw___extension__)) { |
| 2028 | // __extension__ silences extension warnings in the subexpression. |
| 2029 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 2030 | ConsumeToken(); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2031 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs, |
| 2032 | TemplateInfo, TemplateDiags); |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2033 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 2035 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it |
| 2036 | // is a bitfield. |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2037 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2038 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2039 | ParsedAttributesWithRange attrs(AttrFactory); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2040 | ParsedAttributesWithRange FnAttrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2041 | // Optional C++11 attribute-specifier |
| 2042 | MaybeParseCXX11Attributes(attrs); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2043 | // We need to keep these attributes for future diagnostic |
| 2044 | // before they are taken over by declaration specifier. |
| 2045 | FnAttrs.addAll(attrs.getList()); |
| 2046 | FnAttrs.Range = attrs.Range; |
| 2047 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2048 | MaybeParseMicrosoftAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2049 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2050 | if (Tok.is(tok::kw_using)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2051 | ProhibitAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2053 | // Eat 'using'. |
| 2054 | SourceLocation UsingLoc = ConsumeToken(); |
| 2055 | |
| 2056 | if (Tok.is(tok::kw_namespace)) { |
| 2057 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2058 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2059 | } else { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2060 | SourceLocation DeclEnd; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2061 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 2062 | ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo, |
| 2063 | UsingLoc, DeclEnd, AS); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2064 | } |
| 2065 | return; |
| 2066 | } |
| 2067 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2068 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2069 | LateParsedAttrList CommonLateParsedAttrs; |
| 2070 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2071 | // decl-specifier-seq: |
| 2072 | // Parse the common declaration-specifiers piece. |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2073 | ParsingDeclSpec DS(*this, TemplateDiags); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2074 | DS.takeAttributesFrom(attrs); |
Richard Smith | 83a22ec | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2075 | if (MalformedTypeSpec) |
| 2076 | DS.SetTypeSpecError(); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2077 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class, |
| 2078 | &CommonLateParsedAttrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2079 | |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame^] | 2080 | // If we had a free-standing type definition with a missing semicolon, we |
| 2081 | // may get this far before the problem becomes obvious. |
| 2082 | if (DS.hasTagDefinition() && |
| 2083 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate && |
| 2084 | DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class, |
| 2085 | &CommonLateParsedAttrs)) |
| 2086 | return; |
| 2087 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2088 | MultiTemplateParamsArg TemplateParams( |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 2089 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, |
| 2090 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 2091 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2092 | if (Tok.is(tok::semi)) { |
| 2093 | ConsumeToken(); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2094 | |
| 2095 | if (DS.isFriendSpecified()) |
| 2096 | ProhibitAttributes(FnAttrs); |
| 2097 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2098 | Decl *TheDecl = |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 2099 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams); |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2100 | DS.complete(TheDecl); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2101 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2102 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2103 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2104 | ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2105 | VirtSpecifiers VS; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2106 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2107 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2108 | LateParsedAttrList LateParsedAttrs; |
| 2109 | |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2110 | SourceLocation EqualLoc; |
| 2111 | bool HasInitializer = false; |
| 2112 | ExprResult Init; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2113 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2114 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2115 | ColonProtectionRAIIObject X(*this); |
| 2116 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2117 | // Parse the first declarator. |
| 2118 | ParseDeclarator(DeclaratorInfo); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2119 | // Error parsing the declarator? |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 2120 | if (!DeclaratorInfo.hasName()) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2121 | // If so, skip until the semi-colon or a }. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2122 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2123 | if (Tok.is(tok::semi)) |
| 2124 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2125 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2128 | ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2129 | |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 2130 | // If attributes exist after the declarator, but before an '{', parse them. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2131 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 2132 | |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2133 | // MSVC permits pure specifier on inline functions declared at class scope. |
| 2134 | // Hence check for =0 before checking for function definition. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2135 | if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) && |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2136 | DeclaratorInfo.isFunctionDeclarator() && |
| 2137 | NextToken().is(tok::numeric_constant)) { |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2138 | EqualLoc = ConsumeToken(); |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2139 | Init = ParseInitializer(); |
| 2140 | if (Init.isInvalid()) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2141 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2142 | else |
| 2143 | HasInitializer = true; |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2146 | FunctionDefinitionKind DefinitionKind = FDK_Declaration; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2147 | // function-definition: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2148 | // |
| 2149 | // In C++11, a non-function declarator followed by an open brace is a |
| 2150 | // braced-init-list for an in-class member initialization, not an |
| 2151 | // erroneous function definition. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2152 | if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2153 | DefinitionKind = FDK_Definition; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2154 | } else if (DeclaratorInfo.isFunctionDeclarator()) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2155 | if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2156 | DefinitionKind = FDK_Definition; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2157 | } else if (Tok.is(tok::equal)) { |
| 2158 | const Token &KW = NextToken(); |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2159 | if (KW.is(tok::kw_default)) |
| 2160 | DefinitionKind = FDK_Defaulted; |
| 2161 | else if (KW.is(tok::kw_delete)) |
| 2162 | DefinitionKind = FDK_Deleted; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2166 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
| 2167 | // to a friend declaration, that declaration shall be a definition. |
| 2168 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 2169 | DefinitionKind != FDK_Definition && DS.isFriendSpecified()) { |
| 2170 | // Diagnose attributes that appear before decl specifier: |
| 2171 | // [[]] friend int foo(); |
| 2172 | ProhibitAttributes(FnAttrs); |
| 2173 | } |
| 2174 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2175 | if (DefinitionKind) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2176 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2177 | Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2178 | ConsumeBrace(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2179 | SkipUntil(tok::r_brace); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2180 | |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2181 | // Consume the optional ';' |
| 2182 | if (Tok.is(tok::semi)) |
| 2183 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2184 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2185 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2186 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2187 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2188 | Diag(DeclaratorInfo.getIdentifierLoc(), |
| 2189 | diag::err_function_declared_typedef); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2190 | |
Richard Smith | 6f9a445 | 2012-11-15 22:54:20 +0000 | [diff] [blame] | 2191 | // Recover by treating the 'typedef' as spurious. |
| 2192 | DS.ClearStorageClassSpecs(); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2195 | Decl *FunDecl = |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2196 | ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo, |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2197 | VS, DefinitionKind, Init); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2198 | |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2199 | if (FunDecl) { |
| 2200 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) { |
| 2201 | CommonLateParsedAttrs[i]->addDecl(FunDecl); |
| 2202 | } |
| 2203 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) { |
| 2204 | LateParsedAttrs[i]->addDecl(FunDecl); |
| 2205 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2206 | } |
| 2207 | LateParsedAttrs.clear(); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Consume the ';' - it's optional unless we have a delete or default |
Richard Trieu | 4b0e6f1 | 2012-05-16 19:04:59 +0000 | [diff] [blame] | 2210 | if (Tok.is(tok::semi)) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 2211 | ConsumeExtraSemi(AfterMemberFunctionDefinition); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2212 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2213 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2214 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | // member-declarator-list: |
| 2218 | // member-declarator |
| 2219 | // member-declarator-list ',' member-declarator |
| 2220 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2221 | SmallVector<Decl *, 8> DeclsInGroup; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2222 | ExprResult BitfieldSize; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2223 | bool ExpectSemi = true; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2224 | |
| 2225 | while (1) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2226 | // member-declarator: |
| 2227 | // declarator pure-specifier[opt] |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2228 | // declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2229 | // identifier[opt] ':' constant-expression |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2230 | if (Tok.is(tok::colon)) { |
| 2231 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2232 | BitfieldSize = ParseConstantExpression(); |
| 2233 | if (BitfieldSize.isInvalid()) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2234 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2235 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 2237 | // If a simple-asm-expr is present, parse it. |
| 2238 | if (Tok.is(tok::kw_asm)) { |
| 2239 | SourceLocation Loc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2240 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 2241 | if (AsmLabel.isInvalid()) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2242 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 2243 | |
| 2244 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
| 2245 | DeclaratorInfo.SetRangeEnd(Loc); |
| 2246 | } |
| 2247 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2248 | // If attributes exist after the declarator, parse them. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2249 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2250 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2251 | // FIXME: When g++ adds support for this, we'll need to check whether it |
| 2252 | // goes before or after the GNU attributes and __asm__. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2253 | ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2254 | |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2255 | InClassInitStyle HasInClassInit = ICIS_NoInit; |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2256 | if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2257 | if (BitfieldSize.get()) { |
| 2258 | Diag(Tok, diag::err_bitfield_member_init); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2259 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2260 | } else { |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2261 | HasInitializer = true; |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2262 | if (!DeclaratorInfo.isDeclarationOfFunction() && |
| 2263 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2264 | != DeclSpec::SCS_typedef) |
| 2265 | HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2269 | // 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] | 2270 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2271 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2272 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 2273 | NamedDecl *ThisDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2274 | if (DS.isFriendSpecified()) { |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2275 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
| 2276 | // to a friend declaration, that declaration shall be a definition. |
| 2277 | // |
| 2278 | // Diagnose attributes appear after friend member function declarator: |
| 2279 | // foo [[]] (); |
| 2280 | SmallVector<SourceRange, 4> Ranges; |
| 2281 | DeclaratorInfo.getCXX11AttributeRanges(Ranges); |
| 2282 | if (!Ranges.empty()) { |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 2283 | for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(), |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2284 | E = Ranges.end(); I != E; ++I) { |
| 2285 | Diag((*I).getBegin(), diag::err_attributes_not_allowed) |
| 2286 | << *I; |
| 2287 | } |
| 2288 | } |
| 2289 | |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 2290 | // TODO: handle initializers, bitfields, 'delete' |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2291 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2292 | TemplateParams); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2293 | } else { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2294 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2295 | DeclaratorInfo, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2296 | TemplateParams, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2297 | BitfieldSize.release(), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2298 | VS, HasInClassInit); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2299 | |
| 2300 | if (VarTemplateDecl *VT = |
| 2301 | ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0) |
| 2302 | // Re-direct this decl to refer to the templated decl so that we can |
| 2303 | // initialize it. |
| 2304 | ThisDecl = VT->getTemplatedDecl(); |
| 2305 | |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2306 | if (ThisDecl && AccessAttrs) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 2307 | Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2308 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2309 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2310 | // Handle the initializer. |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2311 | if (HasInClassInit != ICIS_NoInit && |
| 2312 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2313 | DeclSpec::SCS_static) { |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2314 | // The initializer was deferred; parse it and cache the tokens. |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2315 | Diag(Tok, getLangOpts().CPlusPlus11 |
| 2316 | ? diag::warn_cxx98_compat_nonstatic_member_init |
| 2317 | : diag::ext_nonstatic_member_init); |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2318 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2319 | if (DeclaratorInfo.isArrayOfUnknownBound()) { |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2320 | // C++11 [dcl.array]p3: An array bound may also be omitted when the |
| 2321 | // declarator is followed by an initializer. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2322 | // |
| 2323 | // A brace-or-equal-initializer for a member-declarator is not an |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2324 | // initializer in the grammar, so this is ill-formed. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2325 | Diag(Tok, diag::err_incomplete_array_member_init); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2326 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2327 | |
| 2328 | // Avoid later warnings about a class member of incomplete type. |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2329 | if (ThisDecl) |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2330 | ThisDecl->setInvalidDecl(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2331 | } else |
| 2332 | ParseCXXNonStaticMemberInitializer(ThisDecl); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2333 | } else if (HasInitializer) { |
| 2334 | // Normal initializer. |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2335 | if (!Init.isUsable()) |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2336 | Init = ParseCXXMemberInitializer( |
| 2337 | ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc); |
| 2338 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2339 | if (Init.isInvalid()) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2340 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2341 | else if (ThisDecl) |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2342 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(), |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 2343 | DS.containsPlaceholderType()); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2344 | } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2345 | // No initializer. |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 2346 | Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType()); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2348 | if (ThisDecl) { |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2349 | if (!ThisDecl->isInvalidDecl()) { |
| 2350 | // Set the Decl for any late parsed attributes |
| 2351 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) |
| 2352 | CommonLateParsedAttrs[i]->addDecl(ThisDecl); |
| 2353 | |
| 2354 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) |
| 2355 | LateParsedAttrs[i]->addDecl(ThisDecl); |
| 2356 | } |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2357 | Actions.FinalizeDeclaration(ThisDecl); |
| 2358 | DeclsInGroup.push_back(ThisDecl); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2359 | |
| 2360 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 2361 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2362 | DeclSpec::SCS_typedef) |
| 2363 | HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2364 | } |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2365 | LateParsedAttrs.clear(); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2366 | |
| 2367 | DeclaratorInfo.complete(ThisDecl); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2368 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2369 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2370 | // or an error, bail out. |
| 2371 | if (Tok.isNot(tok::comma)) |
| 2372 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2374 | // Consume the comma. |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2375 | SourceLocation CommaLoc = ConsumeToken(); |
| 2376 | |
| 2377 | if (Tok.isAtStartOfLine() && |
| 2378 | !MightBeDeclarator(Declarator::MemberContext)) { |
| 2379 | // This comma was followed by a line-break and something which can't be |
| 2380 | // the start of a declarator. The comma was probably a typo for a |
| 2381 | // semicolon. |
| 2382 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2383 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2384 | ExpectSemi = false; |
| 2385 | break; |
| 2386 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2388 | // Parse the next declarator. |
| 2389 | DeclaratorInfo.clear(); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2390 | VS.clear(); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2391 | BitfieldSize = true; |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2392 | Init = true; |
| 2393 | HasInitializer = false; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2394 | DeclaratorInfo.setCommaLoc(CommaLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2396 | // Attributes are only allowed on the second declarator. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2397 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2398 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2399 | if (Tok.isNot(tok::colon)) |
| 2400 | ParseDeclarator(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2403 | if (ExpectSemi && |
| 2404 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2405 | // Skip to end of block or statement. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2406 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2407 | // If we stopped at a ';', eat it. |
| 2408 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2409 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2412 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2415 | /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or |
| 2416 | /// pure-specifier. Also detect and reject any attempted defaulted/deleted |
| 2417 | /// function definition. The location of the '=', if any, will be placed in |
| 2418 | /// EqualLoc. |
| 2419 | /// |
| 2420 | /// pure-specifier: |
| 2421 | /// '= 0' |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2422 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2423 | /// brace-or-equal-initializer: |
| 2424 | /// '=' initializer-expression |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2425 | /// braced-init-list |
| 2426 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2427 | /// initializer-clause: |
| 2428 | /// assignment-expression |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2429 | /// braced-init-list |
| 2430 | /// |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2431 | /// defaulted/deleted function-definition: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2432 | /// '=' 'default' |
| 2433 | /// '=' 'delete' |
| 2434 | /// |
| 2435 | /// Prior to C++0x, the assignment-expression in an initializer-clause must |
| 2436 | /// be a constant-expression. |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2437 | ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2438 | SourceLocation &EqualLoc) { |
| 2439 | assert((Tok.is(tok::equal) || Tok.is(tok::l_brace)) |
| 2440 | && "Data member initializer not starting with '=' or '{'"); |
| 2441 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2442 | EnterExpressionEvaluationContext Context(Actions, |
| 2443 | Sema::PotentiallyEvaluated, |
| 2444 | D); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2445 | if (Tok.is(tok::equal)) { |
| 2446 | EqualLoc = ConsumeToken(); |
| 2447 | if (Tok.is(tok::kw_delete)) { |
| 2448 | // In principle, an initializer of '= delete p;' is legal, but it will |
| 2449 | // never type-check. It's better to diagnose it as an ill-formed expression |
| 2450 | // than as an ill-formed deleted non-function member. |
| 2451 | // An initializer of '= delete p, foo' will never be parsed, because |
| 2452 | // a top-level comma always ends the initializer expression. |
| 2453 | const Token &Next = NextToken(); |
| 2454 | if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) || |
| 2455 | Next.is(tok::eof)) { |
| 2456 | if (IsFunction) |
| 2457 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2458 | << 1 /* delete */; |
| 2459 | else |
| 2460 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
| 2461 | return ExprResult(); |
| 2462 | } |
| 2463 | } else if (Tok.is(tok::kw_default)) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2464 | if (IsFunction) |
| 2465 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
| 2466 | << 0 /* default */; |
| 2467 | else |
| 2468 | Diag(ConsumeToken(), diag::err_default_special_members); |
| 2469 | return ExprResult(); |
| 2470 | } |
| 2471 | |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2472 | } |
| 2473 | return ParseInitializer(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2476 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 2477 | /// |
| 2478 | /// member-specification: |
| 2479 | /// member-declaration member-specification[opt] |
| 2480 | /// access-specifier ':' member-specification[opt] |
| 2481 | /// |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 2482 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 2483 | SourceLocation AttrFixitLoc, |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2484 | ParsedAttributesWithRange &Attrs, |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 2485 | unsigned TagType, Decl *TagDecl) { |
| 2486 | assert((TagType == DeclSpec::TST_struct || |
| 2487 | TagType == DeclSpec::TST_interface || |
| 2488 | TagType == DeclSpec::TST_union || |
| 2489 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
| 2490 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2491 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2492 | "parsing struct/union/class body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2494 | // Determine whether this is a non-nested class. Note that local |
| 2495 | // classes are *not* considered to be nested classes. |
| 2496 | bool NonNestedClass = true; |
| 2497 | if (!ClassStack.empty()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2498 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2499 | if (S->isClassScope()) { |
| 2500 | // We're inside a class scope, so this is a nested class. |
| 2501 | NonNestedClass = false; |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2502 | |
| 2503 | // The Microsoft extension __interface does not permit nested classes. |
| 2504 | if (getCurrentClass().IsInterface) { |
| 2505 | Diag(RecordLoc, diag::err_invalid_member_in_interface) |
| 2506 | << /*ErrorType=*/6 |
| 2507 | << (isa<NamedDecl>(TagDecl) |
| 2508 | ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString() |
| 2509 | : "<anonymous>"); |
| 2510 | } |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2511 | break; |
| 2512 | } |
| 2513 | |
| 2514 | if ((S->getFlags() & Scope::FnScope)) { |
| 2515 | // If we're in a function or function template declared in the |
| 2516 | // body of a class, then this is a local class rather than a |
| 2517 | // nested class. |
| 2518 | const Scope *Parent = S->getParent(); |
| 2519 | if (Parent->isTemplateParamScope()) |
| 2520 | Parent = Parent->getParent(); |
| 2521 | if (Parent->isClassScope()) |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2526 | |
| 2527 | // Enter a scope for the class. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2528 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2529 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2530 | // Note that we are parsing a new (potentially-nested) class definition. |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2531 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass, |
| 2532 | TagType == DeclSpec::TST_interface); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2533 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2534 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2535 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2536 | |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2537 | SourceLocation FinalLoc; |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2538 | bool IsFinalSpelledSealed = false; |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2539 | |
| 2540 | // Parse the optional 'final' keyword. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2541 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2542 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok); |
| 2543 | assert((Specifier == VirtSpecifiers::VS_Final || |
| 2544 | Specifier == VirtSpecifiers::VS_Sealed) && |
| 2545 | "not a class definition"); |
Richard Smith | 8b11b5e | 2011-10-15 04:21:46 +0000 | [diff] [blame] | 2546 | FinalLoc = ConsumeToken(); |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2547 | IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed; |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2548 | |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2549 | if (TagType == DeclSpec::TST_interface) |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2550 | Diag(FinalLoc, diag::err_override_control_interface) |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2551 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 2552 | else if (Specifier == VirtSpecifiers::VS_Final) |
| 2553 | Diag(FinalLoc, getLangOpts().CPlusPlus11 |
| 2554 | ? diag::warn_cxx98_compat_override_control_keyword |
| 2555 | : diag::ext_override_control_keyword) |
| 2556 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 2557 | else if (Specifier == VirtSpecifiers::VS_Sealed) |
| 2558 | Diag(FinalLoc, diag::ext_ms_sealed_keyword); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2559 | |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 2560 | // Parse any C++11 attributes after 'final' keyword. |
| 2561 | // These attributes are not allowed to appear here, |
| 2562 | // and the only possible place for them to appertain |
| 2563 | // to the class would be between class-key and class-name. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2564 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2565 | } |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 2566 | |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2567 | if (Tok.is(tok::colon)) { |
| 2568 | ParseBaseClause(TagDecl); |
| 2569 | |
| 2570 | if (!Tok.is(tok::l_brace)) { |
| 2571 | Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 2572 | |
| 2573 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2574 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2575 | return; |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | assert(Tok.is(tok::l_brace)); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2580 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2581 | T.consumeOpen(); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2582 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2583 | if (TagDecl) |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 2584 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2585 | IsFinalSpelledSealed, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2586 | T.getOpenLocation()); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 2587 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2588 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 2589 | // by default. Members of a class defined with the keywords struct or union |
| 2590 | // are public by default. |
| 2591 | AccessSpecifier CurAS; |
| 2592 | if (TagType == DeclSpec::TST_class) |
| 2593 | CurAS = AS_private; |
| 2594 | else |
| 2595 | CurAS = AS_public; |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2596 | ParsedAttributes AccessAttrs(AttrFactory); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2597 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2598 | if (TagDecl) { |
| 2599 | // While we still have something to read, read the member-declarations. |
| 2600 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 2601 | // Each iteration of this loop reads one member-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2602 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2603 | if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2604 | Tok.is(tok::kw___if_not_exists))) { |
| 2605 | ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS); |
| 2606 | continue; |
| 2607 | } |
| 2608 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2609 | // Check for extraneous top-level semicolon. |
| 2610 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 2611 | ConsumeExtraSemi(InsideStruct, TagType); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2612 | continue; |
| 2613 | } |
| 2614 | |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 2615 | if (Tok.is(tok::annot_pragma_vis)) { |
| 2616 | HandlePragmaVisibility(); |
| 2617 | continue; |
| 2618 | } |
| 2619 | |
| 2620 | if (Tok.is(tok::annot_pragma_pack)) { |
| 2621 | HandlePragmaPack(); |
| 2622 | continue; |
| 2623 | } |
| 2624 | |
Argyrios Kyrtzidis | f4deaef | 2012-10-12 17:39:59 +0000 | [diff] [blame] | 2625 | if (Tok.is(tok::annot_pragma_align)) { |
| 2626 | HandlePragmaAlign(); |
| 2627 | continue; |
| 2628 | } |
| 2629 | |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2630 | if (Tok.is(tok::annot_pragma_openmp)) { |
| 2631 | ParseOpenMPDeclarativeDirective(); |
| 2632 | continue; |
| 2633 | } |
| 2634 | |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2635 | // If we see a namespace here, a close brace was missing somewhere. |
| 2636 | if (Tok.is(tok::kw_namespace)) { |
Richard Smith | 7faf81f | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 2637 | DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl)); |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2638 | break; |
| 2639 | } |
| 2640 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2641 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 2642 | if (AS != AS_none) { |
| 2643 | // Current token is a C++ access specifier. |
| 2644 | CurAS = AS; |
| 2645 | SourceLocation ASLoc = Tok.getLocation(); |
David Blaikie | 13f8daf | 2011-10-13 06:08:43 +0000 | [diff] [blame] | 2646 | unsigned TokLength = Tok.getLength(); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2647 | ConsumeToken(); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2648 | AccessAttrs.clear(); |
| 2649 | MaybeParseGNUAttributes(AccessAttrs); |
| 2650 | |
David Blaikie | 13f8daf | 2011-10-13 06:08:43 +0000 | [diff] [blame] | 2651 | SourceLocation EndLoc; |
| 2652 | if (Tok.is(tok::colon)) { |
| 2653 | EndLoc = Tok.getLocation(); |
| 2654 | ConsumeToken(); |
| 2655 | } else if (Tok.is(tok::semi)) { |
| 2656 | EndLoc = Tok.getLocation(); |
| 2657 | ConsumeToken(); |
| 2658 | Diag(EndLoc, diag::err_expected_colon) |
| 2659 | << FixItHint::CreateReplacement(EndLoc, ":"); |
| 2660 | } else { |
| 2661 | EndLoc = ASLoc.getLocWithOffset(TokLength); |
| 2662 | Diag(EndLoc, diag::err_expected_colon) |
| 2663 | << FixItHint::CreateInsertion(EndLoc, ":"); |
| 2664 | } |
Erik Verbruggen | c35cba4 | 2011-10-17 09:54:52 +0000 | [diff] [blame] | 2665 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2666 | // The Microsoft extension __interface does not permit non-public |
| 2667 | // access specifiers. |
| 2668 | if (TagType == DeclSpec::TST_interface && CurAS != AS_public) { |
| 2669 | Diag(ASLoc, diag::err_access_specifier_interface) |
| 2670 | << (CurAS == AS_protected); |
| 2671 | } |
| 2672 | |
Erik Verbruggen | c35cba4 | 2011-10-17 09:54:52 +0000 | [diff] [blame] | 2673 | if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc, |
| 2674 | AccessAttrs.getList())) { |
| 2675 | // found another attribute than only annotations |
| 2676 | AccessAttrs.clear(); |
| 2677 | } |
| 2678 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2679 | continue; |
| 2680 | } |
| 2681 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2682 | // Parse all the comma separated declarators. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2683 | ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2686 | T.consumeClose(); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2687 | } else { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2688 | SkipUntil(tok::r_brace); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2690 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2691 | // If attributes exist after class contents, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2692 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2693 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2694 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2695 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2696 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2697 | T.getOpenLocation(), |
| 2698 | T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2699 | attrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2701 | // C++11 [class.mem]p2: |
| 2702 | // Within the class member-specification, the class is regarded as complete |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2703 | // within function bodies, default arguments, and |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2704 | // brace-or-equal-initializers for non-static data members (including such |
| 2705 | // things in nested classes). |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2706 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2707 | // 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] | 2708 | // are complete and we can parse the delayed portions of method |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2709 | // declarations and the lexed inline method definitions, along with any |
| 2710 | // delayed attributes. |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 2711 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2712 | ParseLexedAttributes(getCurrentClass()); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2713 | ParseLexedMethodDeclarations(getCurrentClass()); |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 2714 | |
| 2715 | // We've finished with all pending member declarations. |
| 2716 | Actions.ActOnFinishCXXMemberDecls(); |
| 2717 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2718 | ParseLexedMemberInitializers(getCurrentClass()); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2719 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 2720 | PrevTokLocation = SavedPrevTokLocation; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2723 | if (TagDecl) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2724 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2725 | T.getCloseLocation()); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 2726 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2727 | // Leave the class scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2728 | ParsingDef.Pop(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 2729 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2730 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2731 | |
Richard Smith | 7faf81f | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 2732 | void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) { |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2733 | assert(Tok.is(tok::kw_namespace)); |
| 2734 | |
| 2735 | // FIXME: Suggest where the close brace should have gone by looking |
| 2736 | // at indentation changes within the definition body. |
Richard Smith | 7faf81f | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 2737 | Diag(D->getLocation(), |
| 2738 | diag::err_missing_end_of_definition) << D; |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2739 | Diag(Tok.getLocation(), |
Richard Smith | 7faf81f | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 2740 | diag::note_missing_end_of_definition_before) << D; |
Richard Smith | b310439 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2741 | |
| 2742 | // Push '};' onto the token stream to recover. |
| 2743 | PP.EnterToken(Tok); |
| 2744 | |
| 2745 | Tok.startToken(); |
| 2746 | Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation)); |
| 2747 | Tok.setKind(tok::semi); |
| 2748 | PP.EnterToken(Tok); |
| 2749 | |
| 2750 | Tok.setKind(tok::r_brace); |
| 2751 | } |
| 2752 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2753 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 2754 | /// which explicitly initializes the members or base classes of a |
| 2755 | /// class (C++ [class.base.init]). For example, the three initializers |
| 2756 | /// after the ':' in the Derived constructor below: |
| 2757 | /// |
| 2758 | /// @code |
| 2759 | /// class Base { }; |
| 2760 | /// class Derived : Base { |
| 2761 | /// int x; |
| 2762 | /// float f; |
| 2763 | /// public: |
| 2764 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 2765 | /// }; |
| 2766 | /// @endcode |
| 2767 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | /// [C++] ctor-initializer: |
| 2769 | /// ':' mem-initializer-list |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2770 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | /// [C++] mem-initializer-list: |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2772 | /// mem-initializer ...[opt] |
| 2773 | /// mem-initializer ...[opt] , mem-initializer-list |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2774 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2775 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 2776 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2777 | // Poison the SEH identifiers so they are flagged as illegal in constructor initializers |
| 2778 | PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2779 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2781 | SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2782 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2784 | do { |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 2785 | if (Tok.is(tok::code_completion)) { |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 2786 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 2787 | MemInitializers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2788 | return cutOffParsing(); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 2789 | } else { |
| 2790 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 2791 | if (!MemInit.isInvalid()) |
| 2792 | MemInitializers.push_back(MemInit.get()); |
| 2793 | else |
| 2794 | AnyErrors = true; |
| 2795 | } |
| 2796 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2797 | if (Tok.is(tok::comma)) |
| 2798 | ConsumeToken(); |
| 2799 | else if (Tok.is(tok::l_brace)) |
| 2800 | break; |
Douglas Gregor | b1f6fa4 | 2010-09-07 14:35:10 +0000 | [diff] [blame] | 2801 | // If the next token looks like a base or member initializer, assume that |
| 2802 | // we're just missing a comma. |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 2803 | else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { |
| 2804 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 2805 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 2806 | << FixItHint::CreateInsertion(Loc, ", "); |
| 2807 | } else { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2808 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2809 | Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2810 | SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2811 | break; |
| 2812 | } |
| 2813 | } while (true); |
| 2814 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 2815 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2816 | AnyErrors); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 2820 | /// part of a constructor initializer that explicitly initializes one |
| 2821 | /// member or base class (C++ [class.base.init]). See |
| 2822 | /// ParseConstructorInitializer for an example. |
| 2823 | /// |
| 2824 | /// [C++] mem-initializer: |
| 2825 | /// mem-initializer-id '(' expression-list[opt] ')' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2826 | /// [C++0x] mem-initializer-id braced-init-list |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | /// |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2828 | /// [C++] mem-initializer-id: |
| 2829 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 2830 | /// identifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2831 | Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2832 | // parse '::'[opt] nested-name-specifier[opt] |
| 2833 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2834 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2835 | ParsedType TemplateTypeTy; |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2836 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2837 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 2838 | if (TemplateId->Kind == TNK_Type_template || |
| 2839 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2840 | AnnotateTemplateIdTokenAsType(); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2841 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2842 | TemplateTypeTy = getTypeAnnotation(Tok); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2843 | } |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2844 | } |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2845 | // Uses of decltype will already have been converted to annot_decltype by |
| 2846 | // ParseOptionalCXXScopeSpecifier at this point. |
| 2847 | if (!TemplateTypeTy && Tok.isNot(tok::identifier) |
| 2848 | && Tok.isNot(tok::annot_decltype)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2849 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2850 | return true; |
| 2851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2853 | IdentifierInfo *II = 0; |
| 2854 | DeclSpec DS(AttrFactory); |
| 2855 | SourceLocation IdLoc = Tok.getLocation(); |
| 2856 | if (Tok.is(tok::annot_decltype)) { |
| 2857 | // Get the decltype expression, if there is one. |
| 2858 | ParseDecltypeSpecifier(DS); |
| 2859 | } else { |
| 2860 | if (Tok.is(tok::identifier)) |
| 2861 | // Get the identifier. This may be a member name or a class name, |
| 2862 | // but we'll let the semantic analysis determine which it is. |
| 2863 | II = Tok.getIdentifierInfo(); |
| 2864 | ConsumeToken(); |
| 2865 | } |
| 2866 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2867 | |
| 2868 | // Parse the '('. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2869 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2870 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2871 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2872 | ExprResult InitList = ParseBraceInitializer(); |
| 2873 | if (InitList.isInvalid()) |
| 2874 | return true; |
| 2875 | |
| 2876 | SourceLocation EllipsisLoc; |
| 2877 | if (Tok.is(tok::ellipsis)) |
| 2878 | EllipsisLoc = ConsumeToken(); |
| 2879 | |
| 2880 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2881 | TemplateTypeTy, DS, IdLoc, |
| 2882 | InitList.take(), EllipsisLoc); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2883 | } else if(Tok.is(tok::l_paren)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2884 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2885 | T.consumeOpen(); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2886 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2887 | // Parse the optional expression-list. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2888 | ExprVector ArgExprs; |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2889 | CommaLocsTy CommaLocs; |
| 2890 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2891 | SkipUntil(tok::r_paren, StopAtSemi); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2892 | return true; |
| 2893 | } |
| 2894 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2895 | T.consumeClose(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2896 | |
| 2897 | SourceLocation EllipsisLoc; |
| 2898 | if (Tok.is(tok::ellipsis)) |
| 2899 | EllipsisLoc = ConsumeToken(); |
| 2900 | |
| 2901 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2902 | TemplateTypeTy, DS, IdLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2903 | T.getOpenLocation(), ArgExprs, |
| 2904 | T.getCloseLocation(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2907 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2908 | : diag::err_expected_lparen); |
| 2909 | return true; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2910 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2911 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2912 | /// \brief Parse a C++ exception-specification if present (C++0x [except.spec]). |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2913 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2914 | /// exception-specification: |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2915 | /// dynamic-exception-specification |
| 2916 | /// noexcept-specification |
| 2917 | /// |
| 2918 | /// noexcept-specification: |
| 2919 | /// 'noexcept' |
| 2920 | /// 'noexcept' '(' constant-expression ')' |
| 2921 | ExceptionSpecificationType |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2922 | Parser::tryParseExceptionSpecification( |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2923 | SourceRange &SpecificationRange, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2924 | SmallVectorImpl<ParsedType> &DynamicExceptions, |
| 2925 | SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2926 | ExprResult &NoexceptExpr) { |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2927 | ExceptionSpecificationType Result = EST_None; |
| 2928 | |
| 2929 | // See if there's a dynamic specification. |
| 2930 | if (Tok.is(tok::kw_throw)) { |
| 2931 | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
| 2932 | DynamicExceptions, |
| 2933 | DynamicExceptionRanges); |
| 2934 | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
| 2935 | "Produced different number of exception types and ranges."); |
| 2936 | } |
| 2937 | |
| 2938 | // If there's no noexcept specification, we're done. |
| 2939 | if (Tok.isNot(tok::kw_noexcept)) |
| 2940 | return Result; |
| 2941 | |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 2942 | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
| 2943 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2944 | // If we already had a dynamic specification, parse the noexcept for, |
| 2945 | // recovery, but emit a diagnostic and don't store the results. |
| 2946 | SourceRange NoexceptRange; |
| 2947 | ExceptionSpecificationType NoexceptType = EST_None; |
| 2948 | |
| 2949 | SourceLocation KeywordLoc = ConsumeToken(); |
| 2950 | if (Tok.is(tok::l_paren)) { |
| 2951 | // There is an argument. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2952 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2953 | T.consumeOpen(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2954 | NoexceptType = EST_ComputedNoexcept; |
| 2955 | NoexceptExpr = ParseConstantExpression(); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2956 | // The argument must be contextually convertible to bool. We use |
| 2957 | // ActOnBooleanCondition for this purpose. |
| 2958 | if (!NoexceptExpr.isInvalid()) |
| 2959 | NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc, |
| 2960 | NoexceptExpr.get()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2961 | T.consumeClose(); |
| 2962 | NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation()); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2963 | } else { |
| 2964 | // There is no argument. |
| 2965 | NoexceptType = EST_BasicNoexcept; |
| 2966 | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
| 2967 | } |
| 2968 | |
| 2969 | if (Result == EST_None) { |
| 2970 | SpecificationRange = NoexceptRange; |
| 2971 | Result = NoexceptType; |
| 2972 | |
| 2973 | // If there's a dynamic specification after a noexcept specification, |
| 2974 | // parse that and ignore the results. |
| 2975 | if (Tok.is(tok::kw_throw)) { |
| 2976 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2977 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
| 2978 | DynamicExceptionRanges); |
| 2979 | } |
| 2980 | } else { |
| 2981 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2982 | } |
| 2983 | |
| 2984 | return Result; |
| 2985 | } |
| 2986 | |
Richard Smith | 79f4bb7 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 2987 | static void diagnoseDynamicExceptionSpecification( |
| 2988 | Parser &P, const SourceRange &Range, bool IsNoexcept) { |
| 2989 | if (P.getLangOpts().CPlusPlus11) { |
| 2990 | const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)"; |
| 2991 | P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range; |
| 2992 | P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated) |
| 2993 | << Replacement << FixItHint::CreateReplacement(Range, Replacement); |
| 2994 | } |
| 2995 | } |
| 2996 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2997 | /// ParseDynamicExceptionSpecification - Parse a C++ |
| 2998 | /// dynamic-exception-specification (C++ [except.spec]). |
| 2999 | /// |
| 3000 | /// dynamic-exception-specification: |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3001 | /// 'throw' '(' type-id-list [opt] ')' |
| 3002 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3004 | /// type-id-list: |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3005 | /// type-id ... [opt] |
| 3006 | /// type-id-list ',' type-id ... [opt] |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3007 | /// |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3008 | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
| 3009 | SourceRange &SpecificationRange, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3010 | SmallVectorImpl<ParsedType> &Exceptions, |
| 3011 | SmallVectorImpl<SourceRange> &Ranges) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3012 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3014 | SpecificationRange.setBegin(ConsumeToken()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3015 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3016 | if (T.consumeOpen()) { |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3017 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 3018 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3019 | return EST_DynamicNone; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3020 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3022 | // Parse throw(...), a Microsoft extension that means "this function |
| 3023 | // can throw anything". |
| 3024 | if (Tok.is(tok::ellipsis)) { |
| 3025 | SourceLocation EllipsisLoc = ConsumeToken(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3026 | if (!getLangOpts().MicrosoftExt) |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3027 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3028 | T.consumeClose(); |
| 3029 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 79f4bb7 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3030 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3031 | return EST_MSAny; |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3034 | // Parse the sequence of type-ids. |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3035 | SourceRange Range; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3036 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3037 | TypeResult Res(ParseTypeName(&Range)); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3038 | |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3039 | if (Tok.is(tok::ellipsis)) { |
| 3040 | // C++0x [temp.variadic]p5: |
| 3041 | // - In a dynamic-exception-specification (15.4); the pattern is a |
| 3042 | // type-id. |
| 3043 | SourceLocation Ellipsis = ConsumeToken(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3044 | Range.setEnd(Ellipsis); |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3045 | if (!Res.isInvalid()) |
| 3046 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
| 3047 | } |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3048 | |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3049 | if (!Res.isInvalid()) { |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3050 | Exceptions.push_back(Res.get()); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3051 | Ranges.push_back(Range); |
| 3052 | } |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3053 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3054 | if (Tok.is(tok::comma)) |
| 3055 | ConsumeToken(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3056 | else |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3057 | break; |
| 3058 | } |
| 3059 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3060 | T.consumeClose(); |
| 3061 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 79f4bb7 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3062 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, |
| 3063 | Exceptions.empty()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3064 | return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3065 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3066 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3067 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 3068 | /// function declaration. |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 3069 | TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) { |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3070 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 3071 | |
| 3072 | ConsumeToken(); |
| 3073 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3074 | return ParseTypeName(&Range, Declarator::TrailingReturnContext); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3077 | /// \brief We have just started parsing the definition of a new class, |
| 3078 | /// so push that class onto our stack of classes that is currently |
| 3079 | /// being parsed. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3080 | Sema::ParsingClassState |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3081 | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass, |
| 3082 | bool IsInterface) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3083 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3084 | "Nested class without outer class"); |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3085 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface)); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3086 | return Actions.PushParsingClass(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | /// \brief Deallocate the given parsed class and all of its nested |
| 3090 | /// classes. |
| 3091 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3092 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 3093 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3094 | delete Class; |
| 3095 | } |
| 3096 | |
| 3097 | /// \brief Pop the top class of the stack of classes that are |
| 3098 | /// currently being parsed. |
| 3099 | /// |
| 3100 | /// This routine should be called when we have finished parsing the |
| 3101 | /// definition of a class, but have not yet popped the Scope |
| 3102 | /// associated with the class's definition. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3103 | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3104 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3106 | Actions.PopParsingClass(state); |
| 3107 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3108 | ParsingClass *Victim = ClassStack.top(); |
| 3109 | ClassStack.pop(); |
| 3110 | if (Victim->TopLevelClass) { |
| 3111 | // Deallocate all of the nested classes of this class, |
| 3112 | // recursively: we don't need to keep any of this information. |
| 3113 | DeallocateParsedClasses(Victim); |
| 3114 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3116 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 3117 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3118 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3119 | // The victim is a nested class, but we will not need to perform |
| 3120 | // any processing after the definition of this class since it has |
| 3121 | // no members whose handling was delayed. Therefore, we can just |
| 3122 | // remove this nested class. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3123 | DeallocateParsedClasses(Victim); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3124 | return; |
| 3125 | } |
| 3126 | |
| 3127 | // This nested class has some members that will need to be processed |
| 3128 | // after the top-level class is completely defined. Therefore, add |
| 3129 | // it to the list of nested classes within its parent. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3130 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3131 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3132 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3133 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3134 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3135 | /// \brief Try to parse an 'identifier' which appears within an attribute-token. |
| 3136 | /// |
| 3137 | /// \return the parsed identifier on success, and 0 if the next token is not an |
| 3138 | /// attribute-token. |
| 3139 | /// |
| 3140 | /// C++11 [dcl.attr.grammar]p3: |
| 3141 | /// If a keyword or an alternative token that satisfies the syntactic |
| 3142 | /// requirements of an identifier is contained in an attribute-token, |
| 3143 | /// it is considered an identifier. |
| 3144 | IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) { |
| 3145 | switch (Tok.getKind()) { |
| 3146 | default: |
| 3147 | // Identifiers and keywords have identifier info attached. |
| 3148 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 3149 | Loc = ConsumeToken(); |
| 3150 | return II; |
| 3151 | } |
| 3152 | return 0; |
| 3153 | |
| 3154 | case tok::ampamp: // 'and' |
| 3155 | case tok::pipe: // 'bitor' |
| 3156 | case tok::pipepipe: // 'or' |
| 3157 | case tok::caret: // 'xor' |
| 3158 | case tok::tilde: // 'compl' |
| 3159 | case tok::amp: // 'bitand' |
| 3160 | case tok::ampequal: // 'and_eq' |
| 3161 | case tok::pipeequal: // 'or_eq' |
| 3162 | case tok::caretequal: // 'xor_eq' |
| 3163 | case tok::exclaim: // 'not' |
| 3164 | case tok::exclaimequal: // 'not_eq' |
| 3165 | // Alternative tokens do not have identifier info, but their spelling |
| 3166 | // starts with an alphabetical character. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3167 | SmallString<8> SpellingBuf; |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3168 | StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf); |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 3169 | if (isLetter(Spelling[0])) { |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3170 | Loc = ConsumeToken(); |
Benjamin Kramer | 0eb7526 | 2012-04-22 20:43:30 +0000 | [diff] [blame] | 3171 | return &PP.getIdentifierTable().get(Spelling); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3172 | } |
| 3173 | return 0; |
| 3174 | } |
| 3175 | } |
| 3176 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3177 | static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName, |
| 3178 | IdentifierInfo *ScopeName) { |
| 3179 | switch (AttributeList::getKind(AttrName, ScopeName, |
| 3180 | AttributeList::AS_CXX11)) { |
| 3181 | case AttributeList::AT_CarriesDependency: |
| 3182 | case AttributeList::AT_FallThrough: |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3183 | case AttributeList::AT_CXX11NoReturn: { |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3184 | return true; |
| 3185 | } |
| 3186 | |
| 3187 | default: |
| 3188 | return false; |
| 3189 | } |
| 3190 | } |
| 3191 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3192 | /// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3193 | /// only parses standard attributes. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3194 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3195 | /// [C++11] attribute-specifier: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3196 | /// '[' '[' attribute-list ']' ']' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3197 | /// alignment-specifier |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3198 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3199 | /// [C++11] attribute-list: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3200 | /// attribute[opt] |
| 3201 | /// attribute-list ',' attribute[opt] |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3202 | /// attribute '...' |
| 3203 | /// attribute-list ',' attribute '...' |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3204 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3205 | /// [C++11] attribute: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3206 | /// attribute-token attribute-argument-clause[opt] |
| 3207 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3208 | /// [C++11] attribute-token: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3209 | /// identifier |
| 3210 | /// attribute-scoped-token |
| 3211 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3212 | /// [C++11] attribute-scoped-token: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3213 | /// attribute-namespace '::' identifier |
| 3214 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3215 | /// [C++11] attribute-namespace: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3216 | /// identifier |
| 3217 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3218 | /// [C++11] attribute-argument-clause: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3219 | /// '(' balanced-token-seq ')' |
| 3220 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3221 | /// [C++11] balanced-token-seq: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3222 | /// balanced-token |
| 3223 | /// balanced-token-seq balanced-token |
| 3224 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3225 | /// [C++11] balanced-token: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3226 | /// '(' balanced-token-seq ')' |
| 3227 | /// '[' balanced-token-seq ']' |
| 3228 | /// '{' balanced-token-seq '}' |
| 3229 | /// any token but '(', ')', '[', ']', '{', or '}' |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3230 | void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs, |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3231 | SourceLocation *endLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3232 | if (Tok.is(tok::kw_alignas)) { |
Richard Smith | 41be673 | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 3233 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3234 | ParseAlignmentSpecifier(attrs, endLoc); |
| 3235 | return; |
| 3236 | } |
| 3237 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3238 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3239 | && "Not a C++11 attribute list"); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3240 | |
Richard Smith | 41be673 | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 3241 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute); |
| 3242 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3243 | ConsumeBracket(); |
| 3244 | ConsumeBracket(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3245 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3246 | llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs; |
| 3247 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3248 | while (Tok.isNot(tok::r_square)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3249 | // attribute not present |
| 3250 | if (Tok.is(tok::comma)) { |
| 3251 | ConsumeToken(); |
| 3252 | continue; |
| 3253 | } |
| 3254 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3255 | SourceLocation ScopeLoc, AttrLoc; |
| 3256 | IdentifierInfo *ScopeName = 0, *AttrName = 0; |
| 3257 | |
| 3258 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 3259 | if (!AttrName) |
| 3260 | // Break out to the "expected ']'" diagnostic. |
| 3261 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3262 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3263 | // scoped attribute |
| 3264 | if (Tok.is(tok::coloncolon)) { |
| 3265 | ConsumeToken(); |
| 3266 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3267 | ScopeName = AttrName; |
| 3268 | ScopeLoc = AttrLoc; |
| 3269 | |
| 3270 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 3271 | if (!AttrName) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3272 | Diag(Tok.getLocation(), diag::err_expected_ident); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3273 | SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3274 | continue; |
| 3275 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3278 | bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3279 | bool AttrParsed = false; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3280 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3281 | if (StandardAttr && |
| 3282 | !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second) |
| 3283 | Diag(AttrLoc, diag::err_cxx11_attribute_repeated) |
| 3284 | << AttrName << SourceRange(SeenAttrs[AttrName]); |
| 3285 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3286 | // Parse attribute arguments |
| 3287 | if (Tok.is(tok::l_paren)) { |
| 3288 | if (ScopeName && ScopeName->getName() == "gnu") { |
| 3289 | ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc, |
| 3290 | ScopeName, ScopeLoc, AttributeList::AS_CXX11); |
| 3291 | AttrParsed = true; |
| 3292 | } else { |
| 3293 | if (StandardAttr) |
| 3294 | Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments) |
| 3295 | << AttrName->getName(); |
| 3296 | |
| 3297 | // FIXME: handle other formats of c++11 attribute arguments |
| 3298 | ConsumeParen(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3299 | SkipUntil(tok::r_paren); |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | if (!AttrParsed) |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 3304 | attrs.addNew(AttrName, |
| 3305 | SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, |
| 3306 | AttrLoc), |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3307 | ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3308 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3309 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3310 | ConsumeToken(); |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3311 | |
| 3312 | Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) |
| 3313 | << AttrName->getName(); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3314 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3318 | SkipUntil(tok::r_square); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3319 | if (endLoc) |
| 3320 | *endLoc = Tok.getLocation(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3321 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3322 | SkipUntil(tok::r_square); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3323 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3324 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3325 | /// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq. |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3326 | /// |
| 3327 | /// attribute-specifier-seq: |
| 3328 | /// attribute-specifier-seq[opt] attribute-specifier |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3329 | void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs, |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3330 | SourceLocation *endLoc) { |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 3331 | assert(getLangOpts().CPlusPlus11); |
| 3332 | |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3333 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
| 3334 | if (!endLoc) |
| 3335 | endLoc = &Loc; |
| 3336 | |
Douglas Gregor | 8828ee7 | 2011-10-07 20:35:25 +0000 | [diff] [blame] | 3337 | do { |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3338 | ParseCXX11AttributeSpecifier(attrs, endLoc); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3339 | } while (isCXX11AttributeSpecifier()); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3340 | |
| 3341 | attrs.Range = SourceRange(StartLoc, *endLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Richard Smith | 5eed7e0 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 3344 | void Parser::DiagnoseAndSkipCXX11Attributes() { |
| 3345 | if (!isCXX11AttributeSpecifier()) |
| 3346 | return; |
| 3347 | |
| 3348 | // Start and end location of an attribute or an attribute list. |
| 3349 | SourceLocation StartLoc = Tok.getLocation(); |
| 3350 | SourceLocation EndLoc; |
| 3351 | |
| 3352 | do { |
| 3353 | if (Tok.is(tok::l_square)) { |
| 3354 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 3355 | T.consumeOpen(); |
| 3356 | T.skipToEnd(); |
| 3357 | EndLoc = T.getCloseLocation(); |
| 3358 | } else { |
| 3359 | assert(Tok.is(tok::kw_alignas) && "not an attribute specifier"); |
| 3360 | ConsumeToken(); |
| 3361 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3362 | if (!T.consumeOpen()) |
| 3363 | T.skipToEnd(); |
| 3364 | EndLoc = T.getCloseLocation(); |
| 3365 | } |
| 3366 | } while (isCXX11AttributeSpecifier()); |
| 3367 | |
| 3368 | if (EndLoc.isValid()) { |
| 3369 | SourceRange Range(StartLoc, EndLoc); |
| 3370 | Diag(StartLoc, diag::err_attributes_not_allowed) |
| 3371 | << Range; |
| 3372 | } |
| 3373 | } |
| 3374 | |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3375 | /// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr] |
| 3376 | /// |
| 3377 | /// [MS] ms-attribute: |
| 3378 | /// '[' token-seq ']' |
| 3379 | /// |
| 3380 | /// [MS] ms-attribute-seq: |
| 3381 | /// ms-attribute[opt] |
| 3382 | /// ms-attribute ms-attribute-seq |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3383 | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
| 3384 | SourceLocation *endLoc) { |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3385 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 3386 | |
| 3387 | while (Tok.is(tok::l_square)) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3388 | // FIXME: If this is actually a C++11 attribute, parse it as one. |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3389 | ConsumeBracket(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3390 | SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3391 | if (endLoc) *endLoc = Tok.getLocation(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3392 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); |
| 3393 | } |
| 3394 | } |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3395 | |
| 3396 | void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType, |
| 3397 | AccessSpecifier& CurAS) { |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3398 | IfExistsCondition Result; |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3399 | if (ParseMicrosoftIfExistsCondition(Result)) |
| 3400 | return; |
| 3401 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3402 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 3403 | if (Braces.consumeOpen()) { |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3404 | Diag(Tok, diag::err_expected_lbrace); |
| 3405 | return; |
| 3406 | } |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3407 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3408 | switch (Result.Behavior) { |
| 3409 | case IEB_Parse: |
| 3410 | // Parse the declarations below. |
| 3411 | break; |
| 3412 | |
| 3413 | case IEB_Dependent: |
| 3414 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
| 3415 | << Result.IsIfExists; |
| 3416 | // Fall through to skip. |
| 3417 | |
| 3418 | case IEB_Skip: |
| 3419 | Braces.skipToEnd(); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3420 | return; |
| 3421 | } |
| 3422 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3423 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3424 | // __if_exists, __if_not_exists can nest. |
| 3425 | if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) { |
| 3426 | ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS); |
| 3427 | continue; |
| 3428 | } |
| 3429 | |
| 3430 | // Check for extraneous top-level semicolon. |
| 3431 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3432 | ConsumeExtraSemi(InsideStruct, TagType); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3433 | continue; |
| 3434 | } |
| 3435 | |
| 3436 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 3437 | if (AS != AS_none) { |
| 3438 | // Current token is a C++ access specifier. |
| 3439 | CurAS = AS; |
| 3440 | SourceLocation ASLoc = Tok.getLocation(); |
| 3441 | ConsumeToken(); |
| 3442 | if (Tok.is(tok::colon)) |
| 3443 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation()); |
| 3444 | else |
| 3445 | Diag(Tok, diag::err_expected_colon); |
| 3446 | ConsumeToken(); |
| 3447 | continue; |
| 3448 | } |
| 3449 | |
| 3450 | // Parse all the comma separated declarators. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3451 | ParseCXXClassMemberDeclaration(CurAS, 0); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3452 | } |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3453 | |
| 3454 | Braces.consumeClose(); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3455 | } |