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