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