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