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" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Scope.h" |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
| 21 | /// may either be a top level namespace or a block-level namespace alias. |
| 22 | /// |
| 23 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 24 | /// named-namespace-definition |
| 25 | /// unnamed-namespace-definition |
| 26 | /// |
| 27 | /// unnamed-namespace-definition: |
| 28 | /// 'namespace' attributes[opt] '{' namespace-body '}' |
| 29 | /// |
| 30 | /// named-namespace-definition: |
| 31 | /// original-namespace-definition |
| 32 | /// extension-namespace-definition |
| 33 | /// |
| 34 | /// original-namespace-definition: |
| 35 | /// 'namespace' identifier attributes[opt] '{' namespace-body '}' |
| 36 | /// |
| 37 | /// extension-namespace-definition: |
| 38 | /// 'namespace' original-namespace-name '{' namespace-body '}' |
| 39 | /// |
| 40 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 41 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 42 | /// |
| 43 | Parser::DeclTy *Parser::ParseNamespace(unsigned Context) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 44 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 45 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
| 46 | |
| 47 | SourceLocation IdentLoc; |
| 48 | IdentifierInfo *Ident = 0; |
| 49 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 50 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 51 | Ident = Tok.getIdentifierInfo(); |
| 52 | IdentLoc = ConsumeToken(); // eat the identifier. |
| 53 | } |
| 54 | |
| 55 | // Read label attributes, if present. |
| 56 | DeclTy *AttrList = 0; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 57 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 58 | // FIXME: save these somewhere. |
| 59 | AttrList = ParseAttributes(); |
| 60 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 61 | if (Tok.is(tok::equal)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 62 | // FIXME: Verify no attributes were present. |
| 63 | // FIXME: parse this. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 64 | } else if (Tok.is(tok::l_brace)) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 66 | SourceLocation LBrace = ConsumeBrace(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 67 | |
| 68 | // Enter a scope for the namespace. |
| 69 | EnterScope(Scope::DeclScope); |
| 70 | |
| 71 | DeclTy *NamespcDecl = |
| 72 | Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace); |
| 73 | |
| 74 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) |
Chris Lattner | bae3511 | 2007-08-25 18:15:16 +0000 | [diff] [blame] | 75 | ParseExternalDeclaration(); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 76 | |
Argyrios Kyrtzidis | 8ba5d79 | 2008-05-01 21:44:34 +0000 | [diff] [blame] | 77 | // Leave the namespace scope. |
| 78 | ExitScope(); |
| 79 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 80 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 81 | Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace); |
| 82 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 83 | return NamespcDecl; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 85 | } else { |
| 86 | unsigned D = Ident ? diag::err_expected_lbrace : |
| 87 | diag::err_expected_ident_lbrace; |
| 88 | Diag(Tok.getLocation(), D); |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 93 | |
| 94 | /// ParseLinkage - We know that the current token is a string_literal |
| 95 | /// and just before that, that extern was seen. |
| 96 | /// |
| 97 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 98 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 99 | /// 'extern' string-literal declaration |
| 100 | /// |
| 101 | Parser::DeclTy *Parser::ParseLinkage(unsigned Context) { |
| 102 | assert(Tok.is(tok::string_literal) && "Not a stringliteral!"); |
| 103 | llvm::SmallVector<char, 8> LangBuffer; |
| 104 | // LangBuffer is guaranteed to be big enough. |
| 105 | LangBuffer.resize(Tok.getLength()); |
| 106 | const char *LangBufPtr = &LangBuffer[0]; |
| 107 | unsigned StrSize = PP.getSpelling(Tok, LangBufPtr); |
| 108 | |
| 109 | SourceLocation Loc = ConsumeStringToken(); |
| 110 | DeclTy *D = 0; |
| 111 | SourceLocation LBrace, RBrace; |
| 112 | |
| 113 | if (Tok.isNot(tok::l_brace)) { |
| 114 | D = ParseDeclaration(Context); |
| 115 | } else { |
| 116 | LBrace = ConsumeBrace(); |
| 117 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 118 | // FIXME capture the decls. |
| 119 | D = ParseExternalDeclaration(); |
| 120 | } |
| 121 | |
| 122 | RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
| 123 | } |
| 124 | |
| 125 | if (!D) |
| 126 | return 0; |
| 127 | |
| 128 | return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D); |
| 129 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 130 | |
| 131 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 132 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 133 | /// until we reach the start of a definition or see a token that |
| 134 | /// cannot start a definition. |
| 135 | /// |
| 136 | /// class-specifier: [C++ class] |
| 137 | /// class-head '{' member-specification[opt] '}' |
| 138 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 139 | /// class-head: |
| 140 | /// class-key identifier[opt] base-clause[opt] |
| 141 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 142 | /// class-key nested-name-specifier[opt] simple-template-id |
| 143 | /// base-clause[opt] |
| 144 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
| 145 | /// [GNU] class-key attributes[opt] nested-name-specifier |
| 146 | /// identifier base-clause[opt] |
| 147 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
| 148 | /// simple-template-id base-clause[opt] |
| 149 | /// class-key: |
| 150 | /// 'class' |
| 151 | /// 'struct' |
| 152 | /// 'union' |
| 153 | /// |
| 154 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
| 155 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 156 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 157 | /// simple-template-id |
| 158 | /// |
| 159 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 160 | /// together, subsume the C99 struct-or-union-specifier: |
| 161 | /// |
| 162 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 163 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 164 | /// struct-or-union identifier |
| 165 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 166 | /// '}' attributes[opt] |
| 167 | /// [GNU] struct-or-union attributes[opt] identifier |
| 168 | /// struct-or-union: |
| 169 | /// 'struct' |
| 170 | /// 'union' |
| 171 | void Parser::ParseClassSpecifier(DeclSpec &DS) { |
| 172 | assert((Tok.is(tok::kw_class) || |
| 173 | Tok.is(tok::kw_struct) || |
| 174 | Tok.is(tok::kw_union)) && |
| 175 | "Not a class specifier"); |
| 176 | DeclSpec::TST TagType = |
| 177 | Tok.is(tok::kw_class) ? DeclSpec::TST_class : |
| 178 | Tok.is(tok::kw_struct) ? DeclSpec::TST_struct : |
| 179 | DeclSpec::TST_union; |
| 180 | |
| 181 | SourceLocation StartLoc = ConsumeToken(); |
| 182 | |
| 183 | AttributeList *Attr = 0; |
| 184 | // If attributes exist after tag, parse them. |
| 185 | if (Tok.is(tok::kw___attribute)) |
| 186 | Attr = ParseAttributes(); |
| 187 | |
| 188 | // FIXME: Parse the (optional) nested-name-specifier. |
| 189 | |
| 190 | // Parse the (optional) class name. |
| 191 | // FIXME: Alternatively, parse a simple-template-id. |
| 192 | IdentifierInfo *Name = 0; |
| 193 | SourceLocation NameLoc; |
| 194 | if (Tok.is(tok::identifier)) { |
| 195 | Name = Tok.getIdentifierInfo(); |
| 196 | NameLoc = ConsumeToken(); |
| 197 | } |
| 198 | |
| 199 | // There are three options here. If we have 'struct foo;', then |
| 200 | // this is a forward declaration. If we have 'struct foo {...' or |
| 201 | // 'struct fo :...' then this is a definition. Otherwise we have |
| 202 | // something like 'struct foo xyz', a reference. |
| 203 | Action::TagKind TK; |
| 204 | if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) |
| 205 | TK = Action::TK_Definition; |
| 206 | else if (Tok.is(tok::semi)) |
| 207 | TK = Action::TK_Declaration; |
| 208 | else |
| 209 | TK = Action::TK_Reference; |
| 210 | |
| 211 | if (!Name && TK != Action::TK_Definition) { |
| 212 | // We have a declaration or reference to an anonymous class. |
| 213 | Diag(StartLoc, diag::err_anon_type_definition, |
| 214 | DeclSpec::getSpecifierName(TagType)); |
| 215 | |
| 216 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 217 | SkipUntil(tok::comma, true); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | // Parse the tag portion of this. |
| 222 | DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, |
| 223 | NameLoc, Attr); |
| 224 | |
| 225 | // Parse the optional base clause (C++ only). |
| 226 | if (getLang().CPlusPlus && Tok.is(tok::colon)) { |
| 227 | ParseBaseClause(TagDecl); |
| 228 | } |
| 229 | |
| 230 | // If there is a body, parse it and inform the actions module. |
| 231 | if (Tok.is(tok::l_brace)) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 232 | if (getLang().CPlusPlus) |
| 233 | ParseCXXMemberSpecification(StartLoc, TagType, TagDecl); |
| 234 | else |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 235 | ParseStructUnionBody(StartLoc, TagType, TagDecl); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 236 | else if (TK == Action::TK_Definition) { |
| 237 | // FIXME: Complain that we have a base-specifier list but no |
| 238 | // definition. |
| 239 | Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 240 | } |
| 241 | |
| 242 | const char *PrevSpec = 0; |
| 243 | if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) |
| 244 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 245 | } |
| 246 | |
| 247 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
| 248 | /// |
| 249 | /// base-clause : [C++ class.derived] |
| 250 | /// ':' base-specifier-list |
| 251 | /// base-specifier-list: |
| 252 | /// base-specifier '...'[opt] |
| 253 | /// base-specifier-list ',' base-specifier '...'[opt] |
| 254 | void Parser::ParseBaseClause(DeclTy *ClassDecl) |
| 255 | { |
| 256 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 257 | ConsumeToken(); |
| 258 | |
| 259 | while (true) { |
| 260 | // Parse a base-specifier. |
| 261 | if (ParseBaseSpecifier(ClassDecl)) { |
| 262 | // Skip the rest of this base specifier, up until the comma or |
| 263 | // opening brace. |
| 264 | SkipUntil(tok::comma, tok::l_brace); |
| 265 | } |
| 266 | |
| 267 | // If the next token is a comma, consume it and keep reading |
| 268 | // base-specifiers. |
| 269 | if (Tok.isNot(tok::comma)) break; |
| 270 | |
| 271 | // Consume the comma. |
| 272 | ConsumeToken(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 277 | /// one entry in the base class list of a class specifier, for example: |
| 278 | /// class foo : public bar, virtual private baz { |
| 279 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 280 | /// |
| 281 | /// base-specifier: [C++ class.derived] |
| 282 | /// ::[opt] nested-name-specifier[opt] class-name |
| 283 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
| 284 | /// class-name |
| 285 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
| 286 | /// class-name |
| 287 | bool Parser::ParseBaseSpecifier(DeclTy *ClassDecl) |
| 288 | { |
| 289 | bool IsVirtual = false; |
| 290 | SourceLocation StartLoc = Tok.getLocation(); |
| 291 | |
| 292 | // Parse the 'virtual' keyword. |
| 293 | if (Tok.is(tok::kw_virtual)) { |
| 294 | ConsumeToken(); |
| 295 | IsVirtual = true; |
| 296 | } |
| 297 | |
| 298 | // Parse an (optional) access specifier. |
| 299 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
| 300 | if (Access) |
| 301 | ConsumeToken(); |
| 302 | |
| 303 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 304 | // access specifier. |
| 305 | if (Tok.is(tok::kw_virtual)) { |
| 306 | SourceLocation VirtualLoc = ConsumeToken(); |
| 307 | if (IsVirtual) { |
| 308 | // Complain about duplicate 'virtual' |
| 309 | Diag(VirtualLoc, diag::err_dup_virtual); |
| 310 | } |
| 311 | |
| 312 | IsVirtual = true; |
| 313 | } |
| 314 | |
| 315 | // FIXME: Parse optional '::' and optional nested-name-specifier. |
| 316 | |
| 317 | // Parse the class-name. |
| 318 | // FIXME: Alternatively, parse a simple-template-id. |
| 319 | if (Tok.isNot(tok::identifier)) { |
| 320 | Diag(Tok.getLocation(), diag::err_expected_class_name); |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | // We have an identifier; check whether it is actually a type. |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 325 | TypeTy *BaseType = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 326 | if (!BaseType) { |
| 327 | Diag(Tok.getLocation(), diag::err_expected_class_name); |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | // The location of the base class itself. |
| 332 | SourceLocation BaseLoc = Tok.getLocation(); |
| 333 | |
| 334 | // Find the complete source range for the base-specifier. |
| 335 | SourceRange Range(StartLoc, BaseLoc); |
| 336 | |
| 337 | // Consume the identifier token (finally!). |
| 338 | ConsumeToken(); |
| 339 | |
| 340 | // Notify semantic analysis that we have parsed a complete |
| 341 | // base-specifier. |
| 342 | Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, BaseType, |
| 343 | BaseLoc); |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 348 | /// a C++ access-specifier. |
| 349 | /// |
| 350 | /// access-specifier: [C++ class.derived] |
| 351 | /// 'private' |
| 352 | /// 'protected' |
| 353 | /// 'public' |
Douglas Gregor | 1b7f898 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 354 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 355 | { |
| 356 | switch (Tok.getKind()) { |
| 357 | default: return AS_none; |
| 358 | case tok::kw_private: return AS_private; |
| 359 | case tok::kw_protected: return AS_protected; |
| 360 | case tok::kw_public: return AS_public; |
| 361 | } |
| 362 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 363 | |
| 364 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 365 | /// |
| 366 | /// member-declaration: |
| 367 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 368 | /// function-definition ';'[opt] |
| 369 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 370 | /// using-declaration [TODO] |
| 371 | /// [C++0x] static_assert-declaration [TODO] |
| 372 | /// template-declaration [TODO] |
| 373 | /// |
| 374 | /// member-declarator-list: |
| 375 | /// member-declarator |
| 376 | /// member-declarator-list ',' member-declarator |
| 377 | /// |
| 378 | /// member-declarator: |
| 379 | /// declarator pure-specifier[opt] |
| 380 | /// declarator constant-initializer[opt] |
| 381 | /// identifier[opt] ':' constant-expression |
| 382 | /// |
| 383 | /// pure-specifier: [TODO] |
| 384 | /// '= 0' |
| 385 | /// |
| 386 | /// constant-initializer: |
| 387 | /// '=' constant-expression |
| 388 | /// |
| 389 | Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) { |
| 390 | SourceLocation DSStart = Tok.getLocation(); |
| 391 | // decl-specifier-seq: |
| 392 | // Parse the common declaration-specifiers piece. |
| 393 | DeclSpec DS; |
| 394 | ParseDeclarationSpecifiers(DS); |
| 395 | |
| 396 | if (Tok.is(tok::semi)) { |
| 397 | ConsumeToken(); |
| 398 | // C++ 9.2p7: The member-declarator-list can be omitted only after a |
| 399 | // class-specifier or an enum-specifier or in a friend declaration. |
| 400 | // FIXME: Friend declarations. |
| 401 | switch (DS.getTypeSpecType()) { |
| 402 | case DeclSpec::TST_struct: |
| 403 | case DeclSpec::TST_union: |
| 404 | case DeclSpec::TST_class: |
| 405 | case DeclSpec::TST_enum: |
| 406 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 407 | default: |
| 408 | Diag(DSStart, diag::err_no_declarators); |
| 409 | return 0; |
| 410 | } |
| 411 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 412 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 413 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 414 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 415 | if (Tok.isNot(tok::colon)) { |
| 416 | // Parse the first declarator. |
| 417 | ParseDeclarator(DeclaratorInfo); |
| 418 | // Error parsing the declarator? |
| 419 | if (DeclaratorInfo.getIdentifier() == 0) { |
| 420 | // If so, skip until the semi-colon or a }. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 421 | SkipUntil(tok::r_brace, true); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 422 | if (Tok.is(tok::semi)) |
| 423 | ConsumeToken(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 427 | // function-definition: |
| 428 | if (Tok.is(tok::l_brace)) { |
| 429 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
| 430 | Diag(Tok, diag::err_func_def_no_params); |
| 431 | ConsumeBrace(); |
| 432 | SkipUntil(tok::r_brace, true); |
| 433 | return 0; |
| 434 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 435 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 436 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 437 | Diag(Tok, diag::err_function_declared_typedef); |
| 438 | // This recovery skips the entire function body. It would be nice |
| 439 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 440 | // assumes the declarator represents a function, not a typedef. |
| 441 | ConsumeBrace(); |
| 442 | SkipUntil(tok::r_brace, true); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | return ParseCXXInlineMethodDef(AS, DeclaratorInfo); |
| 447 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // member-declarator-list: |
| 451 | // member-declarator |
| 452 | // member-declarator-list ',' member-declarator |
| 453 | |
| 454 | DeclTy *LastDeclInGroup = 0; |
| 455 | ExprTy *BitfieldSize = 0; |
| 456 | ExprTy *Init = 0; |
| 457 | |
| 458 | while (1) { |
| 459 | |
| 460 | // member-declarator: |
| 461 | // declarator pure-specifier[opt] |
| 462 | // declarator constant-initializer[opt] |
| 463 | // identifier[opt] ':' constant-expression |
| 464 | |
| 465 | if (Tok.is(tok::colon)) { |
| 466 | ConsumeToken(); |
| 467 | ExprResult Res = ParseConstantExpression(); |
| 468 | if (Res.isInvalid) |
| 469 | SkipUntil(tok::comma, true, true); |
| 470 | else |
| 471 | BitfieldSize = Res.Val; |
| 472 | } |
| 473 | |
| 474 | // pure-specifier: |
| 475 | // '= 0' |
| 476 | // |
| 477 | // constant-initializer: |
| 478 | // '=' constant-expression |
| 479 | |
| 480 | if (Tok.is(tok::equal)) { |
| 481 | ConsumeToken(); |
| 482 | ExprResult Res = ParseInitializer(); |
| 483 | if (Res.isInvalid) |
| 484 | SkipUntil(tok::comma, true, true); |
| 485 | else |
| 486 | Init = Res.Val; |
| 487 | } |
| 488 | |
| 489 | // If attributes exist after the declarator, parse them. |
| 490 | if (Tok.is(tok::kw___attribute)) |
| 491 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 492 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 493 | // NOTE: If Sema is the Action module and declarator is an instance field, |
| 494 | // this call will *not* return the created decl; LastDeclInGroup will be |
| 495 | // returned instead. |
| 496 | // See Sema::ActOnCXXMemberDeclarator for details. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 497 | LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS, |
| 498 | DeclaratorInfo, |
| 499 | BitfieldSize, Init, |
| 500 | LastDeclInGroup); |
| 501 | |
| 502 | // If we don't have a comma, it is either the end of the list (a ';') |
| 503 | // or an error, bail out. |
| 504 | if (Tok.isNot(tok::comma)) |
| 505 | break; |
| 506 | |
| 507 | // Consume the comma. |
| 508 | ConsumeToken(); |
| 509 | |
| 510 | // Parse the next declarator. |
| 511 | DeclaratorInfo.clear(); |
| 512 | BitfieldSize = Init = 0; |
| 513 | |
| 514 | // Attributes are only allowed on the second declarator. |
| 515 | if (Tok.is(tok::kw___attribute)) |
| 516 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 517 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 518 | if (Tok.isNot(tok::colon)) |
| 519 | ParseDeclarator(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (Tok.is(tok::semi)) { |
| 523 | ConsumeToken(); |
| 524 | // Reverse the chain list. |
| 525 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 526 | } |
| 527 | |
| 528 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 529 | // Skip to end of block or statement |
| 530 | SkipUntil(tok::r_brace, true, true); |
| 531 | if (Tok.is(tok::semi)) |
| 532 | ConsumeToken(); |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 537 | /// |
| 538 | /// member-specification: |
| 539 | /// member-declaration member-specification[opt] |
| 540 | /// access-specifier ':' member-specification[opt] |
| 541 | /// |
| 542 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
| 543 | unsigned TagType, DeclTy *TagDecl) { |
| 544 | assert(TagType == DeclSpec::TST_struct || |
| 545 | TagType == DeclSpec::TST_union || |
| 546 | TagType == DeclSpec::TST_class && "Invalid TagType!"); |
| 547 | |
| 548 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 549 | |
| 550 | if (!CurScope->isCXXClassScope() && // Not about to define a nested class. |
| 551 | CurScope->isInCXXInlineMethodScope()) { |
| 552 | // We will define a local class of an inline method. |
| 553 | // Push a new LexedMethodsForTopClass for its inline methods. |
| 554 | PushTopClassStack(); |
| 555 | } |
| 556 | |
| 557 | // Enter a scope for the class. |
| 558 | EnterScope(Scope::CXXClassScope|Scope::DeclScope); |
| 559 | |
| 560 | Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc); |
| 561 | |
| 562 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 563 | // by default. Members of a class defined with the keywords struct or union |
| 564 | // are public by default. |
| 565 | AccessSpecifier CurAS; |
| 566 | if (TagType == DeclSpec::TST_class) |
| 567 | CurAS = AS_private; |
| 568 | else |
| 569 | CurAS = AS_public; |
| 570 | |
| 571 | // While we still have something to read, read the member-declarations. |
| 572 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 573 | // Each iteration of this loop reads one member-declaration. |
| 574 | |
| 575 | // Check for extraneous top-level semicolon. |
| 576 | if (Tok.is(tok::semi)) { |
| 577 | Diag(Tok, diag::ext_extra_struct_semi); |
| 578 | ConsumeToken(); |
| 579 | continue; |
| 580 | } |
| 581 | |
| 582 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 583 | if (AS != AS_none) { |
| 584 | // Current token is a C++ access specifier. |
| 585 | CurAS = AS; |
| 586 | ConsumeToken(); |
| 587 | ExpectAndConsume(tok::colon, diag::err_expected_colon); |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | // Parse all the comma separated declarators. |
| 592 | ParseCXXClassMemberDeclaration(CurAS); |
| 593 | } |
| 594 | |
| 595 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 596 | |
| 597 | AttributeList *AttrList = 0; |
| 598 | // If attributes exist after class contents, parse them. |
| 599 | if (Tok.is(tok::kw___attribute)) |
| 600 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
| 601 | |
| 602 | Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl, |
| 603 | LBraceLoc, RBraceLoc); |
| 604 | |
| 605 | // C++ 9.2p2: Within the class member-specification, the class is regarded as |
| 606 | // complete within function bodies, default arguments, |
| 607 | // exception-specifications, and constructor ctor-initializers (including |
| 608 | // such things in nested classes). |
| 609 | // |
| 610 | // FIXME: Only function bodies are parsed correctly, fix the rest. |
| 611 | if (!CurScope->getParent()->isCXXClassScope()) { |
| 612 | // We are not inside a nested class. This class and its nested classes |
| 613 | // are complete and we can parse the lexed inline method definitions. |
| 614 | ParseLexedMethodDefs(); |
| 615 | |
| 616 | // For a local class of inline method, pop the LexedMethodsForTopClass that |
| 617 | // was previously pushed. |
| 618 | |
| 619 | assert(CurScope->isInCXXInlineMethodScope() || |
| 620 | TopClassStacks.size() == 1 && |
| 621 | "MethodLexers not getting popped properly!"); |
| 622 | if (CurScope->isInCXXInlineMethodScope()) |
| 623 | PopTopClassStack(); |
| 624 | } |
| 625 | |
| 626 | // Leave the class scope. |
| 627 | ExitScope(); |
| 628 | |
Argyrios Kyrtzidis | 5b7f0c8 | 2008-08-09 00:39:29 +0000 | [diff] [blame] | 629 | Actions.ActOnFinishCXXClassDef(TagDecl); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 630 | } |