Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1 | //===--- ParseObjC.cpp - Objective C Parsing ------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Objective-C portions of the Parser interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Parse/Parser.h" |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 16 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 18 | #include "clang/Sema/DeclSpec.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 19 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Fariborz Jahanian | 0860235 | 2012-09-17 19:15:26 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Nico Weber | 9f4f5f1 | 2013-04-03 17:36:11 +0000 | [diff] [blame] | 25 | /// Skips attributes after an Objective-C @ directive. Emits a diagnostic. |
Nico Weber | e145bfd | 2013-04-04 00:15:10 +0000 | [diff] [blame] | 26 | void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) { |
Nico Weber | 9f4f5f1 | 2013-04-03 17:36:11 +0000 | [diff] [blame] | 27 | ParsedAttributes attrs(AttrFactory); |
| 28 | if (Tok.is(tok::kw___attribute)) { |
Nico Weber | e145bfd | 2013-04-04 00:15:10 +0000 | [diff] [blame] | 29 | if (Kind == tok::objc_interface || Kind == tok::objc_protocol) |
| 30 | Diag(Tok, diag::err_objc_postfix_attribute_hint) |
| 31 | << (Kind == tok::objc_protocol); |
| 32 | else |
| 33 | Diag(Tok, diag::err_objc_postfix_attribute); |
Nico Weber | 9f4f5f1 | 2013-04-03 17:36:11 +0000 | [diff] [blame] | 34 | ParseGNUAttributes(attrs); |
| 35 | } |
| 36 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 891dca6 | 2008-12-08 21:53:24 +0000 | [diff] [blame] | 38 | /// ParseObjCAtDirectives - Handle parts of the external-declaration production: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | /// external-declaration: [C99 6.9] |
| 40 | /// [OBJC] objc-class-definition |
Steve Naroff | 91fa0b7 | 2007-10-29 21:39:29 +0000 | [diff] [blame] | 41 | /// [OBJC] objc-class-declaration |
| 42 | /// [OBJC] objc-alias-declaration |
| 43 | /// [OBJC] objc-protocol-definition |
| 44 | /// [OBJC] objc-method-definition |
| 45 | /// [OBJC] '@' 'end' |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 46 | Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 49 | if (Tok.is(tok::code_completion)) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 50 | Actions.CodeCompleteObjCAtDirective(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 51 | cutOffParsing(); |
| 52 | return DeclGroupPtrTy(); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 53 | } |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 54 | |
| 55 | Decl *SingleDecl = 0; |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 56 | switch (Tok.getObjCKeywordID()) { |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 57 | case tok::objc_class: |
| 58 | return ParseObjCAtClassDeclaration(AtLoc); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 59 | case tok::objc_interface: { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 60 | ParsedAttributes attrs(AttrFactory); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 61 | SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs); |
| 62 | break; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 63 | } |
| 64 | case tok::objc_protocol: { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 65 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 66 | return ParseObjCAtProtocolDeclaration(AtLoc, attrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 67 | } |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 68 | case tok::objc_implementation: |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 69 | return ParseObjCAtImplementationDeclaration(AtLoc); |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 70 | case tok::objc_end: |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 71 | return ParseObjCAtEndDeclaration(AtLoc); |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 72 | case tok::objc_compatibility_alias: |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 73 | SingleDecl = ParseObjCAtAliasDeclaration(AtLoc); |
| 74 | break; |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 75 | case tok::objc_synthesize: |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 76 | SingleDecl = ParseObjCPropertySynthesize(AtLoc); |
| 77 | break; |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 78 | case tok::objc_dynamic: |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 79 | SingleDecl = ParseObjCPropertyDynamic(AtLoc); |
| 80 | break; |
Douglas Gregor | 1b257af | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 81 | case tok::objc_import: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 82 | if (getLangOpts().Modules) |
Douglas Gregor | 94ad28b | 2012-01-03 18:24:14 +0000 | [diff] [blame] | 83 | return ParseModuleImport(AtLoc); |
| 84 | |
| 85 | // Fall through |
| 86 | |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 87 | default: |
| 88 | Diag(AtLoc, diag::err_unexpected_at); |
| 89 | SkipUntil(tok::semi); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 90 | SingleDecl = 0; |
| 91 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | } |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 93 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | /// objc-class-declaration: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | /// '@' 'class' identifier-list ';' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | /// |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 100 | Parser::DeclGroupPtrTy |
| 101 | Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | ConsumeToken(); // the identifier "class" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 103 | SmallVector<IdentifierInfo *, 8> ClassNames; |
| 104 | SmallVector<SourceLocation, 8> ClassLocs; |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 105 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | while (1) { |
Nico Weber | e145bfd | 2013-04-04 00:15:10 +0000 | [diff] [blame] | 108 | MaybeSkipAttributes(tok::objc_class); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 109 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | Diag(Tok, diag::err_expected_ident); |
| 111 | SkipUntil(tok::semi); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 112 | return Actions.ConvertDeclToDeclGroup(0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | ClassNames.push_back(Tok.getIdentifierInfo()); |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 115 | ClassLocs.push_back(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 118 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | ConsumeToken(); |
| 122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | // Consume the ';'. |
| 125 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class")) |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 126 | return Actions.ConvertDeclToDeclGroup(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 128 | return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(), |
| 129 | ClassLocs.data(), |
| 130 | ClassNames.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 133 | void Parser::CheckNestedObjCContexts(SourceLocation AtLoc) |
| 134 | { |
| 135 | Sema::ObjCContainerKind ock = Actions.getObjCContainerKind(); |
| 136 | if (ock == Sema::OCK_None) |
| 137 | return; |
| 138 | |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 139 | Decl *Decl = Actions.getObjCDeclContext(); |
| 140 | if (CurParsedObjCImpl) { |
| 141 | CurParsedObjCImpl->finish(AtLoc); |
| 142 | } else { |
| 143 | Actions.ActOnAtEnd(getCurScope(), AtLoc); |
| 144 | } |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 145 | Diag(AtLoc, diag::err_objc_missing_end) |
| 146 | << FixItHint::CreateInsertion(AtLoc, "@end\n"); |
| 147 | if (Decl) |
| 148 | Diag(Decl->getLocStart(), diag::note_objc_container_start) |
| 149 | << (int) ock; |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 152 | /// |
| 153 | /// objc-interface: |
| 154 | /// objc-class-interface-attributes[opt] objc-class-interface |
| 155 | /// objc-category-interface |
| 156 | /// |
| 157 | /// objc-class-interface: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | /// '@' 'interface' identifier objc-superclass[opt] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 159 | /// objc-protocol-refs[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | /// objc-class-instance-variables[opt] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 161 | /// objc-interface-decl-list |
| 162 | /// @end |
| 163 | /// |
| 164 | /// objc-category-interface: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | /// '@' 'interface' identifier '(' identifier[opt] ')' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 166 | /// objc-protocol-refs[opt] |
| 167 | /// objc-interface-decl-list |
| 168 | /// @end |
| 169 | /// |
| 170 | /// objc-superclass: |
| 171 | /// ':' identifier |
| 172 | /// |
| 173 | /// objc-class-interface-attributes: |
| 174 | /// __attribute__((visibility("default"))) |
| 175 | /// __attribute__((visibility("hidden"))) |
| 176 | /// __attribute__((deprecated)) |
| 177 | /// __attribute__((unavailable)) |
| 178 | /// __attribute__((objc_exception)) - used by NSException on 64-bit |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 179 | /// __attribute__((objc_root_class)) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 180 | /// |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 181 | Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 182 | ParsedAttributes &attrs) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 183 | assert(Tok.isObjCAtKeyword(tok::objc_interface) && |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 184 | "ParseObjCAtInterfaceDeclaration(): Expected @interface"); |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 185 | CheckNestedObjCContexts(AtLoc); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 186 | ConsumeToken(); // the "interface" identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 188 | // Code completion after '@interface'. |
| 189 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 190 | Actions.CodeCompleteObjCInterfaceDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 191 | cutOffParsing(); |
| 192 | return 0; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Nico Weber | e145bfd | 2013-04-04 00:15:10 +0000 | [diff] [blame] | 195 | MaybeSkipAttributes(tok::objc_interface); |
Nico Weber | 9f4f5f1 | 2013-04-03 17:36:11 +0000 | [diff] [blame] | 196 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 197 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 198 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 199 | return 0; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 200 | } |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 201 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 202 | // We have a class or category name - consume it. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 203 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 204 | SourceLocation nameLoc = ConsumeToken(); |
Fariborz Jahanian | 5512ba5 | 2010-04-26 21:18:08 +0000 | [diff] [blame] | 205 | if (Tok.is(tok::l_paren) && |
| 206 | !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 207 | |
| 208 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 209 | T.consumeOpen(); |
| 210 | |
| 211 | SourceLocation categoryLoc; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 212 | IdentifierInfo *categoryId = 0; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 213 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 214 | Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 215 | cutOffParsing(); |
| 216 | return 0; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 219 | // For ObjC2, the category name is optional (not an error). |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 220 | if (Tok.is(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 221 | categoryId = Tok.getIdentifierInfo(); |
| 222 | categoryLoc = ConsumeToken(); |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 223 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 224 | else if (!getLangOpts().ObjC2) { |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 225 | Diag(Tok, diag::err_expected_ident); // missing category name. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 226 | return 0; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 227 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 228 | |
| 229 | T.consumeClose(); |
| 230 | if (T.getCloseLocation().isInvalid()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 231 | return 0; |
Douglas Gregor | 13d05ac | 2011-09-23 19:19:41 +0000 | [diff] [blame] | 232 | |
| 233 | if (!attrs.empty()) { // categories don't support attributes. |
| 234 | Diag(nameLoc, diag::err_objc_no_attributes_on_category); |
| 235 | attrs.clear(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 236 | } |
Douglas Gregor | 13d05ac | 2011-09-23 19:19:41 +0000 | [diff] [blame] | 237 | |
Fariborz Jahanian | 5512ba5 | 2010-04-26 21:18:08 +0000 | [diff] [blame] | 238 | // Next, we need to check for any protocol references. |
| 239 | SourceLocation LAngleLoc, EndProtoLoc; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 240 | SmallVector<Decl *, 8> ProtocolRefs; |
| 241 | SmallVector<SourceLocation, 8> ProtocolLocs; |
Fariborz Jahanian | 5512ba5 | 2010-04-26 21:18:08 +0000 | [diff] [blame] | 242 | if (Tok.is(tok::less) && |
| 243 | ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 244 | LAngleLoc, EndProtoLoc)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 245 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 247 | Decl *CategoryType = |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 248 | Actions.ActOnStartCategoryInterface(AtLoc, |
Fariborz Jahanian | 5512ba5 | 2010-04-26 21:18:08 +0000 | [diff] [blame] | 249 | nameId, nameLoc, |
| 250 | categoryId, categoryLoc, |
| 251 | ProtocolRefs.data(), |
| 252 | ProtocolRefs.size(), |
| 253 | ProtocolLocs.data(), |
| 254 | EndProtoLoc); |
Fariborz Jahanian | e6f07f5 | 2011-08-19 18:02:47 +0000 | [diff] [blame] | 255 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 256 | if (Tok.is(tok::l_brace)) |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 257 | ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 258 | |
Fariborz Jahanian | 2f64cfe | 2011-08-22 21:44:58 +0000 | [diff] [blame] | 259 | ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType); |
Fariborz Jahanian | 5512ba5 | 2010-04-26 21:18:08 +0000 | [diff] [blame] | 260 | return CategoryType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 261 | } |
| 262 | // Parse a class interface. |
| 263 | IdentifierInfo *superClassId = 0; |
| 264 | SourceLocation superClassLoc; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 265 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 266 | if (Tok.is(tok::colon)) { // a super class is specified. |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 267 | ConsumeToken(); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 268 | |
| 269 | // Code completion of superclass names. |
| 270 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 271 | Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 272 | cutOffParsing(); |
| 273 | return 0; |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 276 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 277 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 278 | return 0; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 279 | } |
| 280 | superClassId = Tok.getIdentifierInfo(); |
| 281 | superClassLoc = ConsumeToken(); |
| 282 | } |
| 283 | // Next, we need to check for any protocol references. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 284 | SmallVector<Decl *, 8> ProtocolRefs; |
| 285 | SmallVector<SourceLocation, 8> ProtocolLocs; |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 286 | SourceLocation LAngleLoc, EndProtoLoc; |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 287 | if (Tok.is(tok::less) && |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 288 | ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, |
| 289 | LAngleLoc, EndProtoLoc)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 290 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 292 | Decl *ClsType = |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 293 | Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc, |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 294 | superClassId, superClassLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 295 | ProtocolRefs.data(), ProtocolRefs.size(), |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 296 | ProtocolLocs.data(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 297 | EndProtoLoc, attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 299 | if (Tok.is(tok::l_brace)) |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 300 | ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 301 | |
Fariborz Jahanian | 2f64cfe | 2011-08-22 21:44:58 +0000 | [diff] [blame] | 302 | ParseObjCInterfaceDeclList(tok::objc_interface, ClsType); |
Fariborz Jahanian | 5512ba5 | 2010-04-26 21:18:08 +0000 | [diff] [blame] | 303 | return ClsType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 304 | } |
| 305 | |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 306 | /// The Objective-C property callback. This should be defined where |
| 307 | /// it's used, but instead it's been lifted to here to support VS2005. |
| 308 | struct Parser::ObjCPropertyCallback : FieldCallback { |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 309 | private: |
| 310 | virtual void anchor(); |
| 311 | public: |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 312 | Parser &P; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 313 | SmallVectorImpl<Decl *> &Props; |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 314 | ObjCDeclSpec &OCDS; |
| 315 | SourceLocation AtLoc; |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 316 | SourceLocation LParenLoc; |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 317 | tok::ObjCKeywordKind MethodImplKind; |
| 318 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 319 | ObjCPropertyCallback(Parser &P, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 320 | SmallVectorImpl<Decl *> &Props, |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 321 | ObjCDeclSpec &OCDS, SourceLocation AtLoc, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 322 | SourceLocation LParenLoc, |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 323 | tok::ObjCKeywordKind MethodImplKind) : |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 324 | P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc), |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 325 | MethodImplKind(MethodImplKind) { |
| 326 | } |
| 327 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 328 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 329 | if (FD.D.getIdentifier() == 0) { |
| 330 | P.Diag(AtLoc, diag::err_objc_property_requires_field_name) |
| 331 | << FD.D.getSourceRange(); |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 332 | return; |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 333 | } |
| 334 | if (FD.BitfieldSize) { |
| 335 | P.Diag(AtLoc, diag::err_objc_property_bitfield) |
| 336 | << FD.D.getSourceRange(); |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 337 | return; |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // Install the property declarator into interfaceDecl. |
| 341 | IdentifierInfo *SelName = |
| 342 | OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier(); |
| 343 | |
| 344 | Selector GetterSel = |
| 345 | P.PP.getSelectorTable().getNullarySelector(SelName); |
| 346 | IdentifierInfo *SetterName = OCDS.getSetterName(); |
| 347 | Selector SetterSel; |
| 348 | if (SetterName) |
| 349 | SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName); |
| 350 | else |
Adrian Prantl | 80e8ea9 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 351 | SetterSel = |
| 352 | SelectorTable::constructSetterSelector(P.PP.getIdentifierTable(), |
| 353 | P.PP.getSelectorTable(), |
| 354 | FD.D.getIdentifier()); |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 355 | bool isOverridingProperty = false; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 356 | Decl *Property = |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 357 | P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc, |
| 358 | FD, OCDS, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 359 | GetterSel, SetterSel, |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 360 | &isOverridingProperty, |
| 361 | MethodImplKind); |
| 362 | if (!isOverridingProperty) |
| 363 | Props.push_back(Property); |
| 364 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 365 | FD.complete(Property); |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 366 | } |
| 367 | }; |
| 368 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 369 | void Parser::ObjCPropertyCallback::anchor() { |
| 370 | } |
| 371 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 372 | /// objc-interface-decl-list: |
| 373 | /// empty |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 374 | /// objc-interface-decl-list objc-property-decl [OBJC2] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 375 | /// objc-interface-decl-list objc-method-requirement [OBJC2] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 376 | /// objc-interface-decl-list objc-method-proto ';' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 377 | /// objc-interface-decl-list declaration |
| 378 | /// objc-interface-decl-list ';' |
| 379 | /// |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 380 | /// objc-method-requirement: [OBJC2] |
| 381 | /// @required |
| 382 | /// @optional |
| 383 | /// |
Fariborz Jahanian | 2f64cfe | 2011-08-22 21:44:58 +0000 | [diff] [blame] | 384 | void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey, |
| 385 | Decl *CDecl) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 386 | SmallVector<Decl *, 32> allMethods; |
| 387 | SmallVector<Decl *, 16> allProperties; |
| 388 | SmallVector<DeclGroupPtrTy, 8> allTUVariables; |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 389 | tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 391 | SourceRange AtEnd; |
Fariborz Jahanian | 2f64cfe | 2011-08-22 21:44:58 +0000 | [diff] [blame] | 392 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 393 | while (1) { |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 394 | // If this is a method prototype, parse it. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 395 | if (Tok.is(tok::minus) || Tok.is(tok::plus)) { |
Fariborz Jahanian | d30ec70 | 2012-07-26 17:32:28 +0000 | [diff] [blame] | 396 | if (Decl *methodPrototype = |
| 397 | ParseObjCMethodPrototype(MethodImplKind, false)) |
| 398 | allMethods.push_back(methodPrototype); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 399 | // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for |
| 400 | // method definitions. |
Argyrios Kyrtzidis | 0db9f4d | 2011-12-17 04:13:22 +0000 | [diff] [blame] | 401 | if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) { |
| 402 | // We didn't find a semi and we error'ed out. Skip until a ';' or '@'. |
| 403 | SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 404 | if (Tok.is(tok::semi)) |
| 405 | ConsumeToken(); |
| 406 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 407 | continue; |
| 408 | } |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 409 | if (Tok.is(tok::l_paren)) { |
| 410 | Diag(Tok, diag::err_expected_minus_or_plus); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 411 | ParseObjCMethodDecl(Tok.getLocation(), |
| 412 | tok::minus, |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 413 | MethodImplKind, false); |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 414 | continue; |
| 415 | } |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 416 | // Ignore excess semicolons. |
| 417 | if (Tok.is(tok::semi)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 418 | ConsumeToken(); |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 419 | continue; |
| 420 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 422 | // If we got to the end of the file, exit the loop. |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 423 | if (Tok.is(tok::eof)) |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 424 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 426 | // Code completion within an Objective-C interface. |
| 427 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 428 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 429 | CurParsedObjCImpl? Sema::PCC_ObjCImplementation |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 430 | : Sema::PCC_ObjCInterface); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 431 | return cutOffParsing(); |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 434 | // If we don't have an @ directive, parse it as a function definition. |
| 435 | if (Tok.isNot(tok::at)) { |
Chris Lattner | 1fd8011 | 2009-01-09 04:34:13 +0000 | [diff] [blame] | 436 | // The code below does not consume '}'s because it is afraid of eating the |
| 437 | // end of a namespace. Because of the way this code is structured, an |
| 438 | // erroneous r_brace would cause an infinite loop if not handled here. |
| 439 | if (Tok.is(tok::r_brace)) |
| 440 | break; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 441 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 442 | allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs)); |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 443 | continue; |
| 444 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 446 | // Otherwise, we have an @ directive, eat the @. |
| 447 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 448 | if (Tok.is(tok::code_completion)) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 449 | Actions.CodeCompleteObjCAtDirective(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 450 | return cutOffParsing(); |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 453 | tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 455 | if (DirectiveKind == tok::objc_end) { // @end -> terminate list |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 456 | AtEnd.setBegin(AtLoc); |
| 457 | AtEnd.setEnd(Tok.getLocation()); |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 458 | break; |
Douglas Gregor | c3d43b7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 459 | } else if (DirectiveKind == tok::objc_not_keyword) { |
| 460 | Diag(Tok, diag::err_objc_unknown_at); |
| 461 | SkipUntil(tok::semi); |
| 462 | continue; |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 465 | // Eat the identifier. |
| 466 | ConsumeToken(); |
| 467 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 468 | switch (DirectiveKind) { |
| 469 | default: |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 470 | // FIXME: If someone forgets an @end on a protocol, this loop will |
| 471 | // continue to eat up tons of stuff and spew lots of nonsense errors. It |
| 472 | // would probably be better to bail out if we saw an @class or @interface |
| 473 | // or something like that. |
Chris Lattner | f6ed855 | 2008-10-20 07:22:18 +0000 | [diff] [blame] | 474 | Diag(AtLoc, diag::err_objc_illegal_interface_qual); |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 475 | // Skip until we see an '@' or '}' or ';'. |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 476 | SkipUntil(tok::r_brace, tok::at); |
| 477 | break; |
Fariborz Jahanian | 46d545e | 2010-11-02 00:44:43 +0000 | [diff] [blame] | 478 | |
| 479 | case tok::objc_implementation: |
Fariborz Jahanian | df81c2c | 2010-11-09 20:38:00 +0000 | [diff] [blame] | 480 | case tok::objc_interface: |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 481 | Diag(AtLoc, diag::err_objc_missing_end) |
| 482 | << FixItHint::CreateInsertion(AtLoc, "@end\n"); |
| 483 | Diag(CDecl->getLocStart(), diag::note_objc_container_start) |
| 484 | << (int) Actions.getObjCContainerKind(); |
Fariborz Jahanian | 46d545e | 2010-11-02 00:44:43 +0000 | [diff] [blame] | 485 | ConsumeToken(); |
| 486 | break; |
| 487 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 488 | case tok::objc_required: |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 489 | case tok::objc_optional: |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 490 | // This is only valid on protocols. |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 491 | // FIXME: Should this check for ObjC2 being enabled? |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 492 | if (contextKey != tok::objc_protocol) |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 493 | Diag(AtLoc, diag::err_objc_directive_only_in_protocol); |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 494 | else |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 495 | MethodImplKind = DirectiveKind; |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 496 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 498 | case tok::objc_property: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 499 | if (!getLangOpts().ObjC2) |
Chris Lattner | b321c0c | 2010-12-17 05:40:22 +0000 | [diff] [blame] | 500 | Diag(AtLoc, diag::err_objc_properties_require_objc2); |
Chris Lattner | f6ed855 | 2008-10-20 07:22:18 +0000 | [diff] [blame] | 501 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 502 | ObjCDeclSpec OCDS; |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 503 | SourceLocation LParenLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | // Parse property attribute list, if any. |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 505 | if (Tok.is(tok::l_paren)) { |
| 506 | LParenLoc = Tok.getLocation(); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 507 | ParseObjCPropertyAttribute(OCDS); |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 508 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 510 | ObjCPropertyCallback Callback(*this, allProperties, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 511 | OCDS, AtLoc, LParenLoc, MethodImplKind); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 512 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 513 | // Parse all the comma separated declarators. |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 514 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 515 | ParseStructDeclaration(DS, Callback); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
John McCall | 7da19ea | 2011-03-26 01:53:26 +0000 | [diff] [blame] | 517 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 518 | break; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 519 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 520 | } |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 521 | |
| 522 | // We break out of the big loop in two cases: when we see @end or when we see |
| 523 | // EOF. In the former case, eat the @end. In the later case, emit an error. |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 524 | if (Tok.is(tok::code_completion)) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 525 | Actions.CodeCompleteObjCAtDirective(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 526 | return cutOffParsing(); |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 527 | } else if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 528 | ConsumeToken(); // the "end" identifier |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 529 | } else { |
| 530 | Diag(Tok, diag::err_objc_missing_end) |
| 531 | << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n"); |
| 532 | Diag(CDecl->getLocStart(), diag::note_objc_container_start) |
| 533 | << (int) Actions.getObjCContainerKind(); |
| 534 | AtEnd.setBegin(Tok.getLocation()); |
| 535 | AtEnd.setEnd(Tok.getLocation()); |
| 536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 538 | // Insert collected methods declarations into the @interface object. |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 539 | // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 540 | Actions.ActOnAtEnd(getCurScope(), AtEnd, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | allMethods.data(), allMethods.size(), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 542 | allProperties.data(), allProperties.size(), |
| 543 | allTUVariables.data(), allTUVariables.size()); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 546 | /// Parse property attribute declarations. |
| 547 | /// |
| 548 | /// property-attr-decl: '(' property-attrlist ')' |
| 549 | /// property-attrlist: |
| 550 | /// property-attribute |
| 551 | /// property-attrlist ',' property-attribute |
| 552 | /// property-attribute: |
| 553 | /// getter '=' identifier |
| 554 | /// setter '=' identifier ':' |
| 555 | /// readonly |
| 556 | /// readwrite |
| 557 | /// assign |
| 558 | /// retain |
| 559 | /// copy |
| 560 | /// nonatomic |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 561 | /// atomic |
| 562 | /// strong |
| 563 | /// weak |
| 564 | /// unsafe_unretained |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 565 | /// |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 566 | void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) { |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 567 | assert(Tok.getKind() == tok::l_paren); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 568 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 569 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Chris Lattner | cd9f4b3 | 2008-10-20 07:15:22 +0000 | [diff] [blame] | 571 | while (1) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 572 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 573 | Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 574 | return cutOffParsing(); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 575 | } |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 576 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Chris Lattner | f6ed855 | 2008-10-20 07:22:18 +0000 | [diff] [blame] | 578 | // If this is not an identifier at all, bail out early. |
| 579 | if (II == 0) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 580 | T.consumeClose(); |
Chris Lattner | f6ed855 | 2008-10-20 07:22:18 +0000 | [diff] [blame] | 581 | return; |
| 582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 584 | SourceLocation AttrName = ConsumeToken(); // consume last attribute name |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 586 | if (II->isStr("readonly")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 587 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 588 | else if (II->isStr("assign")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 589 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 590 | else if (II->isStr("unsafe_unretained")) |
| 591 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 592 | else if (II->isStr("readwrite")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 593 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 594 | else if (II->isStr("retain")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 595 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 596 | else if (II->isStr("strong")) |
| 597 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 598 | else if (II->isStr("copy")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 599 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 600 | else if (II->isStr("nonatomic")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 601 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic); |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 602 | else if (II->isStr("atomic")) |
| 603 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 604 | else if (II->isStr("weak")) |
| 605 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 606 | else if (II->isStr("getter") || II->isStr("setter")) { |
Anders Carlsson | 42499be | 2010-10-02 17:45:21 +0000 | [diff] [blame] | 607 | bool IsSetter = II->getNameStart()[0] == 's'; |
| 608 | |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 609 | // getter/setter require extra treatment. |
Anders Carlsson | 42499be | 2010-10-02 17:45:21 +0000 | [diff] [blame] | 610 | unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter : |
| 611 | diag::err_objc_expected_equal_for_getter; |
| 612 | |
| 613 | if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren)) |
Chris Lattner | dd5b5f2 | 2008-10-20 07:00:43 +0000 | [diff] [blame] | 614 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 616 | if (Tok.is(tok::code_completion)) { |
Anders Carlsson | 42499be | 2010-10-02 17:45:21 +0000 | [diff] [blame] | 617 | if (IsSetter) |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 618 | Actions.CodeCompleteObjCPropertySetter(getCurScope()); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 619 | else |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 620 | Actions.CodeCompleteObjCPropertyGetter(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 621 | return cutOffParsing(); |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Anders Carlsson | 42499be | 2010-10-02 17:45:21 +0000 | [diff] [blame] | 624 | |
| 625 | SourceLocation SelLoc; |
| 626 | IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc); |
| 627 | |
| 628 | if (!SelIdent) { |
| 629 | Diag(Tok, diag::err_objc_expected_selector_for_getter_setter) |
| 630 | << IsSetter; |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 631 | SkipUntil(tok::r_paren); |
| 632 | return; |
| 633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Anders Carlsson | 42499be | 2010-10-02 17:45:21 +0000 | [diff] [blame] | 635 | if (IsSetter) { |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 636 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter); |
Anders Carlsson | 42499be | 2010-10-02 17:45:21 +0000 | [diff] [blame] | 637 | DS.setSetterName(SelIdent); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Fariborz Jahanian | e0097db | 2010-02-15 22:20:11 +0000 | [diff] [blame] | 639 | if (ExpectAndConsume(tok::colon, |
| 640 | diag::err_expected_colon_after_setter_name, "", |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 641 | tok::r_paren)) |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 642 | return; |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 643 | } else { |
| 644 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter); |
Anders Carlsson | 42499be | 2010-10-02 17:45:21 +0000 | [diff] [blame] | 645 | DS.setGetterName(SelIdent); |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 646 | } |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 647 | } else { |
Chris Lattner | a9500f0 | 2008-11-19 07:49:38 +0000 | [diff] [blame] | 648 | Diag(AttrName, diag::err_objc_expected_property_attr) << II; |
Chris Lattner | cd9f4b3 | 2008-10-20 07:15:22 +0000 | [diff] [blame] | 649 | SkipUntil(tok::r_paren); |
| 650 | return; |
Chris Lattner | cd9f4b3 | 2008-10-20 07:15:22 +0000 | [diff] [blame] | 651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 653 | if (Tok.isNot(tok::comma)) |
| 654 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 656 | ConsumeToken(); |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 659 | T.consumeClose(); |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 662 | /// objc-method-proto: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | /// objc-instance-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 664 | /// objc-class-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 665 | /// |
| 666 | /// objc-instance-method: '-' |
| 667 | /// objc-class-method: '+' |
| 668 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 669 | /// objc-method-attributes: [OBJC2] |
| 670 | /// __attribute__((deprecated)) |
| 671 | /// |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 672 | Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind, |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 673 | bool MethodDefinition) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 674 | assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 675 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | tok::TokenKind methodType = Tok.getKind(); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 677 | SourceLocation mLoc = ConsumeToken(); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 678 | Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind, |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 679 | MethodDefinition); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 680 | // Since this rule is used for both method declarations and definitions, |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 681 | // the caller is (optionally) responsible for consuming the ';'. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 682 | return MDecl; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /// objc-selector: |
| 686 | /// identifier |
| 687 | /// one of |
| 688 | /// enum struct union if else while do for switch case default |
| 689 | /// break continue return goto asm sizeof typeof __alignof |
| 690 | /// unsigned long const short volatile signed restrict _Complex |
| 691 | /// in out inout bycopy byref oneway int char float double void _Bool |
| 692 | /// |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 693 | IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) { |
Fariborz Jahanian | be74740 | 2010-09-03 01:26:16 +0000 | [diff] [blame] | 694 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 695 | switch (Tok.getKind()) { |
| 696 | default: |
| 697 | return 0; |
Fariborz Jahanian | afbc681 | 2010-09-03 17:33:04 +0000 | [diff] [blame] | 698 | case tok::ampamp: |
| 699 | case tok::ampequal: |
| 700 | case tok::amp: |
| 701 | case tok::pipe: |
| 702 | case tok::tilde: |
| 703 | case tok::exclaim: |
| 704 | case tok::exclaimequal: |
| 705 | case tok::pipepipe: |
| 706 | case tok::pipeequal: |
| 707 | case tok::caret: |
| 708 | case tok::caretequal: { |
Fariborz Jahanian | 3846ca2 | 2010-09-03 18:01:09 +0000 | [diff] [blame] | 709 | std::string ThisTok(PP.getSpelling(Tok)); |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 710 | if (isLetter(ThisTok[0])) { |
Fariborz Jahanian | afbc681 | 2010-09-03 17:33:04 +0000 | [diff] [blame] | 711 | IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data()); |
| 712 | Tok.setKind(tok::identifier); |
| 713 | SelectorLoc = ConsumeToken(); |
| 714 | return II; |
| 715 | } |
| 716 | return 0; |
| 717 | } |
| 718 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 719 | case tok::identifier: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 720 | case tok::kw_asm: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 721 | case tok::kw_auto: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 722 | case tok::kw_bool: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 723 | case tok::kw_break: |
| 724 | case tok::kw_case: |
| 725 | case tok::kw_catch: |
| 726 | case tok::kw_char: |
| 727 | case tok::kw_class: |
| 728 | case tok::kw_const: |
| 729 | case tok::kw_const_cast: |
| 730 | case tok::kw_continue: |
| 731 | case tok::kw_default: |
| 732 | case tok::kw_delete: |
| 733 | case tok::kw_do: |
| 734 | case tok::kw_double: |
| 735 | case tok::kw_dynamic_cast: |
| 736 | case tok::kw_else: |
| 737 | case tok::kw_enum: |
| 738 | case tok::kw_explicit: |
| 739 | case tok::kw_export: |
| 740 | case tok::kw_extern: |
| 741 | case tok::kw_false: |
| 742 | case tok::kw_float: |
| 743 | case tok::kw_for: |
| 744 | case tok::kw_friend: |
| 745 | case tok::kw_goto: |
| 746 | case tok::kw_if: |
| 747 | case tok::kw_inline: |
| 748 | case tok::kw_int: |
| 749 | case tok::kw_long: |
| 750 | case tok::kw_mutable: |
| 751 | case tok::kw_namespace: |
| 752 | case tok::kw_new: |
| 753 | case tok::kw_operator: |
| 754 | case tok::kw_private: |
| 755 | case tok::kw_protected: |
| 756 | case tok::kw_public: |
| 757 | case tok::kw_register: |
| 758 | case tok::kw_reinterpret_cast: |
| 759 | case tok::kw_restrict: |
| 760 | case tok::kw_return: |
| 761 | case tok::kw_short: |
| 762 | case tok::kw_signed: |
| 763 | case tok::kw_sizeof: |
| 764 | case tok::kw_static: |
| 765 | case tok::kw_static_cast: |
| 766 | case tok::kw_struct: |
| 767 | case tok::kw_switch: |
| 768 | case tok::kw_template: |
| 769 | case tok::kw_this: |
| 770 | case tok::kw_throw: |
| 771 | case tok::kw_true: |
| 772 | case tok::kw_try: |
| 773 | case tok::kw_typedef: |
| 774 | case tok::kw_typeid: |
| 775 | case tok::kw_typename: |
| 776 | case tok::kw_typeof: |
| 777 | case tok::kw_union: |
| 778 | case tok::kw_unsigned: |
| 779 | case tok::kw_using: |
| 780 | case tok::kw_virtual: |
| 781 | case tok::kw_void: |
| 782 | case tok::kw_volatile: |
| 783 | case tok::kw_wchar_t: |
| 784 | case tok::kw_while: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 785 | case tok::kw__Bool: |
| 786 | case tok::kw__Complex: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 787 | case tok::kw___alignof: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 788 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 789 | SelectorLoc = ConsumeToken(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 790 | return II; |
Fariborz Jahanian | d064951 | 2007-09-27 19:52:15 +0000 | [diff] [blame] | 791 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 794 | /// objc-for-collection-in: 'in' |
| 795 | /// |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 796 | bool Parser::isTokIdentifier_in() const { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 797 | // FIXME: May have to do additional look-ahead to only allow for |
| 798 | // valid tokens following an 'in'; such as an identifier, unary operators, |
| 799 | // '[' etc. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 800 | return (getLangOpts().ObjC2 && Tok.is(tok::identifier) && |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 801 | Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]); |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 804 | /// ParseObjCTypeQualifierList - This routine parses the objective-c's type |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 805 | /// qualifier list and builds their bitmask representation in the input |
| 806 | /// argument. |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 807 | /// |
| 808 | /// objc-type-qualifiers: |
| 809 | /// objc-type-qualifier |
| 810 | /// objc-type-qualifiers objc-type-qualifier |
| 811 | /// |
Douglas Gregor | b77cab9 | 2011-03-08 19:17:54 +0000 | [diff] [blame] | 812 | void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 813 | Declarator::TheContext Context) { |
| 814 | assert(Context == Declarator::ObjCParameterContext || |
| 815 | Context == Declarator::ObjCResultContext); |
| 816 | |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 817 | while (1) { |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 818 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | b77cab9 | 2011-03-08 19:17:54 +0000 | [diff] [blame] | 819 | Actions.CodeCompleteObjCPassingType(getCurScope(), DS, |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 820 | Context == Declarator::ObjCParameterContext); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 821 | return cutOffParsing(); |
Douglas Gregor | d32b022 | 2010-08-24 01:06:58 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 824 | if (Tok.isNot(tok::identifier)) |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 825 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 827 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 828 | for (unsigned i = 0; i != objc_NumQuals; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 829 | if (II != ObjCTypeQuals[i]) |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 830 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 832 | ObjCDeclSpec::ObjCDeclQualifier Qual; |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 833 | switch (i) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 834 | default: llvm_unreachable("Unknown decl qualifier"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 835 | case objc_in: Qual = ObjCDeclSpec::DQ_In; break; |
| 836 | case objc_out: Qual = ObjCDeclSpec::DQ_Out; break; |
| 837 | case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break; |
| 838 | case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break; |
| 839 | case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break; |
| 840 | case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break; |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 841 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 842 | DS.setObjCDeclQualifier(Qual); |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 843 | ConsumeToken(); |
| 844 | II = 0; |
| 845 | break; |
| 846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 848 | // If this wasn't a recognized qualifier, bail out. |
| 849 | if (II) return; |
| 850 | } |
| 851 | } |
| 852 | |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 853 | /// Take all the decl attributes out of the given list and add |
| 854 | /// them to the given attribute set. |
| 855 | static void takeDeclAttributes(ParsedAttributes &attrs, |
| 856 | AttributeList *list) { |
| 857 | while (list) { |
| 858 | AttributeList *cur = list; |
| 859 | list = cur->getNext(); |
| 860 | |
| 861 | if (!cur->isUsedAsTypeAttr()) { |
| 862 | // Clear out the next pointer. We're really completely |
| 863 | // destroying the internal invariants of the declarator here, |
| 864 | // but it doesn't matter because we're done with it. |
| 865 | cur->setNext(0); |
| 866 | attrs.add(cur); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | /// takeDeclAttributes - Take all the decl attributes from the given |
| 872 | /// declarator and add them to the given list. |
| 873 | static void takeDeclAttributes(ParsedAttributes &attrs, |
| 874 | Declarator &D) { |
| 875 | // First, take ownership of all attributes. |
| 876 | attrs.getPool().takeAllFrom(D.getAttributePool()); |
| 877 | attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool()); |
| 878 | |
| 879 | // Now actually move the attributes over. |
| 880 | takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList()); |
| 881 | takeDeclAttributes(attrs, D.getAttributes()); |
| 882 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 883 | takeDeclAttributes(attrs, |
| 884 | const_cast<AttributeList*>(D.getTypeObject(i).getAttrs())); |
| 885 | } |
| 886 | |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 887 | /// objc-type-name: |
| 888 | /// '(' objc-type-qualifiers[opt] type-name ')' |
| 889 | /// '(' objc-type-qualifiers[opt] ')' |
| 890 | /// |
Douglas Gregor | b77cab9 | 2011-03-08 19:17:54 +0000 | [diff] [blame] | 891 | ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 892 | Declarator::TheContext context, |
| 893 | ParsedAttributes *paramAttrs) { |
| 894 | assert(context == Declarator::ObjCParameterContext || |
| 895 | context == Declarator::ObjCResultContext); |
| 896 | assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext)); |
| 897 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 898 | assert(Tok.is(tok::l_paren) && "expected ("); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 900 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 901 | T.consumeOpen(); |
| 902 | |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 903 | SourceLocation TypeStartLoc = Tok.getLocation(); |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 904 | ObjCDeclContextSwitch ObjCDC(*this); |
| 905 | |
Fariborz Jahanian | 19d74e1 | 2007-10-31 21:59:43 +0000 | [diff] [blame] | 906 | // Parse type qualifiers, in, inout, etc. |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 907 | ParseObjCTypeQualifierList(DS, context); |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 908 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 909 | ParsedType Ty; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 910 | if (isTypeSpecifierQualifier()) { |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 911 | // Parse an abstract declarator. |
| 912 | DeclSpec declSpec(AttrFactory); |
| 913 | declSpec.setObjCQualifiers(&DS); |
| 914 | ParseSpecifierQualifierList(declSpec); |
Fariborz Jahanian | 6d1de1b | 2012-05-29 21:52:45 +0000 | [diff] [blame] | 915 | declSpec.SetRangeEnd(Tok.getLocation()); |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 916 | Declarator declarator(declSpec, context); |
| 917 | ParseDeclarator(declarator); |
| 918 | |
| 919 | // If that's not invalid, extract a type. |
| 920 | if (!declarator.isInvalidType()) { |
| 921 | TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator); |
| 922 | if (!type.isInvalid()) |
| 923 | Ty = type.get(); |
| 924 | |
| 925 | // If we're parsing a parameter, steal all the decl attributes |
| 926 | // and add them to the decl spec. |
| 927 | if (context == Declarator::ObjCParameterContext) |
| 928 | takeDeclAttributes(*paramAttrs, declarator); |
| 929 | } |
| 930 | } else if (context == Declarator::ObjCResultContext && |
| 931 | Tok.is(tok::identifier)) { |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 932 | if (!Ident_instancetype) |
| 933 | Ident_instancetype = PP.getIdentifierInfo("instancetype"); |
| 934 | |
| 935 | if (Tok.getIdentifierInfo() == Ident_instancetype) { |
| 936 | Ty = Actions.ActOnObjCInstanceType(Tok.getLocation()); |
| 937 | ConsumeToken(); |
| 938 | } |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 939 | } |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 940 | |
Steve Naroff | d7333c2 | 2008-10-21 14:15:04 +0000 | [diff] [blame] | 941 | if (Tok.is(tok::r_paren)) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 942 | T.consumeClose(); |
Chris Lattner | 4a76b29 | 2008-10-22 03:52:06 +0000 | [diff] [blame] | 943 | else if (Tok.getLocation() == TypeStartLoc) { |
| 944 | // If we didn't eat any tokens, then this isn't a type. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 945 | Diag(Tok, diag::err_expected_type); |
Chris Lattner | 4a76b29 | 2008-10-22 03:52:06 +0000 | [diff] [blame] | 946 | SkipUntil(tok::r_paren); |
| 947 | } else { |
| 948 | // Otherwise, we found *something*, but didn't get a ')' in the right |
| 949 | // place. Emit an error then return what we have as the type. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 950 | T.consumeClose(); |
Chris Lattner | 4a76b29 | 2008-10-22 03:52:06 +0000 | [diff] [blame] | 951 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 952 | return Ty; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /// objc-method-decl: |
| 956 | /// objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 957 | /// objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 958 | /// objc-type-name objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 959 | /// objc-type-name objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 960 | /// |
| 961 | /// objc-keyword-selector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | /// objc-keyword-decl |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 963 | /// objc-keyword-selector objc-keyword-decl |
| 964 | /// |
| 965 | /// objc-keyword-decl: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 966 | /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 967 | /// objc-selector ':' objc-keyword-attributes[opt] identifier |
| 968 | /// ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 969 | /// ':' objc-keyword-attributes[opt] identifier |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 970 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 971 | /// objc-parmlist: |
| 972 | /// objc-parms objc-ellipsis[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 973 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 974 | /// objc-parms: |
| 975 | /// objc-parms , parameter-declaration |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 976 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 977 | /// objc-ellipsis: |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 978 | /// , ... |
| 979 | /// |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 980 | /// objc-keyword-attributes: [OBJC2] |
| 981 | /// __attribute__((unused)) |
| 982 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 983 | Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 984 | tok::TokenKind mType, |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 985 | tok::ObjCKeywordKind MethodImplKind, |
| 986 | bool MethodDefinition) { |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 987 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 988 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 989 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 990 | Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 991 | /*ReturnType=*/ ParsedType()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 992 | cutOffParsing(); |
| 993 | return 0; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 996 | // Parse the return type if present. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 997 | ParsedType ReturnType; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 998 | ObjCDeclSpec DSRet; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 999 | if (Tok.is(tok::l_paren)) |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 1000 | ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Ted Kremenek | 9e04935 | 2010-02-18 23:05:16 +0000 | [diff] [blame] | 1002 | // If attributes exist before the method, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1003 | ParsedAttributes methodAttrs(AttrFactory); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1004 | if (getLangOpts().ObjC2) |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1005 | MaybeParseGNUAttributes(methodAttrs); |
Ted Kremenek | 9e04935 | 2010-02-18 23:05:16 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 1007 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1008 | Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1009 | ReturnType); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1010 | cutOffParsing(); |
| 1011 | return 0; |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Ted Kremenek | 9e04935 | 2010-02-18 23:05:16 +0000 | [diff] [blame] | 1014 | // Now parse the selector. |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1015 | SourceLocation selLoc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 1016 | IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc); |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 1017 | |
Steve Naroff | 84c4310 | 2009-02-11 20:43:13 +0000 | [diff] [blame] | 1018 | // An unnamed colon is valid. |
| 1019 | if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1020 | Diag(Tok, diag::err_expected_selector_for_method) |
| 1021 | << SourceRange(mLoc, Tok.getLocation()); |
Fariborz Jahanian | d30ec70 | 2012-07-26 17:32:28 +0000 | [diff] [blame] | 1022 | // Skip until we get a ; or @. |
| 1023 | SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1024 | return 0; |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 1025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1027 | SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1028 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1029 | // If attributes exist after the method, parse them. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1030 | if (getLangOpts().ObjC2) |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1031 | MaybeParseGNUAttributes(methodAttrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1033 | Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1034 | Decl *Result |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1035 | = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(), |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1036 | mType, DSRet, ReturnType, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1037 | selLoc, Sel, 0, |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1038 | CParamInfo.data(), CParamInfo.size(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1039 | methodAttrs.getList(), MethodImplKind, |
| 1040 | false, MethodDefinition); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1041 | PD.complete(Result); |
| 1042 | return Result; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1043 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1045 | SmallVector<IdentifierInfo *, 12> KeyIdents; |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 1046 | SmallVector<SourceLocation, 12> KeyLocs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1047 | SmallVector<Sema::ObjCArgInfo, 12> ArgInfos; |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1048 | ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | |
| 1049 | Scope::FunctionDeclarationScope | Scope::DeclScope); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1050 | |
| 1051 | AttributePool allParamAttrs(AttrFactory); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1052 | while (1) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1053 | ParsedAttributes paramAttrs(AttrFactory); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1054 | Sema::ObjCArgInfo ArgInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1056 | // Each iteration parses a single keyword argument. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1057 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1058 | Diag(Tok, diag::err_expected_colon); |
| 1059 | break; |
| 1060 | } |
| 1061 | ConsumeToken(); // Eat the ':'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1063 | ArgInfo.Type = ParsedType(); |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1064 | if (Tok.is(tok::l_paren)) // Parse the argument type if present. |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 1065 | ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, |
| 1066 | Declarator::ObjCParameterContext, |
| 1067 | ¶mAttrs); |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1068 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1069 | // If attributes exist before the argument name, parse them. |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 1070 | // Regardless, collect all the attributes we've parsed so far. |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1071 | ArgInfo.ArgAttrs = 0; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1072 | if (getLangOpts().ObjC2) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1073 | MaybeParseGNUAttributes(paramAttrs); |
| 1074 | ArgInfo.ArgAttrs = paramAttrs.getList(); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1075 | } |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1076 | |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 1077 | // Code completion for the next piece of the selector. |
| 1078 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 1079 | KeyIdents.push_back(SelIdent); |
| 1080 | Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(), |
| 1081 | mType == tok::minus, |
| 1082 | /*AtParameterName=*/true, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 1083 | ReturnType, KeyIdents); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1084 | cutOffParsing(); |
| 1085 | return 0; |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1088 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1089 | Diag(Tok, diag::err_expected_ident); // missing argument name. |
| 1090 | break; |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 1091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1093 | ArgInfo.Name = Tok.getIdentifierInfo(); |
| 1094 | ArgInfo.NameLoc = Tok.getLocation(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1095 | ConsumeToken(); // Eat the identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1097 | ArgInfos.push_back(ArgInfo); |
| 1098 | KeyIdents.push_back(SelIdent); |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 1099 | KeyLocs.push_back(selLoc); |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1100 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1101 | // Make sure the attributes persist. |
| 1102 | allParamAttrs.takeAllFrom(paramAttrs.getPool()); |
| 1103 | |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 1104 | // Code completion for the next piece of the selector. |
| 1105 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 1106 | Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(), |
| 1107 | mType == tok::minus, |
Douglas Gregor | 40ed9a1 | 2010-07-08 23:37:41 +0000 | [diff] [blame] | 1108 | /*AtParameterName=*/false, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 1109 | ReturnType, KeyIdents); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1110 | cutOffParsing(); |
| 1111 | return 0; |
Douglas Gregor | 1f5537a | 2010-07-08 23:20:03 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1114 | // Check for another keyword selector. |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 1115 | SelIdent = ParseObjCSelectorPiece(selLoc); |
Ted Kremenek | b648cf0 | 2012-09-12 16:50:35 +0000 | [diff] [blame] | 1116 | if (!SelIdent && Tok.isNot(tok::colon)) |
| 1117 | break; |
Fariborz Jahanian | 03ebd3b | 2012-09-17 23:09:59 +0000 | [diff] [blame] | 1118 | if (!SelIdent) { |
Fariborz Jahanian | 0860235 | 2012-09-17 19:15:26 +0000 | [diff] [blame] | 1119 | SourceLocation ColonLoc = Tok.getLocation(); |
| 1120 | if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) { |
Fariborz Jahanian | 03ebd3b | 2012-09-17 23:09:59 +0000 | [diff] [blame] | 1121 | Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name; |
| 1122 | Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name; |
| 1123 | Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name; |
Fariborz Jahanian | 0860235 | 2012-09-17 19:15:26 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1126 | // We have a selector or a colon, continue parsing. |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 1127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 1129 | bool isVariadic = false; |
Fariborz Jahanian | 56242ba | 2012-06-21 18:43:08 +0000 | [diff] [blame] | 1130 | bool cStyleParamWarned = false; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1131 | // Parse the (optional) parameter list. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1132 | while (Tok.is(tok::comma)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1133 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1134 | if (Tok.is(tok::ellipsis)) { |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 1135 | isVariadic = true; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1136 | ConsumeToken(); |
| 1137 | break; |
| 1138 | } |
Fariborz Jahanian | 56242ba | 2012-06-21 18:43:08 +0000 | [diff] [blame] | 1139 | if (!cStyleParamWarned) { |
| 1140 | Diag(Tok, diag::warn_cstyle_param); |
| 1141 | cStyleParamWarned = true; |
| 1142 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1143 | DeclSpec DS(AttrFactory); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1144 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | // Parse the declarator. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1146 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1147 | ParseDeclarator(ParmDecl); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1148 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1149 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1150 | CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1151 | ParmDecl.getIdentifierLoc(), |
| 1152 | Param, |
| 1153 | 0)); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Cameron Esfahani | 9c4bb2c | 2010-10-12 00:21:25 +0000 | [diff] [blame] | 1156 | // FIXME: Add support for optional parameter list... |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 1157 | // If attributes exist after the method, parse them. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1158 | if (getLangOpts().ObjC2) |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1159 | MaybeParseGNUAttributes(methodAttrs); |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1160 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1161 | if (KeyIdents.size() == 0) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1162 | return 0; |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1164 | Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), |
| 1165 | &KeyIdents[0]); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1166 | Decl *Result |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1167 | = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(), |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1168 | mType, DSRet, ReturnType, |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 1169 | KeyLocs, Sel, &ArgInfos[0], |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1170 | CParamInfo.data(), CParamInfo.size(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1171 | methodAttrs.getList(), |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 1172 | MethodImplKind, isVariadic, MethodDefinition); |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1173 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1174 | PD.complete(Result); |
| 1175 | return Result; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1178 | /// objc-protocol-refs: |
| 1179 | /// '<' identifier-list '>' |
| 1180 | /// |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1181 | bool Parser:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1182 | ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols, |
| 1183 | SmallVectorImpl<SourceLocation> &ProtocolLocs, |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1184 | bool WarnOnDeclarations, |
| 1185 | SourceLocation &LAngleLoc, SourceLocation &EndLoc) { |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1186 | assert(Tok.is(tok::less) && "expected <"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1188 | LAngleLoc = ConsumeToken(); // the "<" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1190 | SmallVector<IdentifierLocPair, 8> ProtocolIdents; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1192 | while (1) { |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 1193 | if (Tok.is(tok::code_completion)) { |
| 1194 | Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(), |
| 1195 | ProtocolIdents.size()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1196 | cutOffParsing(); |
| 1197 | return true; |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1200 | if (Tok.isNot(tok::identifier)) { |
| 1201 | Diag(Tok, diag::err_expected_ident); |
| 1202 | SkipUntil(tok::greater); |
| 1203 | return true; |
| 1204 | } |
| 1205 | ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(), |
| 1206 | Tok.getLocation())); |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1207 | ProtocolLocs.push_back(Tok.getLocation()); |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1208 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1210 | if (Tok.isNot(tok::comma)) |
| 1211 | break; |
| 1212 | ConsumeToken(); |
| 1213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1215 | // Consume the '>'. |
Nico Weber | b707a47 | 2012-12-14 18:22:38 +0000 | [diff] [blame] | 1216 | if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true)) |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1217 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1219 | // Convert the list of protocols identifiers into a list of protocol decls. |
| 1220 | Actions.FindProtocolDeclaration(WarnOnDeclarations, |
| 1221 | &ProtocolIdents[0], ProtocolIdents.size(), |
| 1222 | Protocols); |
| 1223 | return false; |
| 1224 | } |
| 1225 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1226 | /// \brief Parse the Objective-C protocol qualifiers that follow a typename |
| 1227 | /// in a decl-specifier-seq, starting at the '<'. |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 1228 | bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) { |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1229 | assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'"); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1230 | assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C"); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1231 | SourceLocation LAngleLoc, EndProtoLoc; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1232 | SmallVector<Decl *, 8> ProtocolDecl; |
| 1233 | SmallVector<SourceLocation, 8> ProtocolLocs; |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 1234 | bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1235 | LAngleLoc, EndProtoLoc); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1236 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1237 | ProtocolLocs.data(), LAngleLoc); |
| 1238 | if (EndProtoLoc.isValid()) |
| 1239 | DS.SetRangeEnd(EndProtoLoc); |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 1240 | return Result; |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Fariborz Jahanian | 4cc0cf1 | 2013-03-20 18:09:33 +0000 | [diff] [blame] | 1243 | void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc, |
| 1244 | BalancedDelimiterTracker &T, |
| 1245 | SmallVectorImpl<Decl *> &AllIvarDecls, |
| 1246 | bool RBraceMissing) { |
| 1247 | if (!RBraceMissing) |
| 1248 | T.consumeClose(); |
| 1249 | |
| 1250 | Actions.ActOnObjCContainerStartDefinition(interfaceDecl); |
| 1251 | Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls); |
| 1252 | Actions.ActOnObjCContainerFinishDefinition(); |
| 1253 | // Call ActOnFields() even if we don't have any decls. This is useful |
| 1254 | // for code rewriting tools that need to be aware of the empty list. |
| 1255 | Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl, |
| 1256 | AllIvarDecls, |
| 1257 | T.getOpenLocation(), T.getCloseLocation(), 0); |
| 1258 | } |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1259 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1260 | /// objc-class-instance-variables: |
| 1261 | /// '{' objc-instance-variable-decl-list[opt] '}' |
| 1262 | /// |
| 1263 | /// objc-instance-variable-decl-list: |
| 1264 | /// objc-visibility-spec |
| 1265 | /// objc-instance-variable-decl ';' |
| 1266 | /// ';' |
| 1267 | /// objc-instance-variable-decl-list objc-visibility-spec |
| 1268 | /// objc-instance-variable-decl-list objc-instance-variable-decl ';' |
| 1269 | /// objc-instance-variable-decl-list ';' |
| 1270 | /// |
| 1271 | /// objc-visibility-spec: |
| 1272 | /// @private |
| 1273 | /// @protected |
| 1274 | /// @public |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1275 | /// @package [OBJC2] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1276 | /// |
| 1277 | /// objc-instance-variable-decl: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | /// struct-declaration |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1279 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1280 | void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl, |
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 1281 | tok::ObjCKeywordKind visibility, |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1282 | SourceLocation atLoc) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1283 | assert(Tok.is(tok::l_brace) && "expected {"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1284 | SmallVector<Decl *, 32> AllIvarDecls; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1285 | |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1286 | ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1287 | ObjCDeclContextSwitch ObjCDC(*this); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1288 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1289 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 1290 | T.consumeOpen(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1291 | // While we still have something to read, read the instance variables. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1292 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1293 | // Each iteration of this loop reads one objc-instance-variable-decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1295 | // Check for extraneous top-level semicolon. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1296 | if (Tok.is(tok::semi)) { |
Richard Trieu | 4b0e6f1 | 2012-05-16 19:04:59 +0000 | [diff] [blame] | 1297 | ConsumeExtraSemi(InstanceVariableList); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1298 | continue; |
| 1299 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1301 | // Set the default visibility to private. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1302 | if (Tok.is(tok::at)) { // parse objc-visibility-spec |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1303 | ConsumeToken(); // eat the @ sign |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1306 | Actions.CodeCompleteObjCAtVisibility(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1307 | return cutOffParsing(); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1310 | switch (Tok.getObjCKeywordID()) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1311 | case tok::objc_private: |
| 1312 | case tok::objc_public: |
| 1313 | case tok::objc_protected: |
| 1314 | case tok::objc_package: |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1315 | visibility = Tok.getObjCKeywordID(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1316 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | continue; |
Fariborz Jahanian | 695031c | 2013-03-20 18:45:49 +0000 | [diff] [blame] | 1318 | |
| 1319 | case tok::objc_end: |
| 1320 | Diag(Tok, diag::err_objc_unexpected_atend); |
Fariborz Jahanian | 4cc0cf1 | 2013-03-20 18:09:33 +0000 | [diff] [blame] | 1321 | Tok.setLocation(Tok.getLocation().getLocWithOffset(-1)); |
| 1322 | Tok.setKind(tok::at); |
| 1323 | Tok.setLength(1); |
| 1324 | PP.EnterToken(Tok); |
| 1325 | HelperActionsForIvarDeclarations(interfaceDecl, atLoc, |
| 1326 | T, AllIvarDecls, true); |
| 1327 | return; |
Fariborz Jahanian | 695031c | 2013-03-20 18:45:49 +0000 | [diff] [blame] | 1328 | |
| 1329 | default: |
| 1330 | Diag(Tok, diag::err_objc_illegal_visibility_spec); |
| 1331 | continue; |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1332 | } |
| 1333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1335 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1336 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1337 | Sema::PCC_ObjCInstanceVariableList); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1338 | return cutOffParsing(); |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1341 | struct ObjCIvarCallback : FieldCallback { |
| 1342 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1343 | Decl *IDecl; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1344 | tok::ObjCKeywordKind visibility; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1345 | SmallVectorImpl<Decl *> &AllIvarDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1346 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1347 | ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1348 | SmallVectorImpl<Decl *> &AllIvarDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1349 | P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) { |
| 1350 | } |
| 1351 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 1352 | void invoke(ParsingFieldDeclarator &FD) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1353 | P.Actions.ActOnObjCContainerStartDefinition(IDecl); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1354 | // Install the declarator into the interface decl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1355 | Decl *Field |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1356 | = P.Actions.ActOnIvar(P.getCurScope(), |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1357 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1358 | FD.D, FD.BitfieldSize, visibility); |
Fariborz Jahanian | 10af879 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 1359 | P.Actions.ActOnObjCContainerFinishDefinition(); |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 1360 | if (Field) |
| 1361 | AllIvarDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 1362 | FD.complete(Field); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } Callback(*this, interfaceDecl, visibility, AllIvarDecls); |
Fariborz Jahanian | d097be8 | 2010-08-23 22:46:52 +0000 | [diff] [blame] | 1365 | |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1366 | // Parse all the comma separated declarators. |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 1367 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1368 | ParseStructDeclaration(DS, Callback); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1370 | if (Tok.is(tok::semi)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1371 | ConsumeToken(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1372 | } else { |
| 1373 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 1374 | // Skip to end of block or statement |
| 1375 | SkipUntil(tok::r_brace, true, true); |
| 1376 | } |
| 1377 | } |
Fariborz Jahanian | 4cc0cf1 | 2013-03-20 18:09:33 +0000 | [diff] [blame] | 1378 | HelperActionsForIvarDeclarations(interfaceDecl, atLoc, |
| 1379 | T, AllIvarDecls, false); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1380 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1381 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1382 | |
| 1383 | /// objc-protocol-declaration: |
| 1384 | /// objc-protocol-definition |
| 1385 | /// objc-protocol-forward-reference |
| 1386 | /// |
| 1387 | /// objc-protocol-definition: |
James Dennett | 17d26a6 | 2012-06-11 06:19:40 +0000 | [diff] [blame] | 1388 | /// \@protocol identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | /// objc-protocol-refs[opt] |
| 1390 | /// objc-interface-decl-list |
James Dennett | 17d26a6 | 2012-06-11 06:19:40 +0000 | [diff] [blame] | 1391 | /// \@end |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1392 | /// |
| 1393 | /// objc-protocol-forward-reference: |
James Dennett | 17d26a6 | 2012-06-11 06:19:40 +0000 | [diff] [blame] | 1394 | /// \@protocol identifier-list ';' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1395 | /// |
James Dennett | 17d26a6 | 2012-06-11 06:19:40 +0000 | [diff] [blame] | 1396 | /// "\@protocol identifier ;" should be resolved as "\@protocol |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1397 | /// identifier-list ;": objc-interface-decl-list may not start with a |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1398 | /// semicolon in the first alternative if objc-protocol-refs are omitted. |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1399 | Parser::DeclGroupPtrTy |
| 1400 | Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc, |
| 1401 | ParsedAttributes &attrs) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1402 | assert(Tok.isObjCAtKeyword(tok::objc_protocol) && |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1403 | "ParseObjCAtProtocolDeclaration(): Expected @protocol"); |
| 1404 | ConsumeToken(); // the "protocol" identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 1406 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1407 | Actions.CodeCompleteObjCProtocolDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1408 | cutOffParsing(); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1409 | return DeclGroupPtrTy(); |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Nico Weber | e145bfd | 2013-04-04 00:15:10 +0000 | [diff] [blame] | 1412 | MaybeSkipAttributes(tok::objc_protocol); |
Nico Weber | 9f4f5f1 | 2013-04-03 17:36:11 +0000 | [diff] [blame] | 1413 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1414 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1415 | Diag(Tok, diag::err_expected_ident); // missing protocol name. |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1416 | return DeclGroupPtrTy(); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1417 | } |
| 1418 | // Save the protocol name, then consume it. |
| 1419 | IdentifierInfo *protocolName = Tok.getIdentifierInfo(); |
| 1420 | SourceLocation nameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1422 | if (Tok.is(tok::semi)) { // forward declaration of one protocol. |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1423 | IdentifierLocPair ProtoInfo(protocolName, nameLoc); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1424 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1426 | attrs.getList()); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Erik Verbruggen | 90ec96f | 2011-12-08 09:58:43 +0000 | [diff] [blame] | 1429 | CheckNestedObjCContexts(AtLoc); |
| 1430 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1431 | if (Tok.is(tok::comma)) { // list of forward declarations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1432 | SmallVector<IdentifierLocPair, 8> ProtocolRefs; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1433 | ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc)); |
| 1434 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1435 | // Parse the list of forward declarations. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1436 | while (1) { |
| 1437 | ConsumeToken(); // the ',' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1438 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1439 | Diag(Tok, diag::err_expected_ident); |
| 1440 | SkipUntil(tok::semi); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1441 | return DeclGroupPtrTy(); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1442 | } |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1443 | ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(), |
| 1444 | Tok.getLocation())); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1445 | ConsumeToken(); // the identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1447 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | // Consume the ';'. |
| 1451 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1452 | return DeclGroupPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 1454 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | &ProtocolRefs[0], |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1456 | ProtocolRefs.size(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1457 | attrs.getList()); |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1458 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1460 | // Last, and definitely not least, parse a protocol declaration. |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1461 | SourceLocation LAngleLoc, EndProtoLoc; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1463 | SmallVector<Decl *, 8> ProtocolRefs; |
| 1464 | SmallVector<SourceLocation, 8> ProtocolLocs; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1465 | if (Tok.is(tok::less) && |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1466 | ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, |
| 1467 | LAngleLoc, EndProtoLoc)) |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1468 | return DeclGroupPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1470 | Decl *ProtoType = |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1471 | Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1472 | ProtocolRefs.data(), |
| 1473 | ProtocolRefs.size(), |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1474 | ProtocolLocs.data(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1475 | EndProtoLoc, attrs.getList()); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1476 | |
Fariborz Jahanian | 2f64cfe | 2011-08-22 21:44:58 +0000 | [diff] [blame] | 1477 | ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1478 | return Actions.ConvertDeclToDeclGroup(ProtoType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1479 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1480 | |
| 1481 | /// objc-implementation: |
| 1482 | /// objc-class-implementation-prologue |
| 1483 | /// objc-category-implementation-prologue |
| 1484 | /// |
| 1485 | /// objc-class-implementation-prologue: |
| 1486 | /// @implementation identifier objc-superclass[opt] |
| 1487 | /// objc-class-instance-variables[opt] |
| 1488 | /// |
| 1489 | /// objc-category-implementation-prologue: |
| 1490 | /// @implementation identifier ( identifier ) |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1491 | Parser::DeclGroupPtrTy |
| 1492 | Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1493 | assert(Tok.isObjCAtKeyword(tok::objc_implementation) && |
| 1494 | "ParseObjCAtImplementationDeclaration(): Expected @implementation"); |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 1495 | CheckNestedObjCContexts(AtLoc); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1496 | ConsumeToken(); // the "implementation" identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 1498 | // Code completion after '@implementation'. |
| 1499 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1500 | Actions.CodeCompleteObjCImplementationDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1501 | cutOffParsing(); |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1502 | return DeclGroupPtrTy(); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Nico Weber | e145bfd | 2013-04-04 00:15:10 +0000 | [diff] [blame] | 1505 | MaybeSkipAttributes(tok::objc_implementation); |
Nico Weber | 9f4f5f1 | 2013-04-03 17:36:11 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1507 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1508 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1509 | return DeclGroupPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1510 | } |
| 1511 | // We have a class or category name - consume it. |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1512 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1513 | SourceLocation nameLoc = ConsumeToken(); // consume class or category name |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1514 | Decl *ObjCImpDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
| 1516 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1517 | // we have a category implementation. |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1518 | ConsumeParen(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1519 | SourceLocation categoryLoc, rparenLoc; |
| 1520 | IdentifierInfo *categoryId = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 1522 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1523 | Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1524 | cutOffParsing(); |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1525 | return DeclGroupPtrTy(); |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1528 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1529 | categoryId = Tok.getIdentifierInfo(); |
| 1530 | categoryLoc = ConsumeToken(); |
| 1531 | } else { |
| 1532 | Diag(Tok, diag::err_expected_ident); // missing category name. |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1533 | return DeclGroupPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1535 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1536 | Diag(Tok, diag::err_expected_rparen); |
| 1537 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1538 | return DeclGroupPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1539 | } |
| 1540 | rparenLoc = ConsumeParen(); |
Fariborz Jahanian | e4bb749 | 2013-05-17 17:58:11 +0000 | [diff] [blame] | 1541 | if (Tok.is(tok::less)) { // we have illegal '<' try to recover |
| 1542 | Diag(Tok, diag::err_unexpected_protocol_qualifier); |
| 1543 | AttributeFactory attr; |
| 1544 | DeclSpec DS(attr); |
| 1545 | (void)ParseObjCProtocolQualifiers(DS); |
| 1546 | } |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1547 | ObjCImpDecl = Actions.ActOnStartCategoryImplementation( |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 1548 | AtLoc, nameId, nameLoc, categoryId, |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 1549 | categoryLoc); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1550 | |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1551 | } else { |
| 1552 | // We have a class implementation |
| 1553 | SourceLocation superClassLoc; |
| 1554 | IdentifierInfo *superClassId = 0; |
| 1555 | if (Tok.is(tok::colon)) { |
| 1556 | // We have a super class |
| 1557 | ConsumeToken(); |
| 1558 | if (Tok.isNot(tok::identifier)) { |
| 1559 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 1560 | return DeclGroupPtrTy(); |
| 1561 | } |
| 1562 | superClassId = Tok.getIdentifierInfo(); |
| 1563 | superClassLoc = ConsumeToken(); // Consume super class name |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1564 | } |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1565 | ObjCImpDecl = Actions.ActOnStartClassImplementation( |
| 1566 | AtLoc, nameId, nameLoc, |
| 1567 | superClassId, superClassLoc); |
| 1568 | |
| 1569 | if (Tok.is(tok::l_brace)) // we have ivars |
| 1570 | ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc); |
Fariborz Jahanian | 51c30af | 2013-04-24 23:23:47 +0000 | [diff] [blame] | 1571 | else if (Tok.is(tok::less)) { // we have illegal '<' try to recover |
| 1572 | Diag(Tok, diag::err_unexpected_protocol_qualifier); |
| 1573 | // try to recover. |
| 1574 | AttributeFactory attr; |
| 1575 | DeclSpec DS(attr); |
| 1576 | (void)ParseObjCProtocolQualifiers(DS); |
| 1577 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1578 | } |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1579 | assert(ObjCImpDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1581 | SmallVector<Decl *, 8> DeclsInGroup; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1582 | |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1583 | { |
| 1584 | ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl); |
| 1585 | while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) { |
| 1586 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1587 | MaybeParseCXX11Attributes(attrs); |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1588 | MaybeParseMicrosoftAttributes(attrs); |
| 1589 | if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) { |
| 1590 | DeclGroupRef DG = DGP.get(); |
| 1591 | DeclsInGroup.append(DG.begin(), DG.end()); |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
Argyrios Kyrtzidis | 644af7b | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 1596 | return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1597 | } |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1598 | |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 1599 | Parser::DeclGroupPtrTy |
| 1600 | Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1601 | assert(Tok.isObjCAtKeyword(tok::objc_end) && |
| 1602 | "ParseObjCAtEndDeclaration(): Expected @end"); |
| 1603 | ConsumeToken(); // the "end" identifier |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1604 | if (CurParsedObjCImpl) |
| 1605 | CurParsedObjCImpl->finish(atEnd); |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1606 | else |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1607 | // missing @implementation |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 1608 | Diag(atEnd.getBegin(), diag::err_expected_objc_container); |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1609 | return DeclGroupPtrTy(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1610 | } |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1611 | |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1612 | Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() { |
| 1613 | if (!Finished) { |
| 1614 | finish(P.Tok.getLocation()); |
| 1615 | if (P.Tok.is(tok::eof)) { |
| 1616 | P.Diag(P.Tok, diag::err_objc_missing_end) |
| 1617 | << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n"); |
| 1618 | P.Diag(Dcl->getLocStart(), diag::note_objc_container_start) |
| 1619 | << Sema::OCK_Implementation; |
| 1620 | } |
| 1621 | } |
| 1622 | P.CurParsedObjCImpl = 0; |
| 1623 | assert(LateParsedObjCMethods.empty()); |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1626 | void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) { |
| 1627 | assert(!Finished); |
| 1628 | P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl); |
| 1629 | for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1630 | P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i], |
| 1631 | true/*Methods*/); |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1632 | |
| 1633 | P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd); |
| 1634 | |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1635 | if (HasCFunction) |
| 1636 | for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) |
| 1637 | P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i], |
| 1638 | false/*c-functions*/); |
| 1639 | |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1640 | /// \brief Clear and free the cached objc methods. |
Argyrios Kyrtzidis | 2fea224 | 2011-11-29 08:14:54 +0000 | [diff] [blame] | 1641 | for (LateParsedObjCMethodContainer::iterator |
| 1642 | I = LateParsedObjCMethods.begin(), |
| 1643 | E = LateParsedObjCMethods.end(); I != E; ++I) |
| 1644 | delete *I; |
| 1645 | LateParsedObjCMethods.clear(); |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1646 | |
| 1647 | Finished = true; |
Argyrios Kyrtzidis | 2fea224 | 2011-11-29 08:14:54 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1650 | /// compatibility-alias-decl: |
| 1651 | /// @compatibility_alias alias-name class-name ';' |
| 1652 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1653 | Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1654 | assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && |
| 1655 | "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); |
| 1656 | ConsumeToken(); // consume compatibility_alias |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1657 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1658 | Diag(Tok, diag::err_expected_ident); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1659 | return 0; |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1660 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1661 | IdentifierInfo *aliasId = Tok.getIdentifierInfo(); |
| 1662 | SourceLocation aliasLoc = ConsumeToken(); // consume alias-name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1663 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1664 | Diag(Tok, diag::err_expected_ident); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1665 | return 0; |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1666 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1667 | IdentifierInfo *classId = Tok.getIdentifierInfo(); |
| 1668 | SourceLocation classLoc = ConsumeToken(); // consume class-name; |
Douglas Gregor | e6bf90a | 2011-01-05 01:10:06 +0000 | [diff] [blame] | 1669 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
| 1670 | "@compatibility_alias"); |
Richard Smith | de01b7a | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1671 | return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc, |
| 1672 | classId, classLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1675 | /// property-synthesis: |
| 1676 | /// @synthesize property-ivar-list ';' |
| 1677 | /// |
| 1678 | /// property-ivar-list: |
| 1679 | /// property-ivar |
| 1680 | /// property-ivar-list ',' property-ivar |
| 1681 | /// |
| 1682 | /// property-ivar: |
| 1683 | /// identifier |
| 1684 | /// identifier '=' identifier |
| 1685 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1686 | Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1687 | assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && |
Fariborz Jahanian | df1fdfd | 2013-04-29 15:35:35 +0000 | [diff] [blame] | 1688 | "ParseObjCPropertySynthesize(): Expected '@synthesize'"); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1689 | ConsumeToken(); // consume synthesize |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | |
Douglas Gregor | b328c42 | 2009-11-18 19:45:45 +0000 | [diff] [blame] | 1691 | while (true) { |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1692 | if (Tok.is(tok::code_completion)) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1693 | Actions.CodeCompleteObjCPropertyDefinition(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1694 | cutOffParsing(); |
| 1695 | return 0; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Douglas Gregor | b328c42 | 2009-11-18 19:45:45 +0000 | [diff] [blame] | 1698 | if (Tok.isNot(tok::identifier)) { |
| 1699 | Diag(Tok, diag::err_synthesized_property_name); |
| 1700 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1701 | return 0; |
Douglas Gregor | b328c42 | 2009-11-18 19:45:45 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1704 | IdentifierInfo *propertyIvar = 0; |
| 1705 | IdentifierInfo *propertyId = Tok.getIdentifierInfo(); |
| 1706 | SourceLocation propertyLoc = ConsumeToken(); // consume property name |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1707 | SourceLocation propertyIvarLoc; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1708 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1709 | // property '=' ivar-name |
| 1710 | ConsumeToken(); // consume '=' |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1711 | |
| 1712 | if (Tok.is(tok::code_completion)) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1713 | Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1714 | cutOffParsing(); |
| 1715 | return 0; |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1718 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1719 | Diag(Tok, diag::err_expected_ident); |
| 1720 | break; |
| 1721 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1722 | propertyIvar = Tok.getIdentifierInfo(); |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1723 | propertyIvarLoc = ConsumeToken(); // consume ivar-name |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1724 | } |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1725 | Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1726 | propertyId, propertyIvar, propertyIvarLoc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1727 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1728 | break; |
| 1729 | ConsumeToken(); // consume ',' |
| 1730 | } |
Douglas Gregor | e6bf90a | 2011-01-05 01:10:06 +0000 | [diff] [blame] | 1731 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1732 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1735 | /// property-dynamic: |
| 1736 | /// @dynamic property-list |
| 1737 | /// |
| 1738 | /// property-list: |
| 1739 | /// identifier |
| 1740 | /// property-list ',' identifier |
| 1741 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1742 | Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1743 | assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && |
| 1744 | "ParseObjCPropertyDynamic(): Expected '@dynamic'"); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1745 | ConsumeToken(); // consume dynamic |
Douglas Gregor | 424b2a5 | 2009-11-18 22:56:13 +0000 | [diff] [blame] | 1746 | while (true) { |
| 1747 | if (Tok.is(tok::code_completion)) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1748 | Actions.CodeCompleteObjCPropertyDefinition(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1749 | cutOffParsing(); |
| 1750 | return 0; |
Douglas Gregor | 424b2a5 | 2009-11-18 22:56:13 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | if (Tok.isNot(tok::identifier)) { |
| 1754 | Diag(Tok, diag::err_expected_ident); |
| 1755 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1756 | return 0; |
Douglas Gregor | 424b2a5 | 2009-11-18 22:56:13 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1759 | IdentifierInfo *propertyId = Tok.getIdentifierInfo(); |
| 1760 | SourceLocation propertyLoc = ConsumeToken(); // consume property name |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 1761 | Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1762 | propertyId, 0, SourceLocation()); |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1763 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1764 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1765 | break; |
| 1766 | ConsumeToken(); // consume ',' |
| 1767 | } |
Douglas Gregor | e6bf90a | 2011-01-05 01:10:06 +0000 | [diff] [blame] | 1768 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1769 | return 0; |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1770 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1772 | /// objc-throw-statement: |
| 1773 | /// throw expression[opt]; |
| 1774 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1775 | StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) { |
| 1776 | ExprResult Res; |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1777 | ConsumeToken(); // consume throw |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1778 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1779 | Res = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1780 | if (Res.isInvalid()) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1781 | SkipUntil(tok::semi); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1782 | return StmtError(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
Ted Kremenek | 02418c7 | 2010-04-20 21:21:51 +0000 | [diff] [blame] | 1785 | // consume ';' |
| 1786 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw"); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1787 | return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope()); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1790 | /// objc-synchronized-statement: |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1791 | /// @synchronized '(' expression ')' compound-statement |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1792 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1793 | StmtResult |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1794 | Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) { |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1795 | ConsumeToken(); // consume synchronized |
| 1796 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1797 | Diag(Tok, diag::err_expected_lparen_after) << "@synchronized"; |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1798 | return StmtError(); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1799 | } |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 1800 | |
| 1801 | // The operand is surrounded with parentheses. |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1802 | ConsumeParen(); // '(' |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 1803 | ExprResult operand(ParseExpression()); |
| 1804 | |
| 1805 | if (Tok.is(tok::r_paren)) { |
| 1806 | ConsumeParen(); // ')' |
| 1807 | } else { |
| 1808 | if (!operand.isInvalid()) |
| 1809 | Diag(Tok, diag::err_expected_rparen); |
| 1810 | |
| 1811 | // Skip forward until we see a left brace, but don't consume it. |
| 1812 | SkipUntil(tok::l_brace, true, true); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1813 | } |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 1814 | |
| 1815 | // Require a compound statement. |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1816 | if (Tok.isNot(tok::l_brace)) { |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 1817 | if (!operand.isInvalid()) |
| 1818 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1819 | return StmtError(); |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1820 | } |
Steve Naroff | 3ac438c | 2008-06-04 20:36:13 +0000 | [diff] [blame] | 1821 | |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 1822 | // Check the @synchronized operand now. |
| 1823 | if (!operand.isInvalid()) |
| 1824 | operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1825 | |
John McCall | 0752403 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 1826 | // Parse the compound statement within a new scope. |
| 1827 | ParseScope bodyScope(this, Scope::DeclScope); |
| 1828 | StmtResult body(ParseCompoundStatementBody()); |
| 1829 | bodyScope.Exit(); |
| 1830 | |
| 1831 | // If there was a semantic or parse error earlier with the |
| 1832 | // operand, fail now. |
| 1833 | if (operand.isInvalid()) |
| 1834 | return StmtError(); |
| 1835 | |
| 1836 | if (body.isInvalid()) |
| 1837 | body = Actions.ActOnNullStmt(Tok.getLocation()); |
| 1838 | |
| 1839 | return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get()); |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1842 | /// objc-try-catch-statement: |
| 1843 | /// @try compound-statement objc-catch-list[opt] |
| 1844 | /// @try compound-statement objc-catch-list[opt] @finally compound-statement |
| 1845 | /// |
| 1846 | /// objc-catch-list: |
| 1847 | /// @catch ( parameter-declaration ) compound-statement |
| 1848 | /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement |
| 1849 | /// catch-parameter-declaration: |
| 1850 | /// parameter-declaration |
| 1851 | /// '...' [OBJC2] |
| 1852 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1853 | StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1854 | bool catch_or_finally_seen = false; |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1855 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1856 | ConsumeToken(); // consume try |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1857 | if (Tok.isNot(tok::l_brace)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1858 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1859 | return StmtError(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1860 | } |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1861 | StmtVector CatchStmts; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1862 | StmtResult FinallyStmt; |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1863 | ParseScope TryScope(this, Scope::DeclScope); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1864 | StmtResult TryBody(ParseCompoundStatementBody()); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1865 | TryScope.Exit(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1866 | if (TryBody.isInvalid()) |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1867 | TryBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1869 | while (Tok.is(tok::at)) { |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1870 | // At this point, we need to lookahead to determine if this @ is the start |
| 1871 | // of an @catch or @finally. We don't want to consume the @ token if this |
| 1872 | // is an @try or @encode or something else. |
| 1873 | Token AfterAt = GetLookAheadToken(1); |
| 1874 | if (!AfterAt.isObjCAtKeyword(tok::objc_catch) && |
| 1875 | !AfterAt.isObjCAtKeyword(tok::objc_finally)) |
| 1876 | break; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1877 | |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1878 | SourceLocation AtCatchFinallyLoc = ConsumeToken(); |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 1879 | if (Tok.isObjCAtKeyword(tok::objc_catch)) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1880 | Decl *FirstPart = 0; |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1881 | ConsumeToken(); // consume catch |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1882 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1883 | ConsumeParen(); |
Steve Naroff | e21dd6f | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 1884 | ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1885 | if (Tok.isNot(tok::ellipsis)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1886 | DeclSpec DS(AttrFactory); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1887 | ParseDeclarationSpecifiers(DS); |
Argyrios Kyrtzidis | 17b6399 | 2011-07-01 22:22:40 +0000 | [diff] [blame] | 1888 | Declarator ParmDecl(DS, Declarator::ObjCCatchContext); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1889 | ParseDeclarator(ParmDecl); |
| 1890 | |
Douglas Gregor | 4e6c0d1 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 1891 | // Inform the actions module about the declarator, so it |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1892 | // gets added to the current scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1893 | FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1894 | } else |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1895 | ConsumeToken(); // consume '...' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Steve Naroff | 93a2595 | 2009-04-07 22:56:58 +0000 | [diff] [blame] | 1897 | SourceLocation RParenLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
Steve Naroff | 93a2595 | 2009-04-07 22:56:58 +0000 | [diff] [blame] | 1899 | if (Tok.is(tok::r_paren)) |
| 1900 | RParenLoc = ConsumeParen(); |
| 1901 | else // Skip over garbage, until we get to ')'. Eat the ')'. |
| 1902 | SkipUntil(tok::r_paren, true, false); |
| 1903 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1904 | StmtResult CatchBody(true); |
Chris Lattner | c1b3ba5 | 2008-02-14 19:27:54 +0000 | [diff] [blame] | 1905 | if (Tok.is(tok::l_brace)) |
| 1906 | CatchBody = ParseCompoundStatementBody(); |
| 1907 | else |
| 1908 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1909 | if (CatchBody.isInvalid()) |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1910 | CatchBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1911 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1912 | StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1913 | RParenLoc, |
| 1914 | FirstPart, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1915 | CatchBody.take()); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1916 | if (!Catch.isInvalid()) |
| 1917 | CatchStmts.push_back(Catch.release()); |
| 1918 | |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1919 | } else { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1920 | Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after) |
| 1921 | << "@catch clause"; |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1922 | return StmtError(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1923 | } |
| 1924 | catch_or_finally_seen = true; |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1925 | } else { |
| 1926 | assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?"); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1927 | ConsumeToken(); // consume finally |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1928 | ParseScope FinallyScope(this, Scope::DeclScope); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1929 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1930 | StmtResult FinallyBody(true); |
Chris Lattner | c1b3ba5 | 2008-02-14 19:27:54 +0000 | [diff] [blame] | 1931 | if (Tok.is(tok::l_brace)) |
| 1932 | FinallyBody = ParseCompoundStatementBody(); |
| 1933 | else |
| 1934 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1935 | if (FinallyBody.isInvalid()) |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1936 | FinallyBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1937 | FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1938 | FinallyBody.take()); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1939 | catch_or_finally_seen = true; |
| 1940 | break; |
| 1941 | } |
| 1942 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1943 | if (!catch_or_finally_seen) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1944 | Diag(atLoc, diag::err_missing_catch_finally); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1945 | return StmtError(); |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1946 | } |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 1947 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1948 | return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1949 | CatchStmts, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1950 | FinallyStmt.take()); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1951 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1952 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1953 | /// objc-autoreleasepool-statement: |
| 1954 | /// @autoreleasepool compound-statement |
| 1955 | /// |
| 1956 | StmtResult |
| 1957 | Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) { |
| 1958 | ConsumeToken(); // consume autoreleasepool |
| 1959 | if (Tok.isNot(tok::l_brace)) { |
| 1960 | Diag(Tok, diag::err_expected_lbrace); |
| 1961 | return StmtError(); |
| 1962 | } |
| 1963 | // Enter a scope to hold everything within the compound stmt. Compound |
| 1964 | // statements can always hold declarations. |
| 1965 | ParseScope BodyScope(this, Scope::DeclScope); |
| 1966 | |
| 1967 | StmtResult AutoreleasePoolBody(ParseCompoundStatementBody()); |
| 1968 | |
| 1969 | BodyScope.Exit(); |
| 1970 | if (AutoreleasePoolBody.isInvalid()) |
| 1971 | AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation()); |
| 1972 | return Actions.ActOnObjCAutoreleasePoolStmt(atLoc, |
| 1973 | AutoreleasePoolBody.take()); |
| 1974 | } |
| 1975 | |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1976 | /// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them |
| 1977 | /// for later parsing. |
| 1978 | void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) { |
| 1979 | LexedMethod* LM = new LexedMethod(this, MDecl); |
| 1980 | CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM); |
| 1981 | CachedTokens &Toks = LM->Toks; |
Fariborz Jahanian | 9e5df31 | 2012-08-10 21:15:06 +0000 | [diff] [blame] | 1982 | // Begin by storing the '{' or 'try' or ':' token. |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1983 | Toks.push_back(Tok); |
Fariborz Jahanian | 2eb362b | 2012-08-10 18:10:56 +0000 | [diff] [blame] | 1984 | if (Tok.is(tok::kw_try)) { |
| 1985 | ConsumeToken(); |
Fariborz Jahanian | dbd6945 | 2012-08-10 20:34:17 +0000 | [diff] [blame] | 1986 | if (Tok.is(tok::colon)) { |
| 1987 | Toks.push_back(Tok); |
| 1988 | ConsumeToken(); |
| 1989 | while (Tok.isNot(tok::l_brace)) { |
| 1990 | ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false); |
| 1991 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
| 1992 | } |
| 1993 | } |
Fariborz Jahanian | 9e5df31 | 2012-08-10 21:15:06 +0000 | [diff] [blame] | 1994 | Toks.push_back(Tok); // also store '{' |
| 1995 | } |
| 1996 | else if (Tok.is(tok::colon)) { |
| 1997 | ConsumeToken(); |
| 1998 | while (Tok.isNot(tok::l_brace)) { |
| 1999 | ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false); |
| 2000 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
| 2001 | } |
Fariborz Jahanian | 2eb362b | 2012-08-10 18:10:56 +0000 | [diff] [blame] | 2002 | Toks.push_back(Tok); // also store '{' |
| 2003 | } |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2004 | ConsumeBrace(); |
| 2005 | // Consume everything up to (and including) the matching right brace. |
| 2006 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Fariborz Jahanian | 2eb362b | 2012-08-10 18:10:56 +0000 | [diff] [blame] | 2007 | while (Tok.is(tok::kw_catch)) { |
| 2008 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 2009 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 2010 | } |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 2013 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 2014 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2015 | Decl *Parser::ParseObjCMethodDefinition() { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2016 | Decl *MDecl = ParseObjCMethodPrototype(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2018 | PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(), |
| 2019 | "parsing Objective-C method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 2021 | // parse optional ';' |
Fariborz Jahanian | 209a8c2 | 2009-10-20 16:39:13 +0000 | [diff] [blame] | 2022 | if (Tok.is(tok::semi)) { |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2023 | if (CurParsedObjCImpl) { |
Ted Kremenek | 496e45e | 2009-11-10 22:55:49 +0000 | [diff] [blame] | 2024 | Diag(Tok, diag::warn_semicolon_before_method_body) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2025 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Ted Kremenek | 496e45e | 2009-11-10 22:55:49 +0000 | [diff] [blame] | 2026 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 2027 | ConsumeToken(); |
Fariborz Jahanian | 209a8c2 | 2009-10-20 16:39:13 +0000 | [diff] [blame] | 2028 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 2029 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 2030 | // We should have an opening brace now. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 2031 | if (Tok.isNot(tok::l_brace)) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 2032 | Diag(Tok, diag::err_expected_method_body); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 2034 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 2035 | SkipUntil(tok::l_brace, true, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 2037 | // If we didn't find the '{', bail out. |
| 2038 | if (Tok.isNot(tok::l_brace)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2039 | return 0; |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 2040 | } |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2041 | |
| 2042 | if (!MDecl) { |
| 2043 | ConsumeBrace(); |
| 2044 | SkipUntil(tok::r_brace, /*StopAtSemi=*/false); |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2048 | // Allow the rest of sema to find private method decl implementations. |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2049 | Actions.AddAnyMethodToGlobalPool(MDecl); |
Fariborz Jahanian | c9b9709 | 2012-08-09 17:15:00 +0000 | [diff] [blame] | 2050 | assert (CurParsedObjCImpl |
| 2051 | && "ParseObjCMethodDefinition - Method out of @implementation"); |
| 2052 | // Consume the tokens and store them for later parsing. |
| 2053 | StashAwayMethodOrFunctionBodyTokens(MDecl); |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 2054 | return MDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2055 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2056 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2057 | StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) { |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2058 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2059 | Actions.CodeCompleteObjCAtStatement(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2060 | cutOffParsing(); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2061 | return StmtError(); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | if (Tok.isObjCAtKeyword(tok::objc_try)) |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 2065 | return ParseObjCTryStmt(AtLoc); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 2066 | |
| 2067 | if (Tok.isObjCAtKeyword(tok::objc_throw)) |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 2068 | return ParseObjCThrowStmt(AtLoc); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 2069 | |
| 2070 | if (Tok.isObjCAtKeyword(tok::objc_synchronized)) |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 2071 | return ParseObjCSynchronizedStmt(AtLoc); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2072 | |
| 2073 | if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool)) |
| 2074 | return ParseObjCAutoreleasePoolStmt(AtLoc); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 2075 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2076 | ExprResult Res(ParseExpressionWithLeadingAt(AtLoc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2077 | if (Res.isInvalid()) { |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 2078 | // If the expression is invalid, skip ahead to the next semicolon. Not |
| 2079 | // doing this opens us up to the possibility of infinite loops if |
| 2080 | // ParseExpression does not consume any tokens. |
| 2081 | SkipUntil(tok::semi); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 2082 | return StmtError(); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 2083 | } |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 2084 | |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 2085 | // Otherwise, eat the semicolon. |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 2086 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2087 | return Actions.ActOnExprStmt(Res); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2090 | ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2091 | switch (Tok.getKind()) { |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2092 | case tok::code_completion: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2093 | Actions.CodeCompleteObjCAtExpression(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2094 | cutOffParsing(); |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 2095 | return ExprError(); |
| 2096 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2097 | case tok::minus: |
| 2098 | case tok::plus: { |
| 2099 | tok::TokenKind Kind = Tok.getKind(); |
| 2100 | SourceLocation OpLoc = ConsumeToken(); |
| 2101 | |
| 2102 | if (!Tok.is(tok::numeric_constant)) { |
| 2103 | const char *Symbol = 0; |
| 2104 | switch (Kind) { |
| 2105 | case tok::minus: Symbol = "-"; break; |
| 2106 | case tok::plus: Symbol = "+"; break; |
| 2107 | default: llvm_unreachable("missing unary operator case"); |
| 2108 | } |
| 2109 | Diag(Tok, diag::err_nsnumber_nonliteral_unary) |
| 2110 | << Symbol; |
| 2111 | return ExprError(); |
| 2112 | } |
| 2113 | |
| 2114 | ExprResult Lit(Actions.ActOnNumericConstant(Tok)); |
| 2115 | if (Lit.isInvalid()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2116 | return Lit; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2117 | } |
Benjamin Kramer | 8b8d953 | 2012-03-07 00:14:40 +0000 | [diff] [blame] | 2118 | ConsumeToken(); // Consume the literal token. |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2119 | |
| 2120 | Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take()); |
| 2121 | if (Lit.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2122 | return Lit; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2123 | |
| 2124 | return ParsePostfixExpressionSuffix( |
| 2125 | Actions.BuildObjCNumericLiteral(AtLoc, Lit.take())); |
| 2126 | } |
| 2127 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 2128 | case tok::string_literal: // primary-expression: string-literal |
| 2129 | case tok::wide_string_literal: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2130 | return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2131 | |
| 2132 | case tok::char_constant: |
| 2133 | return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc)); |
| 2134 | |
| 2135 | case tok::numeric_constant: |
| 2136 | return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc)); |
| 2137 | |
| 2138 | case tok::kw_true: // Objective-C++, etc. |
| 2139 | case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes |
| 2140 | return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true)); |
| 2141 | case tok::kw_false: // Objective-C++, etc. |
| 2142 | case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no |
| 2143 | return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false)); |
| 2144 | |
| 2145 | case tok::l_square: |
| 2146 | // Objective-C array literal |
| 2147 | return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc)); |
| 2148 | |
| 2149 | case tok::l_brace: |
| 2150 | // Objective-C dictionary literal |
| 2151 | return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc)); |
| 2152 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2153 | case tok::l_paren: |
| 2154 | // Objective-C boxed expression |
| 2155 | return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc)); |
| 2156 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 2157 | default: |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2158 | if (Tok.getIdentifierInfo() == 0) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2159 | return ExprError(Diag(AtLoc, diag::err_unexpected_at)); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 2160 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2161 | switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { |
| 2162 | case tok::objc_encode: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2163 | return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc)); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2164 | case tok::objc_protocol: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2165 | return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc)); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2166 | case tok::objc_selector: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2167 | return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc)); |
Fariborz Jahanian | 6749ae1 | 2012-07-09 20:00:35 +0000 | [diff] [blame] | 2168 | default: { |
| 2169 | const char *str = 0; |
| 2170 | if (GetLookAheadToken(1).is(tok::l_brace)) { |
| 2171 | char ch = Tok.getIdentifierInfo()->getNameStart()[0]; |
| 2172 | str = |
| 2173 | ch == 't' ? "try" |
| 2174 | : (ch == 'f' ? "finally" |
| 2175 | : (ch == 'a' ? "autoreleasepool" : 0)); |
| 2176 | } |
| 2177 | if (str) { |
| 2178 | SourceLocation kwLoc = Tok.getLocation(); |
| 2179 | return ExprError(Diag(AtLoc, diag::err_unexpected_at) << |
| 2180 | FixItHint::CreateReplacement(kwLoc, str)); |
| 2181 | } |
| 2182 | else |
| 2183 | return ExprError(Diag(AtLoc, diag::err_unexpected_at)); |
| 2184 | } |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2185 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2186 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Dmitri Gribenko | e23fb90 | 2012-09-12 17:01:48 +0000 | [diff] [blame] | 2189 | /// \brief Parse the receiver of an Objective-C++ message send. |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2190 | /// |
| 2191 | /// This routine parses the receiver of a message send in |
| 2192 | /// Objective-C++ either as a type or as an expression. Note that this |
| 2193 | /// routine must not be called to parse a send to 'super', since it |
| 2194 | /// has no way to return such a result. |
| 2195 | /// |
| 2196 | /// \param IsExpr Whether the receiver was parsed as an expression. |
| 2197 | /// |
| 2198 | /// \param TypeOrExpr If the receiver was parsed as an expression (\c |
| 2199 | /// IsExpr is true), the parsed expression. If the receiver was parsed |
| 2200 | /// as a type (\c IsExpr is false), the parsed type. |
| 2201 | /// |
| 2202 | /// \returns True if an error occurred during parsing or semantic |
| 2203 | /// analysis, in which case the arguments do not have valid |
| 2204 | /// values. Otherwise, returns false for a successful parse. |
| 2205 | /// |
| 2206 | /// objc-receiver: [C++] |
| 2207 | /// 'super' [not parsed here] |
| 2208 | /// expression |
| 2209 | /// simple-type-specifier |
| 2210 | /// typename-specifier |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2211 | bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2212 | InMessageExpressionRAIIObject InMessage(*this, true); |
| 2213 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2214 | if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || |
| 2215 | Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) |
| 2216 | TryAnnotateTypeOrScopeToken(); |
| 2217 | |
Kaelyn Uhrain | 7bf3340 | 2012-06-15 23:45:51 +0000 | [diff] [blame] | 2218 | if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2219 | // objc-receiver: |
| 2220 | // expression |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2221 | ExprResult Receiver = ParseExpression(); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2222 | if (Receiver.isInvalid()) |
| 2223 | return true; |
| 2224 | |
| 2225 | IsExpr = true; |
| 2226 | TypeOrExpr = Receiver.take(); |
| 2227 | return false; |
| 2228 | } |
| 2229 | |
| 2230 | // objc-receiver: |
| 2231 | // typename-specifier |
| 2232 | // simple-type-specifier |
| 2233 | // expression (that starts with one of the above) |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2234 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2235 | ParseCXXSimpleTypeSpecifier(DS); |
| 2236 | |
| 2237 | if (Tok.is(tok::l_paren)) { |
| 2238 | // If we see an opening parentheses at this point, we are |
| 2239 | // actually parsing an expression that starts with a |
| 2240 | // function-style cast, e.g., |
| 2241 | // |
| 2242 | // postfix-expression: |
| 2243 | // simple-type-specifier ( expression-list [opt] ) |
| 2244 | // typename-specifier ( expression-list [opt] ) |
| 2245 | // |
| 2246 | // Parse the remainder of this case, then the (optional) |
| 2247 | // postfix-expression suffix, followed by the (optional) |
| 2248 | // right-hand side of the binary expression. We have an |
| 2249 | // instance method. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2250 | ExprResult Receiver = ParseCXXTypeConstructExpression(DS); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2251 | if (!Receiver.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2252 | Receiver = ParsePostfixExpressionSuffix(Receiver.take()); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2253 | if (!Receiver.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2254 | Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2255 | if (Receiver.isInvalid()) |
| 2256 | return true; |
| 2257 | |
| 2258 | IsExpr = true; |
| 2259 | TypeOrExpr = Receiver.take(); |
| 2260 | return false; |
| 2261 | } |
| 2262 | |
| 2263 | // We have a class message. Turn the simple-type-specifier or |
| 2264 | // typename-specifier we parsed into a type and parse the |
| 2265 | // remainder of the class message. |
| 2266 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2267 | TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2268 | if (Type.isInvalid()) |
| 2269 | return true; |
| 2270 | |
| 2271 | IsExpr = false; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2272 | TypeOrExpr = Type.get().getAsOpaquePtr(); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2273 | return false; |
| 2274 | } |
| 2275 | |
Douglas Gregor | 1b730e8 | 2010-05-31 14:40:22 +0000 | [diff] [blame] | 2276 | /// \brief Determine whether the parser is currently referring to a an |
| 2277 | /// Objective-C message send, using a simplified heuristic to avoid overhead. |
| 2278 | /// |
| 2279 | /// This routine will only return true for a subset of valid message-send |
| 2280 | /// expressions. |
| 2281 | bool Parser::isSimpleObjCMessageExpression() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2282 | assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 && |
Douglas Gregor | 1b730e8 | 2010-05-31 14:40:22 +0000 | [diff] [blame] | 2283 | "Incorrect start for isSimpleObjCMessageExpression"); |
Douglas Gregor | 1b730e8 | 2010-05-31 14:40:22 +0000 | [diff] [blame] | 2284 | return GetLookAheadToken(1).is(tok::identifier) && |
| 2285 | GetLookAheadToken(2).is(tok::identifier); |
| 2286 | } |
| 2287 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2288 | bool Parser::isStartOfObjCClassMessageMissingOpenBracket() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2289 | if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) || |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2290 | InMessageExpression) |
| 2291 | return false; |
| 2292 | |
| 2293 | |
| 2294 | ParsedType Type; |
| 2295 | |
| 2296 | if (Tok.is(tok::annot_typename)) |
| 2297 | Type = getTypeAnnotation(Tok); |
| 2298 | else if (Tok.is(tok::identifier)) |
| 2299 | Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(), |
| 2300 | getCurScope()); |
| 2301 | else |
| 2302 | return false; |
| 2303 | |
| 2304 | if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) { |
| 2305 | const Token &AfterNext = GetLookAheadToken(2); |
| 2306 | if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) { |
| 2307 | if (Tok.is(tok::identifier)) |
| 2308 | TryAnnotateTypeOrScopeToken(); |
| 2309 | |
| 2310 | return Tok.is(tok::annot_typename); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | return false; |
| 2315 | } |
| 2316 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | /// objc-message-expr: |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 2318 | /// '[' objc-receiver objc-message-args ']' |
| 2319 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2320 | /// objc-receiver: [C] |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2321 | /// 'super' |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 2322 | /// expression |
| 2323 | /// class-name |
| 2324 | /// type-name |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2325 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2326 | ExprResult Parser::ParseObjCMessageExpression() { |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 2327 | assert(Tok.is(tok::l_square) && "'[' expected"); |
| 2328 | SourceLocation LBracLoc = ConsumeBracket(); // consume '[' |
| 2329 | |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 2330 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2331 | Actions.CodeCompleteObjCMessageReceiver(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2332 | cutOffParsing(); |
Douglas Gregor | 8e254cf | 2010-05-27 23:06:34 +0000 | [diff] [blame] | 2333 | return ExprError(); |
| 2334 | } |
| 2335 | |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2336 | InMessageExpressionRAIIObject InMessage(*this, true); |
| 2337 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2338 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2339 | // We completely separate the C and C++ cases because C++ requires |
| 2340 | // more complicated (read: slower) parsing. |
| 2341 | |
| 2342 | // Handle send to super. |
| 2343 | // FIXME: This doesn't benefit from the same typo-correction we |
| 2344 | // get in Objective-C. |
| 2345 | if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2346 | NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2347 | return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), |
| 2348 | ParsedType(), 0); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2349 | |
| 2350 | // Parse the receiver, which is either a type or an expression. |
| 2351 | bool IsExpr; |
Nick Lewycky | 304b752 | 2010-09-15 18:35:19 +0000 | [diff] [blame] | 2352 | void *TypeOrExpr = NULL; |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2353 | if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { |
| 2354 | SkipUntil(tok::r_square); |
| 2355 | return ExprError(); |
| 2356 | } |
| 2357 | |
| 2358 | if (IsExpr) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2359 | return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), |
| 2360 | ParsedType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2361 | static_cast<Expr*>(TypeOrExpr)); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 2362 | |
| 2363 | return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2364 | ParsedType::getFromOpaquePtr(TypeOrExpr), |
| 2365 | 0); |
Chris Lattner | c59cb38 | 2010-05-31 18:18:22 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 1dbca6e | 2010-04-14 02:22:16 +0000 | [diff] [blame] | 2369 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
| 2370 | SourceLocation NameLoc = Tok.getLocation(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2371 | ParsedType ReceiverType; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2372 | switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc, |
Douglas Gregor | 1dbca6e | 2010-04-14 02:22:16 +0000 | [diff] [blame] | 2373 | Name == Ident_super, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2374 | NextToken().is(tok::period), |
| 2375 | ReceiverType)) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2376 | case Sema::ObjCSuperMessage: |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2377 | return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), |
| 2378 | ParsedType(), 0); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2379 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2380 | case Sema::ObjCClassMessage: |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2381 | if (!ReceiverType) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2382 | SkipUntil(tok::r_square); |
| 2383 | return ExprError(); |
| 2384 | } |
| 2385 | |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2386 | ConsumeToken(); // the type name |
| 2387 | |
| 2388 | return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2389 | ReceiverType, 0); |
Douglas Gregor | 1dbca6e | 2010-04-14 02:22:16 +0000 | [diff] [blame] | 2390 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2391 | case Sema::ObjCInstanceMessage: |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2392 | // Fall through to parse an expression. |
Douglas Gregor | 1dbca6e | 2010-04-14 02:22:16 +0000 | [diff] [blame] | 2393 | break; |
Fariborz Jahanian | d286992 | 2009-04-08 19:50:10 +0000 | [diff] [blame] | 2394 | } |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 2395 | } |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2396 | |
| 2397 | // Otherwise, an arbitrary expression can be the receiver of a send. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2398 | ExprResult Res(ParseExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2399 | if (Res.isInvalid()) { |
Chris Lattner | 5c74942 | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 2400 | SkipUntil(tok::r_square); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2401 | return Res; |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 2402 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2403 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2404 | return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), |
| 2405 | ParsedType(), Res.take()); |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 2406 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2407 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2408 | /// \brief Parse the remainder of an Objective-C message following the |
| 2409 | /// '[' objc-receiver. |
| 2410 | /// |
| 2411 | /// This routine handles sends to super, class messages (sent to a |
| 2412 | /// class name), and instance messages (sent to an object), and the |
| 2413 | /// target is represented by \p SuperLoc, \p ReceiverType, or \p |
| 2414 | /// ReceiverExpr, respectively. Only one of these parameters may have |
| 2415 | /// a valid value. |
| 2416 | /// |
| 2417 | /// \param LBracLoc The location of the opening '['. |
| 2418 | /// |
| 2419 | /// \param SuperLoc If this is a send to 'super', the location of the |
| 2420 | /// 'super' keyword that indicates a send to the superclass. |
| 2421 | /// |
| 2422 | /// \param ReceiverType If this is a class message, the type of the |
| 2423 | /// class we are sending a message to. |
| 2424 | /// |
| 2425 | /// \param ReceiverExpr If this is an instance message, the expression |
| 2426 | /// used to compute the receiver object. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2427 | /// |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 2428 | /// objc-message-args: |
| 2429 | /// objc-selector |
| 2430 | /// objc-keywordarg-list |
| 2431 | /// |
| 2432 | /// objc-keywordarg-list: |
| 2433 | /// objc-keywordarg |
| 2434 | /// objc-keywordarg-list objc-keywordarg |
| 2435 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | /// objc-keywordarg: |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 2437 | /// selector-name[opt] ':' objc-keywordexpr |
| 2438 | /// |
| 2439 | /// objc-keywordexpr: |
| 2440 | /// nonempty-expr-list |
| 2441 | /// |
| 2442 | /// nonempty-expr-list: |
| 2443 | /// assignment-expression |
| 2444 | /// nonempty-expr-list , assignment-expression |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2445 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2446 | ExprResult |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 2447 | Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2448 | SourceLocation SuperLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2449 | ParsedType ReceiverType, |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2450 | ExprArg ReceiverExpr) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2451 | InMessageExpressionRAIIObject InMessage(*this, true); |
| 2452 | |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2453 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2454 | if (SuperLoc.isValid()) |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2455 | Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2456 | false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2457 | else if (ReceiverType) |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2458 | Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2459 | false); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2460 | else |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2461 | Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2462 | None, false); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2463 | cutOffParsing(); |
| 2464 | return ExprError(); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 2465 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2466 | |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2467 | // Parse objc-selector |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2468 | SourceLocation Loc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 2469 | IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc); |
Fariborz Jahanian | 48f3cc2 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2471 | SmallVector<IdentifierInfo *, 12> KeyIdents; |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2472 | SmallVector<SourceLocation, 12> KeyLocs; |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2473 | ExprVector KeyExprs; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 2474 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 2475 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2476 | while (1) { |
| 2477 | // Each iteration parses a single keyword argument. |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 2478 | KeyIdents.push_back(selIdent); |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2479 | KeyLocs.push_back(Loc); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 2480 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 2481 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2482 | Diag(Tok, diag::err_expected_colon); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2483 | // We must manually skip to a ']', otherwise the expression skipper will |
| 2484 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 2485 | // the enclosing expression. |
| 2486 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2487 | return ExprError(); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2488 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2489 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 2490 | ConsumeToken(); // Eat the ':'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2491 | /// Parse the expression after ':' |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2492 | |
| 2493 | if (Tok.is(tok::code_completion)) { |
| 2494 | if (SuperLoc.isValid()) |
| 2495 | Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2496 | KeyIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2497 | /*AtArgumentEpression=*/true); |
| 2498 | else if (ReceiverType) |
| 2499 | Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2500 | KeyIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2501 | /*AtArgumentEpression=*/true); |
| 2502 | else |
| 2503 | Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2504 | KeyIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2505 | /*AtArgumentEpression=*/true); |
| 2506 | |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2507 | cutOffParsing(); |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2508 | return ExprError(); |
| 2509 | } |
| 2510 | |
Fariborz Jahanian | 0c7102f | 2013-04-18 23:43:21 +0000 | [diff] [blame] | 2511 | ExprResult Expr; |
| 2512 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
| 2513 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2514 | Expr = ParseBraceInitializer(); |
| 2515 | } else |
| 2516 | Expr = ParseAssignmentExpression(); |
| 2517 | |
| 2518 | ExprResult Res(Expr); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2519 | if (Res.isInvalid()) { |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2520 | // We must manually skip to a ']', otherwise the expression skipper will |
| 2521 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 2522 | // the enclosing expression. |
| 2523 | SkipUntil(tok::r_square); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2524 | return Res; |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 2525 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2526 | |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 2527 | // We have a valid expression. |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2528 | KeyExprs.push_back(Res.release()); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2529 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2530 | // Code completion after each argument. |
| 2531 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2532 | if (SuperLoc.isValid()) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2533 | Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2534 | KeyIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2535 | /*AtArgumentEpression=*/false); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2536 | else if (ReceiverType) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2537 | Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2538 | KeyIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2539 | /*AtArgumentEpression=*/false); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2540 | else |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2541 | Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2542 | KeyIdents, |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2543 | /*AtArgumentEpression=*/false); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2544 | cutOffParsing(); |
Douglas Gregor | 70c5ac7 | 2010-09-20 23:34:21 +0000 | [diff] [blame] | 2545 | return ExprError(); |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 2548 | // Check for another keyword selector. |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 2549 | selIdent = ParseObjCSelectorPiece(Loc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 2550 | if (!selIdent && Tok.isNot(tok::colon)) |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2551 | break; |
| 2552 | // We have a selector or a colon, continue parsing. |
| 2553 | } |
| 2554 | // Parse the, optional, argument list, comma separated. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 2555 | while (Tok.is(tok::comma)) { |
Fariborz Jahanian | 18df0eb | 2012-05-21 22:43:44 +0000 | [diff] [blame] | 2556 | SourceLocation commaLoc = ConsumeToken(); // Eat the ','. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | /// Parse the expression after ',' |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2558 | ExprResult Res(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2559 | if (Res.isInvalid()) { |
Fariborz Jahanian | 18df0eb | 2012-05-21 22:43:44 +0000 | [diff] [blame] | 2560 | if (Tok.is(tok::colon)) { |
| 2561 | Diag(commaLoc, diag::note_extra_comma_message_arg) << |
| 2562 | FixItHint::CreateRemoval(commaLoc); |
| 2563 | } |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2564 | // We must manually skip to a ']', otherwise the expression skipper will |
| 2565 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 2566 | // the enclosing expression. |
| 2567 | SkipUntil(tok::r_square); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2568 | return Res; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 2569 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2570 | |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 2571 | // We have a valid expression. |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2572 | KeyExprs.push_back(Res.release()); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2573 | } |
| 2574 | } else if (!selIdent) { |
| 2575 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2577 | // We must manually skip to a ']', otherwise the expression skipper will |
| 2578 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 2579 | // the enclosing expression. |
| 2580 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2581 | return ExprError(); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2582 | } |
Fariborz Jahanian | 809872e | 2010-03-31 20:22:35 +0000 | [diff] [blame] | 2583 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 2584 | if (Tok.isNot(tok::r_square)) { |
Fariborz Jahanian | 809872e | 2010-03-31 20:22:35 +0000 | [diff] [blame] | 2585 | if (Tok.is(tok::identifier)) |
| 2586 | Diag(Tok, diag::err_expected_colon); |
| 2587 | else |
| 2588 | Diag(Tok, diag::err_expected_rsquare); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2589 | // We must manually skip to a ']', otherwise the expression skipper will |
| 2590 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 2591 | // the enclosing expression. |
| 2592 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2593 | return ExprError(); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 2594 | } |
Fariborz Jahanian | 48f3cc2 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 2596 | SourceLocation RBracLoc = ConsumeBracket(); // consume ']' |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2597 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 2598 | unsigned nKeys = KeyIdents.size(); |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2599 | if (nKeys == 0) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 2600 | KeyIdents.push_back(selIdent); |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2601 | KeyLocs.push_back(Loc); |
| 2602 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 2603 | Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2604 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2605 | if (SuperLoc.isValid()) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2606 | return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2607 | LBracLoc, KeyLocs, RBracLoc, KeyExprs); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2608 | else if (ReceiverType) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2609 | return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2610 | LBracLoc, KeyLocs, RBracLoc, KeyExprs); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2611 | return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2612 | LBracLoc, KeyLocs, RBracLoc, KeyExprs); |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2615 | ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) { |
| 2616 | ExprResult Res(ParseStringLiteralExpression()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2617 | if (Res.isInvalid()) return Res; |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2618 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 2619 | // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string |
| 2620 | // expressions. At this point, we know that the only valid thing that starts |
| 2621 | // with '@' is an @"". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2622 | SmallVector<SourceLocation, 4> AtLocs; |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2623 | ExprVector AtStrings; |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 2624 | AtLocs.push_back(AtLoc); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2625 | AtStrings.push_back(Res.release()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2626 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 2627 | while (Tok.is(tok::at)) { |
| 2628 | AtLocs.push_back(ConsumeToken()); // eat the @. |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2629 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 2630 | // Invalid unless there is a string literal. |
Chris Lattner | 97cf6eb | 2009-02-18 05:56:09 +0000 | [diff] [blame] | 2631 | if (!isTokenStringLiteral()) |
| 2632 | return ExprError(Diag(Tok, diag::err_objc_concat_string)); |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 2633 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2634 | ExprResult Lit(ParseStringLiteralExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2635 | if (Lit.isInvalid()) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2636 | return Lit; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2637 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2638 | AtStrings.push_back(Lit.release()); |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 2639 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2640 | |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2641 | return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(), |
| 2642 | AtStrings.size()); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2643 | } |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 2644 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2645 | /// ParseObjCBooleanLiteral - |
| 2646 | /// objc-scalar-literal : '@' boolean-keyword |
| 2647 | /// ; |
| 2648 | /// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no' |
| 2649 | /// ; |
| 2650 | ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc, |
| 2651 | bool ArgValue) { |
| 2652 | SourceLocation EndLoc = ConsumeToken(); // consume the keyword. |
| 2653 | return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue); |
| 2654 | } |
| 2655 | |
| 2656 | /// ParseObjCCharacterLiteral - |
| 2657 | /// objc-scalar-literal : '@' character-literal |
| 2658 | /// ; |
| 2659 | ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) { |
| 2660 | ExprResult Lit(Actions.ActOnCharacterConstant(Tok)); |
| 2661 | if (Lit.isInvalid()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2662 | return Lit; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2663 | } |
Benjamin Kramer | 8b8d953 | 2012-03-07 00:14:40 +0000 | [diff] [blame] | 2664 | ConsumeToken(); // Consume the literal token. |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2665 | return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | /// ParseObjCNumericLiteral - |
| 2669 | /// objc-scalar-literal : '@' scalar-literal |
| 2670 | /// ; |
| 2671 | /// scalar-literal : | numeric-constant /* any numeric constant. */ |
| 2672 | /// ; |
| 2673 | ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) { |
| 2674 | ExprResult Lit(Actions.ActOnNumericConstant(Tok)); |
| 2675 | if (Lit.isInvalid()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2676 | return Lit; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2677 | } |
Benjamin Kramer | 8b8d953 | 2012-03-07 00:14:40 +0000 | [diff] [blame] | 2678 | ConsumeToken(); // Consume the literal token. |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2679 | return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2682 | /// ParseObjCBoxedExpr - |
| 2683 | /// objc-box-expression: |
| 2684 | /// @( assignment-expression ) |
| 2685 | ExprResult |
| 2686 | Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) { |
| 2687 | if (Tok.isNot(tok::l_paren)) |
| 2688 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@"); |
| 2689 | |
| 2690 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2691 | T.consumeOpen(); |
| 2692 | ExprResult ValueExpr(ParseAssignmentExpression()); |
| 2693 | if (T.consumeClose()) |
| 2694 | return ExprError(); |
Argyrios Kyrtzidis | edd2760 | 2012-05-10 20:02:36 +0000 | [diff] [blame] | 2695 | |
| 2696 | if (ValueExpr.isInvalid()) |
| 2697 | return ExprError(); |
| 2698 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2699 | // Wrap the sub-expression in a parenthesized expression, to distinguish |
| 2700 | // a boxed expression from a literal. |
| 2701 | SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation(); |
| 2702 | ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take()); |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2703 | return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc), |
| 2704 | ValueExpr.take()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2707 | ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) { |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2708 | ExprVector ElementExprs; // array elements. |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2709 | ConsumeBracket(); // consume the l_square. |
| 2710 | |
| 2711 | while (Tok.isNot(tok::r_square)) { |
| 2712 | // Parse list of array element expressions (all must be id types). |
| 2713 | ExprResult Res(ParseAssignmentExpression()); |
| 2714 | if (Res.isInvalid()) { |
| 2715 | // We must manually skip to a ']', otherwise the expression skipper will |
| 2716 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 2717 | // the enclosing expression. |
| 2718 | SkipUntil(tok::r_square); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2719 | return Res; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | // Parse the ellipsis that indicates a pack expansion. |
| 2723 | if (Tok.is(tok::ellipsis)) |
| 2724 | Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken()); |
| 2725 | if (Res.isInvalid()) |
| 2726 | return true; |
| 2727 | |
| 2728 | ElementExprs.push_back(Res.release()); |
| 2729 | |
| 2730 | if (Tok.is(tok::comma)) |
| 2731 | ConsumeToken(); // Eat the ','. |
| 2732 | else if (Tok.isNot(tok::r_square)) |
| 2733 | return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma)); |
| 2734 | } |
| 2735 | SourceLocation EndLoc = ConsumeBracket(); // location of ']' |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2736 | MultiExprArg Args(ElementExprs); |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2737 | return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) { |
| 2741 | SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements. |
| 2742 | ConsumeBrace(); // consume the l_square. |
| 2743 | while (Tok.isNot(tok::r_brace)) { |
| 2744 | // Parse the comma separated key : value expressions. |
| 2745 | ExprResult KeyExpr; |
| 2746 | { |
| 2747 | ColonProtectionRAIIObject X(*this); |
| 2748 | KeyExpr = ParseAssignmentExpression(); |
| 2749 | if (KeyExpr.isInvalid()) { |
| 2750 | // We must manually skip to a '}', otherwise the expression skipper will |
| 2751 | // stop at the '}' when it skips to the ';'. We want it to skip beyond |
| 2752 | // the enclosing expression. |
| 2753 | SkipUntil(tok::r_brace); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2754 | return KeyExpr; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | if (Tok.is(tok::colon)) { |
| 2759 | ConsumeToken(); |
| 2760 | } else { |
Fariborz Jahanian | 0ac0ee9 | 2013-04-18 19:37:43 +0000 | [diff] [blame] | 2761 | Diag(Tok, diag::err_expected_colon); |
| 2762 | SkipUntil(tok::r_brace); |
| 2763 | return ExprError(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | ExprResult ValueExpr(ParseAssignmentExpression()); |
| 2767 | if (ValueExpr.isInvalid()) { |
| 2768 | // We must manually skip to a '}', otherwise the expression skipper will |
| 2769 | // stop at the '}' when it skips to the ';'. We want it to skip beyond |
| 2770 | // the enclosing expression. |
| 2771 | SkipUntil(tok::r_brace); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2772 | return ValueExpr; |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | // Parse the ellipsis that designates this as a pack expansion. |
| 2776 | SourceLocation EllipsisLoc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2777 | if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus) |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2778 | EllipsisLoc = ConsumeToken(); |
| 2779 | |
| 2780 | // We have a valid expression. Collect it in a vector so we can |
| 2781 | // build the argument list. |
| 2782 | ObjCDictionaryElement Element = { |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 2783 | KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2784 | }; |
| 2785 | Elements.push_back(Element); |
| 2786 | |
| 2787 | if (Tok.is(tok::comma)) |
| 2788 | ConsumeToken(); // Eat the ','. |
| 2789 | else if (Tok.isNot(tok::r_brace)) |
| 2790 | return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma)); |
| 2791 | } |
| 2792 | SourceLocation EndLoc = ConsumeBrace(); |
| 2793 | |
| 2794 | // Create the ObjCDictionaryLiteral. |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2795 | return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc), |
| 2796 | Elements.data(), Elements.size()); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 2799 | /// objc-encode-expression: |
Dmitri Gribenko | e23fb90 | 2012-09-12 17:01:48 +0000 | [diff] [blame] | 2800 | /// \@encode ( type-name ) |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2801 | ExprResult |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2802 | Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 2803 | assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2804 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 2805 | SourceLocation EncLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2806 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2807 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2808 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode"); |
| 2809 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2810 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2811 | T.consumeOpen(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2813 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2815 | T.consumeClose(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2817 | if (Ty.isInvalid()) |
| 2818 | return ExprError(); |
| 2819 | |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2820 | return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(), |
| 2821 | Ty.get(), T.getCloseLocation()); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 2822 | } |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 2823 | |
| 2824 | /// objc-protocol-expression |
James Dennett | 17d26a6 | 2012-06-11 06:19:40 +0000 | [diff] [blame] | 2825 | /// \@protocol ( protocol-name ) |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2826 | ExprResult |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2827 | Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) { |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 2828 | SourceLocation ProtoLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2829 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2830 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2831 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol"); |
| 2832 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2833 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2834 | T.consumeOpen(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2835 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2836 | if (Tok.isNot(tok::identifier)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2837 | return ExprError(Diag(Tok, diag::err_expected_ident)); |
| 2838 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2839 | IdentifierInfo *protocolId = Tok.getIdentifierInfo(); |
Argyrios Kyrtzidis | 7d24e28 | 2012-05-16 00:50:02 +0000 | [diff] [blame] | 2840 | SourceLocation ProtoIdLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2841 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2842 | T.consumeClose(); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 2843 | |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2844 | return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc, |
| 2845 | T.getOpenLocation(), ProtoIdLoc, |
| 2846 | T.getCloseLocation()); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 2847 | } |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2848 | |
| 2849 | /// objc-selector-expression |
| 2850 | /// @selector '(' objc-keyword-selector ')' |
Fariborz Jahanian | 48f3cc2 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 2851 | ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) { |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2852 | SourceLocation SelectorLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2853 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 2854 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2855 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector"); |
| 2856 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2857 | SmallVector<IdentifierInfo *, 12> KeyIdents; |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2858 | SourceLocation sLoc; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 2859 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2860 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2861 | T.consumeOpen(); |
| 2862 | |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 2863 | if (Tok.is(tok::code_completion)) { |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2864 | Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2865 | cutOffParsing(); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 2866 | return ExprError(); |
| 2867 | } |
| 2868 | |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 2869 | IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc); |
Chris Lattner | 5add754 | 2010-08-27 22:32:41 +0000 | [diff] [blame] | 2870 | if (!SelIdent && // missing selector name. |
| 2871 | Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2872 | return ExprError(Diag(Tok, diag::err_expected_ident)); |
| 2873 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2874 | KeyIdents.push_back(SelIdent); |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 2875 | unsigned nColons = 0; |
| 2876 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2877 | while (1) { |
Chris Lattner | 5add754 | 2010-08-27 22:32:41 +0000 | [diff] [blame] | 2878 | if (Tok.is(tok::coloncolon)) { // Handle :: in C++. |
| 2879 | ++nColons; |
| 2880 | KeyIdents.push_back(0); |
| 2881 | } else if (Tok.isNot(tok::colon)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2882 | return ExprError(Diag(Tok, diag::err_expected_colon)); |
| 2883 | |
Chris Lattner | 5add754 | 2010-08-27 22:32:41 +0000 | [diff] [blame] | 2884 | ++nColons; |
Chris Lattner | 3b3e1a9 | 2011-03-26 18:11:38 +0000 | [diff] [blame] | 2885 | ConsumeToken(); // Eat the ':' or '::'. |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2886 | if (Tok.is(tok::r_paren)) |
| 2887 | break; |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 2888 | |
| 2889 | if (Tok.is(tok::code_completion)) { |
Dmitri Gribenko | 050315b | 2013-06-16 03:47:57 +0000 | [diff] [blame^] | 2890 | Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2891 | cutOffParsing(); |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 2892 | return ExprError(); |
| 2893 | } |
| 2894 | |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2895 | // Check for another keyword selector. |
| 2896 | SourceLocation Loc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 2897 | SelIdent = ParseObjCSelectorPiece(Loc); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2898 | KeyIdents.push_back(SelIdent); |
Chris Lattner | 3b3e1a9 | 2011-03-26 18:11:38 +0000 | [diff] [blame] | 2899 | if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon)) |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2900 | break; |
| 2901 | } |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 2902 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2903 | T.consumeClose(); |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 2904 | Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]); |
Nico Weber | d32b94d | 2012-12-31 00:28:03 +0000 | [diff] [blame] | 2905 | return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, |
| 2906 | T.getOpenLocation(), |
Fariborz Jahanian | 48f3cc2 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 2907 | T.getCloseLocation()); |
Gabor Greif | 58065b2 | 2007-10-19 15:38:32 +0000 | [diff] [blame] | 2908 | } |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2909 | |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2910 | void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) { |
| 2911 | // MCDecl might be null due to error in method or c-function prototype, etc. |
| 2912 | Decl *MCDecl = LM.D; |
| 2913 | bool skip = MCDecl && |
| 2914 | ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) || |
| 2915 | (!parseMethod && Actions.isObjCMethodDecl(MCDecl))); |
| 2916 | if (skip) |
| 2917 | return; |
| 2918 | |
Argyrios Kyrtzidis | a24195a | 2011-12-17 04:13:18 +0000 | [diff] [blame] | 2919 | // Save the current token position. |
| 2920 | SourceLocation OrigLoc = Tok.getLocation(); |
| 2921 | |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2922 | assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!"); |
| 2923 | // Append the current token at the end of the new token stream so that it |
| 2924 | // doesn't get lost. |
| 2925 | LM.Toks.push_back(Tok); |
| 2926 | PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); |
| 2927 | |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2928 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 2929 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2930 | |
Fariborz Jahanian | 9e5df31 | 2012-08-10 21:15:06 +0000 | [diff] [blame] | 2931 | assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || |
| 2932 | Tok.is(tok::colon)) && |
| 2933 | "Inline objective-c method not starting with '{' or 'try' or ':'"); |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2934 | // Enter a scope for the method or c-fucntion body. |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2935 | ParseScope BodyScope(this, |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2936 | parseMethod |
| 2937 | ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope |
| 2938 | : Scope::FnScope|Scope::DeclScope); |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2939 | |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2940 | // Tell the actions module that we have entered a method or c-function definition |
| 2941 | // with the specified Declarator for the method/function. |
Fariborz Jahanian | 8c6cb46 | 2012-08-08 23:41:08 +0000 | [diff] [blame] | 2942 | if (parseMethod) |
| 2943 | Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl); |
| 2944 | else |
| 2945 | Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl); |
Fariborz Jahanian | 2eb362b | 2012-08-10 18:10:56 +0000 | [diff] [blame] | 2946 | if (Tok.is(tok::kw_try)) |
| 2947 | MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope); |
Fariborz Jahanian | 9e5df31 | 2012-08-10 21:15:06 +0000 | [diff] [blame] | 2948 | else { |
| 2949 | if (Tok.is(tok::colon)) |
| 2950 | ParseConstructorInitializer(MCDecl); |
Fariborz Jahanian | 2eb362b | 2012-08-10 18:10:56 +0000 | [diff] [blame] | 2951 | MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope); |
Fariborz Jahanian | 9e5df31 | 2012-08-10 21:15:06 +0000 | [diff] [blame] | 2952 | } |
Fariborz Jahanian | 6940972 | 2012-08-09 21:12:39 +0000 | [diff] [blame] | 2953 | |
Argyrios Kyrtzidis | a24195a | 2011-12-17 04:13:18 +0000 | [diff] [blame] | 2954 | if (Tok.getLocation() != OrigLoc) { |
| 2955 | // Due to parsing error, we either went over the cached tokens or |
| 2956 | // there are still cached tokens left. If it's the latter case skip the |
| 2957 | // leftover tokens. |
| 2958 | // Since this is an uncommon situation that should be avoided, use the |
| 2959 | // expensive isBeforeInTranslationUnit call. |
| 2960 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 2961 | OrigLoc)) |
| 2962 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
| 2963 | ConsumeAnyToken(); |
| 2964 | } |
| 2965 | |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2966 | return; |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 2967 | } |