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