blob: 10c74ffdfdbed1d84e102d0cc3d4039cfa00bd19 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
23
Chris Lattner891dca62008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000032Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregorc464ae82009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000037 cutOffParsing();
38 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000039 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000040
41 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000042 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000043 case tok::objc_class:
44 return ParseObjCAtClassDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000045 break;
John McCall7f040a92010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall7f040a92010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000053 SingleDecl = ParseObjCAtProtocolDeclaration(AtLoc, attrs);
54 break;
John McCall7f040a92010-12-24 02:08:15 +000055 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000056 case tok::objc_implementation:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000057 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
58 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000059 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000060 return ParseObjCAtEndDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000062 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000063 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
64 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000065 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
67 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000068 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000069 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
70 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000071 default:
72 Diag(AtLoc, diag::err_unexpected_at);
73 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000074 SingleDecl = 0;
75 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000077 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000078}
79
80///
Mike Stump1eb44332009-09-09 15:08:12 +000081/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000083///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000084Parser::DeclGroupPtrTy
85Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000086 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000087 SmallVector<IdentifierInfo *, 8> ClassNames;
88 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000089
Mike Stump1eb44332009-09-09 15:08:12 +000090
Reid Spencer5f016e22007-07-11 17:01:13 +000091 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000092 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000093 Diag(Tok, diag::err_expected_ident);
94 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000095 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
Reid Spencer5f016e22007-07-11 17:01:13 +000097 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000098 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattnerdf195262007-10-09 17:51:17 +0000101 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 ConsumeToken();
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // Consume the ';'.
108 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000109 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenekc09cba62009-11-17 23:12:20 +0000111 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
112 ClassLocs.data(),
113 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000114}
115
Steve Naroffdac269b2007-08-20 21:31:48 +0000116///
117/// objc-interface:
118/// objc-class-interface-attributes[opt] objc-class-interface
119/// objc-category-interface
120///
121/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000122/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000123/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000124/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000125/// objc-interface-decl-list
126/// @end
127///
128/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000129/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000130/// objc-protocol-refs[opt]
131/// objc-interface-decl-list
132/// @end
133///
134/// objc-superclass:
135/// ':' identifier
136///
137/// objc-class-interface-attributes:
138/// __attribute__((visibility("default")))
139/// __attribute__((visibility("hidden")))
140/// __attribute__((deprecated))
141/// __attribute__((unavailable))
142/// __attribute__((objc_exception)) - used by NSException on 64-bit
143///
John McCall7f040a92010-12-24 02:08:15 +0000144Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
145 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000146 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
148 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000150 // Code completion after '@interface'.
151 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000152 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000153 cutOffParsing();
154 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000155 }
156
Chris Lattnerdf195262007-10-09 17:51:17 +0000157 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000160 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000161
Steve Naroffdac269b2007-08-20 21:31:48 +0000162 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000163 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000165 if (Tok.is(tok::l_paren) &&
166 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000167
168 BalancedDelimiterTracker T(*this, tok::l_paren);
169 T.consumeOpen();
170
171 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000172 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000173 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000174 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000175 cutOffParsing();
176 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000177 }
178
Steve Naroff527fe232007-08-23 19:56:30 +0000179 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000180 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000181 categoryId = Tok.getIdentifierInfo();
182 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000183 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000184 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000185 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000186 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000187 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000188
189 T.consumeClose();
190 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000191 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000192
193 if (!attrs.empty()) { // categories don't support attributes.
194 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
195 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000196 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000197
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000198 // Next, we need to check for any protocol references.
199 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200 SmallVector<Decl *, 8> ProtocolRefs;
201 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000202 if (Tok.is(tok::less) &&
203 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000204 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000205 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
John McCalld226f652010-08-21 09:40:31 +0000207 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000208 Actions.ActOnStartCategoryInterface(atLoc,
209 nameId, nameLoc,
210 categoryId, categoryLoc,
211 ProtocolRefs.data(),
212 ProtocolRefs.size(),
213 ProtocolLocs.data(),
214 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000215
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000216 if (Tok.is(tok::l_brace))
217 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
218
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000219 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000220 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000221 }
222 // Parse a class interface.
223 IdentifierInfo *superClassId = 0;
224 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000225
Chris Lattnerdf195262007-10-09 17:51:17 +0000226 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000227 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000228
229 // Code completion of superclass names.
230 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000231 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000232 cutOffParsing();
233 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000234 }
235
Chris Lattnerdf195262007-10-09 17:51:17 +0000236 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000237 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000238 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000239 }
240 superClassId = Tok.getIdentifierInfo();
241 superClassLoc = ConsumeToken();
242 }
243 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000244 SmallVector<Decl *, 8> ProtocolRefs;
245 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000246 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000247 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000248 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
249 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000250 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
John McCalld226f652010-08-21 09:40:31 +0000252 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000253 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000254 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000255 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000256 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000257 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattnerdf195262007-10-09 17:51:17 +0000259 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000260 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000261
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000262 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000263 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000264}
265
John McCalld0014542009-12-03 22:31:13 +0000266/// The Objective-C property callback. This should be defined where
267/// it's used, but instead it's been lifted to here to support VS2005.
268struct Parser::ObjCPropertyCallback : FieldCallback {
269 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000270 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000271 ObjCDeclSpec &OCDS;
272 SourceLocation AtLoc;
273 tok::ObjCKeywordKind MethodImplKind;
274
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000275 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000276 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000277 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
278 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000279 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000280 MethodImplKind(MethodImplKind) {
281 }
282
John McCalld226f652010-08-21 09:40:31 +0000283 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000284 if (FD.D.getIdentifier() == 0) {
285 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
286 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000287 return 0;
John McCalld0014542009-12-03 22:31:13 +0000288 }
289 if (FD.BitfieldSize) {
290 P.Diag(AtLoc, diag::err_objc_property_bitfield)
291 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000292 return 0;
John McCalld0014542009-12-03 22:31:13 +0000293 }
294
295 // Install the property declarator into interfaceDecl.
296 IdentifierInfo *SelName =
297 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
298
299 Selector GetterSel =
300 P.PP.getSelectorTable().getNullarySelector(SelName);
301 IdentifierInfo *SetterName = OCDS.getSetterName();
302 Selector SetterSel;
303 if (SetterName)
304 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
305 else
306 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
307 P.PP.getSelectorTable(),
308 FD.D.getIdentifier());
309 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000310 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000311 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000312 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000313 &isOverridingProperty,
314 MethodImplKind);
315 if (!isOverridingProperty)
316 Props.push_back(Property);
317
318 return Property;
319 }
320};
321
Steve Naroffdac269b2007-08-20 21:31:48 +0000322/// objc-interface-decl-list:
323/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000324/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000325/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000326/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000327/// objc-interface-decl-list declaration
328/// objc-interface-decl-list ';'
329///
Steve Naroff294494e2007-08-22 16:35:03 +0000330/// objc-method-requirement: [OBJC2]
331/// @required
332/// @optional
333///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000334void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
335 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000336 SmallVector<Decl *, 32> allMethods;
337 SmallVector<Decl *, 16> allProperties;
338 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000339 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek782f2f52010-01-07 01:20:12 +0000341 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000342
Steve Naroff294494e2007-08-22 16:35:03 +0000343 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000344 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000345 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000346 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000347 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000348 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000349 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
350 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000351 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
352 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000353 continue;
354 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000355 if (Tok.is(tok::l_paren)) {
356 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000357 ParseObjCMethodDecl(Tok.getLocation(),
358 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000359 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000360 continue;
361 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000362 // Ignore excess semicolons.
363 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000364 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000365 continue;
366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattnerbc662af2008-10-20 06:10:06 +0000368 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000369 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000370 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000372 // Code completion within an Objective-C interface.
373 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000374 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000375 ObjCImpDecl? Sema::PCC_ObjCImplementation
376 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000377 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000378 }
379
Chris Lattnere82a10f2008-10-20 05:46:22 +0000380 // If we don't have an @ directive, parse it as a function definition.
381 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000382 // The code below does not consume '}'s because it is afraid of eating the
383 // end of a namespace. Because of the way this code is structured, an
384 // erroneous r_brace would cause an infinite loop if not handled here.
385 if (Tok.is(tok::r_brace))
386 break;
John McCall0b7e6782011-03-24 11:26:52 +0000387 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000388 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000389 continue;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnere82a10f2008-10-20 05:46:22 +0000392 // Otherwise, we have an @ directive, eat the @.
393 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000394 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000395 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000396 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000397 break;
398 }
399
Chris Lattnera2449b22008-10-20 05:57:40 +0000400 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattnera2449b22008-10-20 05:57:40 +0000402 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000403 AtEnd.setBegin(AtLoc);
404 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000405 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000406 } else if (DirectiveKind == tok::objc_not_keyword) {
407 Diag(Tok, diag::err_objc_unknown_at);
408 SkipUntil(tok::semi);
409 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattnerbc662af2008-10-20 06:10:06 +0000412 // Eat the identifier.
413 ConsumeToken();
414
Chris Lattnera2449b22008-10-20 05:57:40 +0000415 switch (DirectiveKind) {
416 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000417 // FIXME: If someone forgets an @end on a protocol, this loop will
418 // continue to eat up tons of stuff and spew lots of nonsense errors. It
419 // would probably be better to bail out if we saw an @class or @interface
420 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000421 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000422 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000423 SkipUntil(tok::r_brace, tok::at);
424 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000425
426 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000427 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000428 Diag(Tok, diag::err_objc_missing_end);
429 ConsumeToken();
430 break;
431
Chris Lattnera2449b22008-10-20 05:57:40 +0000432 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000433 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000434 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000435 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000436 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000437 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000438 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000439 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000440 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattnera2449b22008-10-20 05:57:40 +0000442 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000443 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000444 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000445
Chris Lattnere82a10f2008-10-20 05:46:22 +0000446 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000447 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000448 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000449 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000451 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000452 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000453
Chris Lattnere82a10f2008-10-20 05:46:22 +0000454 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000455 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000456 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
John McCall7da19ea2011-03-26 01:53:26 +0000458 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000459 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000460 }
Steve Naroff294494e2007-08-22 16:35:03 +0000461 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000462
463 // We break out of the big loop in two cases: when we see @end or when we see
464 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000465 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000466 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000467 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000468 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000469 ConsumeToken(); // the "end" identifier
470 else
471 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattnera2449b22008-10-20 05:57:40 +0000473 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000474 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000475 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000476 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000477 allProperties.data(), allProperties.size(),
478 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000479}
480
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000481/// Parse property attribute declarations.
482///
483/// property-attr-decl: '(' property-attrlist ')'
484/// property-attrlist:
485/// property-attribute
486/// property-attrlist ',' property-attribute
487/// property-attribute:
488/// getter '=' identifier
489/// setter '=' identifier ':'
490/// readonly
491/// readwrite
492/// assign
493/// retain
494/// copy
495/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000496/// atomic
497/// strong
498/// weak
499/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000500///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000501void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000502 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000503 BalancedDelimiterTracker T(*this, tok::l_paren);
504 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000506 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000507 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000508 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000509 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000510 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000511 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000513 // If this is not an identifier at all, bail out early.
514 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000515 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000516 return;
517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattner156b0612008-10-20 07:37:22 +0000519 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner92e62b02008-11-20 04:42:34 +0000521 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000523 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000525 else if (II->isStr("unsafe_unretained"))
526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000527 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000528 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000529 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000530 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000531 else if (II->isStr("strong"))
532 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000533 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000534 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000535 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000536 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000537 else if (II->isStr("atomic"))
538 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000539 else if (II->isStr("weak"))
540 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000541 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000542 bool IsSetter = II->getNameStart()[0] == 's';
543
Chris Lattnere00da7c2008-10-20 07:39:53 +0000544 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000545 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
546 diag::err_objc_expected_equal_for_getter;
547
548 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000549 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Douglas Gregor4ad96852009-11-19 07:41:15 +0000551 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000552 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000553 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000554 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000555 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000556 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000557 }
558
Anders Carlsson42499be2010-10-02 17:45:21 +0000559
560 SourceLocation SelLoc;
561 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
562
563 if (!SelIdent) {
564 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
565 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000566 SkipUntil(tok::r_paren);
567 return;
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Anders Carlsson42499be2010-10-02 17:45:21 +0000570 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000572 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000574 if (ExpectAndConsume(tok::colon,
575 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000576 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000577 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000578 } else {
579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000580 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000581 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000582 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000583 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000584 SkipUntil(tok::r_paren);
585 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner156b0612008-10-20 07:37:22 +0000588 if (Tok.isNot(tok::comma))
589 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattner156b0612008-10-20 07:37:22 +0000591 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000594 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000595}
596
Steve Naroff3536b442007-09-06 21:24:23 +0000597/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000598/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000599/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000600///
601/// objc-instance-method: '-'
602/// objc-class-method: '+'
603///
Steve Naroff4985ace2007-08-22 18:35:33 +0000604/// objc-method-attributes: [OBJC2]
605/// __attribute__((deprecated))
606///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000607Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000608 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000609 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000610
Mike Stump1eb44332009-09-09 15:08:12 +0000611 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000612 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000613 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000614 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000615 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000616 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000617 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000618}
619
620/// objc-selector:
621/// identifier
622/// one of
623/// enum struct union if else while do for switch case default
624/// break continue return goto asm sizeof typeof __alignof
625/// unsigned long const short volatile signed restrict _Complex
626/// in out inout bycopy byref oneway int char float double void _Bool
627///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000628IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000629
Chris Lattnerff384912007-10-07 02:00:24 +0000630 switch (Tok.getKind()) {
631 default:
632 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000633 case tok::ampamp:
634 case tok::ampequal:
635 case tok::amp:
636 case tok::pipe:
637 case tok::tilde:
638 case tok::exclaim:
639 case tok::exclaimequal:
640 case tok::pipepipe:
641 case tok::pipeequal:
642 case tok::caret:
643 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000644 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000645 if (isalpha(ThisTok[0])) {
646 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
647 Tok.setKind(tok::identifier);
648 SelectorLoc = ConsumeToken();
649 return II;
650 }
651 return 0;
652 }
653
Chris Lattnerff384912007-10-07 02:00:24 +0000654 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000655 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000656 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000657 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000658 case tok::kw_break:
659 case tok::kw_case:
660 case tok::kw_catch:
661 case tok::kw_char:
662 case tok::kw_class:
663 case tok::kw_const:
664 case tok::kw_const_cast:
665 case tok::kw_continue:
666 case tok::kw_default:
667 case tok::kw_delete:
668 case tok::kw_do:
669 case tok::kw_double:
670 case tok::kw_dynamic_cast:
671 case tok::kw_else:
672 case tok::kw_enum:
673 case tok::kw_explicit:
674 case tok::kw_export:
675 case tok::kw_extern:
676 case tok::kw_false:
677 case tok::kw_float:
678 case tok::kw_for:
679 case tok::kw_friend:
680 case tok::kw_goto:
681 case tok::kw_if:
682 case tok::kw_inline:
683 case tok::kw_int:
684 case tok::kw_long:
685 case tok::kw_mutable:
686 case tok::kw_namespace:
687 case tok::kw_new:
688 case tok::kw_operator:
689 case tok::kw_private:
690 case tok::kw_protected:
691 case tok::kw_public:
692 case tok::kw_register:
693 case tok::kw_reinterpret_cast:
694 case tok::kw_restrict:
695 case tok::kw_return:
696 case tok::kw_short:
697 case tok::kw_signed:
698 case tok::kw_sizeof:
699 case tok::kw_static:
700 case tok::kw_static_cast:
701 case tok::kw_struct:
702 case tok::kw_switch:
703 case tok::kw_template:
704 case tok::kw_this:
705 case tok::kw_throw:
706 case tok::kw_true:
707 case tok::kw_try:
708 case tok::kw_typedef:
709 case tok::kw_typeid:
710 case tok::kw_typename:
711 case tok::kw_typeof:
712 case tok::kw_union:
713 case tok::kw_unsigned:
714 case tok::kw_using:
715 case tok::kw_virtual:
716 case tok::kw_void:
717 case tok::kw_volatile:
718 case tok::kw_wchar_t:
719 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000720 case tok::kw__Bool:
721 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000722 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000723 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000724 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000725 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000726 }
Steve Naroff294494e2007-08-22 16:35:03 +0000727}
728
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000729/// objc-for-collection-in: 'in'
730///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000731bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000732 // FIXME: May have to do additional look-ahead to only allow for
733 // valid tokens following an 'in'; such as an identifier, unary operators,
734 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000735 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000736 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000737}
738
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000739/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000740/// qualifier list and builds their bitmask representation in the input
741/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000742///
743/// objc-type-qualifiers:
744/// objc-type-qualifier
745/// objc-type-qualifiers objc-type-qualifier
746///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000747void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000748 Declarator::TheContext Context) {
749 assert(Context == Declarator::ObjCParameterContext ||
750 Context == Declarator::ObjCResultContext);
751
Chris Lattnere8b724d2007-12-12 06:56:32 +0000752 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000753 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000754 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000755 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000756 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000757 }
758
Chris Lattnercb53b362007-12-27 19:57:00 +0000759 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000760 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattnere8b724d2007-12-12 06:56:32 +0000762 const IdentifierInfo *II = Tok.getIdentifierInfo();
763 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000764 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000765 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000767 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000768 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000769 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
771 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
772 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
773 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
774 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
775 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000776 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000777 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000778 ConsumeToken();
779 II = 0;
780 break;
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnere8b724d2007-12-12 06:56:32 +0000783 // If this wasn't a recognized qualifier, bail out.
784 if (II) return;
785 }
786}
787
John McCallcdda47f2011-10-01 09:56:14 +0000788/// Take all the decl attributes out of the given list and add
789/// them to the given attribute set.
790static void takeDeclAttributes(ParsedAttributes &attrs,
791 AttributeList *list) {
792 while (list) {
793 AttributeList *cur = list;
794 list = cur->getNext();
795
796 if (!cur->isUsedAsTypeAttr()) {
797 // Clear out the next pointer. We're really completely
798 // destroying the internal invariants of the declarator here,
799 // but it doesn't matter because we're done with it.
800 cur->setNext(0);
801 attrs.add(cur);
802 }
803 }
804}
805
806/// takeDeclAttributes - Take all the decl attributes from the given
807/// declarator and add them to the given list.
808static void takeDeclAttributes(ParsedAttributes &attrs,
809 Declarator &D) {
810 // First, take ownership of all attributes.
811 attrs.getPool().takeAllFrom(D.getAttributePool());
812 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
813
814 // Now actually move the attributes over.
815 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
816 takeDeclAttributes(attrs, D.getAttributes());
817 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
818 takeDeclAttributes(attrs,
819 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
820}
821
Chris Lattnere8b724d2007-12-12 06:56:32 +0000822/// objc-type-name:
823/// '(' objc-type-qualifiers[opt] type-name ')'
824/// '(' objc-type-qualifiers[opt] ')'
825///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000826ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000827 Declarator::TheContext context,
828 ParsedAttributes *paramAttrs) {
829 assert(context == Declarator::ObjCParameterContext ||
830 context == Declarator::ObjCResultContext);
831 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
832
Chris Lattnerdf195262007-10-09 17:51:17 +0000833 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000835 BalancedDelimiterTracker T(*this, tok::l_paren);
836 T.consumeOpen();
837
Chris Lattnere8904e92008-08-23 01:48:03 +0000838 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000839 ObjCDeclContextSwitch ObjCDC(*this);
840
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000841 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000842 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000843
John McCallb3d87482010-08-24 05:47:05 +0000844 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000845 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000846 // Parse an abstract declarator.
847 DeclSpec declSpec(AttrFactory);
848 declSpec.setObjCQualifiers(&DS);
849 ParseSpecifierQualifierList(declSpec);
850 Declarator declarator(declSpec, context);
851 ParseDeclarator(declarator);
852
853 // If that's not invalid, extract a type.
854 if (!declarator.isInvalidType()) {
855 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
856 if (!type.isInvalid())
857 Ty = type.get();
858
859 // If we're parsing a parameter, steal all the decl attributes
860 // and add them to the decl spec.
861 if (context == Declarator::ObjCParameterContext)
862 takeDeclAttributes(*paramAttrs, declarator);
863 }
864 } else if (context == Declarator::ObjCResultContext &&
865 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000866 if (!Ident_instancetype)
867 Ident_instancetype = PP.getIdentifierInfo("instancetype");
868
869 if (Tok.getIdentifierInfo() == Ident_instancetype) {
870 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
871 ConsumeToken();
872 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000873 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000874
Steve Naroffd7333c22008-10-21 14:15:04 +0000875 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000876 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000877 else if (Tok.getLocation() == TypeStartLoc) {
878 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000879 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000880 SkipUntil(tok::r_paren);
881 } else {
882 // Otherwise, we found *something*, but didn't get a ')' in the right
883 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000884 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000885 }
Steve Narofff28b2642007-09-05 23:30:30 +0000886 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000887}
888
889/// objc-method-decl:
890/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000891/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000892/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000893/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000894///
895/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000896/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000897/// objc-keyword-selector objc-keyword-decl
898///
899/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000900/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
901/// objc-selector ':' objc-keyword-attributes[opt] identifier
902/// ':' objc-type-name objc-keyword-attributes[opt] identifier
903/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000904///
Steve Naroff4985ace2007-08-22 18:35:33 +0000905/// objc-parmlist:
906/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000907///
Steve Naroff4985ace2007-08-22 18:35:33 +0000908/// objc-parms:
909/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000910///
Steve Naroff4985ace2007-08-22 18:35:33 +0000911/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000912/// , ...
913///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000914/// objc-keyword-attributes: [OBJC2]
915/// __attribute__((unused))
916///
John McCalld226f652010-08-21 09:40:31 +0000917Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000918 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000919 tok::ObjCKeywordKind MethodImplKind,
920 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000921 ParsingDeclRAIIObject PD(*this);
922
Douglas Gregore8f5a172010-04-07 00:21:17 +0000923 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000924 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000925 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000926 cutOffParsing();
927 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000928 }
929
Chris Lattnere8904e92008-08-23 01:48:03 +0000930 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000931 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000932 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000933 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000934 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Ted Kremenek9e049352010-02-18 23:05:16 +0000936 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000937 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000938 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000939 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000940
Douglas Gregore8f5a172010-04-07 00:21:17 +0000941 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000942 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000943 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000944 cutOffParsing();
945 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000946 }
947
Ted Kremenek9e049352010-02-18 23:05:16 +0000948 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000949 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000950 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000951
Steve Naroff84c43102009-02-11 20:43:13 +0000952 // An unnamed colon is valid.
953 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000954 Diag(Tok, diag::err_expected_selector_for_method)
955 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000956 // Skip until we get a ; or {}.
957 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000958 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Chris Lattner5f9e2722011-07-23 10:55:15 +0000961 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000963 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000964 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000965 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Chris Lattnerff384912007-10-07 02:00:24 +0000967 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000968 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000969 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000970 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000971 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000972 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000973 methodAttrs.getList(), MethodImplKind,
974 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000975 PD.complete(Result);
976 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000977 }
Steve Narofff28b2642007-09-05 23:30:30 +0000978
Chris Lattner5f9e2722011-07-23 10:55:15 +0000979 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +0000980 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000981 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000982 ParseScope PrototypeScope(this,
983 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000984
985 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000986
Chris Lattnerff384912007-10-07 02:00:24 +0000987 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000988 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000989 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattnerff384912007-10-07 02:00:24 +0000991 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000992 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000993 Diag(Tok, diag::err_expected_colon);
994 break;
995 }
996 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000997
John McCallb3d87482010-08-24 05:47:05 +0000998 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000999 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001000 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1001 Declarator::ObjCParameterContext,
1002 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001003
Chris Lattnerff384912007-10-07 02:00:24 +00001004 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001005 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001006 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001007 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001008 MaybeParseGNUAttributes(paramAttrs);
1009 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001010 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001011
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001012 // Code completion for the next piece of the selector.
1013 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001014 KeyIdents.push_back(SelIdent);
1015 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1016 mType == tok::minus,
1017 /*AtParameterName=*/true,
1018 ReturnType,
1019 KeyIdents.data(),
1020 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001021 cutOffParsing();
1022 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001023 }
1024
Chris Lattnerdf195262007-10-09 17:51:17 +00001025 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001026 Diag(Tok, diag::err_expected_ident); // missing argument name.
1027 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattnere294d3f2009-04-11 18:57:04 +00001030 ArgInfo.Name = Tok.getIdentifierInfo();
1031 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001032 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattnere294d3f2009-04-11 18:57:04 +00001034 ArgInfos.push_back(ArgInfo);
1035 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001036 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001037
John McCall0b7e6782011-03-24 11:26:52 +00001038 // Make sure the attributes persist.
1039 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1040
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001041 // Code completion for the next piece of the selector.
1042 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001043 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1044 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001045 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001046 ReturnType,
1047 KeyIdents.data(),
1048 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001049 cutOffParsing();
1050 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001051 }
1052
Chris Lattnerff384912007-10-07 02:00:24 +00001053 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001054 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001055 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001056 break;
1057 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Steve Naroff335eafa2007-11-15 12:35:21 +00001060 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Chris Lattnerff384912007-10-07 02:00:24 +00001062 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001063 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001064 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001065 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001066 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001067 ConsumeToken();
1068 break;
1069 }
John McCall0b7e6782011-03-24 11:26:52 +00001070 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001071 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001072 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001073 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1074 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001075 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001076 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001077 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1078 ParmDecl.getIdentifierLoc(),
1079 Param,
1080 0));
1081
Chris Lattnerff384912007-10-07 02:00:24 +00001082 }
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001084 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001085 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001086 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001087 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001088
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001089 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001090 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001091
Chris Lattnerff384912007-10-07 02:00:24 +00001092 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1093 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001094 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001095 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001096 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001097 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001098 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001099 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001100 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001101
John McCall54abf7d2009-11-04 02:18:39 +00001102 PD.complete(Result);
1103 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001104}
1105
Steve Naroffdac269b2007-08-20 21:31:48 +00001106/// objc-protocol-refs:
1107/// '<' identifier-list '>'
1108///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001109bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001110ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1111 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001112 bool WarnOnDeclarations,
1113 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001114 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001116 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattner5f9e2722011-07-23 10:55:15 +00001118 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattnere13b9592008-07-26 04:03:38 +00001120 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001121 if (Tok.is(tok::code_completion)) {
1122 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1123 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001124 cutOffParsing();
1125 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001126 }
1127
Chris Lattnere13b9592008-07-26 04:03:38 +00001128 if (Tok.isNot(tok::identifier)) {
1129 Diag(Tok, diag::err_expected_ident);
1130 SkipUntil(tok::greater);
1131 return true;
1132 }
1133 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1134 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001135 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001136 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Chris Lattnere13b9592008-07-26 04:03:38 +00001138 if (Tok.isNot(tok::comma))
1139 break;
1140 ConsumeToken();
1141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattnere13b9592008-07-26 04:03:38 +00001143 // Consume the '>'.
1144 if (Tok.isNot(tok::greater)) {
1145 Diag(Tok, diag::err_expected_greater);
1146 return true;
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Chris Lattnere13b9592008-07-26 04:03:38 +00001149 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Chris Lattnere13b9592008-07-26 04:03:38 +00001151 // Convert the list of protocols identifiers into a list of protocol decls.
1152 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1153 &ProtocolIdents[0], ProtocolIdents.size(),
1154 Protocols);
1155 return false;
1156}
1157
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001158/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1159/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001160bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001161 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1162 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1163 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001164 SmallVector<Decl *, 8> ProtocolDecl;
1165 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001166 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1167 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001168 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1169 ProtocolLocs.data(), LAngleLoc);
1170 if (EndProtoLoc.isValid())
1171 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001172 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001173}
1174
1175
Steve Naroffdac269b2007-08-20 21:31:48 +00001176/// objc-class-instance-variables:
1177/// '{' objc-instance-variable-decl-list[opt] '}'
1178///
1179/// objc-instance-variable-decl-list:
1180/// objc-visibility-spec
1181/// objc-instance-variable-decl ';'
1182/// ';'
1183/// objc-instance-variable-decl-list objc-visibility-spec
1184/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1185/// objc-instance-variable-decl-list ';'
1186///
1187/// objc-visibility-spec:
1188/// @private
1189/// @protected
1190/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001191/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001192///
1193/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001194/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001195///
John McCalld226f652010-08-21 09:40:31 +00001196void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001197 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001198 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001199 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001200 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001201
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001202 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001203 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001204
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001205 BalancedDelimiterTracker T(*this, tok::l_brace);
1206 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Steve Naroffddbff782007-08-21 21:17:12 +00001208 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001209 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001210 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Steve Naroffddbff782007-08-21 21:17:12 +00001212 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001213 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001214 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001215 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001216 ConsumeToken();
1217 continue;
1218 }
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Steve Naroffddbff782007-08-21 21:17:12 +00001220 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001221 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001222 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001223
1224 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001225 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001226 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001227 }
1228
Steve Naroff861cf3e2007-08-23 18:16:40 +00001229 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001230 case tok::objc_private:
1231 case tok::objc_public:
1232 case tok::objc_protected:
1233 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001234 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001235 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001236 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001237 default:
1238 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001239 continue;
1240 }
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001243 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001244 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001245 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001246 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001247 }
1248
John McCallbdd563e2009-11-03 02:38:08 +00001249 struct ObjCIvarCallback : FieldCallback {
1250 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001251 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001252 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001253 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001254
John McCalld226f652010-08-21 09:40:31 +00001255 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001256 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001257 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1258 }
1259
John McCalld226f652010-08-21 09:40:31 +00001260 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001261 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001262 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001263 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001264 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001265 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001266 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001267 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001268 if (Field)
1269 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001270 return Field;
1271 }
1272 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001273
Chris Lattnere1359422008-04-10 06:46:29 +00001274 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001275 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001276 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Chris Lattnerdf195262007-10-09 17:51:17 +00001278 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001279 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001280 } else {
1281 Diag(Tok, diag::err_expected_semi_decl_list);
1282 // Skip to end of block or statement
1283 SkipUntil(tok::r_brace, true, true);
1284 }
1285 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001286 T.consumeClose();
1287
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001288 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001289 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001290 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001291 // Call ActOnFields() even if we don't have any decls. This is useful
1292 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001293 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001294 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001295 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001296 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001297}
Steve Naroffdac269b2007-08-20 21:31:48 +00001298
1299/// objc-protocol-declaration:
1300/// objc-protocol-definition
1301/// objc-protocol-forward-reference
1302///
1303/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001304/// @protocol identifier
1305/// objc-protocol-refs[opt]
1306/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001307/// @end
1308///
1309/// objc-protocol-forward-reference:
1310/// @protocol identifier-list ';'
1311///
1312/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001313/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001314/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001315Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001316 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001317 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001318 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1319 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregor083128f2009-11-18 04:49:41 +00001321 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001322 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001323 cutOffParsing();
1324 return 0;
Douglas Gregor083128f2009-11-18 04:49:41 +00001325 }
1326
Chris Lattnerdf195262007-10-09 17:51:17 +00001327 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001328 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001329 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001330 }
1331 // Save the protocol name, then consume it.
1332 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1333 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Chris Lattnerdf195262007-10-09 17:51:17 +00001335 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001336 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001337 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001338 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001339 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Chris Lattnerdf195262007-10-09 17:51:17 +00001342 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001343 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001344 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1345
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001346 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001347 while (1) {
1348 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001349 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001350 Diag(Tok, diag::err_expected_ident);
1351 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001352 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001353 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001354 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1355 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001356 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Chris Lattnerdf195262007-10-09 17:51:17 +00001358 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001359 break;
1360 }
1361 // Consume the ';'.
1362 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001363 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Steve Naroffe440eb82007-10-10 17:32:04 +00001365 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001366 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001367 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001368 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001369 }
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001371 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001372 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001373
Chris Lattner5f9e2722011-07-23 10:55:15 +00001374 SmallVector<Decl *, 8> ProtocolRefs;
1375 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001376 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001377 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1378 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001379 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
John McCalld226f652010-08-21 09:40:31 +00001381 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001382 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001383 ProtocolRefs.data(),
1384 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001385 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001386 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001387
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001388 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001389 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001390}
Steve Naroffdac269b2007-08-20 21:31:48 +00001391
1392/// objc-implementation:
1393/// objc-class-implementation-prologue
1394/// objc-category-implementation-prologue
1395///
1396/// objc-class-implementation-prologue:
1397/// @implementation identifier objc-superclass[opt]
1398/// objc-class-instance-variables[opt]
1399///
1400/// objc-category-implementation-prologue:
1401/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001402Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001403 SourceLocation atLoc) {
1404 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1405 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1406 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001408 // Code completion after '@implementation'.
1409 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001410 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001411 cutOffParsing();
1412 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001413 }
1414
Chris Lattnerdf195262007-10-09 17:51:17 +00001415 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001416 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001417 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001418 }
1419 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001420 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001421 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001422
1423 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001424 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001425 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001426 SourceLocation categoryLoc, rparenLoc;
1427 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001429 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001430 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001431 cutOffParsing();
1432 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001433 }
1434
Chris Lattnerdf195262007-10-09 17:51:17 +00001435 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001436 categoryId = Tok.getIdentifierInfo();
1437 categoryLoc = ConsumeToken();
1438 } else {
1439 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001440 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001441 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001442 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001443 Diag(Tok, diag::err_expected_rparen);
1444 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001445 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001446 }
1447 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001448 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001449 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001450 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001451
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001452 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001453 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001454 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001455 }
1456 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001457 SourceLocation superClassLoc;
1458 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001459 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001460 // We have a super class
1461 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001462 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001463 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001464 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001465 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001466 superClassId = Tok.getIdentifierInfo();
1467 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001468 }
John McCalld226f652010-08-21 09:40:31 +00001469 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001470 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001471 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Steve Naroff60fccee2007-10-29 21:38:07 +00001473 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001474 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1475
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001477 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001478 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001479}
Steve Naroff60fccee2007-10-29 21:38:07 +00001480
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001481Parser::DeclGroupPtrTy
1482Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001483 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1484 "ParseObjCAtEndDeclaration(): Expected @end");
1485 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001486 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001487 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001488 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1489 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis35593a92011-11-16 02:35:10 +00001490 if (D)
1491 DeclsInGroup.push_back(D);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001492 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001493 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001494
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001495 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001496 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001497 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001498 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001499 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001500 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001501 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001502
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001503 clearLateParsedObjCMethods();
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001504 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001505 return Actions.BuildDeclaratorGroup(
1506 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001507}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001508
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001509Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1510 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001511 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001512 return Actions.ConvertDeclToDeclGroup(0);
1513 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001514 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001515 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1516}
1517
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001518void Parser::clearLateParsedObjCMethods() {
1519 for (LateParsedObjCMethodContainer::iterator
1520 I = LateParsedObjCMethods.begin(),
1521 E = LateParsedObjCMethods.end(); I != E; ++I)
1522 delete *I;
1523 LateParsedObjCMethods.clear();
1524}
1525
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001526/// compatibility-alias-decl:
1527/// @compatibility_alias alias-name class-name ';'
1528///
John McCalld226f652010-08-21 09:40:31 +00001529Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001530 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1531 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1532 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001533 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001534 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001535 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001536 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001537 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1538 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001539 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001540 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001541 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001542 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001543 IdentifierInfo *classId = Tok.getIdentifierInfo();
1544 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001545 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1546 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001547 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1548 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001549}
1550
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001551/// property-synthesis:
1552/// @synthesize property-ivar-list ';'
1553///
1554/// property-ivar-list:
1555/// property-ivar
1556/// property-ivar-list ',' property-ivar
1557///
1558/// property-ivar:
1559/// identifier
1560/// identifier '=' identifier
1561///
John McCalld226f652010-08-21 09:40:31 +00001562Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001563 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1564 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001565 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Douglas Gregorb328c422009-11-18 19:45:45 +00001567 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001568 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001569 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001570 cutOffParsing();
1571 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001572 }
1573
Douglas Gregorb328c422009-11-18 19:45:45 +00001574 if (Tok.isNot(tok::identifier)) {
1575 Diag(Tok, diag::err_synthesized_property_name);
1576 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001577 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001578 }
1579
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001580 IdentifierInfo *propertyIvar = 0;
1581 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1582 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001583 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001584 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001585 // property '=' ivar-name
1586 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001587
1588 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001589 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001590 cutOffParsing();
1591 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001592 }
1593
Chris Lattnerdf195262007-10-09 17:51:17 +00001594 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001595 Diag(Tok, diag::err_expected_ident);
1596 break;
1597 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001598 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001599 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001600 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001601 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001602 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001603 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001604 break;
1605 ConsumeToken(); // consume ','
1606 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001607 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001608 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001609}
1610
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001611/// property-dynamic:
1612/// @dynamic property-list
1613///
1614/// property-list:
1615/// identifier
1616/// property-list ',' identifier
1617///
John McCalld226f652010-08-21 09:40:31 +00001618Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001619 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1620 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001621 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001622 while (true) {
1623 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001624 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001625 cutOffParsing();
1626 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001627 }
1628
1629 if (Tok.isNot(tok::identifier)) {
1630 Diag(Tok, diag::err_expected_ident);
1631 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001632 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001633 }
1634
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001635 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1636 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001637 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001638 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001639
Chris Lattnerdf195262007-10-09 17:51:17 +00001640 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001641 break;
1642 ConsumeToken(); // consume ','
1643 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001644 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001645 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001646}
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001648/// objc-throw-statement:
1649/// throw expression[opt];
1650///
John McCall60d7b3a2010-08-24 06:29:42 +00001651StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1652 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001653 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001654 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001655 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001656 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001657 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001658 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001659 }
1660 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001661 // consume ';'
1662 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001663 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001664}
1665
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001666/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001667/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001668///
John McCall60d7b3a2010-08-24 06:29:42 +00001669StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001670Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001671 ConsumeToken(); // consume synchronized
1672 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001673 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001674 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001675 }
John McCall07524032011-07-27 21:50:02 +00001676
1677 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001678 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001679 ExprResult operand(ParseExpression());
1680
1681 if (Tok.is(tok::r_paren)) {
1682 ConsumeParen(); // ')'
1683 } else {
1684 if (!operand.isInvalid())
1685 Diag(Tok, diag::err_expected_rparen);
1686
1687 // Skip forward until we see a left brace, but don't consume it.
1688 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001689 }
John McCall07524032011-07-27 21:50:02 +00001690
1691 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001692 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001693 if (!operand.isInvalid())
1694 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001695 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001696 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001697
John McCall07524032011-07-27 21:50:02 +00001698 // Check the @synchronized operand now.
1699 if (!operand.isInvalid())
1700 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001701
John McCall07524032011-07-27 21:50:02 +00001702 // Parse the compound statement within a new scope.
1703 ParseScope bodyScope(this, Scope::DeclScope);
1704 StmtResult body(ParseCompoundStatementBody());
1705 bodyScope.Exit();
1706
1707 // If there was a semantic or parse error earlier with the
1708 // operand, fail now.
1709 if (operand.isInvalid())
1710 return StmtError();
1711
1712 if (body.isInvalid())
1713 body = Actions.ActOnNullStmt(Tok.getLocation());
1714
1715 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001716}
1717
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001718/// objc-try-catch-statement:
1719/// @try compound-statement objc-catch-list[opt]
1720/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1721///
1722/// objc-catch-list:
1723/// @catch ( parameter-declaration ) compound-statement
1724/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1725/// catch-parameter-declaration:
1726/// parameter-declaration
1727/// '...' [OBJC2]
1728///
John McCall60d7b3a2010-08-24 06:29:42 +00001729StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001730 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001731
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001732 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001733 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001734 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001735 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001736 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001737 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001738 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001739 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001740 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001741 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001742 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001743 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001744
Chris Lattnerdf195262007-10-09 17:51:17 +00001745 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001746 // At this point, we need to lookahead to determine if this @ is the start
1747 // of an @catch or @finally. We don't want to consume the @ token if this
1748 // is an @try or @encode or something else.
1749 Token AfterAt = GetLookAheadToken(1);
1750 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1751 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1752 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001753
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001754 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001755 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001756 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001757 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001758 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001759 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001760 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001761 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001762 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001763 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001764 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001765 ParseDeclarator(ParmDecl);
1766
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001767 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001768 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001769 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001770 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001771 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Steve Naroff93a25952009-04-07 22:56:58 +00001773 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Steve Naroff93a25952009-04-07 22:56:58 +00001775 if (Tok.is(tok::r_paren))
1776 RParenLoc = ConsumeParen();
1777 else // Skip over garbage, until we get to ')'. Eat the ')'.
1778 SkipUntil(tok::r_paren, true, false);
1779
John McCall60d7b3a2010-08-24 06:29:42 +00001780 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001781 if (Tok.is(tok::l_brace))
1782 CatchBody = ParseCompoundStatementBody();
1783 else
1784 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001785 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001786 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001787
John McCall60d7b3a2010-08-24 06:29:42 +00001788 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001789 RParenLoc,
1790 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001791 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001792 if (!Catch.isInvalid())
1793 CatchStmts.push_back(Catch.release());
1794
Steve Naroff64515f32008-02-05 21:27:35 +00001795 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001796 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1797 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001798 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001799 }
1800 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001801 } else {
1802 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001803 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001804 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001805
John McCall60d7b3a2010-08-24 06:29:42 +00001806 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001807 if (Tok.is(tok::l_brace))
1808 FinallyBody = ParseCompoundStatementBody();
1809 else
1810 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001811 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001812 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001813 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001814 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001815 catch_or_finally_seen = true;
1816 break;
1817 }
1818 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001819 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001820 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001821 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001822 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001823
John McCall9ae2f072010-08-23 23:25:46 +00001824 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001825 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001826 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001827}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001828
John McCallf85e1932011-06-15 23:02:42 +00001829/// objc-autoreleasepool-statement:
1830/// @autoreleasepool compound-statement
1831///
1832StmtResult
1833Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1834 ConsumeToken(); // consume autoreleasepool
1835 if (Tok.isNot(tok::l_brace)) {
1836 Diag(Tok, diag::err_expected_lbrace);
1837 return StmtError();
1838 }
1839 // Enter a scope to hold everything within the compound stmt. Compound
1840 // statements can always hold declarations.
1841 ParseScope BodyScope(this, Scope::DeclScope);
1842
1843 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1844
1845 BodyScope.Exit();
1846 if (AutoreleasePoolBody.isInvalid())
1847 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1848 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1849 AutoreleasePoolBody.take());
1850}
1851
Steve Naroff3536b442007-09-06 21:24:23 +00001852/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001853///
John McCalld226f652010-08-21 09:40:31 +00001854Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001855 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001856
John McCallf312b1e2010-08-26 23:41:50 +00001857 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1858 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001860 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001861 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001862 if (ObjCImpDecl) {
1863 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001864 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001865 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001866 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001867 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001868
Steve Naroff409be832007-11-11 19:54:21 +00001869 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001870 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001871 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Steve Naroff409be832007-11-11 19:54:21 +00001873 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1874 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Steve Naroff409be832007-11-11 19:54:21 +00001876 // If we didn't find the '{', bail out.
1877 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001878 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001879 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001880 // Allow the rest of sema to find private method decl implementations.
1881 if (MDecl)
1882 Actions.AddAnyMethodToGlobalPool(MDecl);
1883
1884 // Consume the tokens and store them for later parsing.
1885 LexedMethod* LM = new LexedMethod(this, MDecl);
1886 LateParsedObjCMethods.push_back(LM);
1887 CachedTokens &Toks = LM->Toks;
1888 // Begin by storing the '{' token.
1889 Toks.push_back(Tok);
1890 ConsumeBrace();
1891 // Consume everything up to (and including) the matching right brace.
1892 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001893 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001894}
Anders Carlsson55085182007-08-21 17:43:55 +00001895
John McCall60d7b3a2010-08-24 06:29:42 +00001896StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001897 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001898 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001899 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001900 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001901 }
1902
1903 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001904 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001905
1906 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001907 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001908
1909 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001910 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001911
1912 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1913 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001914
John McCall60d7b3a2010-08-24 06:29:42 +00001915 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001916 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001917 // If the expression is invalid, skip ahead to the next semicolon. Not
1918 // doing this opens us up to the possibility of infinite loops if
1919 // ParseExpression does not consume any tokens.
1920 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001921 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001922 }
Chris Lattner5d803162009-12-07 16:33:19 +00001923
Steve Naroff64515f32008-02-05 21:27:35 +00001924 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001925 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001926 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001927}
1928
John McCall60d7b3a2010-08-24 06:29:42 +00001929ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001930 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001931 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001932 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001933 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001934 return ExprError();
1935
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001936 case tok::string_literal: // primary-expression: string-literal
1937 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001938 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001939 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001940 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001941 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001942
Chris Lattner4fef81d2008-08-05 06:19:09 +00001943 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1944 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001945 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001946 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001947 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001948 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001949 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001950 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001951 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001952 }
Anders Carlsson55085182007-08-21 17:43:55 +00001953 }
Anders Carlsson55085182007-08-21 17:43:55 +00001954}
1955
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001956/// \brirg Parse the receiver of an Objective-C++ message send.
1957///
1958/// This routine parses the receiver of a message send in
1959/// Objective-C++ either as a type or as an expression. Note that this
1960/// routine must not be called to parse a send to 'super', since it
1961/// has no way to return such a result.
1962///
1963/// \param IsExpr Whether the receiver was parsed as an expression.
1964///
1965/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1966/// IsExpr is true), the parsed expression. If the receiver was parsed
1967/// as a type (\c IsExpr is false), the parsed type.
1968///
1969/// \returns True if an error occurred during parsing or semantic
1970/// analysis, in which case the arguments do not have valid
1971/// values. Otherwise, returns false for a successful parse.
1972///
1973/// objc-receiver: [C++]
1974/// 'super' [not parsed here]
1975/// expression
1976/// simple-type-specifier
1977/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001978bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001979 InMessageExpressionRAIIObject InMessage(*this, true);
1980
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001981 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1982 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1983 TryAnnotateTypeOrScopeToken();
1984
1985 if (!isCXXSimpleTypeSpecifier()) {
1986 // objc-receiver:
1987 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001988 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001989 if (Receiver.isInvalid())
1990 return true;
1991
1992 IsExpr = true;
1993 TypeOrExpr = Receiver.take();
1994 return false;
1995 }
1996
1997 // objc-receiver:
1998 // typename-specifier
1999 // simple-type-specifier
2000 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002001 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002002 ParseCXXSimpleTypeSpecifier(DS);
2003
2004 if (Tok.is(tok::l_paren)) {
2005 // If we see an opening parentheses at this point, we are
2006 // actually parsing an expression that starts with a
2007 // function-style cast, e.g.,
2008 //
2009 // postfix-expression:
2010 // simple-type-specifier ( expression-list [opt] )
2011 // typename-specifier ( expression-list [opt] )
2012 //
2013 // Parse the remainder of this case, then the (optional)
2014 // postfix-expression suffix, followed by the (optional)
2015 // right-hand side of the binary expression. We have an
2016 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002017 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002018 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002019 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002020 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002021 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002022 if (Receiver.isInvalid())
2023 return true;
2024
2025 IsExpr = true;
2026 TypeOrExpr = Receiver.take();
2027 return false;
2028 }
2029
2030 // We have a class message. Turn the simple-type-specifier or
2031 // typename-specifier we parsed into a type and parse the
2032 // remainder of the class message.
2033 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002034 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002035 if (Type.isInvalid())
2036 return true;
2037
2038 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002039 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002040 return false;
2041}
2042
Douglas Gregor1b730e82010-05-31 14:40:22 +00002043/// \brief Determine whether the parser is currently referring to a an
2044/// Objective-C message send, using a simplified heuristic to avoid overhead.
2045///
2046/// This routine will only return true for a subset of valid message-send
2047/// expressions.
2048bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002049 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002050 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002051 return GetLookAheadToken(1).is(tok::identifier) &&
2052 GetLookAheadToken(2).is(tok::identifier);
2053}
2054
Douglas Gregor9497a732010-09-16 01:51:54 +00002055bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2056 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2057 InMessageExpression)
2058 return false;
2059
2060
2061 ParsedType Type;
2062
2063 if (Tok.is(tok::annot_typename))
2064 Type = getTypeAnnotation(Tok);
2065 else if (Tok.is(tok::identifier))
2066 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2067 getCurScope());
2068 else
2069 return false;
2070
2071 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2072 const Token &AfterNext = GetLookAheadToken(2);
2073 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2074 if (Tok.is(tok::identifier))
2075 TryAnnotateTypeOrScopeToken();
2076
2077 return Tok.is(tok::annot_typename);
2078 }
2079 }
2080
2081 return false;
2082}
2083
Mike Stump1eb44332009-09-09 15:08:12 +00002084/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002085/// '[' objc-receiver objc-message-args ']'
2086///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002087/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002088/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002089/// expression
2090/// class-name
2091/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002092///
John McCall60d7b3a2010-08-24 06:29:42 +00002093ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002094 assert(Tok.is(tok::l_square) && "'[' expected");
2095 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2096
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002097 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002098 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002099 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002100 return ExprError();
2101 }
2102
Douglas Gregor0fbda682010-09-15 14:51:05 +00002103 InMessageExpressionRAIIObject InMessage(*this, true);
2104
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002105 if (getLang().CPlusPlus) {
2106 // We completely separate the C and C++ cases because C++ requires
2107 // more complicated (read: slower) parsing.
2108
2109 // Handle send to super.
2110 // FIXME: This doesn't benefit from the same typo-correction we
2111 // get in Objective-C.
2112 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002113 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002114 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2115 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002116
2117 // Parse the receiver, which is either a type or an expression.
2118 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002119 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002120 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2121 SkipUntil(tok::r_square);
2122 return ExprError();
2123 }
2124
2125 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002126 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2127 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002128 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002129
2130 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002131 ParsedType::getFromOpaquePtr(TypeOrExpr),
2132 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002133 }
2134
2135 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002136 IdentifierInfo *Name = Tok.getIdentifierInfo();
2137 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002138 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002139 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002140 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002141 NextToken().is(tok::period),
2142 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002143 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002144 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2145 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002146
John McCallf312b1e2010-08-26 23:41:50 +00002147 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002148 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002149 SkipUntil(tok::r_square);
2150 return ExprError();
2151 }
2152
Douglas Gregor1569f952010-04-21 20:38:13 +00002153 ConsumeToken(); // the type name
2154
2155 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002156 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002157
John McCallf312b1e2010-08-26 23:41:50 +00002158 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002159 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002160 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002161 }
Chris Lattner699b6612008-01-25 18:59:06 +00002162 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002163
2164 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002165 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002166 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002167 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002168 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002169 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002170
John McCallb3d87482010-08-24 05:47:05 +00002171 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2172 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002173}
Sebastian Redl1d922962008-12-13 15:32:12 +00002174
Douglas Gregor2725ca82010-04-21 19:57:20 +00002175/// \brief Parse the remainder of an Objective-C message following the
2176/// '[' objc-receiver.
2177///
2178/// This routine handles sends to super, class messages (sent to a
2179/// class name), and instance messages (sent to an object), and the
2180/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2181/// ReceiverExpr, respectively. Only one of these parameters may have
2182/// a valid value.
2183///
2184/// \param LBracLoc The location of the opening '['.
2185///
2186/// \param SuperLoc If this is a send to 'super', the location of the
2187/// 'super' keyword that indicates a send to the superclass.
2188///
2189/// \param ReceiverType If this is a class message, the type of the
2190/// class we are sending a message to.
2191///
2192/// \param ReceiverExpr If this is an instance message, the expression
2193/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002194///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002195/// objc-message-args:
2196/// objc-selector
2197/// objc-keywordarg-list
2198///
2199/// objc-keywordarg-list:
2200/// objc-keywordarg
2201/// objc-keywordarg-list objc-keywordarg
2202///
Mike Stump1eb44332009-09-09 15:08:12 +00002203/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002204/// selector-name[opt] ':' objc-keywordexpr
2205///
2206/// objc-keywordexpr:
2207/// nonempty-expr-list
2208///
2209/// nonempty-expr-list:
2210/// assignment-expression
2211/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002212///
John McCall60d7b3a2010-08-24 06:29:42 +00002213ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002214Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002215 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002216 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002217 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002218 InMessageExpressionRAIIObject InMessage(*this, true);
2219
Steve Naroffc4df6d22009-11-07 02:08:14 +00002220 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002221 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002222 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2223 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002224 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002225 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2226 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002227 else
John McCall9ae2f072010-08-23 23:25:46 +00002228 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002229 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002230 cutOffParsing();
2231 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002232 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002233
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002234 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002235 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002236 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002237
Chris Lattner5f9e2722011-07-23 10:55:15 +00002238 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002239 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002240 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002241
Chris Lattnerdf195262007-10-09 17:51:17 +00002242 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002243 while (1) {
2244 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002245 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002246 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002247
Chris Lattnerdf195262007-10-09 17:51:17 +00002248 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002249 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002250 // We must manually skip to a ']', otherwise the expression skipper will
2251 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2252 // the enclosing expression.
2253 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002254 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002255 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002256
Steve Naroff68d331a2007-09-27 14:38:14 +00002257 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002258 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002259
2260 if (Tok.is(tok::code_completion)) {
2261 if (SuperLoc.isValid())
2262 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2263 KeyIdents.data(),
2264 KeyIdents.size(),
2265 /*AtArgumentEpression=*/true);
2266 else if (ReceiverType)
2267 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2268 KeyIdents.data(),
2269 KeyIdents.size(),
2270 /*AtArgumentEpression=*/true);
2271 else
2272 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2273 KeyIdents.data(),
2274 KeyIdents.size(),
2275 /*AtArgumentEpression=*/true);
2276
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002277 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002278 return ExprError();
2279 }
2280
John McCall60d7b3a2010-08-24 06:29:42 +00002281 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002282 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002283 // We must manually skip to a ']', otherwise the expression skipper will
2284 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2285 // the enclosing expression.
2286 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002287 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002288 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002289
Steve Naroff37387c92007-09-17 20:25:27 +00002290 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002291 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002292
Douglas Gregord3c68542009-11-19 01:08:35 +00002293 // Code completion after each argument.
2294 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002295 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002296 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002297 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002298 KeyIdents.size(),
2299 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002300 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002301 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002302 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002303 KeyIdents.size(),
2304 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002305 else
John McCall9ae2f072010-08-23 23:25:46 +00002306 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002307 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002308 KeyIdents.size(),
2309 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002310 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002311 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002312 }
2313
Steve Naroff37387c92007-09-17 20:25:27 +00002314 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002315 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002316 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002317 break;
2318 // We have a selector or a colon, continue parsing.
2319 }
2320 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002321 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002322 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002323 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002324 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002325 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002326 // We must manually skip to a ']', otherwise the expression skipper will
2327 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2328 // the enclosing expression.
2329 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002330 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002331 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002332
Steve Naroff49f109c2007-11-15 13:05:42 +00002333 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002334 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002335 }
2336 } else if (!selIdent) {
2337 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002338
Chris Lattner4fef81d2008-08-05 06:19:09 +00002339 // We must manually skip to a ']', otherwise the expression skipper will
2340 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2341 // the enclosing expression.
2342 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002343 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002344 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002345
Chris Lattnerdf195262007-10-09 17:51:17 +00002346 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002347 if (Tok.is(tok::identifier))
2348 Diag(Tok, diag::err_expected_colon);
2349 else
2350 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002351 // We must manually skip to a ']', otherwise the expression skipper will
2352 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2353 // the enclosing expression.
2354 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002355 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002356 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002357
Chris Lattner699b6612008-01-25 18:59:06 +00002358 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002359
Steve Naroff29238a02007-10-05 18:42:47 +00002360 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002361 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002362 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002363 KeyLocs.push_back(Loc);
2364 }
Chris Lattnerff384912007-10-07 02:00:24 +00002365 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002366
Douglas Gregor2725ca82010-04-21 19:57:20 +00002367 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002368 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002369 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002370 MultiExprArg(Actions,
2371 KeyExprs.take(),
2372 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002373 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002374 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002375 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002376 MultiExprArg(Actions,
2377 KeyExprs.take(),
2378 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002379 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002380 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002381 MultiExprArg(Actions,
2382 KeyExprs.take(),
2383 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002384}
2385
John McCall60d7b3a2010-08-24 06:29:42 +00002386ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2387 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002388 if (Res.isInvalid()) return move(Res);
2389
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002390 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2391 // expressions. At this point, we know that the only valid thing that starts
2392 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002393 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002394 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002395 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002396 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002397
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002398 while (Tok.is(tok::at)) {
2399 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002400
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002401 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002402 if (!isTokenStringLiteral())
2403 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002404
John McCall60d7b3a2010-08-24 06:29:42 +00002405 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002406 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002407 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002408
Sebastian Redleffa8d12008-12-10 00:02:53 +00002409 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002410 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002411
2412 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2413 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002414}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002415
2416/// objc-encode-expression:
2417/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002418ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002419Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002420 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002421
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002422 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002423
Chris Lattner4fef81d2008-08-05 06:19:09 +00002424 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002425 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2426
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002427 BalancedDelimiterTracker T(*this, tok::l_paren);
2428 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002429
Douglas Gregor809070a2009-02-18 17:45:20 +00002430 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002431
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002432 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002433
Douglas Gregor809070a2009-02-18 17:45:20 +00002434 if (Ty.isInvalid())
2435 return ExprError();
2436
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002437 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2438 T.getOpenLocation(), Ty.get(),
2439 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002440}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002441
2442/// objc-protocol-expression
2443/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002444ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002445Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002446 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002447
Chris Lattner4fef81d2008-08-05 06:19:09 +00002448 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002449 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2450
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002451 BalancedDelimiterTracker T(*this, tok::l_paren);
2452 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002453
Chris Lattner4fef81d2008-08-05 06:19:09 +00002454 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002455 return ExprError(Diag(Tok, diag::err_expected_ident));
2456
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002457 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002458 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002459
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002460 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002461
Sebastian Redl1d922962008-12-13 15:32:12 +00002462 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002463 T.getOpenLocation(),
2464 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002465}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002466
2467/// objc-selector-expression
2468/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002469ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002470 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002471
Chris Lattner4fef81d2008-08-05 06:19:09 +00002472 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002473 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2474
Chris Lattner5f9e2722011-07-23 10:55:15 +00002475 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002476 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002477
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002478 BalancedDelimiterTracker T(*this, tok::l_paren);
2479 T.consumeOpen();
2480
Douglas Gregor458433d2010-08-26 15:07:07 +00002481 if (Tok.is(tok::code_completion)) {
2482 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2483 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002484 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002485 return ExprError();
2486 }
2487
Chris Lattner2fc5c242009-04-11 18:13:45 +00002488 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002489 if (!SelIdent && // missing selector name.
2490 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002491 return ExprError(Diag(Tok, diag::err_expected_ident));
2492
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002493 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002494 unsigned nColons = 0;
2495 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002496 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002497 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2498 ++nColons;
2499 KeyIdents.push_back(0);
2500 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002501 return ExprError(Diag(Tok, diag::err_expected_colon));
2502
Chris Lattner5add7542010-08-27 22:32:41 +00002503 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002504 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002505 if (Tok.is(tok::r_paren))
2506 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002507
2508 if (Tok.is(tok::code_completion)) {
2509 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2510 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002511 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002512 return ExprError();
2513 }
2514
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002515 // Check for another keyword selector.
2516 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002517 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002518 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002519 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002520 break;
2521 }
Steve Naroff887407e2007-12-05 22:21:29 +00002522 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002523 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002524 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002525 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002526 T.getOpenLocation(),
2527 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002528 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002529
2530Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2531
2532 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2533 // Append the current token at the end of the new token stream so that it
2534 // doesn't get lost.
2535 LM.Toks.push_back(Tok);
2536 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2537
2538 // MDecl might be null due to error in method prototype, etc.
2539 Decl *MDecl = LM.D;
2540 // Consume the previously pushed token.
2541 ConsumeAnyToken();
2542
2543 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2544 SourceLocation BraceLoc = Tok.getLocation();
2545 // Enter a scope for the method body.
2546 ParseScope BodyScope(this,
2547 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2548
2549 // Tell the actions module that we have entered a method definition with the
2550 // specified Declarator for the method.
2551 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2552
2553 if (PP.isCodeCompletionEnabled()) {
2554 if (trySkippingFunctionBodyForCodeCompletion()) {
2555 BodyScope.Exit();
2556 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2557 }
2558 }
2559
2560 StmtResult FnBody(ParseCompoundStatementBody());
2561
2562 // If the function body could not be parsed, make a bogus compoundstmt.
2563 if (FnBody.isInvalid())
2564 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2565 MultiStmtArg(Actions), false);
2566
2567 // Leave the function body scope.
2568 BodyScope.Exit();
2569
2570 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2571}