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