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