blob: 6c5053d4fbceb2f3f431846e7226cc4f9163d2f6 [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
14#include "clang/Parse/Parser.h"
Steve Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
Chris Lattner891dca62008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Chris Lattnerb28317a2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000032
Douglas Gregorc464ae82009-12-07 09:27:33 +000033 if (Tok.is(tok::code_completion)) {
34 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
35 ConsumeToken();
36 }
37
Steve Naroff861cf3e2007-08-23 18:16:40 +000038 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000039 case tok::objc_class:
40 return ParseObjCAtClassDeclaration(AtLoc);
41 case tok::objc_interface:
42 return ParseObjCAtInterfaceDeclaration(AtLoc);
43 case tok::objc_protocol:
44 return ParseObjCAtProtocolDeclaration(AtLoc);
45 case tok::objc_implementation:
46 return ParseObjCAtImplementationDeclaration(AtLoc);
47 case tok::objc_end:
48 return ParseObjCAtEndDeclaration(AtLoc);
49 case tok::objc_compatibility_alias:
50 return ParseObjCAtAliasDeclaration(AtLoc);
51 case tok::objc_synthesize:
52 return ParseObjCPropertySynthesize(AtLoc);
53 case tok::objc_dynamic:
54 return ParseObjCPropertyDynamic(AtLoc);
55 default:
56 Diag(AtLoc, diag::err_unexpected_at);
57 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000058 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000059 }
60}
61
62///
Mike Stump1eb44332009-09-09 15:08:12 +000063/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000064/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000065///
Chris Lattnerb28317a2009-03-28 19:18:32 +000066Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 ConsumeToken(); // the identifier "class"
68 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000069 llvm::SmallVector<SourceLocation, 8> ClassLocs;
70
Mike Stump1eb44332009-09-09 15:08:12 +000071
Reid Spencer5f016e22007-07-11 17:01:13 +000072 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000073 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000074 Diag(Tok, diag::err_expected_ident);
75 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000076 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000077 }
Reid Spencer5f016e22007-07-11 17:01:13 +000078 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000079 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattnerdf195262007-10-09 17:51:17 +000082 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000083 break;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Reid Spencer5f016e22007-07-11 17:01:13 +000085 ConsumeToken();
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // Consume the ';'.
89 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000090 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenekc09cba62009-11-17 23:12:20 +000092 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93 ClassLocs.data(),
94 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
Steve Naroffdac269b2007-08-20 21:31:48 +000097///
98/// objc-interface:
99/// objc-class-interface-attributes[opt] objc-class-interface
100/// objc-category-interface
101///
102/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000103/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000104/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-interface-decl-list
107/// @end
108///
109/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000110/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000111/// objc-protocol-refs[opt]
112/// objc-interface-decl-list
113/// @end
114///
115/// objc-superclass:
116/// ':' identifier
117///
118/// objc-class-interface-attributes:
119/// __attribute__((visibility("default")))
120/// __attribute__((visibility("hidden")))
121/// __attribute__((deprecated))
122/// __attribute__((unavailable))
123/// __attribute__((objc_exception)) - used by NSException on 64-bit
124///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000125Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000126 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000127 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000131 // Code completion after '@interface'.
132 if (Tok.is(tok::code_completion)) {
133 Actions.CodeCompleteObjCInterfaceDecl(CurScope);
134 ConsumeToken();
135 }
136
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000139 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000140 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000141
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000143 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000145 bool Err = false;
Chris Lattnerdf195262007-10-09 17:51:17 +0000146 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 SourceLocation lparenLoc = ConsumeParen();
148 SourceLocation categoryLoc, rparenLoc;
149 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000150 if (Tok.is(tok::code_completion)) {
151 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
152 ConsumeToken();
153 }
154
Steve Naroff527fe232007-08-23 19:56:30 +0000155 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000156 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000157 categoryId = Tok.getIdentifierInfo();
158 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000159 }
160 else if (isKnownToBeTypeSpecifier(Tok)) {
161 // Fall thru after diagnosing for better error recovery.
162 Diag(Tok, diag::err_expected_minus_or_plus);
163 ConsumeToken();
164 Err = true;
165 }
166 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000167 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000168 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000169 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000170 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 Diag(Tok, diag::err_expected_rparen);
172 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000173 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000174 }
175 rparenLoc = ConsumeParen();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000176 if (!Err) {
177 // Next, we need to check for any protocol references.
178 SourceLocation LAngleLoc, EndProtoLoc;
179 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
180 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
181 if (Tok.is(tok::less) &&
182 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000183 LAngleLoc, EndProtoLoc))
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000184 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000186 if (attrList) // categories don't support attributes.
187 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000189 DeclPtrTy CategoryType =
190 Actions.ActOnStartCategoryInterface(atLoc,
191 nameId, nameLoc,
192 categoryId, categoryLoc,
193 ProtocolRefs.data(),
194 ProtocolRefs.size(),
195 ProtocolLocs.data(),
196 EndProtoLoc);
197 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000198 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
199 atLoc);
200
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000201 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
202 return CategoryType;
203 }
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)) {
214 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
215 ConsumeToken();
216 }
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.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000220 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000221 }
222 superClassId = Tok.getIdentifierInfo();
223 superClassLoc = ConsumeToken();
224 }
225 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000226 llvm::SmallVector<Action::DeclPtrTy, 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))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000232 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000233
234 DeclPtrTy ClsType =
235 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(),
Chris Lattner06036d32008-07-26 04:13:19 +0000239 EndProtoLoc, attrList);
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 Jahanian05511fa2010-04-02 23:15:40 +0000245 return Err ? DeclPtrTy() : 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;
252 DeclPtrTy IDecl;
253 llvm::SmallVectorImpl<DeclPtrTy> &Props;
254 ObjCDeclSpec &OCDS;
255 SourceLocation AtLoc;
256 tok::ObjCKeywordKind MethodImplKind;
257
258 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
259 llvm::SmallVectorImpl<DeclPtrTy> &Props,
260 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
261 tok::ObjCKeywordKind MethodImplKind) :
262 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
263 MethodImplKind(MethodImplKind) {
264 }
265
266 DeclPtrTy invoke(FieldDeclarator &FD) {
267 if (FD.D.getIdentifier() == 0) {
268 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
269 << FD.D.getSourceRange();
270 return DeclPtrTy();
271 }
272 if (FD.BitfieldSize) {
273 P.Diag(AtLoc, diag::err_objc_property_bitfield)
274 << FD.D.getSourceRange();
275 return DeclPtrTy();
276 }
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;
293 DeclPtrTy Property =
294 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
295 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///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000317void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000318 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000319 llvm::SmallVector<DeclPtrTy, 32> allMethods;
320 llvm::SmallVector<DeclPtrTy, 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)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000329 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000330 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
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);
340 DeclPtrTy methodPrototype = ParseObjCMethodDecl(Tok.getLocation(),
341 tok::minus,
342 interfaceDecl,
343 MethodImplKind);
344 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)) {
358 Actions.CodeCompleteOrdinaryName(CurScope,
359 ObjCImpDecl? Action::CCC_ObjCImplementation
360 : Action::CCC_ObjCInterface);
361 ConsumeToken();
362 }
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.
Sean Huntbbd37c62009-11-21 08:43:09 +0000374 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000375 continue;
376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattnere82a10f2008-10-20 05:46:22 +0000378 // Otherwise, we have an @ directive, eat the @.
379 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000380 if (Tok.is(tok::code_completion)) {
381 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
382 ConsumeToken();
383 break;
384 }
385
Chris Lattnera2449b22008-10-20 05:57:40 +0000386 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattnera2449b22008-10-20 05:57:40 +0000388 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000389 AtEnd.setBegin(AtLoc);
390 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000391 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000392 } else if (DirectiveKind == tok::objc_not_keyword) {
393 Diag(Tok, diag::err_objc_unknown_at);
394 SkipUntil(tok::semi);
395 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattnerbc662af2008-10-20 06:10:06 +0000398 // Eat the identifier.
399 ConsumeToken();
400
Chris Lattnera2449b22008-10-20 05:57:40 +0000401 switch (DirectiveKind) {
402 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000403 // FIXME: If someone forgets an @end on a protocol, this loop will
404 // continue to eat up tons of stuff and spew lots of nonsense errors. It
405 // would probably be better to bail out if we saw an @class or @interface
406 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000407 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000408 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000409 SkipUntil(tok::r_brace, tok::at);
410 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattnera2449b22008-10-20 05:57:40 +0000412 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000413 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000414 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000415 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000416 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000417 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000418 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000419 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000420 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattnera2449b22008-10-20 05:57:40 +0000422 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000423 if (!getLang().ObjC2)
424 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
425
Chris Lattnere82a10f2008-10-20 05:46:22 +0000426 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000427 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000428 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000429 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
430 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000431
John McCalld0014542009-12-03 22:31:13 +0000432 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
433 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000434
Chris Lattnere82a10f2008-10-20 05:46:22 +0000435 // Parse all the comma separated declarators.
436 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000437 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000439 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
440 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000441 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000442 }
Steve Naroff294494e2007-08-22 16:35:03 +0000443 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000444
445 // We break out of the big loop in two cases: when we see @end or when we see
446 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000447 if (Tok.is(tok::code_completion)) {
448 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
449 ConsumeToken();
450 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000451 ConsumeToken(); // the "end" identifier
452 else
453 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattnera2449b22008-10-20 05:57:40 +0000455 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000456 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek782f2f52010-01-07 01:20:12 +0000457 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000458 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000459 allProperties.data(), allProperties.size(),
460 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000461}
462
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000463/// Parse property attribute declarations.
464///
465/// property-attr-decl: '(' property-attrlist ')'
466/// property-attrlist:
467/// property-attribute
468/// property-attrlist ',' property-attribute
469/// property-attribute:
470/// getter '=' identifier
471/// setter '=' identifier ':'
472/// readonly
473/// readwrite
474/// assign
475/// retain
476/// copy
477/// nonatomic
478///
Douglas Gregor4ad96852009-11-19 07:41:15 +0000479void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
480 DeclPtrTy *Methods,
481 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000482 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000483 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000485 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000486 if (Tok.is(tok::code_completion)) {
Douglas Gregora93b1082009-11-18 23:08:07 +0000487 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroffece8e712009-10-08 21:55:05 +0000488 ConsumeToken();
489 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000490 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000492 // If this is not an identifier at all, bail out early.
493 if (II == 0) {
494 MatchRHSPunctuation(tok::r_paren, LHSLoc);
495 return;
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattner156b0612008-10-20 07:37:22 +0000498 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattner92e62b02008-11-20 04:42:34 +0000500 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000502 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000504 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000506 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000508 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000510 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000512 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000513 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000514 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
515 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000516 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregor4ad96852009-11-19 07:41:15 +0000518 if (Tok.is(tok::code_completion)) {
519 if (II->getNameStart()[0] == 's')
520 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
521 Methods, NumMethods);
522 else
523 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
524 Methods, NumMethods);
525 ConsumeToken();
526 }
527
Chris Lattner8ca329c2008-10-20 07:24:39 +0000528 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000529 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000530 SkipUntil(tok::r_paren);
531 return;
532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Daniel Dunbare013d682009-10-18 20:26:12 +0000534 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000535 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
536 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000537 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000539 if (ExpectAndConsume(tok::colon,
540 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000541 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000542 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000543 } else {
544 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
545 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000546 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000547 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000548 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000549 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000550 SkipUntil(tok::r_paren);
551 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattner156b0612008-10-20 07:37:22 +0000554 if (Tok.isNot(tok::comma))
555 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner156b0612008-10-20 07:37:22 +0000557 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner156b0612008-10-20 07:37:22 +0000560 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000561}
562
Steve Naroff3536b442007-09-06 21:24:23 +0000563/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000564/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000565/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000566///
567/// objc-instance-method: '-'
568/// objc-class-method: '+'
569///
Steve Naroff4985ace2007-08-22 18:35:33 +0000570/// objc-method-attributes: [OBJC2]
571/// __attribute__((deprecated))
572///
Mike Stump1eb44332009-09-09 15:08:12 +0000573Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000574 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000575 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000576
Mike Stump1eb44332009-09-09 15:08:12 +0000577 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000578 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattnerb28317a2009-03-28 19:18:32 +0000580 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000581 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000582 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000583 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000584}
585
586/// objc-selector:
587/// identifier
588/// one of
589/// enum struct union if else while do for switch case default
590/// break continue return goto asm sizeof typeof __alignof
591/// unsigned long const short volatile signed restrict _Complex
592/// in out inout bycopy byref oneway int char float double void _Bool
593///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000594IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000595 switch (Tok.getKind()) {
596 default:
597 return 0;
598 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000599 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000600 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000601 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000602 case tok::kw_break:
603 case tok::kw_case:
604 case tok::kw_catch:
605 case tok::kw_char:
606 case tok::kw_class:
607 case tok::kw_const:
608 case tok::kw_const_cast:
609 case tok::kw_continue:
610 case tok::kw_default:
611 case tok::kw_delete:
612 case tok::kw_do:
613 case tok::kw_double:
614 case tok::kw_dynamic_cast:
615 case tok::kw_else:
616 case tok::kw_enum:
617 case tok::kw_explicit:
618 case tok::kw_export:
619 case tok::kw_extern:
620 case tok::kw_false:
621 case tok::kw_float:
622 case tok::kw_for:
623 case tok::kw_friend:
624 case tok::kw_goto:
625 case tok::kw_if:
626 case tok::kw_inline:
627 case tok::kw_int:
628 case tok::kw_long:
629 case tok::kw_mutable:
630 case tok::kw_namespace:
631 case tok::kw_new:
632 case tok::kw_operator:
633 case tok::kw_private:
634 case tok::kw_protected:
635 case tok::kw_public:
636 case tok::kw_register:
637 case tok::kw_reinterpret_cast:
638 case tok::kw_restrict:
639 case tok::kw_return:
640 case tok::kw_short:
641 case tok::kw_signed:
642 case tok::kw_sizeof:
643 case tok::kw_static:
644 case tok::kw_static_cast:
645 case tok::kw_struct:
646 case tok::kw_switch:
647 case tok::kw_template:
648 case tok::kw_this:
649 case tok::kw_throw:
650 case tok::kw_true:
651 case tok::kw_try:
652 case tok::kw_typedef:
653 case tok::kw_typeid:
654 case tok::kw_typename:
655 case tok::kw_typeof:
656 case tok::kw_union:
657 case tok::kw_unsigned:
658 case tok::kw_using:
659 case tok::kw_virtual:
660 case tok::kw_void:
661 case tok::kw_volatile:
662 case tok::kw_wchar_t:
663 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000664 case tok::kw__Bool:
665 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000666 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000667 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000668 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000669 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000670 }
Steve Naroff294494e2007-08-22 16:35:03 +0000671}
672
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000673/// objc-for-collection-in: 'in'
674///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000675bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000676 // FIXME: May have to do additional look-ahead to only allow for
677 // valid tokens following an 'in'; such as an identifier, unary operators,
678 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000679 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000680 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000681}
682
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000684/// qualifier list and builds their bitmask representation in the input
685/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000686///
687/// objc-type-qualifiers:
688/// objc-type-qualifier
689/// objc-type-qualifiers objc-type-qualifier
690///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000691void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000692 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000693 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000694 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattnere8b724d2007-12-12 06:56:32 +0000696 const IdentifierInfo *II = Tok.getIdentifierInfo();
697 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000698 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000699 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000701 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000702 switch (i) {
703 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000704 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
705 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
706 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
707 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
708 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
709 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000710 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000711 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000712 ConsumeToken();
713 II = 0;
714 break;
715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Chris Lattnere8b724d2007-12-12 06:56:32 +0000717 // If this wasn't a recognized qualifier, bail out.
718 if (II) return;
719 }
720}
721
722/// objc-type-name:
723/// '(' objc-type-qualifiers[opt] type-name ')'
724/// '(' objc-type-qualifiers[opt] ')'
725///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000727 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattner4a76b292008-10-22 03:52:06 +0000729 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000730 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000732 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000733 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000734
Chris Lattner4a76b292008-10-22 03:52:06 +0000735 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000736 if (isTypeSpecifierQualifier()) {
737 TypeResult TypeSpec = ParseTypeName();
738 if (!TypeSpec.isInvalid())
739 Ty = TypeSpec.get();
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Steve Naroffd7333c22008-10-21 14:15:04 +0000742 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000743 ConsumeParen();
744 else if (Tok.getLocation() == TypeStartLoc) {
745 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000746 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000747 SkipUntil(tok::r_paren);
748 } else {
749 // Otherwise, we found *something*, but didn't get a ')' in the right
750 // place. Emit an error then return what we have as the type.
751 MatchRHSPunctuation(tok::r_paren, LParenLoc);
752 }
Steve Narofff28b2642007-09-05 23:30:30 +0000753 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000754}
755
756/// objc-method-decl:
757/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000758/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000759/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000760/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000761///
762/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000763/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000764/// objc-keyword-selector objc-keyword-decl
765///
766/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000767/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
768/// objc-selector ':' objc-keyword-attributes[opt] identifier
769/// ':' objc-type-name objc-keyword-attributes[opt] identifier
770/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000771///
Steve Naroff4985ace2007-08-22 18:35:33 +0000772/// objc-parmlist:
773/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000774///
Steve Naroff4985ace2007-08-22 18:35:33 +0000775/// objc-parms:
776/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000777///
Steve Naroff4985ace2007-08-22 18:35:33 +0000778/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000779/// , ...
780///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000781/// objc-keyword-attributes: [OBJC2]
782/// __attribute__((unused))
783///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000784Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
785 tok::TokenKind mType,
786 DeclPtrTy IDecl,
787 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000788 ParsingDeclRAIIObject PD(*this);
789
Douglas Gregore8f5a172010-04-07 00:21:17 +0000790 if (Tok.is(tok::code_completion)) {
791 Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus,
792 /*ReturnType=*/0, IDecl);
793 ConsumeToken();
794 }
795
Chris Lattnere8904e92008-08-23 01:48:03 +0000796 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000797 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000798 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000799 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000800 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek9e049352010-02-18 23:05:16 +0000802 // If attributes exist before the method, parse them.
803 llvm::OwningPtr<AttributeList> MethodAttrs;
804 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
805 MethodAttrs.reset(ParseGNUAttributes());
806
Douglas Gregore8f5a172010-04-07 00:21:17 +0000807 if (Tok.is(tok::code_completion)) {
808 Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus,
809 ReturnType, IDecl);
810 ConsumeToken();
811 }
812
Ted Kremenek9e049352010-02-18 23:05:16 +0000813 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000814 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000815 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000816
Steve Naroff84c43102009-02-11 20:43:13 +0000817 // An unnamed colon is valid.
818 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000819 Diag(Tok, diag::err_expected_selector_for_method)
820 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000821 // Skip until we get a ; or {}.
822 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000823 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000826 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000827 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000828 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000829 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000830 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
831 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Chris Lattnerff384912007-10-07 02:00:24 +0000833 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000834 DeclPtrTy Result
835 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000836 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000837 0,
838 CParamInfo.data(), CParamInfo.size(),
839 MethodAttrs.get(),
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000840 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000841 PD.complete(Result);
842 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000843 }
Steve Narofff28b2642007-09-05 23:30:30 +0000844
Steve Naroff68d331a2007-09-27 14:38:14 +0000845 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000846 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattnerff384912007-10-07 02:00:24 +0000848 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000849 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattnerff384912007-10-07 02:00:24 +0000851 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000852 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000853 Diag(Tok, diag::err_expected_colon);
854 break;
855 }
856 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattnere294d3f2009-04-11 18:57:04 +0000858 ArgInfo.Type = 0;
859 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
860 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
861
Chris Lattnerff384912007-10-07 02:00:24 +0000862 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000863 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000864 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000865 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000866
Chris Lattnerdf195262007-10-09 17:51:17 +0000867 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000868 Diag(Tok, diag::err_expected_ident); // missing argument name.
869 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Chris Lattnere294d3f2009-04-11 18:57:04 +0000872 ArgInfo.Name = Tok.getIdentifierInfo();
873 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000874 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Chris Lattnere294d3f2009-04-11 18:57:04 +0000876 ArgInfos.push_back(ArgInfo);
877 KeyIdents.push_back(SelIdent);
878
Chris Lattnerff384912007-10-07 02:00:24 +0000879 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000880 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000881 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000882 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000883 break;
884 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Steve Naroff335eafa2007-11-15 12:35:21 +0000887 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Chris Lattnerff384912007-10-07 02:00:24 +0000889 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000890 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000891 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000892 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000893 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000894 ConsumeToken();
895 break;
896 }
Chris Lattnerff384912007-10-07 02:00:24 +0000897 DeclSpec DS;
898 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000899 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000900 Declarator ParmDecl(DS, Declarator::PrototypeContext);
901 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000902 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
903 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
904 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
905 ParmDecl.getIdentifierLoc(),
906 Param,
907 0));
908
Chris Lattnerff384912007-10-07 02:00:24 +0000909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Chris Lattnerff384912007-10-07 02:00:24 +0000911 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000912 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000913 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000914 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
915 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000917 if (KeyIdents.size() == 0)
918 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000919 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
920 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000921 DeclPtrTy Result
922 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000923 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000924 &ArgInfos[0],
925 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +0000926 MethodAttrs.get(),
Steve Naroff335eafa2007-11-15 12:35:21 +0000927 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000928 PD.complete(Result);
Ted Kremenek1e377652010-02-11 02:19:13 +0000929
930 // Delete referenced AttributeList objects.
931 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
932 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
933 delete I->ArgAttrs;
934
John McCall54abf7d2009-11-04 02:18:39 +0000935 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000936}
937
Steve Naroffdac269b2007-08-20 21:31:48 +0000938/// objc-protocol-refs:
939/// '<' identifier-list '>'
940///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000941bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000942ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000943 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
944 bool WarnOnDeclarations,
945 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000946 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000948 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Chris Lattnere13b9592008-07-26 04:03:38 +0000950 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Chris Lattnere13b9592008-07-26 04:03:38 +0000952 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000953 if (Tok.is(tok::code_completion)) {
954 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
955 ProtocolIdents.size());
956 ConsumeToken();
957 }
958
Chris Lattnere13b9592008-07-26 04:03:38 +0000959 if (Tok.isNot(tok::identifier)) {
960 Diag(Tok, diag::err_expected_ident);
961 SkipUntil(tok::greater);
962 return true;
963 }
964 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
965 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000966 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000967 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattnere13b9592008-07-26 04:03:38 +0000969 if (Tok.isNot(tok::comma))
970 break;
971 ConsumeToken();
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chris Lattnere13b9592008-07-26 04:03:38 +0000974 // Consume the '>'.
975 if (Tok.isNot(tok::greater)) {
976 Diag(Tok, diag::err_expected_greater);
977 return true;
978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattnere13b9592008-07-26 04:03:38 +0000980 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattnere13b9592008-07-26 04:03:38 +0000982 // Convert the list of protocols identifiers into a list of protocol decls.
983 Actions.FindProtocolDeclaration(WarnOnDeclarations,
984 &ProtocolIdents[0], ProtocolIdents.size(),
985 Protocols);
986 return false;
987}
988
Steve Naroffdac269b2007-08-20 21:31:48 +0000989/// objc-class-instance-variables:
990/// '{' objc-instance-variable-decl-list[opt] '}'
991///
992/// objc-instance-variable-decl-list:
993/// objc-visibility-spec
994/// objc-instance-variable-decl ';'
995/// ';'
996/// objc-instance-variable-decl-list objc-visibility-spec
997/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
998/// objc-instance-variable-decl-list ';'
999///
1000/// objc-visibility-spec:
1001/// @private
1002/// @protected
1003/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001004/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001005///
1006/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001007/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001008///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001009void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001010 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001011 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001012 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001013 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001014
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001015 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001016
Steve Naroffddbff782007-08-21 21:17:12 +00001017 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Steve Naroffddbff782007-08-21 21:17:12 +00001019 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001020 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001021 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Steve Naroffddbff782007-08-21 21:17:12 +00001023 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001024 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001025 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001026 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001027 ConsumeToken();
1028 continue;
1029 }
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Steve Naroffddbff782007-08-21 21:17:12 +00001031 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001032 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001033 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001034
1035 if (Tok.is(tok::code_completion)) {
1036 Actions.CodeCompleteObjCAtVisibility(CurScope);
1037 ConsumeToken();
1038 }
1039
Steve Naroff861cf3e2007-08-23 18:16:40 +00001040 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001041 case tok::objc_private:
1042 case tok::objc_public:
1043 case tok::objc_protected:
1044 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001045 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001046 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001047 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001048 default:
1049 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001050 continue;
1051 }
1052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001054 if (Tok.is(tok::code_completion)) {
1055 Actions.CodeCompleteOrdinaryName(CurScope,
1056 Action::CCC_ObjCInstanceVariableList);
1057 ConsumeToken();
1058 }
1059
John McCallbdd563e2009-11-03 02:38:08 +00001060 struct ObjCIvarCallback : FieldCallback {
1061 Parser &P;
1062 DeclPtrTy IDecl;
1063 tok::ObjCKeywordKind visibility;
1064 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1065
1066 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1067 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1068 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1069 }
1070
1071 DeclPtrTy invoke(FieldDeclarator &FD) {
1072 // Install the declarator into the interface decl.
1073 DeclPtrTy Field
1074 = P.Actions.ActOnIvar(P.CurScope,
1075 FD.D.getDeclSpec().getSourceRange().getBegin(),
1076 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001077 if (Field)
1078 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001079 return Field;
1080 }
1081 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1082
Chris Lattnere1359422008-04-10 06:46:29 +00001083 // Parse all the comma separated declarators.
1084 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001085 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattnerdf195262007-10-09 17:51:17 +00001087 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001088 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001089 } else {
1090 Diag(Tok, diag::err_expected_semi_decl_list);
1091 // Skip to end of block or statement
1092 SkipUntil(tok::r_brace, true, true);
1093 }
1094 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001095 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +00001096 // Call ActOnFields() even if we don't have any decls. This is useful
1097 // for code rewriting tools that need to be aware of the empty list.
1098 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001099 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001100 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001101 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001102}
Steve Naroffdac269b2007-08-20 21:31:48 +00001103
1104/// objc-protocol-declaration:
1105/// objc-protocol-definition
1106/// objc-protocol-forward-reference
1107///
1108/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001109/// @protocol identifier
1110/// objc-protocol-refs[opt]
1111/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001112/// @end
1113///
1114/// objc-protocol-forward-reference:
1115/// @protocol identifier-list ';'
1116///
1117/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001118/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001119/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001120Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1121 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001122 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001123 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1124 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Douglas Gregor083128f2009-11-18 04:49:41 +00001126 if (Tok.is(tok::code_completion)) {
1127 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1128 ConsumeToken();
1129 }
1130
Chris Lattnerdf195262007-10-09 17:51:17 +00001131 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001132 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001133 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001134 }
1135 // Save the protocol name, then consume it.
1136 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1137 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Chris Lattnerdf195262007-10-09 17:51:17 +00001139 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001140 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001141 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001142 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001143 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Chris Lattnerdf195262007-10-09 17:51:17 +00001146 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001147 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1148 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1149
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001150 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001151 while (1) {
1152 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001153 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001154 Diag(Tok, diag::err_expected_ident);
1155 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001156 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001157 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001158 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1159 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001160 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattnerdf195262007-10-09 17:51:17 +00001162 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001163 break;
1164 }
1165 // Consume the ';'.
1166 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001167 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Steve Naroffe440eb82007-10-10 17:32:04 +00001169 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001170 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001171 ProtocolRefs.size(),
1172 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001175 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001176 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001177
Chris Lattnerb28317a2009-03-28 19:18:32 +00001178 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001179 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001180 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001181 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1182 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001183 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattnerb28317a2009-03-28 19:18:32 +00001185 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001186 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001187 ProtocolRefs.data(),
1188 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001189 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001190 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001191 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001192 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001193}
Steve Naroffdac269b2007-08-20 21:31:48 +00001194
1195/// objc-implementation:
1196/// objc-class-implementation-prologue
1197/// objc-category-implementation-prologue
1198///
1199/// objc-class-implementation-prologue:
1200/// @implementation identifier objc-superclass[opt]
1201/// objc-class-instance-variables[opt]
1202///
1203/// objc-category-implementation-prologue:
1204/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001205Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001206 SourceLocation atLoc) {
1207 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1208 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1209 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001211 // Code completion after '@implementation'.
1212 if (Tok.is(tok::code_completion)) {
1213 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1214 ConsumeToken();
1215 }
1216
Chris Lattnerdf195262007-10-09 17:51:17 +00001217 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001218 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001219 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001220 }
1221 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001222 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001223 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001224
1225 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001226 // we have a category implementation.
1227 SourceLocation lparenLoc = ConsumeParen();
1228 SourceLocation categoryLoc, rparenLoc;
1229 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001231 if (Tok.is(tok::code_completion)) {
1232 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1233 ConsumeToken();
1234 }
1235
Chris Lattnerdf195262007-10-09 17:51:17 +00001236 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001237 categoryId = Tok.getIdentifierInfo();
1238 categoryLoc = ConsumeToken();
1239 } else {
1240 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001241 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001242 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001243 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001244 Diag(Tok, diag::err_expected_rparen);
1245 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001246 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001247 }
1248 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001249 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001250 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001251 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001252 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001253 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001254 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001255 }
1256 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001257 SourceLocation superClassLoc;
1258 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001259 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001260 // We have a super class
1261 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001262 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001263 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001264 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001265 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001266 superClassId = Tok.getIdentifierInfo();
1267 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001268 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001269 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001270 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001271 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Steve Naroff60fccee2007-10-29 21:38:07 +00001273 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001274 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001275 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001276 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001277 PendingObjCImpDecl.push_back(ObjCImpDecl);
1278
Chris Lattnerb28317a2009-03-28 19:18:32 +00001279 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001280}
Steve Naroff60fccee2007-10-29 21:38:07 +00001281
Ted Kremenek782f2f52010-01-07 01:20:12 +00001282Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001283 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1284 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001285 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001286 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001287 if (ObjCImpDecl) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001288 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001289 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001290 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001291 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001292 else {
1293 // missing @implementation
1294 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1295 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001296 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001297}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001298
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001299Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001300 if (PendingObjCImpDecl.empty())
1301 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1302 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenek782f2f52010-01-07 01:20:12 +00001303 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001304 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1305}
1306
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001307/// compatibility-alias-decl:
1308/// @compatibility_alias alias-name class-name ';'
1309///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001310Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001311 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1312 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1313 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001314 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001315 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001316 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001317 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001318 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1319 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001320 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001321 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001322 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001323 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001324 IdentifierInfo *classId = Tok.getIdentifierInfo();
1325 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1326 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001327 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001328 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001329 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001330 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1331 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001332}
1333
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001334/// property-synthesis:
1335/// @synthesize property-ivar-list ';'
1336///
1337/// property-ivar-list:
1338/// property-ivar
1339/// property-ivar-list ',' property-ivar
1340///
1341/// property-ivar:
1342/// identifier
1343/// identifier '=' identifier
1344///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001345Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001346 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1347 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001348 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Douglas Gregorb328c422009-11-18 19:45:45 +00001350 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001351 if (Tok.is(tok::code_completion)) {
Douglas Gregor424b2a52009-11-18 22:56:13 +00001352 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor322328b2009-11-18 22:32:06 +00001353 ConsumeToken();
1354 }
1355
Douglas Gregorb328c422009-11-18 19:45:45 +00001356 if (Tok.isNot(tok::identifier)) {
1357 Diag(Tok, diag::err_synthesized_property_name);
1358 SkipUntil(tok::semi);
1359 return DeclPtrTy();
1360 }
1361
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001362 IdentifierInfo *propertyIvar = 0;
1363 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1364 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001365 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001366 // property '=' ivar-name
1367 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001368
1369 if (Tok.is(tok::code_completion)) {
1370 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1371 ObjCImpDecl);
1372 ConsumeToken();
1373 }
1374
Chris Lattnerdf195262007-10-09 17:51:17 +00001375 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001376 Diag(Tok, diag::err_expected_ident);
1377 break;
1378 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001379 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001380 ConsumeToken(); // consume ivar-name
1381 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001382 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1383 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001384 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001385 break;
1386 ConsumeToken(); // consume ','
1387 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001388 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001389 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001390 SkipUntil(tok::semi);
1391 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001392 else
1393 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001394 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001395}
1396
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001397/// property-dynamic:
1398/// @dynamic property-list
1399///
1400/// property-list:
1401/// identifier
1402/// property-list ',' identifier
1403///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001404Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001405 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1406 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1407 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001408 while (true) {
1409 if (Tok.is(tok::code_completion)) {
1410 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1411 ConsumeToken();
1412 }
1413
1414 if (Tok.isNot(tok::identifier)) {
1415 Diag(Tok, diag::err_expected_ident);
1416 SkipUntil(tok::semi);
1417 return DeclPtrTy();
1418 }
1419
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001420 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1421 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1422 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1423 propertyId, 0);
1424
Chris Lattnerdf195262007-10-09 17:51:17 +00001425 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001426 break;
1427 ConsumeToken(); // consume ','
1428 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001429 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001430 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001431 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001432}
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001434/// objc-throw-statement:
1435/// throw expression[opt];
1436///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001437Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001438 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001439 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001440 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001441 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001442 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001443 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001444 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001445 }
1446 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001447 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001448 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001449}
1450
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001451/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001452/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001453///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001454Parser::OwningStmtResult
1455Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001456 ConsumeToken(); // consume synchronized
1457 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001458 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001459 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001460 }
1461 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001462 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001463 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001464 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001465 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001466 }
1467 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001468 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001469 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001470 }
1471 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001472 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001473 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001474 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001475 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001476 // Enter a scope to hold everything within the compound stmt. Compound
1477 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001478 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001479
Sebastian Redl61364dd2008-12-11 19:30:53 +00001480 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001481
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001482 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001483 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001484 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001485 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001486}
1487
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001488/// objc-try-catch-statement:
1489/// @try compound-statement objc-catch-list[opt]
1490/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1491///
1492/// objc-catch-list:
1493/// @catch ( parameter-declaration ) compound-statement
1494/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1495/// catch-parameter-declaration:
1496/// parameter-declaration
1497/// '...' [OBJC2]
1498///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001499Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001500 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001501
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001502 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001503 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001504 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001505 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001506 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001507 OwningStmtResult CatchStmts(Actions);
1508 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001509 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001510 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001511 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001512 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001513 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001514
Chris Lattnerdf195262007-10-09 17:51:17 +00001515 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001516 // At this point, we need to lookahead to determine if this @ is the start
1517 // of an @catch or @finally. We don't want to consume the @ token if this
1518 // is an @try or @encode or something else.
1519 Token AfterAt = GetLookAheadToken(1);
1520 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1521 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1522 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001523
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001524 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001525 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001526 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001527 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001528 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001529 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001530 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001531 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001532 DeclSpec DS;
1533 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001534 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001535 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001536 // we must accept either 'declarator' or 'abstract-declarator' here.
1537 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1538 ParseDeclarator(ParmDecl);
1539
1540 // Inform the actions module about the parameter declarator, so it
1541 // gets added to the current scope.
Fariborz Jahaniand219a3a2010-02-03 00:32:51 +00001542 // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
Steve Naroff7ba138a2009-03-03 19:52:17 +00001543 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian2f764f12010-02-03 00:01:43 +00001544 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroff64515f32008-02-05 21:27:35 +00001545 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001546 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Steve Naroff93a25952009-04-07 22:56:58 +00001548 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Steve Naroff93a25952009-04-07 22:56:58 +00001550 if (Tok.is(tok::r_paren))
1551 RParenLoc = ConsumeParen();
1552 else // Skip over garbage, until we get to ')'. Eat the ')'.
1553 SkipUntil(tok::r_paren, true, false);
1554
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001555 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001556 if (Tok.is(tok::l_brace))
1557 CatchBody = ParseCompoundStatementBody();
1558 else
1559 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001560 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001561 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001562 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001563 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001564 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001565 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001566 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1567 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001568 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001569 }
1570 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001571 } else {
1572 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001573 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001574 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001575
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001576 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001577 if (Tok.is(tok::l_brace))
1578 FinallyBody = ParseCompoundStatementBody();
1579 else
1580 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001581 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001582 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001583 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001584 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001585 catch_or_finally_seen = true;
1586 break;
1587 }
1588 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001589 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001590 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001591 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001592 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001593 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1594 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001595}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001596
Steve Naroff3536b442007-09-06 21:24:23 +00001597/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001598///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001599Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1600 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Chris Lattner49f28ca2009-03-05 08:00:35 +00001602 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1603 PP.getSourceManager(),
1604 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001606 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001607 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001608 if (ObjCImpDecl) {
1609 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001610 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001611 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001612 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001613 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001614
Steve Naroff409be832007-11-11 19:54:21 +00001615 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001616 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001617 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Steve Naroff409be832007-11-11 19:54:21 +00001619 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1620 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Steve Naroff409be832007-11-11 19:54:21 +00001622 // If we didn't find the '{', bail out.
1623 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001624 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001625 }
Steve Naroff409be832007-11-11 19:54:21 +00001626 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Steve Naroff409be832007-11-11 19:54:21 +00001628 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001629 ParseScope BodyScope(this,
1630 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Steve Naroff409be832007-11-11 19:54:21 +00001632 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001633 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001634 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001635
1636 OwningStmtResult FnBody(ParseCompoundStatementBody());
1637
Steve Naroff409be832007-11-11 19:54:21 +00001638 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001639 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001640 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1641 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001642
Steve Naroff32ce8372009-03-02 22:00:56 +00001643 // TODO: Pass argument information.
1644 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Steve Naroff409be832007-11-11 19:54:21 +00001646 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001647 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001648
Steve Naroff71c0a952007-11-13 23:01:27 +00001649 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001650}
Anders Carlsson55085182007-08-21 17:43:55 +00001651
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001652Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001653 if (Tok.is(tok::code_completion)) {
1654 Actions.CodeCompleteObjCAtStatement(CurScope);
1655 ConsumeToken();
1656 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001657 }
1658
1659 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001660 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001661
1662 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001663 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001664
1665 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001666 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001667
Sebastian Redld8c4e152008-12-11 22:33:27 +00001668 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001669 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001670 // If the expression is invalid, skip ahead to the next semicolon. Not
1671 // doing this opens us up to the possibility of infinite loops if
1672 // ParseExpression does not consume any tokens.
1673 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001674 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001675 }
Chris Lattner5d803162009-12-07 16:33:19 +00001676
Steve Naroff64515f32008-02-05 21:27:35 +00001677 // Otherwise, eat the semicolon.
1678 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001679 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001680}
1681
Sebastian Redl1d922962008-12-13 15:32:12 +00001682Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001683 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001684 case tok::code_completion:
1685 Actions.CodeCompleteObjCAtExpression(CurScope);
1686 ConsumeToken();
1687 return ExprError();
1688
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001689 case tok::string_literal: // primary-expression: string-literal
1690 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001691 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001692 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001693 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001694 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001695
Chris Lattner4fef81d2008-08-05 06:19:09 +00001696 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1697 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001698 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001699 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001700 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001701 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001702 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001703 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001704 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001705 }
Anders Carlsson55085182007-08-21 17:43:55 +00001706 }
Anders Carlsson55085182007-08-21 17:43:55 +00001707}
1708
Mike Stump1eb44332009-09-09 15:08:12 +00001709/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001710/// '[' objc-receiver objc-message-args ']'
1711///
1712/// objc-receiver:
Chris Lattnereb483eb2010-04-11 08:28:14 +00001713/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001714/// expression
1715/// class-name
1716/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001717Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001718 assert(Tok.is(tok::l_square) && "'[' expected");
1719 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1720
Chris Lattnereb483eb2010-04-11 08:28:14 +00001721 if (Tok.is(tok::identifier)) {
1722 IdentifierInfo *II = Tok.getIdentifierInfo();
1723
1724 // If this is '[' 'super', then this is a magic superclass message.
1725 // We parse '[' 'super' '.' 'foo' as an expression?
Chris Lattner15faee12010-04-12 05:38:43 +00001726 if ((II == Ident_super && GetLookAheadToken(1).isNot(tok::period) &&
1727 CurScope->isInObjcMethodScope()) ||
Chris Lattnereb483eb2010-04-11 08:28:14 +00001728 // Check to see if this is a typename. If so, it is a class message.
1729 Actions.getTypeName(*II, Tok.getLocation(), CurScope)) {
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001730 SourceLocation NameLoc = ConsumeToken();
Chris Lattnereb483eb2010-04-11 08:28:14 +00001731 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, II,
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001732 ExprArg(Actions));
1733 }
Chris Lattner699b6612008-01-25 18:59:06 +00001734 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00001735
1736 // Otherwise, an arbitrary expression can be the receiver of a send.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001737 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001738 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001739 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001740 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001741 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001742
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001743 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001744 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001745}
Sebastian Redl1d922962008-12-13 15:32:12 +00001746
Chris Lattner699b6612008-01-25 18:59:06 +00001747/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1748/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001749///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001750/// objc-message-args:
1751/// objc-selector
1752/// objc-keywordarg-list
1753///
1754/// objc-keywordarg-list:
1755/// objc-keywordarg
1756/// objc-keywordarg-list objc-keywordarg
1757///
Mike Stump1eb44332009-09-09 15:08:12 +00001758/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001759/// selector-name[opt] ':' objc-keywordexpr
1760///
1761/// objc-keywordexpr:
1762/// nonempty-expr-list
1763///
1764/// nonempty-expr-list:
1765/// assignment-expression
1766/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001767///
1768Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001769Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001770 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001771 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001772 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001773 if (Tok.is(tok::code_completion)) {
1774 if (ReceiverName)
Douglas Gregord3c68542009-11-19 01:08:35 +00001775 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1776 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001777 else
Douglas Gregord3c68542009-11-19 01:08:35 +00001778 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1779 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001780 ConsumeToken();
1781 }
Douglas Gregord3c68542009-11-19 01:08:35 +00001782
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001783 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001784 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001785 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001786
Anders Carlssonff975cf2009-02-14 18:21:46 +00001787 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Steve Naroff68d331a2007-09-27 14:38:14 +00001789 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001790 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001791
Chris Lattnerdf195262007-10-09 17:51:17 +00001792 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001793 while (1) {
1794 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001795 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001796
Chris Lattnerdf195262007-10-09 17:51:17 +00001797 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001798 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001799 // We must manually skip to a ']', otherwise the expression skipper will
1800 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1801 // the enclosing expression.
1802 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001803 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001804 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001805
Steve Naroff68d331a2007-09-27 14:38:14 +00001806 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001807 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001808 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001809 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001810 // We must manually skip to a ']', otherwise the expression skipper will
1811 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1812 // the enclosing expression.
1813 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001814 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001815 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001816
Steve Naroff37387c92007-09-17 20:25:27 +00001817 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001818 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001819
Douglas Gregord3c68542009-11-19 01:08:35 +00001820 // Code completion after each argument.
1821 if (Tok.is(tok::code_completion)) {
1822 if (ReceiverName)
1823 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1824 KeyIdents.data(),
1825 KeyIdents.size());
1826 else
1827 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1828 KeyIdents.data(),
1829 KeyIdents.size());
1830 ConsumeToken();
1831 }
1832
Steve Naroff37387c92007-09-17 20:25:27 +00001833 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001834 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001835 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001836 break;
1837 // We have a selector or a colon, continue parsing.
1838 }
1839 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001840 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001841 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001842 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001843 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001844 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001845 // We must manually skip to a ']', otherwise the expression skipper will
1846 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1847 // the enclosing expression.
1848 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001849 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001850 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001851
Steve Naroff49f109c2007-11-15 13:05:42 +00001852 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001853 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001854 }
1855 } else if (!selIdent) {
1856 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001857
Chris Lattner4fef81d2008-08-05 06:19:09 +00001858 // We must manually skip to a ']', otherwise the expression skipper will
1859 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1860 // the enclosing expression.
1861 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001862 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001863 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00001864
Chris Lattnerdf195262007-10-09 17:51:17 +00001865 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00001866 if (Tok.is(tok::identifier))
1867 Diag(Tok, diag::err_expected_colon);
1868 else
1869 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001870 // We must manually skip to a ']', otherwise the expression skipper will
1871 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1872 // the enclosing expression.
1873 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001874 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001875 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001876
Chris Lattner699b6612008-01-25 18:59:06 +00001877 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001878
Steve Naroff29238a02007-10-05 18:42:47 +00001879 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001880 if (nKeys == 0)
1881 KeyIdents.push_back(selIdent);
1882 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001883
Chris Lattnerff384912007-10-07 02:00:24 +00001884 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001885 if (ReceiverName)
1886 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001887 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001888 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001889 KeyExprs.take(), KeyExprs.size()));
1890 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001891 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001892 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001893}
1894
Sebastian Redl1d922962008-12-13 15:32:12 +00001895Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001896 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001897 if (Res.isInvalid()) return move(Res);
1898
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001899 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1900 // expressions. At this point, we know that the only valid thing that starts
1901 // with '@' is an @"".
1902 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001903 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001904 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001905 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001906
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001907 while (Tok.is(tok::at)) {
1908 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001909
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001910 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001911 if (!isTokenStringLiteral())
1912 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001913
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001914 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001915 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001916 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001917
Sebastian Redleffa8d12008-12-10 00:02:53 +00001918 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001919 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001920
1921 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1922 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001923}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001924
1925/// objc-encode-expression:
1926/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001927Parser::OwningExprResult
1928Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001929 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001930
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001931 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001932
Chris Lattner4fef81d2008-08-05 06:19:09 +00001933 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001934 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1935
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001936 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001937
Douglas Gregor809070a2009-02-18 17:45:20 +00001938 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001939
Anders Carlsson4988ae32007-08-23 15:31:37 +00001940 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001941
Douglas Gregor809070a2009-02-18 17:45:20 +00001942 if (Ty.isInvalid())
1943 return ExprError();
1944
Mike Stump1eb44332009-09-09 15:08:12 +00001945 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001946 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001947}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001948
1949/// objc-protocol-expression
1950/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001951Parser::OwningExprResult
1952Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001953 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001954
Chris Lattner4fef81d2008-08-05 06:19:09 +00001955 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001956 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1957
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001958 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001959
Chris Lattner4fef81d2008-08-05 06:19:09 +00001960 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001961 return ExprError(Diag(Tok, diag::err_expected_ident));
1962
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001963 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001964 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001965
Anders Carlsson4988ae32007-08-23 15:31:37 +00001966 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001967
Sebastian Redl1d922962008-12-13 15:32:12 +00001968 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1969 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001970}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001971
1972/// objc-selector-expression
1973/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001974Parser::OwningExprResult
1975Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001976 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001977
Chris Lattner4fef81d2008-08-05 06:19:09 +00001978 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001979 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1980
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001981 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001982 SourceLocation LParenLoc = ConsumeParen();
1983 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001984 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001985 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1986 return ExprError(Diag(Tok, diag::err_expected_ident));
1987
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001988 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001989 unsigned nColons = 0;
1990 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001991 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001992 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001993 return ExprError(Diag(Tok, diag::err_expected_colon));
1994
Chris Lattnercb53b362007-12-27 19:57:00 +00001995 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001996 ConsumeToken(); // Eat the ':'.
1997 if (Tok.is(tok::r_paren))
1998 break;
1999 // Check for another keyword selector.
2000 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002001 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002002 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002003 if (!SelIdent && Tok.isNot(tok::colon))
2004 break;
2005 }
Steve Naroff887407e2007-12-05 22:21:29 +00002006 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002007 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002008 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002009 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2010 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002011 }