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