blob: dab63548a45694c509cc4ef1a934d448fd8341a9 [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: {
44 ParsedAttributes attrs;
45 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
46 }
47 case tok::objc_protocol: {
48 ParsedAttributes attrs;
49 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 McCall7f040a92010-12-24 02:08:15 +0000374 ParsedAttributes attrs;
375 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.
442 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000443 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000445 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
446 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000447 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000448 }
Steve Naroff294494e2007-08-22 16:35:03 +0000449 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000450
451 // We break out of the big loop in two cases: when we see @end or when we see
452 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000453 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000454 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000455 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000456 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000457 ConsumeToken(); // the "end" identifier
458 else
459 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattnera2449b22008-10-20 05:57:40 +0000461 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000462 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000463 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000464 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000465 allProperties.data(), allProperties.size(),
466 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000467}
468
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000469/// Parse property attribute declarations.
470///
471/// property-attr-decl: '(' property-attrlist ')'
472/// property-attrlist:
473/// property-attribute
474/// property-attrlist ',' property-attribute
475/// property-attribute:
476/// getter '=' identifier
477/// setter '=' identifier ':'
478/// readonly
479/// readwrite
480/// assign
481/// retain
482/// copy
483/// nonatomic
484///
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000485void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000486 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000487 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000489 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000490 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000491 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000492 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000493 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000494 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000496 // If this is not an identifier at all, bail out early.
497 if (II == 0) {
498 MatchRHSPunctuation(tok::r_paren, LHSLoc);
499 return;
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner156b0612008-10-20 07:37:22 +0000502 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner92e62b02008-11-20 04:42:34 +0000504 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000506 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000508 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000510 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000512 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000513 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000514 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000515 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000516 else if (II->isStr("atomic"))
517 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000518 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000519 bool IsSetter = II->getNameStart()[0] == 's';
520
Chris Lattnere00da7c2008-10-20 07:39:53 +0000521 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000522 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
523 diag::err_objc_expected_equal_for_getter;
524
525 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000526 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Douglas Gregor4ad96852009-11-19 07:41:15 +0000528 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000529 if (IsSetter)
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000530 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
Douglas Gregor4ad96852009-11-19 07:41:15 +0000531 else
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000532 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000533 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000534 }
535
Anders Carlsson42499be2010-10-02 17:45:21 +0000536
537 SourceLocation SelLoc;
538 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
539
540 if (!SelIdent) {
541 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
542 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000543 SkipUntil(tok::r_paren);
544 return;
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Anders Carlsson42499be2010-10-02 17:45:21 +0000547 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000548 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000549 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000551 if (ExpectAndConsume(tok::colon,
552 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000553 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000554 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000555 } else {
556 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000557 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000558 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000559 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000560 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000561 SkipUntil(tok::r_paren);
562 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Chris Lattner156b0612008-10-20 07:37:22 +0000565 if (Tok.isNot(tok::comma))
566 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner156b0612008-10-20 07:37:22 +0000568 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner156b0612008-10-20 07:37:22 +0000571 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000572}
573
Steve Naroff3536b442007-09-06 21:24:23 +0000574/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000575/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000576/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000577///
578/// objc-instance-method: '-'
579/// objc-class-method: '+'
580///
Steve Naroff4985ace2007-08-22 18:35:33 +0000581/// objc-method-attributes: [OBJC2]
582/// __attribute__((deprecated))
583///
John McCalld226f652010-08-21 09:40:31 +0000584Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000585 tok::ObjCKeywordKind MethodImplKind,
586 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000587 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000588
Mike Stump1eb44332009-09-09 15:08:12 +0000589 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000590 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000591 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind,
592 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000593 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000594 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000595 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000596}
597
598/// objc-selector:
599/// identifier
600/// one of
601/// enum struct union if else while do for switch case default
602/// break continue return goto asm sizeof typeof __alignof
603/// unsigned long const short volatile signed restrict _Complex
604/// in out inout bycopy byref oneway int char float double void _Bool
605///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000606IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000607
Chris Lattnerff384912007-10-07 02:00:24 +0000608 switch (Tok.getKind()) {
609 default:
610 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000611 case tok::ampamp:
612 case tok::ampequal:
613 case tok::amp:
614 case tok::pipe:
615 case tok::tilde:
616 case tok::exclaim:
617 case tok::exclaimequal:
618 case tok::pipepipe:
619 case tok::pipeequal:
620 case tok::caret:
621 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000622 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000623 if (isalpha(ThisTok[0])) {
624 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
625 Tok.setKind(tok::identifier);
626 SelectorLoc = ConsumeToken();
627 return II;
628 }
629 return 0;
630 }
631
Chris Lattnerff384912007-10-07 02:00:24 +0000632 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000633 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000634 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000635 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000636 case tok::kw_break:
637 case tok::kw_case:
638 case tok::kw_catch:
639 case tok::kw_char:
640 case tok::kw_class:
641 case tok::kw_const:
642 case tok::kw_const_cast:
643 case tok::kw_continue:
644 case tok::kw_default:
645 case tok::kw_delete:
646 case tok::kw_do:
647 case tok::kw_double:
648 case tok::kw_dynamic_cast:
649 case tok::kw_else:
650 case tok::kw_enum:
651 case tok::kw_explicit:
652 case tok::kw_export:
653 case tok::kw_extern:
654 case tok::kw_false:
655 case tok::kw_float:
656 case tok::kw_for:
657 case tok::kw_friend:
658 case tok::kw_goto:
659 case tok::kw_if:
660 case tok::kw_inline:
661 case tok::kw_int:
662 case tok::kw_long:
663 case tok::kw_mutable:
664 case tok::kw_namespace:
665 case tok::kw_new:
666 case tok::kw_operator:
667 case tok::kw_private:
668 case tok::kw_protected:
669 case tok::kw_public:
670 case tok::kw_register:
671 case tok::kw_reinterpret_cast:
672 case tok::kw_restrict:
673 case tok::kw_return:
674 case tok::kw_short:
675 case tok::kw_signed:
676 case tok::kw_sizeof:
677 case tok::kw_static:
678 case tok::kw_static_cast:
679 case tok::kw_struct:
680 case tok::kw_switch:
681 case tok::kw_template:
682 case tok::kw_this:
683 case tok::kw_throw:
684 case tok::kw_true:
685 case tok::kw_try:
686 case tok::kw_typedef:
687 case tok::kw_typeid:
688 case tok::kw_typename:
689 case tok::kw_typeof:
690 case tok::kw_union:
691 case tok::kw_unsigned:
692 case tok::kw_using:
693 case tok::kw_virtual:
694 case tok::kw_void:
695 case tok::kw_volatile:
696 case tok::kw_wchar_t:
697 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000698 case tok::kw__Bool:
699 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000700 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000701 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000702 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000703 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000704 }
Steve Naroff294494e2007-08-22 16:35:03 +0000705}
706
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000707/// objc-for-collection-in: 'in'
708///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000709bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000710 // FIXME: May have to do additional look-ahead to only allow for
711 // valid tokens following an 'in'; such as an identifier, unary operators,
712 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000713 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000714 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000715}
716
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000717/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000718/// qualifier list and builds their bitmask representation in the input
719/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000720///
721/// objc-type-qualifiers:
722/// objc-type-qualifier
723/// objc-type-qualifiers objc-type-qualifier
724///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000725void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
726 ObjCTypeNameContext Context) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000727 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000728 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000729 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
730 Context == OTN_ParameterType);
Douglas Gregord32b0222010-08-24 01:06:58 +0000731 ConsumeCodeCompletionToken();
732 }
733
Chris Lattnercb53b362007-12-27 19:57:00 +0000734 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000735 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Chris Lattnere8b724d2007-12-12 06:56:32 +0000737 const IdentifierInfo *II = Tok.getIdentifierInfo();
738 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000739 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000740 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000742 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000743 switch (i) {
744 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000745 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
746 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
747 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
748 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
749 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
750 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000751 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000752 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000753 ConsumeToken();
754 II = 0;
755 break;
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattnere8b724d2007-12-12 06:56:32 +0000758 // If this wasn't a recognized qualifier, bail out.
759 if (II) return;
760 }
761}
762
763/// objc-type-name:
764/// '(' objc-type-qualifiers[opt] type-name ')'
765/// '(' objc-type-qualifiers[opt] ')'
766///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000767ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
768 ObjCTypeNameContext Context) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000769 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattner4a76b292008-10-22 03:52:06 +0000771 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000772 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000774 // Parse type qualifiers, in, inout, etc.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000775 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000776
John McCallb3d87482010-08-24 05:47:05 +0000777 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000778 if (isTypeSpecifierQualifier()) {
John McCallc05a94b2011-03-23 23:43:04 +0000779 TypeResult TypeSpec = ParseTypeName(0, Declarator::ObjCPrototypeContext);
Douglas Gregor809070a2009-02-18 17:45:20 +0000780 if (!TypeSpec.isInvalid())
781 Ty = TypeSpec.get();
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Steve Naroffd7333c22008-10-21 14:15:04 +0000784 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000785 ConsumeParen();
786 else if (Tok.getLocation() == TypeStartLoc) {
787 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000788 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000789 SkipUntil(tok::r_paren);
790 } else {
791 // Otherwise, we found *something*, but didn't get a ')' in the right
792 // place. Emit an error then return what we have as the type.
793 MatchRHSPunctuation(tok::r_paren, LParenLoc);
794 }
Steve Narofff28b2642007-09-05 23:30:30 +0000795 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000796}
797
798/// objc-method-decl:
799/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000800/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000801/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000802/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000803///
804/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000805/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000806/// objc-keyword-selector objc-keyword-decl
807///
808/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000809/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
810/// objc-selector ':' objc-keyword-attributes[opt] identifier
811/// ':' objc-type-name objc-keyword-attributes[opt] identifier
812/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000813///
Steve Naroff4985ace2007-08-22 18:35:33 +0000814/// objc-parmlist:
815/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000816///
Steve Naroff4985ace2007-08-22 18:35:33 +0000817/// objc-parms:
818/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000819///
Steve Naroff4985ace2007-08-22 18:35:33 +0000820/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000821/// , ...
822///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000823/// objc-keyword-attributes: [OBJC2]
824/// __attribute__((unused))
825///
John McCalld226f652010-08-21 09:40:31 +0000826Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000827 tok::TokenKind mType,
828 Decl *IDecl,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000829 tok::ObjCKeywordKind MethodImplKind,
830 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000831 ParsingDeclRAIIObject PD(*this);
832
Douglas Gregore8f5a172010-04-07 00:21:17 +0000833 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000834 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000835 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000836 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000837 }
838
Chris Lattnere8904e92008-08-23 01:48:03 +0000839 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000840 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000841 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000842 if (Tok.is(tok::l_paren))
Douglas Gregorb77cab92011-03-08 19:17:54 +0000843 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek9e049352010-02-18 23:05:16 +0000845 // If attributes exist before the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000846 ParsedAttributes attrs;
847 if (getLang().ObjC2)
848 MaybeParseGNUAttributes(attrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000849
Douglas Gregore8f5a172010-04-07 00:21:17 +0000850 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000851 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000852 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000853 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000854 }
855
Ted Kremenek9e049352010-02-18 23:05:16 +0000856 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000857 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000858 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000859
Steve Naroff84c43102009-02-11 20:43:13 +0000860 // An unnamed colon is valid.
861 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000862 Diag(Tok, diag::err_expected_selector_for_method)
863 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000864 // Skip until we get a ; or {}.
865 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000866 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000867 }
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000869 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000870 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000871 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000872 if (getLang().ObjC2)
873 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattnerff384912007-10-07 02:00:24 +0000875 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000876 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000877 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000878 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000879 0,
880 CParamInfo.data(), CParamInfo.size(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000881 attrs.getList(), MethodImplKind, false,
882 MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000883 PD.complete(Result);
884 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000885 }
Steve Narofff28b2642007-09-05 23:30:30 +0000886
Steve Naroff68d331a2007-09-27 14:38:14 +0000887 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallf312b1e2010-08-26 23:41:50 +0000888 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000889 ParseScope PrototypeScope(this,
890 Scope::FunctionPrototypeScope|Scope::DeclScope);
891
Chris Lattnerff384912007-10-07 02:00:24 +0000892 while (1) {
John McCallf312b1e2010-08-26 23:41:50 +0000893 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Chris Lattnerff384912007-10-07 02:00:24 +0000895 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000896 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000897 Diag(Tok, diag::err_expected_colon);
898 break;
899 }
900 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000901
John McCallb3d87482010-08-24 05:47:05 +0000902 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000903 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000904 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000905
Chris Lattnerff384912007-10-07 02:00:24 +0000906 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000907 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +0000908 if (getLang().ObjC2) {
909 ParsedAttributes attrs;
910 MaybeParseGNUAttributes(attrs);
911 ArgInfo.ArgAttrs = attrs.getList();
912 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000913
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000914 // Code completion for the next piece of the selector.
915 if (Tok.is(tok::code_completion)) {
916 ConsumeCodeCompletionToken();
917 KeyIdents.push_back(SelIdent);
918 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
919 mType == tok::minus,
920 /*AtParameterName=*/true,
921 ReturnType,
922 KeyIdents.data(),
923 KeyIdents.size());
924 KeyIdents.pop_back();
925 break;
926 }
927
Chris Lattnerdf195262007-10-09 17:51:17 +0000928 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000929 Diag(Tok, diag::err_expected_ident); // missing argument name.
930 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000931 }
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Chris Lattnere294d3f2009-04-11 18:57:04 +0000933 ArgInfo.Name = Tok.getIdentifierInfo();
934 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000935 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattnere294d3f2009-04-11 18:57:04 +0000937 ArgInfos.push_back(ArgInfo);
938 KeyIdents.push_back(SelIdent);
939
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000940 // Code completion for the next piece of the selector.
941 if (Tok.is(tok::code_completion)) {
942 ConsumeCodeCompletionToken();
943 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
944 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000945 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000946 ReturnType,
947 KeyIdents.data(),
948 KeyIdents.size());
949 break;
950 }
951
Chris Lattnerff384912007-10-07 02:00:24 +0000952 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000953 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000954 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000955 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000956 break;
957 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Steve Naroff335eafa2007-11-15 12:35:21 +0000960 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattnerff384912007-10-07 02:00:24 +0000962 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000963 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000964 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000965 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000966 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000967 ConsumeToken();
968 break;
969 }
Chris Lattnerff384912007-10-07 02:00:24 +0000970 DeclSpec DS;
971 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000972 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000973 Declarator ParmDecl(DS, Declarator::PrototypeContext);
974 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000975 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000976 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000977 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
978 ParmDecl.getIdentifierLoc(),
979 Param,
980 0));
981
Chris Lattnerff384912007-10-07 02:00:24 +0000982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +0000984 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000985 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000986 if (getLang().ObjC2)
987 MaybeParseGNUAttributes(attrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000988
989 if (KeyIdents.size() == 0) {
990 // Leave prototype scope.
991 PrototypeScope.Exit();
John McCalld226f652010-08-21 09:40:31 +0000992 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000993 }
994
Chris Lattnerff384912007-10-07 02:00:24 +0000995 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
996 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +0000997 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000998 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000999 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001000 &ArgInfos[0],
1001 CParamInfo.data(), CParamInfo.size(),
John McCall7f040a92010-12-24 02:08:15 +00001002 attrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001003 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001004 // Leave prototype scope.
1005 PrototypeScope.Exit();
1006
John McCall54abf7d2009-11-04 02:18:39 +00001007 PD.complete(Result);
1008 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001009}
1010
Steve Naroffdac269b2007-08-20 21:31:48 +00001011/// objc-protocol-refs:
1012/// '<' identifier-list '>'
1013///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001014bool Parser::
John McCalld226f652010-08-21 09:40:31 +00001015ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001016 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
1017 bool WarnOnDeclarations,
1018 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001019 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001021 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Chris Lattnere13b9592008-07-26 04:03:38 +00001023 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattnere13b9592008-07-26 04:03:38 +00001025 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001026 if (Tok.is(tok::code_completion)) {
1027 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1028 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001029 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001030 }
1031
Chris Lattnere13b9592008-07-26 04:03:38 +00001032 if (Tok.isNot(tok::identifier)) {
1033 Diag(Tok, diag::err_expected_ident);
1034 SkipUntil(tok::greater);
1035 return true;
1036 }
1037 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1038 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001039 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001040 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Chris Lattnere13b9592008-07-26 04:03:38 +00001042 if (Tok.isNot(tok::comma))
1043 break;
1044 ConsumeToken();
1045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Chris Lattnere13b9592008-07-26 04:03:38 +00001047 // Consume the '>'.
1048 if (Tok.isNot(tok::greater)) {
1049 Diag(Tok, diag::err_expected_greater);
1050 return true;
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Chris Lattnere13b9592008-07-26 04:03:38 +00001053 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattnere13b9592008-07-26 04:03:38 +00001055 // Convert the list of protocols identifiers into a list of protocol decls.
1056 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1057 &ProtocolIdents[0], ProtocolIdents.size(),
1058 Protocols);
1059 return false;
1060}
1061
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001062/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1063/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001064bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001065 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1066 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1067 SourceLocation LAngleLoc, EndProtoLoc;
1068 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1069 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001070 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1071 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001072 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1073 ProtocolLocs.data(), LAngleLoc);
1074 if (EndProtoLoc.isValid())
1075 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001076 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001077}
1078
1079
Steve Naroffdac269b2007-08-20 21:31:48 +00001080/// objc-class-instance-variables:
1081/// '{' objc-instance-variable-decl-list[opt] '}'
1082///
1083/// objc-instance-variable-decl-list:
1084/// objc-visibility-spec
1085/// objc-instance-variable-decl ';'
1086/// ';'
1087/// objc-instance-variable-decl-list objc-visibility-spec
1088/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1089/// objc-instance-variable-decl-list ';'
1090///
1091/// objc-visibility-spec:
1092/// @private
1093/// @protected
1094/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001095/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001096///
1097/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001098/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001099///
John McCalld226f652010-08-21 09:40:31 +00001100void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001101 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001102 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001103 assert(Tok.is(tok::l_brace) && "expected {");
John McCalld226f652010-08-21 09:40:31 +00001104 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001105
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001106 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001107
Steve Naroffddbff782007-08-21 21:17:12 +00001108 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Steve Naroffddbff782007-08-21 21:17:12 +00001110 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001111 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001112 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Steve Naroffddbff782007-08-21 21:17:12 +00001114 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001115 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001116 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001117 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001118 ConsumeToken();
1119 continue;
1120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Steve Naroffddbff782007-08-21 21:17:12 +00001122 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001123 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001124 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001125
1126 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001127 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001128 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001129 }
1130
Steve Naroff861cf3e2007-08-23 18:16:40 +00001131 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001132 case tok::objc_private:
1133 case tok::objc_public:
1134 case tok::objc_protected:
1135 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001136 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001137 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001138 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001139 default:
1140 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001141 continue;
1142 }
1143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001145 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001146 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001147 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001148 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001149 }
1150
John McCallbdd563e2009-11-03 02:38:08 +00001151 struct ObjCIvarCallback : FieldCallback {
1152 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001153 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001154 tok::ObjCKeywordKind visibility;
John McCalld226f652010-08-21 09:40:31 +00001155 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001156
John McCalld226f652010-08-21 09:40:31 +00001157 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1158 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001159 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1160 }
1161
John McCalld226f652010-08-21 09:40:31 +00001162 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001163 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001164 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001165 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001166 FD.D.getDeclSpec().getSourceRange().getBegin(),
1167 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001168 if (Field)
1169 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001170 return Field;
1171 }
1172 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001173
Chris Lattnere1359422008-04-10 06:46:29 +00001174 // Parse all the comma separated declarators.
1175 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001176 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Chris Lattnerdf195262007-10-09 17:51:17 +00001178 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001179 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001180 } else {
1181 Diag(Tok, diag::err_expected_semi_decl_list);
1182 // Skip to end of block or statement
1183 SkipUntil(tok::r_brace, true, true);
1184 }
1185 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001186 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001187 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001188 // Call ActOnFields() even if we don't have any decls. This is useful
1189 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001190 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001191 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001192 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001193 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001194}
Steve Naroffdac269b2007-08-20 21:31:48 +00001195
1196/// objc-protocol-declaration:
1197/// objc-protocol-definition
1198/// objc-protocol-forward-reference
1199///
1200/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001201/// @protocol identifier
1202/// objc-protocol-refs[opt]
1203/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001204/// @end
1205///
1206/// objc-protocol-forward-reference:
1207/// @protocol identifier-list ';'
1208///
1209/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001210/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001211/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001212Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001213 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001214 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001215 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1216 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Douglas Gregor083128f2009-11-18 04:49:41 +00001218 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001219 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001220 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001221 }
1222
Chris Lattnerdf195262007-10-09 17:51:17 +00001223 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001224 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001225 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001226 }
1227 // Save the protocol name, then consume it.
1228 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1229 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Chris Lattnerdf195262007-10-09 17:51:17 +00001231 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001232 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001233 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001234 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001235 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Chris Lattnerdf195262007-10-09 17:51:17 +00001238 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001239 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1240 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1241
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001242 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001243 while (1) {
1244 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001245 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001246 Diag(Tok, diag::err_expected_ident);
1247 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001248 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001249 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001250 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1251 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001252 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001255 break;
1256 }
1257 // Consume the ';'.
1258 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001259 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Steve Naroffe440eb82007-10-10 17:32:04 +00001261 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001262 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001263 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001264 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001267 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001268 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001269
John McCalld226f652010-08-21 09:40:31 +00001270 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001271 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001272 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001273 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1274 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001275 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001276
John McCalld226f652010-08-21 09:40:31 +00001277 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001278 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001279 ProtocolRefs.data(),
1280 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001281 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001282 EndProtoLoc, attrs.getList());
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001283 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001284 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001285}
Steve Naroffdac269b2007-08-20 21:31:48 +00001286
1287/// objc-implementation:
1288/// objc-class-implementation-prologue
1289/// objc-category-implementation-prologue
1290///
1291/// objc-class-implementation-prologue:
1292/// @implementation identifier objc-superclass[opt]
1293/// objc-class-instance-variables[opt]
1294///
1295/// objc-category-implementation-prologue:
1296/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001297Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001298 SourceLocation atLoc) {
1299 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1300 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1301 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001303 // Code completion after '@implementation'.
1304 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001305 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001306 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001307 }
1308
Chris Lattnerdf195262007-10-09 17:51:17 +00001309 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001310 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001311 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001312 }
1313 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001314 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001315 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001316
1317 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001318 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001319 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001320 SourceLocation categoryLoc, rparenLoc;
1321 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001323 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001324 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001325 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001326 }
1327
Chris Lattnerdf195262007-10-09 17:51:17 +00001328 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001329 categoryId = Tok.getIdentifierInfo();
1330 categoryLoc = ConsumeToken();
1331 } else {
1332 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001333 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001334 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001335 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001336 Diag(Tok, diag::err_expected_rparen);
1337 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001338 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001339 }
1340 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001341 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001342 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001343 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001344 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001345 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001346 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347 }
1348 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001349 SourceLocation superClassLoc;
1350 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001351 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001352 // We have a super class
1353 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001354 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001355 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001356 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001357 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001358 superClassId = Tok.getIdentifierInfo();
1359 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001360 }
John McCalld226f652010-08-21 09:40:31 +00001361 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001362 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001363 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Steve Naroff60fccee2007-10-29 21:38:07 +00001365 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001366 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001367 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001368 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001369 PendingObjCImpDecl.push_back(ObjCImpDecl);
1370
John McCalld226f652010-08-21 09:40:31 +00001371 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001372}
Steve Naroff60fccee2007-10-29 21:38:07 +00001373
John McCalld226f652010-08-21 09:40:31 +00001374Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001375 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1376 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001377 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001378 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001379 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001380 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001381 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001382 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001383 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001384 else {
1385 // missing @implementation
1386 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1387 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001388 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001389}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001390
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001391Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1392 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001393 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001394 return Actions.ConvertDeclToDeclGroup(0);
1395 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001396 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001397 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1398}
1399
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001400/// compatibility-alias-decl:
1401/// @compatibility_alias alias-name class-name ';'
1402///
John McCalld226f652010-08-21 09:40:31 +00001403Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001404 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1405 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1406 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001407 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001408 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001409 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001410 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001411 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1412 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001413 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001414 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001415 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001416 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001417 IdentifierInfo *classId = Tok.getIdentifierInfo();
1418 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001419 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1420 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001421 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1422 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001423}
1424
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001425/// property-synthesis:
1426/// @synthesize property-ivar-list ';'
1427///
1428/// property-ivar-list:
1429/// property-ivar
1430/// property-ivar-list ',' property-ivar
1431///
1432/// property-ivar:
1433/// identifier
1434/// identifier '=' identifier
1435///
John McCalld226f652010-08-21 09:40:31 +00001436Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001437 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1438 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001439 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregorb328c422009-11-18 19:45:45 +00001441 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001442 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001443 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001444 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001445 }
1446
Douglas Gregorb328c422009-11-18 19:45:45 +00001447 if (Tok.isNot(tok::identifier)) {
1448 Diag(Tok, diag::err_synthesized_property_name);
1449 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001450 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001451 }
1452
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001453 IdentifierInfo *propertyIvar = 0;
1454 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1455 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001456 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001457 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001458 // property '=' ivar-name
1459 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001460
1461 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001462 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001463 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001464 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001465 }
1466
Chris Lattnerdf195262007-10-09 17:51:17 +00001467 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001468 Diag(Tok, diag::err_expected_ident);
1469 break;
1470 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001471 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001472 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001473 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001474 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001475 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001476 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001477 break;
1478 ConsumeToken(); // consume ','
1479 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001480 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001481 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001482}
1483
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001484/// property-dynamic:
1485/// @dynamic property-list
1486///
1487/// property-list:
1488/// identifier
1489/// property-list ',' identifier
1490///
John McCalld226f652010-08-21 09:40:31 +00001491Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001492 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1493 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001494 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001495 while (true) {
1496 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001497 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001498 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001499 }
1500
1501 if (Tok.isNot(tok::identifier)) {
1502 Diag(Tok, diag::err_expected_ident);
1503 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001504 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001505 }
1506
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001507 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1508 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001509 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001510 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001511
Chris Lattnerdf195262007-10-09 17:51:17 +00001512 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001513 break;
1514 ConsumeToken(); // consume ','
1515 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001516 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001517 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001518}
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001520/// objc-throw-statement:
1521/// throw expression[opt];
1522///
John McCall60d7b3a2010-08-24 06:29:42 +00001523StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1524 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001525 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001526 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001527 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001528 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001529 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001530 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001531 }
1532 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001533 // consume ';'
1534 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001535 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001536}
1537
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001538/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001539/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001540///
John McCall60d7b3a2010-08-24 06:29:42 +00001541StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001542Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001543 ConsumeToken(); // consume synchronized
1544 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001545 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001546 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001547 }
1548 ConsumeParen(); // '('
John McCall60d7b3a2010-08-24 06:29:42 +00001549 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001550 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001551 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001552 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001553 }
1554 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001555 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001556 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001557 }
1558 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001559 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001560 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001561 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001562 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001563 // Enter a scope to hold everything within the compound stmt. Compound
1564 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001565 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001566
John McCall60d7b3a2010-08-24 06:29:42 +00001567 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001568
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001569 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001570 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001571 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00001572 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001573}
1574
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001575/// objc-try-catch-statement:
1576/// @try compound-statement objc-catch-list[opt]
1577/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1578///
1579/// objc-catch-list:
1580/// @catch ( parameter-declaration ) compound-statement
1581/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1582/// catch-parameter-declaration:
1583/// parameter-declaration
1584/// '...' [OBJC2]
1585///
John McCall60d7b3a2010-08-24 06:29:42 +00001586StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001587 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001588
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001589 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001590 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001591 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001592 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001593 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001594 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001595 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001596 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001597 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001598 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001599 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001600 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001601
Chris Lattnerdf195262007-10-09 17:51:17 +00001602 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001603 // At this point, we need to lookahead to determine if this @ is the start
1604 // of an @catch or @finally. We don't want to consume the @ token if this
1605 // is an @try or @encode or something else.
1606 Token AfterAt = GetLookAheadToken(1);
1607 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1608 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1609 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001610
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001611 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001612 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001613 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001614 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001615 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001616 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001617 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001618 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001619 DeclSpec DS;
1620 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001621 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001622 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001623 // we must accept either 'declarator' or 'abstract-declarator' here.
1624 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1625 ParseDeclarator(ParmDecl);
1626
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001627 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001628 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001629 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001630 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001631 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Steve Naroff93a25952009-04-07 22:56:58 +00001633 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Steve Naroff93a25952009-04-07 22:56:58 +00001635 if (Tok.is(tok::r_paren))
1636 RParenLoc = ConsumeParen();
1637 else // Skip over garbage, until we get to ')'. Eat the ')'.
1638 SkipUntil(tok::r_paren, true, false);
1639
John McCall60d7b3a2010-08-24 06:29:42 +00001640 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001641 if (Tok.is(tok::l_brace))
1642 CatchBody = ParseCompoundStatementBody();
1643 else
1644 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001645 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001646 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001647
John McCall60d7b3a2010-08-24 06:29:42 +00001648 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001649 RParenLoc,
1650 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001651 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001652 if (!Catch.isInvalid())
1653 CatchStmts.push_back(Catch.release());
1654
Steve Naroff64515f32008-02-05 21:27:35 +00001655 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001656 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1657 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001658 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001659 }
1660 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001661 } else {
1662 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001663 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001664 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001665
John McCall60d7b3a2010-08-24 06:29:42 +00001666 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001667 if (Tok.is(tok::l_brace))
1668 FinallyBody = ParseCompoundStatementBody();
1669 else
1670 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001671 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001672 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001673 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001674 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001675 catch_or_finally_seen = true;
1676 break;
1677 }
1678 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001679 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001680 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001681 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001682 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001683
John McCall9ae2f072010-08-23 23:25:46 +00001684 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001685 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001686 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001687}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001688
Steve Naroff3536b442007-09-06 21:24:23 +00001689/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001690///
John McCalld226f652010-08-21 09:40:31 +00001691Decl *Parser::ParseObjCMethodDefinition() {
1692 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001693
John McCallf312b1e2010-08-26 23:41:50 +00001694 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1695 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001697 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001698 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001699 if (ObjCImpDecl) {
1700 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001701 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001702 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001703 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001704 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001705
Steve Naroff409be832007-11-11 19:54:21 +00001706 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001707 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001708 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Steve Naroff409be832007-11-11 19:54:21 +00001710 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1711 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Steve Naroff409be832007-11-11 19:54:21 +00001713 // If we didn't find the '{', bail out.
1714 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001715 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001716 }
Steve Naroff409be832007-11-11 19:54:21 +00001717 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Steve Naroff409be832007-11-11 19:54:21 +00001719 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001720 ParseScope BodyScope(this,
1721 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Steve Naroff409be832007-11-11 19:54:21 +00001723 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001724 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001725 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001726
Douglas Gregorc9977d02011-03-16 17:05:57 +00001727 if (PP.isCodeCompletionEnabled()) {
1728 if (trySkippingFunctionBodyForCodeCompletion()) {
1729 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001730 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001731 }
1732 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001733
John McCall60d7b3a2010-08-24 06:29:42 +00001734 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001735
Steve Naroff409be832007-11-11 19:54:21 +00001736 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001737 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001738 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1739 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001740
Steve Naroff409be832007-11-11 19:54:21 +00001741 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001742 BodyScope.Exit();
Douglas Gregorc9977d02011-03-16 17:05:57 +00001743
1744 // TODO: Pass argument information.
1745 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Steve Naroff71c0a952007-11-13 23:01:27 +00001746 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001747}
Anders Carlsson55085182007-08-21 17:43:55 +00001748
John McCall60d7b3a2010-08-24 06:29:42 +00001749StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001750 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001751 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001752 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001753 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001754 }
1755
1756 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001757 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001758
1759 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001760 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001761
1762 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001763 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001764
John McCall60d7b3a2010-08-24 06:29:42 +00001765 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001766 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001767 // If the expression is invalid, skip ahead to the next semicolon. Not
1768 // doing this opens us up to the possibility of infinite loops if
1769 // ParseExpression does not consume any tokens.
1770 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001771 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001772 }
Chris Lattner5d803162009-12-07 16:33:19 +00001773
Steve Naroff64515f32008-02-05 21:27:35 +00001774 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001775 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001776 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001777}
1778
John McCall60d7b3a2010-08-24 06:29:42 +00001779ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001780 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001781 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001782 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001783 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001784 return ExprError();
1785
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001786 case tok::string_literal: // primary-expression: string-literal
1787 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001788 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001789 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001790 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001791 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001792
Chris Lattner4fef81d2008-08-05 06:19:09 +00001793 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1794 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001795 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001796 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001797 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001798 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001799 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001800 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001801 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001802 }
Anders Carlsson55085182007-08-21 17:43:55 +00001803 }
Anders Carlsson55085182007-08-21 17:43:55 +00001804}
1805
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001806/// \brirg Parse the receiver of an Objective-C++ message send.
1807///
1808/// This routine parses the receiver of a message send in
1809/// Objective-C++ either as a type or as an expression. Note that this
1810/// routine must not be called to parse a send to 'super', since it
1811/// has no way to return such a result.
1812///
1813/// \param IsExpr Whether the receiver was parsed as an expression.
1814///
1815/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1816/// IsExpr is true), the parsed expression. If the receiver was parsed
1817/// as a type (\c IsExpr is false), the parsed type.
1818///
1819/// \returns True if an error occurred during parsing or semantic
1820/// analysis, in which case the arguments do not have valid
1821/// values. Otherwise, returns false for a successful parse.
1822///
1823/// objc-receiver: [C++]
1824/// 'super' [not parsed here]
1825/// expression
1826/// simple-type-specifier
1827/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001828bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001829 InMessageExpressionRAIIObject InMessage(*this, true);
1830
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001831 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1832 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1833 TryAnnotateTypeOrScopeToken();
1834
1835 if (!isCXXSimpleTypeSpecifier()) {
1836 // objc-receiver:
1837 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001838 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001839 if (Receiver.isInvalid())
1840 return true;
1841
1842 IsExpr = true;
1843 TypeOrExpr = Receiver.take();
1844 return false;
1845 }
1846
1847 // objc-receiver:
1848 // typename-specifier
1849 // simple-type-specifier
1850 // expression (that starts with one of the above)
1851 DeclSpec DS;
1852 ParseCXXSimpleTypeSpecifier(DS);
1853
1854 if (Tok.is(tok::l_paren)) {
1855 // If we see an opening parentheses at this point, we are
1856 // actually parsing an expression that starts with a
1857 // function-style cast, e.g.,
1858 //
1859 // postfix-expression:
1860 // simple-type-specifier ( expression-list [opt] )
1861 // typename-specifier ( expression-list [opt] )
1862 //
1863 // Parse the remainder of this case, then the (optional)
1864 // postfix-expression suffix, followed by the (optional)
1865 // right-hand side of the binary expression. We have an
1866 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001867 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001868 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001869 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001870 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001871 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001872 if (Receiver.isInvalid())
1873 return true;
1874
1875 IsExpr = true;
1876 TypeOrExpr = Receiver.take();
1877 return false;
1878 }
1879
1880 // We have a class message. Turn the simple-type-specifier or
1881 // typename-specifier we parsed into a type and parse the
1882 // remainder of the class message.
1883 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001884 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001885 if (Type.isInvalid())
1886 return true;
1887
1888 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001889 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001890 return false;
1891}
1892
Douglas Gregor1b730e82010-05-31 14:40:22 +00001893/// \brief Determine whether the parser is currently referring to a an
1894/// Objective-C message send, using a simplified heuristic to avoid overhead.
1895///
1896/// This routine will only return true for a subset of valid message-send
1897/// expressions.
1898bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001899 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001900 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001901 return GetLookAheadToken(1).is(tok::identifier) &&
1902 GetLookAheadToken(2).is(tok::identifier);
1903}
1904
Douglas Gregor9497a732010-09-16 01:51:54 +00001905bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1906 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1907 InMessageExpression)
1908 return false;
1909
1910
1911 ParsedType Type;
1912
1913 if (Tok.is(tok::annot_typename))
1914 Type = getTypeAnnotation(Tok);
1915 else if (Tok.is(tok::identifier))
1916 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1917 getCurScope());
1918 else
1919 return false;
1920
1921 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1922 const Token &AfterNext = GetLookAheadToken(2);
1923 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1924 if (Tok.is(tok::identifier))
1925 TryAnnotateTypeOrScopeToken();
1926
1927 return Tok.is(tok::annot_typename);
1928 }
1929 }
1930
1931 return false;
1932}
1933
Mike Stump1eb44332009-09-09 15:08:12 +00001934/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001935/// '[' objc-receiver objc-message-args ']'
1936///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001937/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001938/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001939/// expression
1940/// class-name
1941/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001942///
John McCall60d7b3a2010-08-24 06:29:42 +00001943ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001944 assert(Tok.is(tok::l_square) && "'[' expected");
1945 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1946
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001947 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001948 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001949 ConsumeCodeCompletionToken();
1950 SkipUntil(tok::r_square);
1951 return ExprError();
1952 }
1953
Douglas Gregor0fbda682010-09-15 14:51:05 +00001954 InMessageExpressionRAIIObject InMessage(*this, true);
1955
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001956 if (getLang().CPlusPlus) {
1957 // We completely separate the C and C++ cases because C++ requires
1958 // more complicated (read: slower) parsing.
1959
1960 // Handle send to super.
1961 // FIXME: This doesn't benefit from the same typo-correction we
1962 // get in Objective-C.
1963 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001964 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00001965 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1966 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001967
1968 // Parse the receiver, which is either a type or an expression.
1969 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00001970 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001971 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1972 SkipUntil(tok::r_square);
1973 return ExprError();
1974 }
1975
1976 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00001977 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1978 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00001979 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001980
1981 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00001982 ParsedType::getFromOpaquePtr(TypeOrExpr),
1983 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00001984 }
1985
1986 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001987 IdentifierInfo *Name = Tok.getIdentifierInfo();
1988 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00001989 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001990 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001991 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001992 NextToken().is(tok::period),
1993 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00001994 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00001995 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1996 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001997
John McCallf312b1e2010-08-26 23:41:50 +00001998 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00001999 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002000 SkipUntil(tok::r_square);
2001 return ExprError();
2002 }
2003
Douglas Gregor1569f952010-04-21 20:38:13 +00002004 ConsumeToken(); // the type name
2005
2006 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002007 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002008
John McCallf312b1e2010-08-26 23:41:50 +00002009 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002010 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002011 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002012 }
Chris Lattner699b6612008-01-25 18:59:06 +00002013 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002014
2015 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002016 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002017 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002018 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002019 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002020 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002021
John McCallb3d87482010-08-24 05:47:05 +00002022 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2023 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002024}
Sebastian Redl1d922962008-12-13 15:32:12 +00002025
Douglas Gregor2725ca82010-04-21 19:57:20 +00002026/// \brief Parse the remainder of an Objective-C message following the
2027/// '[' objc-receiver.
2028///
2029/// This routine handles sends to super, class messages (sent to a
2030/// class name), and instance messages (sent to an object), and the
2031/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2032/// ReceiverExpr, respectively. Only one of these parameters may have
2033/// a valid value.
2034///
2035/// \param LBracLoc The location of the opening '['.
2036///
2037/// \param SuperLoc If this is a send to 'super', the location of the
2038/// 'super' keyword that indicates a send to the superclass.
2039///
2040/// \param ReceiverType If this is a class message, the type of the
2041/// class we are sending a message to.
2042///
2043/// \param ReceiverExpr If this is an instance message, the expression
2044/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002045///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002046/// objc-message-args:
2047/// objc-selector
2048/// objc-keywordarg-list
2049///
2050/// objc-keywordarg-list:
2051/// objc-keywordarg
2052/// objc-keywordarg-list objc-keywordarg
2053///
Mike Stump1eb44332009-09-09 15:08:12 +00002054/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002055/// selector-name[opt] ':' objc-keywordexpr
2056///
2057/// objc-keywordexpr:
2058/// nonempty-expr-list
2059///
2060/// nonempty-expr-list:
2061/// assignment-expression
2062/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002063///
John McCall60d7b3a2010-08-24 06:29:42 +00002064ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002065Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002066 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002067 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002068 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002069 InMessageExpressionRAIIObject InMessage(*this, true);
2070
Steve Naroffc4df6d22009-11-07 02:08:14 +00002071 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002072 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002073 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2074 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002075 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002076 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2077 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002078 else
John McCall9ae2f072010-08-23 23:25:46 +00002079 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002080 0, 0, false);
Douglas Gregordc845342010-05-25 05:58:43 +00002081 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002082 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002083
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002084 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002085 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002086 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002087
Anders Carlssonff975cf2009-02-14 18:21:46 +00002088 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Steve Naroff68d331a2007-09-27 14:38:14 +00002090 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002091 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002092
Chris Lattnerdf195262007-10-09 17:51:17 +00002093 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002094 while (1) {
2095 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002096 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002097
Chris Lattnerdf195262007-10-09 17:51:17 +00002098 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002099 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002100 // We must manually skip to a ']', otherwise the expression skipper will
2101 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2102 // the enclosing expression.
2103 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002104 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002105 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002106
Steve Naroff68d331a2007-09-27 14:38:14 +00002107 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002108 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002109
2110 if (Tok.is(tok::code_completion)) {
2111 if (SuperLoc.isValid())
2112 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2113 KeyIdents.data(),
2114 KeyIdents.size(),
2115 /*AtArgumentEpression=*/true);
2116 else if (ReceiverType)
2117 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2118 KeyIdents.data(),
2119 KeyIdents.size(),
2120 /*AtArgumentEpression=*/true);
2121 else
2122 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2123 KeyIdents.data(),
2124 KeyIdents.size(),
2125 /*AtArgumentEpression=*/true);
2126
2127 ConsumeCodeCompletionToken();
2128 SkipUntil(tok::r_square);
2129 return ExprError();
2130 }
2131
John McCall60d7b3a2010-08-24 06:29:42 +00002132 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002133 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002134 // We must manually skip to a ']', otherwise the expression skipper will
2135 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2136 // the enclosing expression.
2137 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002138 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002139 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002140
Steve Naroff37387c92007-09-17 20:25:27 +00002141 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002142 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002143
Douglas Gregord3c68542009-11-19 01:08:35 +00002144 // Code completion after each argument.
2145 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002146 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002147 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002148 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002149 KeyIdents.size(),
2150 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002151 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002152 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002153 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002154 KeyIdents.size(),
2155 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002156 else
John McCall9ae2f072010-08-23 23:25:46 +00002157 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002158 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002159 KeyIdents.size(),
2160 /*AtArgumentEpression=*/false);
Douglas Gregordc845342010-05-25 05:58:43 +00002161 ConsumeCodeCompletionToken();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002162 SkipUntil(tok::r_square);
2163 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002164 }
2165
Steve Naroff37387c92007-09-17 20:25:27 +00002166 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002167 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002168 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002169 break;
2170 // We have a selector or a colon, continue parsing.
2171 }
2172 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002173 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002174 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002175 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002176 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002177 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002178 // We must manually skip to a ']', otherwise the expression skipper will
2179 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2180 // the enclosing expression.
2181 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002182 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002183 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002184
Steve Naroff49f109c2007-11-15 13:05:42 +00002185 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002186 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002187 }
2188 } else if (!selIdent) {
2189 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002190
Chris Lattner4fef81d2008-08-05 06:19:09 +00002191 // We must manually skip to a ']', otherwise the expression skipper will
2192 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2193 // the enclosing expression.
2194 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002195 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002196 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002197
Chris Lattnerdf195262007-10-09 17:51:17 +00002198 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002199 if (Tok.is(tok::identifier))
2200 Diag(Tok, diag::err_expected_colon);
2201 else
2202 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002203 // We must manually skip to a ']', otherwise the expression skipper will
2204 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2205 // the enclosing expression.
2206 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002207 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002208 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002209
Chris Lattner699b6612008-01-25 18:59:06 +00002210 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002211
Steve Naroff29238a02007-10-05 18:42:47 +00002212 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002213 if (nKeys == 0)
2214 KeyIdents.push_back(selIdent);
2215 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002216
Douglas Gregor2725ca82010-04-21 19:57:20 +00002217 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002218 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002219 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002220 MultiExprArg(Actions,
2221 KeyExprs.take(),
2222 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002223 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002224 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002225 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002226 MultiExprArg(Actions,
2227 KeyExprs.take(),
2228 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002229 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002230 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002231 MultiExprArg(Actions,
2232 KeyExprs.take(),
2233 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002234}
2235
John McCall60d7b3a2010-08-24 06:29:42 +00002236ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2237 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002238 if (Res.isInvalid()) return move(Res);
2239
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002240 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2241 // expressions. At this point, we know that the only valid thing that starts
2242 // with '@' is an @"".
2243 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002244 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002245 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002246 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002247
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002248 while (Tok.is(tok::at)) {
2249 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002250
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002251 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002252 if (!isTokenStringLiteral())
2253 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002254
John McCall60d7b3a2010-08-24 06:29:42 +00002255 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002256 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002257 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002258
Sebastian Redleffa8d12008-12-10 00:02:53 +00002259 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002260 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002261
2262 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2263 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002264}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002265
2266/// objc-encode-expression:
2267/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002268ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002269Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002270 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002271
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002272 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002273
Chris Lattner4fef81d2008-08-05 06:19:09 +00002274 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002275 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2276
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002277 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002278
Douglas Gregor809070a2009-02-18 17:45:20 +00002279 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002280
Anders Carlsson4988ae32007-08-23 15:31:37 +00002281 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002282
Douglas Gregor809070a2009-02-18 17:45:20 +00002283 if (Ty.isInvalid())
2284 return ExprError();
2285
Mike Stump1eb44332009-09-09 15:08:12 +00002286 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002287 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002288}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002289
2290/// objc-protocol-expression
2291/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002292ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002293Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002294 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002295
Chris Lattner4fef81d2008-08-05 06:19:09 +00002296 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002297 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2298
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002299 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002300
Chris Lattner4fef81d2008-08-05 06:19:09 +00002301 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002302 return ExprError(Diag(Tok, diag::err_expected_ident));
2303
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002304 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002305 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002306
Anders Carlsson4988ae32007-08-23 15:31:37 +00002307 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002308
Sebastian Redl1d922962008-12-13 15:32:12 +00002309 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2310 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002311}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002312
2313/// objc-selector-expression
2314/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002315ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002316 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002317
Chris Lattner4fef81d2008-08-05 06:19:09 +00002318 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002319 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2320
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002321 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002322 SourceLocation LParenLoc = ConsumeParen();
2323 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002324
2325 if (Tok.is(tok::code_completion)) {
2326 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2327 KeyIdents.size());
2328 ConsumeCodeCompletionToken();
2329 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2330 return ExprError();
2331 }
2332
Chris Lattner2fc5c242009-04-11 18:13:45 +00002333 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002334 if (!SelIdent && // missing selector name.
2335 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002336 return ExprError(Diag(Tok, diag::err_expected_ident));
2337
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002338 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002339 unsigned nColons = 0;
2340 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002341 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002342 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2343 ++nColons;
2344 KeyIdents.push_back(0);
2345 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002346 return ExprError(Diag(Tok, diag::err_expected_colon));
2347
Chris Lattner5add7542010-08-27 22:32:41 +00002348 ++nColons;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002349 ConsumeToken(); // Eat the ':'.
2350 if (Tok.is(tok::r_paren))
2351 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002352
2353 if (Tok.is(tok::code_completion)) {
2354 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2355 KeyIdents.size());
2356 ConsumeCodeCompletionToken();
2357 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2358 return ExprError();
2359 }
2360
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002361 // Check for another keyword selector.
2362 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002363 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002364 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002365 if (!SelIdent && Tok.isNot(tok::colon))
2366 break;
2367 }
Steve Naroff887407e2007-12-05 22:21:29 +00002368 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002369 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002370 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002371 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2372 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002373 }