blob: c8b2a09d5ea02e90dfe33ea8b7d78d5935f8f30b [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'
John McCalld226f652010-08-21 09:40:31 +000032Decl *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)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregordc845342010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +000038 }
39
Steve Naroff861cf3e2007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000041 case tok::objc_class:
42 return ParseObjCAtClassDeclaration(AtLoc);
John McCall7f040a92010-12-24 02:08:15 +000043 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000044 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000045 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
46 }
47 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000048 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000049 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
50 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000051 case tok::objc_implementation:
52 return ParseObjCAtImplementationDeclaration(AtLoc);
53 case tok::objc_end:
54 return ParseObjCAtEndDeclaration(AtLoc);
55 case tok::objc_compatibility_alias:
56 return ParseObjCAtAliasDeclaration(AtLoc);
57 case tok::objc_synthesize:
58 return ParseObjCPropertySynthesize(AtLoc);
59 case tok::objc_dynamic:
60 return ParseObjCPropertyDynamic(AtLoc);
61 default:
62 Diag(AtLoc, diag::err_unexpected_at);
63 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000064 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 }
66}
67
68///
Mike Stump1eb44332009-09-09 15:08:12 +000069/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000070/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000071///
John McCalld226f652010-08-21 09:40:31 +000072Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000073 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000074 SmallVector<IdentifierInfo *, 8> ClassNames;
75 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000076
Mike Stump1eb44332009-09-09 15:08:12 +000077
Reid Spencer5f016e22007-07-11 17:01:13 +000078 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000079 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000080 Diag(Tok, diag::err_expected_ident);
81 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000082 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000083 }
Reid Spencer5f016e22007-07-11 17:01:13 +000084 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000085 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000086 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattnerdf195262007-10-09 17:51:17 +000088 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000089 break;
Mike Stump1eb44332009-09-09 15:08:12 +000090
Reid Spencer5f016e22007-07-11 17:01:13 +000091 ConsumeToken();
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 // Consume the ';'.
95 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCalld226f652010-08-21 09:40:31 +000096 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenekc09cba62009-11-17 23:12:20 +000098 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
99 ClassLocs.data(),
100 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000101}
102
Steve Naroffdac269b2007-08-20 21:31:48 +0000103///
104/// objc-interface:
105/// objc-class-interface-attributes[opt] objc-class-interface
106/// objc-category-interface
107///
108/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000109/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000110/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000111/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000112/// objc-interface-decl-list
113/// @end
114///
115/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000116/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000117/// objc-protocol-refs[opt]
118/// objc-interface-decl-list
119/// @end
120///
121/// objc-superclass:
122/// ':' identifier
123///
124/// objc-class-interface-attributes:
125/// __attribute__((visibility("default")))
126/// __attribute__((visibility("hidden")))
127/// __attribute__((deprecated))
128/// __attribute__((unavailable))
129/// __attribute__((objc_exception)) - used by NSException on 64-bit
130///
John McCall7f040a92010-12-24 02:08:15 +0000131Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
132 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000133 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000134 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
135 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000137 // Code completion after '@interface'.
138 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000139 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000140 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000141 }
142
Chris Lattnerdf195262007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000145 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000146 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000147
Steve Naroffdac269b2007-08-20 21:31:48 +0000148 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000149 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000150 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000151 if (Tok.is(tok::l_paren) &&
152 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000153 // TODO(dgregor): Use the return value from the next line to provide better
154 // recovery.
155 ConsumeParen();
Steve Naroffdac269b2007-08-20 21:31:48 +0000156 SourceLocation categoryLoc, rparenLoc;
157 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000158 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000159 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000160 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000161 }
162
Steve Naroff527fe232007-08-23 19:56:30 +0000163 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000164 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000165 categoryId = Tok.getIdentifierInfo();
166 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000167 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000168 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000169 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000170 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000172 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000173 Diag(Tok, diag::err_expected_rparen);
174 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +0000175 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 }
177 rparenLoc = ConsumeParen();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000178 // Next, we need to check for any protocol references.
179 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180 SmallVector<Decl *, 8> ProtocolRefs;
181 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000182 if (Tok.is(tok::less) &&
183 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000184 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000185 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
John McCall7f040a92010-12-24 02:08:15 +0000187 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000188 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
John McCalld226f652010-08-21 09:40:31 +0000190 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000191 Actions.ActOnStartCategoryInterface(atLoc,
192 nameId, nameLoc,
193 categoryId, categoryLoc,
194 ProtocolRefs.data(),
195 ProtocolRefs.size(),
196 ProtocolLocs.data(),
197 EndProtoLoc);
198 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000199 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
200 atLoc);
201
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000202 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
203 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 }
205 // Parse a class interface.
206 IdentifierInfo *superClassId = 0;
207 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000208
Chris Lattnerdf195262007-10-09 17:51:17 +0000209 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000210 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000211
212 // Code completion of superclass names.
213 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000214 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000215 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000216 }
217
Chris Lattnerdf195262007-10-09 17:51:17 +0000218 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000219 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000220 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000221 }
222 superClassId = Tok.getIdentifierInfo();
223 superClassLoc = ConsumeToken();
224 }
225 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226 SmallVector<Decl *, 8> ProtocolRefs;
227 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000228 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000229 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000230 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
231 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000232 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
John McCalld226f652010-08-21 09:40:31 +0000234 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000235 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000236 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000237 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000238 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000239 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattnerdf195262007-10-09 17:51:17 +0000241 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000242 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000243
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000244 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000245 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000246}
247
John McCalld0014542009-12-03 22:31:13 +0000248/// The Objective-C property callback. This should be defined where
249/// it's used, but instead it's been lifted to here to support VS2005.
250struct Parser::ObjCPropertyCallback : FieldCallback {
251 Parser &P;
John McCalld226f652010-08-21 09:40:31 +0000252 Decl *IDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000254 ObjCDeclSpec &OCDS;
255 SourceLocation AtLoc;
256 tok::ObjCKeywordKind MethodImplKind;
257
John McCalld226f652010-08-21 09:40:31 +0000258 ObjCPropertyCallback(Parser &P, Decl *IDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000259 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000260 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
261 tok::ObjCKeywordKind MethodImplKind) :
262 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
263 MethodImplKind(MethodImplKind) {
264 }
265
John McCalld226f652010-08-21 09:40:31 +0000266 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000267 if (FD.D.getIdentifier() == 0) {
268 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
269 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000270 return 0;
John McCalld0014542009-12-03 22:31:13 +0000271 }
272 if (FD.BitfieldSize) {
273 P.Diag(AtLoc, diag::err_objc_property_bitfield)
274 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000275 return 0;
John McCalld0014542009-12-03 22:31:13 +0000276 }
277
278 // Install the property declarator into interfaceDecl.
279 IdentifierInfo *SelName =
280 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
281
282 Selector GetterSel =
283 P.PP.getSelectorTable().getNullarySelector(SelName);
284 IdentifierInfo *SetterName = OCDS.getSetterName();
285 Selector SetterSel;
286 if (SetterName)
287 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
288 else
289 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
290 P.PP.getSelectorTable(),
291 FD.D.getIdentifier());
292 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000293 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000294 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCalld0014542009-12-03 22:31:13 +0000295 GetterSel, SetterSel, IDecl,
296 &isOverridingProperty,
297 MethodImplKind);
298 if (!isOverridingProperty)
299 Props.push_back(Property);
300
301 return Property;
302 }
303};
304
Steve Naroffdac269b2007-08-20 21:31:48 +0000305/// objc-interface-decl-list:
306/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000307/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000308/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000309/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000310/// objc-interface-decl-list declaration
311/// objc-interface-decl-list ';'
312///
Steve Naroff294494e2007-08-22 16:35:03 +0000313/// objc-method-requirement: [OBJC2]
314/// @required
315/// @optional
316///
John McCalld226f652010-08-21 09:40:31 +0000317void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000318 tok::ObjCKeywordKind contextKey) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000319 SmallVector<Decl *, 32> allMethods;
320 SmallVector<Decl *, 16> allProperties;
321 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000322 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenek782f2f52010-01-07 01:20:12 +0000324 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000325
Steve Naroff294494e2007-08-22 16:35:03 +0000326 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000327 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000328 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000329 Decl *methodPrototype =
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000330 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000331 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000332 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
333 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000334 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
335 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000336 continue;
337 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000338 if (Tok.is(tok::l_paren)) {
339 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000340 ParseObjCMethodDecl(Tok.getLocation(),
341 tok::minus,
342 interfaceDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000343 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000344 continue;
345 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000346 // Ignore excess semicolons.
347 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000348 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000349 continue;
350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattnerbc662af2008-10-20 06:10:06 +0000352 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000353 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000354 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000356 // Code completion within an Objective-C interface.
357 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000358 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000359 ObjCImpDecl? Sema::PCC_ObjCImplementation
360 : Sema::PCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000361 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000362 }
363
Chris Lattnere82a10f2008-10-20 05:46:22 +0000364 // If we don't have an @ directive, parse it as a function definition.
365 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000366 // The code below does not consume '}'s because it is afraid of eating the
367 // end of a namespace. Because of the way this code is structured, an
368 // erroneous r_brace would cause an infinite loop if not handled here.
369 if (Tok.is(tok::r_brace))
370 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Steve Naroff4985ace2007-08-22 18:35:33 +0000372 // FIXME: as the name implies, this rule allows function definitions.
373 // We could pass a flag or check for functions during semantic analysis.
John McCall0b7e6782011-03-24 11:26:52 +0000374 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000375 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000376 continue;
377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattnere82a10f2008-10-20 05:46:22 +0000379 // Otherwise, we have an @ directive, eat the @.
380 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000381 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000382 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000383 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000384 break;
385 }
386
Chris Lattnera2449b22008-10-20 05:57:40 +0000387 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattnera2449b22008-10-20 05:57:40 +0000389 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000390 AtEnd.setBegin(AtLoc);
391 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000392 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000393 } else if (DirectiveKind == tok::objc_not_keyword) {
394 Diag(Tok, diag::err_objc_unknown_at);
395 SkipUntil(tok::semi);
396 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnerbc662af2008-10-20 06:10:06 +0000399 // Eat the identifier.
400 ConsumeToken();
401
Chris Lattnera2449b22008-10-20 05:57:40 +0000402 switch (DirectiveKind) {
403 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000404 // FIXME: If someone forgets an @end on a protocol, this loop will
405 // continue to eat up tons of stuff and spew lots of nonsense errors. It
406 // would probably be better to bail out if we saw an @class or @interface
407 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000408 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000409 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000410 SkipUntil(tok::r_brace, tok::at);
411 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000412
413 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000414 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000415 Diag(Tok, diag::err_objc_missing_end);
416 ConsumeToken();
417 break;
418
Chris Lattnera2449b22008-10-20 05:57:40 +0000419 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000420 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000421 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000422 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000423 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000424 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000425 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000426 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000427 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattnera2449b22008-10-20 05:57:40 +0000429 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000430 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000431 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000432
Chris Lattnere82a10f2008-10-20 05:46:22 +0000433 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000435 if (Tok.is(tok::l_paren))
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000436 ParseObjCPropertyAttribute(OCDS, interfaceDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
John McCalld0014542009-12-03 22:31:13 +0000438 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
439 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000440
Chris Lattnere82a10f2008-10-20 05:46:22 +0000441 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000442 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000443 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
John McCall7da19ea2011-03-26 01:53:26 +0000445 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000446 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000447 }
Steve Naroff294494e2007-08-22 16:35:03 +0000448 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000449
450 // We break out of the big loop in two cases: when we see @end or when we see
451 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000452 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000453 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000454 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000455 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000456 ConsumeToken(); // the "end" identifier
457 else
458 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattnera2449b22008-10-20 05:57:40 +0000460 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000461 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000462 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000463 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000464 allProperties.data(), allProperties.size(),
465 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000466}
467
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000468/// Parse property attribute declarations.
469///
470/// property-attr-decl: '(' property-attrlist ')'
471/// property-attrlist:
472/// property-attribute
473/// property-attrlist ',' property-attribute
474/// property-attribute:
475/// getter '=' identifier
476/// setter '=' identifier ':'
477/// readonly
478/// readwrite
479/// assign
480/// retain
481/// copy
482/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000483/// atomic
484/// strong
485/// weak
486/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000487///
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000488void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000489 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000490 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000492 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000493 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000494 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000495 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000496 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000497 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000499 // If this is not an identifier at all, bail out early.
500 if (II == 0) {
501 MatchRHSPunctuation(tok::r_paren, LHSLoc);
502 return;
503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattner156b0612008-10-20 07:37:22 +0000505 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattner92e62b02008-11-20 04:42:34 +0000507 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000508 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000509 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000510 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000511 else if (II->isStr("unsafe_unretained"))
512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000513 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000514 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000515 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000516 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000517 else if (II->isStr("strong"))
518 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000519 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000521 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000523 else if (II->isStr("atomic"))
524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000525 else if (II->isStr("weak"))
526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000527 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000528 bool IsSetter = II->getNameStart()[0] == 's';
529
Chris Lattnere00da7c2008-10-20 07:39:53 +0000530 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000531 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
532 diag::err_objc_expected_equal_for_getter;
533
534 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000535 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Douglas Gregor4ad96852009-11-19 07:41:15 +0000537 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000538 if (IsSetter)
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000539 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
Douglas Gregor4ad96852009-11-19 07:41:15 +0000540 else
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000541 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000542 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000543 }
544
Anders Carlsson42499be2010-10-02 17:45:21 +0000545
546 SourceLocation SelLoc;
547 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
548
549 if (!SelIdent) {
550 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
551 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000552 SkipUntil(tok::r_paren);
553 return;
554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Anders Carlsson42499be2010-10-02 17:45:21 +0000556 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000557 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000558 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000560 if (ExpectAndConsume(tok::colon,
561 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000562 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000563 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000564 } else {
565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000566 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000567 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000568 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000569 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000570 SkipUntil(tok::r_paren);
571 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner156b0612008-10-20 07:37:22 +0000574 if (Tok.isNot(tok::comma))
575 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner156b0612008-10-20 07:37:22 +0000577 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattner156b0612008-10-20 07:37:22 +0000580 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000581}
582
Steve Naroff3536b442007-09-06 21:24:23 +0000583/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000584/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000585/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000586///
587/// objc-instance-method: '-'
588/// objc-class-method: '+'
589///
Steve Naroff4985ace2007-08-22 18:35:33 +0000590/// objc-method-attributes: [OBJC2]
591/// __attribute__((deprecated))
592///
John McCalld226f652010-08-21 09:40:31 +0000593Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000594 tok::ObjCKeywordKind MethodImplKind,
595 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000596 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000597
Mike Stump1eb44332009-09-09 15:08:12 +0000598 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000599 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000600 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind,
601 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000602 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000603 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000604 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000605}
606
607/// objc-selector:
608/// identifier
609/// one of
610/// enum struct union if else while do for switch case default
611/// break continue return goto asm sizeof typeof __alignof
612/// unsigned long const short volatile signed restrict _Complex
613/// in out inout bycopy byref oneway int char float double void _Bool
614///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000615IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000616
Chris Lattnerff384912007-10-07 02:00:24 +0000617 switch (Tok.getKind()) {
618 default:
619 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000620 case tok::ampamp:
621 case tok::ampequal:
622 case tok::amp:
623 case tok::pipe:
624 case tok::tilde:
625 case tok::exclaim:
626 case tok::exclaimequal:
627 case tok::pipepipe:
628 case tok::pipeequal:
629 case tok::caret:
630 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000631 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000632 if (isalpha(ThisTok[0])) {
633 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
634 Tok.setKind(tok::identifier);
635 SelectorLoc = ConsumeToken();
636 return II;
637 }
638 return 0;
639 }
640
Chris Lattnerff384912007-10-07 02:00:24 +0000641 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000642 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000643 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000644 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000645 case tok::kw_break:
646 case tok::kw_case:
647 case tok::kw_catch:
648 case tok::kw_char:
649 case tok::kw_class:
650 case tok::kw_const:
651 case tok::kw_const_cast:
652 case tok::kw_continue:
653 case tok::kw_default:
654 case tok::kw_delete:
655 case tok::kw_do:
656 case tok::kw_double:
657 case tok::kw_dynamic_cast:
658 case tok::kw_else:
659 case tok::kw_enum:
660 case tok::kw_explicit:
661 case tok::kw_export:
662 case tok::kw_extern:
663 case tok::kw_false:
664 case tok::kw_float:
665 case tok::kw_for:
666 case tok::kw_friend:
667 case tok::kw_goto:
668 case tok::kw_if:
669 case tok::kw_inline:
670 case tok::kw_int:
671 case tok::kw_long:
672 case tok::kw_mutable:
673 case tok::kw_namespace:
674 case tok::kw_new:
675 case tok::kw_operator:
676 case tok::kw_private:
677 case tok::kw_protected:
678 case tok::kw_public:
679 case tok::kw_register:
680 case tok::kw_reinterpret_cast:
681 case tok::kw_restrict:
682 case tok::kw_return:
683 case tok::kw_short:
684 case tok::kw_signed:
685 case tok::kw_sizeof:
686 case tok::kw_static:
687 case tok::kw_static_cast:
688 case tok::kw_struct:
689 case tok::kw_switch:
690 case tok::kw_template:
691 case tok::kw_this:
692 case tok::kw_throw:
693 case tok::kw_true:
694 case tok::kw_try:
695 case tok::kw_typedef:
696 case tok::kw_typeid:
697 case tok::kw_typename:
698 case tok::kw_typeof:
699 case tok::kw_union:
700 case tok::kw_unsigned:
701 case tok::kw_using:
702 case tok::kw_virtual:
703 case tok::kw_void:
704 case tok::kw_volatile:
705 case tok::kw_wchar_t:
706 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000707 case tok::kw__Bool:
708 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000709 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000710 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000711 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000712 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000713 }
Steve Naroff294494e2007-08-22 16:35:03 +0000714}
715
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000716/// objc-for-collection-in: 'in'
717///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000718bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000719 // FIXME: May have to do additional look-ahead to only allow for
720 // valid tokens following an 'in'; such as an identifier, unary operators,
721 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000722 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000723 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000724}
725
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000727/// qualifier list and builds their bitmask representation in the input
728/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000729///
730/// objc-type-qualifiers:
731/// objc-type-qualifier
732/// objc-type-qualifiers objc-type-qualifier
733///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000734void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
735 ObjCTypeNameContext Context) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000736 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000737 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000738 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
739 Context == OTN_ParameterType);
Douglas Gregord32b0222010-08-24 01:06:58 +0000740 ConsumeCodeCompletionToken();
741 }
742
Chris Lattnercb53b362007-12-27 19:57:00 +0000743 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000744 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattnere8b724d2007-12-12 06:56:32 +0000746 const IdentifierInfo *II = Tok.getIdentifierInfo();
747 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000748 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000749 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000752 switch (i) {
753 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000754 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
755 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
756 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
757 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
758 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
759 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000760 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000762 ConsumeToken();
763 II = 0;
764 break;
765 }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Chris Lattnere8b724d2007-12-12 06:56:32 +0000767 // If this wasn't a recognized qualifier, bail out.
768 if (II) return;
769 }
770}
771
772/// objc-type-name:
773/// '(' objc-type-qualifiers[opt] type-name ')'
774/// '(' objc-type-qualifiers[opt] ')'
775///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000776ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
777 ObjCTypeNameContext Context) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000778 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattner4a76b292008-10-22 03:52:06 +0000780 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000781 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000783 // Parse type qualifiers, in, inout, etc.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000784 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000785
John McCallb3d87482010-08-24 05:47:05 +0000786 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000787 if (isTypeSpecifierQualifier()) {
John McCallf85e1932011-06-15 23:02:42 +0000788 TypeResult TypeSpec =
789 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000790 if (!TypeSpec.isInvalid())
791 Ty = TypeSpec.get();
792 }
John McCallf85e1932011-06-15 23:02:42 +0000793
Steve Naroffd7333c22008-10-21 14:15:04 +0000794 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000795 ConsumeParen();
796 else if (Tok.getLocation() == TypeStartLoc) {
797 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000798 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000799 SkipUntil(tok::r_paren);
800 } else {
801 // Otherwise, we found *something*, but didn't get a ')' in the right
802 // place. Emit an error then return what we have as the type.
803 MatchRHSPunctuation(tok::r_paren, LParenLoc);
804 }
Steve Narofff28b2642007-09-05 23:30:30 +0000805 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000806}
807
808/// objc-method-decl:
809/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000810/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000811/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000812/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000813///
814/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000815/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000816/// objc-keyword-selector objc-keyword-decl
817///
818/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000819/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
820/// objc-selector ':' objc-keyword-attributes[opt] identifier
821/// ':' objc-type-name objc-keyword-attributes[opt] identifier
822/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000823///
Steve Naroff4985ace2007-08-22 18:35:33 +0000824/// objc-parmlist:
825/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000826///
Steve Naroff4985ace2007-08-22 18:35:33 +0000827/// objc-parms:
828/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000829///
Steve Naroff4985ace2007-08-22 18:35:33 +0000830/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000831/// , ...
832///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000833/// objc-keyword-attributes: [OBJC2]
834/// __attribute__((unused))
835///
John McCalld226f652010-08-21 09:40:31 +0000836Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000837 tok::TokenKind mType,
838 Decl *IDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000839 tok::ObjCKeywordKind MethodImplKind,
840 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000841 ParsingDeclRAIIObject PD(*this);
842
Douglas Gregore8f5a172010-04-07 00:21:17 +0000843 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000844 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000845 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000846 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000847 }
848
Chris Lattnere8904e92008-08-23 01:48:03 +0000849 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000850 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000852 if (Tok.is(tok::l_paren))
Douglas Gregorb77cab92011-03-08 19:17:54 +0000853 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek9e049352010-02-18 23:05:16 +0000855 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000856 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000857 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000858 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000859
Douglas Gregore8f5a172010-04-07 00:21:17 +0000860 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000861 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000862 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000863 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000864 }
865
Ted Kremenek9e049352010-02-18 23:05:16 +0000866 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000867 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000868 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000869
Steve Naroff84c43102009-02-11 20:43:13 +0000870 // An unnamed colon is valid.
871 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000872 Diag(Tok, diag::err_expected_selector_for_method)
873 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000874 // Skip until we get a ; or {}.
875 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000876 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattner5f9e2722011-07-23 10:55:15 +0000879 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000880 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000881 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000882 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000883 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattnerff384912007-10-07 02:00:24 +0000885 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000886 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000887 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Douglas Gregor926df6c2011-06-11 01:09:30 +0000888 mType, IDecl, DSRet, ReturnType,
889 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000890 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000891 methodAttrs.getList(), MethodImplKind,
892 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000893 PD.complete(Result);
894 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000895 }
Steve Narofff28b2642007-09-05 23:30:30 +0000896
Chris Lattner5f9e2722011-07-23 10:55:15 +0000897 SmallVector<IdentifierInfo *, 12> KeyIdents;
898 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000899 ParseScope PrototypeScope(this,
900 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000901
902 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000903
Chris Lattnerff384912007-10-07 02:00:24 +0000904 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000905 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000906 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Chris Lattnerff384912007-10-07 02:00:24 +0000908 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000909 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000910 Diag(Tok, diag::err_expected_colon);
911 break;
912 }
913 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000914
John McCallb3d87482010-08-24 05:47:05 +0000915 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000916 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000917 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000918
Chris Lattnerff384912007-10-07 02:00:24 +0000919 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000920 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +0000921 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +0000922 MaybeParseGNUAttributes(paramAttrs);
923 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +0000924 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000925
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000926 // Code completion for the next piece of the selector.
927 if (Tok.is(tok::code_completion)) {
928 ConsumeCodeCompletionToken();
929 KeyIdents.push_back(SelIdent);
930 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
931 mType == tok::minus,
932 /*AtParameterName=*/true,
933 ReturnType,
934 KeyIdents.data(),
935 KeyIdents.size());
936 KeyIdents.pop_back();
937 break;
938 }
939
Chris Lattnerdf195262007-10-09 17:51:17 +0000940 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000941 Diag(Tok, diag::err_expected_ident); // missing argument name.
942 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattnere294d3f2009-04-11 18:57:04 +0000945 ArgInfo.Name = Tok.getIdentifierInfo();
946 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000947 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattnere294d3f2009-04-11 18:57:04 +0000949 ArgInfos.push_back(ArgInfo);
950 KeyIdents.push_back(SelIdent);
951
John McCall0b7e6782011-03-24 11:26:52 +0000952 // Make sure the attributes persist.
953 allParamAttrs.takeAllFrom(paramAttrs.getPool());
954
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000955 // Code completion for the next piece of the selector.
956 if (Tok.is(tok::code_completion)) {
957 ConsumeCodeCompletionToken();
958 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
959 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000960 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000961 ReturnType,
962 KeyIdents.data(),
963 KeyIdents.size());
964 break;
965 }
966
Chris Lattnerff384912007-10-07 02:00:24 +0000967 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000968 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000969 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000970 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000971 break;
972 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000973 }
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Steve Naroff335eafa2007-11-15 12:35:21 +0000975 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattnerff384912007-10-07 02:00:24 +0000977 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000978 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000979 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000980 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000981 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000982 ConsumeToken();
983 break;
984 }
John McCall0b7e6782011-03-24 11:26:52 +0000985 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +0000986 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000987 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000988 Declarator ParmDecl(DS, Declarator::PrototypeContext);
989 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000990 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000991 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000992 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
993 ParmDecl.getIdentifierLoc(),
994 Param,
995 0));
996
Chris Lattnerff384912007-10-07 02:00:24 +0000997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +0000999 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001000 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001001 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001002 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001003
1004 if (KeyIdents.size() == 0) {
1005 // Leave prototype scope.
1006 PrototypeScope.Exit();
John McCalld226f652010-08-21 09:40:31 +00001007 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001008 }
1009
Chris Lattnerff384912007-10-07 02:00:24 +00001010 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1011 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001012 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001013 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00001014 mType, IDecl, DSRet, ReturnType,
1015 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001016 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001017 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001018 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001019 // Leave prototype scope.
1020 PrototypeScope.Exit();
1021
John McCall54abf7d2009-11-04 02:18:39 +00001022 PD.complete(Result);
1023 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001024}
1025
Steve Naroffdac269b2007-08-20 21:31:48 +00001026/// objc-protocol-refs:
1027/// '<' identifier-list '>'
1028///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001029bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001030ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1031 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001032 bool WarnOnDeclarations,
1033 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001034 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001036 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattner5f9e2722011-07-23 10:55:15 +00001038 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattnere13b9592008-07-26 04:03:38 +00001040 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001041 if (Tok.is(tok::code_completion)) {
1042 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1043 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001044 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001045 }
1046
Chris Lattnere13b9592008-07-26 04:03:38 +00001047 if (Tok.isNot(tok::identifier)) {
1048 Diag(Tok, diag::err_expected_ident);
1049 SkipUntil(tok::greater);
1050 return true;
1051 }
1052 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1053 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001054 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001055 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattnere13b9592008-07-26 04:03:38 +00001057 if (Tok.isNot(tok::comma))
1058 break;
1059 ConsumeToken();
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Chris Lattnere13b9592008-07-26 04:03:38 +00001062 // Consume the '>'.
1063 if (Tok.isNot(tok::greater)) {
1064 Diag(Tok, diag::err_expected_greater);
1065 return true;
1066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Chris Lattnere13b9592008-07-26 04:03:38 +00001068 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattnere13b9592008-07-26 04:03:38 +00001070 // Convert the list of protocols identifiers into a list of protocol decls.
1071 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1072 &ProtocolIdents[0], ProtocolIdents.size(),
1073 Protocols);
1074 return false;
1075}
1076
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001077/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1078/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001079bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001080 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1081 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1082 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001083 SmallVector<Decl *, 8> ProtocolDecl;
1084 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001085 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1086 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001087 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1088 ProtocolLocs.data(), LAngleLoc);
1089 if (EndProtoLoc.isValid())
1090 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001091 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001092}
1093
1094
Steve Naroffdac269b2007-08-20 21:31:48 +00001095/// objc-class-instance-variables:
1096/// '{' objc-instance-variable-decl-list[opt] '}'
1097///
1098/// objc-instance-variable-decl-list:
1099/// objc-visibility-spec
1100/// objc-instance-variable-decl ';'
1101/// ';'
1102/// objc-instance-variable-decl-list objc-visibility-spec
1103/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1104/// objc-instance-variable-decl-list ';'
1105///
1106/// objc-visibility-spec:
1107/// @private
1108/// @protected
1109/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001110/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001111///
1112/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001113/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001114///
John McCalld226f652010-08-21 09:40:31 +00001115void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001116 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001117 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001118 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001119 SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001120
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001121 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001122
Steve Naroffddbff782007-08-21 21:17:12 +00001123 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Steve Naroffddbff782007-08-21 21:17:12 +00001125 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001126 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001127 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Steve Naroffddbff782007-08-21 21:17:12 +00001129 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001130 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001131 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001132 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001133 ConsumeToken();
1134 continue;
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Steve Naroffddbff782007-08-21 21:17:12 +00001137 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001138 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001139 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001140
1141 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001142 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001143 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001144 }
1145
Steve Naroff861cf3e2007-08-23 18:16:40 +00001146 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001147 case tok::objc_private:
1148 case tok::objc_public:
1149 case tok::objc_protected:
1150 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001151 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001152 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001153 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001154 default:
1155 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001156 continue;
1157 }
1158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001160 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001161 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001162 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001163 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001164 }
1165
John McCallbdd563e2009-11-03 02:38:08 +00001166 struct ObjCIvarCallback : FieldCallback {
1167 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001168 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001169 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001170 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001171
John McCalld226f652010-08-21 09:40:31 +00001172 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001173 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001174 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1175 }
1176
John McCalld226f652010-08-21 09:40:31 +00001177 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001178 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001179 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001180 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001181 FD.D.getDeclSpec().getSourceRange().getBegin(),
1182 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001183 if (Field)
1184 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001185 return Field;
1186 }
1187 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001188
Chris Lattnere1359422008-04-10 06:46:29 +00001189 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001190 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001191 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Chris Lattnerdf195262007-10-09 17:51:17 +00001193 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001194 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001195 } else {
1196 Diag(Tok, diag::err_expected_semi_decl_list);
1197 // Skip to end of block or statement
1198 SkipUntil(tok::r_brace, true, true);
1199 }
1200 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001201 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001202 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001203 // Call ActOnFields() even if we don't have any decls. This is useful
1204 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001205 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001206 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001207 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001208 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001209}
Steve Naroffdac269b2007-08-20 21:31:48 +00001210
1211/// objc-protocol-declaration:
1212/// objc-protocol-definition
1213/// objc-protocol-forward-reference
1214///
1215/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001216/// @protocol identifier
1217/// objc-protocol-refs[opt]
1218/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001219/// @end
1220///
1221/// objc-protocol-forward-reference:
1222/// @protocol identifier-list ';'
1223///
1224/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001225/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001226/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001227Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001228 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001229 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001230 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1231 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Douglas Gregor083128f2009-11-18 04:49:41 +00001233 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001234 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001235 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001236 }
1237
Chris Lattnerdf195262007-10-09 17:51:17 +00001238 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001239 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001240 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001241 }
1242 // Save the protocol name, then consume it.
1243 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1244 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattnerdf195262007-10-09 17:51:17 +00001246 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001247 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001248 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001249 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001250 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Chris Lattnerdf195262007-10-09 17:51:17 +00001253 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001254 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001255 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1256
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001257 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001258 while (1) {
1259 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001260 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001261 Diag(Tok, diag::err_expected_ident);
1262 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001263 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001264 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001265 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1266 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001267 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Chris Lattnerdf195262007-10-09 17:51:17 +00001269 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001270 break;
1271 }
1272 // Consume the ';'.
1273 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001274 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Steve Naroffe440eb82007-10-10 17:32:04 +00001276 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001277 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001278 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001279 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001282 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001283 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001284
Chris Lattner5f9e2722011-07-23 10:55:15 +00001285 SmallVector<Decl *, 8> ProtocolRefs;
1286 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001287 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001288 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1289 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001290 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
John McCalld226f652010-08-21 09:40:31 +00001292 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001293 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001294 ProtocolRefs.data(),
1295 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001296 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001297 EndProtoLoc, attrs.getList());
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001298 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001299 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001300}
Steve Naroffdac269b2007-08-20 21:31:48 +00001301
1302/// objc-implementation:
1303/// objc-class-implementation-prologue
1304/// objc-category-implementation-prologue
1305///
1306/// objc-class-implementation-prologue:
1307/// @implementation identifier objc-superclass[opt]
1308/// objc-class-instance-variables[opt]
1309///
1310/// objc-category-implementation-prologue:
1311/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001312Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001313 SourceLocation atLoc) {
1314 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1315 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1316 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001318 // Code completion after '@implementation'.
1319 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001320 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001321 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001322 }
1323
Chris Lattnerdf195262007-10-09 17:51:17 +00001324 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001325 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001326 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001327 }
1328 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001329 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001330 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001331
1332 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001333 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001334 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001335 SourceLocation categoryLoc, rparenLoc;
1336 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001338 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001339 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001340 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001341 }
1342
Chris Lattnerdf195262007-10-09 17:51:17 +00001343 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001344 categoryId = Tok.getIdentifierInfo();
1345 categoryLoc = ConsumeToken();
1346 } else {
1347 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001348 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001349 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001350 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001351 Diag(Tok, diag::err_expected_rparen);
1352 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001353 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001354 }
1355 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001356 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001357 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001358 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001359 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001360 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001361 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001362 }
1363 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001364 SourceLocation superClassLoc;
1365 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001366 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001367 // We have a super class
1368 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001369 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001370 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001371 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001372 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001373 superClassId = Tok.getIdentifierInfo();
1374 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001375 }
John McCalld226f652010-08-21 09:40:31 +00001376 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001377 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001378 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Steve Naroff60fccee2007-10-29 21:38:07 +00001380 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001381 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001382 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001384 PendingObjCImpDecl.push_back(ObjCImpDecl);
1385
John McCalld226f652010-08-21 09:40:31 +00001386 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001387}
Steve Naroff60fccee2007-10-29 21:38:07 +00001388
John McCalld226f652010-08-21 09:40:31 +00001389Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001390 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1391 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001392 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001393 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001394 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001395 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001396 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001397 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001398 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001399 else {
1400 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001401 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001402 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001403 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001404}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001405
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001406Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1407 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001408 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001409 return Actions.ConvertDeclToDeclGroup(0);
1410 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001411 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001412 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1413}
1414
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001415/// compatibility-alias-decl:
1416/// @compatibility_alias alias-name class-name ';'
1417///
John McCalld226f652010-08-21 09:40:31 +00001418Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001419 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1420 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1421 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001422 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001423 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001424 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001425 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001426 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1427 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001428 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001429 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001430 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001431 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001432 IdentifierInfo *classId = Tok.getIdentifierInfo();
1433 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001434 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1435 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001436 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1437 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001438}
1439
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001440/// property-synthesis:
1441/// @synthesize property-ivar-list ';'
1442///
1443/// property-ivar-list:
1444/// property-ivar
1445/// property-ivar-list ',' property-ivar
1446///
1447/// property-ivar:
1448/// identifier
1449/// identifier '=' identifier
1450///
John McCalld226f652010-08-21 09:40:31 +00001451Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001452 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1453 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001454 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregorb328c422009-11-18 19:45:45 +00001456 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001457 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001458 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001459 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001460 }
1461
Douglas Gregorb328c422009-11-18 19:45:45 +00001462 if (Tok.isNot(tok::identifier)) {
1463 Diag(Tok, diag::err_synthesized_property_name);
1464 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001465 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001466 }
1467
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001468 IdentifierInfo *propertyIvar = 0;
1469 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1470 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001471 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001472 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001473 // property '=' ivar-name
1474 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001475
1476 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001477 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001478 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001479 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001480 }
1481
Chris Lattnerdf195262007-10-09 17:51:17 +00001482 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001483 Diag(Tok, diag::err_expected_ident);
1484 break;
1485 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001486 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001487 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001488 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001489 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001490 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001491 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001492 break;
1493 ConsumeToken(); // consume ','
1494 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001495 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001496 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001497}
1498
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001499/// property-dynamic:
1500/// @dynamic property-list
1501///
1502/// property-list:
1503/// identifier
1504/// property-list ',' identifier
1505///
John McCalld226f652010-08-21 09:40:31 +00001506Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001507 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1508 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001509 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001510 while (true) {
1511 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001512 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001513 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001514 }
1515
1516 if (Tok.isNot(tok::identifier)) {
1517 Diag(Tok, diag::err_expected_ident);
1518 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001519 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001520 }
1521
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001522 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1523 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001524 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001525 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001526
Chris Lattnerdf195262007-10-09 17:51:17 +00001527 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001528 break;
1529 ConsumeToken(); // consume ','
1530 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001531 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001532 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001533}
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001535/// objc-throw-statement:
1536/// throw expression[opt];
1537///
John McCall60d7b3a2010-08-24 06:29:42 +00001538StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1539 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001540 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001541 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001542 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001543 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001544 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001545 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001546 }
1547 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001548 // consume ';'
1549 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001550 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001551}
1552
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001553/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001554/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001555///
John McCall60d7b3a2010-08-24 06:29:42 +00001556StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001557Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001558 ConsumeToken(); // consume synchronized
1559 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001560 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001561 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001562 }
John McCall07524032011-07-27 21:50:02 +00001563
1564 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001565 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001566 ExprResult operand(ParseExpression());
1567
1568 if (Tok.is(tok::r_paren)) {
1569 ConsumeParen(); // ')'
1570 } else {
1571 if (!operand.isInvalid())
1572 Diag(Tok, diag::err_expected_rparen);
1573
1574 // Skip forward until we see a left brace, but don't consume it.
1575 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001576 }
John McCall07524032011-07-27 21:50:02 +00001577
1578 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001579 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001580 if (!operand.isInvalid())
1581 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001582 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001583 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001584
John McCall07524032011-07-27 21:50:02 +00001585 // Check the @synchronized operand now.
1586 if (!operand.isInvalid())
1587 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001588
John McCall07524032011-07-27 21:50:02 +00001589 // Parse the compound statement within a new scope.
1590 ParseScope bodyScope(this, Scope::DeclScope);
1591 StmtResult body(ParseCompoundStatementBody());
1592 bodyScope.Exit();
1593
1594 // If there was a semantic or parse error earlier with the
1595 // operand, fail now.
1596 if (operand.isInvalid())
1597 return StmtError();
1598
1599 if (body.isInvalid())
1600 body = Actions.ActOnNullStmt(Tok.getLocation());
1601
1602 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001603}
1604
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001605/// objc-try-catch-statement:
1606/// @try compound-statement objc-catch-list[opt]
1607/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1608///
1609/// objc-catch-list:
1610/// @catch ( parameter-declaration ) compound-statement
1611/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1612/// catch-parameter-declaration:
1613/// parameter-declaration
1614/// '...' [OBJC2]
1615///
John McCall60d7b3a2010-08-24 06:29:42 +00001616StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001617 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001618
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001619 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001620 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001621 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001622 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001623 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001624 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001625 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001626 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001627 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001628 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001629 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001630 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001631
Chris Lattnerdf195262007-10-09 17:51:17 +00001632 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001633 // At this point, we need to lookahead to determine if this @ is the start
1634 // of an @catch or @finally. We don't want to consume the @ token if this
1635 // is an @try or @encode or something else.
1636 Token AfterAt = GetLookAheadToken(1);
1637 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1638 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1639 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001640
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001641 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001642 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001643 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001644 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001645 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001646 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001647 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001648 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001649 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001650 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001651 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001652 ParseDeclarator(ParmDecl);
1653
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001654 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001655 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001656 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001657 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001658 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Steve Naroff93a25952009-04-07 22:56:58 +00001660 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Steve Naroff93a25952009-04-07 22:56:58 +00001662 if (Tok.is(tok::r_paren))
1663 RParenLoc = ConsumeParen();
1664 else // Skip over garbage, until we get to ')'. Eat the ')'.
1665 SkipUntil(tok::r_paren, true, false);
1666
John McCall60d7b3a2010-08-24 06:29:42 +00001667 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001668 if (Tok.is(tok::l_brace))
1669 CatchBody = ParseCompoundStatementBody();
1670 else
1671 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001672 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001673 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001674
John McCall60d7b3a2010-08-24 06:29:42 +00001675 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001676 RParenLoc,
1677 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001678 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001679 if (!Catch.isInvalid())
1680 CatchStmts.push_back(Catch.release());
1681
Steve Naroff64515f32008-02-05 21:27:35 +00001682 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001683 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1684 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001685 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001686 }
1687 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001688 } else {
1689 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001690 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001691 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001692
John McCall60d7b3a2010-08-24 06:29:42 +00001693 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001694 if (Tok.is(tok::l_brace))
1695 FinallyBody = ParseCompoundStatementBody();
1696 else
1697 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001698 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001699 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001700 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001701 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001702 catch_or_finally_seen = true;
1703 break;
1704 }
1705 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001706 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001707 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001708 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001709 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001710
John McCall9ae2f072010-08-23 23:25:46 +00001711 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001712 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001713 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001714}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001715
John McCallf85e1932011-06-15 23:02:42 +00001716/// objc-autoreleasepool-statement:
1717/// @autoreleasepool compound-statement
1718///
1719StmtResult
1720Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1721 ConsumeToken(); // consume autoreleasepool
1722 if (Tok.isNot(tok::l_brace)) {
1723 Diag(Tok, diag::err_expected_lbrace);
1724 return StmtError();
1725 }
1726 // Enter a scope to hold everything within the compound stmt. Compound
1727 // statements can always hold declarations.
1728 ParseScope BodyScope(this, Scope::DeclScope);
1729
1730 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1731
1732 BodyScope.Exit();
1733 if (AutoreleasePoolBody.isInvalid())
1734 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1735 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1736 AutoreleasePoolBody.take());
1737}
1738
Steve Naroff3536b442007-09-06 21:24:23 +00001739/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001740///
John McCalld226f652010-08-21 09:40:31 +00001741Decl *Parser::ParseObjCMethodDefinition() {
1742 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001743
John McCallf312b1e2010-08-26 23:41:50 +00001744 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1745 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001746
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001747 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001748 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001749 if (ObjCImpDecl) {
1750 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001751 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001752 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001753 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001754 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001755
Steve Naroff409be832007-11-11 19:54:21 +00001756 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001757 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001758 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Steve Naroff409be832007-11-11 19:54:21 +00001760 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1761 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Steve Naroff409be832007-11-11 19:54:21 +00001763 // If we didn't find the '{', bail out.
1764 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001765 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001766 }
Steve Naroff409be832007-11-11 19:54:21 +00001767 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Steve Naroff409be832007-11-11 19:54:21 +00001769 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001770 ParseScope BodyScope(this,
1771 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Steve Naroff409be832007-11-11 19:54:21 +00001773 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001774 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001775 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001776
Douglas Gregorc9977d02011-03-16 17:05:57 +00001777 if (PP.isCodeCompletionEnabled()) {
1778 if (trySkippingFunctionBodyForCodeCompletion()) {
1779 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001780 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001781 }
1782 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001783
John McCall60d7b3a2010-08-24 06:29:42 +00001784 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001785
Steve Naroff409be832007-11-11 19:54:21 +00001786 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001787 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001788 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1789 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001790
Steve Naroff409be832007-11-11 19:54:21 +00001791 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001792 BodyScope.Exit();
Douglas Gregorc9977d02011-03-16 17:05:57 +00001793
1794 // TODO: Pass argument information.
1795 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Steve Naroff71c0a952007-11-13 23:01:27 +00001796 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001797}
Anders Carlsson55085182007-08-21 17:43:55 +00001798
John McCall60d7b3a2010-08-24 06:29:42 +00001799StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001800 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001801 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001802 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001803 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001804 }
1805
1806 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001807 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001808
1809 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001810 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001811
1812 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001813 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001814
1815 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1816 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001817
John McCall60d7b3a2010-08-24 06:29:42 +00001818 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001819 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001820 // If the expression is invalid, skip ahead to the next semicolon. Not
1821 // doing this opens us up to the possibility of infinite loops if
1822 // ParseExpression does not consume any tokens.
1823 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001824 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001825 }
Chris Lattner5d803162009-12-07 16:33:19 +00001826
Steve Naroff64515f32008-02-05 21:27:35 +00001827 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001828 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001829 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001830}
1831
John McCall60d7b3a2010-08-24 06:29:42 +00001832ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001833 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001834 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001835 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001836 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001837 return ExprError();
1838
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001839 case tok::string_literal: // primary-expression: string-literal
1840 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001841 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001842 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001843 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001844 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001845
Chris Lattner4fef81d2008-08-05 06:19:09 +00001846 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1847 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001848 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001849 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001850 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001851 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001852 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001853 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001854 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001855 }
Anders Carlsson55085182007-08-21 17:43:55 +00001856 }
Anders Carlsson55085182007-08-21 17:43:55 +00001857}
1858
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001859/// \brirg Parse the receiver of an Objective-C++ message send.
1860///
1861/// This routine parses the receiver of a message send in
1862/// Objective-C++ either as a type or as an expression. Note that this
1863/// routine must not be called to parse a send to 'super', since it
1864/// has no way to return such a result.
1865///
1866/// \param IsExpr Whether the receiver was parsed as an expression.
1867///
1868/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1869/// IsExpr is true), the parsed expression. If the receiver was parsed
1870/// as a type (\c IsExpr is false), the parsed type.
1871///
1872/// \returns True if an error occurred during parsing or semantic
1873/// analysis, in which case the arguments do not have valid
1874/// values. Otherwise, returns false for a successful parse.
1875///
1876/// objc-receiver: [C++]
1877/// 'super' [not parsed here]
1878/// expression
1879/// simple-type-specifier
1880/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001881bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001882 InMessageExpressionRAIIObject InMessage(*this, true);
1883
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001884 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1885 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1886 TryAnnotateTypeOrScopeToken();
1887
1888 if (!isCXXSimpleTypeSpecifier()) {
1889 // objc-receiver:
1890 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001891 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001892 if (Receiver.isInvalid())
1893 return true;
1894
1895 IsExpr = true;
1896 TypeOrExpr = Receiver.take();
1897 return false;
1898 }
1899
1900 // objc-receiver:
1901 // typename-specifier
1902 // simple-type-specifier
1903 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001904 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001905 ParseCXXSimpleTypeSpecifier(DS);
1906
1907 if (Tok.is(tok::l_paren)) {
1908 // If we see an opening parentheses at this point, we are
1909 // actually parsing an expression that starts with a
1910 // function-style cast, e.g.,
1911 //
1912 // postfix-expression:
1913 // simple-type-specifier ( expression-list [opt] )
1914 // typename-specifier ( expression-list [opt] )
1915 //
1916 // Parse the remainder of this case, then the (optional)
1917 // postfix-expression suffix, followed by the (optional)
1918 // right-hand side of the binary expression. We have an
1919 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001920 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001921 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001922 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001923 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001924 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001925 if (Receiver.isInvalid())
1926 return true;
1927
1928 IsExpr = true;
1929 TypeOrExpr = Receiver.take();
1930 return false;
1931 }
1932
1933 // We have a class message. Turn the simple-type-specifier or
1934 // typename-specifier we parsed into a type and parse the
1935 // remainder of the class message.
1936 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001937 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001938 if (Type.isInvalid())
1939 return true;
1940
1941 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001942 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001943 return false;
1944}
1945
Douglas Gregor1b730e82010-05-31 14:40:22 +00001946/// \brief Determine whether the parser is currently referring to a an
1947/// Objective-C message send, using a simplified heuristic to avoid overhead.
1948///
1949/// This routine will only return true for a subset of valid message-send
1950/// expressions.
1951bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001952 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001953 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001954 return GetLookAheadToken(1).is(tok::identifier) &&
1955 GetLookAheadToken(2).is(tok::identifier);
1956}
1957
Douglas Gregor9497a732010-09-16 01:51:54 +00001958bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1959 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1960 InMessageExpression)
1961 return false;
1962
1963
1964 ParsedType Type;
1965
1966 if (Tok.is(tok::annot_typename))
1967 Type = getTypeAnnotation(Tok);
1968 else if (Tok.is(tok::identifier))
1969 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1970 getCurScope());
1971 else
1972 return false;
1973
1974 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1975 const Token &AfterNext = GetLookAheadToken(2);
1976 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1977 if (Tok.is(tok::identifier))
1978 TryAnnotateTypeOrScopeToken();
1979
1980 return Tok.is(tok::annot_typename);
1981 }
1982 }
1983
1984 return false;
1985}
1986
Mike Stump1eb44332009-09-09 15:08:12 +00001987/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001988/// '[' objc-receiver objc-message-args ']'
1989///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001990/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001991/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001992/// expression
1993/// class-name
1994/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001995///
John McCall60d7b3a2010-08-24 06:29:42 +00001996ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001997 assert(Tok.is(tok::l_square) && "'[' expected");
1998 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1999
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002000 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002001 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002002 ConsumeCodeCompletionToken();
2003 SkipUntil(tok::r_square);
2004 return ExprError();
2005 }
2006
Douglas Gregor0fbda682010-09-15 14:51:05 +00002007 InMessageExpressionRAIIObject InMessage(*this, true);
2008
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002009 if (getLang().CPlusPlus) {
2010 // We completely separate the C and C++ cases because C++ requires
2011 // more complicated (read: slower) parsing.
2012
2013 // Handle send to super.
2014 // FIXME: This doesn't benefit from the same typo-correction we
2015 // get in Objective-C.
2016 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002017 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002018 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2019 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002020
2021 // Parse the receiver, which is either a type or an expression.
2022 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002023 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002024 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2025 SkipUntil(tok::r_square);
2026 return ExprError();
2027 }
2028
2029 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002030 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2031 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002032 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002033
2034 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002035 ParsedType::getFromOpaquePtr(TypeOrExpr),
2036 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002037 }
2038
2039 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002040 IdentifierInfo *Name = Tok.getIdentifierInfo();
2041 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002042 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002043 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002044 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002045 NextToken().is(tok::period),
2046 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002047 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002048 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2049 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002050
John McCallf312b1e2010-08-26 23:41:50 +00002051 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002052 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002053 SkipUntil(tok::r_square);
2054 return ExprError();
2055 }
2056
Douglas Gregor1569f952010-04-21 20:38:13 +00002057 ConsumeToken(); // the type name
2058
2059 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002060 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002061
John McCallf312b1e2010-08-26 23:41:50 +00002062 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002063 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002064 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002065 }
Chris Lattner699b6612008-01-25 18:59:06 +00002066 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002067
2068 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002069 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002070 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002071 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002072 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002073 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002074
John McCallb3d87482010-08-24 05:47:05 +00002075 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2076 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002077}
Sebastian Redl1d922962008-12-13 15:32:12 +00002078
Douglas Gregor2725ca82010-04-21 19:57:20 +00002079/// \brief Parse the remainder of an Objective-C message following the
2080/// '[' objc-receiver.
2081///
2082/// This routine handles sends to super, class messages (sent to a
2083/// class name), and instance messages (sent to an object), and the
2084/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2085/// ReceiverExpr, respectively. Only one of these parameters may have
2086/// a valid value.
2087///
2088/// \param LBracLoc The location of the opening '['.
2089///
2090/// \param SuperLoc If this is a send to 'super', the location of the
2091/// 'super' keyword that indicates a send to the superclass.
2092///
2093/// \param ReceiverType If this is a class message, the type of the
2094/// class we are sending a message to.
2095///
2096/// \param ReceiverExpr If this is an instance message, the expression
2097/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002098///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002099/// objc-message-args:
2100/// objc-selector
2101/// objc-keywordarg-list
2102///
2103/// objc-keywordarg-list:
2104/// objc-keywordarg
2105/// objc-keywordarg-list objc-keywordarg
2106///
Mike Stump1eb44332009-09-09 15:08:12 +00002107/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002108/// selector-name[opt] ':' objc-keywordexpr
2109///
2110/// objc-keywordexpr:
2111/// nonempty-expr-list
2112///
2113/// nonempty-expr-list:
2114/// assignment-expression
2115/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002116///
John McCall60d7b3a2010-08-24 06:29:42 +00002117ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002118Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002119 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002120 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002121 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002122 InMessageExpressionRAIIObject InMessage(*this, true);
2123
Steve Naroffc4df6d22009-11-07 02:08:14 +00002124 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002125 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002126 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2127 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002128 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002129 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2130 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002131 else
John McCall9ae2f072010-08-23 23:25:46 +00002132 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002133 0, 0, false);
Douglas Gregordc845342010-05-25 05:58:43 +00002134 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002135 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002136
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002137 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002138 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002139 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002140
Anders Carlssonff975cf2009-02-14 18:21:46 +00002141 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Chris Lattner5f9e2722011-07-23 10:55:15 +00002143 SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002144 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002145
Chris Lattnerdf195262007-10-09 17:51:17 +00002146 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002147 while (1) {
2148 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002149 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002150
Chris Lattnerdf195262007-10-09 17:51:17 +00002151 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002152 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002153 // We must manually skip to a ']', otherwise the expression skipper will
2154 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2155 // the enclosing expression.
2156 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002157 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002158 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002159
Steve Naroff68d331a2007-09-27 14:38:14 +00002160 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002161 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002162
2163 if (Tok.is(tok::code_completion)) {
2164 if (SuperLoc.isValid())
2165 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2166 KeyIdents.data(),
2167 KeyIdents.size(),
2168 /*AtArgumentEpression=*/true);
2169 else if (ReceiverType)
2170 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2171 KeyIdents.data(),
2172 KeyIdents.size(),
2173 /*AtArgumentEpression=*/true);
2174 else
2175 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2176 KeyIdents.data(),
2177 KeyIdents.size(),
2178 /*AtArgumentEpression=*/true);
2179
2180 ConsumeCodeCompletionToken();
2181 SkipUntil(tok::r_square);
2182 return ExprError();
2183 }
2184
John McCall60d7b3a2010-08-24 06:29:42 +00002185 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002186 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002187 // We must manually skip to a ']', otherwise the expression skipper will
2188 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2189 // the enclosing expression.
2190 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002191 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002192 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002193
Steve Naroff37387c92007-09-17 20:25:27 +00002194 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002195 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002196
Douglas Gregord3c68542009-11-19 01:08:35 +00002197 // Code completion after each argument.
2198 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002199 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002200 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002201 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002202 KeyIdents.size(),
2203 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002204 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002205 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002206 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002207 KeyIdents.size(),
2208 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002209 else
John McCall9ae2f072010-08-23 23:25:46 +00002210 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002211 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002212 KeyIdents.size(),
2213 /*AtArgumentEpression=*/false);
Douglas Gregordc845342010-05-25 05:58:43 +00002214 ConsumeCodeCompletionToken();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002215 SkipUntil(tok::r_square);
2216 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002217 }
2218
Steve Naroff37387c92007-09-17 20:25:27 +00002219 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002220 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002221 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002222 break;
2223 // We have a selector or a colon, continue parsing.
2224 }
2225 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002226 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002227 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002228 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002229 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002230 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002231 // We must manually skip to a ']', otherwise the expression skipper will
2232 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2233 // the enclosing expression.
2234 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002235 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002236 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002237
Steve Naroff49f109c2007-11-15 13:05:42 +00002238 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002239 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002240 }
2241 } else if (!selIdent) {
2242 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002243
Chris Lattner4fef81d2008-08-05 06:19:09 +00002244 // We must manually skip to a ']', otherwise the expression skipper will
2245 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2246 // the enclosing expression.
2247 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002248 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002249 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002250
Chris Lattnerdf195262007-10-09 17:51:17 +00002251 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002252 if (Tok.is(tok::identifier))
2253 Diag(Tok, diag::err_expected_colon);
2254 else
2255 Diag(Tok, diag::err_expected_rsquare);
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 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002262
Chris Lattner699b6612008-01-25 18:59:06 +00002263 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002264
Steve Naroff29238a02007-10-05 18:42:47 +00002265 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002266 if (nKeys == 0)
2267 KeyIdents.push_back(selIdent);
2268 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002269
Douglas Gregor2725ca82010-04-21 19:57:20 +00002270 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002271 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002272 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002273 MultiExprArg(Actions,
2274 KeyExprs.take(),
2275 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002276 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002277 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002278 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002279 MultiExprArg(Actions,
2280 KeyExprs.take(),
2281 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002282 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002283 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002284 MultiExprArg(Actions,
2285 KeyExprs.take(),
2286 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002287}
2288
John McCall60d7b3a2010-08-24 06:29:42 +00002289ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2290 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002291 if (Res.isInvalid()) return move(Res);
2292
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002293 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2294 // expressions. At this point, we know that the only valid thing that starts
2295 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002296 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002297 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002298 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002299 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002300
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002301 while (Tok.is(tok::at)) {
2302 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002303
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002304 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002305 if (!isTokenStringLiteral())
2306 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002307
John McCall60d7b3a2010-08-24 06:29:42 +00002308 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002309 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002310 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002311
Sebastian Redleffa8d12008-12-10 00:02:53 +00002312 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002313 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002314
2315 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2316 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002317}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002318
2319/// objc-encode-expression:
2320/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002321ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002322Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002323 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002324
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002325 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002326
Chris Lattner4fef81d2008-08-05 06:19:09 +00002327 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002328 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2329
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002330 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002331
Douglas Gregor809070a2009-02-18 17:45:20 +00002332 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002333
Anders Carlsson4988ae32007-08-23 15:31:37 +00002334 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002335
Douglas Gregor809070a2009-02-18 17:45:20 +00002336 if (Ty.isInvalid())
2337 return ExprError();
2338
Mike Stump1eb44332009-09-09 15:08:12 +00002339 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002340 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002341}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002342
2343/// objc-protocol-expression
2344/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002345ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002346Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002347 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002348
Chris Lattner4fef81d2008-08-05 06:19:09 +00002349 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002350 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2351
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002352 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002353
Chris Lattner4fef81d2008-08-05 06:19:09 +00002354 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002355 return ExprError(Diag(Tok, diag::err_expected_ident));
2356
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002357 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002358 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002359
Anders Carlsson4988ae32007-08-23 15:31:37 +00002360 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002361
Sebastian Redl1d922962008-12-13 15:32:12 +00002362 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2363 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002364}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002365
2366/// objc-selector-expression
2367/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002368ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002369 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002370
Chris Lattner4fef81d2008-08-05 06:19:09 +00002371 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002372 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2373
Chris Lattner5f9e2722011-07-23 10:55:15 +00002374 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002375 SourceLocation LParenLoc = ConsumeParen();
2376 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002377
2378 if (Tok.is(tok::code_completion)) {
2379 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2380 KeyIdents.size());
2381 ConsumeCodeCompletionToken();
2382 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2383 return ExprError();
2384 }
2385
Chris Lattner2fc5c242009-04-11 18:13:45 +00002386 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002387 if (!SelIdent && // missing selector name.
2388 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002389 return ExprError(Diag(Tok, diag::err_expected_ident));
2390
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002391 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002392 unsigned nColons = 0;
2393 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002394 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002395 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2396 ++nColons;
2397 KeyIdents.push_back(0);
2398 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002399 return ExprError(Diag(Tok, diag::err_expected_colon));
2400
Chris Lattner5add7542010-08-27 22:32:41 +00002401 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002402 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002403 if (Tok.is(tok::r_paren))
2404 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002405
2406 if (Tok.is(tok::code_completion)) {
2407 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2408 KeyIdents.size());
2409 ConsumeCodeCompletionToken();
2410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2411 return ExprError();
2412 }
2413
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002414 // Check for another keyword selector.
2415 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002416 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002417 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002418 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002419 break;
2420 }
Steve Naroff887407e2007-12-05 22:21:29 +00002421 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002422 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002423 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002424 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2425 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002426 }