blob: a8c18c01a291421a0e86fa8f20c5b31ef61539d4 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
23
Chris Lattner891dca62008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
John McCalld226f652010-08-21 09:40:31 +000032Decl *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregorc464ae82009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregordc845342010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +000038 }
39
Steve Naroff861cf3e2007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000041 case tok::objc_class:
42 return ParseObjCAtClassDeclaration(AtLoc);
John McCall7f040a92010-12-24 02:08:15 +000043 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000044 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000045 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
46 }
47 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000048 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000049 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
50 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000051 case tok::objc_implementation:
52 return ParseObjCAtImplementationDeclaration(AtLoc);
53 case tok::objc_end:
54 return ParseObjCAtEndDeclaration(AtLoc);
55 case tok::objc_compatibility_alias:
56 return ParseObjCAtAliasDeclaration(AtLoc);
57 case tok::objc_synthesize:
58 return ParseObjCPropertySynthesize(AtLoc);
59 case tok::objc_dynamic:
60 return ParseObjCPropertyDynamic(AtLoc);
61 default:
62 Diag(AtLoc, diag::err_unexpected_at);
63 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000064 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 }
66}
67
68///
Mike Stump1eb44332009-09-09 15:08:12 +000069/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000070/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000071///
John McCalld226f652010-08-21 09:40:31 +000072Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000073 ConsumeToken(); // the identifier "class"
74 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000075 llvm::SmallVector<SourceLocation, 8> ClassLocs;
76
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;
John McCalld226f652010-08-21 09:40:31 +0000180 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000181 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
182 if (Tok.is(tok::less) &&
183 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000184 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000185 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
John McCall7f040a92010-12-24 02:08:15 +0000187 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000188 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
John McCalld226f652010-08-21 09:40:31 +0000190 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000191 Actions.ActOnStartCategoryInterface(atLoc,
192 nameId, nameLoc,
193 categoryId, categoryLoc,
194 ProtocolRefs.data(),
195 ProtocolRefs.size(),
196 ProtocolLocs.data(),
197 EndProtoLoc);
198 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000199 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
200 atLoc);
201
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000202 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
203 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 }
205 // Parse a class interface.
206 IdentifierInfo *superClassId = 0;
207 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000208
Chris Lattnerdf195262007-10-09 17:51:17 +0000209 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000210 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000211
212 // Code completion of superclass names.
213 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000214 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000215 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000216 }
217
Chris Lattnerdf195262007-10-09 17:51:17 +0000218 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000219 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000220 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000221 }
222 superClassId = Tok.getIdentifierInfo();
223 superClassLoc = ConsumeToken();
224 }
225 // Next, we need to check for any protocol references.
John McCalld226f652010-08-21 09:40:31 +0000226 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000227 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
228 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000229 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000230 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
231 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000232 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
John McCalld226f652010-08-21 09:40:31 +0000234 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000235 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000236 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000237 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000238 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000239 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattnerdf195262007-10-09 17:51:17 +0000241 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000242 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000243
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000244 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000245 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000246}
247
John McCalld0014542009-12-03 22:31:13 +0000248/// The Objective-C property callback. This should be defined where
249/// it's used, but instead it's been lifted to here to support VS2005.
250struct Parser::ObjCPropertyCallback : FieldCallback {
251 Parser &P;
John McCalld226f652010-08-21 09:40:31 +0000252 Decl *IDecl;
253 llvm::SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000254 ObjCDeclSpec &OCDS;
255 SourceLocation AtLoc;
256 tok::ObjCKeywordKind MethodImplKind;
257
John McCalld226f652010-08-21 09:40:31 +0000258 ObjCPropertyCallback(Parser &P, Decl *IDecl,
259 llvm::SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000260 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
261 tok::ObjCKeywordKind MethodImplKind) :
262 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
263 MethodImplKind(MethodImplKind) {
264 }
265
John McCalld226f652010-08-21 09:40:31 +0000266 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000267 if (FD.D.getIdentifier() == 0) {
268 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
269 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000270 return 0;
John McCalld0014542009-12-03 22:31:13 +0000271 }
272 if (FD.BitfieldSize) {
273 P.Diag(AtLoc, diag::err_objc_property_bitfield)
274 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000275 return 0;
John McCalld0014542009-12-03 22:31:13 +0000276 }
277
278 // Install the property declarator into interfaceDecl.
279 IdentifierInfo *SelName =
280 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
281
282 Selector GetterSel =
283 P.PP.getSelectorTable().getNullarySelector(SelName);
284 IdentifierInfo *SetterName = OCDS.getSetterName();
285 Selector SetterSel;
286 if (SetterName)
287 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
288 else
289 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
290 P.PP.getSelectorTable(),
291 FD.D.getIdentifier());
292 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000293 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000294 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCalld0014542009-12-03 22:31:13 +0000295 GetterSel, SetterSel, IDecl,
296 &isOverridingProperty,
297 MethodImplKind);
298 if (!isOverridingProperty)
299 Props.push_back(Property);
300
301 return Property;
302 }
303};
304
Steve Naroffdac269b2007-08-20 21:31:48 +0000305/// objc-interface-decl-list:
306/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000307/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000308/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000309/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000310/// objc-interface-decl-list declaration
311/// objc-interface-decl-list ';'
312///
Steve Naroff294494e2007-08-22 16:35:03 +0000313/// objc-method-requirement: [OBJC2]
314/// @required
315/// @optional
316///
John McCalld226f652010-08-21 09:40:31 +0000317void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000318 tok::ObjCKeywordKind contextKey) {
John McCalld226f652010-08-21 09:40:31 +0000319 llvm::SmallVector<Decl *, 32> allMethods;
320 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000321 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000322 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenek782f2f52010-01-07 01:20:12 +0000324 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000325
Steve Naroff294494e2007-08-22 16:35:03 +0000326 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000327 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000328 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000329 Decl *methodPrototype =
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000330 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000331 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000332 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
333 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000334 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
335 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000336 continue;
337 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000338 if (Tok.is(tok::l_paren)) {
339 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000340 ParseObjCMethodDecl(Tok.getLocation(),
341 tok::minus,
342 interfaceDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000343 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000344 continue;
345 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000346 // Ignore excess semicolons.
347 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000348 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000349 continue;
350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattnerbc662af2008-10-20 06:10:06 +0000352 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000353 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000354 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000356 // Code completion within an Objective-C interface.
357 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000358 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000359 ObjCImpDecl? Sema::PCC_ObjCImplementation
360 : Sema::PCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000361 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000362 }
363
Chris Lattnere82a10f2008-10-20 05:46:22 +0000364 // If we don't have an @ directive, parse it as a function definition.
365 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000366 // The code below does not consume '}'s because it is afraid of eating the
367 // end of a namespace. Because of the way this code is structured, an
368 // erroneous r_brace would cause an infinite loop if not handled here.
369 if (Tok.is(tok::r_brace))
370 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Steve Naroff4985ace2007-08-22 18:35:33 +0000372 // FIXME: as the name implies, this rule allows function definitions.
373 // We could pass a flag or check for functions during semantic analysis.
John McCall0b7e6782011-03-24 11:26:52 +0000374 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000375 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000376 continue;
377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattnere82a10f2008-10-20 05:46:22 +0000379 // Otherwise, we have an @ directive, eat the @.
380 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000381 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000382 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000383 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000384 break;
385 }
386
Chris Lattnera2449b22008-10-20 05:57:40 +0000387 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattnera2449b22008-10-20 05:57:40 +0000389 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000390 AtEnd.setBegin(AtLoc);
391 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000392 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000393 } else if (DirectiveKind == tok::objc_not_keyword) {
394 Diag(Tok, diag::err_objc_unknown_at);
395 SkipUntil(tok::semi);
396 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnerbc662af2008-10-20 06:10:06 +0000399 // Eat the identifier.
400 ConsumeToken();
401
Chris Lattnera2449b22008-10-20 05:57:40 +0000402 switch (DirectiveKind) {
403 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000404 // FIXME: If someone forgets an @end on a protocol, this loop will
405 // continue to eat up tons of stuff and spew lots of nonsense errors. It
406 // would probably be better to bail out if we saw an @class or @interface
407 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000408 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000409 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000410 SkipUntil(tok::r_brace, tok::at);
411 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000412
413 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000414 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000415 Diag(Tok, diag::err_objc_missing_end);
416 ConsumeToken();
417 break;
418
Chris Lattnera2449b22008-10-20 05:57:40 +0000419 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000420 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000421 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000422 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000423 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000424 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000425 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000426 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000427 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattnera2449b22008-10-20 05:57:40 +0000429 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000430 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000431 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000432
Chris Lattnere82a10f2008-10-20 05:46:22 +0000433 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000435 if (Tok.is(tok::l_paren))
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000436 ParseObjCPropertyAttribute(OCDS, interfaceDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
John McCalld0014542009-12-03 22:31:13 +0000438 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
439 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000440
Chris Lattnere82a10f2008-10-20 05:46:22 +0000441 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000442 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000443 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
John McCall7da19ea2011-03-26 01:53:26 +0000445 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000446 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000447 }
Steve Naroff294494e2007-08-22 16:35:03 +0000448 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000449
450 // We break out of the big loop in two cases: when we see @end or when we see
451 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000452 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000453 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000454 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000455 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000456 ConsumeToken(); // the "end" identifier
457 else
458 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattnera2449b22008-10-20 05:57:40 +0000460 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000461 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000462 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000463 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000464 allProperties.data(), allProperties.size(),
465 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000466}
467
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000468/// Parse property attribute declarations.
469///
470/// property-attr-decl: '(' property-attrlist ')'
471/// property-attrlist:
472/// property-attribute
473/// property-attrlist ',' property-attribute
474/// property-attribute:
475/// getter '=' identifier
476/// setter '=' identifier ':'
477/// readonly
478/// readwrite
479/// assign
480/// retain
481/// copy
482/// nonatomic
483///
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000484void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000485 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000486 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000488 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000489 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000490 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000491 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000492 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000493 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000495 // If this is not an identifier at all, bail out early.
496 if (II == 0) {
497 MatchRHSPunctuation(tok::r_paren, LHSLoc);
498 return;
499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Chris Lattner156b0612008-10-20 07:37:22 +0000501 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner92e62b02008-11-20 04:42:34 +0000503 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000504 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000505 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000506 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000507 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000508 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000509 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000510 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000511 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000513 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000514 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000515 else if (II->isStr("atomic"))
516 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000517 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000518 bool IsSetter = II->getNameStart()[0] == 's';
519
Chris Lattnere00da7c2008-10-20 07:39:53 +0000520 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000521 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
522 diag::err_objc_expected_equal_for_getter;
523
524 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000525 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Douglas Gregor4ad96852009-11-19 07:41:15 +0000527 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000528 if (IsSetter)
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000529 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
Douglas Gregor4ad96852009-11-19 07:41:15 +0000530 else
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000531 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000532 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000533 }
534
Anders Carlsson42499be2010-10-02 17:45:21 +0000535
536 SourceLocation SelLoc;
537 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
538
539 if (!SelIdent) {
540 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
541 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000542 SkipUntil(tok::r_paren);
543 return;
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlsson42499be2010-10-02 17:45:21 +0000546 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000547 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000548 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000550 if (ExpectAndConsume(tok::colon,
551 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000552 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000553 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000554 } else {
555 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000556 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000557 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000558 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000559 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000560 SkipUntil(tok::r_paren);
561 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner156b0612008-10-20 07:37:22 +0000564 if (Tok.isNot(tok::comma))
565 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner156b0612008-10-20 07:37:22 +0000567 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner156b0612008-10-20 07:37:22 +0000570 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000571}
572
Steve Naroff3536b442007-09-06 21:24:23 +0000573/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000574/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000575/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000576///
577/// objc-instance-method: '-'
578/// objc-class-method: '+'
579///
Steve Naroff4985ace2007-08-22 18:35:33 +0000580/// objc-method-attributes: [OBJC2]
581/// __attribute__((deprecated))
582///
John McCalld226f652010-08-21 09:40:31 +0000583Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000584 tok::ObjCKeywordKind MethodImplKind,
585 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000586 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000587
Mike Stump1eb44332009-09-09 15:08:12 +0000588 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000589 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000590 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind,
591 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000592 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000593 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000594 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000595}
596
597/// objc-selector:
598/// identifier
599/// one of
600/// enum struct union if else while do for switch case default
601/// break continue return goto asm sizeof typeof __alignof
602/// unsigned long const short volatile signed restrict _Complex
603/// in out inout bycopy byref oneway int char float double void _Bool
604///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000605IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000606
Chris Lattnerff384912007-10-07 02:00:24 +0000607 switch (Tok.getKind()) {
608 default:
609 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000610 case tok::ampamp:
611 case tok::ampequal:
612 case tok::amp:
613 case tok::pipe:
614 case tok::tilde:
615 case tok::exclaim:
616 case tok::exclaimequal:
617 case tok::pipepipe:
618 case tok::pipeequal:
619 case tok::caret:
620 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000621 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000622 if (isalpha(ThisTok[0])) {
623 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
624 Tok.setKind(tok::identifier);
625 SelectorLoc = ConsumeToken();
626 return II;
627 }
628 return 0;
629 }
630
Chris Lattnerff384912007-10-07 02:00:24 +0000631 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000632 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000633 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000634 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000635 case tok::kw_break:
636 case tok::kw_case:
637 case tok::kw_catch:
638 case tok::kw_char:
639 case tok::kw_class:
640 case tok::kw_const:
641 case tok::kw_const_cast:
642 case tok::kw_continue:
643 case tok::kw_default:
644 case tok::kw_delete:
645 case tok::kw_do:
646 case tok::kw_double:
647 case tok::kw_dynamic_cast:
648 case tok::kw_else:
649 case tok::kw_enum:
650 case tok::kw_explicit:
651 case tok::kw_export:
652 case tok::kw_extern:
653 case tok::kw_false:
654 case tok::kw_float:
655 case tok::kw_for:
656 case tok::kw_friend:
657 case tok::kw_goto:
658 case tok::kw_if:
659 case tok::kw_inline:
660 case tok::kw_int:
661 case tok::kw_long:
662 case tok::kw_mutable:
663 case tok::kw_namespace:
664 case tok::kw_new:
665 case tok::kw_operator:
666 case tok::kw_private:
667 case tok::kw_protected:
668 case tok::kw_public:
669 case tok::kw_register:
670 case tok::kw_reinterpret_cast:
671 case tok::kw_restrict:
672 case tok::kw_return:
673 case tok::kw_short:
674 case tok::kw_signed:
675 case tok::kw_sizeof:
676 case tok::kw_static:
677 case tok::kw_static_cast:
678 case tok::kw_struct:
679 case tok::kw_switch:
680 case tok::kw_template:
681 case tok::kw_this:
682 case tok::kw_throw:
683 case tok::kw_true:
684 case tok::kw_try:
685 case tok::kw_typedef:
686 case tok::kw_typeid:
687 case tok::kw_typename:
688 case tok::kw_typeof:
689 case tok::kw_union:
690 case tok::kw_unsigned:
691 case tok::kw_using:
692 case tok::kw_virtual:
693 case tok::kw_void:
694 case tok::kw_volatile:
695 case tok::kw_wchar_t:
696 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000697 case tok::kw__Bool:
698 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000699 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000700 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000701 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000702 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000703 }
Steve Naroff294494e2007-08-22 16:35:03 +0000704}
705
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000706/// objc-for-collection-in: 'in'
707///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000708bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000709 // FIXME: May have to do additional look-ahead to only allow for
710 // valid tokens following an 'in'; such as an identifier, unary operators,
711 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000712 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000713 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000714}
715
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000716/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000717/// qualifier list and builds their bitmask representation in the input
718/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000719///
720/// objc-type-qualifiers:
721/// objc-type-qualifier
722/// objc-type-qualifiers objc-type-qualifier
723///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000724void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
725 ObjCTypeNameContext Context) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000726 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000727 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000728 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
729 Context == OTN_ParameterType);
Douglas Gregord32b0222010-08-24 01:06:58 +0000730 ConsumeCodeCompletionToken();
731 }
732
Chris Lattnercb53b362007-12-27 19:57:00 +0000733 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000734 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Chris Lattnere8b724d2007-12-12 06:56:32 +0000736 const IdentifierInfo *II = Tok.getIdentifierInfo();
737 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000738 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000739 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000742 switch (i) {
743 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
745 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
746 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
747 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
748 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
749 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000750 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000752 ConsumeToken();
753 II = 0;
754 break;
755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattnere8b724d2007-12-12 06:56:32 +0000757 // If this wasn't a recognized qualifier, bail out.
758 if (II) return;
759 }
760}
761
762/// objc-type-name:
763/// '(' objc-type-qualifiers[opt] type-name ')'
764/// '(' objc-type-qualifiers[opt] ')'
765///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000766ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
767 ObjCTypeNameContext Context) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000768 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattner4a76b292008-10-22 03:52:06 +0000770 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000771 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000773 // Parse type qualifiers, in, inout, etc.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000774 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000775
John McCallb3d87482010-08-24 05:47:05 +0000776 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000777 if (isTypeSpecifierQualifier()) {
John McCallc05a94b2011-03-23 23:43:04 +0000778 TypeResult TypeSpec = ParseTypeName(0, Declarator::ObjCPrototypeContext);
Douglas Gregor809070a2009-02-18 17:45:20 +0000779 if (!TypeSpec.isInvalid())
780 Ty = TypeSpec.get();
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Steve Naroffd7333c22008-10-21 14:15:04 +0000783 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000784 ConsumeParen();
785 else if (Tok.getLocation() == TypeStartLoc) {
786 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000787 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000788 SkipUntil(tok::r_paren);
789 } else {
790 // Otherwise, we found *something*, but didn't get a ')' in the right
791 // place. Emit an error then return what we have as the type.
792 MatchRHSPunctuation(tok::r_paren, LParenLoc);
793 }
Steve Narofff28b2642007-09-05 23:30:30 +0000794 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000795}
796
797/// objc-method-decl:
798/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000799/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000800/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000801/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000802///
803/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000804/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000805/// objc-keyword-selector objc-keyword-decl
806///
807/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000808/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
809/// objc-selector ':' objc-keyword-attributes[opt] identifier
810/// ':' objc-type-name objc-keyword-attributes[opt] identifier
811/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000812///
Steve Naroff4985ace2007-08-22 18:35:33 +0000813/// objc-parmlist:
814/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000815///
Steve Naroff4985ace2007-08-22 18:35:33 +0000816/// objc-parms:
817/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000818///
Steve Naroff4985ace2007-08-22 18:35:33 +0000819/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000820/// , ...
821///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000822/// objc-keyword-attributes: [OBJC2]
823/// __attribute__((unused))
824///
John McCalld226f652010-08-21 09:40:31 +0000825Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000826 tok::TokenKind mType,
827 Decl *IDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000828 tok::ObjCKeywordKind MethodImplKind,
829 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000830 ParsingDeclRAIIObject PD(*this);
831
Douglas Gregore8f5a172010-04-07 00:21:17 +0000832 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000833 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000834 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000835 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000836 }
837
Chris Lattnere8904e92008-08-23 01:48:03 +0000838 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000839 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000840 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000841 if (Tok.is(tok::l_paren))
Douglas Gregorb77cab92011-03-08 19:17:54 +0000842 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek9e049352010-02-18 23:05:16 +0000844 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000845 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000846 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000847 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000848
Douglas Gregore8f5a172010-04-07 00:21:17 +0000849 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000850 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000851 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000852 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000853 }
854
Ted Kremenek9e049352010-02-18 23:05:16 +0000855 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000856 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000857 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000858
Steve Naroff84c43102009-02-11 20:43:13 +0000859 // An unnamed colon is valid.
860 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000861 Diag(Tok, diag::err_expected_selector_for_method)
862 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000863 // Skip until we get a ; or {}.
864 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000865 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000866 }
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000868 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000869 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000870 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000871 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000872 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chris Lattnerff384912007-10-07 02:00:24 +0000874 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000875 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000876 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Douglas Gregor926df6c2011-06-11 01:09:30 +0000877 mType, IDecl, DSRet, ReturnType,
878 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000879 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000880 methodAttrs.getList(), MethodImplKind,
881 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000882 PD.complete(Result);
883 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000884 }
Steve Narofff28b2642007-09-05 23:30:30 +0000885
Steve Naroff68d331a2007-09-27 14:38:14 +0000886 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallf312b1e2010-08-26 23:41:50 +0000887 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000888 ParseScope PrototypeScope(this,
889 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000890
891 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000892
Chris Lattnerff384912007-10-07 02:00:24 +0000893 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000894 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000895 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattnerff384912007-10-07 02:00:24 +0000897 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000898 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000899 Diag(Tok, diag::err_expected_colon);
900 break;
901 }
902 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000903
John McCallb3d87482010-08-24 05:47:05 +0000904 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000905 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000906 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000907
Chris Lattnerff384912007-10-07 02:00:24 +0000908 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000909 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +0000910 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +0000911 MaybeParseGNUAttributes(paramAttrs);
912 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +0000913 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000914
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000915 // Code completion for the next piece of the selector.
916 if (Tok.is(tok::code_completion)) {
917 ConsumeCodeCompletionToken();
918 KeyIdents.push_back(SelIdent);
919 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
920 mType == tok::minus,
921 /*AtParameterName=*/true,
922 ReturnType,
923 KeyIdents.data(),
924 KeyIdents.size());
925 KeyIdents.pop_back();
926 break;
927 }
928
Chris Lattnerdf195262007-10-09 17:51:17 +0000929 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000930 Diag(Tok, diag::err_expected_ident); // missing argument name.
931 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattnere294d3f2009-04-11 18:57:04 +0000934 ArgInfo.Name = Tok.getIdentifierInfo();
935 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000936 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Chris Lattnere294d3f2009-04-11 18:57:04 +0000938 ArgInfos.push_back(ArgInfo);
939 KeyIdents.push_back(SelIdent);
940
John McCall0b7e6782011-03-24 11:26:52 +0000941 // Make sure the attributes persist.
942 allParamAttrs.takeAllFrom(paramAttrs.getPool());
943
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000944 // Code completion for the next piece of the selector.
945 if (Tok.is(tok::code_completion)) {
946 ConsumeCodeCompletionToken();
947 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
948 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000949 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000950 ReturnType,
951 KeyIdents.data(),
952 KeyIdents.size());
953 break;
954 }
955
Chris Lattnerff384912007-10-07 02:00:24 +0000956 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000957 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000958 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000960 break;
961 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Steve Naroff335eafa2007-11-15 12:35:21 +0000964 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Chris Lattnerff384912007-10-07 02:00:24 +0000966 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000967 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000968 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000969 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000970 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000971 ConsumeToken();
972 break;
973 }
John McCall0b7e6782011-03-24 11:26:52 +0000974 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +0000975 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000976 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000977 Declarator ParmDecl(DS, Declarator::PrototypeContext);
978 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000979 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000980 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000981 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
982 ParmDecl.getIdentifierLoc(),
983 Param,
984 0));
985
Chris Lattnerff384912007-10-07 02:00:24 +0000986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +0000988 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000989 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000990 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000991 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000992
993 if (KeyIdents.size() == 0) {
994 // Leave prototype scope.
995 PrototypeScope.Exit();
John McCalld226f652010-08-21 09:40:31 +0000996 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000997 }
998
Chris Lattnerff384912007-10-07 02:00:24 +0000999 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1000 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001001 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001002 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00001003 mType, IDecl, DSRet, ReturnType,
1004 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001005 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001006 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001007 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001008 // Leave prototype scope.
1009 PrototypeScope.Exit();
1010
John McCall54abf7d2009-11-04 02:18:39 +00001011 PD.complete(Result);
1012 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001013}
1014
Steve Naroffdac269b2007-08-20 21:31:48 +00001015/// objc-protocol-refs:
1016/// '<' identifier-list '>'
1017///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001018bool Parser::
John McCalld226f652010-08-21 09:40:31 +00001019ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001020 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
1021 bool WarnOnDeclarations,
1022 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001023 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001025 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattnere13b9592008-07-26 04:03:38 +00001027 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattnere13b9592008-07-26 04:03:38 +00001029 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001030 if (Tok.is(tok::code_completion)) {
1031 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1032 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001033 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001034 }
1035
Chris Lattnere13b9592008-07-26 04:03:38 +00001036 if (Tok.isNot(tok::identifier)) {
1037 Diag(Tok, diag::err_expected_ident);
1038 SkipUntil(tok::greater);
1039 return true;
1040 }
1041 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1042 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001043 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001044 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Chris Lattnere13b9592008-07-26 04:03:38 +00001046 if (Tok.isNot(tok::comma))
1047 break;
1048 ConsumeToken();
1049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattnere13b9592008-07-26 04:03:38 +00001051 // Consume the '>'.
1052 if (Tok.isNot(tok::greater)) {
1053 Diag(Tok, diag::err_expected_greater);
1054 return true;
1055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattnere13b9592008-07-26 04:03:38 +00001057 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Chris Lattnere13b9592008-07-26 04:03:38 +00001059 // Convert the list of protocols identifiers into a list of protocol decls.
1060 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1061 &ProtocolIdents[0], ProtocolIdents.size(),
1062 Protocols);
1063 return false;
1064}
1065
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001066/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1067/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001068bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001069 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1070 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1071 SourceLocation LAngleLoc, EndProtoLoc;
1072 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1073 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001074 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1075 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001076 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1077 ProtocolLocs.data(), LAngleLoc);
1078 if (EndProtoLoc.isValid())
1079 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001080 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001081}
1082
1083
Steve Naroffdac269b2007-08-20 21:31:48 +00001084/// objc-class-instance-variables:
1085/// '{' objc-instance-variable-decl-list[opt] '}'
1086///
1087/// objc-instance-variable-decl-list:
1088/// objc-visibility-spec
1089/// objc-instance-variable-decl ';'
1090/// ';'
1091/// objc-instance-variable-decl-list objc-visibility-spec
1092/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1093/// objc-instance-variable-decl-list ';'
1094///
1095/// objc-visibility-spec:
1096/// @private
1097/// @protected
1098/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001099/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001100///
1101/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001102/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001103///
John McCalld226f652010-08-21 09:40:31 +00001104void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001105 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001106 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001107 assert(Tok.is(tok::l_brace) && "expected {");
John McCalld226f652010-08-21 09:40:31 +00001108 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001109
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001110 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001111
Steve Naroffddbff782007-08-21 21:17:12 +00001112 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Steve Naroffddbff782007-08-21 21:17:12 +00001114 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001115 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001116 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Steve Naroffddbff782007-08-21 21:17:12 +00001118 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001119 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001120 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001121 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001122 ConsumeToken();
1123 continue;
1124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Steve Naroffddbff782007-08-21 21:17:12 +00001126 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001127 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001128 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001129
1130 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001131 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001132 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001133 }
1134
Steve Naroff861cf3e2007-08-23 18:16:40 +00001135 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001136 case tok::objc_private:
1137 case tok::objc_public:
1138 case tok::objc_protected:
1139 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001140 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001141 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001142 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001143 default:
1144 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001145 continue;
1146 }
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001149 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001150 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001151 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001152 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001153 }
1154
John McCallbdd563e2009-11-03 02:38:08 +00001155 struct ObjCIvarCallback : FieldCallback {
1156 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001157 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001158 tok::ObjCKeywordKind visibility;
John McCalld226f652010-08-21 09:40:31 +00001159 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001160
John McCalld226f652010-08-21 09:40:31 +00001161 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1162 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001163 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1164 }
1165
John McCalld226f652010-08-21 09:40:31 +00001166 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001167 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001168 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001169 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001170 FD.D.getDeclSpec().getSourceRange().getBegin(),
1171 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001172 if (Field)
1173 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001174 return Field;
1175 }
1176 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001177
Chris Lattnere1359422008-04-10 06:46:29 +00001178 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001179 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001180 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Chris Lattnerdf195262007-10-09 17:51:17 +00001182 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001183 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001184 } else {
1185 Diag(Tok, diag::err_expected_semi_decl_list);
1186 // Skip to end of block or statement
1187 SkipUntil(tok::r_brace, true, true);
1188 }
1189 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001190 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001191 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001192 // Call ActOnFields() even if we don't have any decls. This is useful
1193 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001194 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001195 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001196 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001197 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001198}
Steve Naroffdac269b2007-08-20 21:31:48 +00001199
1200/// objc-protocol-declaration:
1201/// objc-protocol-definition
1202/// objc-protocol-forward-reference
1203///
1204/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001205/// @protocol identifier
1206/// objc-protocol-refs[opt]
1207/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001208/// @end
1209///
1210/// objc-protocol-forward-reference:
1211/// @protocol identifier-list ';'
1212///
1213/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001214/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001215/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001216Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001217 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001218 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001219 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1220 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor083128f2009-11-18 04:49:41 +00001222 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001223 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001224 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001225 }
1226
Chris Lattnerdf195262007-10-09 17:51:17 +00001227 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001228 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001229 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001230 }
1231 // Save the protocol name, then consume it.
1232 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1233 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Chris Lattnerdf195262007-10-09 17:51:17 +00001235 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001236 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001237 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001238 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001239 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Chris Lattnerdf195262007-10-09 17:51:17 +00001242 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001243 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1244 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1245
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001246 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001247 while (1) {
1248 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001249 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001250 Diag(Tok, diag::err_expected_ident);
1251 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001252 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001253 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001254 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1255 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001256 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Chris Lattnerdf195262007-10-09 17:51:17 +00001258 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001259 break;
1260 }
1261 // Consume the ';'.
1262 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001263 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Steve Naroffe440eb82007-10-10 17:32:04 +00001265 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001266 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001267 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001268 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001271 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001272 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001273
John McCalld226f652010-08-21 09:40:31 +00001274 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001275 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001276 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001277 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1278 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001279 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
John McCalld226f652010-08-21 09:40:31 +00001281 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001282 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001283 ProtocolRefs.data(),
1284 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001285 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001286 EndProtoLoc, attrs.getList());
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001287 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001288 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001289}
Steve Naroffdac269b2007-08-20 21:31:48 +00001290
1291/// objc-implementation:
1292/// objc-class-implementation-prologue
1293/// objc-category-implementation-prologue
1294///
1295/// objc-class-implementation-prologue:
1296/// @implementation identifier objc-superclass[opt]
1297/// objc-class-instance-variables[opt]
1298///
1299/// objc-category-implementation-prologue:
1300/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001301Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001302 SourceLocation atLoc) {
1303 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1304 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1305 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001307 // Code completion after '@implementation'.
1308 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001309 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001310 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001311 }
1312
Chris Lattnerdf195262007-10-09 17:51:17 +00001313 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001314 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001315 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001316 }
1317 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001318 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001319 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001320
1321 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001322 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001323 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001324 SourceLocation categoryLoc, rparenLoc;
1325 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001327 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001328 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001329 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001330 }
1331
Chris Lattnerdf195262007-10-09 17:51:17 +00001332 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001333 categoryId = Tok.getIdentifierInfo();
1334 categoryLoc = ConsumeToken();
1335 } else {
1336 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001337 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001338 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001339 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001340 Diag(Tok, diag::err_expected_rparen);
1341 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001342 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001343 }
1344 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001345 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001346 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001347 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001349 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001350 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001351 }
1352 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001353 SourceLocation superClassLoc;
1354 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001355 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001356 // We have a super class
1357 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001358 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001359 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001360 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001361 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001362 superClassId = Tok.getIdentifierInfo();
1363 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001364 }
John McCalld226f652010-08-21 09:40:31 +00001365 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001366 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001367 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Steve Naroff60fccee2007-10-29 21:38:07 +00001369 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001370 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001371 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001372 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001373 PendingObjCImpDecl.push_back(ObjCImpDecl);
1374
John McCalld226f652010-08-21 09:40:31 +00001375 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001376}
Steve Naroff60fccee2007-10-29 21:38:07 +00001377
John McCalld226f652010-08-21 09:40:31 +00001378Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001379 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1380 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001381 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001382 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001383 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001384 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001385 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001386 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001387 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001388 else {
1389 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001390 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001391 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001392 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001393}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001394
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001395Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1396 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001397 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001398 return Actions.ConvertDeclToDeclGroup(0);
1399 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001400 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001401 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1402}
1403
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001404/// compatibility-alias-decl:
1405/// @compatibility_alias alias-name class-name ';'
1406///
John McCalld226f652010-08-21 09:40:31 +00001407Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001408 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1409 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1410 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001411 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001412 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001413 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001414 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001415 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1416 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001417 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001418 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001419 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001420 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001421 IdentifierInfo *classId = Tok.getIdentifierInfo();
1422 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001423 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1424 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001425 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1426 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001427}
1428
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001429/// property-synthesis:
1430/// @synthesize property-ivar-list ';'
1431///
1432/// property-ivar-list:
1433/// property-ivar
1434/// property-ivar-list ',' property-ivar
1435///
1436/// property-ivar:
1437/// identifier
1438/// identifier '=' identifier
1439///
John McCalld226f652010-08-21 09:40:31 +00001440Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001441 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1442 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001443 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Douglas Gregorb328c422009-11-18 19:45:45 +00001445 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001446 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001447 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001448 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001449 }
1450
Douglas Gregorb328c422009-11-18 19:45:45 +00001451 if (Tok.isNot(tok::identifier)) {
1452 Diag(Tok, diag::err_synthesized_property_name);
1453 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001454 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001455 }
1456
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001457 IdentifierInfo *propertyIvar = 0;
1458 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1459 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001460 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001461 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001462 // property '=' ivar-name
1463 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001464
1465 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001466 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001467 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001468 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001469 }
1470
Chris Lattnerdf195262007-10-09 17:51:17 +00001471 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001472 Diag(Tok, diag::err_expected_ident);
1473 break;
1474 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001475 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001476 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001477 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001478 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001479 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001480 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001481 break;
1482 ConsumeToken(); // consume ','
1483 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001484 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001485 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001486}
1487
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001488/// property-dynamic:
1489/// @dynamic property-list
1490///
1491/// property-list:
1492/// identifier
1493/// property-list ',' identifier
1494///
John McCalld226f652010-08-21 09:40:31 +00001495Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001496 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1497 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001498 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001499 while (true) {
1500 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001501 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001502 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001503 }
1504
1505 if (Tok.isNot(tok::identifier)) {
1506 Diag(Tok, diag::err_expected_ident);
1507 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001508 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001509 }
1510
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001511 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1512 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001513 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001514 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001515
Chris Lattnerdf195262007-10-09 17:51:17 +00001516 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001517 break;
1518 ConsumeToken(); // consume ','
1519 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001520 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001521 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001522}
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001524/// objc-throw-statement:
1525/// throw expression[opt];
1526///
John McCall60d7b3a2010-08-24 06:29:42 +00001527StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1528 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001529 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001530 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001531 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001532 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001533 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001534 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001535 }
1536 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001537 // consume ';'
1538 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001539 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001540}
1541
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001542/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001543/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001544///
John McCall60d7b3a2010-08-24 06:29:42 +00001545StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001546Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001547 ConsumeToken(); // consume synchronized
1548 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001549 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001550 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001551 }
1552 ConsumeParen(); // '('
John McCall60d7b3a2010-08-24 06:29:42 +00001553 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001554 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001555 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001556 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001557 }
1558 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001559 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001560 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001561 }
1562 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001563 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001564 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001565 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001566 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001567 // Enter a scope to hold everything within the compound stmt. Compound
1568 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001569 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001570
John McCall60d7b3a2010-08-24 06:29:42 +00001571 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001572
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001573 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001574 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001575 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00001576 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001577}
1578
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001579/// objc-try-catch-statement:
1580/// @try compound-statement objc-catch-list[opt]
1581/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1582///
1583/// objc-catch-list:
1584/// @catch ( parameter-declaration ) compound-statement
1585/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1586/// catch-parameter-declaration:
1587/// parameter-declaration
1588/// '...' [OBJC2]
1589///
John McCall60d7b3a2010-08-24 06:29:42 +00001590StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001591 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001592
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001593 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001594 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001595 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001596 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001597 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001598 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001599 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001600 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001601 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001602 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001603 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001604 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001605
Chris Lattnerdf195262007-10-09 17:51:17 +00001606 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001607 // At this point, we need to lookahead to determine if this @ is the start
1608 // of an @catch or @finally. We don't want to consume the @ token if this
1609 // is an @try or @encode or something else.
1610 Token AfterAt = GetLookAheadToken(1);
1611 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1612 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1613 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001614
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001615 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001616 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001617 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001618 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001619 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001620 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001621 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001622 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001623 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001624 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001625 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001626 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001627 // we must accept either 'declarator' or 'abstract-declarator' here.
1628 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1629 ParseDeclarator(ParmDecl);
1630
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001631 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001632 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001633 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001634 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001635 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Steve Naroff93a25952009-04-07 22:56:58 +00001637 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Steve Naroff93a25952009-04-07 22:56:58 +00001639 if (Tok.is(tok::r_paren))
1640 RParenLoc = ConsumeParen();
1641 else // Skip over garbage, until we get to ')'. Eat the ')'.
1642 SkipUntil(tok::r_paren, true, false);
1643
John McCall60d7b3a2010-08-24 06:29:42 +00001644 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001645 if (Tok.is(tok::l_brace))
1646 CatchBody = ParseCompoundStatementBody();
1647 else
1648 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001649 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001650 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001651
John McCall60d7b3a2010-08-24 06:29:42 +00001652 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001653 RParenLoc,
1654 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001655 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001656 if (!Catch.isInvalid())
1657 CatchStmts.push_back(Catch.release());
1658
Steve Naroff64515f32008-02-05 21:27:35 +00001659 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001660 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1661 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001662 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001663 }
1664 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001665 } else {
1666 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001667 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001668 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001669
John McCall60d7b3a2010-08-24 06:29:42 +00001670 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001671 if (Tok.is(tok::l_brace))
1672 FinallyBody = ParseCompoundStatementBody();
1673 else
1674 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001675 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001676 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001677 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001678 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001679 catch_or_finally_seen = true;
1680 break;
1681 }
1682 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001683 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001684 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001685 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001686 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001687
John McCall9ae2f072010-08-23 23:25:46 +00001688 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001689 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001690 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001691}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001692
Steve Naroff3536b442007-09-06 21:24:23 +00001693/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001694///
John McCalld226f652010-08-21 09:40:31 +00001695Decl *Parser::ParseObjCMethodDefinition() {
1696 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001697
John McCallf312b1e2010-08-26 23:41:50 +00001698 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1699 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001701 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001702 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001703 if (ObjCImpDecl) {
1704 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001705 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001706 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001707 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001708 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001709
Steve Naroff409be832007-11-11 19:54:21 +00001710 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001711 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001712 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Steve Naroff409be832007-11-11 19:54:21 +00001714 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1715 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Steve Naroff409be832007-11-11 19:54:21 +00001717 // If we didn't find the '{', bail out.
1718 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001719 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001720 }
Steve Naroff409be832007-11-11 19:54:21 +00001721 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Steve Naroff409be832007-11-11 19:54:21 +00001723 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001724 ParseScope BodyScope(this,
1725 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Steve Naroff409be832007-11-11 19:54:21 +00001727 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001728 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001729 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001730
Douglas Gregorc9977d02011-03-16 17:05:57 +00001731 if (PP.isCodeCompletionEnabled()) {
1732 if (trySkippingFunctionBodyForCodeCompletion()) {
1733 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001734 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001735 }
1736 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001737
John McCall60d7b3a2010-08-24 06:29:42 +00001738 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001739
Steve Naroff409be832007-11-11 19:54:21 +00001740 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001741 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001742 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1743 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001744
Steve Naroff409be832007-11-11 19:54:21 +00001745 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001746 BodyScope.Exit();
Douglas Gregorc9977d02011-03-16 17:05:57 +00001747
1748 // TODO: Pass argument information.
1749 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Steve Naroff71c0a952007-11-13 23:01:27 +00001750 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001751}
Anders Carlsson55085182007-08-21 17:43:55 +00001752
John McCall60d7b3a2010-08-24 06:29:42 +00001753StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001754 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001755 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001756 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001757 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001758 }
1759
1760 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001761 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001762
1763 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001764 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001765
1766 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001767 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001768
John McCall60d7b3a2010-08-24 06:29:42 +00001769 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001770 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001771 // If the expression is invalid, skip ahead to the next semicolon. Not
1772 // doing this opens us up to the possibility of infinite loops if
1773 // ParseExpression does not consume any tokens.
1774 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001775 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001776 }
Chris Lattner5d803162009-12-07 16:33:19 +00001777
Steve Naroff64515f32008-02-05 21:27:35 +00001778 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001779 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001780 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001781}
1782
John McCall60d7b3a2010-08-24 06:29:42 +00001783ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001784 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001785 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001786 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001787 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001788 return ExprError();
1789
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001790 case tok::string_literal: // primary-expression: string-literal
1791 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001792 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001793 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001794 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001795 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001796
Chris Lattner4fef81d2008-08-05 06:19:09 +00001797 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1798 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001799 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001800 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001801 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001802 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001803 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001804 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001805 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001806 }
Anders Carlsson55085182007-08-21 17:43:55 +00001807 }
Anders Carlsson55085182007-08-21 17:43:55 +00001808}
1809
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001810/// \brirg Parse the receiver of an Objective-C++ message send.
1811///
1812/// This routine parses the receiver of a message send in
1813/// Objective-C++ either as a type or as an expression. Note that this
1814/// routine must not be called to parse a send to 'super', since it
1815/// has no way to return such a result.
1816///
1817/// \param IsExpr Whether the receiver was parsed as an expression.
1818///
1819/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1820/// IsExpr is true), the parsed expression. If the receiver was parsed
1821/// as a type (\c IsExpr is false), the parsed type.
1822///
1823/// \returns True if an error occurred during parsing or semantic
1824/// analysis, in which case the arguments do not have valid
1825/// values. Otherwise, returns false for a successful parse.
1826///
1827/// objc-receiver: [C++]
1828/// 'super' [not parsed here]
1829/// expression
1830/// simple-type-specifier
1831/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001832bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001833 InMessageExpressionRAIIObject InMessage(*this, true);
1834
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001835 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1836 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1837 TryAnnotateTypeOrScopeToken();
1838
1839 if (!isCXXSimpleTypeSpecifier()) {
1840 // objc-receiver:
1841 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001842 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001843 if (Receiver.isInvalid())
1844 return true;
1845
1846 IsExpr = true;
1847 TypeOrExpr = Receiver.take();
1848 return false;
1849 }
1850
1851 // objc-receiver:
1852 // typename-specifier
1853 // simple-type-specifier
1854 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001855 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001856 ParseCXXSimpleTypeSpecifier(DS);
1857
1858 if (Tok.is(tok::l_paren)) {
1859 // If we see an opening parentheses at this point, we are
1860 // actually parsing an expression that starts with a
1861 // function-style cast, e.g.,
1862 //
1863 // postfix-expression:
1864 // simple-type-specifier ( expression-list [opt] )
1865 // typename-specifier ( expression-list [opt] )
1866 //
1867 // Parse the remainder of this case, then the (optional)
1868 // postfix-expression suffix, followed by the (optional)
1869 // right-hand side of the binary expression. We have an
1870 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001871 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001872 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001873 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001874 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001875 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001876 if (Receiver.isInvalid())
1877 return true;
1878
1879 IsExpr = true;
1880 TypeOrExpr = Receiver.take();
1881 return false;
1882 }
1883
1884 // We have a class message. Turn the simple-type-specifier or
1885 // typename-specifier we parsed into a type and parse the
1886 // remainder of the class message.
1887 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001888 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001889 if (Type.isInvalid())
1890 return true;
1891
1892 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001893 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001894 return false;
1895}
1896
Douglas Gregor1b730e82010-05-31 14:40:22 +00001897/// \brief Determine whether the parser is currently referring to a an
1898/// Objective-C message send, using a simplified heuristic to avoid overhead.
1899///
1900/// This routine will only return true for a subset of valid message-send
1901/// expressions.
1902bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001903 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001904 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001905 return GetLookAheadToken(1).is(tok::identifier) &&
1906 GetLookAheadToken(2).is(tok::identifier);
1907}
1908
Douglas Gregor9497a732010-09-16 01:51:54 +00001909bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1910 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1911 InMessageExpression)
1912 return false;
1913
1914
1915 ParsedType Type;
1916
1917 if (Tok.is(tok::annot_typename))
1918 Type = getTypeAnnotation(Tok);
1919 else if (Tok.is(tok::identifier))
1920 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1921 getCurScope());
1922 else
1923 return false;
1924
1925 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1926 const Token &AfterNext = GetLookAheadToken(2);
1927 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1928 if (Tok.is(tok::identifier))
1929 TryAnnotateTypeOrScopeToken();
1930
1931 return Tok.is(tok::annot_typename);
1932 }
1933 }
1934
1935 return false;
1936}
1937
Mike Stump1eb44332009-09-09 15:08:12 +00001938/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001939/// '[' objc-receiver objc-message-args ']'
1940///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001941/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001942/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001943/// expression
1944/// class-name
1945/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001946///
John McCall60d7b3a2010-08-24 06:29:42 +00001947ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001948 assert(Tok.is(tok::l_square) && "'[' expected");
1949 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1950
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001951 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001952 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001953 ConsumeCodeCompletionToken();
1954 SkipUntil(tok::r_square);
1955 return ExprError();
1956 }
1957
Douglas Gregor0fbda682010-09-15 14:51:05 +00001958 InMessageExpressionRAIIObject InMessage(*this, true);
1959
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001960 if (getLang().CPlusPlus) {
1961 // We completely separate the C and C++ cases because C++ requires
1962 // more complicated (read: slower) parsing.
1963
1964 // Handle send to super.
1965 // FIXME: This doesn't benefit from the same typo-correction we
1966 // get in Objective-C.
1967 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001968 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00001969 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1970 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001971
1972 // Parse the receiver, which is either a type or an expression.
1973 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00001974 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001975 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1976 SkipUntil(tok::r_square);
1977 return ExprError();
1978 }
1979
1980 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00001981 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1982 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00001983 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001984
1985 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00001986 ParsedType::getFromOpaquePtr(TypeOrExpr),
1987 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00001988 }
1989
1990 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001991 IdentifierInfo *Name = Tok.getIdentifierInfo();
1992 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00001993 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001994 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001995 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001996 NextToken().is(tok::period),
1997 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00001998 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00001999 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2000 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002001
John McCallf312b1e2010-08-26 23:41:50 +00002002 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002003 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002004 SkipUntil(tok::r_square);
2005 return ExprError();
2006 }
2007
Douglas Gregor1569f952010-04-21 20:38:13 +00002008 ConsumeToken(); // the type name
2009
2010 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002011 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002012
John McCallf312b1e2010-08-26 23:41:50 +00002013 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002014 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002015 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002016 }
Chris Lattner699b6612008-01-25 18:59:06 +00002017 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002018
2019 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002020 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002021 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002022 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002023 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002024 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002025
John McCallb3d87482010-08-24 05:47:05 +00002026 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2027 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002028}
Sebastian Redl1d922962008-12-13 15:32:12 +00002029
Douglas Gregor2725ca82010-04-21 19:57:20 +00002030/// \brief Parse the remainder of an Objective-C message following the
2031/// '[' objc-receiver.
2032///
2033/// This routine handles sends to super, class messages (sent to a
2034/// class name), and instance messages (sent to an object), and the
2035/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2036/// ReceiverExpr, respectively. Only one of these parameters may have
2037/// a valid value.
2038///
2039/// \param LBracLoc The location of the opening '['.
2040///
2041/// \param SuperLoc If this is a send to 'super', the location of the
2042/// 'super' keyword that indicates a send to the superclass.
2043///
2044/// \param ReceiverType If this is a class message, the type of the
2045/// class we are sending a message to.
2046///
2047/// \param ReceiverExpr If this is an instance message, the expression
2048/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002049///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002050/// objc-message-args:
2051/// objc-selector
2052/// objc-keywordarg-list
2053///
2054/// objc-keywordarg-list:
2055/// objc-keywordarg
2056/// objc-keywordarg-list objc-keywordarg
2057///
Mike Stump1eb44332009-09-09 15:08:12 +00002058/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002059/// selector-name[opt] ':' objc-keywordexpr
2060///
2061/// objc-keywordexpr:
2062/// nonempty-expr-list
2063///
2064/// nonempty-expr-list:
2065/// assignment-expression
2066/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002067///
John McCall60d7b3a2010-08-24 06:29:42 +00002068ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002069Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002070 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002071 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002072 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002073 InMessageExpressionRAIIObject InMessage(*this, true);
2074
Steve Naroffc4df6d22009-11-07 02:08:14 +00002075 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002076 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002077 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2078 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002079 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002080 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2081 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002082 else
John McCall9ae2f072010-08-23 23:25:46 +00002083 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002084 0, 0, false);
Douglas Gregordc845342010-05-25 05:58:43 +00002085 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002086 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002087
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002088 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002089 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002090 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002091
Anders Carlssonff975cf2009-02-14 18:21:46 +00002092 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002093
Steve Naroff68d331a2007-09-27 14:38:14 +00002094 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002095 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002096
Chris Lattnerdf195262007-10-09 17:51:17 +00002097 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002098 while (1) {
2099 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002100 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002101
Chris Lattnerdf195262007-10-09 17:51:17 +00002102 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002103 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002104 // We must manually skip to a ']', otherwise the expression skipper will
2105 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2106 // the enclosing expression.
2107 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002108 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002109 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002110
Steve Naroff68d331a2007-09-27 14:38:14 +00002111 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002112 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002113
2114 if (Tok.is(tok::code_completion)) {
2115 if (SuperLoc.isValid())
2116 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2117 KeyIdents.data(),
2118 KeyIdents.size(),
2119 /*AtArgumentEpression=*/true);
2120 else if (ReceiverType)
2121 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2122 KeyIdents.data(),
2123 KeyIdents.size(),
2124 /*AtArgumentEpression=*/true);
2125 else
2126 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2127 KeyIdents.data(),
2128 KeyIdents.size(),
2129 /*AtArgumentEpression=*/true);
2130
2131 ConsumeCodeCompletionToken();
2132 SkipUntil(tok::r_square);
2133 return ExprError();
2134 }
2135
John McCall60d7b3a2010-08-24 06:29:42 +00002136 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002137 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002138 // We must manually skip to a ']', otherwise the expression skipper will
2139 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2140 // the enclosing expression.
2141 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002142 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002143 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002144
Steve Naroff37387c92007-09-17 20:25:27 +00002145 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002146 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002147
Douglas Gregord3c68542009-11-19 01:08:35 +00002148 // Code completion after each argument.
2149 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002150 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002151 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002152 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002153 KeyIdents.size(),
2154 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002155 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002156 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002157 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002158 KeyIdents.size(),
2159 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002160 else
John McCall9ae2f072010-08-23 23:25:46 +00002161 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002162 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002163 KeyIdents.size(),
2164 /*AtArgumentEpression=*/false);
Douglas Gregordc845342010-05-25 05:58:43 +00002165 ConsumeCodeCompletionToken();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002166 SkipUntil(tok::r_square);
2167 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002168 }
2169
Steve Naroff37387c92007-09-17 20:25:27 +00002170 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002171 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002172 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002173 break;
2174 // We have a selector or a colon, continue parsing.
2175 }
2176 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002177 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002178 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002179 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002180 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002181 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002182 // We must manually skip to a ']', otherwise the expression skipper will
2183 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2184 // the enclosing expression.
2185 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002186 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002187 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002188
Steve Naroff49f109c2007-11-15 13:05:42 +00002189 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002190 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002191 }
2192 } else if (!selIdent) {
2193 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002194
Chris Lattner4fef81d2008-08-05 06:19:09 +00002195 // We must manually skip to a ']', otherwise the expression skipper will
2196 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2197 // the enclosing expression.
2198 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002199 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002200 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002201
Chris Lattnerdf195262007-10-09 17:51:17 +00002202 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002203 if (Tok.is(tok::identifier))
2204 Diag(Tok, diag::err_expected_colon);
2205 else
2206 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002207 // We must manually skip to a ']', otherwise the expression skipper will
2208 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2209 // the enclosing expression.
2210 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002211 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002212 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002213
Chris Lattner699b6612008-01-25 18:59:06 +00002214 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002215
Steve Naroff29238a02007-10-05 18:42:47 +00002216 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002217 if (nKeys == 0)
2218 KeyIdents.push_back(selIdent);
2219 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002220
Douglas Gregor2725ca82010-04-21 19:57:20 +00002221 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002222 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002223 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002224 MultiExprArg(Actions,
2225 KeyExprs.take(),
2226 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002227 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002228 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002229 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002230 MultiExprArg(Actions,
2231 KeyExprs.take(),
2232 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002233 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002234 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002235 MultiExprArg(Actions,
2236 KeyExprs.take(),
2237 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002238}
2239
John McCall60d7b3a2010-08-24 06:29:42 +00002240ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2241 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002242 if (Res.isInvalid()) return move(Res);
2243
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002244 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2245 // expressions. At this point, we know that the only valid thing that starts
2246 // with '@' is an @"".
2247 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002248 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002249 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002250 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002251
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002252 while (Tok.is(tok::at)) {
2253 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002254
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002255 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002256 if (!isTokenStringLiteral())
2257 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002258
John McCall60d7b3a2010-08-24 06:29:42 +00002259 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002260 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002261 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002262
Sebastian Redleffa8d12008-12-10 00:02:53 +00002263 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002264 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002265
2266 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2267 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002268}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002269
2270/// objc-encode-expression:
2271/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002272ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002273Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002274 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002275
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002276 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002277
Chris Lattner4fef81d2008-08-05 06:19:09 +00002278 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002279 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2280
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002281 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002282
Douglas Gregor809070a2009-02-18 17:45:20 +00002283 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002284
Anders Carlsson4988ae32007-08-23 15:31:37 +00002285 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002286
Douglas Gregor809070a2009-02-18 17:45:20 +00002287 if (Ty.isInvalid())
2288 return ExprError();
2289
Mike Stump1eb44332009-09-09 15:08:12 +00002290 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002291 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002292}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002293
2294/// objc-protocol-expression
2295/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002296ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002297Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002298 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002299
Chris Lattner4fef81d2008-08-05 06:19:09 +00002300 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002301 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2302
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002303 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002304
Chris Lattner4fef81d2008-08-05 06:19:09 +00002305 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002306 return ExprError(Diag(Tok, diag::err_expected_ident));
2307
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002308 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002309 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002310
Anders Carlsson4988ae32007-08-23 15:31:37 +00002311 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002312
Sebastian Redl1d922962008-12-13 15:32:12 +00002313 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2314 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002315}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002316
2317/// objc-selector-expression
2318/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002319ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002320 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002321
Chris Lattner4fef81d2008-08-05 06:19:09 +00002322 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002323 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2324
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002325 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002326 SourceLocation LParenLoc = ConsumeParen();
2327 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002328
2329 if (Tok.is(tok::code_completion)) {
2330 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2331 KeyIdents.size());
2332 ConsumeCodeCompletionToken();
2333 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2334 return ExprError();
2335 }
2336
Chris Lattner2fc5c242009-04-11 18:13:45 +00002337 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002338 if (!SelIdent && // missing selector name.
2339 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002340 return ExprError(Diag(Tok, diag::err_expected_ident));
2341
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002342 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002343 unsigned nColons = 0;
2344 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002345 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002346 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2347 ++nColons;
2348 KeyIdents.push_back(0);
2349 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002350 return ExprError(Diag(Tok, diag::err_expected_colon));
2351
Chris Lattner5add7542010-08-27 22:32:41 +00002352 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002353 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002354 if (Tok.is(tok::r_paren))
2355 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002356
2357 if (Tok.is(tok::code_completion)) {
2358 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2359 KeyIdents.size());
2360 ConsumeCodeCompletionToken();
2361 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2362 return ExprError();
2363 }
2364
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002365 // Check for another keyword selector.
2366 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002367 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002368 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002369 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002370 break;
2371 }
Steve Naroff887407e2007-12-05 22:21:29 +00002372 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002373 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002374 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002375 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2376 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002377 }