blob: 3014f95a8482f6baa4549245d17695bfa477d36a [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 "@"
32
Steve Naroff861cf3e2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000053 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Chris Lattnerb28317a2009-03-28 19:18:32 +000061Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000069 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
Reid Spencer5f016e22007-07-11 17:01:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnerdf195262007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000082 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000083
Steve Naroffe440eb82007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Steve Naroffdac269b2007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000116Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnerdf195262007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000124 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000125 }
126 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnerdf195262007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
134
Steve Naroff527fe232007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000146 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 }
148 rparenLoc = ConsumeParen();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000149
Steve Naroffdac269b2007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000151 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000152 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000153 if (Tok.is(tok::less) &&
154 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000155 return DeclPtrTy();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000156
Steve Naroffdac269b2007-08-20 21:31:48 +0000157 if (attrList) // categories don't support attributes.
158 Diag(Tok, diag::err_objc_no_attributes_on_category);
159
Jay Foadbeaaccd2009-05-21 09:52:38 +0000160 DeclPtrTy CategoryType =
161 Actions.ActOnStartCategoryInterface(atLoc,
162 nameId, nameLoc,
163 categoryId, categoryLoc,
164 ProtocolRefs.data(),
165 ProtocolRefs.size(),
166 EndProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000167
168 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000169 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 }
171 // Parse a class interface.
172 IdentifierInfo *superClassId = 0;
173 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000174
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000179 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 }
181 superClassId = Tok.getIdentifierInfo();
182 superClassLoc = ConsumeToken();
183 }
184 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000185 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Chris Lattner06036d32008-07-26 04:13:19 +0000186 SourceLocation EndProtoLoc;
187 if (Tok.is(tok::less) &&
188 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000189 return DeclPtrTy();
Chris Lattner06036d32008-07-26 04:13:19 +0000190
Chris Lattnerb28317a2009-03-28 19:18:32 +0000191 DeclPtrTy ClsType =
Chris Lattner06036d32008-07-26 04:13:19 +0000192 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
193 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000194 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattner06036d32008-07-26 04:13:19 +0000195 EndProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000196
Chris Lattnerdf195262007-10-09 17:51:17 +0000197 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000198 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000199
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000200 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000201 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000202}
203
204/// objc-interface-decl-list:
205/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000206/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000207/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000208/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000209/// objc-interface-decl-list declaration
210/// objc-interface-decl-list ';'
211///
Steve Naroff294494e2007-08-22 16:35:03 +0000212/// objc-method-requirement: [OBJC2]
213/// @required
214/// @optional
215///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000217 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000218 llvm::SmallVector<DeclPtrTy, 32> allMethods;
219 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000220 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000221 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000222
Chris Lattnerbc662af2008-10-20 06:10:06 +0000223 SourceLocation AtEndLoc;
224
Steve Naroff294494e2007-08-22 16:35:03 +0000225 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000226 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000227 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000228 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000229 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000230 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000231 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
232 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000233 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
234 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000235 continue;
236 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000237
Chris Lattnere82a10f2008-10-20 05:46:22 +0000238 // Ignore excess semicolons.
239 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000240 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000241 continue;
242 }
243
Chris Lattnerbc662af2008-10-20 06:10:06 +0000244 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000245 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000246 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000247
248 // If we don't have an @ directive, parse it as a function definition.
249 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000250 // The code below does not consume '}'s because it is afraid of eating the
251 // end of a namespace. Because of the way this code is structured, an
252 // erroneous r_brace would cause an infinite loop if not handled here.
253 if (Tok.is(tok::r_brace))
254 break;
255
Steve Naroff4985ace2007-08-22 18:35:33 +0000256 // FIXME: as the name implies, this rule allows function definitions.
257 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner682bf922009-03-29 16:50:03 +0000258 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000259 continue;
260 }
261
262 // Otherwise, we have an @ directive, eat the @.
263 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000264 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000265
Chris Lattnera2449b22008-10-20 05:57:40 +0000266 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000267 AtEndLoc = AtLoc;
268 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000269 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000270
Chris Lattnerbc662af2008-10-20 06:10:06 +0000271 // Eat the identifier.
272 ConsumeToken();
273
Chris Lattnera2449b22008-10-20 05:57:40 +0000274 switch (DirectiveKind) {
275 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000276 // FIXME: If someone forgets an @end on a protocol, this loop will
277 // continue to eat up tons of stuff and spew lots of nonsense errors. It
278 // would probably be better to bail out if we saw an @class or @interface
279 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000280 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000281 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000282 SkipUntil(tok::r_brace, tok::at);
283 break;
284
285 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000286 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000287 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000288 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000289 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000290 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000292 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000293 break;
294
295 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000296 if (!getLang().ObjC2)
297 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
298
Chris Lattnere82a10f2008-10-20 05:46:22 +0000299 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000300 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000301 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000302 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000303
Chris Lattnere82a10f2008-10-20 05:46:22 +0000304 // Parse all the comma separated declarators.
305 DeclSpec DS;
306 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
307 ParseStructDeclaration(DS, FieldDeclarators);
308
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000309 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
310 tok::at);
311
Chris Lattnere82a10f2008-10-20 05:46:22 +0000312 // Convert them all to property declarations.
313 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
314 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000315 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000316 Diag(AtLoc, diag::err_objc_property_requires_field_name)
317 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000318 continue;
319 }
Fariborz Jahanian573acde2009-01-17 23:21:10 +0000320 if (FD.BitfieldSize) {
321 Diag(AtLoc, diag::err_objc_property_bitfield)
322 << FD.D.getSourceRange();
323 continue;
324 }
Chris Lattnerda3253d2008-10-20 06:33:53 +0000325
Chris Lattnere82a10f2008-10-20 05:46:22 +0000326 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000327 IdentifierInfo *SelName =
328 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
329
Chris Lattnere82a10f2008-10-20 05:46:22 +0000330 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000331 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000332 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahanian2e050f12009-03-12 22:34:11 +0000333 Selector SetterSel;
334 if (SetterName)
335 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
336 else
337 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
338 PP.getSelectorTable(),
339 FD.D.getIdentifier());
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000340 bool isOverridingProperty = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000341 DeclPtrTy Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
342 GetterSel, SetterSel,
343 interfaceDecl,
344 &isOverridingProperty,
345 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000346 if (!isOverridingProperty)
347 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000348 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000349 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000350 }
Steve Naroff294494e2007-08-22 16:35:03 +0000351 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000352
353 // We break out of the big loop in two cases: when we see @end or when we see
354 // EOF. In the former case, eat the @end. In the later case, emit an error.
355 if (Tok.isObjCAtKeyword(tok::objc_end))
356 ConsumeToken(); // the "end" identifier
357 else
358 Diag(Tok, diag::err_objc_missing_end);
359
Chris Lattnera2449b22008-10-20 05:57:40 +0000360 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000361 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000362 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000363 allMethods.data(), allMethods.size(),
364 allProperties.data(), allProperties.size(),
365 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000366}
367
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000368/// Parse property attribute declarations.
369///
370/// property-attr-decl: '(' property-attrlist ')'
371/// property-attrlist:
372/// property-attribute
373/// property-attrlist ',' property-attribute
374/// property-attribute:
375/// getter '=' identifier
376/// setter '=' identifier ':'
377/// readonly
378/// readwrite
379/// assign
380/// retain
381/// copy
382/// nonatomic
383///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000384void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000385 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000386 SourceLocation LHSLoc = ConsumeParen(); // consume '('
387
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000388 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000389 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000390
391 // If this is not an identifier at all, bail out early.
392 if (II == 0) {
393 MatchRHSPunctuation(tok::r_paren, LHSLoc);
394 return;
395 }
396
Chris Lattner156b0612008-10-20 07:37:22 +0000397 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
398
Chris Lattner92e62b02008-11-20 04:42:34 +0000399 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000400 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000401 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000402 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000403 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000404 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000405 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000406 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000407 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000409 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000410 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000411 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000412 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000413 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
414 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000415 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000416
Chris Lattner8ca329c2008-10-20 07:24:39 +0000417 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000418 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000419 SkipUntil(tok::r_paren);
420 return;
421 }
422
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000423 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000424 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
425 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000426 ConsumeToken(); // consume method name
427
428 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
429 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000430 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000431 } else {
432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
433 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000434 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000435 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000436 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000437 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000438 SkipUntil(tok::r_paren);
439 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000440 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000441
Chris Lattner156b0612008-10-20 07:37:22 +0000442 if (Tok.isNot(tok::comma))
443 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000444
Chris Lattner156b0612008-10-20 07:37:22 +0000445 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000446 }
Chris Lattner156b0612008-10-20 07:37:22 +0000447
448 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000449}
450
Steve Naroff3536b442007-09-06 21:24:23 +0000451/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000452/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000453/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000454///
455/// objc-instance-method: '-'
456/// objc-class-method: '+'
457///
Steve Naroff4985ace2007-08-22 18:35:33 +0000458/// objc-method-attributes: [OBJC2]
459/// __attribute__((deprecated))
460///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000461Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
462 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000463 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000464
465 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000466 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000467
Chris Lattnerb28317a2009-03-28 19:18:32 +0000468 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000469 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000470 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000471 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000472}
473
474/// objc-selector:
475/// identifier
476/// one of
477/// enum struct union if else while do for switch case default
478/// break continue return goto asm sizeof typeof __alignof
479/// unsigned long const short volatile signed restrict _Complex
480/// in out inout bycopy byref oneway int char float double void _Bool
481///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000482IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000483 switch (Tok.getKind()) {
484 default:
485 return 0;
486 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000487 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000488 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000489 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000490 case tok::kw_break:
491 case tok::kw_case:
492 case tok::kw_catch:
493 case tok::kw_char:
494 case tok::kw_class:
495 case tok::kw_const:
496 case tok::kw_const_cast:
497 case tok::kw_continue:
498 case tok::kw_default:
499 case tok::kw_delete:
500 case tok::kw_do:
501 case tok::kw_double:
502 case tok::kw_dynamic_cast:
503 case tok::kw_else:
504 case tok::kw_enum:
505 case tok::kw_explicit:
506 case tok::kw_export:
507 case tok::kw_extern:
508 case tok::kw_false:
509 case tok::kw_float:
510 case tok::kw_for:
511 case tok::kw_friend:
512 case tok::kw_goto:
513 case tok::kw_if:
514 case tok::kw_inline:
515 case tok::kw_int:
516 case tok::kw_long:
517 case tok::kw_mutable:
518 case tok::kw_namespace:
519 case tok::kw_new:
520 case tok::kw_operator:
521 case tok::kw_private:
522 case tok::kw_protected:
523 case tok::kw_public:
524 case tok::kw_register:
525 case tok::kw_reinterpret_cast:
526 case tok::kw_restrict:
527 case tok::kw_return:
528 case tok::kw_short:
529 case tok::kw_signed:
530 case tok::kw_sizeof:
531 case tok::kw_static:
532 case tok::kw_static_cast:
533 case tok::kw_struct:
534 case tok::kw_switch:
535 case tok::kw_template:
536 case tok::kw_this:
537 case tok::kw_throw:
538 case tok::kw_true:
539 case tok::kw_try:
540 case tok::kw_typedef:
541 case tok::kw_typeid:
542 case tok::kw_typename:
543 case tok::kw_typeof:
544 case tok::kw_union:
545 case tok::kw_unsigned:
546 case tok::kw_using:
547 case tok::kw_virtual:
548 case tok::kw_void:
549 case tok::kw_volatile:
550 case tok::kw_wchar_t:
551 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000552 case tok::kw__Bool:
553 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000554 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000555 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000556 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000557 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000558 }
Steve Naroff294494e2007-08-22 16:35:03 +0000559}
560
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000561/// objc-for-collection-in: 'in'
562///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000563bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000564 // FIXME: May have to do additional look-ahead to only allow for
565 // valid tokens following an 'in'; such as an identifier, unary operators,
566 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000567 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000568 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000569}
570
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000572/// qualifier list and builds their bitmask representation in the input
573/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000574///
575/// objc-type-qualifiers:
576/// objc-type-qualifier
577/// objc-type-qualifiers objc-type-qualifier
578///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000580 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000581 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000582 return;
583
584 const IdentifierInfo *II = Tok.getIdentifierInfo();
585 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000586 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000587 continue;
588
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000590 switch (i) {
591 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
593 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
594 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
595 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
596 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
597 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000598 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000599 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000600 ConsumeToken();
601 II = 0;
602 break;
603 }
604
605 // If this wasn't a recognized qualifier, bail out.
606 if (II) return;
607 }
608}
609
610/// objc-type-name:
611/// '(' objc-type-qualifiers[opt] type-name ')'
612/// '(' objc-type-qualifiers[opt] ')'
613///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000614Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000615 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000616
Chris Lattner4a76b292008-10-22 03:52:06 +0000617 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000618 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000619
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000620 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000622
Chris Lattner4a76b292008-10-22 03:52:06 +0000623 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000624 if (isTypeSpecifierQualifier()) {
625 TypeResult TypeSpec = ParseTypeName();
626 if (!TypeSpec.isInvalid())
627 Ty = TypeSpec.get();
628 }
Chris Lattnere8904e92008-08-23 01:48:03 +0000629
Steve Naroffd7333c22008-10-21 14:15:04 +0000630 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000631 ConsumeParen();
632 else if (Tok.getLocation() == TypeStartLoc) {
633 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000634 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000635 SkipUntil(tok::r_paren);
636 } else {
637 // Otherwise, we found *something*, but didn't get a ')' in the right
638 // place. Emit an error then return what we have as the type.
639 MatchRHSPunctuation(tok::r_paren, LParenLoc);
640 }
Steve Narofff28b2642007-09-05 23:30:30 +0000641 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000642}
643
644/// objc-method-decl:
645/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000646/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000647/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000648/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000649///
650/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000651/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000652/// objc-keyword-selector objc-keyword-decl
653///
654/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000655/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
656/// objc-selector ':' objc-keyword-attributes[opt] identifier
657/// ':' objc-type-name objc-keyword-attributes[opt] identifier
658/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000659///
Steve Naroff4985ace2007-08-22 18:35:33 +0000660/// objc-parmlist:
661/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000662///
Steve Naroff4985ace2007-08-22 18:35:33 +0000663/// objc-parms:
664/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000665///
Steve Naroff4985ace2007-08-22 18:35:33 +0000666/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000667/// , ...
668///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000669/// objc-keyword-attributes: [OBJC2]
670/// __attribute__((unused))
671///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000672Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
673 tok::TokenKind mType,
674 DeclPtrTy IDecl,
675 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnere8904e92008-08-23 01:48:03 +0000676 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000677 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000678 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000679 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000680 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000681
Steve Naroffbef11852007-10-26 20:53:56 +0000682 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000683 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000684
Steve Naroff84c43102009-02-11 20:43:13 +0000685 // An unnamed colon is valid.
686 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000687 Diag(Tok, diag::err_expected_selector_for_method)
688 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000689 // Skip until we get a ; or {}.
690 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000691 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000692 }
693
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000694 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000695 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000696 // If attributes exist after the method, parse them.
697 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000698 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000699 MethodAttrs = ParseAttributes();
700
701 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000702 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000703 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000704 0, CargNames, MethodAttrs,
705 MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000706 }
Steve Narofff28b2642007-09-05 23:30:30 +0000707
Steve Naroff68d331a2007-09-27 14:38:14 +0000708 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000709 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Chris Lattnerff384912007-10-07 02:00:24 +0000710
Chris Lattnerff384912007-10-07 02:00:24 +0000711 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000712 Action::ObjCArgInfo ArgInfo;
Steve Naroff68d331a2007-09-27 14:38:14 +0000713
Chris Lattnerff384912007-10-07 02:00:24 +0000714 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000715 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000716 Diag(Tok, diag::err_expected_colon);
717 break;
718 }
719 ConsumeToken(); // Eat the ':'.
Steve Narofff28b2642007-09-05 23:30:30 +0000720
Chris Lattnere294d3f2009-04-11 18:57:04 +0000721 ArgInfo.Type = 0;
722 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
723 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
724
Chris Lattnerff384912007-10-07 02:00:24 +0000725 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000726 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000727 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000728 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000729
Chris Lattnerdf195262007-10-09 17:51:17 +0000730 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000731 Diag(Tok, diag::err_expected_ident); // missing argument name.
732 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000733 }
Chris Lattnere294d3f2009-04-11 18:57:04 +0000734
735 ArgInfo.Name = Tok.getIdentifierInfo();
736 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000737 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000738
Chris Lattnere294d3f2009-04-11 18:57:04 +0000739 ArgInfos.push_back(ArgInfo);
740 KeyIdents.push_back(SelIdent);
741
Chris Lattnerff384912007-10-07 02:00:24 +0000742 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000743 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000744 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000745 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000746 break;
747 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000748 }
Chris Lattnerff384912007-10-07 02:00:24 +0000749
Steve Naroff335eafa2007-11-15 12:35:21 +0000750 bool isVariadic = false;
751
Chris Lattnerff384912007-10-07 02:00:24 +0000752 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000753 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000754 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000755 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000756 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000757 ConsumeToken();
758 break;
759 }
Chris Lattnerff384912007-10-07 02:00:24 +0000760 DeclSpec DS;
761 ParseDeclarationSpecifiers(DS);
762 // Parse the declarator.
763 Declarator ParmDecl(DS, Declarator::PrototypeContext);
764 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000765 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000766 }
767
768 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000769 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000770 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000771 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000772 MethodAttrs = ParseAttributes();
773
774 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
775 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000776 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000777 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000778 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000779 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000780}
781
Steve Naroffdac269b2007-08-20 21:31:48 +0000782/// objc-protocol-refs:
783/// '<' identifier-list '>'
784///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000785bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000786ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Chris Lattnere13b9592008-07-26 04:03:38 +0000787 bool WarnOnDeclarations, SourceLocation &EndLoc) {
788 assert(Tok.is(tok::less) && "expected <");
789
790 ConsumeToken(); // the "<"
791
792 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
793
794 while (1) {
795 if (Tok.isNot(tok::identifier)) {
796 Diag(Tok, diag::err_expected_ident);
797 SkipUntil(tok::greater);
798 return true;
799 }
800 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
801 Tok.getLocation()));
802 ConsumeToken();
803
804 if (Tok.isNot(tok::comma))
805 break;
806 ConsumeToken();
807 }
808
809 // Consume the '>'.
810 if (Tok.isNot(tok::greater)) {
811 Diag(Tok, diag::err_expected_greater);
812 return true;
813 }
814
815 EndLoc = ConsumeAnyToken();
816
817 // Convert the list of protocols identifiers into a list of protocol decls.
818 Actions.FindProtocolDeclaration(WarnOnDeclarations,
819 &ProtocolIdents[0], ProtocolIdents.size(),
820 Protocols);
821 return false;
822}
823
Steve Naroffdac269b2007-08-20 21:31:48 +0000824/// objc-class-instance-variables:
825/// '{' objc-instance-variable-decl-list[opt] '}'
826///
827/// objc-instance-variable-decl-list:
828/// objc-visibility-spec
829/// objc-instance-variable-decl ';'
830/// ';'
831/// objc-instance-variable-decl-list objc-visibility-spec
832/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
833/// objc-instance-variable-decl-list ';'
834///
835/// objc-visibility-spec:
836/// @private
837/// @protected
838/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000839/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000840///
841/// objc-instance-variable-decl:
842/// struct-declaration
843///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000844void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000845 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000846 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000847 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000848 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
849
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000850 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000851
Steve Naroffddbff782007-08-21 21:17:12 +0000852 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000853
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000854 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000855 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000856 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000857 // Each iteration of this loop reads one objc-instance-variable-decl.
858
859 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000860 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000861 Diag(Tok, diag::ext_extra_struct_semi);
862 ConsumeToken();
863 continue;
864 }
Chris Lattnere1359422008-04-10 06:46:29 +0000865
Steve Naroffddbff782007-08-21 21:17:12 +0000866 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000867 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000868 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000869 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000870 case tok::objc_private:
871 case tok::objc_public:
872 case tok::objc_protected:
873 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000874 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000875 ConsumeToken();
876 continue;
877 default:
878 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000879 continue;
880 }
881 }
Chris Lattnere1359422008-04-10 06:46:29 +0000882
883 // Parse all the comma separated declarators.
884 DeclSpec DS;
885 FieldDeclarators.clear();
886 ParseStructDeclaration(DS, FieldDeclarators);
887
888 // Convert them all to fields.
889 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
890 FieldDeclarator &FD = FieldDeclarators[i];
891 // Install the declarator into interfaceDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000892 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
893 DS.getSourceRange().getBegin(),
894 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000895 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000896 }
Steve Naroff3536b442007-09-06 21:24:23 +0000897
Chris Lattnerdf195262007-10-09 17:51:17 +0000898 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000899 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000900 } else {
901 Diag(Tok, diag::err_expected_semi_decl_list);
902 // Skip to end of block or statement
903 SkipUntil(tok::r_brace, true, true);
904 }
905 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000906 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000907 // Call ActOnFields() even if we don't have any decls. This is useful
908 // for code rewriting tools that need to be aware of the empty list.
909 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000910 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000911 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000912 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000913}
Steve Naroffdac269b2007-08-20 21:31:48 +0000914
915/// objc-protocol-declaration:
916/// objc-protocol-definition
917/// objc-protocol-forward-reference
918///
919/// objc-protocol-definition:
920/// @protocol identifier
921/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000922/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000923/// @end
924///
925/// objc-protocol-forward-reference:
926/// @protocol identifier-list ';'
927///
928/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000929/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000930/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000931Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
932 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000933 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000934 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
935 ConsumeToken(); // the "protocol" identifier
936
Chris Lattnerdf195262007-10-09 17:51:17 +0000937 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000938 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000939 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000940 }
941 // Save the protocol name, then consume it.
942 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
943 SourceLocation nameLoc = ConsumeToken();
944
Chris Lattnerdf195262007-10-09 17:51:17 +0000945 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000946 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000947 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000948 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
949 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000950 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000951
Chris Lattnerdf195262007-10-09 17:51:17 +0000952 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000953 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
954 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
955
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000956 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000957 while (1) {
958 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000960 Diag(Tok, diag::err_expected_ident);
961 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000962 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000963 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000964 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
965 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000966 ConsumeToken(); // the identifier
967
Chris Lattnerdf195262007-10-09 17:51:17 +0000968 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000969 break;
970 }
971 // Consume the ';'.
972 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000973 return DeclPtrTy();
Chris Lattner7caeabd2008-07-21 22:17:28 +0000974
Steve Naroffe440eb82007-10-10 17:32:04 +0000975 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000976 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000977 ProtocolRefs.size(),
978 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000979 }
980
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000981 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000982 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000983
Chris Lattnerb28317a2009-03-28 19:18:32 +0000984 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000985 if (Tok.is(tok::less) &&
Chris Lattner58fe03b2009-04-12 08:43:13 +0000986 ParseObjCProtocolReferences(ProtocolRefs, false, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000987 return DeclPtrTy();
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000988
Chris Lattnerb28317a2009-03-28 19:18:32 +0000989 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +0000990 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000991 ProtocolRefs.data(),
992 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000993 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000994 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000995 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000996}
Steve Naroffdac269b2007-08-20 21:31:48 +0000997
998/// objc-implementation:
999/// objc-class-implementation-prologue
1000/// objc-category-implementation-prologue
1001///
1002/// objc-class-implementation-prologue:
1003/// @implementation identifier objc-superclass[opt]
1004/// objc-class-instance-variables[opt]
1005///
1006/// objc-category-implementation-prologue:
1007/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001008Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001009 SourceLocation atLoc) {
1010 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1011 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1012 ConsumeToken(); // the "implementation" identifier
1013
Chris Lattnerdf195262007-10-09 17:51:17 +00001014 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001015 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001016 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001017 }
1018 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001019 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001020 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1021
Chris Lattnerdf195262007-10-09 17:51:17 +00001022 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001023 // we have a category implementation.
1024 SourceLocation lparenLoc = ConsumeParen();
1025 SourceLocation categoryLoc, rparenLoc;
1026 IdentifierInfo *categoryId = 0;
1027
Chris Lattnerdf195262007-10-09 17:51:17 +00001028 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001029 categoryId = Tok.getIdentifierInfo();
1030 categoryLoc = ConsumeToken();
1031 } else {
1032 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001033 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001034 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001035 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001036 Diag(Tok, diag::err_expected_rparen);
1037 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001038 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001039 }
1040 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001041 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001042 atLoc, nameId, nameLoc, categoryId,
1043 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001044 ObjCImpDecl = ImplCatType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001045 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001046 }
1047 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001048 SourceLocation superClassLoc;
1049 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001050 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001051 // We have a super class
1052 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001053 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001054 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001055 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001056 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001057 superClassId = Tok.getIdentifierInfo();
1058 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001059 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001060 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001061 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001062 superClassId, superClassLoc);
1063
Steve Naroff60fccee2007-10-29 21:38:07 +00001064 if (Tok.is(tok::l_brace)) // we have ivars
1065 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001066 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001067
Chris Lattnerb28317a2009-03-28 19:18:32 +00001068 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001069}
Steve Naroff60fccee2007-10-29 21:38:07 +00001070
Chris Lattnerb28317a2009-03-28 19:18:32 +00001071Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001072 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1073 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001074 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001075 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001076 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001077 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001078 ObjCImpDecl = DeclPtrTy();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001079 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001080 else
1081 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001082 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001083}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001084
1085/// compatibility-alias-decl:
1086/// @compatibility_alias alias-name class-name ';'
1087///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001088Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001089 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1090 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1091 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001092 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001093 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001094 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001095 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001096 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1097 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001098 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001099 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001100 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001101 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001102 IdentifierInfo *classId = Tok.getIdentifierInfo();
1103 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1104 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001105 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001106 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001107 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001108 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1109 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001110}
1111
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001112/// property-synthesis:
1113/// @synthesize property-ivar-list ';'
1114///
1115/// property-ivar-list:
1116/// property-ivar
1117/// property-ivar-list ',' property-ivar
1118///
1119/// property-ivar:
1120/// identifier
1121/// identifier '=' identifier
1122///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001123Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001124 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1125 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001126 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001127 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001128 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001129 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001130 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001131
Chris Lattnerdf195262007-10-09 17:51:17 +00001132 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001133 IdentifierInfo *propertyIvar = 0;
1134 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1135 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001136 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001137 // property '=' ivar-name
1138 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001139 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001140 Diag(Tok, diag::err_expected_ident);
1141 break;
1142 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001143 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001144 ConsumeToken(); // consume ivar-name
1145 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001146 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1147 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001148 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001149 break;
1150 ConsumeToken(); // consume ','
1151 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001152 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001153 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001154 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001155}
1156
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001157/// property-dynamic:
1158/// @dynamic property-list
1159///
1160/// property-list:
1161/// identifier
1162/// property-list ',' identifier
1163///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001164Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001165 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1166 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1167 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001168 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001169 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001170 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001171 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001172 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001173 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1174 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1175 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1176 propertyId, 0);
1177
Chris Lattnerdf195262007-10-09 17:51:17 +00001178 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001179 break;
1180 ConsumeToken(); // consume ','
1181 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001183 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001184 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001185}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001186
1187/// objc-throw-statement:
1188/// throw expression[opt];
1189///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001190Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001191 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001192 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001193 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001194 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001195 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001196 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001197 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001198 }
1199 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001200 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001201 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001202}
1203
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001204/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001205/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001206///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001207Parser::OwningStmtResult
1208Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001209 ConsumeToken(); // consume synchronized
1210 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001211 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001212 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001213 }
1214 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001215 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001216 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001217 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001218 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001219 }
1220 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001221 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001222 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001223 }
1224 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001225 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001226 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001227 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001228 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001229 // Enter a scope to hold everything within the compound stmt. Compound
1230 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001231 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001232
Sebastian Redl61364dd2008-12-11 19:30:53 +00001233 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001234
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001235 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001236 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001237 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001238 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001239}
1240
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001241/// objc-try-catch-statement:
1242/// @try compound-statement objc-catch-list[opt]
1243/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1244///
1245/// objc-catch-list:
1246/// @catch ( parameter-declaration ) compound-statement
1247/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1248/// catch-parameter-declaration:
1249/// parameter-declaration
1250/// '...' [OBJC2]
1251///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001252Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001253 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001254
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001255 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001256 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001257 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001258 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001259 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001260 OwningStmtResult CatchStmts(Actions);
1261 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001262 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001263 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001264 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001265 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001266 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001267
Chris Lattnerdf195262007-10-09 17:51:17 +00001268 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001269 // At this point, we need to lookahead to determine if this @ is the start
1270 // of an @catch or @finally. We don't want to consume the @ token if this
1271 // is an @try or @encode or something else.
1272 Token AfterAt = GetLookAheadToken(1);
1273 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1274 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1275 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001276
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001277 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001278 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001279 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001280 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001281 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001282 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001283 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001284 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001285 DeclSpec DS;
1286 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001287 // For some odd reason, the name of the exception variable is
Steve Naroff7ba138a2009-03-03 19:52:17 +00001288 // optional. As a result, we need to use "PrototypeContext", because
1289 // we must accept either 'declarator' or 'abstract-declarator' here.
1290 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1291 ParseDeclarator(ParmDecl);
1292
1293 // Inform the actions module about the parameter declarator, so it
1294 // gets added to the current scope.
1295 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001296 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001297 ConsumeToken(); // consume '...'
Steve Naroff93a25952009-04-07 22:56:58 +00001298
1299 SourceLocation RParenLoc;
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001300
Steve Naroff93a25952009-04-07 22:56:58 +00001301 if (Tok.is(tok::r_paren))
1302 RParenLoc = ConsumeParen();
1303 else // Skip over garbage, until we get to ')'. Eat the ')'.
1304 SkipUntil(tok::r_paren, true, false);
1305
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001306 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001307 if (Tok.is(tok::l_brace))
1308 CatchBody = ParseCompoundStatementBody();
1309 else
1310 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001311 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001312 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001313 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001314 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001315 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001316 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001317 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1318 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001319 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001320 }
1321 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001322 } else {
1323 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001324 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001325 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001326
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001327 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001328 if (Tok.is(tok::l_brace))
1329 FinallyBody = ParseCompoundStatementBody();
1330 else
1331 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001332 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001333 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001334 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001335 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001336 catch_or_finally_seen = true;
1337 break;
1338 }
1339 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001340 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001341 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001342 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001343 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001344 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1345 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001346}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347
Steve Naroff3536b442007-09-06 21:24:23 +00001348/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001349///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001350Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1351 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner73e80f62009-03-05 02:03:49 +00001352
Chris Lattner49f28ca2009-03-05 08:00:35 +00001353 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1354 PP.getSourceManager(),
1355 "parsing Objective-C method");
Chris Lattner73e80f62009-03-05 02:03:49 +00001356
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001357 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001358 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001359 ConsumeToken();
1360
Steve Naroff409be832007-11-11 19:54:21 +00001361 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001362 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001363 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001364
1365 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1366 SkipUntil(tok::l_brace, true, true);
1367
1368 // If we didn't find the '{', bail out.
1369 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001370 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001371 }
Steve Naroff409be832007-11-11 19:54:21 +00001372 SourceLocation BraceLoc = Tok.getLocation();
1373
1374 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001375 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001376
1377 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001378 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001379 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001380
1381 OwningStmtResult FnBody(ParseCompoundStatementBody());
1382
Steve Naroff409be832007-11-11 19:54:21 +00001383 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001384 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001385 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1386 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001387
Steve Naroff32ce8372009-03-02 22:00:56 +00001388 // TODO: Pass argument information.
1389 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1390
Steve Naroff409be832007-11-11 19:54:21 +00001391 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001392 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001393
Steve Naroff71c0a952007-11-13 23:01:27 +00001394 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001395}
Anders Carlsson55085182007-08-21 17:43:55 +00001396
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001397Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001398 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001399 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001400 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1401 return ParseObjCThrowStmt(AtLoc);
1402 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1403 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001404 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001405 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001406 // If the expression is invalid, skip ahead to the next semicolon. Not
1407 // doing this opens us up to the possibility of infinite loops if
1408 // ParseExpression does not consume any tokens.
1409 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001410 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001411 }
1412 // Otherwise, eat the semicolon.
1413 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001414 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001415}
1416
Sebastian Redl1d922962008-12-13 15:32:12 +00001417Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001418 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001419 case tok::string_literal: // primary-expression: string-literal
1420 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001421 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001422 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001423 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001424 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001425
Chris Lattner4fef81d2008-08-05 06:19:09 +00001426 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1427 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001428 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001429 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001430 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001431 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001432 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001433 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001434 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001435 }
Anders Carlsson55085182007-08-21 17:43:55 +00001436 }
Anders Carlsson55085182007-08-21 17:43:55 +00001437}
1438
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001439/// objc-message-expr:
1440/// '[' objc-receiver objc-message-args ']'
1441///
1442/// objc-receiver:
1443/// expression
1444/// class-name
1445/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001446Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001447 assert(Tok.is(tok::l_square) && "'[' expected");
1448 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1449
1450 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001451 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001452 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001453 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1454 SourceLocation NameLoc = ConsumeToken();
1455 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1456 ExprArg(Actions));
1457 }
Chris Lattner699b6612008-01-25 18:59:06 +00001458 }
1459
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001460 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001461 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001462 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001463 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001464 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001465
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001466 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001467 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001468}
Sebastian Redl1d922962008-12-13 15:32:12 +00001469
Chris Lattner699b6612008-01-25 18:59:06 +00001470/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1471/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001472///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001473/// objc-message-args:
1474/// objc-selector
1475/// objc-keywordarg-list
1476///
1477/// objc-keywordarg-list:
1478/// objc-keywordarg
1479/// objc-keywordarg-list objc-keywordarg
1480///
1481/// objc-keywordarg:
1482/// selector-name[opt] ':' objc-keywordexpr
1483///
1484/// objc-keywordexpr:
1485/// nonempty-expr-list
1486///
1487/// nonempty-expr-list:
1488/// assignment-expression
1489/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001490///
1491Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001492Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001493 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001494 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001495 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001496 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001497 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001498 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001499
Anders Carlssonff975cf2009-02-14 18:21:46 +00001500 SourceLocation SelectorLoc = Loc;
1501
Steve Naroff68d331a2007-09-27 14:38:14 +00001502 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001503 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001504
Chris Lattnerdf195262007-10-09 17:51:17 +00001505 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001506 while (1) {
1507 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001508 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001509
Chris Lattnerdf195262007-10-09 17:51:17 +00001510 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001511 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001512 // We must manually skip to a ']', otherwise the expression skipper will
1513 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1514 // the enclosing expression.
1515 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001516 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001517 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001518
Steve Naroff68d331a2007-09-27 14:38:14 +00001519 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001520 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001521 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001522 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001523 // We must manually skip to a ']', otherwise the expression skipper will
1524 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1525 // the enclosing expression.
1526 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001527 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001528 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001529
Steve Naroff37387c92007-09-17 20:25:27 +00001530 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001531 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001532
Steve Naroff37387c92007-09-17 20:25:27 +00001533 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001534 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001535 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001536 break;
1537 // We have a selector or a colon, continue parsing.
1538 }
1539 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001540 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001541 ConsumeToken(); // Eat the ','.
1542 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001543 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001544 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001545 // We must manually skip to a ']', otherwise the expression skipper will
1546 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1547 // the enclosing expression.
1548 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001549 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001550 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001551
Steve Naroff49f109c2007-11-15 13:05:42 +00001552 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001553 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001554 }
1555 } else if (!selIdent) {
1556 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001557
Chris Lattner4fef81d2008-08-05 06:19:09 +00001558 // We must manually skip to a ']', otherwise the expression skipper will
1559 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1560 // the enclosing expression.
1561 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001562 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001563 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001564
Chris Lattnerdf195262007-10-09 17:51:17 +00001565 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001566 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001567 // We must manually skip to a ']', otherwise the expression skipper will
1568 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1569 // the enclosing expression.
1570 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001571 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001572 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001573
Chris Lattner699b6612008-01-25 18:59:06 +00001574 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001575
Steve Naroff29238a02007-10-05 18:42:47 +00001576 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001577 if (nKeys == 0)
1578 KeyIdents.push_back(selIdent);
1579 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001580
Chris Lattnerff384912007-10-07 02:00:24 +00001581 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001582 if (ReceiverName)
1583 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001584 LBracLoc, NameLoc, SelectorLoc,
1585 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001586 KeyExprs.take(), KeyExprs.size()));
1587 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001588 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001589 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001590}
1591
Sebastian Redl1d922962008-12-13 15:32:12 +00001592Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001593 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001594 if (Res.isInvalid()) return move(Res);
1595
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001596 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1597 // expressions. At this point, we know that the only valid thing that starts
1598 // with '@' is an @"".
1599 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001600 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001601 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001602 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001603
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001604 while (Tok.is(tok::at)) {
1605 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001606
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001607 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001608 if (!isTokenStringLiteral())
1609 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001610
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001611 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001612 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001613 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001614
Sebastian Redleffa8d12008-12-10 00:02:53 +00001615 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001616 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001617
1618 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1619 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001620}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001621
1622/// objc-encode-expression:
1623/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001624Parser::OwningExprResult
1625Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001626 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001627
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001628 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001629
Chris Lattner4fef81d2008-08-05 06:19:09 +00001630 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001631 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1632
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001633 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001634
Douglas Gregor809070a2009-02-18 17:45:20 +00001635 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001636
Anders Carlsson4988ae32007-08-23 15:31:37 +00001637 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001638
Douglas Gregor809070a2009-02-18 17:45:20 +00001639 if (Ty.isInvalid())
1640 return ExprError();
1641
1642 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1643 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001644}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001645
1646/// objc-protocol-expression
1647/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001648Parser::OwningExprResult
1649Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001650 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001651
Chris Lattner4fef81d2008-08-05 06:19:09 +00001652 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001653 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1654
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001655 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001656
Chris Lattner4fef81d2008-08-05 06:19:09 +00001657 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001658 return ExprError(Diag(Tok, diag::err_expected_ident));
1659
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001660 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001661 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001662
Anders Carlsson4988ae32007-08-23 15:31:37 +00001663 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001664
Sebastian Redl1d922962008-12-13 15:32:12 +00001665 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1666 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001667}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001668
1669/// objc-selector-expression
1670/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001671Parser::OwningExprResult
1672Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001673 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001674
Chris Lattner4fef81d2008-08-05 06:19:09 +00001675 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001676 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1677
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001678 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001679 SourceLocation LParenLoc = ConsumeParen();
1680 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001681 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001682 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1683 return ExprError(Diag(Tok, diag::err_expected_ident));
1684
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001685 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001686 unsigned nColons = 0;
1687 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001688 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001689 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001690 return ExprError(Diag(Tok, diag::err_expected_colon));
1691
Chris Lattnercb53b362007-12-27 19:57:00 +00001692 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001693 ConsumeToken(); // Eat the ':'.
1694 if (Tok.is(tok::r_paren))
1695 break;
1696 // Check for another keyword selector.
1697 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001698 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001699 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001700 if (!SelIdent && Tok.isNot(tok::colon))
1701 break;
1702 }
Steve Naroff887407e2007-12-05 22:21:29 +00001703 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001704 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001705 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001706 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1707 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001708 }