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" |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
| 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 | |
| 76 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) |
Chris Lattner | 9c13572 | 2007-08-25 18:15:16 +0000 | [diff] [blame] | 77 | ParseExternalDeclaration(); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 78 | |
Argiris Kirtzidis | 5f21e59 | 2008-05-01 21:44:34 +0000 | [diff] [blame] | 79 | // Leave the namespace scope. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 80 | NamespaceScope.Exit(); |
Argiris Kirtzidis | 5f21e59 | 2008-05-01 21:44:34 +0000 | [diff] [blame] | 81 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 82 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 83 | Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace); |
| 84 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 85 | return NamespcDecl; |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 86 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 87 | } else { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 88 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
| 89 | diag::err_expected_ident_lbrace); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 94 | |
| 95 | /// ParseLinkage - We know that the current token is a string_literal |
| 96 | /// and just before that, that extern was seen. |
| 97 | /// |
| 98 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 99 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 100 | /// 'extern' string-literal declaration |
| 101 | /// |
| 102 | Parser::DeclTy *Parser::ParseLinkage(unsigned Context) { |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 103 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 104 | llvm::SmallVector<char, 8> LangBuffer; |
| 105 | // LangBuffer is guaranteed to be big enough. |
| 106 | LangBuffer.resize(Tok.getLength()); |
| 107 | const char *LangBufPtr = &LangBuffer[0]; |
| 108 | unsigned StrSize = PP.getSpelling(Tok, LangBufPtr); |
| 109 | |
| 110 | SourceLocation Loc = ConsumeStringToken(); |
| 111 | DeclTy *D = 0; |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 112 | |
| 113 | if (Tok.isNot(tok::l_brace)) { |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 114 | D = ParseDeclarationOrFunctionDefinition(); |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 115 | if (D) |
| 116 | return Actions.ActOnLinkageSpec(Loc, LangBufPtr, StrSize, D); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | SourceLocation LBrace = ConsumeBrace(); |
| 122 | llvm::SmallVector<DeclTy *, 8> InnerDecls; |
| 123 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 124 | D = ParseExternalDeclaration(); |
| 125 | if (D) |
| 126 | InnerDecls.push_back(D); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 129 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
| 130 | return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, |
| 131 | &InnerDecls.front(), InnerDecls.size()); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 132 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 133 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 134 | /// ParseClassName - Parse a C++ class-name, which names a class. Note |
| 135 | /// that we only check that the result names a type; semantic analysis |
| 136 | /// will need to verify that the type names a class. The result is |
| 137 | /// either a type or NULL, dependending on whether a type name was |
| 138 | /// found. |
| 139 | /// |
| 140 | /// class-name: [C++ 9.1] |
| 141 | /// identifier |
| 142 | /// template-id [TODO] |
| 143 | /// |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 144 | Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) { |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 145 | // Parse the class-name. |
| 146 | // FIXME: Alternatively, parse a simple-template-id. |
| 147 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 148 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | // We have an identifier; check whether it is actually a type. |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 153 | TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 154 | if (!Type) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 155 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | // Consume the identifier. |
| 160 | ConsumeToken(); |
| 161 | |
| 162 | return Type; |
| 163 | } |
| 164 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 165 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 166 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 167 | /// until we reach the start of a definition or see a token that |
| 168 | /// cannot start a definition. |
| 169 | /// |
| 170 | /// class-specifier: [C++ class] |
| 171 | /// class-head '{' member-specification[opt] '}' |
| 172 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 173 | /// class-head: |
| 174 | /// class-key identifier[opt] base-clause[opt] |
| 175 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 176 | /// class-key nested-name-specifier[opt] simple-template-id |
| 177 | /// base-clause[opt] |
| 178 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
| 179 | /// [GNU] class-key attributes[opt] nested-name-specifier |
| 180 | /// identifier base-clause[opt] |
| 181 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
| 182 | /// simple-template-id base-clause[opt] |
| 183 | /// class-key: |
| 184 | /// 'class' |
| 185 | /// 'struct' |
| 186 | /// 'union' |
| 187 | /// |
| 188 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
| 189 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 190 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 191 | /// simple-template-id |
| 192 | /// |
| 193 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 194 | /// together, subsume the C99 struct-or-union-specifier: |
| 195 | /// |
| 196 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 197 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 198 | /// struct-or-union identifier |
| 199 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 200 | /// '}' attributes[opt] |
| 201 | /// [GNU] struct-or-union attributes[opt] identifier |
| 202 | /// struct-or-union: |
| 203 | /// 'struct' |
| 204 | /// 'union' |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame^] | 205 | void Parser::ParseClassSpecifier(DeclSpec &DS, |
| 206 | TemplateParameterLists *TemplateParams) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 207 | assert((Tok.is(tok::kw_class) || |
| 208 | Tok.is(tok::kw_struct) || |
| 209 | Tok.is(tok::kw_union)) && |
| 210 | "Not a class specifier"); |
| 211 | DeclSpec::TST TagType = |
| 212 | Tok.is(tok::kw_class) ? DeclSpec::TST_class : |
| 213 | Tok.is(tok::kw_struct) ? DeclSpec::TST_struct : |
| 214 | DeclSpec::TST_union; |
| 215 | |
| 216 | SourceLocation StartLoc = ConsumeToken(); |
| 217 | |
| 218 | AttributeList *Attr = 0; |
| 219 | // If attributes exist after tag, parse them. |
| 220 | if (Tok.is(tok::kw___attribute)) |
| 221 | Attr = ParseAttributes(); |
| 222 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 223 | // Parse the (optional) nested-name-specifier. |
| 224 | CXXScopeSpec SS; |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 225 | if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) { |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 226 | if (Tok.isNot(tok::identifier)) |
| 227 | Diag(Tok, diag::err_expected_ident); |
| 228 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 229 | |
| 230 | // Parse the (optional) class name. |
| 231 | // FIXME: Alternatively, parse a simple-template-id. |
| 232 | IdentifierInfo *Name = 0; |
| 233 | SourceLocation NameLoc; |
| 234 | if (Tok.is(tok::identifier)) { |
| 235 | Name = Tok.getIdentifierInfo(); |
| 236 | NameLoc = ConsumeToken(); |
| 237 | } |
| 238 | |
| 239 | // There are three options here. If we have 'struct foo;', then |
| 240 | // this is a forward declaration. If we have 'struct foo {...' or |
| 241 | // 'struct fo :...' then this is a definition. Otherwise we have |
| 242 | // something like 'struct foo xyz', a reference. |
| 243 | Action::TagKind TK; |
| 244 | if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) |
| 245 | TK = Action::TK_Definition; |
| 246 | else if (Tok.is(tok::semi)) |
| 247 | TK = Action::TK_Declaration; |
| 248 | else |
| 249 | TK = Action::TK_Reference; |
| 250 | |
| 251 | if (!Name && TK != Action::TK_Definition) { |
| 252 | // We have a declaration or reference to an anonymous class. |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 253 | Diag(StartLoc, diag::err_anon_type_definition) |
| 254 | << DeclSpec::getSpecifierName(TagType); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 255 | |
| 256 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 257 | SkipUntil(tok::comma, true); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // Parse the tag portion of this. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame^] | 262 | DeclTy *TagDecl |
| 263 | = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name, |
| 264 | NameLoc, Attr, |
| 265 | Action::MultiTemplateParamsArg( |
| 266 | Actions, |
| 267 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 268 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 269 | |
| 270 | // Parse the optional base clause (C++ only). |
| 271 | if (getLang().CPlusPlus && Tok.is(tok::colon)) { |
| 272 | ParseBaseClause(TagDecl); |
| 273 | } |
| 274 | |
| 275 | // If there is a body, parse it and inform the actions module. |
| 276 | if (Tok.is(tok::l_brace)) |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 277 | if (getLang().CPlusPlus) |
| 278 | ParseCXXMemberSpecification(StartLoc, TagType, TagDecl); |
| 279 | else |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 280 | ParseStructUnionBody(StartLoc, TagType, TagDecl); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 281 | else if (TK == Action::TK_Definition) { |
| 282 | // FIXME: Complain that we have a base-specifier list but no |
| 283 | // definition. |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 284 | Diag(Tok, diag::err_expected_lbrace); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | const char *PrevSpec = 0; |
| 288 | if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 289 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
| 293 | /// |
| 294 | /// base-clause : [C++ class.derived] |
| 295 | /// ':' base-specifier-list |
| 296 | /// base-specifier-list: |
| 297 | /// base-specifier '...'[opt] |
| 298 | /// base-specifier-list ',' base-specifier '...'[opt] |
| 299 | void Parser::ParseBaseClause(DeclTy *ClassDecl) |
| 300 | { |
| 301 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 302 | ConsumeToken(); |
| 303 | |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 304 | // Build up an array of parsed base specifiers. |
| 305 | llvm::SmallVector<BaseTy *, 8> BaseInfo; |
| 306 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 307 | while (true) { |
| 308 | // Parse a base-specifier. |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 309 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
| 310 | if (Result.isInvalid) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 311 | // Skip the rest of this base specifier, up until the comma or |
| 312 | // opening brace. |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 313 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 314 | } else { |
| 315 | // Add this to our array of base specifiers. |
| 316 | BaseInfo.push_back(Result.Val); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // If the next token is a comma, consume it and keep reading |
| 320 | // base-specifiers. |
| 321 | if (Tok.isNot(tok::comma)) break; |
| 322 | |
| 323 | // Consume the comma. |
| 324 | ConsumeToken(); |
| 325 | } |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 326 | |
| 327 | // Attach the base specifiers |
| 328 | Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 332 | /// one entry in the base class list of a class specifier, for example: |
| 333 | /// class foo : public bar, virtual private baz { |
| 334 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 335 | /// |
| 336 | /// base-specifier: [C++ class.derived] |
| 337 | /// ::[opt] nested-name-specifier[opt] class-name |
| 338 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
| 339 | /// class-name |
| 340 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
| 341 | /// class-name |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 342 | Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl) |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 343 | { |
| 344 | bool IsVirtual = false; |
| 345 | SourceLocation StartLoc = Tok.getLocation(); |
| 346 | |
| 347 | // Parse the 'virtual' keyword. |
| 348 | if (Tok.is(tok::kw_virtual)) { |
| 349 | ConsumeToken(); |
| 350 | IsVirtual = true; |
| 351 | } |
| 352 | |
| 353 | // Parse an (optional) access specifier. |
| 354 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
| 355 | if (Access) |
| 356 | ConsumeToken(); |
| 357 | |
| 358 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 359 | // access specifier. |
| 360 | if (Tok.is(tok::kw_virtual)) { |
| 361 | SourceLocation VirtualLoc = ConsumeToken(); |
| 362 | if (IsVirtual) { |
| 363 | // Complain about duplicate 'virtual' |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 364 | Diag(VirtualLoc, diag::err_dup_virtual) |
| 365 | << SourceRange(VirtualLoc, VirtualLoc); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | IsVirtual = true; |
| 369 | } |
| 370 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 371 | // Parse optional '::' and optional nested-name-specifier. |
| 372 | CXXScopeSpec SS; |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 373 | MaybeParseCXXScopeSpecifier(SS); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 375 | // The location of the base class itself. |
| 376 | SourceLocation BaseLoc = Tok.getLocation(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 377 | |
| 378 | // Parse the class-name. |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 379 | TypeTy *BaseType = ParseClassName(&SS); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 380 | if (!BaseType) |
| 381 | return true; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 382 | |
| 383 | // Find the complete source range for the base-specifier. |
| 384 | SourceRange Range(StartLoc, BaseLoc); |
| 385 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 386 | // Notify semantic analysis that we have parsed a complete |
| 387 | // base-specifier. |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 388 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, |
| 389 | BaseType, BaseLoc); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 393 | /// a C++ access-specifier. |
| 394 | /// |
| 395 | /// access-specifier: [C++ class.derived] |
| 396 | /// 'private' |
| 397 | /// 'protected' |
| 398 | /// 'public' |
Douglas Gregor | 696be93 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 399 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 400 | { |
| 401 | switch (Tok.getKind()) { |
| 402 | default: return AS_none; |
| 403 | case tok::kw_private: return AS_private; |
| 404 | case tok::kw_protected: return AS_protected; |
| 405 | case tok::kw_public: return AS_public; |
| 406 | } |
| 407 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 408 | |
| 409 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 410 | /// |
| 411 | /// member-declaration: |
| 412 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 413 | /// function-definition ';'[opt] |
| 414 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 415 | /// using-declaration [TODO] |
| 416 | /// [C++0x] static_assert-declaration [TODO] |
| 417 | /// template-declaration [TODO] |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 418 | /// [GNU] '__extension__' member-declaration |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 419 | /// |
| 420 | /// member-declarator-list: |
| 421 | /// member-declarator |
| 422 | /// member-declarator-list ',' member-declarator |
| 423 | /// |
| 424 | /// member-declarator: |
| 425 | /// declarator pure-specifier[opt] |
| 426 | /// declarator constant-initializer[opt] |
| 427 | /// identifier[opt] ':' constant-expression |
| 428 | /// |
| 429 | /// pure-specifier: [TODO] |
| 430 | /// '= 0' |
| 431 | /// |
| 432 | /// constant-initializer: |
| 433 | /// '=' constant-expression |
| 434 | /// |
| 435 | Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) { |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 436 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 437 | if (Tok.is(tok::kw___extension__)) { |
| 438 | // __extension__ silences extension warnings in the subexpression. |
| 439 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 440 | ConsumeToken(); |
| 441 | return ParseCXXClassMemberDeclaration(AS); |
| 442 | } |
| 443 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 444 | SourceLocation DSStart = Tok.getLocation(); |
| 445 | // decl-specifier-seq: |
| 446 | // Parse the common declaration-specifiers piece. |
| 447 | DeclSpec DS; |
| 448 | ParseDeclarationSpecifiers(DS); |
| 449 | |
| 450 | if (Tok.is(tok::semi)) { |
| 451 | ConsumeToken(); |
| 452 | // C++ 9.2p7: The member-declarator-list can be omitted only after a |
| 453 | // class-specifier or an enum-specifier or in a friend declaration. |
| 454 | // FIXME: Friend declarations. |
| 455 | switch (DS.getTypeSpecType()) { |
| 456 | case DeclSpec::TST_struct: |
| 457 | case DeclSpec::TST_union: |
| 458 | case DeclSpec::TST_class: |
| 459 | case DeclSpec::TST_enum: |
| 460 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 461 | default: |
| 462 | Diag(DSStart, diag::err_no_declarators); |
| 463 | return 0; |
| 464 | } |
| 465 | } |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 466 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 467 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 468 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 469 | if (Tok.isNot(tok::colon)) { |
| 470 | // Parse the first declarator. |
| 471 | ParseDeclarator(DeclaratorInfo); |
| 472 | // Error parsing the declarator? |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 473 | if (!DeclaratorInfo.hasName()) { |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 474 | // If so, skip until the semi-colon or a }. |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 475 | SkipUntil(tok::r_brace, true); |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 476 | if (Tok.is(tok::semi)) |
| 477 | ConsumeToken(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 481 | // function-definition: |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 482 | if (Tok.is(tok::l_brace) |
| 483 | || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) { |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 484 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
| 485 | Diag(Tok, diag::err_func_def_no_params); |
| 486 | ConsumeBrace(); |
| 487 | SkipUntil(tok::r_brace, true); |
| 488 | return 0; |
| 489 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 490 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 491 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 492 | Diag(Tok, diag::err_function_declared_typedef); |
| 493 | // This recovery skips the entire function body. It would be nice |
| 494 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 495 | // assumes the declarator represents a function, not a typedef. |
| 496 | ConsumeBrace(); |
| 497 | SkipUntil(tok::r_brace, true); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | return ParseCXXInlineMethodDef(AS, DeclaratorInfo); |
| 502 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // member-declarator-list: |
| 506 | // member-declarator |
| 507 | // member-declarator-list ',' member-declarator |
| 508 | |
| 509 | DeclTy *LastDeclInGroup = 0; |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 510 | OwningExprResult BitfieldSize(Actions); |
| 511 | OwningExprResult Init(Actions); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 512 | |
| 513 | while (1) { |
| 514 | |
| 515 | // member-declarator: |
| 516 | // declarator pure-specifier[opt] |
| 517 | // declarator constant-initializer[opt] |
| 518 | // identifier[opt] ':' constant-expression |
| 519 | |
| 520 | if (Tok.is(tok::colon)) { |
| 521 | ConsumeToken(); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 522 | BitfieldSize = ParseConstantExpression(); |
| 523 | if (BitfieldSize.isInvalid()) |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 524 | SkipUntil(tok::comma, true, true); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // pure-specifier: |
| 528 | // '= 0' |
| 529 | // |
| 530 | // constant-initializer: |
| 531 | // '=' constant-expression |
| 532 | |
| 533 | if (Tok.is(tok::equal)) { |
| 534 | ConsumeToken(); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 535 | Init = ParseInitializer(); |
| 536 | if (Init.isInvalid()) |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 537 | SkipUntil(tok::comma, true, true); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // If attributes exist after the declarator, parse them. |
| 541 | if (Tok.is(tok::kw___attribute)) |
| 542 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 543 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 544 | // NOTE: If Sema is the Action module and declarator is an instance field, |
| 545 | // this call will *not* return the created decl; LastDeclInGroup will be |
| 546 | // returned instead. |
| 547 | // See Sema::ActOnCXXMemberDeclarator for details. |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 548 | LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS, |
| 549 | DeclaratorInfo, |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 550 | BitfieldSize.release(), |
| 551 | Init.release(), |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 552 | LastDeclInGroup); |
| 553 | |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 554 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 555 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
| 556 | != DeclSpec::SCS_typedef) { |
| 557 | // We just declared a member function. If this member function |
| 558 | // has any default arguments, we'll need to parse them later. |
| 559 | LateParsedMethodDeclaration *LateMethod = 0; |
| 560 | DeclaratorChunk::FunctionTypeInfo &FTI |
| 561 | = DeclaratorInfo.getTypeObject(0).Fun; |
| 562 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 563 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 564 | if (!LateMethod) { |
| 565 | // Push this method onto the stack of late-parsed method |
| 566 | // declarations. |
| 567 | getCurTopClassStack().MethodDecls.push_back( |
| 568 | LateParsedMethodDeclaration(LastDeclInGroup)); |
| 569 | LateMethod = &getCurTopClassStack().MethodDecls.back(); |
| 570 | |
| 571 | // Add all of the parameters prior to this one (they don't |
| 572 | // have default arguments). |
| 573 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 574 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 575 | LateMethod->DefaultArgs.push_back( |
| 576 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param)); |
| 577 | } |
| 578 | |
| 579 | // Add this parameter to the list of parameters (it or may |
| 580 | // not have a default argument). |
| 581 | LateMethod->DefaultArgs.push_back( |
| 582 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 583 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 588 | // If we don't have a comma, it is either the end of the list (a ';') |
| 589 | // or an error, bail out. |
| 590 | if (Tok.isNot(tok::comma)) |
| 591 | break; |
| 592 | |
| 593 | // Consume the comma. |
| 594 | ConsumeToken(); |
| 595 | |
| 596 | // Parse the next declarator. |
| 597 | DeclaratorInfo.clear(); |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 598 | BitfieldSize = 0; |
| 599 | Init = 0; |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 600 | |
| 601 | // Attributes are only allowed on the second declarator. |
| 602 | if (Tok.is(tok::kw___attribute)) |
| 603 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 604 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 605 | if (Tok.isNot(tok::colon)) |
| 606 | ParseDeclarator(DeclaratorInfo); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | if (Tok.is(tok::semi)) { |
| 610 | ConsumeToken(); |
| 611 | // Reverse the chain list. |
| 612 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 613 | } |
| 614 | |
| 615 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 616 | // Skip to end of block or statement |
| 617 | SkipUntil(tok::r_brace, true, true); |
| 618 | if (Tok.is(tok::semi)) |
| 619 | ConsumeToken(); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 624 | /// |
| 625 | /// member-specification: |
| 626 | /// member-declaration member-specification[opt] |
| 627 | /// access-specifier ':' member-specification[opt] |
| 628 | /// |
| 629 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
| 630 | unsigned TagType, DeclTy *TagDecl) { |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 631 | assert((TagType == DeclSpec::TST_struct || |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 632 | TagType == DeclSpec::TST_union || |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 633 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 634 | |
| 635 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 636 | |
| 637 | if (!CurScope->isCXXClassScope() && // Not about to define a nested class. |
| 638 | CurScope->isInCXXInlineMethodScope()) { |
| 639 | // We will define a local class of an inline method. |
| 640 | // Push a new LexedMethodsForTopClass for its inline methods. |
| 641 | PushTopClassStack(); |
| 642 | } |
| 643 | |
| 644 | // Enter a scope for the class. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 645 | ParseScope ClassScope(this, Scope::CXXClassScope|Scope::DeclScope); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 646 | |
| 647 | Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc); |
| 648 | |
| 649 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 650 | // by default. Members of a class defined with the keywords struct or union |
| 651 | // are public by default. |
| 652 | AccessSpecifier CurAS; |
| 653 | if (TagType == DeclSpec::TST_class) |
| 654 | CurAS = AS_private; |
| 655 | else |
| 656 | CurAS = AS_public; |
| 657 | |
| 658 | // While we still have something to read, read the member-declarations. |
| 659 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 660 | // Each iteration of this loop reads one member-declaration. |
| 661 | |
| 662 | // Check for extraneous top-level semicolon. |
| 663 | if (Tok.is(tok::semi)) { |
| 664 | Diag(Tok, diag::ext_extra_struct_semi); |
| 665 | ConsumeToken(); |
| 666 | continue; |
| 667 | } |
| 668 | |
| 669 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 670 | if (AS != AS_none) { |
| 671 | // Current token is a C++ access specifier. |
| 672 | CurAS = AS; |
| 673 | ConsumeToken(); |
| 674 | ExpectAndConsume(tok::colon, diag::err_expected_colon); |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | // Parse all the comma separated declarators. |
| 679 | ParseCXXClassMemberDeclaration(CurAS); |
| 680 | } |
| 681 | |
| 682 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 683 | |
| 684 | AttributeList *AttrList = 0; |
| 685 | // If attributes exist after class contents, parse them. |
| 686 | if (Tok.is(tok::kw___attribute)) |
| 687 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
| 688 | |
| 689 | Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl, |
| 690 | LBraceLoc, RBraceLoc); |
| 691 | |
| 692 | // C++ 9.2p2: Within the class member-specification, the class is regarded as |
| 693 | // complete within function bodies, default arguments, |
| 694 | // exception-specifications, and constructor ctor-initializers (including |
| 695 | // such things in nested classes). |
| 696 | // |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 697 | // FIXME: Only function bodies and constructor ctor-initializers are |
| 698 | // parsed correctly, fix the rest. |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 699 | if (!CurScope->getParent()->isCXXClassScope()) { |
| 700 | // 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] | 701 | // are complete and we can parse the delayed portions of method |
| 702 | // declarations and the lexed inline method definitions. |
| 703 | ParseLexedMethodDeclarations(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 704 | ParseLexedMethodDefs(); |
| 705 | |
| 706 | // For a local class of inline method, pop the LexedMethodsForTopClass that |
| 707 | // was previously pushed. |
| 708 | |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 709 | assert((CurScope->isInCXXInlineMethodScope() || |
| 710 | TopClassStacks.size() == 1) && |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 711 | "MethodLexers not getting popped properly!"); |
| 712 | if (CurScope->isInCXXInlineMethodScope()) |
| 713 | PopTopClassStack(); |
| 714 | } |
| 715 | |
| 716 | // Leave the class scope. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 717 | ClassScope.Exit(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 718 | |
Argiris Kirtzidis | 448b4e4 | 2008-08-09 00:39:29 +0000 | [diff] [blame] | 719 | Actions.ActOnFinishCXXClassDef(TagDecl); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 720 | } |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 721 | |
| 722 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 723 | /// which explicitly initializes the members or base classes of a |
| 724 | /// class (C++ [class.base.init]). For example, the three initializers |
| 725 | /// after the ':' in the Derived constructor below: |
| 726 | /// |
| 727 | /// @code |
| 728 | /// class Base { }; |
| 729 | /// class Derived : Base { |
| 730 | /// int x; |
| 731 | /// float f; |
| 732 | /// public: |
| 733 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 734 | /// }; |
| 735 | /// @endcode |
| 736 | /// |
| 737 | /// [C++] ctor-initializer: |
| 738 | /// ':' mem-initializer-list |
| 739 | /// |
| 740 | /// [C++] mem-initializer-list: |
| 741 | /// mem-initializer |
| 742 | /// mem-initializer , mem-initializer-list |
| 743 | void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) { |
| 744 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 745 | |
| 746 | SourceLocation ColonLoc = ConsumeToken(); |
| 747 | |
| 748 | llvm::SmallVector<MemInitTy*, 4> MemInitializers; |
| 749 | |
| 750 | do { |
| 751 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 752 | if (!MemInit.isInvalid) |
| 753 | MemInitializers.push_back(MemInit.Val); |
| 754 | |
| 755 | if (Tok.is(tok::comma)) |
| 756 | ConsumeToken(); |
| 757 | else if (Tok.is(tok::l_brace)) |
| 758 | break; |
| 759 | else { |
| 760 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 761 | SkipUntil(tok::l_brace, true, true); |
| 762 | break; |
| 763 | } |
| 764 | } while (true); |
| 765 | |
| 766 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, |
| 767 | &MemInitializers[0], MemInitializers.size()); |
| 768 | } |
| 769 | |
| 770 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 771 | /// part of a constructor initializer that explicitly initializes one |
| 772 | /// member or base class (C++ [class.base.init]). See |
| 773 | /// ParseConstructorInitializer for an example. |
| 774 | /// |
| 775 | /// [C++] mem-initializer: |
| 776 | /// mem-initializer-id '(' expression-list[opt] ')' |
| 777 | /// |
| 778 | /// [C++] mem-initializer-id: |
| 779 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 780 | /// identifier |
| 781 | Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) { |
| 782 | // FIXME: parse '::'[opt] nested-name-specifier[opt] |
| 783 | |
| 784 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 785 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 786 | return true; |
| 787 | } |
| 788 | |
| 789 | // Get the identifier. This may be a member name or a class name, |
| 790 | // but we'll let the semantic analysis determine which it is. |
| 791 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 792 | SourceLocation IdLoc = ConsumeToken(); |
| 793 | |
| 794 | // Parse the '('. |
| 795 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 796 | Diag(Tok, diag::err_expected_lparen); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 797 | return true; |
| 798 | } |
| 799 | SourceLocation LParenLoc = ConsumeParen(); |
| 800 | |
| 801 | // Parse the optional expression-list. |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 802 | ExprVector ArgExprs(Actions); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 803 | CommaLocsTy CommaLocs; |
| 804 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 805 | SkipUntil(tok::r_paren); |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 810 | |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 811 | return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc, |
| 812 | LParenLoc, ArgExprs.take(), |
| 813 | ArgExprs.size(), &CommaLocs[0], RParenLoc); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 814 | } |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 815 | |
| 816 | /// ParseExceptionSpecification - Parse a C++ exception-specification |
| 817 | /// (C++ [except.spec]). |
| 818 | /// |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 819 | /// exception-specification: |
| 820 | /// 'throw' '(' type-id-list [opt] ')' |
| 821 | /// [MS] 'throw' '(' '...' ')' |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 822 | /// |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 823 | /// type-id-list: |
| 824 | /// type-id |
| 825 | /// type-id-list ',' type-id |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 826 | /// |
| 827 | bool Parser::ParseExceptionSpecification() { |
| 828 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
| 829 | |
| 830 | SourceLocation ThrowLoc = ConsumeToken(); |
| 831 | |
| 832 | if (!Tok.is(tok::l_paren)) { |
| 833 | return Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 834 | } |
| 835 | SourceLocation LParenLoc = ConsumeParen(); |
| 836 | |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 837 | // Parse throw(...), a Microsoft extension that means "this function |
| 838 | // can throw anything". |
| 839 | if (Tok.is(tok::ellipsis)) { |
| 840 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 841 | if (!getLang().Microsoft) |
| 842 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
| 843 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 844 | return false; |
| 845 | } |
| 846 | |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 847 | // Parse the sequence of type-ids. |
| 848 | while (Tok.isNot(tok::r_paren)) { |
| 849 | ParseTypeName(); |
| 850 | if (Tok.is(tok::comma)) |
| 851 | ConsumeToken(); |
| 852 | else |
| 853 | break; |
| 854 | } |
| 855 | |
| 856 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 857 | return false; |
| 858 | } |