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