blob: 684d8edf891906af8bdc3545729e2736835d7211 [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)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
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);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000198
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000199 if (Tok.is(tok::l_brace))
200 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
201
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000202 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000203 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 Jahanian2f64cfe2011-08-22 21:44:58 +0000244 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
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;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000252 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000253 ObjCDeclSpec &OCDS;
254 SourceLocation AtLoc;
255 tok::ObjCKeywordKind MethodImplKind;
256
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000257 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000258 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000259 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
260 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000261 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000262 MethodImplKind(MethodImplKind) {
263 }
264
John McCalld226f652010-08-21 09:40:31 +0000265 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000266 if (FD.D.getIdentifier() == 0) {
267 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
268 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000269 return 0;
John McCalld0014542009-12-03 22:31:13 +0000270 }
271 if (FD.BitfieldSize) {
272 P.Diag(AtLoc, diag::err_objc_property_bitfield)
273 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000274 return 0;
John McCalld0014542009-12-03 22:31:13 +0000275 }
276
277 // Install the property declarator into interfaceDecl.
278 IdentifierInfo *SelName =
279 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
280
281 Selector GetterSel =
282 P.PP.getSelectorTable().getNullarySelector(SelName);
283 IdentifierInfo *SetterName = OCDS.getSetterName();
284 Selector SetterSel;
285 if (SetterName)
286 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
287 else
288 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
289 P.PP.getSelectorTable(),
290 FD.D.getIdentifier());
291 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000292 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000293 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000294 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000295 &isOverridingProperty,
296 MethodImplKind);
297 if (!isOverridingProperty)
298 Props.push_back(Property);
299
300 return Property;
301 }
302};
303
Steve Naroffdac269b2007-08-20 21:31:48 +0000304/// objc-interface-decl-list:
305/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000306/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000307/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000308/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000309/// objc-interface-decl-list declaration
310/// objc-interface-decl-list ';'
311///
Steve Naroff294494e2007-08-22 16:35:03 +0000312/// objc-method-requirement: [OBJC2]
313/// @required
314/// @optional
315///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000316void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
317 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000318 SmallVector<Decl *, 32> allMethods;
319 SmallVector<Decl *, 16> allProperties;
320 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000321 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek782f2f52010-01-07 01:20:12 +0000323 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000324 Actions.ActOnObjCContainerStartDefinition(CDecl);
325
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 Jahaniana28948f2011-08-22 15:54:49 +0000330 ParseObjCMethodPrototype(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,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000342 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000343 continue;
344 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000345 // Ignore excess semicolons.
346 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000347 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000348 continue;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattnerbc662af2008-10-20 06:10:06 +0000351 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000352 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000353 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000355 // Code completion within an Objective-C interface.
356 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000357 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000358 ObjCImpDecl? Sema::PCC_ObjCImplementation
359 : Sema::PCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000360 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000361 }
362
Chris Lattnere82a10f2008-10-20 05:46:22 +0000363 // If we don't have an @ directive, parse it as a function definition.
364 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000365 // The code below does not consume '}'s because it is afraid of eating the
366 // end of a namespace. Because of the way this code is structured, an
367 // erroneous r_brace would cause an infinite loop if not handled here.
368 if (Tok.is(tok::r_brace))
369 break;
John McCall0b7e6782011-03-24 11:26:52 +0000370 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000371 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000372 continue;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattnere82a10f2008-10-20 05:46:22 +0000375 // Otherwise, we have an @ directive, eat the @.
376 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000377 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000378 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000379 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000380 break;
381 }
382
Chris Lattnera2449b22008-10-20 05:57:40 +0000383 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattnera2449b22008-10-20 05:57:40 +0000385 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000386 AtEnd.setBegin(AtLoc);
387 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000388 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000389 } else if (DirectiveKind == tok::objc_not_keyword) {
390 Diag(Tok, diag::err_objc_unknown_at);
391 SkipUntil(tok::semi);
392 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Chris Lattnerbc662af2008-10-20 06:10:06 +0000395 // Eat the identifier.
396 ConsumeToken();
397
Chris Lattnera2449b22008-10-20 05:57:40 +0000398 switch (DirectiveKind) {
399 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000400 // FIXME: If someone forgets an @end on a protocol, this loop will
401 // continue to eat up tons of stuff and spew lots of nonsense errors. It
402 // would probably be better to bail out if we saw an @class or @interface
403 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000404 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000405 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000406 SkipUntil(tok::r_brace, tok::at);
407 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000408
409 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000410 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000411 Diag(Tok, diag::err_objc_missing_end);
412 ConsumeToken();
413 break;
414
Chris Lattnera2449b22008-10-20 05:57:40 +0000415 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000416 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000417 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000418 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000419 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000420 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000421 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000422 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000423 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattnera2449b22008-10-20 05:57:40 +0000425 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000426 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000427 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000428
Chris Lattnere82a10f2008-10-20 05:46:22 +0000429 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000430 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000431 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000432 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000434 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000435 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000436
Chris Lattnere82a10f2008-10-20 05:46:22 +0000437 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000438 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000439 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
John McCall7da19ea2011-03-26 01:53:26 +0000441 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000442 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000443 }
Steve Naroff294494e2007-08-22 16:35:03 +0000444 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000445
446 // We break out of the big loop in two cases: when we see @end or when we see
447 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000448 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000449 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000450 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000451 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000452 ConsumeToken(); // the "end" identifier
453 else
454 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattnera2449b22008-10-20 05:57:40 +0000456 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000457 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000458 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000459 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000460 allProperties.data(), allProperties.size(),
461 allTUVariables.data(), allTUVariables.size());
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000462 Actions.ActOnObjCContainerFinishDefinition(CDecl);
Steve Naroff294494e2007-08-22 16:35:03 +0000463}
464
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000465/// Parse property attribute declarations.
466///
467/// property-attr-decl: '(' property-attrlist ')'
468/// property-attrlist:
469/// property-attribute
470/// property-attrlist ',' property-attribute
471/// property-attribute:
472/// getter '=' identifier
473/// setter '=' identifier ':'
474/// readonly
475/// readwrite
476/// assign
477/// retain
478/// copy
479/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000480/// atomic
481/// strong
482/// weak
483/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000484///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000485void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000486 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000487 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000489 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000490 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000491 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000492 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000493 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000494 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000496 // If this is not an identifier at all, bail out early.
497 if (II == 0) {
498 MatchRHSPunctuation(tok::r_paren, LHSLoc);
499 return;
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner156b0612008-10-20 07:37:22 +0000502 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner92e62b02008-11-20 04:42:34 +0000504 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000506 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000508 else if (II->isStr("unsafe_unretained"))
509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000510 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000512 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000513 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000514 else if (II->isStr("strong"))
515 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000516 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000517 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000518 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000519 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000520 else if (II->isStr("atomic"))
521 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000522 else if (II->isStr("weak"))
523 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000524 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000525 bool IsSetter = II->getNameStart()[0] == 's';
526
Chris Lattnere00da7c2008-10-20 07:39:53 +0000527 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000528 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
529 diag::err_objc_expected_equal_for_getter;
530
531 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000532 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Douglas Gregor4ad96852009-11-19 07:41:15 +0000534 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000535 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000536 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000537 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000538 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000539 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000540 }
541
Anders Carlsson42499be2010-10-02 17:45:21 +0000542
543 SourceLocation SelLoc;
544 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
545
546 if (!SelIdent) {
547 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
548 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000549 SkipUntil(tok::r_paren);
550 return;
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Anders Carlsson42499be2010-10-02 17:45:21 +0000553 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000554 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000555 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000557 if (ExpectAndConsume(tok::colon,
558 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000559 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000560 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000561 } else {
562 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000563 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000564 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000565 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000566 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000567 SkipUntil(tok::r_paren);
568 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner156b0612008-10-20 07:37:22 +0000571 if (Tok.isNot(tok::comma))
572 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner156b0612008-10-20 07:37:22 +0000574 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner156b0612008-10-20 07:37:22 +0000577 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000578}
579
Steve Naroff3536b442007-09-06 21:24:23 +0000580/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000581/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000582/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000583///
584/// objc-instance-method: '-'
585/// objc-class-method: '+'
586///
Steve Naroff4985ace2007-08-22 18:35:33 +0000587/// objc-method-attributes: [OBJC2]
588/// __attribute__((deprecated))
589///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000590Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000591 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000592 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000593
Mike Stump1eb44332009-09-09 15:08:12 +0000594 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000595 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000596 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000597 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000598 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000599 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000600 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000601}
602
603/// objc-selector:
604/// identifier
605/// one of
606/// enum struct union if else while do for switch case default
607/// break continue return goto asm sizeof typeof __alignof
608/// unsigned long const short volatile signed restrict _Complex
609/// in out inout bycopy byref oneway int char float double void _Bool
610///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000611IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000612
Chris Lattnerff384912007-10-07 02:00:24 +0000613 switch (Tok.getKind()) {
614 default:
615 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000616 case tok::ampamp:
617 case tok::ampequal:
618 case tok::amp:
619 case tok::pipe:
620 case tok::tilde:
621 case tok::exclaim:
622 case tok::exclaimequal:
623 case tok::pipepipe:
624 case tok::pipeequal:
625 case tok::caret:
626 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000627 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000628 if (isalpha(ThisTok[0])) {
629 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
630 Tok.setKind(tok::identifier);
631 SelectorLoc = ConsumeToken();
632 return II;
633 }
634 return 0;
635 }
636
Chris Lattnerff384912007-10-07 02:00:24 +0000637 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000638 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000639 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000640 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000641 case tok::kw_break:
642 case tok::kw_case:
643 case tok::kw_catch:
644 case tok::kw_char:
645 case tok::kw_class:
646 case tok::kw_const:
647 case tok::kw_const_cast:
648 case tok::kw_continue:
649 case tok::kw_default:
650 case tok::kw_delete:
651 case tok::kw_do:
652 case tok::kw_double:
653 case tok::kw_dynamic_cast:
654 case tok::kw_else:
655 case tok::kw_enum:
656 case tok::kw_explicit:
657 case tok::kw_export:
658 case tok::kw_extern:
659 case tok::kw_false:
660 case tok::kw_float:
661 case tok::kw_for:
662 case tok::kw_friend:
663 case tok::kw_goto:
664 case tok::kw_if:
665 case tok::kw_inline:
666 case tok::kw_int:
667 case tok::kw_long:
668 case tok::kw_mutable:
669 case tok::kw_namespace:
670 case tok::kw_new:
671 case tok::kw_operator:
672 case tok::kw_private:
673 case tok::kw_protected:
674 case tok::kw_public:
675 case tok::kw_register:
676 case tok::kw_reinterpret_cast:
677 case tok::kw_restrict:
678 case tok::kw_return:
679 case tok::kw_short:
680 case tok::kw_signed:
681 case tok::kw_sizeof:
682 case tok::kw_static:
683 case tok::kw_static_cast:
684 case tok::kw_struct:
685 case tok::kw_switch:
686 case tok::kw_template:
687 case tok::kw_this:
688 case tok::kw_throw:
689 case tok::kw_true:
690 case tok::kw_try:
691 case tok::kw_typedef:
692 case tok::kw_typeid:
693 case tok::kw_typename:
694 case tok::kw_typeof:
695 case tok::kw_union:
696 case tok::kw_unsigned:
697 case tok::kw_using:
698 case tok::kw_virtual:
699 case tok::kw_void:
700 case tok::kw_volatile:
701 case tok::kw_wchar_t:
702 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000703 case tok::kw__Bool:
704 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000705 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000706 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000707 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000708 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000709 }
Steve Naroff294494e2007-08-22 16:35:03 +0000710}
711
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000712/// objc-for-collection-in: 'in'
713///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000714bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000715 // FIXME: May have to do additional look-ahead to only allow for
716 // valid tokens following an 'in'; such as an identifier, unary operators,
717 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000718 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000719 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000720}
721
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000723/// qualifier list and builds their bitmask representation in the input
724/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000725///
726/// objc-type-qualifiers:
727/// objc-type-qualifier
728/// objc-type-qualifiers objc-type-qualifier
729///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000730void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
731 ObjCTypeNameContext Context) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000732 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000733 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000734 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
735 Context == OTN_ParameterType);
Douglas Gregord32b0222010-08-24 01:06:58 +0000736 ConsumeCodeCompletionToken();
737 }
738
Chris Lattnercb53b362007-12-27 19:57:00 +0000739 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000740 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Chris Lattnere8b724d2007-12-12 06:56:32 +0000742 const IdentifierInfo *II = Tok.getIdentifierInfo();
743 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000745 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000747 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000748 switch (i) {
749 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000750 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
751 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
752 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
753 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
754 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
755 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000756 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000757 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000758 ConsumeToken();
759 II = 0;
760 break;
761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Chris Lattnere8b724d2007-12-12 06:56:32 +0000763 // If this wasn't a recognized qualifier, bail out.
764 if (II) return;
765 }
766}
767
768/// objc-type-name:
769/// '(' objc-type-qualifiers[opt] type-name ')'
770/// '(' objc-type-qualifiers[opt] ')'
771///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000772ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
773 ObjCTypeNameContext Context) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000774 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattner4a76b292008-10-22 03:52:06 +0000776 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000777 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000778 ObjCDeclContextSwitch ObjCDC(*this);
779
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000780 // Parse type qualifiers, in, inout, etc.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000781 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000782
John McCallb3d87482010-08-24 05:47:05 +0000783 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000784 if (isTypeSpecifierQualifier()) {
John McCallf85e1932011-06-15 23:02:42 +0000785 TypeResult TypeSpec =
786 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000787 if (!TypeSpec.isInvalid())
788 Ty = TypeSpec.get();
789 }
John McCallf85e1932011-06-15 23:02:42 +0000790
Steve Naroffd7333c22008-10-21 14:15:04 +0000791 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000792 ConsumeParen();
793 else if (Tok.getLocation() == TypeStartLoc) {
794 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000795 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000796 SkipUntil(tok::r_paren);
797 } else {
798 // Otherwise, we found *something*, but didn't get a ')' in the right
799 // place. Emit an error then return what we have as the type.
800 MatchRHSPunctuation(tok::r_paren, LParenLoc);
801 }
Steve Narofff28b2642007-09-05 23:30:30 +0000802 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000803}
804
805/// objc-method-decl:
806/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000807/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000808/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000809/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000810///
811/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000812/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000813/// objc-keyword-selector objc-keyword-decl
814///
815/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000816/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
817/// objc-selector ':' objc-keyword-attributes[opt] identifier
818/// ':' objc-type-name objc-keyword-attributes[opt] identifier
819/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000820///
Steve Naroff4985ace2007-08-22 18:35:33 +0000821/// objc-parmlist:
822/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000823///
Steve Naroff4985ace2007-08-22 18:35:33 +0000824/// objc-parms:
825/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000826///
Steve Naroff4985ace2007-08-22 18:35:33 +0000827/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000828/// , ...
829///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000830/// objc-keyword-attributes: [OBJC2]
831/// __attribute__((unused))
832///
John McCalld226f652010-08-21 09:40:31 +0000833Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000834 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000835 tok::ObjCKeywordKind MethodImplKind,
836 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000837 ParsingDeclRAIIObject PD(*this);
838
Douglas Gregore8f5a172010-04-07 00:21:17 +0000839 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000840 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000841 /*ReturnType=*/ ParsedType());
Douglas Gregordc845342010-05-25 05:58:43 +0000842 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000843 }
844
Chris Lattnere8904e92008-08-23 01:48:03 +0000845 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000846 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000847 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000848 if (Tok.is(tok::l_paren))
Douglas Gregorb77cab92011-03-08 19:17:54 +0000849 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek9e049352010-02-18 23:05:16 +0000851 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000852 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000853 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000854 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000855
Douglas Gregore8f5a172010-04-07 00:21:17 +0000856 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000857 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000858 ReturnType);
Douglas Gregordc845342010-05-25 05:58:43 +0000859 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000860 }
861
Ted Kremenek9e049352010-02-18 23:05:16 +0000862 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000863 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000864 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000865
Steve Naroff84c43102009-02-11 20:43:13 +0000866 // An unnamed colon is valid.
867 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000868 Diag(Tok, diag::err_expected_selector_for_method)
869 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000870 // Skip until we get a ; or {}.
871 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000872 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattner5f9e2722011-07-23 10:55:15 +0000875 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000876 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000877 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000878 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000879 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Chris Lattnerff384912007-10-07 02:00:24 +0000881 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000882 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000883 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000884 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000885 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000886 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000887 methodAttrs.getList(), MethodImplKind,
888 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000889 PD.complete(Result);
890 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000891 }
Steve Narofff28b2642007-09-05 23:30:30 +0000892
Chris Lattner5f9e2722011-07-23 10:55:15 +0000893 SmallVector<IdentifierInfo *, 12> KeyIdents;
894 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000895 ParseScope PrototypeScope(this,
896 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000897
898 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000899
Chris Lattnerff384912007-10-07 02:00:24 +0000900 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000901 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000902 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Chris Lattnerff384912007-10-07 02:00:24 +0000904 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000905 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000906 Diag(Tok, diag::err_expected_colon);
907 break;
908 }
909 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000910
John McCallb3d87482010-08-24 05:47:05 +0000911 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000912 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000913 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000914
Chris Lattnerff384912007-10-07 02:00:24 +0000915 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000916 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +0000917 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +0000918 MaybeParseGNUAttributes(paramAttrs);
919 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +0000920 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000921
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000922 // Code completion for the next piece of the selector.
923 if (Tok.is(tok::code_completion)) {
924 ConsumeCodeCompletionToken();
925 KeyIdents.push_back(SelIdent);
926 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
927 mType == tok::minus,
928 /*AtParameterName=*/true,
929 ReturnType,
930 KeyIdents.data(),
931 KeyIdents.size());
932 KeyIdents.pop_back();
933 break;
934 }
935
Chris Lattnerdf195262007-10-09 17:51:17 +0000936 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000937 Diag(Tok, diag::err_expected_ident); // missing argument name.
938 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Chris Lattnere294d3f2009-04-11 18:57:04 +0000941 ArgInfo.Name = Tok.getIdentifierInfo();
942 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000943 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattnere294d3f2009-04-11 18:57:04 +0000945 ArgInfos.push_back(ArgInfo);
946 KeyIdents.push_back(SelIdent);
947
John McCall0b7e6782011-03-24 11:26:52 +0000948 // Make sure the attributes persist.
949 allParamAttrs.takeAllFrom(paramAttrs.getPool());
950
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000951 // Code completion for the next piece of the selector.
952 if (Tok.is(tok::code_completion)) {
953 ConsumeCodeCompletionToken();
954 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
955 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000956 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000957 ReturnType,
958 KeyIdents.data(),
959 KeyIdents.size());
960 break;
961 }
962
Chris Lattnerff384912007-10-07 02:00:24 +0000963 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000964 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000965 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000966 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000967 break;
968 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000969 }
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Steve Naroff335eafa2007-11-15 12:35:21 +0000971 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattnerff384912007-10-07 02:00:24 +0000973 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000974 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000975 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000976 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000977 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000978 ConsumeToken();
979 break;
980 }
John McCall0b7e6782011-03-24 11:26:52 +0000981 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +0000982 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000983 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000984 Declarator ParmDecl(DS, Declarator::PrototypeContext);
985 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000986 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000987 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000988 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
989 ParmDecl.getIdentifierLoc(),
990 Param,
991 0));
992
Chris Lattnerff384912007-10-07 02:00:24 +0000993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +0000995 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000996 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000997 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000998 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000999
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001000 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001001 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001002
Chris Lattnerff384912007-10-07 02:00:24 +00001003 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1004 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001005 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001006 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001007 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001008 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001009 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001010 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001011 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001012
John McCall54abf7d2009-11-04 02:18:39 +00001013 PD.complete(Result);
1014 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001015}
1016
Steve Naroffdac269b2007-08-20 21:31:48 +00001017/// objc-protocol-refs:
1018/// '<' identifier-list '>'
1019///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001020bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001021ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1022 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001023 bool WarnOnDeclarations,
1024 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001025 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001027 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattner5f9e2722011-07-23 10:55:15 +00001029 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Chris Lattnere13b9592008-07-26 04:03:38 +00001031 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001032 if (Tok.is(tok::code_completion)) {
1033 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1034 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001035 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001036 }
1037
Chris Lattnere13b9592008-07-26 04:03:38 +00001038 if (Tok.isNot(tok::identifier)) {
1039 Diag(Tok, diag::err_expected_ident);
1040 SkipUntil(tok::greater);
1041 return true;
1042 }
1043 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1044 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001045 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001046 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Chris Lattnere13b9592008-07-26 04:03:38 +00001048 if (Tok.isNot(tok::comma))
1049 break;
1050 ConsumeToken();
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Chris Lattnere13b9592008-07-26 04:03:38 +00001053 // Consume the '>'.
1054 if (Tok.isNot(tok::greater)) {
1055 Diag(Tok, diag::err_expected_greater);
1056 return true;
1057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Chris Lattnere13b9592008-07-26 04:03:38 +00001059 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Chris Lattnere13b9592008-07-26 04:03:38 +00001061 // Convert the list of protocols identifiers into a list of protocol decls.
1062 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1063 &ProtocolIdents[0], ProtocolIdents.size(),
1064 Protocols);
1065 return false;
1066}
1067
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001068/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1069/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001070bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001071 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1072 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1073 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001074 SmallVector<Decl *, 8> ProtocolDecl;
1075 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001076 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1077 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001078 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1079 ProtocolLocs.data(), LAngleLoc);
1080 if (EndProtoLoc.isValid())
1081 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001082 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001083}
1084
1085
Steve Naroffdac269b2007-08-20 21:31:48 +00001086/// objc-class-instance-variables:
1087/// '{' objc-instance-variable-decl-list[opt] '}'
1088///
1089/// objc-instance-variable-decl-list:
1090/// objc-visibility-spec
1091/// objc-instance-variable-decl ';'
1092/// ';'
1093/// objc-instance-variable-decl-list objc-visibility-spec
1094/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1095/// objc-instance-variable-decl-list ';'
1096///
1097/// objc-visibility-spec:
1098/// @private
1099/// @protected
1100/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001101/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001102///
1103/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001104/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001105///
John McCalld226f652010-08-21 09:40:31 +00001106void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001107 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001108 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001109 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001110 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001111
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001112 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001113
Steve Naroffddbff782007-08-21 21:17:12 +00001114 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Steve Naroffddbff782007-08-21 21:17:12 +00001116 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001117 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001118 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Steve Naroffddbff782007-08-21 21:17:12 +00001120 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001121 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001122 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001123 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001124 ConsumeToken();
1125 continue;
1126 }
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Steve Naroffddbff782007-08-21 21:17:12 +00001128 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001129 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001130 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001131
1132 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001133 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001134 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001135 }
1136
Steve Naroff861cf3e2007-08-23 18:16:40 +00001137 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001138 case tok::objc_private:
1139 case tok::objc_public:
1140 case tok::objc_protected:
1141 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001142 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001143 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001144 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001145 default:
1146 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001147 continue;
1148 }
1149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001151 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001152 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001153 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001154 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001155 }
1156
John McCallbdd563e2009-11-03 02:38:08 +00001157 struct ObjCIvarCallback : FieldCallback {
1158 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001159 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001160 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001161 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001162
John McCalld226f652010-08-21 09:40:31 +00001163 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001164 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001165 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1166 }
1167
John McCalld226f652010-08-21 09:40:31 +00001168 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001169 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001170 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001171 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001172 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001173 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001174 FD.D, FD.BitfieldSize, visibility);
1175 P.Actions.ActOnObjCContainerFinishDefinition(IDecl);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001176 if (Field)
1177 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001178 return Field;
1179 }
1180 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001181
Chris Lattnere1359422008-04-10 06:46:29 +00001182 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001183 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001184 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattnerdf195262007-10-09 17:51:17 +00001186 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001187 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001188 } else {
1189 Diag(Tok, diag::err_expected_semi_decl_list);
1190 // Skip to end of block or statement
1191 SkipUntil(tok::r_brace, true, true);
1192 }
1193 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001194 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001195 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1196 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
1197 Actions.ActOnObjCContainerFinishDefinition(interfaceDecl);
Steve Naroff8749be52007-10-31 22:11:35 +00001198 // Call ActOnFields() even if we don't have any decls. This is useful
1199 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001200 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001201 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001202 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001203 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204}
Steve Naroffdac269b2007-08-20 21:31:48 +00001205
1206/// objc-protocol-declaration:
1207/// objc-protocol-definition
1208/// objc-protocol-forward-reference
1209///
1210/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001211/// @protocol identifier
1212/// objc-protocol-refs[opt]
1213/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001214/// @end
1215///
1216/// objc-protocol-forward-reference:
1217/// @protocol identifier-list ';'
1218///
1219/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001220/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001221/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001222Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001223 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001224 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001225 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1226 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregor083128f2009-11-18 04:49:41 +00001228 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001229 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001230 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001231 }
1232
Chris Lattnerdf195262007-10-09 17:51:17 +00001233 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001234 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001235 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001236 }
1237 // Save the protocol name, then consume it.
1238 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1239 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattnerdf195262007-10-09 17:51:17 +00001241 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001242 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001243 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001244 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001245 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattnerdf195262007-10-09 17:51:17 +00001248 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001249 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001250 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1251
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001252 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001253 while (1) {
1254 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001255 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001256 Diag(Tok, diag::err_expected_ident);
1257 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001258 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001259 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001260 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1261 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001262 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Chris Lattnerdf195262007-10-09 17:51:17 +00001264 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001265 break;
1266 }
1267 // Consume the ';'.
1268 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001269 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Steve Naroffe440eb82007-10-10 17:32:04 +00001271 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001272 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001273 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001274 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001275 }
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001277 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001278 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001279
Chris Lattner5f9e2722011-07-23 10:55:15 +00001280 SmallVector<Decl *, 8> ProtocolRefs;
1281 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001282 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001283 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1284 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001285 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001286
John McCalld226f652010-08-21 09:40:31 +00001287 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001288 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001289 ProtocolRefs.data(),
1290 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001291 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001292 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001293
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001294 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001295 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001296}
Steve Naroffdac269b2007-08-20 21:31:48 +00001297
1298/// objc-implementation:
1299/// objc-class-implementation-prologue
1300/// objc-category-implementation-prologue
1301///
1302/// objc-class-implementation-prologue:
1303/// @implementation identifier objc-superclass[opt]
1304/// objc-class-instance-variables[opt]
1305///
1306/// objc-category-implementation-prologue:
1307/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001308Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001309 SourceLocation atLoc) {
1310 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1311 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1312 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001314 // Code completion after '@implementation'.
1315 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001316 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001317 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001318 }
1319
Chris Lattnerdf195262007-10-09 17:51:17 +00001320 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001321 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001322 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001323 }
1324 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001325 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001326 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001327
1328 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001329 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001330 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001331 SourceLocation categoryLoc, rparenLoc;
1332 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001334 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001335 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001336 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001337 }
1338
Chris Lattnerdf195262007-10-09 17:51:17 +00001339 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001340 categoryId = Tok.getIdentifierInfo();
1341 categoryLoc = ConsumeToken();
1342 } else {
1343 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001344 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001345 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001346 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347 Diag(Tok, diag::err_expected_rparen);
1348 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001349 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001350 }
1351 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001352 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001353 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001354 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001355
1356 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001357 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001358 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001359 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001360 }
1361 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001362 SourceLocation superClassLoc;
1363 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001364 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001365 // We have a super class
1366 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001367 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001368 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001369 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001370 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001371 superClassId = Tok.getIdentifierInfo();
1372 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001373 }
John McCalld226f652010-08-21 09:40:31 +00001374 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001375 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001376 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Steve Naroff60fccee2007-10-29 21:38:07 +00001378 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001379 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1380
1381 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001382 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001383 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001384 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001385}
Steve Naroff60fccee2007-10-29 21:38:07 +00001386
John McCalld226f652010-08-21 09:40:31 +00001387Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001388 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1389 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001390 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001391 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001392 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001393 Actions.ActOnAtEnd(getCurScope(), atEnd);
1394 Actions.ActOnObjCContainerFinishDefinition(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001395 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001396 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001397 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001398 else {
1399 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001400 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001401 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001402 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001403}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001404
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001405Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1406 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001407 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001408 return Actions.ConvertDeclToDeclGroup(0);
1409 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001410 Actions.ActOnAtEnd(getCurScope(), SourceRange());
1411 Actions.ActOnObjCContainerFinishDefinition(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)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001458 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
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)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001477 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Douglas Gregordc845342010-05-25 05:58:43 +00001478 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001479 }
1480
Chris Lattnerdf195262007-10-09 17:51:17 +00001481 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001482 Diag(Tok, diag::err_expected_ident);
1483 break;
1484 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001485 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001486 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001487 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001488 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001489 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001490 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001491 break;
1492 ConsumeToken(); // consume ','
1493 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001494 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001495 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001496}
1497
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001498/// property-dynamic:
1499/// @dynamic property-list
1500///
1501/// property-list:
1502/// identifier
1503/// property-list ',' identifier
1504///
John McCalld226f652010-08-21 09:40:31 +00001505Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001506 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1507 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001508 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001509 while (true) {
1510 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001511 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001512 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001513 }
1514
1515 if (Tok.isNot(tok::identifier)) {
1516 Diag(Tok, diag::err_expected_ident);
1517 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001518 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001519 }
1520
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001521 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1522 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001523 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001524 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001525
Chris Lattnerdf195262007-10-09 17:51:17 +00001526 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001527 break;
1528 ConsumeToken(); // consume ','
1529 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001530 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001531 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001532}
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001534/// objc-throw-statement:
1535/// throw expression[opt];
1536///
John McCall60d7b3a2010-08-24 06:29:42 +00001537StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1538 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001539 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001540 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001541 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001542 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001543 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001544 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001545 }
1546 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001547 // consume ';'
1548 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001549 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001550}
1551
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001552/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001553/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001554///
John McCall60d7b3a2010-08-24 06:29:42 +00001555StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001556Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001557 ConsumeToken(); // consume synchronized
1558 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001559 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001560 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001561 }
John McCall07524032011-07-27 21:50:02 +00001562
1563 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001564 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001565 ExprResult operand(ParseExpression());
1566
1567 if (Tok.is(tok::r_paren)) {
1568 ConsumeParen(); // ')'
1569 } else {
1570 if (!operand.isInvalid())
1571 Diag(Tok, diag::err_expected_rparen);
1572
1573 // Skip forward until we see a left brace, but don't consume it.
1574 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001575 }
John McCall07524032011-07-27 21:50:02 +00001576
1577 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001578 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001579 if (!operand.isInvalid())
1580 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001581 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001582 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001583
John McCall07524032011-07-27 21:50:02 +00001584 // Check the @synchronized operand now.
1585 if (!operand.isInvalid())
1586 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001587
John McCall07524032011-07-27 21:50:02 +00001588 // Parse the compound statement within a new scope.
1589 ParseScope bodyScope(this, Scope::DeclScope);
1590 StmtResult body(ParseCompoundStatementBody());
1591 bodyScope.Exit();
1592
1593 // If there was a semantic or parse error earlier with the
1594 // operand, fail now.
1595 if (operand.isInvalid())
1596 return StmtError();
1597
1598 if (body.isInvalid())
1599 body = Actions.ActOnNullStmt(Tok.getLocation());
1600
1601 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001602}
1603
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001604/// objc-try-catch-statement:
1605/// @try compound-statement objc-catch-list[opt]
1606/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1607///
1608/// objc-catch-list:
1609/// @catch ( parameter-declaration ) compound-statement
1610/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1611/// catch-parameter-declaration:
1612/// parameter-declaration
1613/// '...' [OBJC2]
1614///
John McCall60d7b3a2010-08-24 06:29:42 +00001615StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001616 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001617
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001618 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001619 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001620 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001621 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001622 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001623 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001624 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001625 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001626 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001627 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001628 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001629 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001630
Chris Lattnerdf195262007-10-09 17:51:17 +00001631 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001632 // At this point, we need to lookahead to determine if this @ is the start
1633 // of an @catch or @finally. We don't want to consume the @ token if this
1634 // is an @try or @encode or something else.
1635 Token AfterAt = GetLookAheadToken(1);
1636 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1637 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1638 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001639
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001640 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001641 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001642 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001643 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001644 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001645 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001646 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001647 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001648 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001649 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001650 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001651 ParseDeclarator(ParmDecl);
1652
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001653 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001654 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001655 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001656 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001657 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Steve Naroff93a25952009-04-07 22:56:58 +00001659 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Steve Naroff93a25952009-04-07 22:56:58 +00001661 if (Tok.is(tok::r_paren))
1662 RParenLoc = ConsumeParen();
1663 else // Skip over garbage, until we get to ')'. Eat the ')'.
1664 SkipUntil(tok::r_paren, true, false);
1665
John McCall60d7b3a2010-08-24 06:29:42 +00001666 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001667 if (Tok.is(tok::l_brace))
1668 CatchBody = ParseCompoundStatementBody();
1669 else
1670 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001671 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001672 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001673
John McCall60d7b3a2010-08-24 06:29:42 +00001674 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001675 RParenLoc,
1676 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001677 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001678 if (!Catch.isInvalid())
1679 CatchStmts.push_back(Catch.release());
1680
Steve Naroff64515f32008-02-05 21:27:35 +00001681 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001682 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1683 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001684 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001685 }
1686 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001687 } else {
1688 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001689 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001690 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001691
John McCall60d7b3a2010-08-24 06:29:42 +00001692 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001693 if (Tok.is(tok::l_brace))
1694 FinallyBody = ParseCompoundStatementBody();
1695 else
1696 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001697 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001698 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001699 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001700 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001701 catch_or_finally_seen = true;
1702 break;
1703 }
1704 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001705 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001706 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001707 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001708 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001709
John McCall9ae2f072010-08-23 23:25:46 +00001710 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001711 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001712 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001713}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001714
John McCallf85e1932011-06-15 23:02:42 +00001715/// objc-autoreleasepool-statement:
1716/// @autoreleasepool compound-statement
1717///
1718StmtResult
1719Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1720 ConsumeToken(); // consume autoreleasepool
1721 if (Tok.isNot(tok::l_brace)) {
1722 Diag(Tok, diag::err_expected_lbrace);
1723 return StmtError();
1724 }
1725 // Enter a scope to hold everything within the compound stmt. Compound
1726 // statements can always hold declarations.
1727 ParseScope BodyScope(this, Scope::DeclScope);
1728
1729 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1730
1731 BodyScope.Exit();
1732 if (AutoreleasePoolBody.isInvalid())
1733 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1734 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1735 AutoreleasePoolBody.take());
1736}
1737
Steve Naroff3536b442007-09-06 21:24:23 +00001738/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001739///
John McCalld226f652010-08-21 09:40:31 +00001740Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001741 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001742
John McCallf312b1e2010-08-26 23:41:50 +00001743 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1744 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001746 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001747 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001748 if (ObjCImpDecl) {
1749 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001750 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001751 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001752 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001753 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001754
Steve Naroff409be832007-11-11 19:54:21 +00001755 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001756 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001757 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Steve Naroff409be832007-11-11 19:54:21 +00001759 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1760 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Steve Naroff409be832007-11-11 19:54:21 +00001762 // If we didn't find the '{', bail out.
1763 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001764 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001765 }
Steve Naroff409be832007-11-11 19:54:21 +00001766 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Steve Naroff409be832007-11-11 19:54:21 +00001768 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001769 ParseScope BodyScope(this,
1770 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Steve Naroff409be832007-11-11 19:54:21 +00001772 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001773 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001774 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001775
Douglas Gregorc9977d02011-03-16 17:05:57 +00001776 if (PP.isCodeCompletionEnabled()) {
1777 if (trySkippingFunctionBodyForCodeCompletion()) {
1778 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001779 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001780 }
1781 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001782
John McCall60d7b3a2010-08-24 06:29:42 +00001783 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001784
Steve Naroff409be832007-11-11 19:54:21 +00001785 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001786 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001787 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1788 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001789
Steve Naroff409be832007-11-11 19:54:21 +00001790 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001791 BodyScope.Exit();
Douglas Gregorc9977d02011-03-16 17:05:57 +00001792
1793 // TODO: Pass argument information.
1794 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Steve Naroff71c0a952007-11-13 23:01:27 +00001795 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001796}
Anders Carlsson55085182007-08-21 17:43:55 +00001797
John McCall60d7b3a2010-08-24 06:29:42 +00001798StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001799 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001800 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001801 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001802 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001803 }
1804
1805 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001806 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001807
1808 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001809 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001810
1811 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001812 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001813
1814 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1815 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001816
John McCall60d7b3a2010-08-24 06:29:42 +00001817 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001818 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001819 // If the expression is invalid, skip ahead to the next semicolon. Not
1820 // doing this opens us up to the possibility of infinite loops if
1821 // ParseExpression does not consume any tokens.
1822 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001823 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001824 }
Chris Lattner5d803162009-12-07 16:33:19 +00001825
Steve Naroff64515f32008-02-05 21:27:35 +00001826 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001827 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001828 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001829}
1830
John McCall60d7b3a2010-08-24 06:29:42 +00001831ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001832 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001833 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001834 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001835 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001836 return ExprError();
1837
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001838 case tok::string_literal: // primary-expression: string-literal
1839 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001840 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001841 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001842 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001843 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001844
Chris Lattner4fef81d2008-08-05 06:19:09 +00001845 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1846 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001847 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001848 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001849 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001850 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001851 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001852 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001853 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001854 }
Anders Carlsson55085182007-08-21 17:43:55 +00001855 }
Anders Carlsson55085182007-08-21 17:43:55 +00001856}
1857
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001858/// \brirg Parse the receiver of an Objective-C++ message send.
1859///
1860/// This routine parses the receiver of a message send in
1861/// Objective-C++ either as a type or as an expression. Note that this
1862/// routine must not be called to parse a send to 'super', since it
1863/// has no way to return such a result.
1864///
1865/// \param IsExpr Whether the receiver was parsed as an expression.
1866///
1867/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1868/// IsExpr is true), the parsed expression. If the receiver was parsed
1869/// as a type (\c IsExpr is false), the parsed type.
1870///
1871/// \returns True if an error occurred during parsing or semantic
1872/// analysis, in which case the arguments do not have valid
1873/// values. Otherwise, returns false for a successful parse.
1874///
1875/// objc-receiver: [C++]
1876/// 'super' [not parsed here]
1877/// expression
1878/// simple-type-specifier
1879/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001880bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001881 InMessageExpressionRAIIObject InMessage(*this, true);
1882
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001883 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1884 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1885 TryAnnotateTypeOrScopeToken();
1886
1887 if (!isCXXSimpleTypeSpecifier()) {
1888 // objc-receiver:
1889 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001890 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001891 if (Receiver.isInvalid())
1892 return true;
1893
1894 IsExpr = true;
1895 TypeOrExpr = Receiver.take();
1896 return false;
1897 }
1898
1899 // objc-receiver:
1900 // typename-specifier
1901 // simple-type-specifier
1902 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001903 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001904 ParseCXXSimpleTypeSpecifier(DS);
1905
1906 if (Tok.is(tok::l_paren)) {
1907 // If we see an opening parentheses at this point, we are
1908 // actually parsing an expression that starts with a
1909 // function-style cast, e.g.,
1910 //
1911 // postfix-expression:
1912 // simple-type-specifier ( expression-list [opt] )
1913 // typename-specifier ( expression-list [opt] )
1914 //
1915 // Parse the remainder of this case, then the (optional)
1916 // postfix-expression suffix, followed by the (optional)
1917 // right-hand side of the binary expression. We have an
1918 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001919 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001920 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001921 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001922 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001923 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001924 if (Receiver.isInvalid())
1925 return true;
1926
1927 IsExpr = true;
1928 TypeOrExpr = Receiver.take();
1929 return false;
1930 }
1931
1932 // We have a class message. Turn the simple-type-specifier or
1933 // typename-specifier we parsed into a type and parse the
1934 // remainder of the class message.
1935 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001936 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001937 if (Type.isInvalid())
1938 return true;
1939
1940 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001941 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001942 return false;
1943}
1944
Douglas Gregor1b730e82010-05-31 14:40:22 +00001945/// \brief Determine whether the parser is currently referring to a an
1946/// Objective-C message send, using a simplified heuristic to avoid overhead.
1947///
1948/// This routine will only return true for a subset of valid message-send
1949/// expressions.
1950bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001951 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001952 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001953 return GetLookAheadToken(1).is(tok::identifier) &&
1954 GetLookAheadToken(2).is(tok::identifier);
1955}
1956
Douglas Gregor9497a732010-09-16 01:51:54 +00001957bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1958 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1959 InMessageExpression)
1960 return false;
1961
1962
1963 ParsedType Type;
1964
1965 if (Tok.is(tok::annot_typename))
1966 Type = getTypeAnnotation(Tok);
1967 else if (Tok.is(tok::identifier))
1968 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1969 getCurScope());
1970 else
1971 return false;
1972
1973 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1974 const Token &AfterNext = GetLookAheadToken(2);
1975 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1976 if (Tok.is(tok::identifier))
1977 TryAnnotateTypeOrScopeToken();
1978
1979 return Tok.is(tok::annot_typename);
1980 }
1981 }
1982
1983 return false;
1984}
1985
Mike Stump1eb44332009-09-09 15:08:12 +00001986/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001987/// '[' objc-receiver objc-message-args ']'
1988///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001989/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001990/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001991/// expression
1992/// class-name
1993/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001994///
John McCall60d7b3a2010-08-24 06:29:42 +00001995ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001996 assert(Tok.is(tok::l_square) && "'[' expected");
1997 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1998
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001999 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002000 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002001 ConsumeCodeCompletionToken();
2002 SkipUntil(tok::r_square);
2003 return ExprError();
2004 }
2005
Douglas Gregor0fbda682010-09-15 14:51:05 +00002006 InMessageExpressionRAIIObject InMessage(*this, true);
2007
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002008 if (getLang().CPlusPlus) {
2009 // We completely separate the C and C++ cases because C++ requires
2010 // more complicated (read: slower) parsing.
2011
2012 // Handle send to super.
2013 // FIXME: This doesn't benefit from the same typo-correction we
2014 // get in Objective-C.
2015 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002016 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002017 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2018 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002019
2020 // Parse the receiver, which is either a type or an expression.
2021 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002022 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002023 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2024 SkipUntil(tok::r_square);
2025 return ExprError();
2026 }
2027
2028 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002029 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2030 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002031 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002032
2033 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002034 ParsedType::getFromOpaquePtr(TypeOrExpr),
2035 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002036 }
2037
2038 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002039 IdentifierInfo *Name = Tok.getIdentifierInfo();
2040 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002041 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002042 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002043 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002044 NextToken().is(tok::period),
2045 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002046 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002047 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2048 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002049
John McCallf312b1e2010-08-26 23:41:50 +00002050 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002051 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002052 SkipUntil(tok::r_square);
2053 return ExprError();
2054 }
2055
Douglas Gregor1569f952010-04-21 20:38:13 +00002056 ConsumeToken(); // the type name
2057
2058 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002059 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002060
John McCallf312b1e2010-08-26 23:41:50 +00002061 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002062 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002063 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002064 }
Chris Lattner699b6612008-01-25 18:59:06 +00002065 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002066
2067 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002068 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002069 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002070 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002071 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002072 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002073
John McCallb3d87482010-08-24 05:47:05 +00002074 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2075 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002076}
Sebastian Redl1d922962008-12-13 15:32:12 +00002077
Douglas Gregor2725ca82010-04-21 19:57:20 +00002078/// \brief Parse the remainder of an Objective-C message following the
2079/// '[' objc-receiver.
2080///
2081/// This routine handles sends to super, class messages (sent to a
2082/// class name), and instance messages (sent to an object), and the
2083/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2084/// ReceiverExpr, respectively. Only one of these parameters may have
2085/// a valid value.
2086///
2087/// \param LBracLoc The location of the opening '['.
2088///
2089/// \param SuperLoc If this is a send to 'super', the location of the
2090/// 'super' keyword that indicates a send to the superclass.
2091///
2092/// \param ReceiverType If this is a class message, the type of the
2093/// class we are sending a message to.
2094///
2095/// \param ReceiverExpr If this is an instance message, the expression
2096/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002097///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002098/// objc-message-args:
2099/// objc-selector
2100/// objc-keywordarg-list
2101///
2102/// objc-keywordarg-list:
2103/// objc-keywordarg
2104/// objc-keywordarg-list objc-keywordarg
2105///
Mike Stump1eb44332009-09-09 15:08:12 +00002106/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002107/// selector-name[opt] ':' objc-keywordexpr
2108///
2109/// objc-keywordexpr:
2110/// nonempty-expr-list
2111///
2112/// nonempty-expr-list:
2113/// assignment-expression
2114/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002115///
John McCall60d7b3a2010-08-24 06:29:42 +00002116ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002117Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002118 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002119 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002120 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002121 InMessageExpressionRAIIObject InMessage(*this, true);
2122
Steve Naroffc4df6d22009-11-07 02:08:14 +00002123 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002124 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002125 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2126 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002127 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002128 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2129 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002130 else
John McCall9ae2f072010-08-23 23:25:46 +00002131 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002132 0, 0, false);
Douglas Gregordc845342010-05-25 05:58:43 +00002133 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002134 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002135
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002136 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002137 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002138 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002139
Anders Carlssonff975cf2009-02-14 18:21:46 +00002140 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Chris Lattner5f9e2722011-07-23 10:55:15 +00002142 SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002143 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002144
Chris Lattnerdf195262007-10-09 17:51:17 +00002145 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002146 while (1) {
2147 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002148 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002149
Chris Lattnerdf195262007-10-09 17:51:17 +00002150 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002151 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002152 // We must manually skip to a ']', otherwise the expression skipper will
2153 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2154 // the enclosing expression.
2155 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002156 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002157 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002158
Steve Naroff68d331a2007-09-27 14:38:14 +00002159 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002160 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002161
2162 if (Tok.is(tok::code_completion)) {
2163 if (SuperLoc.isValid())
2164 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2165 KeyIdents.data(),
2166 KeyIdents.size(),
2167 /*AtArgumentEpression=*/true);
2168 else if (ReceiverType)
2169 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2170 KeyIdents.data(),
2171 KeyIdents.size(),
2172 /*AtArgumentEpression=*/true);
2173 else
2174 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2175 KeyIdents.data(),
2176 KeyIdents.size(),
2177 /*AtArgumentEpression=*/true);
2178
2179 ConsumeCodeCompletionToken();
2180 SkipUntil(tok::r_square);
2181 return ExprError();
2182 }
2183
John McCall60d7b3a2010-08-24 06:29:42 +00002184 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002185 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002186 // We must manually skip to a ']', otherwise the expression skipper will
2187 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2188 // the enclosing expression.
2189 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002190 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002191 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002192
Steve Naroff37387c92007-09-17 20:25:27 +00002193 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002194 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002195
Douglas Gregord3c68542009-11-19 01:08:35 +00002196 // Code completion after each argument.
2197 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002198 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002199 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002200 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002201 KeyIdents.size(),
2202 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002203 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002204 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002205 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002206 KeyIdents.size(),
2207 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002208 else
John McCall9ae2f072010-08-23 23:25:46 +00002209 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002210 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002211 KeyIdents.size(),
2212 /*AtArgumentEpression=*/false);
Douglas Gregordc845342010-05-25 05:58:43 +00002213 ConsumeCodeCompletionToken();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002214 SkipUntil(tok::r_square);
2215 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002216 }
2217
Steve Naroff37387c92007-09-17 20:25:27 +00002218 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002219 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002220 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002221 break;
2222 // We have a selector or a colon, continue parsing.
2223 }
2224 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002225 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002226 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002227 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002228 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002229 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002230 // We must manually skip to a ']', otherwise the expression skipper will
2231 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2232 // the enclosing expression.
2233 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002234 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002235 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002236
Steve Naroff49f109c2007-11-15 13:05:42 +00002237 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002238 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002239 }
2240 } else if (!selIdent) {
2241 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002242
Chris Lattner4fef81d2008-08-05 06:19:09 +00002243 // We must manually skip to a ']', otherwise the expression skipper will
2244 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2245 // the enclosing expression.
2246 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002247 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002248 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002249
Chris Lattnerdf195262007-10-09 17:51:17 +00002250 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002251 if (Tok.is(tok::identifier))
2252 Diag(Tok, diag::err_expected_colon);
2253 else
2254 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002255 // We must manually skip to a ']', otherwise the expression skipper will
2256 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2257 // the enclosing expression.
2258 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002259 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002260 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002261
Chris Lattner699b6612008-01-25 18:59:06 +00002262 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002263
Steve Naroff29238a02007-10-05 18:42:47 +00002264 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002265 if (nKeys == 0)
2266 KeyIdents.push_back(selIdent);
2267 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002268
Douglas Gregor2725ca82010-04-21 19:57:20 +00002269 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002270 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002271 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002272 MultiExprArg(Actions,
2273 KeyExprs.take(),
2274 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002275 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002276 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002277 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002278 MultiExprArg(Actions,
2279 KeyExprs.take(),
2280 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002281 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002282 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002283 MultiExprArg(Actions,
2284 KeyExprs.take(),
2285 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002286}
2287
John McCall60d7b3a2010-08-24 06:29:42 +00002288ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2289 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002290 if (Res.isInvalid()) return move(Res);
2291
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002292 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2293 // expressions. At this point, we know that the only valid thing that starts
2294 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002295 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002296 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002297 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002298 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002299
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002300 while (Tok.is(tok::at)) {
2301 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002302
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002303 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002304 if (!isTokenStringLiteral())
2305 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002306
John McCall60d7b3a2010-08-24 06:29:42 +00002307 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002308 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002309 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002310
Sebastian Redleffa8d12008-12-10 00:02:53 +00002311 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002312 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002313
2314 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2315 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002316}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002317
2318/// objc-encode-expression:
2319/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002320ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002321Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002322 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002323
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002324 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002325
Chris Lattner4fef81d2008-08-05 06:19:09 +00002326 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002327 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2328
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002329 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002330
Douglas Gregor809070a2009-02-18 17:45:20 +00002331 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002332
Anders Carlsson4988ae32007-08-23 15:31:37 +00002333 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002334
Douglas Gregor809070a2009-02-18 17:45:20 +00002335 if (Ty.isInvalid())
2336 return ExprError();
2337
Mike Stump1eb44332009-09-09 15:08:12 +00002338 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002339 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002340}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002341
2342/// objc-protocol-expression
2343/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002344ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002345Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002346 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002347
Chris Lattner4fef81d2008-08-05 06:19:09 +00002348 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002349 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2350
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002351 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002352
Chris Lattner4fef81d2008-08-05 06:19:09 +00002353 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002354 return ExprError(Diag(Tok, diag::err_expected_ident));
2355
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002356 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002357 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002358
Anders Carlsson4988ae32007-08-23 15:31:37 +00002359 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002360
Sebastian Redl1d922962008-12-13 15:32:12 +00002361 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2362 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002363}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002364
2365/// objc-selector-expression
2366/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002367ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002368 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002369
Chris Lattner4fef81d2008-08-05 06:19:09 +00002370 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002371 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2372
Chris Lattner5f9e2722011-07-23 10:55:15 +00002373 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002374 SourceLocation LParenLoc = ConsumeParen();
2375 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002376
2377 if (Tok.is(tok::code_completion)) {
2378 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2379 KeyIdents.size());
2380 ConsumeCodeCompletionToken();
2381 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2382 return ExprError();
2383 }
2384
Chris Lattner2fc5c242009-04-11 18:13:45 +00002385 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002386 if (!SelIdent && // missing selector name.
2387 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002388 return ExprError(Diag(Tok, diag::err_expected_ident));
2389
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002390 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002391 unsigned nColons = 0;
2392 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002393 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002394 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2395 ++nColons;
2396 KeyIdents.push_back(0);
2397 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002398 return ExprError(Diag(Tok, diag::err_expected_colon));
2399
Chris Lattner5add7542010-08-27 22:32:41 +00002400 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002401 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002402 if (Tok.is(tok::r_paren))
2403 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002404
2405 if (Tok.is(tok::code_completion)) {
2406 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2407 KeyIdents.size());
2408 ConsumeCodeCompletionToken();
2409 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2410 return ExprError();
2411 }
2412
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002413 // Check for another keyword selector.
2414 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002415 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002416 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002417 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002418 break;
2419 }
Steve Naroff887407e2007-12-05 22:21:29 +00002420 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002421 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002422 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002423 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2424 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002425 }