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