blob: 013e26b891e1ba5d083d41bec7080639a000ba3d [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
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000774 if (KeyIdents.size() == 0)
775 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000776 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
777 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000778 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000779 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000780 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000781 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000782}
783
Steve Naroffdac269b2007-08-20 21:31:48 +0000784/// objc-protocol-refs:
785/// '<' identifier-list '>'
786///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000787bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000788ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Chris Lattnere13b9592008-07-26 04:03:38 +0000789 bool WarnOnDeclarations, SourceLocation &EndLoc) {
790 assert(Tok.is(tok::less) && "expected <");
791
792 ConsumeToken(); // the "<"
793
794 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
795
796 while (1) {
797 if (Tok.isNot(tok::identifier)) {
798 Diag(Tok, diag::err_expected_ident);
799 SkipUntil(tok::greater);
800 return true;
801 }
802 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
803 Tok.getLocation()));
804 ConsumeToken();
805
806 if (Tok.isNot(tok::comma))
807 break;
808 ConsumeToken();
809 }
810
811 // Consume the '>'.
812 if (Tok.isNot(tok::greater)) {
813 Diag(Tok, diag::err_expected_greater);
814 return true;
815 }
816
817 EndLoc = ConsumeAnyToken();
818
819 // Convert the list of protocols identifiers into a list of protocol decls.
820 Actions.FindProtocolDeclaration(WarnOnDeclarations,
821 &ProtocolIdents[0], ProtocolIdents.size(),
822 Protocols);
823 return false;
824}
825
Steve Naroffdac269b2007-08-20 21:31:48 +0000826/// objc-class-instance-variables:
827/// '{' objc-instance-variable-decl-list[opt] '}'
828///
829/// objc-instance-variable-decl-list:
830/// objc-visibility-spec
831/// objc-instance-variable-decl ';'
832/// ';'
833/// objc-instance-variable-decl-list objc-visibility-spec
834/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
835/// objc-instance-variable-decl-list ';'
836///
837/// objc-visibility-spec:
838/// @private
839/// @protected
840/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000841/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000842///
843/// objc-instance-variable-decl:
844/// struct-declaration
845///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000846void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000847 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000848 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000849 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000850 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
851
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000852 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000853
Steve Naroffddbff782007-08-21 21:17:12 +0000854 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000855
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000856 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000857 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000858 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000859 // Each iteration of this loop reads one objc-instance-variable-decl.
860
861 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000862 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000863 Diag(Tok, diag::ext_extra_struct_semi);
864 ConsumeToken();
865 continue;
866 }
Chris Lattnere1359422008-04-10 06:46:29 +0000867
Steve Naroffddbff782007-08-21 21:17:12 +0000868 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000869 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000870 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000871 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000872 case tok::objc_private:
873 case tok::objc_public:
874 case tok::objc_protected:
875 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000876 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000877 ConsumeToken();
878 continue;
879 default:
880 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000881 continue;
882 }
883 }
Chris Lattnere1359422008-04-10 06:46:29 +0000884
885 // Parse all the comma separated declarators.
886 DeclSpec DS;
887 FieldDeclarators.clear();
888 ParseStructDeclaration(DS, FieldDeclarators);
889
890 // Convert them all to fields.
891 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
892 FieldDeclarator &FD = FieldDeclarators[i];
893 // Install the declarator into interfaceDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000894 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
895 DS.getSourceRange().getBegin(),
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000896 interfaceDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000897 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000898 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000899 }
Steve Naroff3536b442007-09-06 21:24:23 +0000900
Chris Lattnerdf195262007-10-09 17:51:17 +0000901 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000902 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000903 } else {
904 Diag(Tok, diag::err_expected_semi_decl_list);
905 // Skip to end of block or statement
906 SkipUntil(tok::r_brace, true, true);
907 }
908 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000909 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000910 // Call ActOnFields() even if we don't have any decls. This is useful
911 // for code rewriting tools that need to be aware of the empty list.
912 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000913 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000914 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000915 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000916}
Steve Naroffdac269b2007-08-20 21:31:48 +0000917
918/// objc-protocol-declaration:
919/// objc-protocol-definition
920/// objc-protocol-forward-reference
921///
922/// objc-protocol-definition:
923/// @protocol identifier
924/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000925/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000926/// @end
927///
928/// objc-protocol-forward-reference:
929/// @protocol identifier-list ';'
930///
931/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000932/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000933/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000934Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
935 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000936 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000937 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
938 ConsumeToken(); // the "protocol" identifier
939
Chris Lattnerdf195262007-10-09 17:51:17 +0000940 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000941 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000942 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000943 }
944 // Save the protocol name, then consume it.
945 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
946 SourceLocation nameLoc = ConsumeToken();
947
Chris Lattnerdf195262007-10-09 17:51:17 +0000948 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000949 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000950 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000951 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
952 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000953 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000954
Chris Lattnerdf195262007-10-09 17:51:17 +0000955 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000956 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
957 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
958
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000959 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000960 while (1) {
961 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000963 Diag(Tok, diag::err_expected_ident);
964 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000965 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000966 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000967 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
968 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000969 ConsumeToken(); // the identifier
970
Chris Lattnerdf195262007-10-09 17:51:17 +0000971 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000972 break;
973 }
974 // Consume the ';'.
975 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000976 return DeclPtrTy();
Chris Lattner7caeabd2008-07-21 22:17:28 +0000977
Steve Naroffe440eb82007-10-10 17:32:04 +0000978 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000979 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000980 ProtocolRefs.size(),
981 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000982 }
983
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000984 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000985 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000986
Chris Lattnerb28317a2009-03-28 19:18:32 +0000987 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000988 if (Tok.is(tok::less) &&
Chris Lattner58fe03b2009-04-12 08:43:13 +0000989 ParseObjCProtocolReferences(ProtocolRefs, false, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000990 return DeclPtrTy();
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000991
Chris Lattnerb28317a2009-03-28 19:18:32 +0000992 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +0000993 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000994 ProtocolRefs.data(),
995 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000996 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000997 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000998 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000999}
Steve Naroffdac269b2007-08-20 21:31:48 +00001000
1001/// objc-implementation:
1002/// objc-class-implementation-prologue
1003/// objc-category-implementation-prologue
1004///
1005/// objc-class-implementation-prologue:
1006/// @implementation identifier objc-superclass[opt]
1007/// objc-class-instance-variables[opt]
1008///
1009/// objc-category-implementation-prologue:
1010/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001011Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001012 SourceLocation atLoc) {
1013 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1014 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1015 ConsumeToken(); // the "implementation" identifier
1016
Chris Lattnerdf195262007-10-09 17:51:17 +00001017 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001018 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001019 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001020 }
1021 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001022 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001023 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1024
Chris Lattnerdf195262007-10-09 17:51:17 +00001025 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001026 // we have a category implementation.
1027 SourceLocation lparenLoc = ConsumeParen();
1028 SourceLocation categoryLoc, rparenLoc;
1029 IdentifierInfo *categoryId = 0;
1030
Chris Lattnerdf195262007-10-09 17:51:17 +00001031 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001032 categoryId = Tok.getIdentifierInfo();
1033 categoryLoc = ConsumeToken();
1034 } else {
1035 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001036 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001037 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001038 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001039 Diag(Tok, diag::err_expected_rparen);
1040 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001041 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001042 }
1043 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001044 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001045 atLoc, nameId, nameLoc, categoryId,
1046 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001047 ObjCImpDecl = ImplCatType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001048 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001049 }
1050 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001051 SourceLocation superClassLoc;
1052 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001053 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001054 // We have a super class
1055 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001056 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001057 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001058 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001059 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001060 superClassId = Tok.getIdentifierInfo();
1061 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001062 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001063 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001064 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001065 superClassId, superClassLoc);
1066
Steve Naroff60fccee2007-10-29 21:38:07 +00001067 if (Tok.is(tok::l_brace)) // we have ivars
1068 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001069 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001070
Chris Lattnerb28317a2009-03-28 19:18:32 +00001071 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001072}
Steve Naroff60fccee2007-10-29 21:38:07 +00001073
Chris Lattnerb28317a2009-03-28 19:18:32 +00001074Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001075 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1076 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001077 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001078 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001079 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001080 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001081 ObjCImpDecl = DeclPtrTy();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001082 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001083 else
1084 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001085 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001086}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001087
1088/// compatibility-alias-decl:
1089/// @compatibility_alias alias-name class-name ';'
1090///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001091Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001092 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1093 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1094 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001095 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001096 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001097 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001098 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001099 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1100 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001101 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001102 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001103 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001104 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001105 IdentifierInfo *classId = Tok.getIdentifierInfo();
1106 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1107 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001108 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001109 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001110 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001111 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1112 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001113}
1114
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001115/// property-synthesis:
1116/// @synthesize property-ivar-list ';'
1117///
1118/// property-ivar-list:
1119/// property-ivar
1120/// property-ivar-list ',' property-ivar
1121///
1122/// property-ivar:
1123/// identifier
1124/// identifier '=' identifier
1125///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001126Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001127 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1128 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001129 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001130 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001131 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001132 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001133 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001134
Chris Lattnerdf195262007-10-09 17:51:17 +00001135 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001136 IdentifierInfo *propertyIvar = 0;
1137 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1138 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001139 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001140 // property '=' ivar-name
1141 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001142 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001143 Diag(Tok, diag::err_expected_ident);
1144 break;
1145 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001146 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001147 ConsumeToken(); // consume ivar-name
1148 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001149 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1150 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001152 break;
1153 ConsumeToken(); // consume ','
1154 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001155 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001156 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001157 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001158}
1159
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001160/// property-dynamic:
1161/// @dynamic property-list
1162///
1163/// property-list:
1164/// identifier
1165/// property-list ',' identifier
1166///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001167Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001168 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1169 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1170 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001171 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001172 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001173 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001174 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001175 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001176 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1177 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1178 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1179 propertyId, 0);
1180
Chris Lattnerdf195262007-10-09 17:51:17 +00001181 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001182 break;
1183 ConsumeToken(); // consume ','
1184 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001185 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001186 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001187 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001188}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001189
1190/// objc-throw-statement:
1191/// throw expression[opt];
1192///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001193Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001194 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001195 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001196 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001197 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001198 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001199 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001200 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001201 }
1202 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001203 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001204 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001205}
1206
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001207/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001208/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001209///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001210Parser::OwningStmtResult
1211Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001212 ConsumeToken(); // consume synchronized
1213 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001214 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001215 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001216 }
1217 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001218 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001219 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001220 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001221 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001222 }
1223 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001224 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001225 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001226 }
1227 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001228 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001229 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001230 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001231 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001232 // Enter a scope to hold everything within the compound stmt. Compound
1233 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001234 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001235
Sebastian Redl61364dd2008-12-11 19:30:53 +00001236 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001237
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001238 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001239 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001240 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001241 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001242}
1243
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001244/// objc-try-catch-statement:
1245/// @try compound-statement objc-catch-list[opt]
1246/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1247///
1248/// objc-catch-list:
1249/// @catch ( parameter-declaration ) compound-statement
1250/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1251/// catch-parameter-declaration:
1252/// parameter-declaration
1253/// '...' [OBJC2]
1254///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001255Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001256 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001257
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001258 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001259 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001260 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001261 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001262 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001263 OwningStmtResult CatchStmts(Actions);
1264 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001265 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001266 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001267 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001268 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001269 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001270
Chris Lattnerdf195262007-10-09 17:51:17 +00001271 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001272 // At this point, we need to lookahead to determine if this @ is the start
1273 // of an @catch or @finally. We don't want to consume the @ token if this
1274 // is an @try or @encode or something else.
1275 Token AfterAt = GetLookAheadToken(1);
1276 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1277 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1278 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001279
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001280 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001281 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001282 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001283 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001284 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001285 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001286 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001287 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001288 DeclSpec DS;
1289 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001290 // For some odd reason, the name of the exception variable is
Steve Naroff7ba138a2009-03-03 19:52:17 +00001291 // optional. As a result, we need to use "PrototypeContext", because
1292 // we must accept either 'declarator' or 'abstract-declarator' here.
1293 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1294 ParseDeclarator(ParmDecl);
1295
1296 // Inform the actions module about the parameter declarator, so it
1297 // gets added to the current scope.
1298 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001299 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001300 ConsumeToken(); // consume '...'
Steve Naroff93a25952009-04-07 22:56:58 +00001301
1302 SourceLocation RParenLoc;
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001303
Steve Naroff93a25952009-04-07 22:56:58 +00001304 if (Tok.is(tok::r_paren))
1305 RParenLoc = ConsumeParen();
1306 else // Skip over garbage, until we get to ')'. Eat the ')'.
1307 SkipUntil(tok::r_paren, true, false);
1308
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001309 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001310 if (Tok.is(tok::l_brace))
1311 CatchBody = ParseCompoundStatementBody();
1312 else
1313 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001314 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001315 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001316 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001317 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001318 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001319 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001320 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1321 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001322 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001323 }
1324 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001325 } else {
1326 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001327 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001328 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001329
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001330 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001331 if (Tok.is(tok::l_brace))
1332 FinallyBody = ParseCompoundStatementBody();
1333 else
1334 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001335 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001336 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001337 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001338 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001339 catch_or_finally_seen = true;
1340 break;
1341 }
1342 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001343 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001344 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001345 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001346 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001347 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1348 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001349}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001350
Steve Naroff3536b442007-09-06 21:24:23 +00001351/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001352///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001353Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1354 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner73e80f62009-03-05 02:03:49 +00001355
Chris Lattner49f28ca2009-03-05 08:00:35 +00001356 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1357 PP.getSourceManager(),
1358 "parsing Objective-C method");
Chris Lattner73e80f62009-03-05 02:03:49 +00001359
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001360 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001361 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001362 ConsumeToken();
1363
Steve Naroff409be832007-11-11 19:54:21 +00001364 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001365 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001366 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001367
1368 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1369 SkipUntil(tok::l_brace, true, true);
1370
1371 // If we didn't find the '{', bail out.
1372 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001373 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001374 }
Steve Naroff409be832007-11-11 19:54:21 +00001375 SourceLocation BraceLoc = Tok.getLocation();
1376
1377 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001378 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001379
1380 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001381 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001382 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001383
1384 OwningStmtResult FnBody(ParseCompoundStatementBody());
1385
Steve Naroff409be832007-11-11 19:54:21 +00001386 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001387 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001388 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1389 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001390
Steve Naroff32ce8372009-03-02 22:00:56 +00001391 // TODO: Pass argument information.
1392 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1393
Steve Naroff409be832007-11-11 19:54:21 +00001394 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001395 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001396
Steve Naroff71c0a952007-11-13 23:01:27 +00001397 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001398}
Anders Carlsson55085182007-08-21 17:43:55 +00001399
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001400Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001401 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001402 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001403 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1404 return ParseObjCThrowStmt(AtLoc);
1405 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1406 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001407 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001408 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001409 // If the expression is invalid, skip ahead to the next semicolon. Not
1410 // doing this opens us up to the possibility of infinite loops if
1411 // ParseExpression does not consume any tokens.
1412 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001413 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001414 }
1415 // Otherwise, eat the semicolon.
1416 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001417 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001418}
1419
Sebastian Redl1d922962008-12-13 15:32:12 +00001420Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001421 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001422 case tok::string_literal: // primary-expression: string-literal
1423 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001424 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001425 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001426 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001427 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001428
Chris Lattner4fef81d2008-08-05 06:19:09 +00001429 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1430 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001431 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001432 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001433 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001434 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001435 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001436 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001437 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001438 }
Anders Carlsson55085182007-08-21 17:43:55 +00001439 }
Anders Carlsson55085182007-08-21 17:43:55 +00001440}
1441
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001442/// objc-message-expr:
1443/// '[' objc-receiver objc-message-args ']'
1444///
1445/// objc-receiver:
1446/// expression
1447/// class-name
1448/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001449Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001450 assert(Tok.is(tok::l_square) && "'[' expected");
1451 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1452
1453 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001454 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001455 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001456 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1457 SourceLocation NameLoc = ConsumeToken();
1458 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1459 ExprArg(Actions));
1460 }
Chris Lattner699b6612008-01-25 18:59:06 +00001461 }
1462
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001463 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001464 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001465 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001466 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001467 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001468
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001469 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001470 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001471}
Sebastian Redl1d922962008-12-13 15:32:12 +00001472
Chris Lattner699b6612008-01-25 18:59:06 +00001473/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1474/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001475///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001476/// objc-message-args:
1477/// objc-selector
1478/// objc-keywordarg-list
1479///
1480/// objc-keywordarg-list:
1481/// objc-keywordarg
1482/// objc-keywordarg-list objc-keywordarg
1483///
1484/// objc-keywordarg:
1485/// selector-name[opt] ':' objc-keywordexpr
1486///
1487/// objc-keywordexpr:
1488/// nonempty-expr-list
1489///
1490/// nonempty-expr-list:
1491/// assignment-expression
1492/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001493///
1494Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001495Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001496 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001497 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001498 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001499 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001500 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001501 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001502
Anders Carlssonff975cf2009-02-14 18:21:46 +00001503 SourceLocation SelectorLoc = Loc;
1504
Steve Naroff68d331a2007-09-27 14:38:14 +00001505 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001506 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001507
Chris Lattnerdf195262007-10-09 17:51:17 +00001508 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001509 while (1) {
1510 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001511 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001512
Chris Lattnerdf195262007-10-09 17:51:17 +00001513 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001514 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001515 // We must manually skip to a ']', otherwise the expression skipper will
1516 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1517 // the enclosing expression.
1518 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001519 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001520 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001521
Steve Naroff68d331a2007-09-27 14:38:14 +00001522 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001523 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001524 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001525 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001526 // We must manually skip to a ']', otherwise the expression skipper will
1527 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1528 // the enclosing expression.
1529 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001530 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001531 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001532
Steve Naroff37387c92007-09-17 20:25:27 +00001533 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001534 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001535
Steve Naroff37387c92007-09-17 20:25:27 +00001536 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001537 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001538 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001539 break;
1540 // We have a selector or a colon, continue parsing.
1541 }
1542 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001543 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001544 ConsumeToken(); // Eat the ','.
1545 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001546 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001547 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001548 // We must manually skip to a ']', otherwise the expression skipper will
1549 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1550 // the enclosing expression.
1551 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001552 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001553 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001554
Steve Naroff49f109c2007-11-15 13:05:42 +00001555 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001556 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001557 }
1558 } else if (!selIdent) {
1559 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001560
Chris Lattner4fef81d2008-08-05 06:19:09 +00001561 // We must manually skip to a ']', otherwise the expression skipper will
1562 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1563 // the enclosing expression.
1564 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001565 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001566 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001567
Chris Lattnerdf195262007-10-09 17:51:17 +00001568 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001569 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001570 // We must manually skip to a ']', otherwise the expression skipper will
1571 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1572 // the enclosing expression.
1573 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001574 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001575 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001576
Chris Lattner699b6612008-01-25 18:59:06 +00001577 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001578
Steve Naroff29238a02007-10-05 18:42:47 +00001579 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001580 if (nKeys == 0)
1581 KeyIdents.push_back(selIdent);
1582 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001583
Chris Lattnerff384912007-10-07 02:00:24 +00001584 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001585 if (ReceiverName)
1586 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001587 LBracLoc, NameLoc, SelectorLoc,
1588 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001589 KeyExprs.take(), KeyExprs.size()));
1590 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001591 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001592 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001593}
1594
Sebastian Redl1d922962008-12-13 15:32:12 +00001595Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001596 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001597 if (Res.isInvalid()) return move(Res);
1598
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001599 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1600 // expressions. At this point, we know that the only valid thing that starts
1601 // with '@' is an @"".
1602 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001603 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001604 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001605 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001606
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001607 while (Tok.is(tok::at)) {
1608 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001609
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001610 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001611 if (!isTokenStringLiteral())
1612 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001613
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001614 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001615 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001616 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001617
Sebastian Redleffa8d12008-12-10 00:02:53 +00001618 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001619 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001620
1621 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1622 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001623}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001624
1625/// objc-encode-expression:
1626/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001627Parser::OwningExprResult
1628Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001629 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001630
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001631 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001632
Chris Lattner4fef81d2008-08-05 06:19:09 +00001633 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001634 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1635
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001636 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001637
Douglas Gregor809070a2009-02-18 17:45:20 +00001638 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001639
Anders Carlsson4988ae32007-08-23 15:31:37 +00001640 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001641
Douglas Gregor809070a2009-02-18 17:45:20 +00001642 if (Ty.isInvalid())
1643 return ExprError();
1644
1645 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1646 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001647}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001648
1649/// objc-protocol-expression
1650/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001651Parser::OwningExprResult
1652Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001653 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001654
Chris Lattner4fef81d2008-08-05 06:19:09 +00001655 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001656 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1657
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001658 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001659
Chris Lattner4fef81d2008-08-05 06:19:09 +00001660 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001661 return ExprError(Diag(Tok, diag::err_expected_ident));
1662
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001663 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001664 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001665
Anders Carlsson4988ae32007-08-23 15:31:37 +00001666 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001667
Sebastian Redl1d922962008-12-13 15:32:12 +00001668 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1669 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001670}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001671
1672/// objc-selector-expression
1673/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001674Parser::OwningExprResult
1675Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001676 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001677
Chris Lattner4fef81d2008-08-05 06:19:09 +00001678 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001679 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1680
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001681 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001682 SourceLocation LParenLoc = ConsumeParen();
1683 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001684 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001685 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1686 return ExprError(Diag(Tok, diag::err_expected_ident));
1687
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001688 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001689 unsigned nColons = 0;
1690 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001691 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001692 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001693 return ExprError(Diag(Tok, diag::err_expected_colon));
1694
Chris Lattnercb53b362007-12-27 19:57:00 +00001695 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001696 ConsumeToken(); // Eat the ':'.
1697 if (Tok.is(tok::r_paren))
1698 break;
1699 // Check for another keyword selector.
1700 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001701 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001702 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001703 if (!SelIdent && Tok.isNot(tok::colon))
1704 break;
1705 }
Steve Naroff887407e2007-12-05 22:21:29 +00001706 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001707 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001708 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001709 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1710 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001711 }