Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1 | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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 | f7b2e55 | 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 | |
Douglas Gregor | 696be93 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Scope.h" |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 18 | #include "AstGuard.h" |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 19 | #include "ExtensionRAIIObject.h" |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
| 23 | /// may either be a top level namespace or a block-level namespace alias. |
| 24 | /// |
| 25 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 26 | /// named-namespace-definition |
| 27 | /// unnamed-namespace-definition |
| 28 | /// |
| 29 | /// unnamed-namespace-definition: |
| 30 | /// 'namespace' attributes[opt] '{' namespace-body '}' |
| 31 | /// |
| 32 | /// named-namespace-definition: |
| 33 | /// original-namespace-definition |
| 34 | /// extension-namespace-definition |
| 35 | /// |
| 36 | /// original-namespace-definition: |
| 37 | /// 'namespace' identifier attributes[opt] '{' namespace-body '}' |
| 38 | /// |
| 39 | /// extension-namespace-definition: |
| 40 | /// 'namespace' original-namespace-name '{' namespace-body '}' |
| 41 | /// |
| 42 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 43 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 44 | /// |
| 45 | Parser::DeclTy *Parser::ParseNamespace(unsigned Context) { |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 46 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 47 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
| 48 | |
| 49 | SourceLocation IdentLoc; |
| 50 | IdentifierInfo *Ident = 0; |
| 51 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 52 | if (Tok.is(tok::identifier)) { |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 53 | Ident = Tok.getIdentifierInfo(); |
| 54 | IdentLoc = ConsumeToken(); // eat the identifier. |
| 55 | } |
| 56 | |
| 57 | // Read label attributes, if present. |
| 58 | DeclTy *AttrList = 0; |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 59 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 60 | // FIXME: save these somewhere. |
| 61 | AttrList = ParseAttributes(); |
| 62 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 63 | if (Tok.is(tok::equal)) { |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 64 | // FIXME: Verify no attributes were present. |
| 65 | // FIXME: parse this. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 66 | } else if (Tok.is(tok::l_brace)) { |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 67 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 68 | SourceLocation LBrace = ConsumeBrace(); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 69 | |
| 70 | // Enter a scope for the namespace. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 71 | ParseScope NamespaceScope(this, Scope::DeclScope); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 72 | |
| 73 | DeclTy *NamespcDecl = |
| 74 | Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace); |
| 75 | |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 76 | PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions, |
| 77 | PP.getSourceManager(), |
| 78 | "parsing namespace"); |
Chris Lattner | 696da20 | 2009-03-05 02:09:07 +0000 | [diff] [blame] | 79 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 80 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) |
Chris Lattner | 9c13572 | 2007-08-25 18:15:16 +0000 | [diff] [blame] | 81 | ParseExternalDeclaration(); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 82 | |
Argiris Kirtzidis | 5f21e59 | 2008-05-01 21:44:34 +0000 | [diff] [blame] | 83 | // Leave the namespace scope. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 84 | NamespaceScope.Exit(); |
Argiris Kirtzidis | 5f21e59 | 2008-05-01 21:44:34 +0000 | [diff] [blame] | 85 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 86 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 87 | Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace); |
| 88 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 89 | return NamespcDecl; |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 90 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 91 | } else { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 92 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
| 93 | diag::err_expected_ident_lbrace); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 98 | |
| 99 | /// ParseLinkage - We know that the current token is a string_literal |
| 100 | /// and just before that, that extern was seen. |
| 101 | /// |
| 102 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 103 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 104 | /// 'extern' string-literal declaration |
| 105 | /// |
| 106 | Parser::DeclTy *Parser::ParseLinkage(unsigned Context) { |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 107 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 108 | llvm::SmallVector<char, 8> LangBuffer; |
| 109 | // LangBuffer is guaranteed to be big enough. |
| 110 | LangBuffer.resize(Tok.getLength()); |
| 111 | const char *LangBufPtr = &LangBuffer[0]; |
| 112 | unsigned StrSize = PP.getSpelling(Tok, LangBufPtr); |
| 113 | |
| 114 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 115 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 116 | ParseScope LinkageScope(this, Scope::DeclScope); |
| 117 | DeclTy *LinkageSpec |
| 118 | = Actions.ActOnStartLinkageSpecification(CurScope, |
| 119 | /*FIXME: */SourceLocation(), |
| 120 | Loc, LangBufPtr, StrSize, |
| 121 | Tok.is(tok::l_brace)? Tok.getLocation() |
| 122 | : SourceLocation()); |
| 123 | |
| 124 | if (Tok.isNot(tok::l_brace)) { |
| 125 | ParseDeclarationOrFunctionDefinition(); |
| 126 | return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, |
| 127 | SourceLocation()); |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | SourceLocation LBrace = ConsumeBrace(); |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 131 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 132 | ParseExternalDeclaration(); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 135 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 136 | return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 137 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 138 | |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 139 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 140 | /// using-directive. Assumes that current token is 'using'. |
Chris Lattner | 08ab416 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 141 | Parser::DeclTy *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context) { |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 142 | assert(Tok.is(tok::kw_using) && "Not using token"); |
| 143 | |
| 144 | // Eat 'using'. |
| 145 | SourceLocation UsingLoc = ConsumeToken(); |
| 146 | |
Chris Lattner | 08ab416 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 147 | if (Tok.is(tok::kw_namespace)) |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 148 | // Next token after 'using' is 'namespace' so it must be using-directive |
| 149 | return ParseUsingDirective(Context, UsingLoc); |
Chris Lattner | 08ab416 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 150 | |
| 151 | // Otherwise, it must be using-declaration. |
| 152 | return ParseUsingDeclaration(Context, UsingLoc); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 156 | /// that current token is 'namespace' and 'using' was already parsed. |
| 157 | /// |
| 158 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 159 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 160 | /// namespace-name ; |
| 161 | /// [GNU] using-directive: |
| 162 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 163 | /// namespace-name attributes[opt] ; |
| 164 | /// |
| 165 | Parser::DeclTy *Parser::ParseUsingDirective(unsigned Context, |
| 166 | SourceLocation UsingLoc) { |
| 167 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 168 | |
| 169 | // Eat 'namespace'. |
| 170 | SourceLocation NamespcLoc = ConsumeToken(); |
| 171 | |
| 172 | CXXScopeSpec SS; |
| 173 | // Parse (optional) nested-name-specifier. |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 174 | ParseOptionalCXXScopeSpecifier(SS); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 175 | |
| 176 | AttributeList *AttrList = 0; |
| 177 | IdentifierInfo *NamespcName = 0; |
| 178 | SourceLocation IdentLoc = SourceLocation(); |
| 179 | |
| 180 | // Parse namespace-name. |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 181 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 182 | Diag(Tok, diag::err_expected_namespace_name); |
| 183 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 184 | SkipUntil(tok::semi); |
| 185 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
| 186 | return 0; |
| 187 | } |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 188 | |
| 189 | // Parse identifier. |
| 190 | NamespcName = Tok.getIdentifierInfo(); |
| 191 | IdentLoc = ConsumeToken(); |
| 192 | |
| 193 | // Parse (optional) attributes (most likely GNU strong-using extension). |
| 194 | if (Tok.is(tok::kw___attribute)) |
| 195 | AttrList = ParseAttributes(); |
| 196 | |
| 197 | // Eat ';'. |
| 198 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
| 199 | AttrList ? "attributes list" : "namespace name", tok::semi); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 200 | |
| 201 | return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS, |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 202 | IdentLoc, NamespcName, AttrList); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that |
| 206 | /// 'using' was already seen. |
| 207 | /// |
| 208 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 209 | /// 'using' 'typename'[opt] ::[opt] nested-name-specifier |
| 210 | /// unqualified-id [TODO] |
| 211 | /// 'using' :: unqualified-id [TODO] |
| 212 | /// |
| 213 | Parser::DeclTy *Parser::ParseUsingDeclaration(unsigned Context, |
| 214 | SourceLocation UsingLoc) { |
| 215 | assert(false && "Not implemented"); |
| 216 | // FIXME: Implement parsing. |
| 217 | return 0; |
| 218 | } |
| 219 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 220 | /// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion. |
| 221 | /// |
| 222 | /// static_assert-declaration: |
| 223 | /// static_assert ( constant-expression , string-literal ) ; |
| 224 | /// |
| 225 | Parser::DeclTy *Parser::ParseStaticAssertDeclaration() { |
| 226 | assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration"); |
| 227 | SourceLocation StaticAssertLoc = ConsumeToken(); |
| 228 | |
| 229 | if (Tok.isNot(tok::l_paren)) { |
| 230 | Diag(Tok, diag::err_expected_lparen); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | SourceLocation LParenLoc = ConsumeParen(); |
| 235 | |
| 236 | OwningExprResult AssertExpr(ParseConstantExpression()); |
| 237 | if (AssertExpr.isInvalid()) { |
| 238 | SkipUntil(tok::semi); |
| 239 | return 0; |
| 240 | } |
| 241 | |
Anders Carlsson | a24e8d5 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 242 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 243 | return 0; |
Anders Carlsson | a24e8d5 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 244 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 245 | if (Tok.isNot(tok::string_literal)) { |
| 246 | Diag(Tok, diag::err_expected_string_literal); |
| 247 | SkipUntil(tok::semi); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | OwningExprResult AssertMessage(ParseStringLiteralExpression()); |
| 252 | if (AssertMessage.isInvalid()) |
| 253 | return 0; |
| 254 | |
Anders Carlsson | c45057a | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 255 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 256 | |
| 257 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert); |
| 258 | |
Anders Carlsson | a24e8d5 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 259 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr), |
Anders Carlsson | c45057a | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 260 | move(AssertMessage)); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 263 | /// ParseClassName - Parse a C++ class-name, which names a class. Note |
| 264 | /// that we only check that the result names a type; semantic analysis |
| 265 | /// will need to verify that the type names a class. The result is |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 266 | /// either a type or NULL, depending on whether a type name was |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 267 | /// found. |
| 268 | /// |
| 269 | /// class-name: [C++ 9.1] |
| 270 | /// identifier |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 271 | /// simple-template-id |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 272 | /// |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 273 | Parser::TypeTy *Parser::ParseClassName(SourceLocation &EndLocation, |
| 274 | const CXXScopeSpec *SS) { |
| 275 | // Check whether we have a template-id that names a type. |
| 276 | if (Tok.is(tok::annot_template_id)) { |
| 277 | TemplateIdAnnotation *TemplateId |
| 278 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 279 | if (TemplateId->Kind == TNK_Class_template) { |
| 280 | if (AnnotateTemplateIdTokenAsType(SS)) |
| 281 | return 0; |
| 282 | |
| 283 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
| 284 | TypeTy *Type = Tok.getAnnotationValue(); |
| 285 | EndLocation = Tok.getAnnotationEndLoc(); |
| 286 | ConsumeToken(); |
| 287 | return Type; |
| 288 | } |
| 289 | |
| 290 | // Fall through to produce an error below. |
| 291 | } |
| 292 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 293 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 294 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | // We have an identifier; check whether it is actually a type. |
Douglas Gregor | 1075a16 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 299 | TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 300 | Tok.getLocation(), CurScope, SS); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 301 | if (!Type) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 302 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | // Consume the identifier. |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 307 | EndLocation = ConsumeToken(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 308 | return Type; |
| 309 | } |
| 310 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 311 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 312 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 313 | /// until we reach the start of a definition or see a token that |
| 314 | /// cannot start a definition. |
| 315 | /// |
| 316 | /// class-specifier: [C++ class] |
| 317 | /// class-head '{' member-specification[opt] '}' |
| 318 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 319 | /// class-head: |
| 320 | /// class-key identifier[opt] base-clause[opt] |
| 321 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 322 | /// class-key nested-name-specifier[opt] simple-template-id |
| 323 | /// base-clause[opt] |
| 324 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
| 325 | /// [GNU] class-key attributes[opt] nested-name-specifier |
| 326 | /// identifier base-clause[opt] |
| 327 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
| 328 | /// simple-template-id base-clause[opt] |
| 329 | /// class-key: |
| 330 | /// 'class' |
| 331 | /// 'struct' |
| 332 | /// 'union' |
| 333 | /// |
| 334 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
| 335 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 336 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 337 | /// simple-template-id |
| 338 | /// |
| 339 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 340 | /// together, subsume the C99 struct-or-union-specifier: |
| 341 | /// |
| 342 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 343 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 344 | /// struct-or-union identifier |
| 345 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 346 | /// '}' attributes[opt] |
| 347 | /// [GNU] struct-or-union attributes[opt] identifier |
| 348 | /// struct-or-union: |
| 349 | /// 'struct' |
| 350 | /// 'union' |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 351 | void Parser::ParseClassSpecifier(DeclSpec &DS, |
Douglas Gregor | 0c793bb | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 352 | TemplateParameterLists *TemplateParams, |
| 353 | AccessSpecifier AS) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 354 | assert((Tok.is(tok::kw_class) || |
| 355 | Tok.is(tok::kw_struct) || |
| 356 | Tok.is(tok::kw_union)) && |
| 357 | "Not a class specifier"); |
| 358 | DeclSpec::TST TagType = |
| 359 | Tok.is(tok::kw_class) ? DeclSpec::TST_class : |
| 360 | Tok.is(tok::kw_struct) ? DeclSpec::TST_struct : |
| 361 | DeclSpec::TST_union; |
| 362 | |
| 363 | SourceLocation StartLoc = ConsumeToken(); |
| 364 | |
| 365 | AttributeList *Attr = 0; |
| 366 | // If attributes exist after tag, parse them. |
| 367 | if (Tok.is(tok::kw___attribute)) |
| 368 | Attr = ParseAttributes(); |
| 369 | |
Steve Naroff | c5ab14f | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 370 | // If declspecs exist after tag, parse them. |
| 371 | if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft) |
| 372 | FuzzyParseMicrosoftDeclSpec(); |
| 373 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 374 | // Parse the (optional) nested-name-specifier. |
| 375 | CXXScopeSpec SS; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 376 | if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) |
| 377 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 378 | Diag(Tok, diag::err_expected_ident); |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 379 | |
| 380 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 381 | IdentifierInfo *Name = 0; |
| 382 | SourceLocation NameLoc; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 383 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 384 | if (Tok.is(tok::identifier)) { |
| 385 | Name = Tok.getIdentifierInfo(); |
| 386 | NameLoc = ConsumeToken(); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 387 | } else if (Tok.is(tok::annot_template_id)) { |
| 388 | TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 389 | NameLoc = ConsumeToken(); |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 391 | if (TemplateId->Kind != TNK_Class_template) { |
| 392 | // The template-name in the simple-template-id refers to |
| 393 | // something other than a class template. Give an appropriate |
| 394 | // error message and skip to the ';'. |
| 395 | SourceRange Range(NameLoc); |
| 396 | if (SS.isNotEmpty()) |
| 397 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 398 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 399 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
| 400 | << Name << static_cast<int>(TemplateId->Kind) << Range; |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 402 | DS.SetTypeSpecError(); |
| 403 | SkipUntil(tok::semi, false, true); |
| 404 | TemplateId->Destroy(); |
| 405 | return; |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 406 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // There are three options here. If we have 'struct foo;', then |
| 410 | // this is a forward declaration. If we have 'struct foo {...' or |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 411 | // 'struct foo :...' then this is a definition. Otherwise we have |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 412 | // something like 'struct foo xyz', a reference. |
| 413 | Action::TagKind TK; |
| 414 | if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) |
| 415 | TK = Action::TK_Definition; |
| 416 | else if (Tok.is(tok::semi)) |
| 417 | TK = Action::TK_Declaration; |
| 418 | else |
| 419 | TK = Action::TK_Reference; |
| 420 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 421 | if (!Name && !TemplateId && TK != Action::TK_Definition) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 422 | // We have a declaration or reference to an anonymous class. |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 423 | Diag(StartLoc, diag::err_anon_type_definition) |
| 424 | << DeclSpec::getSpecifierName(TagType); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 425 | |
| 426 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 427 | SkipUntil(tok::comma, true); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 428 | |
| 429 | if (TemplateId) |
| 430 | TemplateId->Destroy(); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 434 | // Create the tag portion of the class or class template. |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 435 | Action::DeclResult TagOrTempResult; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 436 | if (TemplateId && TK != Action::TK_Reference) { |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 437 | // Explicit specialization or class template partial |
| 438 | // specialization. Let semantic analysis decide. |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 439 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
| 440 | TemplateId->getTemplateArgs(), |
| 441 | TemplateId->getTemplateArgIsType(), |
| 442 | TemplateId->NumArgs); |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 443 | TagOrTempResult |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 444 | = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK, |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 445 | StartLoc, SS, |
| 446 | TemplateId->Template, |
| 447 | TemplateId->TemplateNameLoc, |
| 448 | TemplateId->LAngleLoc, |
| 449 | TemplateArgsPtr, |
| 450 | TemplateId->getTemplateArgLocations(), |
| 451 | TemplateId->RAngleLoc, |
| 452 | Attr, |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 453 | Action::MultiTemplateParamsArg(Actions, |
| 454 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 455 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 456 | TemplateId->Destroy(); |
| 457 | } else if (TemplateParams && TK != Action::TK_Reference) |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 458 | TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK, |
| 459 | StartLoc, SS, Name, NameLoc, |
| 460 | Attr, |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 461 | Action::MultiTemplateParamsArg(Actions, |
| 462 | &(*TemplateParams)[0], |
Anders Carlsson | ed20fb9 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 463 | TemplateParams->size()), |
| 464 | AS); |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 465 | else |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 466 | TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name, |
Douglas Gregor | 0c793bb | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 467 | NameLoc, Attr, AS); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 468 | |
| 469 | // Parse the optional base clause (C++ only). |
Chris Lattner | 31ccf0a | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 470 | if (getLang().CPlusPlus && Tok.is(tok::colon)) |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 471 | ParseBaseClause(TagOrTempResult.get()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 472 | |
| 473 | // If there is a body, parse it and inform the actions module. |
| 474 | if (Tok.is(tok::l_brace)) |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 475 | if (getLang().CPlusPlus) |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 476 | ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 477 | else |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 478 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 479 | else if (TK == Action::TK_Definition) { |
| 480 | // FIXME: Complain that we have a base-specifier list but no |
| 481 | // definition. |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 482 | Diag(Tok, diag::err_expected_lbrace); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | const char *PrevSpec = 0; |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 486 | if (TagOrTempResult.isInvalid()) |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 487 | DS.SetTypeSpecError(); |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 488 | else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, |
| 489 | TagOrTempResult.get())) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 490 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
| 494 | /// |
| 495 | /// base-clause : [C++ class.derived] |
| 496 | /// ':' base-specifier-list |
| 497 | /// base-specifier-list: |
| 498 | /// base-specifier '...'[opt] |
| 499 | /// base-specifier-list ',' base-specifier '...'[opt] |
| 500 | void Parser::ParseBaseClause(DeclTy *ClassDecl) |
| 501 | { |
| 502 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 503 | ConsumeToken(); |
| 504 | |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 505 | // Build up an array of parsed base specifiers. |
| 506 | llvm::SmallVector<BaseTy *, 8> BaseInfo; |
| 507 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 508 | while (true) { |
| 509 | // Parse a base-specifier. |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 510 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 10a18fc | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 511 | if (Result.isInvalid()) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 512 | // Skip the rest of this base specifier, up until the comma or |
| 513 | // opening brace. |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 514 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 515 | } else { |
| 516 | // Add this to our array of base specifiers. |
Douglas Gregor | 10a18fc | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 517 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // If the next token is a comma, consume it and keep reading |
| 521 | // base-specifiers. |
| 522 | if (Tok.isNot(tok::comma)) break; |
| 523 | |
| 524 | // Consume the comma. |
| 525 | ConsumeToken(); |
| 526 | } |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 527 | |
| 528 | // Attach the base specifiers |
| 529 | Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 533 | /// one entry in the base class list of a class specifier, for example: |
| 534 | /// class foo : public bar, virtual private baz { |
| 535 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 536 | /// |
| 537 | /// base-specifier: [C++ class.derived] |
| 538 | /// ::[opt] nested-name-specifier[opt] class-name |
| 539 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
| 540 | /// class-name |
| 541 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
| 542 | /// class-name |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 543 | Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl) |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 544 | { |
| 545 | bool IsVirtual = false; |
| 546 | SourceLocation StartLoc = Tok.getLocation(); |
| 547 | |
| 548 | // Parse the 'virtual' keyword. |
| 549 | if (Tok.is(tok::kw_virtual)) { |
| 550 | ConsumeToken(); |
| 551 | IsVirtual = true; |
| 552 | } |
| 553 | |
| 554 | // Parse an (optional) access specifier. |
| 555 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
| 556 | if (Access) |
| 557 | ConsumeToken(); |
| 558 | |
| 559 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 560 | // access specifier. |
| 561 | if (Tok.is(tok::kw_virtual)) { |
| 562 | SourceLocation VirtualLoc = ConsumeToken(); |
| 563 | if (IsVirtual) { |
| 564 | // Complain about duplicate 'virtual' |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 565 | Diag(VirtualLoc, diag::err_dup_virtual) |
| 566 | << SourceRange(VirtualLoc, VirtualLoc); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | IsVirtual = true; |
| 570 | } |
| 571 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 572 | // Parse optional '::' and optional nested-name-specifier. |
| 573 | CXXScopeSpec SS; |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 574 | ParseOptionalCXXScopeSpecifier(SS); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 576 | // The location of the base class itself. |
| 577 | SourceLocation BaseLoc = Tok.getLocation(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 578 | |
| 579 | // Parse the class-name. |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 580 | SourceLocation EndLocation; |
| 581 | TypeTy *BaseType = ParseClassName(EndLocation, &SS); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 582 | if (!BaseType) |
| 583 | return true; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 584 | |
| 585 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 586 | SourceRange Range(StartLoc, EndLocation); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 587 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 588 | // Notify semantic analysis that we have parsed a complete |
| 589 | // base-specifier. |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 590 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, |
| 591 | BaseType, BaseLoc); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 595 | /// a C++ access-specifier. |
| 596 | /// |
| 597 | /// access-specifier: [C++ class.derived] |
| 598 | /// 'private' |
| 599 | /// 'protected' |
| 600 | /// 'public' |
Douglas Gregor | 696be93 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 601 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 602 | { |
| 603 | switch (Tok.getKind()) { |
| 604 | default: return AS_none; |
| 605 | case tok::kw_private: return AS_private; |
| 606 | case tok::kw_protected: return AS_protected; |
| 607 | case tok::kw_public: return AS_public; |
| 608 | } |
| 609 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 610 | |
| 611 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 612 | /// |
| 613 | /// member-declaration: |
| 614 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 615 | /// function-definition ';'[opt] |
| 616 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 617 | /// using-declaration [TODO] |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 618 | /// [C++0x] static_assert-declaration |
Anders Carlsson | ed20fb9 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 619 | /// template-declaration |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 620 | /// [GNU] '__extension__' member-declaration |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 621 | /// |
| 622 | /// member-declarator-list: |
| 623 | /// member-declarator |
| 624 | /// member-declarator-list ',' member-declarator |
| 625 | /// |
| 626 | /// member-declarator: |
| 627 | /// declarator pure-specifier[opt] |
| 628 | /// declarator constant-initializer[opt] |
| 629 | /// identifier[opt] ':' constant-expression |
| 630 | /// |
| 631 | /// pure-specifier: [TODO] |
| 632 | /// '= 0' |
| 633 | /// |
| 634 | /// constant-initializer: |
| 635 | /// '=' constant-expression |
| 636 | /// |
| 637 | Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) { |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 638 | // static_assert-declaration |
| 639 | if (Tok.is(tok::kw_static_assert)) |
| 640 | return ParseStaticAssertDeclaration(); |
| 641 | |
Anders Carlsson | ed20fb9 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 642 | if (Tok.is(tok::kw_template)) |
| 643 | return ParseTemplateDeclarationOrSpecialization(Declarator::MemberContext, |
| 644 | AS); |
| 645 | |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 646 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 647 | if (Tok.is(tok::kw___extension__)) { |
| 648 | // __extension__ silences extension warnings in the subexpression. |
| 649 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 650 | ConsumeToken(); |
| 651 | return ParseCXXClassMemberDeclaration(AS); |
| 652 | } |
| 653 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 654 | SourceLocation DSStart = Tok.getLocation(); |
| 655 | // decl-specifier-seq: |
| 656 | // Parse the common declaration-specifiers piece. |
| 657 | DeclSpec DS; |
Douglas Gregor | 0c793bb | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 658 | ParseDeclarationSpecifiers(DS, 0, AS); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 659 | |
| 660 | if (Tok.is(tok::semi)) { |
| 661 | ConsumeToken(); |
| 662 | // C++ 9.2p7: The member-declarator-list can be omitted only after a |
| 663 | // class-specifier or an enum-specifier or in a friend declaration. |
| 664 | // FIXME: Friend declarations. |
| 665 | switch (DS.getTypeSpecType()) { |
| 666 | case DeclSpec::TST_struct: |
| 667 | case DeclSpec::TST_union: |
| 668 | case DeclSpec::TST_class: |
| 669 | case DeclSpec::TST_enum: |
| 670 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 671 | default: |
| 672 | Diag(DSStart, diag::err_no_declarators); |
| 673 | return 0; |
| 674 | } |
| 675 | } |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 676 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 677 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 678 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 679 | if (Tok.isNot(tok::colon)) { |
| 680 | // Parse the first declarator. |
| 681 | ParseDeclarator(DeclaratorInfo); |
| 682 | // Error parsing the declarator? |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 683 | if (!DeclaratorInfo.hasName()) { |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 684 | // If so, skip until the semi-colon or a }. |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 685 | SkipUntil(tok::r_brace, true); |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 686 | if (Tok.is(tok::semi)) |
| 687 | ConsumeToken(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 691 | // function-definition: |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 692 | if (Tok.is(tok::l_brace) |
| 693 | || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) { |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 694 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
| 695 | Diag(Tok, diag::err_func_def_no_params); |
| 696 | ConsumeBrace(); |
| 697 | SkipUntil(tok::r_brace, true); |
| 698 | return 0; |
| 699 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 700 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 701 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 702 | Diag(Tok, diag::err_function_declared_typedef); |
| 703 | // This recovery skips the entire function body. It would be nice |
| 704 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 705 | // assumes the declarator represents a function, not a typedef. |
| 706 | ConsumeBrace(); |
| 707 | SkipUntil(tok::r_brace, true); |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | return ParseCXXInlineMethodDef(AS, DeclaratorInfo); |
| 712 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // member-declarator-list: |
| 716 | // member-declarator |
| 717 | // member-declarator-list ',' member-declarator |
| 718 | |
| 719 | DeclTy *LastDeclInGroup = 0; |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 720 | OwningExprResult BitfieldSize(Actions); |
| 721 | OwningExprResult Init(Actions); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 722 | |
| 723 | while (1) { |
| 724 | |
| 725 | // member-declarator: |
| 726 | // declarator pure-specifier[opt] |
| 727 | // declarator constant-initializer[opt] |
| 728 | // identifier[opt] ':' constant-expression |
| 729 | |
| 730 | if (Tok.is(tok::colon)) { |
| 731 | ConsumeToken(); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 732 | BitfieldSize = ParseConstantExpression(); |
| 733 | if (BitfieldSize.isInvalid()) |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 734 | SkipUntil(tok::comma, true, true); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // pure-specifier: |
| 738 | // '= 0' |
| 739 | // |
| 740 | // constant-initializer: |
| 741 | // '=' constant-expression |
| 742 | |
| 743 | if (Tok.is(tok::equal)) { |
| 744 | ConsumeToken(); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 745 | Init = ParseInitializer(); |
| 746 | if (Init.isInvalid()) |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 747 | SkipUntil(tok::comma, true, true); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // If attributes exist after the declarator, parse them. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 751 | if (Tok.is(tok::kw___attribute)) { |
| 752 | SourceLocation Loc; |
| 753 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 754 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 755 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 756 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 757 | // NOTE: If Sema is the Action module and declarator is an instance field, |
| 758 | // this call will *not* return the created decl; LastDeclInGroup will be |
| 759 | // returned instead. |
| 760 | // See Sema::ActOnCXXMemberDeclarator for details. |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 761 | LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS, |
| 762 | DeclaratorInfo, |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 763 | BitfieldSize.release(), |
| 764 | Init.release(), |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 765 | LastDeclInGroup); |
| 766 | |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 767 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 768 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
| 769 | != DeclSpec::SCS_typedef) { |
| 770 | // We just declared a member function. If this member function |
| 771 | // has any default arguments, we'll need to parse them later. |
| 772 | LateParsedMethodDeclaration *LateMethod = 0; |
| 773 | DeclaratorChunk::FunctionTypeInfo &FTI |
| 774 | = DeclaratorInfo.getTypeObject(0).Fun; |
| 775 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 776 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 777 | if (!LateMethod) { |
| 778 | // Push this method onto the stack of late-parsed method |
| 779 | // declarations. |
| 780 | getCurTopClassStack().MethodDecls.push_back( |
| 781 | LateParsedMethodDeclaration(LastDeclInGroup)); |
| 782 | LateMethod = &getCurTopClassStack().MethodDecls.back(); |
| 783 | |
| 784 | // Add all of the parameters prior to this one (they don't |
| 785 | // have default arguments). |
| 786 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 787 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 788 | LateMethod->DefaultArgs.push_back( |
| 789 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param)); |
| 790 | } |
| 791 | |
| 792 | // Add this parameter to the list of parameters (it or may |
| 793 | // not have a default argument). |
| 794 | LateMethod->DefaultArgs.push_back( |
| 795 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 796 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 801 | // If we don't have a comma, it is either the end of the list (a ';') |
| 802 | // or an error, bail out. |
| 803 | if (Tok.isNot(tok::comma)) |
| 804 | break; |
| 805 | |
| 806 | // Consume the comma. |
| 807 | ConsumeToken(); |
| 808 | |
| 809 | // Parse the next declarator. |
| 810 | DeclaratorInfo.clear(); |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 811 | BitfieldSize = 0; |
| 812 | Init = 0; |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 813 | |
| 814 | // Attributes are only allowed on the second declarator. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 815 | if (Tok.is(tok::kw___attribute)) { |
| 816 | SourceLocation Loc; |
| 817 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 818 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 819 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 820 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 821 | if (Tok.isNot(tok::colon)) |
| 822 | ParseDeclarator(DeclaratorInfo); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | if (Tok.is(tok::semi)) { |
| 826 | ConsumeToken(); |
| 827 | // Reverse the chain list. |
| 828 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 829 | } |
| 830 | |
| 831 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 832 | // Skip to end of block or statement |
| 833 | SkipUntil(tok::r_brace, true, true); |
| 834 | if (Tok.is(tok::semi)) |
| 835 | ConsumeToken(); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 840 | /// |
| 841 | /// member-specification: |
| 842 | /// member-declaration member-specification[opt] |
| 843 | /// access-specifier ':' member-specification[opt] |
| 844 | /// |
| 845 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
| 846 | unsigned TagType, DeclTy *TagDecl) { |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 847 | assert((TagType == DeclSpec::TST_struct || |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 848 | TagType == DeclSpec::TST_union || |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 849 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 850 | |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 851 | PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions, |
| 852 | PP.getSourceManager(), |
| 853 | "parsing struct/union/class body"); |
Chris Lattner | 7efd75e | 2009-03-05 02:25:03 +0000 | [diff] [blame] | 854 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 855 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 856 | |
Douglas Gregor | cab994d | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 857 | if (!CurScope->isClassScope() && // Not about to define a nested class. |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 858 | CurScope->isInCXXInlineMethodScope()) { |
| 859 | // We will define a local class of an inline method. |
| 860 | // Push a new LexedMethodsForTopClass for its inline methods. |
| 861 | PushTopClassStack(); |
| 862 | } |
| 863 | |
| 864 | // Enter a scope for the class. |
Douglas Gregor | cab994d | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 865 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 867 | if (TagDecl) |
| 868 | Actions.ActOnTagStartDefinition(CurScope, TagDecl); |
| 869 | else { |
| 870 | SkipUntil(tok::r_brace, false, false); |
| 871 | return; |
| 872 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 873 | |
| 874 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 875 | // by default. Members of a class defined with the keywords struct or union |
| 876 | // are public by default. |
| 877 | AccessSpecifier CurAS; |
| 878 | if (TagType == DeclSpec::TST_class) |
| 879 | CurAS = AS_private; |
| 880 | else |
| 881 | CurAS = AS_public; |
| 882 | |
| 883 | // While we still have something to read, read the member-declarations. |
| 884 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 885 | // Each iteration of this loop reads one member-declaration. |
| 886 | |
| 887 | // Check for extraneous top-level semicolon. |
| 888 | if (Tok.is(tok::semi)) { |
| 889 | Diag(Tok, diag::ext_extra_struct_semi); |
| 890 | ConsumeToken(); |
| 891 | continue; |
| 892 | } |
| 893 | |
| 894 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 895 | if (AS != AS_none) { |
| 896 | // Current token is a C++ access specifier. |
| 897 | CurAS = AS; |
| 898 | ConsumeToken(); |
| 899 | ExpectAndConsume(tok::colon, diag::err_expected_colon); |
| 900 | continue; |
| 901 | } |
| 902 | |
| 903 | // Parse all the comma separated declarators. |
| 904 | ParseCXXClassMemberDeclaration(CurAS); |
| 905 | } |
| 906 | |
| 907 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 908 | |
| 909 | AttributeList *AttrList = 0; |
| 910 | // If attributes exist after class contents, parse them. |
| 911 | if (Tok.is(tok::kw___attribute)) |
| 912 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
| 913 | |
| 914 | Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl, |
| 915 | LBraceLoc, RBraceLoc); |
| 916 | |
| 917 | // C++ 9.2p2: Within the class member-specification, the class is regarded as |
| 918 | // complete within function bodies, default arguments, |
| 919 | // exception-specifications, and constructor ctor-initializers (including |
| 920 | // such things in nested classes). |
| 921 | // |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 922 | // FIXME: Only function bodies and constructor ctor-initializers are |
| 923 | // parsed correctly, fix the rest. |
Douglas Gregor | cab994d | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 924 | if (!CurScope->getParent()->isClassScope()) { |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 925 | // We are not inside a nested class. This class and its nested classes |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 926 | // are complete and we can parse the delayed portions of method |
| 927 | // declarations and the lexed inline method definitions. |
| 928 | ParseLexedMethodDeclarations(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 929 | ParseLexedMethodDefs(); |
| 930 | |
| 931 | // For a local class of inline method, pop the LexedMethodsForTopClass that |
| 932 | // was previously pushed. |
| 933 | |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 934 | assert((CurScope->isInCXXInlineMethodScope() || |
| 935 | TopClassStacks.size() == 1) && |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 936 | "MethodLexers not getting popped properly!"); |
| 937 | if (CurScope->isInCXXInlineMethodScope()) |
| 938 | PopTopClassStack(); |
| 939 | } |
| 940 | |
| 941 | // Leave the class scope. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 942 | ClassScope.Exit(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 943 | |
Douglas Gregor | db568cf | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 944 | Actions.ActOnTagFinishDefinition(CurScope, TagDecl); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 945 | } |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 946 | |
| 947 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 948 | /// which explicitly initializes the members or base classes of a |
| 949 | /// class (C++ [class.base.init]). For example, the three initializers |
| 950 | /// after the ':' in the Derived constructor below: |
| 951 | /// |
| 952 | /// @code |
| 953 | /// class Base { }; |
| 954 | /// class Derived : Base { |
| 955 | /// int x; |
| 956 | /// float f; |
| 957 | /// public: |
| 958 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 959 | /// }; |
| 960 | /// @endcode |
| 961 | /// |
| 962 | /// [C++] ctor-initializer: |
| 963 | /// ':' mem-initializer-list |
| 964 | /// |
| 965 | /// [C++] mem-initializer-list: |
| 966 | /// mem-initializer |
| 967 | /// mem-initializer , mem-initializer-list |
| 968 | void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) { |
| 969 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 970 | |
| 971 | SourceLocation ColonLoc = ConsumeToken(); |
| 972 | |
| 973 | llvm::SmallVector<MemInitTy*, 4> MemInitializers; |
| 974 | |
| 975 | do { |
| 976 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
Douglas Gregor | 10a18fc | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 977 | if (!MemInit.isInvalid()) |
| 978 | MemInitializers.push_back(MemInit.get()); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 979 | |
| 980 | if (Tok.is(tok::comma)) |
| 981 | ConsumeToken(); |
| 982 | else if (Tok.is(tok::l_brace)) |
| 983 | break; |
| 984 | else { |
| 985 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 986 | SkipUntil(tok::l_brace, true, true); |
| 987 | break; |
| 988 | } |
| 989 | } while (true); |
| 990 | |
| 991 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, |
| 992 | &MemInitializers[0], MemInitializers.size()); |
| 993 | } |
| 994 | |
| 995 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 996 | /// part of a constructor initializer that explicitly initializes one |
| 997 | /// member or base class (C++ [class.base.init]). See |
| 998 | /// ParseConstructorInitializer for an example. |
| 999 | /// |
| 1000 | /// [C++] mem-initializer: |
| 1001 | /// mem-initializer-id '(' expression-list[opt] ')' |
| 1002 | /// |
| 1003 | /// [C++] mem-initializer-id: |
| 1004 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 1005 | /// identifier |
| 1006 | Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) { |
| 1007 | // FIXME: parse '::'[opt] nested-name-specifier[opt] |
| 1008 | |
| 1009 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1010 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1011 | return true; |
| 1012 | } |
| 1013 | |
| 1014 | // Get the identifier. This may be a member name or a class name, |
| 1015 | // but we'll let the semantic analysis determine which it is. |
| 1016 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1017 | SourceLocation IdLoc = ConsumeToken(); |
| 1018 | |
| 1019 | // Parse the '('. |
| 1020 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1021 | Diag(Tok, diag::err_expected_lparen); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1022 | return true; |
| 1023 | } |
| 1024 | SourceLocation LParenLoc = ConsumeParen(); |
| 1025 | |
| 1026 | // Parse the optional expression-list. |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1027 | ExprVector ArgExprs(Actions); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1028 | CommaLocsTy CommaLocs; |
| 1029 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 1030 | SkipUntil(tok::r_paren); |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
| 1034 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1035 | |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1036 | return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc, |
| 1037 | LParenLoc, ArgExprs.take(), |
| 1038 | ArgExprs.size(), &CommaLocs[0], RParenLoc); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1039 | } |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1040 | |
| 1041 | /// ParseExceptionSpecification - Parse a C++ exception-specification |
| 1042 | /// (C++ [except.spec]). |
| 1043 | /// |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1044 | /// exception-specification: |
| 1045 | /// 'throw' '(' type-id-list [opt] ')' |
| 1046 | /// [MS] 'throw' '(' '...' ')' |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1047 | /// |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1048 | /// type-id-list: |
| 1049 | /// type-id |
| 1050 | /// type-id-list ',' type-id |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1051 | /// |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1052 | bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc) { |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1053 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
| 1054 | |
| 1055 | SourceLocation ThrowLoc = ConsumeToken(); |
| 1056 | |
| 1057 | if (!Tok.is(tok::l_paren)) { |
| 1058 | return Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 1059 | } |
| 1060 | SourceLocation LParenLoc = ConsumeParen(); |
| 1061 | |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1062 | // Parse throw(...), a Microsoft extension that means "this function |
| 1063 | // can throw anything". |
| 1064 | if (Tok.is(tok::ellipsis)) { |
| 1065 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 1066 | if (!getLang().Microsoft) |
| 1067 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1068 | EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1072 | // Parse the sequence of type-ids. |
| 1073 | while (Tok.isNot(tok::r_paren)) { |
| 1074 | ParseTypeName(); |
| 1075 | if (Tok.is(tok::comma)) |
| 1076 | ConsumeToken(); |
| 1077 | else |
| 1078 | break; |
| 1079 | } |
| 1080 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1081 | EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1082 | return false; |
| 1083 | } |