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