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