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