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