blob: 8a3672d29ce559d94652f542bc954f16163e41b5 [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'
Steve Naroffdac269b2007-08-20 21:31:48 +000031Parser::DeclTy *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);
54 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Steve Naroffdac269b2007-08-20 21:31:48 +000062Parser::DeclTy *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);
Steve Naroffdac269b2007-08-20 21:31:48 +000070 return 0;
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"))
Steve Naroffdac269b2007-08-20 21:31:48 +000083 return 0;
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///
117Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
118 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.
125 return 0;
126 }
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.
142 return 0;
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 ';'
147 return 0;
148 }
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;
153 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
156 return 0;
157
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
Steve Naroffe440eb82007-10-10 17:32:04 +0000161 DeclTy *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.
177 return 0;
178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattner06036d32008-07-26 04:13:19 +0000183 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
184 SourceLocation EndProtoLoc;
185 if (Tok.is(tok::less) &&
186 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
187 return 0;
188
189 DeclTy *ClsType =
190 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///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000214void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000215 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8a779312008-06-06 16:45:15 +0000216 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000217 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000218 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000219
Chris Lattnerbc662af2008-10-20 06:10:06 +0000220 SourceLocation AtEndLoc;
221
Steve Naroff294494e2007-08-22 16:35:03 +0000222 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000223 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000224 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
225 DeclTy *methodPrototype =
226 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000227 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000228 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
229 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000230 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
231 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000232 continue;
233 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000234
Chris Lattnere82a10f2008-10-20 05:46:22 +0000235 // Ignore excess semicolons.
236 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000237 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000238 continue;
239 }
240
Chris Lattnerbc662af2008-10-20 06:10:06 +0000241 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000242 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000243 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000244
245 // If we don't have an @ directive, parse it as a function definition.
246 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000247 // The code below does not consume '}'s because it is afraid of eating the
248 // end of a namespace. Because of the way this code is structured, an
249 // erroneous r_brace would cause an infinite loop if not handled here.
250 if (Tok.is(tok::r_brace))
251 break;
252
Steve Naroff4985ace2007-08-22 18:35:33 +0000253 // FIXME: as the name implies, this rule allows function definitions.
254 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000255 ParseDeclarationOrFunctionDefinition();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000256 continue;
257 }
258
259 // Otherwise, we have an @ directive, eat the @.
260 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000261 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000262
Chris Lattnera2449b22008-10-20 05:57:40 +0000263 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000264 AtEndLoc = AtLoc;
265 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000266 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000267
Chris Lattnerbc662af2008-10-20 06:10:06 +0000268 // Eat the identifier.
269 ConsumeToken();
270
Chris Lattnera2449b22008-10-20 05:57:40 +0000271 switch (DirectiveKind) {
272 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000273 // FIXME: If someone forgets an @end on a protocol, this loop will
274 // continue to eat up tons of stuff and spew lots of nonsense errors. It
275 // would probably be better to bail out if we saw an @class or @interface
276 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000277 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000278 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000279 SkipUntil(tok::r_brace, tok::at);
280 break;
281
282 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000283 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000284 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000285 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000286 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000287 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000288 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000289 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000290 break;
291
292 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000293 if (!getLang().ObjC2)
294 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
295
Chris Lattnere82a10f2008-10-20 05:46:22 +0000296 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000297 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000298 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000299 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000300
Chris Lattnere82a10f2008-10-20 05:46:22 +0000301 // Parse all the comma separated declarators.
302 DeclSpec DS;
303 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
304 ParseStructDeclaration(DS, FieldDeclarators);
305
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000306 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
307 tok::at);
308
Chris Lattnere82a10f2008-10-20 05:46:22 +0000309 // Convert them all to property declarations.
310 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
311 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000312 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000313 Diag(AtLoc, diag::err_objc_property_requires_field_name)
314 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000315 continue;
316 }
Fariborz Jahanian573acde2009-01-17 23:21:10 +0000317 if (FD.BitfieldSize) {
318 Diag(AtLoc, diag::err_objc_property_bitfield)
319 << FD.D.getSourceRange();
320 continue;
321 }
Chris Lattnerda3253d2008-10-20 06:33:53 +0000322
Chris Lattnere82a10f2008-10-20 05:46:22 +0000323 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000324 IdentifierInfo *SelName =
325 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
326
Chris Lattnere82a10f2008-10-20 05:46:22 +0000327 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000328 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 IdentifierInfo *SetterName = OCDS.getSetterName();
330 if (!SetterName)
Steve Naroff61f72cb2009-03-09 21:12:44 +0000331 SetterName =
332 SelectorTable::constructSetterName(PP.getIdentifierTable(),
333 FD.D.getIdentifier());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000334 Selector SetterSel =
335 PP.getSelectorTable().getUnarySelector(SetterName);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000336 bool isOverridingProperty = false;
Chris Lattnerda3253d2008-10-20 06:33:53 +0000337 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
338 GetterSel, SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000339 interfaceDecl,
340 &isOverridingProperty,
Chris Lattnerda3253d2008-10-20 06:33:53 +0000341 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000342 if (!isOverridingProperty)
343 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000344 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000345 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000346 }
Steve Naroff294494e2007-08-22 16:35:03 +0000347 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000348
349 // We break out of the big loop in two cases: when we see @end or when we see
350 // EOF. In the former case, eat the @end. In the later case, emit an error.
351 if (Tok.isObjCAtKeyword(tok::objc_end))
352 ConsumeToken(); // the "end" identifier
353 else
354 Diag(Tok, diag::err_objc_missing_end);
355
Chris Lattnera2449b22008-10-20 05:57:40 +0000356 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000357 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000358 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
359 allMethods.empty() ? 0 : &allMethods[0],
360 allMethods.size(),
361 allProperties.empty() ? 0 : &allProperties[0],
362 allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000363}
364
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000365/// Parse property attribute declarations.
366///
367/// property-attr-decl: '(' property-attrlist ')'
368/// property-attrlist:
369/// property-attribute
370/// property-attrlist ',' property-attribute
371/// property-attribute:
372/// getter '=' identifier
373/// setter '=' identifier ':'
374/// readonly
375/// readwrite
376/// assign
377/// retain
378/// copy
379/// nonatomic
380///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000381void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000382 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000383 SourceLocation LHSLoc = ConsumeParen(); // consume '('
384
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000385 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000386 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000387
388 // If this is not an identifier at all, bail out early.
389 if (II == 0) {
390 MatchRHSPunctuation(tok::r_paren, LHSLoc);
391 return;
392 }
393
Chris Lattner156b0612008-10-20 07:37:22 +0000394 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
395
Chris Lattner92e62b02008-11-20 04:42:34 +0000396 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000397 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000398 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000399 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000400 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000401 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000402 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000403 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000404 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000405 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000406 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000407 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000408 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000409 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000410 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
411 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000412 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000413
Chris Lattner8ca329c2008-10-20 07:24:39 +0000414 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000415 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000416 SkipUntil(tok::r_paren);
417 return;
418 }
419
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000420 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000421 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
422 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000423 ConsumeToken(); // consume method name
424
425 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
426 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000427 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000428 } else {
429 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
430 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000431 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000432 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000433 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000434 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000435 SkipUntil(tok::r_paren);
436 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000437 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000438
Chris Lattner156b0612008-10-20 07:37:22 +0000439 if (Tok.isNot(tok::comma))
440 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000441
Chris Lattner156b0612008-10-20 07:37:22 +0000442 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000443 }
Chris Lattner156b0612008-10-20 07:37:22 +0000444
445 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000446}
447
Steve Naroff3536b442007-09-06 21:24:23 +0000448/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000449/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000450/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000451///
452/// objc-instance-method: '-'
453/// objc-class-method: '+'
454///
Steve Naroff4985ace2007-08-22 18:35:33 +0000455/// objc-method-attributes: [OBJC2]
456/// __attribute__((deprecated))
457///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000458Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000459 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000460 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000461
462 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000463 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000464
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000465 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000466 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000467 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000468 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000469}
470
471/// objc-selector:
472/// identifier
473/// one of
474/// enum struct union if else while do for switch case default
475/// break continue return goto asm sizeof typeof __alignof
476/// unsigned long const short volatile signed restrict _Complex
477/// in out inout bycopy byref oneway int char float double void _Bool
478///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000479IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000480 switch (Tok.getKind()) {
481 default:
482 return 0;
483 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000484 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000485 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000486 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000487 case tok::kw_break:
488 case tok::kw_case:
489 case tok::kw_catch:
490 case tok::kw_char:
491 case tok::kw_class:
492 case tok::kw_const:
493 case tok::kw_const_cast:
494 case tok::kw_continue:
495 case tok::kw_default:
496 case tok::kw_delete:
497 case tok::kw_do:
498 case tok::kw_double:
499 case tok::kw_dynamic_cast:
500 case tok::kw_else:
501 case tok::kw_enum:
502 case tok::kw_explicit:
503 case tok::kw_export:
504 case tok::kw_extern:
505 case tok::kw_false:
506 case tok::kw_float:
507 case tok::kw_for:
508 case tok::kw_friend:
509 case tok::kw_goto:
510 case tok::kw_if:
511 case tok::kw_inline:
512 case tok::kw_int:
513 case tok::kw_long:
514 case tok::kw_mutable:
515 case tok::kw_namespace:
516 case tok::kw_new:
517 case tok::kw_operator:
518 case tok::kw_private:
519 case tok::kw_protected:
520 case tok::kw_public:
521 case tok::kw_register:
522 case tok::kw_reinterpret_cast:
523 case tok::kw_restrict:
524 case tok::kw_return:
525 case tok::kw_short:
526 case tok::kw_signed:
527 case tok::kw_sizeof:
528 case tok::kw_static:
529 case tok::kw_static_cast:
530 case tok::kw_struct:
531 case tok::kw_switch:
532 case tok::kw_template:
533 case tok::kw_this:
534 case tok::kw_throw:
535 case tok::kw_true:
536 case tok::kw_try:
537 case tok::kw_typedef:
538 case tok::kw_typeid:
539 case tok::kw_typename:
540 case tok::kw_typeof:
541 case tok::kw_union:
542 case tok::kw_unsigned:
543 case tok::kw_using:
544 case tok::kw_virtual:
545 case tok::kw_void:
546 case tok::kw_volatile:
547 case tok::kw_wchar_t:
548 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000549 case tok::kw__Bool:
550 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000551 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000552 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000553 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000554 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000555 }
Steve Naroff294494e2007-08-22 16:35:03 +0000556}
557
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000558/// objc-for-collection-in: 'in'
559///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000560bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000561 // FIXME: May have to do additional look-ahead to only allow for
562 // valid tokens following an 'in'; such as an identifier, unary operators,
563 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000564 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000565 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000566}
567
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000568/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000569/// qualifier list and builds their bitmask representation in the input
570/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000571///
572/// objc-type-qualifiers:
573/// objc-type-qualifier
574/// objc-type-qualifiers objc-type-qualifier
575///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000576void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000577 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000578 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000579 return;
580
581 const IdentifierInfo *II = Tok.getIdentifierInfo();
582 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000583 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000584 continue;
585
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000586 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000587 switch (i) {
588 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
590 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
591 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
592 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
593 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
594 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000595 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000596 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000597 ConsumeToken();
598 II = 0;
599 break;
600 }
601
602 // If this wasn't a recognized qualifier, bail out.
603 if (II) return;
604 }
605}
606
607/// objc-type-name:
608/// '(' objc-type-qualifiers[opt] type-name ')'
609/// '(' objc-type-qualifiers[opt] ')'
610///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000612 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000613
Chris Lattner4a76b292008-10-22 03:52:06 +0000614 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000615 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000616
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000617 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000619
Chris Lattner4a76b292008-10-22 03:52:06 +0000620 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000621 if (isTypeSpecifierQualifier()) {
622 TypeResult TypeSpec = ParseTypeName();
623 if (!TypeSpec.isInvalid())
624 Ty = TypeSpec.get();
625 }
Chris Lattnere8904e92008-08-23 01:48:03 +0000626
Steve Naroffd7333c22008-10-21 14:15:04 +0000627 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000628 ConsumeParen();
629 else if (Tok.getLocation() == TypeStartLoc) {
630 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000631 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000632 SkipUntil(tok::r_paren);
633 } else {
634 // Otherwise, we found *something*, but didn't get a ')' in the right
635 // place. Emit an error then return what we have as the type.
636 MatchRHSPunctuation(tok::r_paren, LParenLoc);
637 }
Steve Narofff28b2642007-09-05 23:30:30 +0000638 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000639}
640
641/// objc-method-decl:
642/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000643/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000644/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000645/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000646///
647/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000648/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000649/// objc-keyword-selector objc-keyword-decl
650///
651/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000652/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
653/// objc-selector ':' objc-keyword-attributes[opt] identifier
654/// ':' objc-type-name objc-keyword-attributes[opt] identifier
655/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000656///
Steve Naroff4985ace2007-08-22 18:35:33 +0000657/// objc-parmlist:
658/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000659///
Steve Naroff4985ace2007-08-22 18:35:33 +0000660/// objc-parms:
661/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000662///
Steve Naroff4985ace2007-08-22 18:35:33 +0000663/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000664/// , ...
665///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000666/// objc-keyword-attributes: [OBJC2]
667/// __attribute__((unused))
668///
Steve Naroffbef11852007-10-26 20:53:56 +0000669Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000670 tok::TokenKind mType,
671 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000672 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000673{
Chris Lattnere8904e92008-08-23 01:48:03 +0000674 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000675 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000677 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000678 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000679
Steve Naroffbef11852007-10-26 20:53:56 +0000680 SourceLocation selLoc;
681 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000682
Steve Naroff84c43102009-02-11 20:43:13 +0000683 // An unnamed colon is valid.
684 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000685 Diag(Tok, diag::err_expected_selector_for_method)
686 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000687 // Skip until we get a ; or {}.
688 SkipUntil(tok::r_brace);
689 return 0;
690 }
691
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000692 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000693 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000694 // If attributes exist after the method, parse them.
695 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000696 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000697 MethodAttrs = ParseAttributes();
698
699 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000700 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000701 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000702 0, 0, 0, CargNames,
703 MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000704 }
Steve Narofff28b2642007-09-05 23:30:30 +0000705
Steve Naroff68d331a2007-09-27 14:38:14 +0000706 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
707 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000708 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000709 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000710
711 Action::TypeTy *TypeInfo;
712 while (1) {
713 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000714
Chris Lattnerff384912007-10-07 02:00:24 +0000715 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000716 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000717 Diag(Tok, diag::err_expected_colon);
718 break;
719 }
720 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000722 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000723 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000724 else
725 TypeInfo = 0;
726 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000727 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000728
Chris Lattnerff384912007-10-07 02:00:24 +0000729 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000730 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000731 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000732
Chris Lattnerdf195262007-10-09 17:51:17 +0000733 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000734 Diag(Tok, diag::err_expected_ident); // missing argument name.
735 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000736 }
Chris Lattnerff384912007-10-07 02:00:24 +0000737 ArgNames.push_back(Tok.getIdentifierInfo());
738 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000739
Chris Lattnerff384912007-10-07 02:00:24 +0000740 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000741 SourceLocation Loc;
742 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000743 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000744 break;
745 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000746 }
Chris Lattnerff384912007-10-07 02:00:24 +0000747
Steve Naroff335eafa2007-11-15 12:35:21 +0000748 bool isVariadic = false;
749
Chris Lattnerff384912007-10-07 02:00:24 +0000750 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000751 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000752 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000753 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000754 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000755 ConsumeToken();
756 break;
757 }
Chris Lattnerff384912007-10-07 02:00:24 +0000758 DeclSpec DS;
759 ParseDeclarationSpecifiers(DS);
760 // Parse the declarator.
761 Declarator ParmDecl(DS, Declarator::PrototypeContext);
762 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000763 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000764 }
765
766 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000767 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000768 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000769 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000770 MethodAttrs = ParseAttributes();
771
772 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
773 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000774 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000775 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000776 &ArgTypeQuals[0], &KeyTypes[0],
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000777 &ArgNames[0], CargNames,
778 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000779 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000780}
781
Steve Naroffdac269b2007-08-20 21:31:48 +0000782/// objc-protocol-refs:
783/// '<' identifier-list '>'
784///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000785bool Parser::
Chris Lattnere13b9592008-07-26 04:03:38 +0000786ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
787 bool WarnOnDeclarations, SourceLocation &EndLoc) {
788 assert(Tok.is(tok::less) && "expected <");
789
790 ConsumeToken(); // the "<"
791
792 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
793
794 while (1) {
795 if (Tok.isNot(tok::identifier)) {
796 Diag(Tok, diag::err_expected_ident);
797 SkipUntil(tok::greater);
798 return true;
799 }
800 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
801 Tok.getLocation()));
802 ConsumeToken();
803
804 if (Tok.isNot(tok::comma))
805 break;
806 ConsumeToken();
807 }
808
809 // Consume the '>'.
810 if (Tok.isNot(tok::greater)) {
811 Diag(Tok, diag::err_expected_greater);
812 return true;
813 }
814
815 EndLoc = ConsumeAnyToken();
816
817 // Convert the list of protocols identifiers into a list of protocol decls.
818 Actions.FindProtocolDeclaration(WarnOnDeclarations,
819 &ProtocolIdents[0], ProtocolIdents.size(),
820 Protocols);
821 return false;
822}
823
Steve Naroffdac269b2007-08-20 21:31:48 +0000824/// objc-class-instance-variables:
825/// '{' objc-instance-variable-decl-list[opt] '}'
826///
827/// objc-instance-variable-decl-list:
828/// objc-visibility-spec
829/// objc-instance-variable-decl ';'
830/// ';'
831/// objc-instance-variable-decl-list objc-visibility-spec
832/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
833/// objc-instance-variable-decl-list ';'
834///
835/// objc-visibility-spec:
836/// @private
837/// @protected
838/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000839/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000840///
841/// objc-instance-variable-decl:
842/// struct-declaration
843///
Steve Naroff60fccee2007-10-29 21:38:07 +0000844void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
845 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000846 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000847 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000848 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
849
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000850 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000851
Steve Naroffddbff782007-08-21 21:17:12 +0000852 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000853
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000854 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000855 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000856 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000857 // Each iteration of this loop reads one objc-instance-variable-decl.
858
859 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000860 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000861 Diag(Tok, diag::ext_extra_struct_semi);
862 ConsumeToken();
863 continue;
864 }
Chris Lattnere1359422008-04-10 06:46:29 +0000865
Steve Naroffddbff782007-08-21 21:17:12 +0000866 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000867 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000868 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000869 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000870 case tok::objc_private:
871 case tok::objc_public:
872 case tok::objc_protected:
873 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000874 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000875 ConsumeToken();
876 continue;
877 default:
878 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000879 continue;
880 }
881 }
Chris Lattnere1359422008-04-10 06:46:29 +0000882
883 // Parse all the comma separated declarators.
884 DeclSpec DS;
885 FieldDeclarators.clear();
886 ParseStructDeclaration(DS, FieldDeclarators);
887
888 // Convert them all to fields.
889 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
890 FieldDeclarator &FD = FieldDeclarators[i];
891 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000892 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000893 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000894 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000895 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000896 }
Steve Naroff3536b442007-09-06 21:24:23 +0000897
Chris Lattnerdf195262007-10-09 17:51:17 +0000898 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000899 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000900 } else {
901 Diag(Tok, diag::err_expected_semi_decl_list);
902 // Skip to end of block or statement
903 SkipUntil(tok::r_brace, true, true);
904 }
905 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000906 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000907 // Call ActOnFields() even if we don't have any decls. This is useful
908 // for code rewriting tools that need to be aware of the empty list.
909 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
910 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000911 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000912 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000913}
Steve Naroffdac269b2007-08-20 21:31:48 +0000914
915/// objc-protocol-declaration:
916/// objc-protocol-definition
917/// objc-protocol-forward-reference
918///
919/// objc-protocol-definition:
920/// @protocol identifier
921/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000922/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000923/// @end
924///
925/// objc-protocol-forward-reference:
926/// @protocol identifier-list ';'
927///
928/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000929/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000930/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000931Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
932 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000933 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000934 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
935 ConsumeToken(); // the "protocol" identifier
936
Chris Lattnerdf195262007-10-09 17:51:17 +0000937 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000938 Diag(Tok, diag::err_expected_ident); // missing protocol name.
939 return 0;
940 }
941 // Save the protocol name, then consume it.
942 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
943 SourceLocation nameLoc = ConsumeToken();
944
Chris Lattnerdf195262007-10-09 17:51:17 +0000945 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000946 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000947 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000948 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
949 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000950 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000951
Chris Lattnerdf195262007-10-09 17:51:17 +0000952 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000953 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
954 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
955
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000956 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000957 while (1) {
958 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000960 Diag(Tok, diag::err_expected_ident);
961 SkipUntil(tok::semi);
962 return 0;
963 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000964 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
965 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000966 ConsumeToken(); // the identifier
967
Chris Lattnerdf195262007-10-09 17:51:17 +0000968 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000969 break;
970 }
971 // Consume the ';'.
972 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
973 return 0;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000974
Steve Naroffe440eb82007-10-10 17:32:04 +0000975 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000976 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000977 ProtocolRefs.size(),
978 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000979 }
980
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000981 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000982 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000983
Chris Lattnere13b9592008-07-26 04:03:38 +0000984 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000985 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000986 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner7caeabd2008-07-21 22:17:28 +0000987 return 0;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000988
Chris Lattnere13b9592008-07-26 04:03:38 +0000989 DeclTy *ProtoType =
990 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
991 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000992 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000993 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000994 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000995}
Steve Naroffdac269b2007-08-20 21:31:48 +0000996
997/// objc-implementation:
998/// objc-class-implementation-prologue
999/// objc-category-implementation-prologue
1000///
1001/// objc-class-implementation-prologue:
1002/// @implementation identifier objc-superclass[opt]
1003/// objc-class-instance-variables[opt]
1004///
1005/// objc-category-implementation-prologue:
1006/// @implementation identifier ( identifier )
1007
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001008Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1009 SourceLocation atLoc) {
1010 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1011 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1012 ConsumeToken(); // the "implementation" identifier
1013
Chris Lattnerdf195262007-10-09 17:51:17 +00001014 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001015 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1016 return 0;
1017 }
1018 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001019 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001020 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1021
Chris Lattnerdf195262007-10-09 17:51:17 +00001022 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001023 // we have a category implementation.
1024 SourceLocation lparenLoc = ConsumeParen();
1025 SourceLocation categoryLoc, rparenLoc;
1026 IdentifierInfo *categoryId = 0;
1027
Chris Lattnerdf195262007-10-09 17:51:17 +00001028 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001029 categoryId = Tok.getIdentifierInfo();
1030 categoryLoc = ConsumeToken();
1031 } else {
1032 Diag(Tok, diag::err_expected_ident); // missing category name.
1033 return 0;
1034 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001035 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001036 Diag(Tok, diag::err_expected_rparen);
1037 SkipUntil(tok::r_paren, false); // don't stop at ';'
1038 return 0;
1039 }
1040 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +00001041 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001042 atLoc, nameId, nameLoc, categoryId,
1043 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001044 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001045 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001046 }
1047 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001048 SourceLocation superClassLoc;
1049 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001050 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001051 // We have a super class
1052 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001053 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001054 Diag(Tok, diag::err_expected_ident); // missing super class name.
1055 return 0;
1056 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001057 superClassId = Tok.getIdentifierInfo();
1058 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001059 }
Steve Naroffe440eb82007-10-10 17:32:04 +00001060 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001061 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001062 superClassId, superClassLoc);
1063
Steve Naroff60fccee2007-10-29 21:38:07 +00001064 if (Tok.is(tok::l_brace)) // we have ivars
1065 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001066 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001067
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001068 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001069}
Steve Naroff60fccee2007-10-29 21:38:07 +00001070
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001071Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1072 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1073 "ParseObjCAtEndDeclaration(): Expected @end");
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001074 DeclTy *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001075 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001076 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001077 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001078 ObjCImpDecl = 0;
1079 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001080 else
1081 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001082 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001083}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001084
1085/// compatibility-alias-decl:
1086/// @compatibility_alias alias-name class-name ';'
1087///
1088Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1089 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1090 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1091 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001092 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001093 Diag(Tok, diag::err_expected_ident);
1094 return 0;
1095 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001096 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1097 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001098 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001099 Diag(Tok, diag::err_expected_ident);
1100 return 0;
1101 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001102 IdentifierInfo *classId = Tok.getIdentifierInfo();
1103 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1104 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001105 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001106 return 0;
1107 }
1108 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1109 aliasId, aliasLoc,
1110 classId, classLoc);
1111 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001112}
1113
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001114/// property-synthesis:
1115/// @synthesize property-ivar-list ';'
1116///
1117/// property-ivar-list:
1118/// property-ivar
1119/// property-ivar-list ',' property-ivar
1120///
1121/// property-ivar:
1122/// identifier
1123/// identifier '=' identifier
1124///
1125Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1126 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1127 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001128 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001129 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001130 Diag(Tok, diag::err_expected_ident);
1131 return 0;
1132 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001133 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001134 IdentifierInfo *propertyIvar = 0;
1135 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1136 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001137 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001138 // property '=' ivar-name
1139 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001140 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001141 Diag(Tok, diag::err_expected_ident);
1142 break;
1143 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001144 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001145 ConsumeToken(); // consume ivar-name
1146 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001147 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1148 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001149 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001150 break;
1151 ConsumeToken(); // consume ','
1152 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001153 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001154 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001155 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001156}
1157
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001158/// property-dynamic:
1159/// @dynamic property-list
1160///
1161/// property-list:
1162/// identifier
1163/// property-list ',' identifier
1164///
1165Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1166 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1167 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1168 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001169 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001170 Diag(Tok, diag::err_expected_ident);
1171 return 0;
1172 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001173 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001174 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1175 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1176 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1177 propertyId, 0);
1178
Chris Lattnerdf195262007-10-09 17:51:17 +00001179 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001180 break;
1181 ConsumeToken(); // consume ','
1182 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001183 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001184 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001185 return 0;
1186}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001187
1188/// objc-throw-statement:
1189/// throw expression[opt];
1190///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001191Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001192 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001193 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001195 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001196 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001197 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001198 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001199 }
1200 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001201 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001202 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001203}
1204
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001205/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001206/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001207///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001208Parser::OwningStmtResult
1209Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001210 ConsumeToken(); // consume synchronized
1211 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001212 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001213 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001214 }
1215 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001216 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001217 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001218 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001219 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001220 }
1221 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001222 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001223 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001224 }
1225 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001226 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001227 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001228 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001229 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001230 // Enter a scope to hold everything within the compound stmt. Compound
1231 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001232 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001233
Sebastian Redl61364dd2008-12-11 19:30:53 +00001234 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001235
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001236 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001237 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001238 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001239 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001240}
1241
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001242/// objc-try-catch-statement:
1243/// @try compound-statement objc-catch-list[opt]
1244/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1245///
1246/// objc-catch-list:
1247/// @catch ( parameter-declaration ) compound-statement
1248/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1249/// catch-parameter-declaration:
1250/// parameter-declaration
1251/// '...' [OBJC2]
1252///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001253Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001254 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001255
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001256 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001257 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001258 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001259 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001260 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001261 OwningStmtResult CatchStmts(Actions);
1262 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001263 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001264 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001265 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001266 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001267 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001268
Chris Lattnerdf195262007-10-09 17:51:17 +00001269 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001270 // At this point, we need to lookahead to determine if this @ is the start
1271 // of an @catch or @finally. We don't want to consume the @ token if this
1272 // is an @try or @encode or something else.
1273 Token AfterAt = GetLookAheadToken(1);
1274 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1275 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1276 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001277
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001278 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001279 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00001280 DeclTy *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001281 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001282 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001283 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001284 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001285 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001286 DeclSpec DS;
1287 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001288 // For some odd reason, the name of the exception variable is
Steve Naroff7ba138a2009-03-03 19:52:17 +00001289 // optional. As a result, we need to use "PrototypeContext", because
1290 // we must accept either 'declarator' or 'abstract-declarator' here.
1291 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1292 ParseDeclarator(ParmDecl);
1293
1294 // Inform the actions module about the parameter declarator, so it
1295 // gets added to the current scope.
1296 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001297 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001298 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001299 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001300
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001301 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001302 if (Tok.is(tok::l_brace))
1303 CatchBody = ParseCompoundStatementBody();
1304 else
1305 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001306 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001307 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001308 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001309 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001310 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001311 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001312 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1313 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001314 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001315 }
1316 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001317 } else {
1318 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001319 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001320 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001321
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001322 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001323 if (Tok.is(tok::l_brace))
1324 FinallyBody = ParseCompoundStatementBody();
1325 else
1326 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001327 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001328 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001329 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001330 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001331 catch_or_finally_seen = true;
1332 break;
1333 }
1334 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001335 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001336 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001337 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001338 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001339 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1340 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001341}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001342
Steve Naroff3536b442007-09-06 21:24:23 +00001343/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001344///
Steve Naroff71c0a952007-11-13 23:01:27 +00001345Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001346 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner73e80f62009-03-05 02:03:49 +00001347
Chris Lattner49f28ca2009-03-05 08:00:35 +00001348 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1349 PP.getSourceManager(),
1350 "parsing Objective-C method");
Chris Lattner73e80f62009-03-05 02:03:49 +00001351
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001352 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001353 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001354 ConsumeToken();
1355
Steve Naroff409be832007-11-11 19:54:21 +00001356 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001357 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001358 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001359
1360 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1361 SkipUntil(tok::l_brace, true, true);
1362
1363 // If we didn't find the '{', bail out.
1364 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001365 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001366 }
Steve Naroff409be832007-11-11 19:54:21 +00001367 SourceLocation BraceLoc = Tok.getLocation();
1368
1369 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001370 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001371
1372 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001373 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001374 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001375
1376 OwningStmtResult FnBody(ParseCompoundStatementBody());
1377
Steve Naroff409be832007-11-11 19:54:21 +00001378 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001379 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001380 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1381 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001382
Steve Naroff32ce8372009-03-02 22:00:56 +00001383 // TODO: Pass argument information.
1384 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1385
Steve Naroff409be832007-11-11 19:54:21 +00001386 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001387 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001388
Steve Naroff71c0a952007-11-13 23:01:27 +00001389 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001390}
Anders Carlsson55085182007-08-21 17:43:55 +00001391
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001392Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001393 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001394 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001395 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1396 return ParseObjCThrowStmt(AtLoc);
1397 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1398 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001399 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001400 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001401 // If the expression is invalid, skip ahead to the next semicolon. Not
1402 // doing this opens us up to the possibility of infinite loops if
1403 // ParseExpression does not consume any tokens.
1404 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001405 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001406 }
1407 // Otherwise, eat the semicolon.
1408 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001409 return Actions.ActOnExprStmt(move(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001410}
1411
Sebastian Redl1d922962008-12-13 15:32:12 +00001412Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001413 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001414 case tok::string_literal: // primary-expression: string-literal
1415 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001416 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001417 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001418 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001419 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001420
Chris Lattner4fef81d2008-08-05 06:19:09 +00001421 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1422 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001423 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001424 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001425 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001426 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001427 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001428 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001429 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001430 }
Anders Carlsson55085182007-08-21 17:43:55 +00001431 }
Anders Carlsson55085182007-08-21 17:43:55 +00001432}
1433
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001434/// objc-message-expr:
1435/// '[' objc-receiver objc-message-args ']'
1436///
1437/// objc-receiver:
1438/// expression
1439/// class-name
1440/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001441Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001442 assert(Tok.is(tok::l_square) && "'[' expected");
1443 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1444
1445 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001446 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001447 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +00001448 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001449 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1450 ExprArg(Actions));
Chris Lattner699b6612008-01-25 18:59:06 +00001451 }
1452
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001453 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001454 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001455 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001456 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001457 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001458
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001459 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001460 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001461}
Sebastian Redl1d922962008-12-13 15:32:12 +00001462
Chris Lattner699b6612008-01-25 18:59:06 +00001463/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1464/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001465///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001466/// objc-message-args:
1467/// objc-selector
1468/// objc-keywordarg-list
1469///
1470/// objc-keywordarg-list:
1471/// objc-keywordarg
1472/// objc-keywordarg-list objc-keywordarg
1473///
1474/// objc-keywordarg:
1475/// selector-name[opt] ':' objc-keywordexpr
1476///
1477/// objc-keywordexpr:
1478/// nonempty-expr-list
1479///
1480/// nonempty-expr-list:
1481/// assignment-expression
1482/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001483///
1484Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001485Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001486 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001487 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001488 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001489 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001490 SourceLocation Loc;
1491 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001492
Anders Carlssonff975cf2009-02-14 18:21:46 +00001493 SourceLocation SelectorLoc = Loc;
1494
Steve Naroff68d331a2007-09-27 14:38:14 +00001495 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001496 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001497
Chris Lattnerdf195262007-10-09 17:51:17 +00001498 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001499 while (1) {
1500 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001501 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001502
Chris Lattnerdf195262007-10-09 17:51:17 +00001503 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001504 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001505 // We must manually skip to a ']', otherwise the expression skipper will
1506 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1507 // the enclosing expression.
1508 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001509 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001510 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001511
Steve Naroff68d331a2007-09-27 14:38:14 +00001512 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001513 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001514 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001515 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001516 // We must manually skip to a ']', otherwise the expression skipper will
1517 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1518 // the enclosing expression.
1519 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001520 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001521 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001522
Steve Naroff37387c92007-09-17 20:25:27 +00001523 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001524 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001525
Steve Naroff37387c92007-09-17 20:25:27 +00001526 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001527 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001528 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001529 break;
1530 // We have a selector or a colon, continue parsing.
1531 }
1532 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001533 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001534 ConsumeToken(); // Eat the ','.
1535 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001536 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001537 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001538 // We must manually skip to a ']', otherwise the expression skipper will
1539 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1540 // the enclosing expression.
1541 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001542 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001543 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001544
Steve Naroff49f109c2007-11-15 13:05:42 +00001545 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001546 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001547 }
1548 } else if (!selIdent) {
1549 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001550
Chris Lattner4fef81d2008-08-05 06:19:09 +00001551 // We must manually skip to a ']', otherwise the expression skipper will
1552 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1553 // the enclosing expression.
1554 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001555 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001556 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001557
Chris Lattnerdf195262007-10-09 17:51:17 +00001558 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001559 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001560 // We must manually skip to a ']', otherwise the expression skipper will
1561 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1562 // the enclosing expression.
1563 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001564 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001565 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001566
Chris Lattner699b6612008-01-25 18:59:06 +00001567 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001568
Steve Naroff29238a02007-10-05 18:42:47 +00001569 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001570 if (nKeys == 0)
1571 KeyIdents.push_back(selIdent);
1572 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001573
Chris Lattnerff384912007-10-07 02:00:24 +00001574 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001575 if (ReceiverName)
1576 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001577 LBracLoc, NameLoc, SelectorLoc,
1578 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001579 KeyExprs.take(), KeyExprs.size()));
1580 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001581 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001582 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001583}
1584
Sebastian Redl1d922962008-12-13 15:32:12 +00001585Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001586 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001587 if (Res.isInvalid()) return move(Res);
1588
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001589 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1590 // expressions. At this point, we know that the only valid thing that starts
1591 // with '@' is an @"".
1592 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001593 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001594 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001595 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001596
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001597 while (Tok.is(tok::at)) {
1598 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001599
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001600 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001601 if (!isTokenStringLiteral())
1602 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001603
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001604 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001605 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001606 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001607
Sebastian Redleffa8d12008-12-10 00:02:53 +00001608 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001609 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001610
1611 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1612 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001613}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001614
1615/// objc-encode-expression:
1616/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001617Parser::OwningExprResult
1618Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001619 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001620
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001621 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001622
Chris Lattner4fef81d2008-08-05 06:19:09 +00001623 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001624 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1625
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001626 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001627
Douglas Gregor809070a2009-02-18 17:45:20 +00001628 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001629
Anders Carlsson4988ae32007-08-23 15:31:37 +00001630 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001631
Douglas Gregor809070a2009-02-18 17:45:20 +00001632 if (Ty.isInvalid())
1633 return ExprError();
1634
1635 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1636 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001637}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001638
1639/// objc-protocol-expression
1640/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001641Parser::OwningExprResult
1642Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001643 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001644
Chris Lattner4fef81d2008-08-05 06:19:09 +00001645 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001646 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1647
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001648 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001649
Chris Lattner4fef81d2008-08-05 06:19:09 +00001650 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001651 return ExprError(Diag(Tok, diag::err_expected_ident));
1652
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001653 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001654 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001655
Anders Carlsson4988ae32007-08-23 15:31:37 +00001656 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001657
Sebastian Redl1d922962008-12-13 15:32:12 +00001658 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1659 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001660}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001661
1662/// objc-selector-expression
1663/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001664Parser::OwningExprResult
1665Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001666 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001667
Chris Lattner4fef81d2008-08-05 06:19:09 +00001668 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001669 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1670
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001671 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001672 SourceLocation LParenLoc = ConsumeParen();
1673 SourceLocation sLoc;
1674 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001675 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1676 return ExprError(Diag(Tok, diag::err_expected_ident));
1677
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001678 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001679 unsigned nColons = 0;
1680 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001681 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001682 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001683 return ExprError(Diag(Tok, diag::err_expected_colon));
1684
Chris Lattnercb53b362007-12-27 19:57:00 +00001685 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001686 ConsumeToken(); // Eat the ':'.
1687 if (Tok.is(tok::r_paren))
1688 break;
1689 // Check for another keyword selector.
1690 SourceLocation Loc;
1691 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001692 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001693 if (!SelIdent && Tok.isNot(tok::colon))
1694 break;
1695 }
Steve Naroff887407e2007-12-05 22:21:29 +00001696 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001697 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001698 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001699 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1700 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001701 }