blob: 5be5a6c3c2f2115fa08e61bc4cc3529ec1cd7ae7 [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.
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000167 // TODO(dgregor): Use the return value from the next line to provide better
168 // recovery.
169 ConsumeParen();
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 SourceLocation categoryLoc, rparenLoc;
171 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000172 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000173 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000174 cutOffParsing();
175 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000176 }
177
Steve Naroff527fe232007-08-23 19:56:30 +0000178 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000179 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 categoryId = Tok.getIdentifierInfo();
181 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000182 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000183 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000184 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000185 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000186 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000187 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000188 Diag(Tok, diag::err_expected_rparen);
189 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +0000190 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000191 }
192 rparenLoc = ConsumeParen();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000193 // Next, we need to check for any protocol references.
194 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000195 SmallVector<Decl *, 8> ProtocolRefs;
196 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000197 if (Tok.is(tok::less) &&
198 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000199 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000200 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000201
John McCall7f040a92010-12-24 02:08:15 +0000202 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000203 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
John McCalld226f652010-08-21 09:40:31 +0000205 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000206 Actions.ActOnStartCategoryInterface(atLoc,
207 nameId, nameLoc,
208 categoryId, categoryLoc,
209 ProtocolRefs.data(),
210 ProtocolRefs.size(),
211 ProtocolLocs.data(),
212 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000213
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000214 if (Tok.is(tok::l_brace))
215 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
216
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000217 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000218 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000219 }
220 // Parse a class interface.
221 IdentifierInfo *superClassId = 0;
222 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000223
Chris Lattnerdf195262007-10-09 17:51:17 +0000224 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000225 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000226
227 // Code completion of superclass names.
228 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000229 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000230 cutOffParsing();
231 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000232 }
233
Chris Lattnerdf195262007-10-09 17:51:17 +0000234 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000235 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000236 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000237 }
238 superClassId = Tok.getIdentifierInfo();
239 superClassLoc = ConsumeToken();
240 }
241 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000242 SmallVector<Decl *, 8> ProtocolRefs;
243 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000244 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000245 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000246 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
247 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000248 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
John McCalld226f652010-08-21 09:40:31 +0000250 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000251 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000252 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000253 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000254 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000255 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnerdf195262007-10-09 17:51:17 +0000257 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000258 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000259
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000260 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000261 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000262}
263
John McCalld0014542009-12-03 22:31:13 +0000264/// The Objective-C property callback. This should be defined where
265/// it's used, but instead it's been lifted to here to support VS2005.
266struct Parser::ObjCPropertyCallback : FieldCallback {
267 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000268 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000269 ObjCDeclSpec &OCDS;
270 SourceLocation AtLoc;
271 tok::ObjCKeywordKind MethodImplKind;
272
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000273 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000274 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000275 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
276 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000277 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000278 MethodImplKind(MethodImplKind) {
279 }
280
John McCalld226f652010-08-21 09:40:31 +0000281 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000282 if (FD.D.getIdentifier() == 0) {
283 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
284 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000285 return 0;
John McCalld0014542009-12-03 22:31:13 +0000286 }
287 if (FD.BitfieldSize) {
288 P.Diag(AtLoc, diag::err_objc_property_bitfield)
289 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000290 return 0;
John McCalld0014542009-12-03 22:31:13 +0000291 }
292
293 // Install the property declarator into interfaceDecl.
294 IdentifierInfo *SelName =
295 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
296
297 Selector GetterSel =
298 P.PP.getSelectorTable().getNullarySelector(SelName);
299 IdentifierInfo *SetterName = OCDS.getSetterName();
300 Selector SetterSel;
301 if (SetterName)
302 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
303 else
304 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
305 P.PP.getSelectorTable(),
306 FD.D.getIdentifier());
307 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000308 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000309 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000310 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000311 &isOverridingProperty,
312 MethodImplKind);
313 if (!isOverridingProperty)
314 Props.push_back(Property);
315
316 return Property;
317 }
318};
319
Steve Naroffdac269b2007-08-20 21:31:48 +0000320/// objc-interface-decl-list:
321/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000322/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000323/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000324/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000325/// objc-interface-decl-list declaration
326/// objc-interface-decl-list ';'
327///
Steve Naroff294494e2007-08-22 16:35:03 +0000328/// objc-method-requirement: [OBJC2]
329/// @required
330/// @optional
331///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000332void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
333 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000334 SmallVector<Decl *, 32> allMethods;
335 SmallVector<Decl *, 16> allProperties;
336 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000337 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek782f2f52010-01-07 01:20:12 +0000339 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000340 Actions.ActOnObjCContainerStartDefinition(CDecl);
341
Steve Naroff294494e2007-08-22 16:35:03 +0000342 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000343 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000344 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000345 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000346 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000347 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000348 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
349 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000350 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
351 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000352 continue;
353 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000354 if (Tok.is(tok::l_paren)) {
355 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000356 ParseObjCMethodDecl(Tok.getLocation(),
357 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000358 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000359 continue;
360 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000361 // Ignore excess semicolons.
362 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000363 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000364 continue;
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Chris Lattnerbc662af2008-10-20 06:10:06 +0000367 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000368 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000369 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000371 // Code completion within an Objective-C interface.
372 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000373 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000374 ObjCImpDecl? Sema::PCC_ObjCImplementation
375 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000376 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000377 }
378
Chris Lattnere82a10f2008-10-20 05:46:22 +0000379 // If we don't have an @ directive, parse it as a function definition.
380 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000381 // The code below does not consume '}'s because it is afraid of eating the
382 // end of a namespace. Because of the way this code is structured, an
383 // erroneous r_brace would cause an infinite loop if not handled here.
384 if (Tok.is(tok::r_brace))
385 break;
John McCall0b7e6782011-03-24 11:26:52 +0000386 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000387 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000388 continue;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattnere82a10f2008-10-20 05:46:22 +0000391 // Otherwise, we have an @ directive, eat the @.
392 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000393 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000394 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000395 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000396 break;
397 }
398
Chris Lattnera2449b22008-10-20 05:57:40 +0000399 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattnera2449b22008-10-20 05:57:40 +0000401 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000402 AtEnd.setBegin(AtLoc);
403 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000404 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000405 } else if (DirectiveKind == tok::objc_not_keyword) {
406 Diag(Tok, diag::err_objc_unknown_at);
407 SkipUntil(tok::semi);
408 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Chris Lattnerbc662af2008-10-20 06:10:06 +0000411 // Eat the identifier.
412 ConsumeToken();
413
Chris Lattnera2449b22008-10-20 05:57:40 +0000414 switch (DirectiveKind) {
415 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000416 // FIXME: If someone forgets an @end on a protocol, this loop will
417 // continue to eat up tons of stuff and spew lots of nonsense errors. It
418 // would probably be better to bail out if we saw an @class or @interface
419 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000420 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000421 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000422 SkipUntil(tok::r_brace, tok::at);
423 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000424
425 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000426 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000427 Diag(Tok, diag::err_objc_missing_end);
428 ConsumeToken();
429 break;
430
Chris Lattnera2449b22008-10-20 05:57:40 +0000431 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000432 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000433 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000434 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000435 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000436 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000437 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000438 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000439 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattnera2449b22008-10-20 05:57:40 +0000441 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000442 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000443 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000444
Chris Lattnere82a10f2008-10-20 05:46:22 +0000445 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000446 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000447 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000448 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000450 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000451 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000452
Chris Lattnere82a10f2008-10-20 05:46:22 +0000453 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000454 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000455 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
John McCall7da19ea2011-03-26 01:53:26 +0000457 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000458 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000459 }
Steve Naroff294494e2007-08-22 16:35:03 +0000460 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000461
462 // We break out of the big loop in two cases: when we see @end or when we see
463 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000464 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000465 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000466 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000467 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000468 ConsumeToken(); // the "end" identifier
469 else
470 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattnera2449b22008-10-20 05:57:40 +0000472 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000473 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000474 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000475 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000476 allProperties.data(), allProperties.size(),
477 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000478}
479
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000480/// Parse property attribute declarations.
481///
482/// property-attr-decl: '(' property-attrlist ')'
483/// property-attrlist:
484/// property-attribute
485/// property-attrlist ',' property-attribute
486/// property-attribute:
487/// getter '=' identifier
488/// setter '=' identifier ':'
489/// readonly
490/// readwrite
491/// assign
492/// retain
493/// copy
494/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000495/// atomic
496/// strong
497/// weak
498/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000499///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000500void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000501 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000502 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000504 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000505 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000506 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000507 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000508 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000509 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000511 // If this is not an identifier at all, bail out early.
512 if (II == 0) {
513 MatchRHSPunctuation(tok::r_paren, LHSLoc);
514 return;
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner156b0612008-10-20 07:37:22 +0000517 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattner92e62b02008-11-20 04:42:34 +0000519 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000521 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000523 else if (II->isStr("unsafe_unretained"))
524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000525 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000527 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000528 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000529 else if (II->isStr("strong"))
530 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000531 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000532 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000533 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000534 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000535 else if (II->isStr("atomic"))
536 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000537 else if (II->isStr("weak"))
538 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000539 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000540 bool IsSetter = II->getNameStart()[0] == 's';
541
Chris Lattnere00da7c2008-10-20 07:39:53 +0000542 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000543 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
544 diag::err_objc_expected_equal_for_getter;
545
546 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000547 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregor4ad96852009-11-19 07:41:15 +0000549 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000550 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000551 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000552 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000553 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000554 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000555 }
556
Anders Carlsson42499be2010-10-02 17:45:21 +0000557
558 SourceLocation SelLoc;
559 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
560
561 if (!SelIdent) {
562 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
563 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000564 SkipUntil(tok::r_paren);
565 return;
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Anders Carlsson42499be2010-10-02 17:45:21 +0000568 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000570 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000572 if (ExpectAndConsume(tok::colon,
573 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000574 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000575 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000576 } else {
577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000578 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000579 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000580 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000581 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000582 SkipUntil(tok::r_paren);
583 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattner156b0612008-10-20 07:37:22 +0000586 if (Tok.isNot(tok::comma))
587 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Chris Lattner156b0612008-10-20 07:37:22 +0000589 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Chris Lattner156b0612008-10-20 07:37:22 +0000592 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000593}
594
Steve Naroff3536b442007-09-06 21:24:23 +0000595/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000596/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000597/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000598///
599/// objc-instance-method: '-'
600/// objc-class-method: '+'
601///
Steve Naroff4985ace2007-08-22 18:35:33 +0000602/// objc-method-attributes: [OBJC2]
603/// __attribute__((deprecated))
604///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000605Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000606 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000607 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000608
Mike Stump1eb44332009-09-09 15:08:12 +0000609 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000610 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000611 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000612 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000613 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000614 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000615 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000616}
617
618/// objc-selector:
619/// identifier
620/// one of
621/// enum struct union if else while do for switch case default
622/// break continue return goto asm sizeof typeof __alignof
623/// unsigned long const short volatile signed restrict _Complex
624/// in out inout bycopy byref oneway int char float double void _Bool
625///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000626IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000627
Chris Lattnerff384912007-10-07 02:00:24 +0000628 switch (Tok.getKind()) {
629 default:
630 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000631 case tok::ampamp:
632 case tok::ampequal:
633 case tok::amp:
634 case tok::pipe:
635 case tok::tilde:
636 case tok::exclaim:
637 case tok::exclaimequal:
638 case tok::pipepipe:
639 case tok::pipeequal:
640 case tok::caret:
641 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000642 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000643 if (isalpha(ThisTok[0])) {
644 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
645 Tok.setKind(tok::identifier);
646 SelectorLoc = ConsumeToken();
647 return II;
648 }
649 return 0;
650 }
651
Chris Lattnerff384912007-10-07 02:00:24 +0000652 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000653 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000654 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000655 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000656 case tok::kw_break:
657 case tok::kw_case:
658 case tok::kw_catch:
659 case tok::kw_char:
660 case tok::kw_class:
661 case tok::kw_const:
662 case tok::kw_const_cast:
663 case tok::kw_continue:
664 case tok::kw_default:
665 case tok::kw_delete:
666 case tok::kw_do:
667 case tok::kw_double:
668 case tok::kw_dynamic_cast:
669 case tok::kw_else:
670 case tok::kw_enum:
671 case tok::kw_explicit:
672 case tok::kw_export:
673 case tok::kw_extern:
674 case tok::kw_false:
675 case tok::kw_float:
676 case tok::kw_for:
677 case tok::kw_friend:
678 case tok::kw_goto:
679 case tok::kw_if:
680 case tok::kw_inline:
681 case tok::kw_int:
682 case tok::kw_long:
683 case tok::kw_mutable:
684 case tok::kw_namespace:
685 case tok::kw_new:
686 case tok::kw_operator:
687 case tok::kw_private:
688 case tok::kw_protected:
689 case tok::kw_public:
690 case tok::kw_register:
691 case tok::kw_reinterpret_cast:
692 case tok::kw_restrict:
693 case tok::kw_return:
694 case tok::kw_short:
695 case tok::kw_signed:
696 case tok::kw_sizeof:
697 case tok::kw_static:
698 case tok::kw_static_cast:
699 case tok::kw_struct:
700 case tok::kw_switch:
701 case tok::kw_template:
702 case tok::kw_this:
703 case tok::kw_throw:
704 case tok::kw_true:
705 case tok::kw_try:
706 case tok::kw_typedef:
707 case tok::kw_typeid:
708 case tok::kw_typename:
709 case tok::kw_typeof:
710 case tok::kw_union:
711 case tok::kw_unsigned:
712 case tok::kw_using:
713 case tok::kw_virtual:
714 case tok::kw_void:
715 case tok::kw_volatile:
716 case tok::kw_wchar_t:
717 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000718 case tok::kw__Bool:
719 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000720 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000721 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000722 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000723 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000724 }
Steve Naroff294494e2007-08-22 16:35:03 +0000725}
726
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000727/// objc-for-collection-in: 'in'
728///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000729bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000730 // FIXME: May have to do additional look-ahead to only allow for
731 // valid tokens following an 'in'; such as an identifier, unary operators,
732 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000733 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000734 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000735}
736
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000737/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000738/// qualifier list and builds their bitmask representation in the input
739/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000740///
741/// objc-type-qualifiers:
742/// objc-type-qualifier
743/// objc-type-qualifiers objc-type-qualifier
744///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000745void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
746 ObjCTypeNameContext Context) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000747 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000748 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000749 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
750 Context == OTN_ParameterType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000751 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000752 }
753
Chris Lattnercb53b362007-12-27 19:57:00 +0000754 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000755 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattnere8b724d2007-12-12 06:56:32 +0000757 const IdentifierInfo *II = Tok.getIdentifierInfo();
758 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000759 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000760 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000762 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000763 switch (i) {
764 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000765 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
766 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
767 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
768 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
769 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
770 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000771 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000772 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000773 ConsumeToken();
774 II = 0;
775 break;
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattnere8b724d2007-12-12 06:56:32 +0000778 // If this wasn't a recognized qualifier, bail out.
779 if (II) return;
780 }
781}
782
783/// objc-type-name:
784/// '(' objc-type-qualifiers[opt] type-name ')'
785/// '(' objc-type-qualifiers[opt] ')'
786///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000787ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
788 ObjCTypeNameContext Context) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000789 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattner4a76b292008-10-22 03:52:06 +0000791 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000792 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000793 ObjCDeclContextSwitch ObjCDC(*this);
794
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000795 // Parse type qualifiers, in, inout, etc.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000796 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000797
John McCallb3d87482010-08-24 05:47:05 +0000798 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000799 if (isTypeSpecifierQualifier()) {
John McCallf85e1932011-06-15 23:02:42 +0000800 TypeResult TypeSpec =
801 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000802 if (!TypeSpec.isInvalid())
803 Ty = TypeSpec.get();
804 }
John McCallf85e1932011-06-15 23:02:42 +0000805
Steve Naroffd7333c22008-10-21 14:15:04 +0000806 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000807 ConsumeParen();
808 else if (Tok.getLocation() == TypeStartLoc) {
809 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000810 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000811 SkipUntil(tok::r_paren);
812 } else {
813 // Otherwise, we found *something*, but didn't get a ')' in the right
814 // place. Emit an error then return what we have as the type.
815 MatchRHSPunctuation(tok::r_paren, LParenLoc);
816 }
Steve Narofff28b2642007-09-05 23:30:30 +0000817 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000818}
819
820/// objc-method-decl:
821/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000822/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000823/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000824/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000825///
826/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000827/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000828/// objc-keyword-selector objc-keyword-decl
829///
830/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000831/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
832/// objc-selector ':' objc-keyword-attributes[opt] identifier
833/// ':' objc-type-name objc-keyword-attributes[opt] identifier
834/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000835///
Steve Naroff4985ace2007-08-22 18:35:33 +0000836/// objc-parmlist:
837/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000838///
Steve Naroff4985ace2007-08-22 18:35:33 +0000839/// objc-parms:
840/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000841///
Steve Naroff4985ace2007-08-22 18:35:33 +0000842/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000843/// , ...
844///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000845/// objc-keyword-attributes: [OBJC2]
846/// __attribute__((unused))
847///
John McCalld226f652010-08-21 09:40:31 +0000848Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000849 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000850 tok::ObjCKeywordKind MethodImplKind,
851 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000852 ParsingDeclRAIIObject PD(*this);
853
Douglas Gregore8f5a172010-04-07 00:21:17 +0000854 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000855 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000856 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000857 cutOffParsing();
858 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000859 }
860
Chris Lattnere8904e92008-08-23 01:48:03 +0000861 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000862 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000863 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000864 if (Tok.is(tok::l_paren))
Douglas Gregorb77cab92011-03-08 19:17:54 +0000865 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Ted Kremenek9e049352010-02-18 23:05:16 +0000867 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000868 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000869 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000870 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000871
Douglas Gregore8f5a172010-04-07 00:21:17 +0000872 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000873 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000874 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000875 cutOffParsing();
876 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000877 }
878
Ted Kremenek9e049352010-02-18 23:05:16 +0000879 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000880 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000881 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000882
Steve Naroff84c43102009-02-11 20:43:13 +0000883 // An unnamed colon is valid.
884 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000885 Diag(Tok, diag::err_expected_selector_for_method)
886 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000887 // Skip until we get a ; or {}.
888 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000889 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner5f9e2722011-07-23 10:55:15 +0000892 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000893 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000894 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000895 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000896 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Chris Lattnerff384912007-10-07 02:00:24 +0000898 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000899 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000900 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000901 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000902 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000903 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000904 methodAttrs.getList(), MethodImplKind,
905 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000906 PD.complete(Result);
907 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000908 }
Steve Narofff28b2642007-09-05 23:30:30 +0000909
Chris Lattner5f9e2722011-07-23 10:55:15 +0000910 SmallVector<IdentifierInfo *, 12> KeyIdents;
911 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000912 ParseScope PrototypeScope(this,
913 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000914
915 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000916
Chris Lattnerff384912007-10-07 02:00:24 +0000917 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000918 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000919 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Chris Lattnerff384912007-10-07 02:00:24 +0000921 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000922 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000923 Diag(Tok, diag::err_expected_colon);
924 break;
925 }
926 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000927
John McCallb3d87482010-08-24 05:47:05 +0000928 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000929 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000930 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000931
Chris Lattnerff384912007-10-07 02:00:24 +0000932 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000933 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +0000934 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +0000935 MaybeParseGNUAttributes(paramAttrs);
936 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +0000937 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000938
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000939 // Code completion for the next piece of the selector.
940 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000941 KeyIdents.push_back(SelIdent);
942 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
943 mType == tok::minus,
944 /*AtParameterName=*/true,
945 ReturnType,
946 KeyIdents.data(),
947 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000948 cutOffParsing();
949 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000950 }
951
Chris Lattnerdf195262007-10-09 17:51:17 +0000952 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000953 Diag(Tok, diag::err_expected_ident); // missing argument name.
954 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000955 }
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Chris Lattnere294d3f2009-04-11 18:57:04 +0000957 ArgInfo.Name = Tok.getIdentifierInfo();
958 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000959 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Chris Lattnere294d3f2009-04-11 18:57:04 +0000961 ArgInfos.push_back(ArgInfo);
962 KeyIdents.push_back(SelIdent);
963
John McCall0b7e6782011-03-24 11:26:52 +0000964 // Make sure the attributes persist.
965 allParamAttrs.takeAllFrom(paramAttrs.getPool());
966
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000967 // Code completion for the next piece of the selector.
968 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000969 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
970 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000971 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000972 ReturnType,
973 KeyIdents.data(),
974 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000975 cutOffParsing();
976 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000977 }
978
Chris Lattnerff384912007-10-07 02:00:24 +0000979 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000980 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000981 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000982 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000983 break;
984 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Steve Naroff335eafa2007-11-15 12:35:21 +0000987 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattnerff384912007-10-07 02:00:24 +0000989 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000990 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000991 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000992 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000993 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000994 ConsumeToken();
995 break;
996 }
John McCall0b7e6782011-03-24 11:26:52 +0000997 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +0000998 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000999 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001000 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1001 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001002 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001003 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001004 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1005 ParmDecl.getIdentifierLoc(),
1006 Param,
1007 0));
1008
Chris Lattnerff384912007-10-07 02:00:24 +00001009 }
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001011 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001012 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001013 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001014 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001015
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001016 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001017 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001018
Chris Lattnerff384912007-10-07 02:00:24 +00001019 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1020 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001021 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001022 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001023 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001024 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001025 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001026 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001027 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001028
John McCall54abf7d2009-11-04 02:18:39 +00001029 PD.complete(Result);
1030 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001031}
1032
Steve Naroffdac269b2007-08-20 21:31:48 +00001033/// objc-protocol-refs:
1034/// '<' identifier-list '>'
1035///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001036bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001037ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1038 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001039 bool WarnOnDeclarations,
1040 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001041 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001043 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattner5f9e2722011-07-23 10:55:15 +00001045 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Chris Lattnere13b9592008-07-26 04:03:38 +00001047 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001048 if (Tok.is(tok::code_completion)) {
1049 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1050 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001051 cutOffParsing();
1052 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001053 }
1054
Chris Lattnere13b9592008-07-26 04:03:38 +00001055 if (Tok.isNot(tok::identifier)) {
1056 Diag(Tok, diag::err_expected_ident);
1057 SkipUntil(tok::greater);
1058 return true;
1059 }
1060 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1061 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001062 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001063 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattnere13b9592008-07-26 04:03:38 +00001065 if (Tok.isNot(tok::comma))
1066 break;
1067 ConsumeToken();
1068 }
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattnere13b9592008-07-26 04:03:38 +00001070 // Consume the '>'.
1071 if (Tok.isNot(tok::greater)) {
1072 Diag(Tok, diag::err_expected_greater);
1073 return true;
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Chris Lattnere13b9592008-07-26 04:03:38 +00001076 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Chris Lattnere13b9592008-07-26 04:03:38 +00001078 // Convert the list of protocols identifiers into a list of protocol decls.
1079 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1080 &ProtocolIdents[0], ProtocolIdents.size(),
1081 Protocols);
1082 return false;
1083}
1084
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001085/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1086/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001087bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001088 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1089 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1090 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001091 SmallVector<Decl *, 8> ProtocolDecl;
1092 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001093 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1094 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001095 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1096 ProtocolLocs.data(), LAngleLoc);
1097 if (EndProtoLoc.isValid())
1098 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001099 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001100}
1101
1102
Steve Naroffdac269b2007-08-20 21:31:48 +00001103/// objc-class-instance-variables:
1104/// '{' objc-instance-variable-decl-list[opt] '}'
1105///
1106/// objc-instance-variable-decl-list:
1107/// objc-visibility-spec
1108/// objc-instance-variable-decl ';'
1109/// ';'
1110/// objc-instance-variable-decl-list objc-visibility-spec
1111/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1112/// objc-instance-variable-decl-list ';'
1113///
1114/// objc-visibility-spec:
1115/// @private
1116/// @protected
1117/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001118/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001119///
1120/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001121/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001122///
John McCalld226f652010-08-21 09:40:31 +00001123void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001124 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001125 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001126 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001127 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001128
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001129 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001130
Steve Naroffddbff782007-08-21 21:17:12 +00001131 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Steve Naroffddbff782007-08-21 21:17:12 +00001133 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001134 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001135 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Steve Naroffddbff782007-08-21 21:17:12 +00001137 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001138 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001139 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001140 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001141 ConsumeToken();
1142 continue;
1143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Steve Naroffddbff782007-08-21 21:17:12 +00001145 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001146 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001147 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001148
1149 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001150 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001151 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001152 }
1153
Steve Naroff861cf3e2007-08-23 18:16:40 +00001154 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001155 case tok::objc_private:
1156 case tok::objc_public:
1157 case tok::objc_protected:
1158 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001159 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001160 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001161 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001162 default:
1163 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001164 continue;
1165 }
1166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001168 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001169 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001170 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001171 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001172 }
1173
John McCallbdd563e2009-11-03 02:38:08 +00001174 struct ObjCIvarCallback : FieldCallback {
1175 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001176 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001177 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001178 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001179
John McCalld226f652010-08-21 09:40:31 +00001180 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001181 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001182 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1183 }
1184
John McCalld226f652010-08-21 09:40:31 +00001185 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001186 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001187 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001188 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001189 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001190 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001191 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001192 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001193 if (Field)
1194 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001195 return Field;
1196 }
1197 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001198
Chris Lattnere1359422008-04-10 06:46:29 +00001199 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001200 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001201 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Chris Lattnerdf195262007-10-09 17:51:17 +00001203 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001204 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001205 } else {
1206 Diag(Tok, diag::err_expected_semi_decl_list);
1207 // Skip to end of block or statement
1208 SkipUntil(tok::r_brace, true, true);
1209 }
1210 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001211 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001212 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1213 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001214 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001215 // Call ActOnFields() even if we don't have any decls. This is useful
1216 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001217 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001218 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001219 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001220 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001221}
Steve Naroffdac269b2007-08-20 21:31:48 +00001222
1223/// objc-protocol-declaration:
1224/// objc-protocol-definition
1225/// objc-protocol-forward-reference
1226///
1227/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001228/// @protocol identifier
1229/// objc-protocol-refs[opt]
1230/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001231/// @end
1232///
1233/// objc-protocol-forward-reference:
1234/// @protocol identifier-list ';'
1235///
1236/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001237/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001238/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001239Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001240 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001241 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001242 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1243 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Douglas Gregor083128f2009-11-18 04:49:41 +00001245 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001246 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001247 cutOffParsing();
1248 return 0;
Douglas Gregor083128f2009-11-18 04:49:41 +00001249 }
1250
Chris Lattnerdf195262007-10-09 17:51:17 +00001251 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001252 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001253 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001254 }
1255 // Save the protocol name, then consume it.
1256 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1257 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Chris Lattnerdf195262007-10-09 17:51:17 +00001259 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001260 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001261 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001262 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001263 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001264 }
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Chris Lattnerdf195262007-10-09 17:51:17 +00001266 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001267 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001268 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1269
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001270 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001271 while (1) {
1272 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001273 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001274 Diag(Tok, diag::err_expected_ident);
1275 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001276 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001277 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001278 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1279 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001280 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Chris Lattnerdf195262007-10-09 17:51:17 +00001282 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001283 break;
1284 }
1285 // Consume the ';'.
1286 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001287 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Steve Naroffe440eb82007-10-10 17:32:04 +00001289 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001290 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001291 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001292 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001295 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001296 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001297
Chris Lattner5f9e2722011-07-23 10:55:15 +00001298 SmallVector<Decl *, 8> ProtocolRefs;
1299 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001300 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001301 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1302 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001303 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001304
John McCalld226f652010-08-21 09:40:31 +00001305 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001306 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001307 ProtocolRefs.data(),
1308 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001309 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001310 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001311
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001312 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001313 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001314}
Steve Naroffdac269b2007-08-20 21:31:48 +00001315
1316/// objc-implementation:
1317/// objc-class-implementation-prologue
1318/// objc-category-implementation-prologue
1319///
1320/// objc-class-implementation-prologue:
1321/// @implementation identifier objc-superclass[opt]
1322/// objc-class-instance-variables[opt]
1323///
1324/// objc-category-implementation-prologue:
1325/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001326Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001327 SourceLocation atLoc) {
1328 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1329 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1330 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001332 // Code completion after '@implementation'.
1333 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001334 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001335 cutOffParsing();
1336 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001337 }
1338
Chris Lattnerdf195262007-10-09 17:51:17 +00001339 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001340 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001341 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001342 }
1343 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001344 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001345 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001346
1347 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001348 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001349 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001350 SourceLocation categoryLoc, rparenLoc;
1351 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001353 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001354 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001355 cutOffParsing();
1356 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001357 }
1358
Chris Lattnerdf195262007-10-09 17:51:17 +00001359 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001360 categoryId = Tok.getIdentifierInfo();
1361 categoryLoc = ConsumeToken();
1362 } else {
1363 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001364 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001365 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001366 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001367 Diag(Tok, diag::err_expected_rparen);
1368 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001369 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001370 }
1371 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001372 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001373 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001374 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001375
1376 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001377 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001378 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001379 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001380 }
1381 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001382 SourceLocation superClassLoc;
1383 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001384 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001385 // We have a super class
1386 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001387 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001388 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001389 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001390 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001391 superClassId = Tok.getIdentifierInfo();
1392 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001393 }
John McCalld226f652010-08-21 09:40:31 +00001394 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001395 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001396 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Steve Naroff60fccee2007-10-29 21:38:07 +00001398 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001399 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1400
1401 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001402 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001403 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001404 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001405}
Steve Naroff60fccee2007-10-29 21:38:07 +00001406
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001407Parser::DeclGroupPtrTy
1408Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001409 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1410 "ParseObjCAtEndDeclaration(): Expected @end");
1411 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001412 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001413 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001414 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1415 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1416 DeclsInGroup.push_back(D);
1417 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001418 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001419
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001420 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001421 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001422 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001423 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001424 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001425 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001426 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001427
1428 LateParsedObjCMethods.clear();
1429 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001430 return Actions.BuildDeclaratorGroup(
1431 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001432}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001433
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001434Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1435 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001436 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001437 return Actions.ConvertDeclToDeclGroup(0);
1438 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001439 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001440 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1441}
1442
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001443/// compatibility-alias-decl:
1444/// @compatibility_alias alias-name class-name ';'
1445///
John McCalld226f652010-08-21 09:40:31 +00001446Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001447 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1448 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1449 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001450 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001451 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001452 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001453 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001454 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1455 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001456 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001457 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001458 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001459 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001460 IdentifierInfo *classId = Tok.getIdentifierInfo();
1461 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001462 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1463 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001464 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1465 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001466}
1467
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001468/// property-synthesis:
1469/// @synthesize property-ivar-list ';'
1470///
1471/// property-ivar-list:
1472/// property-ivar
1473/// property-ivar-list ',' property-ivar
1474///
1475/// property-ivar:
1476/// identifier
1477/// identifier '=' identifier
1478///
John McCalld226f652010-08-21 09:40:31 +00001479Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001480 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1481 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001482 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregorb328c422009-11-18 19:45:45 +00001484 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001485 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001486 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001487 cutOffParsing();
1488 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001489 }
1490
Douglas Gregorb328c422009-11-18 19:45:45 +00001491 if (Tok.isNot(tok::identifier)) {
1492 Diag(Tok, diag::err_synthesized_property_name);
1493 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001494 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001495 }
1496
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001497 IdentifierInfo *propertyIvar = 0;
1498 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1499 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001500 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001501 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001502 // property '=' ivar-name
1503 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001504
1505 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001506 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001507 cutOffParsing();
1508 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001509 }
1510
Chris Lattnerdf195262007-10-09 17:51:17 +00001511 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001512 Diag(Tok, diag::err_expected_ident);
1513 break;
1514 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001515 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001516 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001517 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001518 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001519 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001520 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001521 break;
1522 ConsumeToken(); // consume ','
1523 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001524 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001525 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001526}
1527
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001528/// property-dynamic:
1529/// @dynamic property-list
1530///
1531/// property-list:
1532/// identifier
1533/// property-list ',' identifier
1534///
John McCalld226f652010-08-21 09:40:31 +00001535Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001536 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1537 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001538 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001539 while (true) {
1540 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001541 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001542 cutOffParsing();
1543 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001544 }
1545
1546 if (Tok.isNot(tok::identifier)) {
1547 Diag(Tok, diag::err_expected_ident);
1548 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001549 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001550 }
1551
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001552 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1553 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001554 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001555 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001556
Chris Lattnerdf195262007-10-09 17:51:17 +00001557 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001558 break;
1559 ConsumeToken(); // consume ','
1560 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001561 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001562 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001563}
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001565/// objc-throw-statement:
1566/// throw expression[opt];
1567///
John McCall60d7b3a2010-08-24 06:29:42 +00001568StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1569 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001570 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001571 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001572 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001573 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001574 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001575 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001576 }
1577 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001578 // consume ';'
1579 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001580 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001581}
1582
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001583/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001584/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001585///
John McCall60d7b3a2010-08-24 06:29:42 +00001586StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001587Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001588 ConsumeToken(); // consume synchronized
1589 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001590 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001591 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001592 }
John McCall07524032011-07-27 21:50:02 +00001593
1594 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001595 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001596 ExprResult operand(ParseExpression());
1597
1598 if (Tok.is(tok::r_paren)) {
1599 ConsumeParen(); // ')'
1600 } else {
1601 if (!operand.isInvalid())
1602 Diag(Tok, diag::err_expected_rparen);
1603
1604 // Skip forward until we see a left brace, but don't consume it.
1605 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001606 }
John McCall07524032011-07-27 21:50:02 +00001607
1608 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001609 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001610 if (!operand.isInvalid())
1611 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001612 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001613 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001614
John McCall07524032011-07-27 21:50:02 +00001615 // Check the @synchronized operand now.
1616 if (!operand.isInvalid())
1617 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001618
John McCall07524032011-07-27 21:50:02 +00001619 // Parse the compound statement within a new scope.
1620 ParseScope bodyScope(this, Scope::DeclScope);
1621 StmtResult body(ParseCompoundStatementBody());
1622 bodyScope.Exit();
1623
1624 // If there was a semantic or parse error earlier with the
1625 // operand, fail now.
1626 if (operand.isInvalid())
1627 return StmtError();
1628
1629 if (body.isInvalid())
1630 body = Actions.ActOnNullStmt(Tok.getLocation());
1631
1632 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001633}
1634
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001635/// objc-try-catch-statement:
1636/// @try compound-statement objc-catch-list[opt]
1637/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1638///
1639/// objc-catch-list:
1640/// @catch ( parameter-declaration ) compound-statement
1641/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1642/// catch-parameter-declaration:
1643/// parameter-declaration
1644/// '...' [OBJC2]
1645///
John McCall60d7b3a2010-08-24 06:29:42 +00001646StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001647 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001648
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001649 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001650 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001651 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001652 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001653 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001654 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001655 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001656 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001657 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001658 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001659 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001660 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001661
Chris Lattnerdf195262007-10-09 17:51:17 +00001662 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001663 // At this point, we need to lookahead to determine if this @ is the start
1664 // of an @catch or @finally. We don't want to consume the @ token if this
1665 // is an @try or @encode or something else.
1666 Token AfterAt = GetLookAheadToken(1);
1667 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1668 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1669 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001670
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001671 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001672 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001673 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001674 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001675 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001676 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001677 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001678 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001679 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001680 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001681 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001682 ParseDeclarator(ParmDecl);
1683
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001684 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001685 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001686 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001687 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001688 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001689
Steve Naroff93a25952009-04-07 22:56:58 +00001690 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Steve Naroff93a25952009-04-07 22:56:58 +00001692 if (Tok.is(tok::r_paren))
1693 RParenLoc = ConsumeParen();
1694 else // Skip over garbage, until we get to ')'. Eat the ')'.
1695 SkipUntil(tok::r_paren, true, false);
1696
John McCall60d7b3a2010-08-24 06:29:42 +00001697 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001698 if (Tok.is(tok::l_brace))
1699 CatchBody = ParseCompoundStatementBody();
1700 else
1701 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001702 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001703 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001704
John McCall60d7b3a2010-08-24 06:29:42 +00001705 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001706 RParenLoc,
1707 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001708 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001709 if (!Catch.isInvalid())
1710 CatchStmts.push_back(Catch.release());
1711
Steve Naroff64515f32008-02-05 21:27:35 +00001712 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001713 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1714 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001715 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001716 }
1717 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001718 } else {
1719 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001720 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001721 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001722
John McCall60d7b3a2010-08-24 06:29:42 +00001723 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001724 if (Tok.is(tok::l_brace))
1725 FinallyBody = ParseCompoundStatementBody();
1726 else
1727 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001728 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001729 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001730 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001731 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001732 catch_or_finally_seen = true;
1733 break;
1734 }
1735 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001736 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001737 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001738 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001739 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001740
John McCall9ae2f072010-08-23 23:25:46 +00001741 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001742 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001743 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001744}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001745
John McCallf85e1932011-06-15 23:02:42 +00001746/// objc-autoreleasepool-statement:
1747/// @autoreleasepool compound-statement
1748///
1749StmtResult
1750Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1751 ConsumeToken(); // consume autoreleasepool
1752 if (Tok.isNot(tok::l_brace)) {
1753 Diag(Tok, diag::err_expected_lbrace);
1754 return StmtError();
1755 }
1756 // Enter a scope to hold everything within the compound stmt. Compound
1757 // statements can always hold declarations.
1758 ParseScope BodyScope(this, Scope::DeclScope);
1759
1760 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1761
1762 BodyScope.Exit();
1763 if (AutoreleasePoolBody.isInvalid())
1764 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1765 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1766 AutoreleasePoolBody.take());
1767}
1768
Steve Naroff3536b442007-09-06 21:24:23 +00001769/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001770///
John McCalld226f652010-08-21 09:40:31 +00001771Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001772 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001773
John McCallf312b1e2010-08-26 23:41:50 +00001774 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1775 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001777 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001778 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001779 if (ObjCImpDecl) {
1780 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001781 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001782 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001783 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001784 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001785
Steve Naroff409be832007-11-11 19:54:21 +00001786 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001787 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001788 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Steve Naroff409be832007-11-11 19:54:21 +00001790 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1791 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Steve Naroff409be832007-11-11 19:54:21 +00001793 // If we didn't find the '{', bail out.
1794 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001795 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001796 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001797 // Allow the rest of sema to find private method decl implementations.
1798 if (MDecl)
1799 Actions.AddAnyMethodToGlobalPool(MDecl);
1800
1801 // Consume the tokens and store them for later parsing.
1802 LexedMethod* LM = new LexedMethod(this, MDecl);
1803 LateParsedObjCMethods.push_back(LM);
1804 CachedTokens &Toks = LM->Toks;
1805 // Begin by storing the '{' token.
1806 Toks.push_back(Tok);
1807 ConsumeBrace();
1808 // Consume everything up to (and including) the matching right brace.
1809 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001810 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001811}
Anders Carlsson55085182007-08-21 17:43:55 +00001812
John McCall60d7b3a2010-08-24 06:29:42 +00001813StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001814 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001815 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001816 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001817 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001818 }
1819
1820 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001821 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001822
1823 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001824 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001825
1826 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001827 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001828
1829 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1830 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001831
John McCall60d7b3a2010-08-24 06:29:42 +00001832 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001833 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001834 // If the expression is invalid, skip ahead to the next semicolon. Not
1835 // doing this opens us up to the possibility of infinite loops if
1836 // ParseExpression does not consume any tokens.
1837 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001838 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001839 }
Chris Lattner5d803162009-12-07 16:33:19 +00001840
Steve Naroff64515f32008-02-05 21:27:35 +00001841 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001842 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001843 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001844}
1845
John McCall60d7b3a2010-08-24 06:29:42 +00001846ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001847 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001848 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001849 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001850 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001851 return ExprError();
1852
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001853 case tok::string_literal: // primary-expression: string-literal
1854 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001855 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001856 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001857 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001858 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001859
Chris Lattner4fef81d2008-08-05 06:19:09 +00001860 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1861 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001862 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001863 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001864 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001865 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001866 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001867 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001868 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001869 }
Anders Carlsson55085182007-08-21 17:43:55 +00001870 }
Anders Carlsson55085182007-08-21 17:43:55 +00001871}
1872
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001873/// \brirg Parse the receiver of an Objective-C++ message send.
1874///
1875/// This routine parses the receiver of a message send in
1876/// Objective-C++ either as a type or as an expression. Note that this
1877/// routine must not be called to parse a send to 'super', since it
1878/// has no way to return such a result.
1879///
1880/// \param IsExpr Whether the receiver was parsed as an expression.
1881///
1882/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1883/// IsExpr is true), the parsed expression. If the receiver was parsed
1884/// as a type (\c IsExpr is false), the parsed type.
1885///
1886/// \returns True if an error occurred during parsing or semantic
1887/// analysis, in which case the arguments do not have valid
1888/// values. Otherwise, returns false for a successful parse.
1889///
1890/// objc-receiver: [C++]
1891/// 'super' [not parsed here]
1892/// expression
1893/// simple-type-specifier
1894/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001895bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001896 InMessageExpressionRAIIObject InMessage(*this, true);
1897
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001898 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1899 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1900 TryAnnotateTypeOrScopeToken();
1901
1902 if (!isCXXSimpleTypeSpecifier()) {
1903 // objc-receiver:
1904 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001905 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001906 if (Receiver.isInvalid())
1907 return true;
1908
1909 IsExpr = true;
1910 TypeOrExpr = Receiver.take();
1911 return false;
1912 }
1913
1914 // objc-receiver:
1915 // typename-specifier
1916 // simple-type-specifier
1917 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001918 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001919 ParseCXXSimpleTypeSpecifier(DS);
1920
1921 if (Tok.is(tok::l_paren)) {
1922 // If we see an opening parentheses at this point, we are
1923 // actually parsing an expression that starts with a
1924 // function-style cast, e.g.,
1925 //
1926 // postfix-expression:
1927 // simple-type-specifier ( expression-list [opt] )
1928 // typename-specifier ( expression-list [opt] )
1929 //
1930 // Parse the remainder of this case, then the (optional)
1931 // postfix-expression suffix, followed by the (optional)
1932 // right-hand side of the binary expression. We have an
1933 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001934 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001935 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001936 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001937 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001938 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001939 if (Receiver.isInvalid())
1940 return true;
1941
1942 IsExpr = true;
1943 TypeOrExpr = Receiver.take();
1944 return false;
1945 }
1946
1947 // We have a class message. Turn the simple-type-specifier or
1948 // typename-specifier we parsed into a type and parse the
1949 // remainder of the class message.
1950 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001951 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001952 if (Type.isInvalid())
1953 return true;
1954
1955 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001956 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001957 return false;
1958}
1959
Douglas Gregor1b730e82010-05-31 14:40:22 +00001960/// \brief Determine whether the parser is currently referring to a an
1961/// Objective-C message send, using a simplified heuristic to avoid overhead.
1962///
1963/// This routine will only return true for a subset of valid message-send
1964/// expressions.
1965bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001966 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001967 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001968 return GetLookAheadToken(1).is(tok::identifier) &&
1969 GetLookAheadToken(2).is(tok::identifier);
1970}
1971
Douglas Gregor9497a732010-09-16 01:51:54 +00001972bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1973 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1974 InMessageExpression)
1975 return false;
1976
1977
1978 ParsedType Type;
1979
1980 if (Tok.is(tok::annot_typename))
1981 Type = getTypeAnnotation(Tok);
1982 else if (Tok.is(tok::identifier))
1983 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1984 getCurScope());
1985 else
1986 return false;
1987
1988 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1989 const Token &AfterNext = GetLookAheadToken(2);
1990 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1991 if (Tok.is(tok::identifier))
1992 TryAnnotateTypeOrScopeToken();
1993
1994 return Tok.is(tok::annot_typename);
1995 }
1996 }
1997
1998 return false;
1999}
2000
Mike Stump1eb44332009-09-09 15:08:12 +00002001/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002002/// '[' objc-receiver objc-message-args ']'
2003///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002004/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002005/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002006/// expression
2007/// class-name
2008/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002009///
John McCall60d7b3a2010-08-24 06:29:42 +00002010ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002011 assert(Tok.is(tok::l_square) && "'[' expected");
2012 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2013
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002014 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002015 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002016 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002017 return ExprError();
2018 }
2019
Douglas Gregor0fbda682010-09-15 14:51:05 +00002020 InMessageExpressionRAIIObject InMessage(*this, true);
2021
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002022 if (getLang().CPlusPlus) {
2023 // We completely separate the C and C++ cases because C++ requires
2024 // more complicated (read: slower) parsing.
2025
2026 // Handle send to super.
2027 // FIXME: This doesn't benefit from the same typo-correction we
2028 // get in Objective-C.
2029 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002030 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002031 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2032 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002033
2034 // Parse the receiver, which is either a type or an expression.
2035 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002036 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002037 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2038 SkipUntil(tok::r_square);
2039 return ExprError();
2040 }
2041
2042 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002043 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2044 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002045 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002046
2047 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002048 ParsedType::getFromOpaquePtr(TypeOrExpr),
2049 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002050 }
2051
2052 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002053 IdentifierInfo *Name = Tok.getIdentifierInfo();
2054 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002055 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002056 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002057 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002058 NextToken().is(tok::period),
2059 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002060 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002061 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2062 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002063
John McCallf312b1e2010-08-26 23:41:50 +00002064 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002065 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002066 SkipUntil(tok::r_square);
2067 return ExprError();
2068 }
2069
Douglas Gregor1569f952010-04-21 20:38:13 +00002070 ConsumeToken(); // the type name
2071
2072 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002073 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002074
John McCallf312b1e2010-08-26 23:41:50 +00002075 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002076 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002077 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002078 }
Chris Lattner699b6612008-01-25 18:59:06 +00002079 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002080
2081 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002082 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002083 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002084 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002085 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002086 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002087
John McCallb3d87482010-08-24 05:47:05 +00002088 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2089 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002090}
Sebastian Redl1d922962008-12-13 15:32:12 +00002091
Douglas Gregor2725ca82010-04-21 19:57:20 +00002092/// \brief Parse the remainder of an Objective-C message following the
2093/// '[' objc-receiver.
2094///
2095/// This routine handles sends to super, class messages (sent to a
2096/// class name), and instance messages (sent to an object), and the
2097/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2098/// ReceiverExpr, respectively. Only one of these parameters may have
2099/// a valid value.
2100///
2101/// \param LBracLoc The location of the opening '['.
2102///
2103/// \param SuperLoc If this is a send to 'super', the location of the
2104/// 'super' keyword that indicates a send to the superclass.
2105///
2106/// \param ReceiverType If this is a class message, the type of the
2107/// class we are sending a message to.
2108///
2109/// \param ReceiverExpr If this is an instance message, the expression
2110/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002111///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002112/// objc-message-args:
2113/// objc-selector
2114/// objc-keywordarg-list
2115///
2116/// objc-keywordarg-list:
2117/// objc-keywordarg
2118/// objc-keywordarg-list objc-keywordarg
2119///
Mike Stump1eb44332009-09-09 15:08:12 +00002120/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002121/// selector-name[opt] ':' objc-keywordexpr
2122///
2123/// objc-keywordexpr:
2124/// nonempty-expr-list
2125///
2126/// nonempty-expr-list:
2127/// assignment-expression
2128/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002129///
John McCall60d7b3a2010-08-24 06:29:42 +00002130ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002131Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002132 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002133 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002134 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002135 InMessageExpressionRAIIObject InMessage(*this, true);
2136
Steve Naroffc4df6d22009-11-07 02:08:14 +00002137 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002138 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002139 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2140 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002141 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002142 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2143 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002144 else
John McCall9ae2f072010-08-23 23:25:46 +00002145 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002146 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002147 cutOffParsing();
2148 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002149 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002150
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002151 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002152 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002153 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002154
Anders Carlssonff975cf2009-02-14 18:21:46 +00002155 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Chris Lattner5f9e2722011-07-23 10:55:15 +00002157 SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002158 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002159
Chris Lattnerdf195262007-10-09 17:51:17 +00002160 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002161 while (1) {
2162 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002163 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002164
Chris Lattnerdf195262007-10-09 17:51:17 +00002165 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002166 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002167 // We must manually skip to a ']', otherwise the expression skipper will
2168 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2169 // the enclosing expression.
2170 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002171 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002172 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002173
Steve Naroff68d331a2007-09-27 14:38:14 +00002174 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002175 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002176
2177 if (Tok.is(tok::code_completion)) {
2178 if (SuperLoc.isValid())
2179 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2180 KeyIdents.data(),
2181 KeyIdents.size(),
2182 /*AtArgumentEpression=*/true);
2183 else if (ReceiverType)
2184 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2185 KeyIdents.data(),
2186 KeyIdents.size(),
2187 /*AtArgumentEpression=*/true);
2188 else
2189 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2190 KeyIdents.data(),
2191 KeyIdents.size(),
2192 /*AtArgumentEpression=*/true);
2193
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002194 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002195 return ExprError();
2196 }
2197
John McCall60d7b3a2010-08-24 06:29:42 +00002198 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002199 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002200 // We must manually skip to a ']', otherwise the expression skipper will
2201 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2202 // the enclosing expression.
2203 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002204 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002205 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002206
Steve Naroff37387c92007-09-17 20:25:27 +00002207 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002208 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002209
Douglas Gregord3c68542009-11-19 01:08:35 +00002210 // Code completion after each argument.
2211 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002212 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002213 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002214 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002215 KeyIdents.size(),
2216 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002217 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002218 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002219 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002220 KeyIdents.size(),
2221 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002222 else
John McCall9ae2f072010-08-23 23:25:46 +00002223 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002224 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002225 KeyIdents.size(),
2226 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002227 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002228 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002229 }
2230
Steve Naroff37387c92007-09-17 20:25:27 +00002231 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002232 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002233 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002234 break;
2235 // We have a selector or a colon, continue parsing.
2236 }
2237 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002238 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002239 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002240 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002241 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002242 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002243 // We must manually skip to a ']', otherwise the expression skipper will
2244 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2245 // the enclosing expression.
2246 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002247 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002248 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002249
Steve Naroff49f109c2007-11-15 13:05:42 +00002250 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002251 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002252 }
2253 } else if (!selIdent) {
2254 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002255
Chris Lattner4fef81d2008-08-05 06:19:09 +00002256 // We must manually skip to a ']', otherwise the expression skipper will
2257 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2258 // the enclosing expression.
2259 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002260 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002261 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002262
Chris Lattnerdf195262007-10-09 17:51:17 +00002263 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002264 if (Tok.is(tok::identifier))
2265 Diag(Tok, diag::err_expected_colon);
2266 else
2267 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002268 // We must manually skip to a ']', otherwise the expression skipper will
2269 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2270 // the enclosing expression.
2271 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002272 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002273 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002274
Chris Lattner699b6612008-01-25 18:59:06 +00002275 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002276
Steve Naroff29238a02007-10-05 18:42:47 +00002277 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002278 if (nKeys == 0)
2279 KeyIdents.push_back(selIdent);
2280 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002281
Douglas Gregor2725ca82010-04-21 19:57:20 +00002282 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002283 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002284 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002285 MultiExprArg(Actions,
2286 KeyExprs.take(),
2287 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002288 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002289 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002290 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002291 MultiExprArg(Actions,
2292 KeyExprs.take(),
2293 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002294 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002295 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002296 MultiExprArg(Actions,
2297 KeyExprs.take(),
2298 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002299}
2300
John McCall60d7b3a2010-08-24 06:29:42 +00002301ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2302 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002303 if (Res.isInvalid()) return move(Res);
2304
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002305 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2306 // expressions. At this point, we know that the only valid thing that starts
2307 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002308 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002309 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002310 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002311 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002312
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002313 while (Tok.is(tok::at)) {
2314 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002315
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002316 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002317 if (!isTokenStringLiteral())
2318 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002319
John McCall60d7b3a2010-08-24 06:29:42 +00002320 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002321 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002322 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002323
Sebastian Redleffa8d12008-12-10 00:02:53 +00002324 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002325 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002326
2327 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2328 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002329}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002330
2331/// objc-encode-expression:
2332/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002333ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002334Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002335 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002336
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002337 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002338
Chris Lattner4fef81d2008-08-05 06:19:09 +00002339 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002340 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2341
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002342 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002343
Douglas Gregor809070a2009-02-18 17:45:20 +00002344 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002345
Anders Carlsson4988ae32007-08-23 15:31:37 +00002346 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002347
Douglas Gregor809070a2009-02-18 17:45:20 +00002348 if (Ty.isInvalid())
2349 return ExprError();
2350
Mike Stump1eb44332009-09-09 15:08:12 +00002351 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002352 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002353}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002354
2355/// objc-protocol-expression
2356/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002357ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002358Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002359 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002360
Chris Lattner4fef81d2008-08-05 06:19:09 +00002361 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002362 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2363
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002364 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002365
Chris Lattner4fef81d2008-08-05 06:19:09 +00002366 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002367 return ExprError(Diag(Tok, diag::err_expected_ident));
2368
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002369 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002370 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002371
Anders Carlsson4988ae32007-08-23 15:31:37 +00002372 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002373
Sebastian Redl1d922962008-12-13 15:32:12 +00002374 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2375 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002376}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002377
2378/// objc-selector-expression
2379/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002380ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002381 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002382
Chris Lattner4fef81d2008-08-05 06:19:09 +00002383 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002384 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2385
Chris Lattner5f9e2722011-07-23 10:55:15 +00002386 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002387 SourceLocation LParenLoc = ConsumeParen();
2388 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002389
2390 if (Tok.is(tok::code_completion)) {
2391 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2392 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002393 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002394 return ExprError();
2395 }
2396
Chris Lattner2fc5c242009-04-11 18:13:45 +00002397 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002398 if (!SelIdent && // missing selector name.
2399 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002400 return ExprError(Diag(Tok, diag::err_expected_ident));
2401
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002402 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002403 unsigned nColons = 0;
2404 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002405 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002406 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2407 ++nColons;
2408 KeyIdents.push_back(0);
2409 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002410 return ExprError(Diag(Tok, diag::err_expected_colon));
2411
Chris Lattner5add7542010-08-27 22:32:41 +00002412 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002413 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002414 if (Tok.is(tok::r_paren))
2415 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002416
2417 if (Tok.is(tok::code_completion)) {
2418 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2419 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002420 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002421 return ExprError();
2422 }
2423
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002424 // Check for another keyword selector.
2425 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002426 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002427 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002428 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002429 break;
2430 }
Steve Naroff887407e2007-12-05 22:21:29 +00002431 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002432 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002433 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002434 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2435 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002436 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002437
2438Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2439
2440 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2441 // Append the current token at the end of the new token stream so that it
2442 // doesn't get lost.
2443 LM.Toks.push_back(Tok);
2444 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2445
2446 // MDecl might be null due to error in method prototype, etc.
2447 Decl *MDecl = LM.D;
2448 // Consume the previously pushed token.
2449 ConsumeAnyToken();
2450
2451 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2452 SourceLocation BraceLoc = Tok.getLocation();
2453 // Enter a scope for the method body.
2454 ParseScope BodyScope(this,
2455 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2456
2457 // Tell the actions module that we have entered a method definition with the
2458 // specified Declarator for the method.
2459 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2460
2461 if (PP.isCodeCompletionEnabled()) {
2462 if (trySkippingFunctionBodyForCodeCompletion()) {
2463 BodyScope.Exit();
2464 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2465 }
2466 }
2467
2468 StmtResult FnBody(ParseCompoundStatementBody());
2469
2470 // If the function body could not be parsed, make a bogus compoundstmt.
2471 if (FnBody.isInvalid())
2472 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2473 MultiStmtArg(Actions), false);
2474
2475 // Leave the function body scope.
2476 BodyScope.Exit();
2477
2478 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2479}