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