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