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