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 | |
| 14 | #include "clang/Parse/Parser.h" |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 15 | #include "clang/Parse/DeclSpec.h" |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 17 | #include "clang/Parse/ParseDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | |
Chris Lattner | 891dca6 | 2008-12-08 21:53:24 +0000 | [diff] [blame] | 22 | /// ParseObjCAtDirectives - Handle parts of the external-declaration production: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | /// external-declaration: [C99 6.9] |
| 24 | /// [OBJC] objc-class-definition |
Steve Naroff | 91fa0b7 | 2007-10-29 21:39:29 +0000 | [diff] [blame] | 25 | /// [OBJC] objc-class-declaration |
| 26 | /// [OBJC] objc-alias-declaration |
| 27 | /// [OBJC] objc-protocol-definition |
| 28 | /// [OBJC] objc-method-definition |
| 29 | /// [OBJC] '@' 'end' |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 30 | Parser::DeclPtrTy Parser::ParseObjCAtDirectives() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 33 | if (Tok.is(tok::code_completion)) { |
| 34 | Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false); |
| 35 | ConsumeToken(); |
| 36 | } |
| 37 | |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 38 | switch (Tok.getObjCKeywordID()) { |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 39 | case tok::objc_class: |
| 40 | return ParseObjCAtClassDeclaration(AtLoc); |
| 41 | case tok::objc_interface: |
| 42 | return ParseObjCAtInterfaceDeclaration(AtLoc); |
| 43 | case tok::objc_protocol: |
| 44 | return ParseObjCAtProtocolDeclaration(AtLoc); |
| 45 | case tok::objc_implementation: |
| 46 | return ParseObjCAtImplementationDeclaration(AtLoc); |
| 47 | case tok::objc_end: |
| 48 | return ParseObjCAtEndDeclaration(AtLoc); |
| 49 | case tok::objc_compatibility_alias: |
| 50 | return ParseObjCAtAliasDeclaration(AtLoc); |
| 51 | case tok::objc_synthesize: |
| 52 | return ParseObjCPropertySynthesize(AtLoc); |
| 53 | case tok::objc_dynamic: |
| 54 | return ParseObjCPropertyDynamic(AtLoc); |
| 55 | default: |
| 56 | Diag(AtLoc, diag::err_unexpected_at); |
| 57 | SkipUntil(tok::semi); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 58 | return DeclPtrTy(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | /// objc-class-declaration: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | /// '@' 'class' identifier-list ';' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 66 | Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | ConsumeToken(); // the identifier "class" |
| 68 | llvm::SmallVector<IdentifierInfo *, 8> ClassNames; |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 69 | llvm::SmallVector<SourceLocation, 8> ClassLocs; |
| 70 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | while (1) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 73 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | Diag(Tok, diag::err_expected_ident); |
| 75 | SkipUntil(tok::semi); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 76 | return DeclPtrTy(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | ClassNames.push_back(Tok.getIdentifierInfo()); |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 79 | ClassLocs.push_back(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 82 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | ConsumeToken(); |
| 86 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | // Consume the ';'. |
| 89 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class")) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 90 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 92 | return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(), |
| 93 | ClassLocs.data(), |
| 94 | ClassNames.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 97 | /// |
| 98 | /// objc-interface: |
| 99 | /// objc-class-interface-attributes[opt] objc-class-interface |
| 100 | /// objc-category-interface |
| 101 | /// |
| 102 | /// objc-class-interface: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | /// '@' 'interface' identifier objc-superclass[opt] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 104 | /// objc-protocol-refs[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | /// objc-class-instance-variables[opt] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 106 | /// objc-interface-decl-list |
| 107 | /// @end |
| 108 | /// |
| 109 | /// objc-category-interface: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | /// '@' 'interface' identifier '(' identifier[opt] ')' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 111 | /// objc-protocol-refs[opt] |
| 112 | /// objc-interface-decl-list |
| 113 | /// @end |
| 114 | /// |
| 115 | /// objc-superclass: |
| 116 | /// ':' identifier |
| 117 | /// |
| 118 | /// objc-class-interface-attributes: |
| 119 | /// __attribute__((visibility("default"))) |
| 120 | /// __attribute__((visibility("hidden"))) |
| 121 | /// __attribute__((deprecated)) |
| 122 | /// __attribute__((unavailable)) |
| 123 | /// __attribute__((objc_exception)) - used by NSException on 64-bit |
| 124 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 125 | Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration( |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 126 | SourceLocation atLoc, AttributeList *attrList) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 127 | assert(Tok.isObjCAtKeyword(tok::objc_interface) && |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 128 | "ParseObjCAtInterfaceDeclaration(): Expected @interface"); |
| 129 | ConsumeToken(); // the "interface" identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 131 | // Code completion after '@interface'. |
| 132 | if (Tok.is(tok::code_completion)) { |
| 133 | Actions.CodeCompleteObjCInterfaceDecl(CurScope); |
| 134 | ConsumeToken(); |
| 135 | } |
| 136 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 137 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 138 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 139 | return DeclPtrTy(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 140 | } |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 141 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 142 | // We have a class or category name - consume it. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 143 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 144 | SourceLocation nameLoc = ConsumeToken(); |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 145 | bool Err = false; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 146 | if (Tok.is(tok::l_paren)) { // we have a category. |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 147 | SourceLocation lparenLoc = ConsumeParen(); |
| 148 | SourceLocation categoryLoc, rparenLoc; |
| 149 | IdentifierInfo *categoryId = 0; |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 150 | if (Tok.is(tok::code_completion)) { |
| 151 | Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId); |
| 152 | ConsumeToken(); |
| 153 | } |
| 154 | |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 155 | // For ObjC2, the category name is optional (not an error). |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 156 | if (Tok.is(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 157 | categoryId = Tok.getIdentifierInfo(); |
| 158 | categoryLoc = ConsumeToken(); |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 159 | } |
| 160 | else if (isKnownToBeTypeSpecifier(Tok)) { |
| 161 | // Fall thru after diagnosing for better error recovery. |
| 162 | Diag(Tok, diag::err_expected_minus_or_plus); |
| 163 | ConsumeToken(); |
| 164 | Err = true; |
| 165 | } |
| 166 | else if (!getLang().ObjC2) { |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 167 | Diag(Tok, diag::err_expected_ident); // missing category name. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 168 | return DeclPtrTy(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 169 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 170 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 171 | Diag(Tok, diag::err_expected_rparen); |
| 172 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 173 | return DeclPtrTy(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 174 | } |
| 175 | rparenLoc = ConsumeParen(); |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 176 | if (!Err) { |
| 177 | // Next, we need to check for any protocol references. |
| 178 | SourceLocation LAngleLoc, EndProtoLoc; |
| 179 | llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs; |
| 180 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 181 | if (Tok.is(tok::less) && |
| 182 | ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 183 | LAngleLoc, EndProtoLoc)) |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 184 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 186 | if (attrList) // categories don't support attributes. |
| 187 | Diag(Tok, diag::err_objc_no_attributes_on_category); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 189 | DeclPtrTy CategoryType = |
| 190 | Actions.ActOnStartCategoryInterface(atLoc, |
| 191 | nameId, nameLoc, |
| 192 | categoryId, categoryLoc, |
| 193 | ProtocolRefs.data(), |
| 194 | ProtocolRefs.size(), |
| 195 | ProtocolLocs.data(), |
| 196 | EndProtoLoc); |
| 197 | if (Tok.is(tok::l_brace)) |
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 198 | ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, |
| 199 | atLoc); |
| 200 | |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 201 | ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword); |
| 202 | return CategoryType; |
| 203 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 204 | } |
| 205 | // Parse a class interface. |
| 206 | IdentifierInfo *superClassId = 0; |
| 207 | SourceLocation superClassLoc; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 208 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 209 | if (Tok.is(tok::colon)) { // a super class is specified. |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 210 | ConsumeToken(); |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 211 | |
| 212 | // Code completion of superclass names. |
| 213 | if (Tok.is(tok::code_completion)) { |
| 214 | Actions.CodeCompleteObjCSuperclass(CurScope, nameId); |
| 215 | ConsumeToken(); |
| 216 | } |
| 217 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 218 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 219 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 220 | return DeclPtrTy(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 221 | } |
| 222 | superClassId = Tok.getIdentifierInfo(); |
| 223 | superClassLoc = ConsumeToken(); |
| 224 | } |
| 225 | // Next, we need to check for any protocol references. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 226 | llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs; |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 227 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 228 | SourceLocation LAngleLoc, EndProtoLoc; |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 229 | if (Tok.is(tok::less) && |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 230 | ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, |
| 231 | LAngleLoc, EndProtoLoc)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 232 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
| 234 | DeclPtrTy ClsType = |
| 235 | Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc, |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 236 | superClassId, superClassLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 237 | ProtocolRefs.data(), ProtocolRefs.size(), |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 238 | ProtocolLocs.data(), |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 239 | EndProtoLoc, attrList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 241 | if (Tok.is(tok::l_brace)) |
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 242 | ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 243 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 244 | ParseObjCInterfaceDeclList(ClsType, tok::objc_interface); |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 245 | return Err ? DeclPtrTy() : ClsType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 246 | } |
| 247 | |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 248 | /// The Objective-C property callback. This should be defined where |
| 249 | /// it's used, but instead it's been lifted to here to support VS2005. |
| 250 | struct Parser::ObjCPropertyCallback : FieldCallback { |
| 251 | Parser &P; |
| 252 | DeclPtrTy IDecl; |
| 253 | llvm::SmallVectorImpl<DeclPtrTy> &Props; |
| 254 | ObjCDeclSpec &OCDS; |
| 255 | SourceLocation AtLoc; |
| 256 | tok::ObjCKeywordKind MethodImplKind; |
| 257 | |
| 258 | ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl, |
| 259 | llvm::SmallVectorImpl<DeclPtrTy> &Props, |
| 260 | ObjCDeclSpec &OCDS, SourceLocation AtLoc, |
| 261 | tok::ObjCKeywordKind MethodImplKind) : |
| 262 | P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc), |
| 263 | MethodImplKind(MethodImplKind) { |
| 264 | } |
| 265 | |
| 266 | DeclPtrTy invoke(FieldDeclarator &FD) { |
| 267 | if (FD.D.getIdentifier() == 0) { |
| 268 | P.Diag(AtLoc, diag::err_objc_property_requires_field_name) |
| 269 | << FD.D.getSourceRange(); |
| 270 | return DeclPtrTy(); |
| 271 | } |
| 272 | if (FD.BitfieldSize) { |
| 273 | P.Diag(AtLoc, diag::err_objc_property_bitfield) |
| 274 | << FD.D.getSourceRange(); |
| 275 | return DeclPtrTy(); |
| 276 | } |
| 277 | |
| 278 | // Install the property declarator into interfaceDecl. |
| 279 | IdentifierInfo *SelName = |
| 280 | OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier(); |
| 281 | |
| 282 | Selector GetterSel = |
| 283 | P.PP.getSelectorTable().getNullarySelector(SelName); |
| 284 | IdentifierInfo *SetterName = OCDS.getSetterName(); |
| 285 | Selector SetterSel; |
| 286 | if (SetterName) |
| 287 | SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName); |
| 288 | else |
| 289 | SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(), |
| 290 | P.PP.getSelectorTable(), |
| 291 | FD.D.getIdentifier()); |
| 292 | bool isOverridingProperty = false; |
| 293 | DeclPtrTy Property = |
| 294 | P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS, |
| 295 | GetterSel, SetterSel, IDecl, |
| 296 | &isOverridingProperty, |
| 297 | MethodImplKind); |
| 298 | if (!isOverridingProperty) |
| 299 | Props.push_back(Property); |
| 300 | |
| 301 | return Property; |
| 302 | } |
| 303 | }; |
| 304 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 305 | /// objc-interface-decl-list: |
| 306 | /// empty |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 307 | /// objc-interface-decl-list objc-property-decl [OBJC2] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 308 | /// objc-interface-decl-list objc-method-requirement [OBJC2] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 309 | /// objc-interface-decl-list objc-method-proto ';' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 310 | /// objc-interface-decl-list declaration |
| 311 | /// objc-interface-decl-list ';' |
| 312 | /// |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 313 | /// objc-method-requirement: [OBJC2] |
| 314 | /// @required |
| 315 | /// @optional |
| 316 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 317 | void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl, |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 318 | tok::ObjCKeywordKind contextKey) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 319 | llvm::SmallVector<DeclPtrTy, 32> allMethods; |
| 320 | llvm::SmallVector<DeclPtrTy, 16> allProperties; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 321 | llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables; |
Fariborz Jahanian | 0093359 | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 322 | tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 324 | SourceRange AtEnd; |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 325 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 326 | while (1) { |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 327 | // If this is a method prototype, parse it. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 328 | if (Tok.is(tok::minus) || Tok.is(tok::plus)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | DeclPtrTy methodPrototype = |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 330 | ParseObjCMethodPrototype(interfaceDecl, MethodImplKind); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 331 | allMethods.push_back(methodPrototype); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 332 | // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for |
| 333 | // method definitions. |
Chris Lattner | b6d74a1 | 2009-02-15 22:24:30 +0000 | [diff] [blame] | 334 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto, |
| 335 | "", tok::semi); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 336 | continue; |
| 337 | } |
Fariborz Jahanian | 05511fa | 2010-04-02 23:15:40 +0000 | [diff] [blame] | 338 | if (Tok.is(tok::l_paren)) { |
| 339 | Diag(Tok, diag::err_expected_minus_or_plus); |
| 340 | DeclPtrTy methodPrototype = ParseObjCMethodDecl(Tok.getLocation(), |
| 341 | tok::minus, |
| 342 | interfaceDecl, |
| 343 | MethodImplKind); |
| 344 | continue; |
| 345 | } |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 346 | // Ignore excess semicolons. |
| 347 | if (Tok.is(tok::semi)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 348 | ConsumeToken(); |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 349 | continue; |
| 350 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 352 | // If we got to the end of the file, exit the loop. |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 353 | if (Tok.is(tok::eof)) |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 354 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | b6ac245 | 2010-01-13 21:24:21 +0000 | [diff] [blame] | 356 | // Code completion within an Objective-C interface. |
| 357 | if (Tok.is(tok::code_completion)) { |
| 358 | Actions.CodeCompleteOrdinaryName(CurScope, |
| 359 | ObjCImpDecl? Action::CCC_ObjCImplementation |
| 360 | : Action::CCC_ObjCInterface); |
| 361 | ConsumeToken(); |
| 362 | } |
| 363 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 364 | // If we don't have an @ directive, parse it as a function definition. |
| 365 | if (Tok.isNot(tok::at)) { |
Chris Lattner | 1fd8011 | 2009-01-09 04:34:13 +0000 | [diff] [blame] | 366 | // The code below does not consume '}'s because it is afraid of eating the |
| 367 | // end of a namespace. Because of the way this code is structured, an |
| 368 | // erroneous r_brace would cause an infinite loop if not handled here. |
| 369 | if (Tok.is(tok::r_brace)) |
| 370 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 372 | // FIXME: as the name implies, this rule allows function definitions. |
| 373 | // We could pass a flag or check for functions during semantic analysis. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 374 | allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0)); |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 375 | continue; |
| 376 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 378 | // Otherwise, we have an @ directive, eat the @. |
| 379 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Douglas Gregor | c464ae8 | 2009-12-07 09:27:33 +0000 | [diff] [blame] | 380 | if (Tok.is(tok::code_completion)) { |
| 381 | Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true); |
| 382 | ConsumeToken(); |
| 383 | break; |
| 384 | } |
| 385 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 386 | tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 388 | if (DirectiveKind == tok::objc_end) { // @end -> terminate list |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 389 | AtEnd.setBegin(AtLoc); |
| 390 | AtEnd.setEnd(Tok.getLocation()); |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 391 | break; |
Douglas Gregor | c3d43b7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 392 | } else if (DirectiveKind == tok::objc_not_keyword) { |
| 393 | Diag(Tok, diag::err_objc_unknown_at); |
| 394 | SkipUntil(tok::semi); |
| 395 | continue; |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 396 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 398 | // Eat the identifier. |
| 399 | ConsumeToken(); |
| 400 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 401 | switch (DirectiveKind) { |
| 402 | default: |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 403 | // FIXME: If someone forgets an @end on a protocol, this loop will |
| 404 | // continue to eat up tons of stuff and spew lots of nonsense errors. It |
| 405 | // would probably be better to bail out if we saw an @class or @interface |
| 406 | // or something like that. |
Chris Lattner | f6ed855 | 2008-10-20 07:22:18 +0000 | [diff] [blame] | 407 | Diag(AtLoc, diag::err_objc_illegal_interface_qual); |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 408 | // Skip until we see an '@' or '}' or ';'. |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 409 | SkipUntil(tok::r_brace, tok::at); |
| 410 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 412 | case tok::objc_required: |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 413 | case tok::objc_optional: |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 414 | // This is only valid on protocols. |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 415 | // FIXME: Should this check for ObjC2 being enabled? |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 416 | if (contextKey != tok::objc_protocol) |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 417 | Diag(AtLoc, diag::err_objc_directive_only_in_protocol); |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 418 | else |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 419 | MethodImplKind = DirectiveKind; |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 420 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 422 | case tok::objc_property: |
Chris Lattner | f6ed855 | 2008-10-20 07:22:18 +0000 | [diff] [blame] | 423 | if (!getLang().ObjC2) |
| 424 | Diag(AtLoc, diag::err_objc_propertoes_require_objc2); |
| 425 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 426 | ObjCDeclSpec OCDS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | // Parse property attribute list, if any. |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 428 | if (Tok.is(tok::l_paren)) |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 429 | ParseObjCPropertyAttribute(OCDS, interfaceDecl, |
| 430 | allMethods.data(), allMethods.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
John McCall | d001454 | 2009-12-03 22:31:13 +0000 | [diff] [blame] | 432 | ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties, |
| 433 | OCDS, AtLoc, MethodImplKind); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 434 | |
Chris Lattner | e82a10f | 2008-10-20 05:46:22 +0000 | [diff] [blame] | 435 | // Parse all the comma separated declarators. |
| 436 | DeclSpec DS; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 437 | ParseStructDeclaration(DS, Callback); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | a1fed7e | 2008-10-20 06:15:13 +0000 | [diff] [blame] | 439 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "", |
| 440 | tok::at); |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 441 | break; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 442 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 443 | } |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 444 | |
| 445 | // We break out of the big loop in two cases: when we see @end or when we see |
| 446 | // 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] | 447 | if (Tok.is(tok::code_completion)) { |
| 448 | Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true); |
| 449 | ConsumeToken(); |
| 450 | } else if (Tok.isObjCAtKeyword(tok::objc_end)) |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 451 | ConsumeToken(); // the "end" identifier |
| 452 | else |
| 453 | Diag(Tok, diag::err_objc_missing_end); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Chris Lattner | a2449b2 | 2008-10-20 05:57:40 +0000 | [diff] [blame] | 455 | // Insert collected methods declarations into the @interface object. |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 456 | // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 457 | Actions.ActOnAtEnd(AtEnd, interfaceDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | allMethods.data(), allMethods.size(), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 459 | allProperties.data(), allProperties.size(), |
| 460 | allTUVariables.data(), allTUVariables.size()); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 463 | /// Parse property attribute declarations. |
| 464 | /// |
| 465 | /// property-attr-decl: '(' property-attrlist ')' |
| 466 | /// property-attrlist: |
| 467 | /// property-attribute |
| 468 | /// property-attrlist ',' property-attribute |
| 469 | /// property-attribute: |
| 470 | /// getter '=' identifier |
| 471 | /// setter '=' identifier ':' |
| 472 | /// readonly |
| 473 | /// readwrite |
| 474 | /// assign |
| 475 | /// retain |
| 476 | /// copy |
| 477 | /// nonatomic |
| 478 | /// |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 479 | void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl, |
| 480 | DeclPtrTy *Methods, |
| 481 | unsigned NumMethods) { |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 482 | assert(Tok.getKind() == tok::l_paren); |
Chris Lattner | dd5b5f2 | 2008-10-20 07:00:43 +0000 | [diff] [blame] | 483 | SourceLocation LHSLoc = ConsumeParen(); // consume '(' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Chris Lattner | cd9f4b3 | 2008-10-20 07:15:22 +0000 | [diff] [blame] | 485 | while (1) { |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 486 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | a93b108 | 2009-11-18 23:08:07 +0000 | [diff] [blame] | 487 | Actions.CodeCompleteObjCPropertyFlags(CurScope, DS); |
Steve Naroff | ece8e71 | 2009-10-08 21:55:05 +0000 | [diff] [blame] | 488 | ConsumeToken(); |
| 489 | } |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 490 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Chris Lattner | f6ed855 | 2008-10-20 07:22:18 +0000 | [diff] [blame] | 492 | // If this is not an identifier at all, bail out early. |
| 493 | if (II == 0) { |
| 494 | MatchRHSPunctuation(tok::r_paren, LHSLoc); |
| 495 | return; |
| 496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 498 | SourceLocation AttrName = ConsumeToken(); // consume last attribute name |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 500 | if (II->isStr("readonly")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 501 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 502 | else if (II->isStr("assign")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 503 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 504 | else if (II->isStr("readwrite")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 505 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 506 | else if (II->isStr("retain")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 507 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 508 | else if (II->isStr("copy")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 509 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 510 | else if (II->isStr("nonatomic")) |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 511 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic); |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 512 | else if (II->isStr("getter") || II->isStr("setter")) { |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 513 | // getter/setter require extra treatment. |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 514 | if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "", |
| 515 | tok::r_paren)) |
Chris Lattner | dd5b5f2 | 2008-10-20 07:00:43 +0000 | [diff] [blame] | 516 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Douglas Gregor | 4ad9685 | 2009-11-19 07:41:15 +0000 | [diff] [blame] | 518 | if (Tok.is(tok::code_completion)) { |
| 519 | if (II->getNameStart()[0] == 's') |
| 520 | Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl, |
| 521 | Methods, NumMethods); |
| 522 | else |
| 523 | Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl, |
| 524 | Methods, NumMethods); |
| 525 | ConsumeToken(); |
| 526 | } |
| 527 | |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 528 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 529 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 530 | SkipUntil(tok::r_paren); |
| 531 | return; |
| 532 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 534 | if (II->getNameStart()[0] == 's') { |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 535 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter); |
| 536 | DS.setSetterName(Tok.getIdentifierInfo()); |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 537 | ConsumeToken(); // consume method name |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Fariborz Jahanian | e0097db | 2010-02-15 22:20:11 +0000 | [diff] [blame] | 539 | if (ExpectAndConsume(tok::colon, |
| 540 | diag::err_expected_colon_after_setter_name, "", |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 541 | tok::r_paren)) |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 542 | return; |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 543 | } else { |
| 544 | DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter); |
| 545 | DS.setGetterName(Tok.getIdentifierInfo()); |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 546 | ConsumeToken(); // consume method name |
Chris Lattner | 8ca329c | 2008-10-20 07:24:39 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | e00da7c | 2008-10-20 07:39:53 +0000 | [diff] [blame] | 548 | } else { |
Chris Lattner | a9500f0 | 2008-11-19 07:49:38 +0000 | [diff] [blame] | 549 | Diag(AttrName, diag::err_objc_expected_property_attr) << II; |
Chris Lattner | cd9f4b3 | 2008-10-20 07:15:22 +0000 | [diff] [blame] | 550 | SkipUntil(tok::r_paren); |
| 551 | return; |
Chris Lattner | cd9f4b3 | 2008-10-20 07:15:22 +0000 | [diff] [blame] | 552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 554 | if (Tok.isNot(tok::comma)) |
| 555 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 557 | ConsumeToken(); |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 558 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 156b061 | 2008-10-20 07:37:22 +0000 | [diff] [blame] | 560 | MatchRHSPunctuation(tok::r_paren, LHSLoc); |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 563 | /// objc-method-proto: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | /// objc-instance-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 565 | /// objc-class-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 566 | /// |
| 567 | /// objc-instance-method: '-' |
| 568 | /// objc-class-method: '+' |
| 569 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 570 | /// objc-method-attributes: [OBJC2] |
| 571 | /// __attribute__((deprecated)) |
| 572 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 574 | tok::ObjCKeywordKind MethodImplKind) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 575 | assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 576 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | tok::TokenKind methodType = Tok.getKind(); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 578 | SourceLocation mLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 580 | DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 581 | // Since this rule is used for both method declarations and definitions, |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 582 | // the caller is (optionally) responsible for consuming the ';'. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 583 | return MDecl; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /// objc-selector: |
| 587 | /// identifier |
| 588 | /// one of |
| 589 | /// enum struct union if else while do for switch case default |
| 590 | /// break continue return goto asm sizeof typeof __alignof |
| 591 | /// unsigned long const short volatile signed restrict _Complex |
| 592 | /// in out inout bycopy byref oneway int char float double void _Bool |
| 593 | /// |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 594 | IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 595 | switch (Tok.getKind()) { |
| 596 | default: |
| 597 | return 0; |
| 598 | case tok::identifier: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 599 | case tok::kw_asm: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 600 | case tok::kw_auto: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 601 | case tok::kw_bool: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 602 | case tok::kw_break: |
| 603 | case tok::kw_case: |
| 604 | case tok::kw_catch: |
| 605 | case tok::kw_char: |
| 606 | case tok::kw_class: |
| 607 | case tok::kw_const: |
| 608 | case tok::kw_const_cast: |
| 609 | case tok::kw_continue: |
| 610 | case tok::kw_default: |
| 611 | case tok::kw_delete: |
| 612 | case tok::kw_do: |
| 613 | case tok::kw_double: |
| 614 | case tok::kw_dynamic_cast: |
| 615 | case tok::kw_else: |
| 616 | case tok::kw_enum: |
| 617 | case tok::kw_explicit: |
| 618 | case tok::kw_export: |
| 619 | case tok::kw_extern: |
| 620 | case tok::kw_false: |
| 621 | case tok::kw_float: |
| 622 | case tok::kw_for: |
| 623 | case tok::kw_friend: |
| 624 | case tok::kw_goto: |
| 625 | case tok::kw_if: |
| 626 | case tok::kw_inline: |
| 627 | case tok::kw_int: |
| 628 | case tok::kw_long: |
| 629 | case tok::kw_mutable: |
| 630 | case tok::kw_namespace: |
| 631 | case tok::kw_new: |
| 632 | case tok::kw_operator: |
| 633 | case tok::kw_private: |
| 634 | case tok::kw_protected: |
| 635 | case tok::kw_public: |
| 636 | case tok::kw_register: |
| 637 | case tok::kw_reinterpret_cast: |
| 638 | case tok::kw_restrict: |
| 639 | case tok::kw_return: |
| 640 | case tok::kw_short: |
| 641 | case tok::kw_signed: |
| 642 | case tok::kw_sizeof: |
| 643 | case tok::kw_static: |
| 644 | case tok::kw_static_cast: |
| 645 | case tok::kw_struct: |
| 646 | case tok::kw_switch: |
| 647 | case tok::kw_template: |
| 648 | case tok::kw_this: |
| 649 | case tok::kw_throw: |
| 650 | case tok::kw_true: |
| 651 | case tok::kw_try: |
| 652 | case tok::kw_typedef: |
| 653 | case tok::kw_typeid: |
| 654 | case tok::kw_typename: |
| 655 | case tok::kw_typeof: |
| 656 | case tok::kw_union: |
| 657 | case tok::kw_unsigned: |
| 658 | case tok::kw_using: |
| 659 | case tok::kw_virtual: |
| 660 | case tok::kw_void: |
| 661 | case tok::kw_volatile: |
| 662 | case tok::kw_wchar_t: |
| 663 | case tok::kw_while: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 664 | case tok::kw__Bool: |
| 665 | case tok::kw__Complex: |
Anders Carlsson | ef048ef | 2008-08-23 21:00:01 +0000 | [diff] [blame] | 666 | case tok::kw___alignof: |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 667 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 668 | SelectorLoc = ConsumeToken(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 669 | return II; |
Fariborz Jahanian | d064951 | 2007-09-27 19:52:15 +0000 | [diff] [blame] | 670 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 673 | /// objc-for-collection-in: 'in' |
| 674 | /// |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 675 | bool Parser::isTokIdentifier_in() const { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 676 | // FIXME: May have to do additional look-ahead to only allow for |
| 677 | // valid tokens following an 'in'; such as an identifier, unary operators, |
| 678 | // '[' etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | return (getLang().ObjC2 && Tok.is(tok::identifier) && |
Chris Lattner | 5ffb14b | 2008-08-23 02:02:23 +0000 | [diff] [blame] | 680 | Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]); |
Fariborz Jahanian | 0196cab | 2008-01-02 22:54:34 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 683 | /// ParseObjCTypeQualifierList - This routine parses the objective-c's type |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 684 | /// qualifier list and builds their bitmask representation in the input |
| 685 | /// argument. |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 686 | /// |
| 687 | /// objc-type-qualifiers: |
| 688 | /// objc-type-qualifier |
| 689 | /// objc-type-qualifiers objc-type-qualifier |
| 690 | /// |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 691 | void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) { |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 692 | while (1) { |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 693 | if (Tok.isNot(tok::identifier)) |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 694 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 696 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 697 | for (unsigned i = 0; i != objc_NumQuals; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 698 | if (II != ObjCTypeQuals[i]) |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 699 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 701 | ObjCDeclSpec::ObjCDeclQualifier Qual; |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 702 | switch (i) { |
| 703 | default: assert(0 && "Unknown decl qualifier"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 704 | case objc_in: Qual = ObjCDeclSpec::DQ_In; break; |
| 705 | case objc_out: Qual = ObjCDeclSpec::DQ_Out; break; |
| 706 | case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break; |
| 707 | case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break; |
| 708 | case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break; |
| 709 | case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break; |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 710 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 711 | DS.setObjCDeclQualifier(Qual); |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 712 | ConsumeToken(); |
| 713 | II = 0; |
| 714 | break; |
| 715 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | |
Chris Lattner | e8b724d | 2007-12-12 06:56:32 +0000 | [diff] [blame] | 717 | // If this wasn't a recognized qualifier, bail out. |
| 718 | if (II) return; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | /// objc-type-name: |
| 723 | /// '(' objc-type-qualifiers[opt] type-name ')' |
| 724 | /// '(' objc-type-qualifiers[opt] ')' |
| 725 | /// |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 726 | Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 727 | assert(Tok.is(tok::l_paren) && "expected ("); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 4a76b29 | 2008-10-22 03:52:06 +0000 | [diff] [blame] | 729 | SourceLocation LParenLoc = ConsumeParen(); |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 730 | SourceLocation TypeStartLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | |
Fariborz Jahanian | 19d74e1 | 2007-10-31 21:59:43 +0000 | [diff] [blame] | 732 | // Parse type qualifiers, in, inout, etc. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 733 | ParseObjCTypeQualifierList(DS); |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 734 | |
Chris Lattner | 4a76b29 | 2008-10-22 03:52:06 +0000 | [diff] [blame] | 735 | TypeTy *Ty = 0; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 736 | if (isTypeSpecifierQualifier()) { |
| 737 | TypeResult TypeSpec = ParseTypeName(); |
| 738 | if (!TypeSpec.isInvalid()) |
| 739 | Ty = TypeSpec.get(); |
| 740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Steve Naroff | d7333c2 | 2008-10-21 14:15:04 +0000 | [diff] [blame] | 742 | if (Tok.is(tok::r_paren)) |
Chris Lattner | 4a76b29 | 2008-10-22 03:52:06 +0000 | [diff] [blame] | 743 | ConsumeParen(); |
| 744 | else if (Tok.getLocation() == TypeStartLoc) { |
| 745 | // 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] | 746 | Diag(Tok, diag::err_expected_type); |
Chris Lattner | 4a76b29 | 2008-10-22 03:52:06 +0000 | [diff] [blame] | 747 | SkipUntil(tok::r_paren); |
| 748 | } else { |
| 749 | // Otherwise, we found *something*, but didn't get a ')' in the right |
| 750 | // place. Emit an error then return what we have as the type. |
| 751 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 752 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 753 | return Ty; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /// objc-method-decl: |
| 757 | /// objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 758 | /// objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 759 | /// objc-type-name objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 760 | /// objc-type-name objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 761 | /// |
| 762 | /// objc-keyword-selector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | /// objc-keyword-decl |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 764 | /// objc-keyword-selector objc-keyword-decl |
| 765 | /// |
| 766 | /// objc-keyword-decl: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 767 | /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 768 | /// objc-selector ':' objc-keyword-attributes[opt] identifier |
| 769 | /// ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 770 | /// ':' objc-keyword-attributes[opt] identifier |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 771 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 772 | /// objc-parmlist: |
| 773 | /// objc-parms objc-ellipsis[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 774 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 775 | /// objc-parms: |
| 776 | /// objc-parms , parameter-declaration |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 777 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 778 | /// objc-ellipsis: |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 779 | /// , ... |
| 780 | /// |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 781 | /// objc-keyword-attributes: [OBJC2] |
| 782 | /// __attribute__((unused)) |
| 783 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 784 | Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc, |
| 785 | tok::TokenKind mType, |
| 786 | DeclPtrTy IDecl, |
| 787 | tok::ObjCKeywordKind MethodImplKind) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 788 | ParsingDeclRAIIObject PD(*this); |
| 789 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 790 | if (Tok.is(tok::code_completion)) { |
| 791 | Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus, |
| 792 | /*ReturnType=*/0, IDecl); |
| 793 | ConsumeToken(); |
| 794 | } |
| 795 | |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 796 | // Parse the return type if present. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 797 | TypeTy *ReturnType = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 798 | ObjCDeclSpec DSRet; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 799 | if (Tok.is(tok::l_paren)) |
Fariborz Jahanian | f1de0ca | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 800 | ReturnType = ParseObjCTypeName(DSRet); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Ted Kremenek | 9e04935 | 2010-02-18 23:05:16 +0000 | [diff] [blame] | 802 | // If attributes exist before the method, parse them. |
| 803 | llvm::OwningPtr<AttributeList> MethodAttrs; |
| 804 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
| 805 | MethodAttrs.reset(ParseGNUAttributes()); |
| 806 | |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 807 | if (Tok.is(tok::code_completion)) { |
| 808 | Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus, |
| 809 | ReturnType, IDecl); |
| 810 | ConsumeToken(); |
| 811 | } |
| 812 | |
Ted Kremenek | 9e04935 | 2010-02-18 23:05:16 +0000 | [diff] [blame] | 813 | // Now parse the selector. |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 814 | SourceLocation selLoc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 815 | IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc); |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 816 | |
Steve Naroff | 84c4310 | 2009-02-11 20:43:13 +0000 | [diff] [blame] | 817 | // An unnamed colon is valid. |
| 818 | if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 819 | Diag(Tok, diag::err_expected_selector_for_method) |
| 820 | << SourceRange(mLoc, Tok.getLocation()); |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 821 | // Skip until we get a ; or {}. |
| 822 | SkipUntil(tok::r_brace); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 823 | return DeclPtrTy(); |
Chris Lattner | e8904e9 | 2008-08-23 01:48:03 +0000 | [diff] [blame] | 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 826 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 827 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 828 | // If attributes exist after the method, parse them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Ted Kremenek | 9e04935 | 2010-02-18 23:05:16 +0000 | [diff] [blame] | 830 | MethodAttrs.reset(addAttributeLists(MethodAttrs.take(), |
| 831 | ParseGNUAttributes())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 833 | Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 834 | DeclPtrTy Result |
| 835 | = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 836 | mType, IDecl, DSRet, ReturnType, Sel, |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 837 | 0, |
| 838 | CParamInfo.data(), CParamInfo.size(), |
| 839 | MethodAttrs.get(), |
Ted Kremenek | 1c6a3cc | 2009-05-04 17:04:30 +0000 | [diff] [blame] | 840 | MethodImplKind); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 841 | PD.complete(Result); |
| 842 | return Result; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 843 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 844 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 845 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 846 | llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 848 | while (1) { |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 849 | Action::ObjCArgInfo ArgInfo; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 851 | // Each iteration parses a single keyword argument. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 852 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 853 | Diag(Tok, diag::err_expected_colon); |
| 854 | break; |
| 855 | } |
| 856 | ConsumeToken(); // Eat the ':'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 858 | ArgInfo.Type = 0; |
| 859 | if (Tok.is(tok::l_paren)) // Parse the argument type if present. |
| 860 | ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec); |
| 861 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 862 | // If attributes exist before the argument name, parse them. |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 863 | ArgInfo.ArgAttrs = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 864 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 865 | ArgInfo.ArgAttrs = ParseGNUAttributes(); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 866 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 867 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 868 | Diag(Tok, diag::err_expected_ident); // missing argument name. |
| 869 | break; |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 870 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 872 | ArgInfo.Name = Tok.getIdentifierInfo(); |
| 873 | ArgInfo.NameLoc = Tok.getLocation(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 874 | ConsumeToken(); // Eat the identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 876 | ArgInfos.push_back(ArgInfo); |
| 877 | KeyIdents.push_back(SelIdent); |
| 878 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 879 | // Check for another keyword selector. |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 880 | SourceLocation Loc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 881 | SelIdent = ParseObjCSelectorPiece(Loc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 882 | if (!SelIdent && Tok.isNot(tok::colon)) |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 883 | break; |
| 884 | // We have a selector or a colon, continue parsing. |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 885 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 887 | bool isVariadic = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 889 | // Parse the (optional) parameter list. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 890 | while (Tok.is(tok::comma)) { |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 891 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 892 | if (Tok.is(tok::ellipsis)) { |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 893 | isVariadic = true; |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 894 | ConsumeToken(); |
| 895 | break; |
| 896 | } |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 897 | DeclSpec DS; |
| 898 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | // Parse the declarator. |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 900 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 901 | ParseDeclarator(ParmDecl); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 902 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 903 | DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
| 904 | CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 905 | ParmDecl.getIdentifierLoc(), |
| 906 | Param, |
| 907 | 0)); |
| 908 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 909 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 911 | // FIXME: Add support for optional parmameter list... |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 912 | // If attributes exist after the method, parse them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Ted Kremenek | 9e04935 | 2010-02-18 23:05:16 +0000 | [diff] [blame] | 914 | MethodAttrs.reset(addAttributeLists(MethodAttrs.take(), |
| 915 | ParseGNUAttributes())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Fariborz Jahanian | 3688fc6 | 2009-06-24 17:00:18 +0000 | [diff] [blame] | 917 | if (KeyIdents.size() == 0) |
| 918 | return DeclPtrTy(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 919 | Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), |
| 920 | &KeyIdents[0]); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 921 | DeclPtrTy Result |
| 922 | = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), |
Fariborz Jahanian | 3688fc6 | 2009-06-24 17:00:18 +0000 | [diff] [blame] | 923 | mType, IDecl, DSRet, ReturnType, Sel, |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 924 | &ArgInfos[0], |
| 925 | CParamInfo.data(), CParamInfo.size(), |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 926 | MethodAttrs.get(), |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 927 | MethodImplKind, isVariadic); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 928 | PD.complete(Result); |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 929 | |
| 930 | // Delete referenced AttributeList objects. |
| 931 | for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator |
| 932 | I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I) |
| 933 | delete I->ArgAttrs; |
| 934 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 935 | return Result; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 938 | /// objc-protocol-refs: |
| 939 | /// '<' identifier-list '>' |
| 940 | /// |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 941 | bool Parser:: |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 942 | ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols, |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 943 | llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs, |
| 944 | bool WarnOnDeclarations, |
| 945 | SourceLocation &LAngleLoc, SourceLocation &EndLoc) { |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 946 | assert(Tok.is(tok::less) && "expected <"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 948 | LAngleLoc = ConsumeToken(); // the "<" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 950 | llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 952 | while (1) { |
Douglas Gregor | 55385fe | 2009-11-18 04:19:12 +0000 | [diff] [blame] | 953 | if (Tok.is(tok::code_completion)) { |
| 954 | Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(), |
| 955 | ProtocolIdents.size()); |
| 956 | ConsumeToken(); |
| 957 | } |
| 958 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 959 | if (Tok.isNot(tok::identifier)) { |
| 960 | Diag(Tok, diag::err_expected_ident); |
| 961 | SkipUntil(tok::greater); |
| 962 | return true; |
| 963 | } |
| 964 | ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(), |
| 965 | Tok.getLocation())); |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 966 | ProtocolLocs.push_back(Tok.getLocation()); |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 967 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 969 | if (Tok.isNot(tok::comma)) |
| 970 | break; |
| 971 | ConsumeToken(); |
| 972 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 974 | // Consume the '>'. |
| 975 | if (Tok.isNot(tok::greater)) { |
| 976 | Diag(Tok, diag::err_expected_greater); |
| 977 | return true; |
| 978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 980 | EndLoc = ConsumeAnyToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 982 | // Convert the list of protocols identifiers into a list of protocol decls. |
| 983 | Actions.FindProtocolDeclaration(WarnOnDeclarations, |
| 984 | &ProtocolIdents[0], ProtocolIdents.size(), |
| 985 | Protocols); |
| 986 | return false; |
| 987 | } |
| 988 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 989 | /// objc-class-instance-variables: |
| 990 | /// '{' objc-instance-variable-decl-list[opt] '}' |
| 991 | /// |
| 992 | /// objc-instance-variable-decl-list: |
| 993 | /// objc-visibility-spec |
| 994 | /// objc-instance-variable-decl ';' |
| 995 | /// ';' |
| 996 | /// objc-instance-variable-decl-list objc-visibility-spec |
| 997 | /// objc-instance-variable-decl-list objc-instance-variable-decl ';' |
| 998 | /// objc-instance-variable-decl-list ';' |
| 999 | /// |
| 1000 | /// objc-visibility-spec: |
| 1001 | /// @private |
| 1002 | /// @protected |
| 1003 | /// @public |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1004 | /// @package [OBJC2] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1005 | /// |
| 1006 | /// objc-instance-variable-decl: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | /// struct-declaration |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1008 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1009 | void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl, |
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 1010 | tok::ObjCKeywordKind visibility, |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1011 | SourceLocation atLoc) { |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1012 | assert(Tok.is(tok::l_brace) && "expected {"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1013 | llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1014 | |
Douglas Gregor | 1a0d31a | 2009-01-12 18:45:55 +0000 | [diff] [blame] | 1015 | ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1016 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1017 | SourceLocation LBraceLoc = ConsumeBrace(); // the "{" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1019 | // While we still have something to read, read the instance variables. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1020 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1021 | // Each iteration of this loop reads one objc-instance-variable-decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1023 | // Check for extraneous top-level semicolon. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1024 | if (Tok.is(tok::semi)) { |
Chris Lattner | c2253f5 | 2009-11-06 06:40:12 +0000 | [diff] [blame] | 1025 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1026 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1027 | ConsumeToken(); |
| 1028 | continue; |
| 1029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1031 | // Set the default visibility to private. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1032 | if (Tok.is(tok::at)) { // parse objc-visibility-spec |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1033 | ConsumeToken(); // eat the @ sign |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (Tok.is(tok::code_completion)) { |
| 1036 | Actions.CodeCompleteObjCAtVisibility(CurScope); |
| 1037 | ConsumeToken(); |
| 1038 | } |
| 1039 | |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1040 | switch (Tok.getObjCKeywordID()) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1041 | case tok::objc_private: |
| 1042 | case tok::objc_public: |
| 1043 | case tok::objc_protected: |
| 1044 | case tok::objc_package: |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1045 | visibility = Tok.getObjCKeywordID(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1046 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | continue; |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1048 | default: |
| 1049 | Diag(Tok, diag::err_objc_illegal_visibility_spec); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1050 | continue; |
| 1051 | } |
| 1052 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
Douglas Gregor | c38c3e1 | 2010-01-13 21:54:15 +0000 | [diff] [blame] | 1054 | if (Tok.is(tok::code_completion)) { |
| 1055 | Actions.CodeCompleteOrdinaryName(CurScope, |
| 1056 | Action::CCC_ObjCInstanceVariableList); |
| 1057 | ConsumeToken(); |
| 1058 | } |
| 1059 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1060 | struct ObjCIvarCallback : FieldCallback { |
| 1061 | Parser &P; |
| 1062 | DeclPtrTy IDecl; |
| 1063 | tok::ObjCKeywordKind visibility; |
| 1064 | llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls; |
| 1065 | |
| 1066 | ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V, |
| 1067 | llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) : |
| 1068 | P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) { |
| 1069 | } |
| 1070 | |
| 1071 | DeclPtrTy invoke(FieldDeclarator &FD) { |
| 1072 | // Install the declarator into the interface decl. |
| 1073 | DeclPtrTy Field |
| 1074 | = P.Actions.ActOnIvar(P.CurScope, |
| 1075 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 1076 | IDecl, FD.D, FD.BitfieldSize, visibility); |
Fariborz Jahanian | 0bd0459 | 2010-04-06 22:43:48 +0000 | [diff] [blame] | 1077 | if (Field) |
| 1078 | AllIvarDecls.push_back(Field); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1079 | return Field; |
| 1080 | } |
| 1081 | } Callback(*this, interfaceDecl, visibility, AllIvarDecls); |
| 1082 | |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1083 | // Parse all the comma separated declarators. |
| 1084 | DeclSpec DS; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1085 | ParseStructDeclaration(DS, Callback); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1087 | if (Tok.is(tok::semi)) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1088 | ConsumeToken(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 1091 | // Skip to end of block or statement |
| 1092 | SkipUntil(tok::r_brace, true, true); |
| 1093 | } |
| 1094 | } |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1095 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Steve Naroff | 8749be5 | 2007-10-31 22:11:35 +0000 | [diff] [blame] | 1096 | // Call ActOnFields() even if we don't have any decls. This is useful |
| 1097 | // for code rewriting tools that need to be aware of the empty list. |
| 1098 | Actions.ActOnFields(CurScope, atLoc, interfaceDecl, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1099 | AllIvarDecls.data(), AllIvarDecls.size(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1100 | LBraceLoc, RBraceLoc, 0); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 1101 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1102 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1103 | |
| 1104 | /// objc-protocol-declaration: |
| 1105 | /// objc-protocol-definition |
| 1106 | /// objc-protocol-forward-reference |
| 1107 | /// |
| 1108 | /// objc-protocol-definition: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | /// @protocol identifier |
| 1110 | /// objc-protocol-refs[opt] |
| 1111 | /// objc-interface-decl-list |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1112 | /// @end |
| 1113 | /// |
| 1114 | /// objc-protocol-forward-reference: |
| 1115 | /// @protocol identifier-list ';' |
| 1116 | /// |
| 1117 | /// "@protocol identifier ;" should be resolved as "@protocol |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1118 | /// identifier-list ;": objc-interface-decl-list may not start with a |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1119 | /// semicolon in the first alternative if objc-protocol-refs are omitted. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1120 | Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc, |
| 1121 | AttributeList *attrList) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1122 | assert(Tok.isObjCAtKeyword(tok::objc_protocol) && |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1123 | "ParseObjCAtProtocolDeclaration(): Expected @protocol"); |
| 1124 | ConsumeToken(); // the "protocol" identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | 083128f | 2009-11-18 04:49:41 +0000 | [diff] [blame] | 1126 | if (Tok.is(tok::code_completion)) { |
| 1127 | Actions.CodeCompleteObjCProtocolDecl(CurScope); |
| 1128 | ConsumeToken(); |
| 1129 | } |
| 1130 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1131 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1132 | Diag(Tok, diag::err_expected_ident); // missing protocol name. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1133 | return DeclPtrTy(); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1134 | } |
| 1135 | // Save the protocol name, then consume it. |
| 1136 | IdentifierInfo *protocolName = Tok.getIdentifierInfo(); |
| 1137 | SourceLocation nameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1139 | if (Tok.is(tok::semi)) { // forward declaration of one protocol. |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1140 | IdentifierLocPair ProtoInfo(protocolName, nameLoc); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1141 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1, |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1143 | attrList); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1146 | if (Tok.is(tok::comma)) { // list of forward declarations. |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1147 | llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs; |
| 1148 | ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc)); |
| 1149 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1150 | // Parse the list of forward declarations. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1151 | while (1) { |
| 1152 | ConsumeToken(); // the ',' |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1153 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1154 | Diag(Tok, diag::err_expected_ident); |
| 1155 | SkipUntil(tok::semi); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1156 | return DeclPtrTy(); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1157 | } |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1158 | ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(), |
| 1159 | Tok.getLocation())); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1160 | ConsumeToken(); // the identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1162 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | } |
| 1165 | // Consume the ';'. |
| 1166 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1167 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Steve Naroff | e440eb8 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 1169 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | &ProtocolRefs[0], |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1171 | ProtocolRefs.size(), |
| 1172 | attrList); |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1175 | // Last, and definitely not least, parse a protocol declaration. |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1176 | SourceLocation LAngleLoc, EndProtoLoc; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1178 | llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs; |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1179 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1180 | if (Tok.is(tok::less) && |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1181 | ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, |
| 1182 | LAngleLoc, EndProtoLoc)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1183 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1185 | DeclPtrTy ProtoType = |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1186 | Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1187 | ProtocolRefs.data(), |
| 1188 | ProtocolRefs.size(), |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1189 | ProtocolLocs.data(), |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 1190 | EndProtoLoc, attrList); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 1191 | ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol); |
Chris Lattner | bc662af | 2008-10-20 06:10:06 +0000 | [diff] [blame] | 1192 | return ProtoType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1193 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1194 | |
| 1195 | /// objc-implementation: |
| 1196 | /// objc-class-implementation-prologue |
| 1197 | /// objc-category-implementation-prologue |
| 1198 | /// |
| 1199 | /// objc-class-implementation-prologue: |
| 1200 | /// @implementation identifier objc-superclass[opt] |
| 1201 | /// objc-class-instance-variables[opt] |
| 1202 | /// |
| 1203 | /// objc-category-implementation-prologue: |
| 1204 | /// @implementation identifier ( identifier ) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1205 | Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration( |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1206 | SourceLocation atLoc) { |
| 1207 | assert(Tok.isObjCAtKeyword(tok::objc_implementation) && |
| 1208 | "ParseObjCAtImplementationDeclaration(): Expected @implementation"); |
| 1209 | ConsumeToken(); // the "implementation" identifier |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | |
Douglas Gregor | 3b49aca | 2009-11-18 16:26:39 +0000 | [diff] [blame] | 1211 | // Code completion after '@implementation'. |
| 1212 | if (Tok.is(tok::code_completion)) { |
| 1213 | Actions.CodeCompleteObjCImplementationDecl(CurScope); |
| 1214 | ConsumeToken(); |
| 1215 | } |
| 1216 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1217 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1218 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1219 | return DeclPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1220 | } |
| 1221 | // We have a class or category name - consume it. |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1222 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1223 | SourceLocation nameLoc = ConsumeToken(); // consume class or category name |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
| 1225 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1226 | // we have a category implementation. |
| 1227 | SourceLocation lparenLoc = ConsumeParen(); |
| 1228 | SourceLocation categoryLoc, rparenLoc; |
| 1229 | IdentifierInfo *categoryId = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | |
Douglas Gregor | 33ced0b | 2009-11-18 19:08:43 +0000 | [diff] [blame] | 1231 | if (Tok.is(tok::code_completion)) { |
| 1232 | Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId); |
| 1233 | ConsumeToken(); |
| 1234 | } |
| 1235 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1236 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1237 | categoryId = Tok.getIdentifierInfo(); |
| 1238 | categoryLoc = ConsumeToken(); |
| 1239 | } else { |
| 1240 | Diag(Tok, diag::err_expected_ident); // missing category name. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1241 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1243 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1244 | Diag(Tok, diag::err_expected_rparen); |
| 1245 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1246 | return DeclPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1247 | } |
| 1248 | rparenLoc = ConsumeParen(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1249 | DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | atLoc, nameId, nameLoc, categoryId, |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 1251 | categoryLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1252 | ObjCImpDecl = ImplCatType; |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1253 | PendingObjCImpDecl.push_back(ObjCImpDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1254 | return DeclPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1255 | } |
| 1256 | // We have a class implementation |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1257 | SourceLocation superClassLoc; |
| 1258 | IdentifierInfo *superClassId = 0; |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1259 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1260 | // We have a super class |
| 1261 | ConsumeToken(); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1262 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1263 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1264 | return DeclPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1265 | } |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1266 | superClassId = Tok.getIdentifierInfo(); |
| 1267 | superClassLoc = ConsumeToken(); // Consume super class name |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1268 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1269 | DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation( |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 1270 | atLoc, nameId, nameLoc, |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 1271 | superClassId, superClassLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1273 | if (Tok.is(tok::l_brace)) // we have ivars |
Fariborz Jahanian | 83c481a | 2010-02-22 23:04:20 +0000 | [diff] [blame] | 1274 | ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, |
Fariborz Jahanian | 01f1bfc | 2010-03-22 19:04:14 +0000 | [diff] [blame] | 1275 | tok::objc_private, atLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1276 | ObjCImpDecl = ImplClsType; |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1277 | PendingObjCImpDecl.push_back(ObjCImpDecl); |
| 1278 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1279 | return DeclPtrTy(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1280 | } |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1281 | |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1282 | Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1283 | assert(Tok.isObjCAtKeyword(tok::objc_end) && |
| 1284 | "ParseObjCAtEndDeclaration(): Expected @end"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1285 | DeclPtrTy Result = ObjCImpDecl; |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1286 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1287 | if (ObjCImpDecl) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1288 | Actions.ActOnAtEnd(atEnd, ObjCImpDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1289 | ObjCImpDecl = DeclPtrTy(); |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1290 | PendingObjCImpDecl.pop_back(); |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1291 | } |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1292 | else { |
| 1293 | // missing @implementation |
| 1294 | Diag(atEnd.getBegin(), diag::warn_expected_implementation); |
| 1295 | } |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1296 | return Result; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 1297 | } |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1298 | |
Fariborz Jahanian | 1ac7104 | 2009-11-17 17:15:16 +0000 | [diff] [blame] | 1299 | Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() { |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1300 | if (PendingObjCImpDecl.empty()) |
| 1301 | return Actions.ConvertDeclToDeclGroup(DeclPtrTy()); |
| 1302 | DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val(); |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1303 | Actions.ActOnAtEnd(SourceRange(), ImpDecl); |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1304 | return Actions.ConvertDeclToDeclGroup(ImpDecl); |
| 1305 | } |
| 1306 | |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1307 | /// compatibility-alias-decl: |
| 1308 | /// @compatibility_alias alias-name class-name ';' |
| 1309 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1310 | Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1311 | assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && |
| 1312 | "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); |
| 1313 | ConsumeToken(); // consume compatibility_alias |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1314 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1315 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1316 | return DeclPtrTy(); |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1317 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1318 | IdentifierInfo *aliasId = Tok.getIdentifierInfo(); |
| 1319 | SourceLocation aliasLoc = ConsumeToken(); // consume alias-name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1320 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1321 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1322 | return DeclPtrTy(); |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 1323 | } |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1324 | IdentifierInfo *classId = Tok.getIdentifierInfo(); |
| 1325 | SourceLocation classLoc = ConsumeToken(); // consume class-name; |
| 1326 | if (Tok.isNot(tok::semi)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1327 | Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias"; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1328 | return DeclPtrTy(); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 1329 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1330 | return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc, |
| 1331 | classId, classLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1334 | /// property-synthesis: |
| 1335 | /// @synthesize property-ivar-list ';' |
| 1336 | /// |
| 1337 | /// property-ivar-list: |
| 1338 | /// property-ivar |
| 1339 | /// property-ivar-list ',' property-ivar |
| 1340 | /// |
| 1341 | /// property-ivar: |
| 1342 | /// identifier |
| 1343 | /// identifier '=' identifier |
| 1344 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1345 | Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1346 | assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && |
| 1347 | "ParseObjCPropertyDynamic(): Expected '@synthesize'"); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1348 | SourceLocation loc = ConsumeToken(); // consume synthesize |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | b328c42 | 2009-11-18 19:45:45 +0000 | [diff] [blame] | 1350 | while (true) { |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1351 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 424b2a5 | 2009-11-18 22:56:13 +0000 | [diff] [blame] | 1352 | Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl); |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1353 | ConsumeToken(); |
| 1354 | } |
| 1355 | |
Douglas Gregor | b328c42 | 2009-11-18 19:45:45 +0000 | [diff] [blame] | 1356 | if (Tok.isNot(tok::identifier)) { |
| 1357 | Diag(Tok, diag::err_synthesized_property_name); |
| 1358 | SkipUntil(tok::semi); |
| 1359 | return DeclPtrTy(); |
| 1360 | } |
| 1361 | |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1362 | IdentifierInfo *propertyIvar = 0; |
| 1363 | IdentifierInfo *propertyId = Tok.getIdentifierInfo(); |
| 1364 | SourceLocation propertyLoc = ConsumeToken(); // consume property name |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1365 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1366 | // property '=' ivar-name |
| 1367 | ConsumeToken(); // consume '=' |
Douglas Gregor | 322328b | 2009-11-18 22:32:06 +0000 | [diff] [blame] | 1368 | |
| 1369 | if (Tok.is(tok::code_completion)) { |
| 1370 | Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId, |
| 1371 | ObjCImpDecl); |
| 1372 | ConsumeToken(); |
| 1373 | } |
| 1374 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1375 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1376 | Diag(Tok, diag::err_expected_ident); |
| 1377 | break; |
| 1378 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1379 | propertyIvar = Tok.getIdentifierInfo(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1380 | ConsumeToken(); // consume ivar-name |
| 1381 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1382 | Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl, |
| 1383 | propertyId, propertyIvar); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1384 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1385 | break; |
| 1386 | ConsumeToken(); // consume ',' |
| 1387 | } |
Douglas Gregor | b328c42 | 2009-11-18 19:45:45 +0000 | [diff] [blame] | 1388 | if (Tok.isNot(tok::semi)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1389 | Diag(Tok, diag::err_expected_semi_after) << "@synthesize"; |
Douglas Gregor | b328c42 | 2009-11-18 19:45:45 +0000 | [diff] [blame] | 1390 | SkipUntil(tok::semi); |
| 1391 | } |
Fariborz Jahanian | d3fdcb5 | 2009-11-06 21:48:47 +0000 | [diff] [blame] | 1392 | else |
| 1393 | ConsumeToken(); // consume ';' |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1394 | return DeclPtrTy(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1397 | /// property-dynamic: |
| 1398 | /// @dynamic property-list |
| 1399 | /// |
| 1400 | /// property-list: |
| 1401 | /// identifier |
| 1402 | /// property-list ',' identifier |
| 1403 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1404 | Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1405 | assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && |
| 1406 | "ParseObjCPropertyDynamic(): Expected '@dynamic'"); |
| 1407 | SourceLocation loc = ConsumeToken(); // consume dynamic |
Douglas Gregor | 424b2a5 | 2009-11-18 22:56:13 +0000 | [diff] [blame] | 1408 | while (true) { |
| 1409 | if (Tok.is(tok::code_completion)) { |
| 1410 | Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl); |
| 1411 | ConsumeToken(); |
| 1412 | } |
| 1413 | |
| 1414 | if (Tok.isNot(tok::identifier)) { |
| 1415 | Diag(Tok, diag::err_expected_ident); |
| 1416 | SkipUntil(tok::semi); |
| 1417 | return DeclPtrTy(); |
| 1418 | } |
| 1419 | |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1420 | IdentifierInfo *propertyId = Tok.getIdentifierInfo(); |
| 1421 | SourceLocation propertyLoc = ConsumeToken(); // consume property name |
| 1422 | Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl, |
| 1423 | propertyId, 0); |
| 1424 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1425 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1426 | break; |
| 1427 | ConsumeToken(); // consume ',' |
| 1428 | } |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1429 | if (Tok.isNot(tok::semi)) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1430 | Diag(Tok, diag::err_expected_semi_after) << "@dynamic"; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1431 | return DeclPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1434 | /// objc-throw-statement: |
| 1435 | /// throw expression[opt]; |
| 1436 | /// |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1437 | Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) { |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1438 | OwningExprResult Res(Actions); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1439 | ConsumeToken(); // consume throw |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1440 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1441 | Res = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1442 | if (Res.isInvalid()) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1443 | SkipUntil(tok::semi); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1444 | return StmtError(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1445 | } |
| 1446 | } |
Fariborz Jahanian | 39f8f15 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 1447 | ConsumeToken(); // consume ';' |
Steve Naroff | e21dd6f | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 1448 | return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1451 | /// objc-synchronized-statement: |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1452 | /// @synchronized '(' expression ')' compound-statement |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1453 | /// |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1454 | Parser::OwningStmtResult |
| 1455 | Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) { |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1456 | ConsumeToken(); // consume synchronized |
| 1457 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1458 | Diag(Tok, diag::err_expected_lparen_after) << "@synchronized"; |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1459 | return StmtError(); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1460 | } |
| 1461 | ConsumeParen(); // '(' |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1462 | OwningExprResult Res(ParseExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1463 | if (Res.isInvalid()) { |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1464 | SkipUntil(tok::semi); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1465 | return StmtError(); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1466 | } |
| 1467 | if (Tok.isNot(tok::r_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1468 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1469 | return StmtError(); |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1470 | } |
| 1471 | ConsumeParen(); // ')' |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1472 | if (Tok.isNot(tok::l_brace)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1473 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1474 | return StmtError(); |
Fariborz Jahanian | 78a677b | 2008-01-30 17:38:29 +0000 | [diff] [blame] | 1475 | } |
Steve Naroff | 3ac438c | 2008-06-04 20:36:13 +0000 | [diff] [blame] | 1476 | // Enter a scope to hold everything within the compound stmt. Compound |
| 1477 | // statements can always hold declarations. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1478 | ParseScope BodyScope(this, Scope::DeclScope); |
Steve Naroff | 3ac438c | 2008-06-04 20:36:13 +0000 | [diff] [blame] | 1479 | |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1480 | OwningStmtResult SynchBody(ParseCompoundStatementBody()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1481 | |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1482 | BodyScope.Exit(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1483 | if (SynchBody.isInvalid()) |
Fariborz Jahanian | fa3ee8e | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 1484 | SynchBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1485 | return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody)); |
Fariborz Jahanian | c385c90 | 2008-01-29 18:21:32 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1488 | /// objc-try-catch-statement: |
| 1489 | /// @try compound-statement objc-catch-list[opt] |
| 1490 | /// @try compound-statement objc-catch-list[opt] @finally compound-statement |
| 1491 | /// |
| 1492 | /// objc-catch-list: |
| 1493 | /// @catch ( parameter-declaration ) compound-statement |
| 1494 | /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement |
| 1495 | /// catch-parameter-declaration: |
| 1496 | /// parameter-declaration |
| 1497 | /// '...' [OBJC2] |
| 1498 | /// |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1499 | Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1500 | bool catch_or_finally_seen = false; |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1501 | |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1502 | ConsumeToken(); // consume try |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1503 | if (Tok.isNot(tok::l_brace)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1504 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1505 | return StmtError(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1506 | } |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1507 | OwningStmtResult CatchStmts(Actions); |
| 1508 | OwningStmtResult FinallyStmt(Actions); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1509 | ParseScope TryScope(this, Scope::DeclScope); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1510 | OwningStmtResult TryBody(ParseCompoundStatementBody()); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1511 | TryScope.Exit(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1512 | if (TryBody.isInvalid()) |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1513 | TryBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1514 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1515 | while (Tok.is(tok::at)) { |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1516 | // At this point, we need to lookahead to determine if this @ is the start |
| 1517 | // of an @catch or @finally. We don't want to consume the @ token if this |
| 1518 | // is an @try or @encode or something else. |
| 1519 | Token AfterAt = GetLookAheadToken(1); |
| 1520 | if (!AfterAt.isObjCAtKeyword(tok::objc_catch) && |
| 1521 | !AfterAt.isObjCAtKeyword(tok::objc_finally)) |
| 1522 | break; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1523 | |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1524 | SourceLocation AtCatchFinallyLoc = ConsumeToken(); |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 1525 | if (Tok.isObjCAtKeyword(tok::objc_catch)) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1526 | DeclPtrTy FirstPart; |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1527 | ConsumeToken(); // consume catch |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1528 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1529 | ConsumeParen(); |
Steve Naroff | e21dd6f | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 1530 | ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1531 | if (Tok.isNot(tok::ellipsis)) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1532 | DeclSpec DS; |
| 1533 | ParseDeclarationSpecifiers(DS); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1534 | // For some odd reason, the name of the exception variable is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | // optional. As a result, we need to use "PrototypeContext", because |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1536 | // we must accept either 'declarator' or 'abstract-declarator' here. |
| 1537 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1538 | ParseDeclarator(ParmDecl); |
| 1539 | |
| 1540 | // Inform the actions module about the parameter declarator, so it |
| 1541 | // gets added to the current scope. |
Fariborz Jahanian | d219a3a | 2010-02-03 00:32:51 +0000 | [diff] [blame] | 1542 | // FIXME. Probably can build a VarDecl and avoid setting DeclContext. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1543 | FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
Fariborz Jahanian | 2f764f1 | 2010-02-03 00:01:43 +0000 | [diff] [blame] | 1544 | Actions.ActOnObjCCatchParam(FirstPart); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1545 | } else |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1546 | ConsumeToken(); // consume '...' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Steve Naroff | 93a2595 | 2009-04-07 22:56:58 +0000 | [diff] [blame] | 1548 | SourceLocation RParenLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
Steve Naroff | 93a2595 | 2009-04-07 22:56:58 +0000 | [diff] [blame] | 1550 | if (Tok.is(tok::r_paren)) |
| 1551 | RParenLoc = ConsumeParen(); |
| 1552 | else // Skip over garbage, until we get to ')'. Eat the ')'. |
| 1553 | SkipUntil(tok::r_paren, true, false); |
| 1554 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1555 | OwningStmtResult CatchBody(Actions, true); |
Chris Lattner | c1b3ba5 | 2008-02-14 19:27:54 +0000 | [diff] [blame] | 1556 | if (Tok.is(tok::l_brace)) |
| 1557 | CatchBody = ParseCompoundStatementBody(); |
| 1558 | else |
| 1559 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1560 | if (CatchBody.isInvalid()) |
Fariborz Jahanian | 3b1191d | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1561 | CatchBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1562 | CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1563 | RParenLoc, FirstPart, move(CatchBody), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1564 | move(CatchStmts)); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1565 | } else { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1566 | Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after) |
| 1567 | << "@catch clause"; |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1568 | return StmtError(); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1569 | } |
| 1570 | catch_or_finally_seen = true; |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1571 | } else { |
| 1572 | assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?"); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1573 | ConsumeToken(); // consume finally |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1574 | ParseScope FinallyScope(this, Scope::DeclScope); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1575 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1576 | OwningStmtResult FinallyBody(Actions, true); |
Chris Lattner | c1b3ba5 | 2008-02-14 19:27:54 +0000 | [diff] [blame] | 1577 | if (Tok.is(tok::l_brace)) |
| 1578 | FinallyBody = ParseCompoundStatementBody(); |
| 1579 | else |
| 1580 | Diag(Tok, diag::err_expected_lbrace); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1581 | if (FinallyBody.isInvalid()) |
Fariborz Jahanian | 161a9c5 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1582 | FinallyBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1583 | FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc, |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1584 | move(FinallyBody)); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1585 | catch_or_finally_seen = true; |
| 1586 | break; |
| 1587 | } |
| 1588 | } |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1589 | if (!catch_or_finally_seen) { |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1590 | Diag(atLoc, diag::err_missing_catch_finally); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1591 | return StmtError(); |
Fariborz Jahanian | bd49a64 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1592 | } |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1593 | return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts), |
| 1594 | move(FinallyStmt)); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1595 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1596 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1597 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1598 | /// |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1599 | Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() { |
| 1600 | DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 1602 | PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions, |
| 1603 | PP.getSourceManager(), |
| 1604 | "parsing Objective-C method"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1606 | // parse optional ';' |
Fariborz Jahanian | 209a8c2 | 2009-10-20 16:39:13 +0000 | [diff] [blame] | 1607 | if (Tok.is(tok::semi)) { |
Ted Kremenek | 496e45e | 2009-11-10 22:55:49 +0000 | [diff] [blame] | 1608 | if (ObjCImpDecl) { |
| 1609 | Diag(Tok, diag::warn_semicolon_before_method_body) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1610 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Ted Kremenek | 496e45e | 2009-11-10 22:55:49 +0000 | [diff] [blame] | 1611 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1612 | ConsumeToken(); |
Fariborz Jahanian | 209a8c2 | 2009-10-20 16:39:13 +0000 | [diff] [blame] | 1613 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1614 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1615 | // We should have an opening brace now. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1616 | if (Tok.isNot(tok::l_brace)) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1617 | Diag(Tok, diag::err_expected_method_body); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1619 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 1620 | SkipUntil(tok::l_brace, true, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1622 | // If we didn't find the '{', bail out. |
| 1623 | if (Tok.isNot(tok::l_brace)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1624 | return DeclPtrTy(); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1625 | } |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1626 | SourceLocation BraceLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1628 | // Enter a scope for the method body. |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 1629 | ParseScope BodyScope(this, |
| 1630 | Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1632 | // Tell the actions module that we have entered a method definition with the |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 1633 | // specified Declarator for the method. |
Steve Naroff | ebf6443 | 2009-02-28 16:59:13 +0000 | [diff] [blame] | 1634 | Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1635 | |
| 1636 | OwningStmtResult FnBody(ParseCompoundStatementBody()); |
| 1637 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1638 | // If the function body could not be parsed, make a bogus compoundstmt. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1639 | if (FnBody.isInvalid()) |
Sebastian Redl | a60528c | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 1640 | FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, |
| 1641 | MultiStmtArg(Actions), false); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 1642 | |
Steve Naroff | 32ce837 | 2009-03-02 22:00:56 +0000 | [diff] [blame] | 1643 | // TODO: Pass argument information. |
| 1644 | Actions.ActOnFinishFunctionBody(MDecl, move(FnBody)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Steve Naroff | 409be83 | 2007-11-11 19:54:21 +0000 | [diff] [blame] | 1646 | // Leave the function body scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1647 | BodyScope.Exit(); |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 1648 | |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 1649 | return MDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1650 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1651 | |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1652 | Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) { |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 1653 | if (Tok.is(tok::code_completion)) { |
| 1654 | Actions.CodeCompleteObjCAtStatement(CurScope); |
| 1655 | ConsumeToken(); |
| 1656 | return StmtError(); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | if (Tok.isObjCAtKeyword(tok::objc_try)) |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 1660 | return ParseObjCTryStmt(AtLoc); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 1661 | |
| 1662 | if (Tok.isObjCAtKeyword(tok::objc_throw)) |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1663 | return ParseObjCThrowStmt(AtLoc); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (Tok.isObjCAtKeyword(tok::objc_synchronized)) |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1666 | return ParseObjCSynchronizedStmt(AtLoc); |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 1667 | |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1668 | OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1669 | if (Res.isInvalid()) { |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1670 | // If the expression is invalid, skip ahead to the next semicolon. Not |
| 1671 | // doing this opens us up to the possibility of infinite loops if |
| 1672 | // ParseExpression does not consume any tokens. |
| 1673 | SkipUntil(tok::semi); |
Sebastian Redl | 43bc2a0 | 2008-12-11 20:12:42 +0000 | [diff] [blame] | 1674 | return StmtError(); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1675 | } |
Chris Lattner | 5d80316 | 2009-12-07 16:33:19 +0000 | [diff] [blame] | 1676 | |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1677 | // Otherwise, eat the semicolon. |
| 1678 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
Anders Carlsson | 5ee56e9 | 2009-12-16 02:09:40 +0000 | [diff] [blame] | 1679 | return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res)); |
Steve Naroff | 64515f3 | 2008-02-05 21:27:35 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1682 | Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1683 | switch (Tok.getKind()) { |
Douglas Gregor | 9a0c85e | 2009-12-07 09:51:25 +0000 | [diff] [blame] | 1684 | case tok::code_completion: |
| 1685 | Actions.CodeCompleteObjCAtExpression(CurScope); |
| 1686 | ConsumeToken(); |
| 1687 | return ExprError(); |
| 1688 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1689 | case tok::string_literal: // primary-expression: string-literal |
| 1690 | case tok::wide_string_literal: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1691 | return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc)); |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1692 | default: |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1693 | if (Tok.getIdentifierInfo() == 0) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1694 | return ExprError(Diag(AtLoc, diag::err_unexpected_at)); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1695 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1696 | switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { |
| 1697 | case tok::objc_encode: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1698 | return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc)); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1699 | case tok::objc_protocol: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1700 | return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc)); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1701 | case tok::objc_selector: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1702 | return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc)); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1703 | default: |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1704 | return ExprError(Diag(AtLoc, diag::err_unexpected_at)); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1705 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1706 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | /// objc-message-expr: |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1710 | /// '[' objc-receiver objc-message-args ']' |
| 1711 | /// |
| 1712 | /// objc-receiver: |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1713 | /// 'super' |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1714 | /// expression |
| 1715 | /// class-name |
| 1716 | /// type-name |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1717 | Parser::OwningExprResult Parser::ParseObjCMessageExpression() { |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1718 | assert(Tok.is(tok::l_square) && "'[' expected"); |
| 1719 | SourceLocation LBracLoc = ConsumeBracket(); // consume '[' |
| 1720 | |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1721 | if (Tok.is(tok::identifier)) { |
| 1722 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1723 | |
| 1724 | // If this is '[' 'super', then this is a magic superclass message. |
| 1725 | // We parse '[' 'super' '.' 'foo' as an expression? |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 1726 | if ((II == Ident_super && GetLookAheadToken(1).isNot(tok::period) && |
| 1727 | CurScope->isInObjcMethodScope()) || |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1728 | // Check to see if this is a typename. If so, it is a class message. |
| 1729 | Actions.getTypeName(*II, Tok.getLocation(), CurScope)) { |
Fariborz Jahanian | d286992 | 2009-04-08 19:50:10 +0000 | [diff] [blame] | 1730 | SourceLocation NameLoc = ConsumeToken(); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1731 | return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, II, |
Fariborz Jahanian | d286992 | 2009-04-08 19:50:10 +0000 | [diff] [blame] | 1732 | ExprArg(Actions)); |
| 1733 | } |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1734 | } |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1735 | |
| 1736 | // Otherwise, an arbitrary expression can be the receiver of a send. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1737 | OwningExprResult Res(ParseExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1738 | if (Res.isInvalid()) { |
Chris Lattner | 5c74942 | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 1739 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1740 | return move(Res); |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1741 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1742 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1743 | return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1744 | 0, move(Res)); |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1745 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1746 | |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1747 | /// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse |
| 1748 | /// the rest of a message expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | /// |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1750 | /// objc-message-args: |
| 1751 | /// objc-selector |
| 1752 | /// objc-keywordarg-list |
| 1753 | /// |
| 1754 | /// objc-keywordarg-list: |
| 1755 | /// objc-keywordarg |
| 1756 | /// objc-keywordarg-list objc-keywordarg |
| 1757 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | /// objc-keywordarg: |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1759 | /// selector-name[opt] ':' objc-keywordexpr |
| 1760 | /// |
| 1761 | /// objc-keywordexpr: |
| 1762 | /// nonempty-expr-list |
| 1763 | /// |
| 1764 | /// nonempty-expr-list: |
| 1765 | /// assignment-expression |
| 1766 | /// nonempty-expr-list , assignment-expression |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1767 | /// |
| 1768 | Parser::OwningExprResult |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1769 | Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc, |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 1770 | SourceLocation NameLoc, |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1771 | IdentifierInfo *ReceiverName, |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1772 | ExprArg ReceiverExpr) { |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1773 | if (Tok.is(tok::code_completion)) { |
| 1774 | if (ReceiverName) |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1775 | Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc, |
| 1776 | 0, 0); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1777 | else |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1778 | Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(), |
| 1779 | 0, 0); |
Steve Naroff | c4df6d2 | 2009-11-07 02:08:14 +0000 | [diff] [blame] | 1780 | ConsumeToken(); |
| 1781 | } |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1782 | |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1783 | // Parse objc-selector |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1784 | SourceLocation Loc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 1785 | IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc); |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1786 | |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 1787 | SourceLocation SelectorLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1789 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1790 | ExprVector KeyExprs(Actions); |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1792 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1793 | while (1) { |
| 1794 | // Each iteration parses a single keyword argument. |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1795 | KeyIdents.push_back(selIdent); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1796 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1797 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1798 | Diag(Tok, diag::err_expected_colon); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1799 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1800 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1801 | // the enclosing expression. |
| 1802 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1803 | return ExprError(); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1804 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1805 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1806 | ConsumeToken(); // Eat the ':'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | /// Parse the expression after ':' |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1808 | OwningExprResult Res(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1809 | if (Res.isInvalid()) { |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1810 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1811 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1812 | // the enclosing expression. |
| 1813 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1814 | return move(Res); |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1815 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1816 | |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1817 | // We have a valid expression. |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1818 | KeyExprs.push_back(Res.release()); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1819 | |
Douglas Gregor | d3c6854 | 2009-11-19 01:08:35 +0000 | [diff] [blame] | 1820 | // Code completion after each argument. |
| 1821 | if (Tok.is(tok::code_completion)) { |
| 1822 | if (ReceiverName) |
| 1823 | Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc, |
| 1824 | KeyIdents.data(), |
| 1825 | KeyIdents.size()); |
| 1826 | else |
| 1827 | Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(), |
| 1828 | KeyIdents.data(), |
| 1829 | KeyIdents.size()); |
| 1830 | ConsumeToken(); |
| 1831 | } |
| 1832 | |
Steve Naroff | 37387c9 | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1833 | // Check for another keyword selector. |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 1834 | selIdent = ParseObjCSelectorPiece(Loc); |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1835 | if (!selIdent && Tok.isNot(tok::colon)) |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1836 | break; |
| 1837 | // We have a selector or a colon, continue parsing. |
| 1838 | } |
| 1839 | // Parse the, optional, argument list, comma separated. |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1840 | while (Tok.is(tok::comma)) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1841 | ConsumeToken(); // Eat the ','. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | /// Parse the expression after ',' |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1843 | OwningExprResult Res(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1844 | if (Res.isInvalid()) { |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1845 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1846 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1847 | // the enclosing expression. |
| 1848 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1849 | return move(Res); |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1850 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1851 | |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1852 | // We have a valid expression. |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1853 | KeyExprs.push_back(Res.release()); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } else if (!selIdent) { |
| 1856 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1858 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1859 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1860 | // the enclosing expression. |
| 1861 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1862 | return ExprError(); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1863 | } |
Fariborz Jahanian | 809872e | 2010-03-31 20:22:35 +0000 | [diff] [blame] | 1864 | |
Chris Lattner | df19526 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1865 | if (Tok.isNot(tok::r_square)) { |
Fariborz Jahanian | 809872e | 2010-03-31 20:22:35 +0000 | [diff] [blame] | 1866 | if (Tok.is(tok::identifier)) |
| 1867 | Diag(Tok, diag::err_expected_colon); |
| 1868 | else |
| 1869 | Diag(Tok, diag::err_expected_rsquare); |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1870 | // We must manually skip to a ']', otherwise the expression skipper will |
| 1871 | // stop at the ']' when it skips to the ';'. We want it to skip beyond |
| 1872 | // the enclosing expression. |
| 1873 | SkipUntil(tok::r_square); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1874 | return ExprError(); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1875 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1876 | |
Chris Lattner | 699b661 | 2008-01-25 18:59:06 +0000 | [diff] [blame] | 1877 | SourceLocation RBracLoc = ConsumeBracket(); // consume ']' |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1878 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 1879 | unsigned nKeys = KeyIdents.size(); |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1880 | if (nKeys == 0) |
| 1881 | KeyIdents.push_back(selIdent); |
| 1882 | Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1884 | // We've just parsed a keyword message. |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1885 | if (ReceiverName) |
| 1886 | return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | LBracLoc, NameLoc, SelectorLoc, |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 1888 | RBracLoc, |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1889 | KeyExprs.take(), KeyExprs.size())); |
| 1890 | return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel, |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 1891 | LBracLoc, SelectorLoc, RBracLoc, |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1892 | KeyExprs.take(), KeyExprs.size())); |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1895 | Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) { |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1896 | OwningExprResult Res(ParseStringLiteralExpression()); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1897 | if (Res.isInvalid()) return move(Res); |
| 1898 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1899 | // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string |
| 1900 | // expressions. At this point, we know that the only valid thing that starts |
| 1901 | // with '@' is an @"". |
| 1902 | llvm::SmallVector<SourceLocation, 4> AtLocs; |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1903 | ExprVector AtStrings(Actions); |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1904 | AtLocs.push_back(AtLoc); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1905 | AtStrings.push_back(Res.release()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1906 | |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1907 | while (Tok.is(tok::at)) { |
| 1908 | AtLocs.push_back(ConsumeToken()); // eat the @. |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1909 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1910 | // Invalid unless there is a string literal. |
Chris Lattner | 97cf6eb | 2009-02-18 05:56:09 +0000 | [diff] [blame] | 1911 | if (!isTokenStringLiteral()) |
| 1912 | return ExprError(Diag(Tok, diag::err_objc_concat_string)); |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | 97cf6eb | 2009-02-18 05:56:09 +0000 | [diff] [blame] | 1914 | OwningExprResult Lit(ParseStringLiteralExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1915 | if (Lit.isInvalid()) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1916 | return move(Lit); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1917 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1918 | AtStrings.push_back(Lit.release()); |
Chris Lattner | b3a99cd | 2007-12-12 01:04:12 +0000 | [diff] [blame] | 1919 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1920 | |
| 1921 | return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(), |
| 1922 | AtStrings.size())); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1923 | } |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1924 | |
| 1925 | /// objc-encode-expression: |
| 1926 | /// @encode ( type-name ) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1927 | Parser::OwningExprResult |
| 1928 | Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1929 | assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1930 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1931 | SourceLocation EncLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1932 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1933 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1934 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode"); |
| 1935 | |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1936 | SourceLocation LParenLoc = ConsumeParen(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1937 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1938 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1939 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1940 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1942 | if (Ty.isInvalid()) |
| 1943 | return ExprError(); |
| 1944 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1946 | Ty.get(), RParenLoc)); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1947 | } |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1948 | |
| 1949 | /// objc-protocol-expression |
| 1950 | /// @protocol ( protocol-name ) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1951 | Parser::OwningExprResult |
| 1952 | Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) { |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1953 | SourceLocation ProtoLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1954 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1955 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1956 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol"); |
| 1957 | |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1958 | SourceLocation LParenLoc = ConsumeParen(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1960 | if (Tok.isNot(tok::identifier)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1961 | return ExprError(Diag(Tok, diag::err_expected_ident)); |
| 1962 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1963 | IdentifierInfo *protocolId = Tok.getIdentifierInfo(); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1964 | ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1965 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1966 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1967 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1968 | return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc, |
| 1969 | LParenLoc, RParenLoc)); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1970 | } |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1971 | |
| 1972 | /// objc-selector-expression |
| 1973 | /// @selector '(' objc-keyword-selector ')' |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1974 | Parser::OwningExprResult |
| 1975 | Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) { |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1976 | SourceLocation SelectorLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1977 | |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1978 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1979 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector"); |
| 1980 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1981 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1982 | SourceLocation LParenLoc = ConsumeParen(); |
| 1983 | SourceLocation sLoc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 1984 | IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1985 | if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name. |
| 1986 | return ExprError(Diag(Tok, diag::err_expected_ident)); |
| 1987 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1988 | KeyIdents.push_back(SelIdent); |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 1989 | unsigned nColons = 0; |
| 1990 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1991 | while (1) { |
Chris Lattner | 4fef81d | 2008-08-05 06:19:09 +0000 | [diff] [blame] | 1992 | if (Tok.isNot(tok::colon)) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1993 | return ExprError(Diag(Tok, diag::err_expected_colon)); |
| 1994 | |
Chris Lattner | cb53b36 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 1995 | nColons++; |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1996 | ConsumeToken(); // Eat the ':'. |
| 1997 | if (Tok.is(tok::r_paren)) |
| 1998 | break; |
| 1999 | // Check for another keyword selector. |
| 2000 | SourceLocation Loc; |
Chris Lattner | 2fc5c24 | 2009-04-11 18:13:45 +0000 | [diff] [blame] | 2001 | SelIdent = ParseObjCSelectorPiece(Loc); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2002 | KeyIdents.push_back(SelIdent); |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2003 | if (!SelIdent && Tok.isNot(tok::colon)) |
| 2004 | break; |
| 2005 | } |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 2006 | } |
Fariborz Jahanian | a0818e3 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 2007 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 887407e | 2007-12-05 22:21:29 +0000 | [diff] [blame] | 2008 | Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2009 | return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, |
| 2010 | LParenLoc, RParenLoc)); |
Gabor Greif | 58065b2 | 2007-10-19 15:38:32 +0000 | [diff] [blame] | 2011 | } |