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