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