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