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