blob: 8ff1944a9df5d7477317d346f2a31e903cc5c2cf [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"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22
Chris Lattner891dca62008-12-08 21:53:24 +000023/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000024/// external-declaration: [C99 6.9]
25/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000026/// [OBJC] objc-class-declaration
27/// [OBJC] objc-alias-declaration
28/// [OBJC] objc-protocol-definition
29/// [OBJC] objc-method-definition
30/// [OBJC] '@' 'end'
Chris Lattnerb28317a2009-03-28 19:18:32 +000031Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 SourceLocation AtLoc = ConsumeToken(); // the "@"
33
Steve Naroff861cf3e2007-08-23 18:16:40 +000034 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000035 case tok::objc_class:
36 return ParseObjCAtClassDeclaration(AtLoc);
37 case tok::objc_interface:
38 return ParseObjCAtInterfaceDeclaration(AtLoc);
39 case tok::objc_protocol:
40 return ParseObjCAtProtocolDeclaration(AtLoc);
41 case tok::objc_implementation:
42 return ParseObjCAtImplementationDeclaration(AtLoc);
43 case tok::objc_end:
44 return ParseObjCAtEndDeclaration(AtLoc);
45 case tok::objc_compatibility_alias:
46 return ParseObjCAtAliasDeclaration(AtLoc);
47 case tok::objc_synthesize:
48 return ParseObjCPropertySynthesize(AtLoc);
49 case tok::objc_dynamic:
50 return ParseObjCPropertyDynamic(AtLoc);
51 default:
52 Diag(AtLoc, diag::err_unexpected_at);
53 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000054 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Chris Lattnerb28317a2009-03-28 19:18:32 +000062Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000063 ConsumeToken(); // the identifier "class"
64 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
65
66 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000067 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 Diag(Tok, diag::err_expected_ident);
69 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000070 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000071 }
Reid Spencer5f016e22007-07-11 17:01:13 +000072 ClassNames.push_back(Tok.getIdentifierInfo());
73 ConsumeToken();
74
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000076 break;
77
78 ConsumeToken();
79 }
80
81 // Consume the ';'.
82 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000083 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000084
Steve Naroffe440eb82007-10-10 17:32:04 +000085 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000086 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Steve Naroffdac269b2007-08-20 21:31:48 +000089///
90/// objc-interface:
91/// objc-class-interface-attributes[opt] objc-class-interface
92/// objc-category-interface
93///
94/// objc-class-interface:
95/// '@' 'interface' identifier objc-superclass[opt]
96/// objc-protocol-refs[opt]
97/// objc-class-instance-variables[opt]
98/// objc-interface-decl-list
99/// @end
100///
101/// objc-category-interface:
102/// '@' 'interface' identifier '(' identifier[opt] ')'
103/// objc-protocol-refs[opt]
104/// objc-interface-decl-list
105/// @end
106///
107/// objc-superclass:
108/// ':' identifier
109///
110/// objc-class-interface-attributes:
111/// __attribute__((visibility("default")))
112/// __attribute__((visibility("hidden")))
113/// __attribute__((deprecated))
114/// __attribute__((unavailable))
115/// __attribute__((objc_exception)) - used by NSException on 64-bit
116///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000117Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000118 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000119 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000120 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
121 ConsumeToken(); // the "interface" identifier
122
Chris Lattnerdf195262007-10-09 17:51:17 +0000123 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000124 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000125 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000126 }
127 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000128 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 SourceLocation nameLoc = ConsumeToken();
130
Chris Lattnerdf195262007-10-09 17:51:17 +0000131 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000132 SourceLocation lparenLoc = ConsumeParen();
133 SourceLocation categoryLoc, rparenLoc;
134 IdentifierInfo *categoryId = 0;
135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000142 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000147 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000148 }
149 rparenLoc = ConsumeParen();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000150
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000152 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000153 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000156 return DeclPtrTy();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000157
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 if (attrList) // categories don't support attributes.
159 Diag(Tok, diag::err_objc_no_attributes_on_category);
160
Chris Lattnerb28317a2009-03-28 19:18:32 +0000161 DeclPtrTy CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000162 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000163 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000164 EndProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000165
166 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000167 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 }
169 // Parse a class interface.
170 IdentifierInfo *superClassId = 0;
171 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000172
Chris Lattnerdf195262007-10-09 17:51:17 +0000173 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000174 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000177 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000183 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Chris Lattner06036d32008-07-26 04:13:19 +0000184 SourceLocation EndProtoLoc;
185 if (Tok.is(tok::less) &&
186 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 return DeclPtrTy();
Chris Lattner06036d32008-07-26 04:13:19 +0000188
Chris Lattnerb28317a2009-03-28 19:18:32 +0000189 DeclPtrTy ClsType =
Chris Lattner06036d32008-07-26 04:13:19 +0000190 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
191 superClassId, superClassLoc,
192 &ProtocolRefs[0], ProtocolRefs.size(),
193 EndProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000194
Chris Lattnerdf195262007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000196 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000197
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000199 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000200}
201
202/// objc-interface-decl-list:
203/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000204/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000205/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000206/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000207/// objc-interface-decl-list declaration
208/// objc-interface-decl-list ';'
209///
Steve Naroff294494e2007-08-22 16:35:03 +0000210/// objc-method-requirement: [OBJC2]
211/// @required
212/// @optional
213///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000214void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000215 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216 llvm::SmallVector<DeclPtrTy, 32> allMethods;
217 llvm::SmallVector<DeclPtrTy, 16> allProperties;
218 llvm::SmallVector<DeclPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000219 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000220
Chris Lattnerbc662af2008-10-20 06:10:06 +0000221 SourceLocation AtEndLoc;
222
Steve Naroff294494e2007-08-22 16:35:03 +0000223 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000224 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000225 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000226 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000227 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000228 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000229 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
230 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000231 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
232 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000233 continue;
234 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000235
Chris Lattnere82a10f2008-10-20 05:46:22 +0000236 // Ignore excess semicolons.
237 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000238 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000239 continue;
240 }
241
Chris Lattnerbc662af2008-10-20 06:10:06 +0000242 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000243 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000244 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000245
246 // If we don't have an @ directive, parse it as a function definition.
247 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000248 // The code below does not consume '}'s because it is afraid of eating the
249 // end of a namespace. Because of the way this code is structured, an
250 // erroneous r_brace would cause an infinite loop if not handled here.
251 if (Tok.is(tok::r_brace))
252 break;
253
Steve Naroff4985ace2007-08-22 18:35:33 +0000254 // FIXME: as the name implies, this rule allows function definitions.
255 // We could pass a flag or check for functions during semantic analysis.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000256 DeclPtrTy VFDecl = ParseDeclarationOrFunctionDefinition();
Fariborz Jahanian38e24c72009-03-18 22:33:24 +0000257 allTUVariables.push_back(VFDecl);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000258 continue;
259 }
260
261 // Otherwise, we have an @ directive, eat the @.
262 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000263 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000264
Chris Lattnera2449b22008-10-20 05:57:40 +0000265 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000266 AtEndLoc = AtLoc;
267 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000268 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000269
Chris Lattnerbc662af2008-10-20 06:10:06 +0000270 // Eat the identifier.
271 ConsumeToken();
272
Chris Lattnera2449b22008-10-20 05:57:40 +0000273 switch (DirectiveKind) {
274 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000275 // FIXME: If someone forgets an @end on a protocol, this loop will
276 // continue to eat up tons of stuff and spew lots of nonsense errors. It
277 // would probably be better to bail out if we saw an @class or @interface
278 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000279 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000280 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000281 SkipUntil(tok::r_brace, tok::at);
282 break;
283
284 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000285 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000286 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000287 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000288 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000289 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000290 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000291 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000292 break;
293
294 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000295 if (!getLang().ObjC2)
296 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
297
Chris Lattnere82a10f2008-10-20 05:46:22 +0000298 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000299 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000300 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000301 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000302
Chris Lattnere82a10f2008-10-20 05:46:22 +0000303 // Parse all the comma separated declarators.
304 DeclSpec DS;
305 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
306 ParseStructDeclaration(DS, FieldDeclarators);
307
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000308 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
309 tok::at);
310
Chris Lattnere82a10f2008-10-20 05:46:22 +0000311 // Convert them all to property declarations.
312 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
313 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000314 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000315 Diag(AtLoc, diag::err_objc_property_requires_field_name)
316 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000317 continue;
318 }
Fariborz Jahanian573acde2009-01-17 23:21:10 +0000319 if (FD.BitfieldSize) {
320 Diag(AtLoc, diag::err_objc_property_bitfield)
321 << FD.D.getSourceRange();
322 continue;
323 }
Chris Lattnerda3253d2008-10-20 06:33:53 +0000324
Chris Lattnere82a10f2008-10-20 05:46:22 +0000325 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000326 IdentifierInfo *SelName =
327 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
328
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000330 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000331 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahanian2e050f12009-03-12 22:34:11 +0000332 Selector SetterSel;
333 if (SetterName)
334 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
335 else
336 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
337 PP.getSelectorTable(),
338 FD.D.getIdentifier());
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000339 bool isOverridingProperty = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000340 DeclPtrTy Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
341 GetterSel, SetterSel,
342 interfaceDecl,
343 &isOverridingProperty,
344 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000345 if (!isOverridingProperty)
346 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000347 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000348 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000349 }
Steve Naroff294494e2007-08-22 16:35:03 +0000350 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000351
352 // We break out of the big loop in two cases: when we see @end or when we see
353 // EOF. In the former case, eat the @end. In the later case, emit an error.
354 if (Tok.isObjCAtKeyword(tok::objc_end))
355 ConsumeToken(); // the "end" identifier
356 else
357 Diag(Tok, diag::err_objc_missing_end);
358
Chris Lattnera2449b22008-10-20 05:57:40 +0000359 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000360 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000361 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
362 allMethods.empty() ? 0 : &allMethods[0],
363 allMethods.size(),
364 allProperties.empty() ? 0 : &allProperties[0],
Fariborz Jahanian38e24c72009-03-18 22:33:24 +0000365 allProperties.size(),
366 allTUVariables.empty() ? 0 :
367 &allTUVariables[0],
368 allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000369}
370
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000371/// Parse property attribute declarations.
372///
373/// property-attr-decl: '(' property-attrlist ')'
374/// property-attrlist:
375/// property-attribute
376/// property-attrlist ',' property-attribute
377/// property-attribute:
378/// getter '=' identifier
379/// setter '=' identifier ':'
380/// readonly
381/// readwrite
382/// assign
383/// retain
384/// copy
385/// nonatomic
386///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000387void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000388 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000389 SourceLocation LHSLoc = ConsumeParen(); // consume '('
390
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000391 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000392 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000393
394 // If this is not an identifier at all, bail out early.
395 if (II == 0) {
396 MatchRHSPunctuation(tok::r_paren, LHSLoc);
397 return;
398 }
399
Chris Lattner156b0612008-10-20 07:37:22 +0000400 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
401
Chris Lattner92e62b02008-11-20 04:42:34 +0000402 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000403 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000404 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000405 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000406 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000407 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000408 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000409 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000410 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000411 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000412 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000413 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000414 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000415 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000416 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
417 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000418 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000419
Chris Lattner8ca329c2008-10-20 07:24:39 +0000420 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000421 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000422 SkipUntil(tok::r_paren);
423 return;
424 }
425
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000426 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000427 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
428 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000429 ConsumeToken(); // consume method name
430
431 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
432 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000433 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000434 } else {
435 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
436 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000437 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000438 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000439 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000440 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000441 SkipUntil(tok::r_paren);
442 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000443 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000444
Chris Lattner156b0612008-10-20 07:37:22 +0000445 if (Tok.isNot(tok::comma))
446 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000447
Chris Lattner156b0612008-10-20 07:37:22 +0000448 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000449 }
Chris Lattner156b0612008-10-20 07:37:22 +0000450
451 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000452}
453
Steve Naroff3536b442007-09-06 21:24:23 +0000454/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000455/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000456/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000457///
458/// objc-instance-method: '-'
459/// objc-class-method: '+'
460///
Steve Naroff4985ace2007-08-22 18:35:33 +0000461/// objc-method-attributes: [OBJC2]
462/// __attribute__((deprecated))
463///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000464Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
465 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000466 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000467
468 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000469 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000470
Chris Lattnerb28317a2009-03-28 19:18:32 +0000471 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000472 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000473 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000474 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000475}
476
477/// objc-selector:
478/// identifier
479/// one of
480/// enum struct union if else while do for switch case default
481/// break continue return goto asm sizeof typeof __alignof
482/// unsigned long const short volatile signed restrict _Complex
483/// in out inout bycopy byref oneway int char float double void _Bool
484///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000485IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000486 switch (Tok.getKind()) {
487 default:
488 return 0;
489 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000490 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000491 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000492 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000493 case tok::kw_break:
494 case tok::kw_case:
495 case tok::kw_catch:
496 case tok::kw_char:
497 case tok::kw_class:
498 case tok::kw_const:
499 case tok::kw_const_cast:
500 case tok::kw_continue:
501 case tok::kw_default:
502 case tok::kw_delete:
503 case tok::kw_do:
504 case tok::kw_double:
505 case tok::kw_dynamic_cast:
506 case tok::kw_else:
507 case tok::kw_enum:
508 case tok::kw_explicit:
509 case tok::kw_export:
510 case tok::kw_extern:
511 case tok::kw_false:
512 case tok::kw_float:
513 case tok::kw_for:
514 case tok::kw_friend:
515 case tok::kw_goto:
516 case tok::kw_if:
517 case tok::kw_inline:
518 case tok::kw_int:
519 case tok::kw_long:
520 case tok::kw_mutable:
521 case tok::kw_namespace:
522 case tok::kw_new:
523 case tok::kw_operator:
524 case tok::kw_private:
525 case tok::kw_protected:
526 case tok::kw_public:
527 case tok::kw_register:
528 case tok::kw_reinterpret_cast:
529 case tok::kw_restrict:
530 case tok::kw_return:
531 case tok::kw_short:
532 case tok::kw_signed:
533 case tok::kw_sizeof:
534 case tok::kw_static:
535 case tok::kw_static_cast:
536 case tok::kw_struct:
537 case tok::kw_switch:
538 case tok::kw_template:
539 case tok::kw_this:
540 case tok::kw_throw:
541 case tok::kw_true:
542 case tok::kw_try:
543 case tok::kw_typedef:
544 case tok::kw_typeid:
545 case tok::kw_typename:
546 case tok::kw_typeof:
547 case tok::kw_union:
548 case tok::kw_unsigned:
549 case tok::kw_using:
550 case tok::kw_virtual:
551 case tok::kw_void:
552 case tok::kw_volatile:
553 case tok::kw_wchar_t:
554 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000555 case tok::kw__Bool:
556 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000557 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000558 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000559 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000560 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000561 }
Steve Naroff294494e2007-08-22 16:35:03 +0000562}
563
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000564/// objc-for-collection-in: 'in'
565///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000566bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000567 // FIXME: May have to do additional look-ahead to only allow for
568 // valid tokens following an 'in'; such as an identifier, unary operators,
569 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000570 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000571 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000572}
573
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000574/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000575/// qualifier list and builds their bitmask representation in the input
576/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000577///
578/// objc-type-qualifiers:
579/// objc-type-qualifier
580/// objc-type-qualifiers objc-type-qualifier
581///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000583 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000584 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000585 return;
586
587 const IdentifierInfo *II = Tok.getIdentifierInfo();
588 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000590 continue;
591
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000593 switch (i) {
594 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
596 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
597 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
598 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
599 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
600 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000601 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000602 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000603 ConsumeToken();
604 II = 0;
605 break;
606 }
607
608 // If this wasn't a recognized qualifier, bail out.
609 if (II) return;
610 }
611}
612
613/// objc-type-name:
614/// '(' objc-type-qualifiers[opt] type-name ')'
615/// '(' objc-type-qualifiers[opt] ')'
616///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000618 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000619
Chris Lattner4a76b292008-10-22 03:52:06 +0000620 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000621 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000622
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000623 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000625
Chris Lattner4a76b292008-10-22 03:52:06 +0000626 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000627 if (isTypeSpecifierQualifier()) {
628 TypeResult TypeSpec = ParseTypeName();
629 if (!TypeSpec.isInvalid())
630 Ty = TypeSpec.get();
631 }
Chris Lattnere8904e92008-08-23 01:48:03 +0000632
Steve Naroffd7333c22008-10-21 14:15:04 +0000633 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000634 ConsumeParen();
635 else if (Tok.getLocation() == TypeStartLoc) {
636 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000637 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000638 SkipUntil(tok::r_paren);
639 } else {
640 // Otherwise, we found *something*, but didn't get a ')' in the right
641 // place. Emit an error then return what we have as the type.
642 MatchRHSPunctuation(tok::r_paren, LParenLoc);
643 }
Steve Narofff28b2642007-09-05 23:30:30 +0000644 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000645}
646
647/// objc-method-decl:
648/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000649/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000650/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000651/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000652///
653/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000654/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000655/// objc-keyword-selector objc-keyword-decl
656///
657/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000658/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
659/// objc-selector ':' objc-keyword-attributes[opt] identifier
660/// ':' objc-type-name objc-keyword-attributes[opt] identifier
661/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000662///
Steve Naroff4985ace2007-08-22 18:35:33 +0000663/// objc-parmlist:
664/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000665///
Steve Naroff4985ace2007-08-22 18:35:33 +0000666/// objc-parms:
667/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000668///
Steve Naroff4985ace2007-08-22 18:35:33 +0000669/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000670/// , ...
671///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000672/// objc-keyword-attributes: [OBJC2]
673/// __attribute__((unused))
674///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000675Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
676 tok::TokenKind mType,
677 DeclPtrTy IDecl,
678 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnere8904e92008-08-23 01:48:03 +0000679 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000680 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000681 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000682 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000683 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000684
Steve Naroffbef11852007-10-26 20:53:56 +0000685 SourceLocation selLoc;
686 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000687
Steve Naroff84c43102009-02-11 20:43:13 +0000688 // An unnamed colon is valid.
689 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000690 Diag(Tok, diag::err_expected_selector_for_method)
691 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000692 // Skip until we get a ; or {}.
693 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000694 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000695 }
696
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000697 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000698 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000699 // If attributes exist after the method, parse them.
700 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000701 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000702 MethodAttrs = ParseAttributes();
703
704 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000705 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000706 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000707 0, 0, 0, CargNames,
708 MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000709 }
Steve Narofff28b2642007-09-05 23:30:30 +0000710
Steve Naroff68d331a2007-09-27 14:38:14 +0000711 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
712 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000713 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000714 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000715
716 Action::TypeTy *TypeInfo;
717 while (1) {
718 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000719
Chris Lattnerff384912007-10-07 02:00:24 +0000720 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000721 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000722 Diag(Tok, diag::err_expected_colon);
723 break;
724 }
725 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000726 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000727 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000728 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000729 else
730 TypeInfo = 0;
731 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000732 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000733
Chris Lattnerff384912007-10-07 02:00:24 +0000734 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000735 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000736 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000737
Chris Lattnerdf195262007-10-09 17:51:17 +0000738 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000739 Diag(Tok, diag::err_expected_ident); // missing argument name.
740 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000741 }
Chris Lattnerff384912007-10-07 02:00:24 +0000742 ArgNames.push_back(Tok.getIdentifierInfo());
743 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000744
Chris Lattnerff384912007-10-07 02:00:24 +0000745 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000746 SourceLocation Loc;
747 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000748 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000749 break;
750 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000751 }
Chris Lattnerff384912007-10-07 02:00:24 +0000752
Steve Naroff335eafa2007-11-15 12:35:21 +0000753 bool isVariadic = false;
754
Chris Lattnerff384912007-10-07 02:00:24 +0000755 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000756 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000757 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000758 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000759 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000760 ConsumeToken();
761 break;
762 }
Chris Lattnerff384912007-10-07 02:00:24 +0000763 DeclSpec DS;
764 ParseDeclarationSpecifiers(DS);
765 // Parse the declarator.
766 Declarator ParmDecl(DS, Declarator::PrototypeContext);
767 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000768 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000769 }
770
771 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000772 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000773 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000774 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000775 MethodAttrs = ParseAttributes();
776
777 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
778 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000779 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000780 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000781 &ArgTypeQuals[0], &KeyTypes[0],
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000782 &ArgNames[0], CargNames,
783 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000784 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000785}
786
Steve Naroffdac269b2007-08-20 21:31:48 +0000787/// objc-protocol-refs:
788/// '<' identifier-list '>'
789///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000790bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000791ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Chris Lattnere13b9592008-07-26 04:03:38 +0000792 bool WarnOnDeclarations, SourceLocation &EndLoc) {
793 assert(Tok.is(tok::less) && "expected <");
794
795 ConsumeToken(); // the "<"
796
797 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
798
799 while (1) {
800 if (Tok.isNot(tok::identifier)) {
801 Diag(Tok, diag::err_expected_ident);
802 SkipUntil(tok::greater);
803 return true;
804 }
805 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
806 Tok.getLocation()));
807 ConsumeToken();
808
809 if (Tok.isNot(tok::comma))
810 break;
811 ConsumeToken();
812 }
813
814 // Consume the '>'.
815 if (Tok.isNot(tok::greater)) {
816 Diag(Tok, diag::err_expected_greater);
817 return true;
818 }
819
820 EndLoc = ConsumeAnyToken();
821
822 // Convert the list of protocols identifiers into a list of protocol decls.
823 Actions.FindProtocolDeclaration(WarnOnDeclarations,
824 &ProtocolIdents[0], ProtocolIdents.size(),
825 Protocols);
826 return false;
827}
828
Steve Naroffdac269b2007-08-20 21:31:48 +0000829/// objc-class-instance-variables:
830/// '{' objc-instance-variable-decl-list[opt] '}'
831///
832/// objc-instance-variable-decl-list:
833/// objc-visibility-spec
834/// objc-instance-variable-decl ';'
835/// ';'
836/// objc-instance-variable-decl-list objc-visibility-spec
837/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
838/// objc-instance-variable-decl-list ';'
839///
840/// objc-visibility-spec:
841/// @private
842/// @protected
843/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000844/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000845///
846/// objc-instance-variable-decl:
847/// struct-declaration
848///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000849void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000850 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000851 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000852 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000853 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
854
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000855 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000856
Steve Naroffddbff782007-08-21 21:17:12 +0000857 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000858
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000859 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000860 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000861 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000862 // Each iteration of this loop reads one objc-instance-variable-decl.
863
864 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000865 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000866 Diag(Tok, diag::ext_extra_struct_semi);
867 ConsumeToken();
868 continue;
869 }
Chris Lattnere1359422008-04-10 06:46:29 +0000870
Steve Naroffddbff782007-08-21 21:17:12 +0000871 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000872 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000873 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000874 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000875 case tok::objc_private:
876 case tok::objc_public:
877 case tok::objc_protected:
878 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000879 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000880 ConsumeToken();
881 continue;
882 default:
883 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000884 continue;
885 }
886 }
Chris Lattnere1359422008-04-10 06:46:29 +0000887
888 // Parse all the comma separated declarators.
889 DeclSpec DS;
890 FieldDeclarators.clear();
891 ParseStructDeclaration(DS, FieldDeclarators);
892
893 // Convert them all to fields.
894 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
895 FieldDeclarator &FD = FieldDeclarators[i];
896 // Install the declarator into interfaceDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000897 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
898 DS.getSourceRange().getBegin(),
899 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000900 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000901 }
Steve Naroff3536b442007-09-06 21:24:23 +0000902
Chris Lattnerdf195262007-10-09 17:51:17 +0000903 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000904 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000905 } else {
906 Diag(Tok, diag::err_expected_semi_decl_list);
907 // Skip to end of block or statement
908 SkipUntil(tok::r_brace, true, true);
909 }
910 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000911 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000912 // Call ActOnFields() even if we don't have any decls. This is useful
913 // for code rewriting tools that need to be aware of the empty list.
914 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
915 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000916 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000917 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000918}
Steve Naroffdac269b2007-08-20 21:31:48 +0000919
920/// objc-protocol-declaration:
921/// objc-protocol-definition
922/// objc-protocol-forward-reference
923///
924/// objc-protocol-definition:
925/// @protocol identifier
926/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000927/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000928/// @end
929///
930/// objc-protocol-forward-reference:
931/// @protocol identifier-list ';'
932///
933/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000934/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000935/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000936Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
937 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000938 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000939 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
940 ConsumeToken(); // the "protocol" identifier
941
Chris Lattnerdf195262007-10-09 17:51:17 +0000942 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000943 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000944 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000945 }
946 // Save the protocol name, then consume it.
947 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
948 SourceLocation nameLoc = ConsumeToken();
949
Chris Lattnerdf195262007-10-09 17:51:17 +0000950 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000951 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000952 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000953 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
954 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000955 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000956
Chris Lattnerdf195262007-10-09 17:51:17 +0000957 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000958 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
959 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
960
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000961 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000962 while (1) {
963 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000964 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000965 Diag(Tok, diag::err_expected_ident);
966 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000967 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000968 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000969 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
970 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000971 ConsumeToken(); // the identifier
972
Chris Lattnerdf195262007-10-09 17:51:17 +0000973 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000974 break;
975 }
976 // Consume the ';'.
977 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000978 return DeclPtrTy();
Chris Lattner7caeabd2008-07-21 22:17:28 +0000979
Steve Naroffe440eb82007-10-10 17:32:04 +0000980 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000981 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000982 ProtocolRefs.size(),
983 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000984 }
985
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000986 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000987 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000988
Chris Lattnerb28317a2009-03-28 19:18:32 +0000989 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000990 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000991 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000992 return DeclPtrTy();
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000993
Chris Lattnerb28317a2009-03-28 19:18:32 +0000994 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +0000995 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
996 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000997 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000998 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000999 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001000}
Steve Naroffdac269b2007-08-20 21:31:48 +00001001
1002/// objc-implementation:
1003/// objc-class-implementation-prologue
1004/// objc-category-implementation-prologue
1005///
1006/// objc-class-implementation-prologue:
1007/// @implementation identifier objc-superclass[opt]
1008/// objc-class-instance-variables[opt]
1009///
1010/// objc-category-implementation-prologue:
1011/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001012Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001013 SourceLocation atLoc) {
1014 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1015 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1016 ConsumeToken(); // the "implementation" identifier
1017
Chris Lattnerdf195262007-10-09 17:51:17 +00001018 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001019 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001020 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001021 }
1022 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001023 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001024 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1025
Chris Lattnerdf195262007-10-09 17:51:17 +00001026 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001027 // we have a category implementation.
1028 SourceLocation lparenLoc = ConsumeParen();
1029 SourceLocation categoryLoc, rparenLoc;
1030 IdentifierInfo *categoryId = 0;
1031
Chris Lattnerdf195262007-10-09 17:51:17 +00001032 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001033 categoryId = Tok.getIdentifierInfo();
1034 categoryLoc = ConsumeToken();
1035 } else {
1036 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001037 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001038 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001039 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001040 Diag(Tok, diag::err_expected_rparen);
1041 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001042 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001043 }
1044 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001045 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001046 atLoc, nameId, nameLoc, categoryId,
1047 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001048 ObjCImpDecl = ImplCatType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001049 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001050 }
1051 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001052 SourceLocation superClassLoc;
1053 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001054 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001055 // We have a super class
1056 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001058 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001059 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001060 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001061 superClassId = Tok.getIdentifierInfo();
1062 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001063 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001064 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001065 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001066 superClassId, superClassLoc);
1067
Steve Naroff60fccee2007-10-29 21:38:07 +00001068 if (Tok.is(tok::l_brace)) // we have ivars
1069 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001070 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001071
Chris Lattnerb28317a2009-03-28 19:18:32 +00001072 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001073}
Steve Naroff60fccee2007-10-29 21:38:07 +00001074
Chris Lattnerb28317a2009-03-28 19:18:32 +00001075Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001076 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1077 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001078 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001079 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001080 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001081 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001082 ObjCImpDecl = DeclPtrTy();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001083 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001084 else
1085 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001086 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001087}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001088
1089/// compatibility-alias-decl:
1090/// @compatibility_alias alias-name class-name ';'
1091///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001092Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001093 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1094 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1095 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001097 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001098 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001099 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001100 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1101 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001102 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001103 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001104 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001105 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001106 IdentifierInfo *classId = Tok.getIdentifierInfo();
1107 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1108 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001109 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001110 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001111 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001112 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1113 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001114}
1115
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001116/// property-synthesis:
1117/// @synthesize property-ivar-list ';'
1118///
1119/// property-ivar-list:
1120/// property-ivar
1121/// property-ivar-list ',' property-ivar
1122///
1123/// property-ivar:
1124/// identifier
1125/// identifier '=' identifier
1126///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001127Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001128 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1129 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001130 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001131 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001132 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001133 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001134 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001135
Chris Lattnerdf195262007-10-09 17:51:17 +00001136 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001137 IdentifierInfo *propertyIvar = 0;
1138 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1139 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001140 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001141 // property '=' ivar-name
1142 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001143 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001144 Diag(Tok, diag::err_expected_ident);
1145 break;
1146 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001147 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001148 ConsumeToken(); // consume ivar-name
1149 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001150 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1151 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001152 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001153 break;
1154 ConsumeToken(); // consume ','
1155 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001156 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001157 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001158 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001159}
1160
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001161/// property-dynamic:
1162/// @dynamic property-list
1163///
1164/// property-list:
1165/// identifier
1166/// property-list ',' identifier
1167///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001168Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001169 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1170 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1171 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001172 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001173 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001174 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001175 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001176 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001177 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1178 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1179 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1180 propertyId, 0);
1181
Chris Lattnerdf195262007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001183 break;
1184 ConsumeToken(); // consume ','
1185 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001186 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001187 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001188 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001189}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001190
1191/// objc-throw-statement:
1192/// throw expression[opt];
1193///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001194Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001195 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001196 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001197 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001198 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001199 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001200 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001201 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001202 }
1203 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001204 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001205 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001206}
1207
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001208/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001209/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001210///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001211Parser::OwningStmtResult
1212Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001213 ConsumeToken(); // consume synchronized
1214 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001215 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001216 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001217 }
1218 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001219 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001220 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001221 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001222 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001223 }
1224 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001225 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001226 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001227 }
1228 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001229 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001230 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001231 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001232 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001233 // Enter a scope to hold everything within the compound stmt. Compound
1234 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001235 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001236
Sebastian Redl61364dd2008-12-11 19:30:53 +00001237 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001238
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001239 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001240 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001241 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001242 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001243}
1244
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001245/// objc-try-catch-statement:
1246/// @try compound-statement objc-catch-list[opt]
1247/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1248///
1249/// objc-catch-list:
1250/// @catch ( parameter-declaration ) compound-statement
1251/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1252/// catch-parameter-declaration:
1253/// parameter-declaration
1254/// '...' [OBJC2]
1255///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001256Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001257 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001258
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001259 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001260 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001261 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001262 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001263 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001264 OwningStmtResult CatchStmts(Actions);
1265 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001266 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001267 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001268 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001269 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001270 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001271
Chris Lattnerdf195262007-10-09 17:51:17 +00001272 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001273 // At this point, we need to lookahead to determine if this @ is the start
1274 // of an @catch or @finally. We don't want to consume the @ token if this
1275 // is an @try or @encode or something else.
1276 Token AfterAt = GetLookAheadToken(1);
1277 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1278 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1279 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001280
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001281 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001282 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001283 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001284 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001285 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001286 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001287 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001288 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001289 DeclSpec DS;
1290 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001291 // For some odd reason, the name of the exception variable is
Steve Naroff7ba138a2009-03-03 19:52:17 +00001292 // optional. As a result, we need to use "PrototypeContext", because
1293 // we must accept either 'declarator' or 'abstract-declarator' here.
1294 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1295 ParseDeclarator(ParmDecl);
1296
1297 // Inform the actions module about the parameter declarator, so it
1298 // gets added to the current scope.
1299 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001300 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001301 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001302 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001303
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001304 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001305 if (Tok.is(tok::l_brace))
1306 CatchBody = ParseCompoundStatementBody();
1307 else
1308 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001309 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001310 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001311 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001312 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001313 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001314 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001315 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1316 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001317 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001318 }
1319 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001320 } else {
1321 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001322 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001323 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001324
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001325 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001326 if (Tok.is(tok::l_brace))
1327 FinallyBody = ParseCompoundStatementBody();
1328 else
1329 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001330 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001331 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001332 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001333 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001334 catch_or_finally_seen = true;
1335 break;
1336 }
1337 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001338 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001339 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001340 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001341 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001342 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1343 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001344}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001345
Steve Naroff3536b442007-09-06 21:24:23 +00001346/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001348Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1349 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner73e80f62009-03-05 02:03:49 +00001350
Chris Lattner49f28ca2009-03-05 08:00:35 +00001351 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1352 PP.getSourceManager(),
1353 "parsing Objective-C method");
Chris Lattner73e80f62009-03-05 02:03:49 +00001354
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001355 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001356 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001357 ConsumeToken();
1358
Steve Naroff409be832007-11-11 19:54:21 +00001359 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001360 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001361 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001362
1363 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1364 SkipUntil(tok::l_brace, true, true);
1365
1366 // If we didn't find the '{', bail out.
1367 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001368 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001369 }
Steve Naroff409be832007-11-11 19:54:21 +00001370 SourceLocation BraceLoc = Tok.getLocation();
1371
1372 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001373 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001374
1375 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001376 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001377 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001378
1379 OwningStmtResult FnBody(ParseCompoundStatementBody());
1380
Steve Naroff409be832007-11-11 19:54:21 +00001381 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001382 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001383 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1384 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001385
Steve Naroff32ce8372009-03-02 22:00:56 +00001386 // TODO: Pass argument information.
1387 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1388
Steve Naroff409be832007-11-11 19:54:21 +00001389 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001390 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001391
Steve Naroff71c0a952007-11-13 23:01:27 +00001392 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001393}
Anders Carlsson55085182007-08-21 17:43:55 +00001394
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001395Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001396 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001397 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001398 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1399 return ParseObjCThrowStmt(AtLoc);
1400 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1401 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001402 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001403 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001404 // If the expression is invalid, skip ahead to the next semicolon. Not
1405 // doing this opens us up to the possibility of infinite loops if
1406 // ParseExpression does not consume any tokens.
1407 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001408 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001409 }
1410 // Otherwise, eat the semicolon.
1411 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001412 return Actions.ActOnExprStmt(move(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001413}
1414
Sebastian Redl1d922962008-12-13 15:32:12 +00001415Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001416 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001417 case tok::string_literal: // primary-expression: string-literal
1418 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001419 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001420 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001421 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001422 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001423
Chris Lattner4fef81d2008-08-05 06:19:09 +00001424 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1425 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001426 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001427 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001428 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001429 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001430 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001431 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001432 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001433 }
Anders Carlsson55085182007-08-21 17:43:55 +00001434 }
Anders Carlsson55085182007-08-21 17:43:55 +00001435}
1436
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001437/// objc-message-expr:
1438/// '[' objc-receiver objc-message-args ']'
1439///
1440/// objc-receiver:
1441/// expression
1442/// class-name
1443/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001444Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001445 assert(Tok.is(tok::l_square) && "'[' expected");
1446 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1447
1448 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001449 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001450 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +00001451 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001452 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1453 ExprArg(Actions));
Chris Lattner699b6612008-01-25 18:59:06 +00001454 }
1455
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001456 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001457 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001458 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001459 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001460 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001461
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001462 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001463 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001464}
Sebastian Redl1d922962008-12-13 15:32:12 +00001465
Chris Lattner699b6612008-01-25 18:59:06 +00001466/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1467/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001468///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001469/// objc-message-args:
1470/// objc-selector
1471/// objc-keywordarg-list
1472///
1473/// objc-keywordarg-list:
1474/// objc-keywordarg
1475/// objc-keywordarg-list objc-keywordarg
1476///
1477/// objc-keywordarg:
1478/// selector-name[opt] ':' objc-keywordexpr
1479///
1480/// objc-keywordexpr:
1481/// nonempty-expr-list
1482///
1483/// nonempty-expr-list:
1484/// assignment-expression
1485/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001486///
1487Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001488Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001489 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001490 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001491 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001492 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001493 SourceLocation Loc;
1494 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001495
Anders Carlssonff975cf2009-02-14 18:21:46 +00001496 SourceLocation SelectorLoc = Loc;
1497
Steve Naroff68d331a2007-09-27 14:38:14 +00001498 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001499 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001500
Chris Lattnerdf195262007-10-09 17:51:17 +00001501 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001502 while (1) {
1503 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001504 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001505
Chris Lattnerdf195262007-10-09 17:51:17 +00001506 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001507 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001508 // We must manually skip to a ']', otherwise the expression skipper will
1509 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1510 // the enclosing expression.
1511 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001512 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001513 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001514
Steve Naroff68d331a2007-09-27 14:38:14 +00001515 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001516 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001517 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001518 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001519 // We must manually skip to a ']', otherwise the expression skipper will
1520 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1521 // the enclosing expression.
1522 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001523 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001524 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001525
Steve Naroff37387c92007-09-17 20:25:27 +00001526 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001527 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001528
Steve Naroff37387c92007-09-17 20:25:27 +00001529 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001530 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001531 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001532 break;
1533 // We have a selector or a colon, continue parsing.
1534 }
1535 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001536 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001537 ConsumeToken(); // Eat the ','.
1538 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001539 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001540 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001541 // We must manually skip to a ']', otherwise the expression skipper will
1542 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1543 // the enclosing expression.
1544 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001545 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001546 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001547
Steve Naroff49f109c2007-11-15 13:05:42 +00001548 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001549 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001550 }
1551 } else if (!selIdent) {
1552 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001553
Chris Lattner4fef81d2008-08-05 06:19:09 +00001554 // We must manually skip to a ']', otherwise the expression skipper will
1555 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1556 // the enclosing expression.
1557 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001558 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001559 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001560
Chris Lattnerdf195262007-10-09 17:51:17 +00001561 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001562 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001563 // We must manually skip to a ']', otherwise the expression skipper will
1564 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1565 // the enclosing expression.
1566 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001567 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001568 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001569
Chris Lattner699b6612008-01-25 18:59:06 +00001570 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001571
Steve Naroff29238a02007-10-05 18:42:47 +00001572 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001573 if (nKeys == 0)
1574 KeyIdents.push_back(selIdent);
1575 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001576
Chris Lattnerff384912007-10-07 02:00:24 +00001577 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001578 if (ReceiverName)
1579 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001580 LBracLoc, NameLoc, SelectorLoc,
1581 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001582 KeyExprs.take(), KeyExprs.size()));
1583 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001584 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001585 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001586}
1587
Sebastian Redl1d922962008-12-13 15:32:12 +00001588Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001589 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001590 if (Res.isInvalid()) return move(Res);
1591
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001592 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1593 // expressions. At this point, we know that the only valid thing that starts
1594 // with '@' is an @"".
1595 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001596 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001597 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001598 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001599
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001600 while (Tok.is(tok::at)) {
1601 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001602
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001603 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001604 if (!isTokenStringLiteral())
1605 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001606
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001607 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001608 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001609 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001610
Sebastian Redleffa8d12008-12-10 00:02:53 +00001611 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001612 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001613
1614 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1615 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001616}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001617
1618/// objc-encode-expression:
1619/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001620Parser::OwningExprResult
1621Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001622 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001623
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001624 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001625
Chris Lattner4fef81d2008-08-05 06:19:09 +00001626 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001627 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1628
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001629 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001630
Douglas Gregor809070a2009-02-18 17:45:20 +00001631 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001632
Anders Carlsson4988ae32007-08-23 15:31:37 +00001633 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001634
Douglas Gregor809070a2009-02-18 17:45:20 +00001635 if (Ty.isInvalid())
1636 return ExprError();
1637
1638 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1639 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001640}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001641
1642/// objc-protocol-expression
1643/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001644Parser::OwningExprResult
1645Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001646 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001647
Chris Lattner4fef81d2008-08-05 06:19:09 +00001648 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001649 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1650
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001651 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001652
Chris Lattner4fef81d2008-08-05 06:19:09 +00001653 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001654 return ExprError(Diag(Tok, diag::err_expected_ident));
1655
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001656 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001657 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001658
Anders Carlsson4988ae32007-08-23 15:31:37 +00001659 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001660
Sebastian Redl1d922962008-12-13 15:32:12 +00001661 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1662 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001663}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001664
1665/// objc-selector-expression
1666/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001667Parser::OwningExprResult
1668Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001669 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001670
Chris Lattner4fef81d2008-08-05 06:19:09 +00001671 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001672 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1673
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001674 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001675 SourceLocation LParenLoc = ConsumeParen();
1676 SourceLocation sLoc;
1677 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001678 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1679 return ExprError(Diag(Tok, diag::err_expected_ident));
1680
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001681 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001682 unsigned nColons = 0;
1683 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001684 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001685 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001686 return ExprError(Diag(Tok, diag::err_expected_colon));
1687
Chris Lattnercb53b362007-12-27 19:57:00 +00001688 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001689 ConsumeToken(); // Eat the ':'.
1690 if (Tok.is(tok::r_paren))
1691 break;
1692 // Check for another keyword selector.
1693 SourceLocation Loc;
1694 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001695 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001696 if (!SelIdent && Tok.isNot(tok::colon))
1697 break;
1698 }
Steve Naroff887407e2007-12-05 22:21:29 +00001699 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001700 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001701 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001702 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1703 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001704 }