blob: e9bb3d7c31e28bf15a8a3a96bc3d98e491dce74b [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
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000202/// constructSetterName - Return the setter name for the given
203/// identifier, i.e. "set" + Name where the initial character of Name
204/// has been capitalized.
205static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
206 const IdentifierInfo *Name) {
Chris Lattneredc66f32008-11-19 07:41:27 +0000207 llvm::SmallString<100> SelectorName;
Chris Lattner69d27b92008-11-20 07:09:32 +0000208 SelectorName = "set";
Chris Lattneredc66f32008-11-19 07:41:27 +0000209 SelectorName.append(Name->getName(), Name->getName()+Name->getLength());
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000210 SelectorName[3] = toupper(SelectorName[3]);
Chris Lattneredc66f32008-11-19 07:41:27 +0000211 return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]);
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000212}
213
Steve Naroffdac269b2007-08-20 21:31:48 +0000214/// objc-interface-decl-list:
215/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000216/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000217/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000218/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000219/// objc-interface-decl-list declaration
220/// objc-interface-decl-list ';'
221///
Steve Naroff294494e2007-08-22 16:35:03 +0000222/// objc-method-requirement: [OBJC2]
223/// @required
224/// @optional
225///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000226void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000227 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8a779312008-06-06 16:45:15 +0000228 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000229 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000230 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000231
Chris Lattnerbc662af2008-10-20 06:10:06 +0000232 SourceLocation AtEndLoc;
233
Steve Naroff294494e2007-08-22 16:35:03 +0000234 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000235 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000236 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
237 DeclTy *methodPrototype =
238 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000239 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000240 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
241 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
243 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000244 continue;
245 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000246
Chris Lattnere82a10f2008-10-20 05:46:22 +0000247 // Ignore excess semicolons.
248 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000249 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000250 continue;
251 }
252
Chris Lattnerbc662af2008-10-20 06:10:06 +0000253 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000254 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000255 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000256
257 // If we don't have an @ directive, parse it as a function definition.
258 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000259 // The code below does not consume '}'s because it is afraid of eating the
260 // end of a namespace. Because of the way this code is structured, an
261 // erroneous r_brace would cause an infinite loop if not handled here.
262 if (Tok.is(tok::r_brace))
263 break;
264
Steve Naroff4985ace2007-08-22 18:35:33 +0000265 // FIXME: as the name implies, this rule allows function definitions.
266 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000267 ParseDeclarationOrFunctionDefinition();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000268 continue;
269 }
270
271 // Otherwise, we have an @ directive, eat the @.
272 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000273 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000274
Chris Lattnera2449b22008-10-20 05:57:40 +0000275 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000276 AtEndLoc = AtLoc;
277 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000278 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000279
Chris Lattnerbc662af2008-10-20 06:10:06 +0000280 // Eat the identifier.
281 ConsumeToken();
282
Chris Lattnera2449b22008-10-20 05:57:40 +0000283 switch (DirectiveKind) {
284 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000285 // FIXME: If someone forgets an @end on a protocol, this loop will
286 // continue to eat up tons of stuff and spew lots of nonsense errors. It
287 // would probably be better to bail out if we saw an @class or @interface
288 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000289 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000290 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 SkipUntil(tok::r_brace, tok::at);
292 break;
293
294 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000295 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000296 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000297 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000298 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000299 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000300 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000301 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000302 break;
303
304 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000305 if (!getLang().ObjC2)
306 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
307
Chris Lattnere82a10f2008-10-20 05:46:22 +0000308 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000309 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000310 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000311 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000312
Chris Lattnere82a10f2008-10-20 05:46:22 +0000313 // Parse all the comma separated declarators.
314 DeclSpec DS;
315 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
316 ParseStructDeclaration(DS, FieldDeclarators);
317
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000318 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
319 tok::at);
320
Chris Lattnere82a10f2008-10-20 05:46:22 +0000321 // Convert them all to property declarations.
322 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
323 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000324 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000325 Diag(AtLoc, diag::err_objc_property_requires_field_name)
326 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000327 continue;
328 }
Fariborz Jahanian573acde2009-01-17 23:21:10 +0000329 if (FD.BitfieldSize) {
330 Diag(AtLoc, diag::err_objc_property_bitfield)
331 << FD.D.getSourceRange();
332 continue;
333 }
Chris Lattnerda3253d2008-10-20 06:33:53 +0000334
Chris Lattnere82a10f2008-10-20 05:46:22 +0000335 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000336 IdentifierInfo *SelName =
337 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
338
Chris Lattnere82a10f2008-10-20 05:46:22 +0000339 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000340 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000341 IdentifierInfo *SetterName = OCDS.getSetterName();
342 if (!SetterName)
343 SetterName = constructSetterName(PP.getIdentifierTable(),
344 FD.D.getIdentifier());
345 Selector SetterSel =
346 PP.getSelectorTable().getUnarySelector(SetterName);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000347 bool isOverridingProperty = false;
Chris Lattnerda3253d2008-10-20 06:33:53 +0000348 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
349 GetterSel, SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000350 interfaceDecl,
351 &isOverridingProperty,
Chris Lattnerda3253d2008-10-20 06:33:53 +0000352 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000353 if (!isOverridingProperty)
354 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000355 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000356 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000357 }
Steve Naroff294494e2007-08-22 16:35:03 +0000358 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000359
360 // We break out of the big loop in two cases: when we see @end or when we see
361 // EOF. In the former case, eat the @end. In the later case, emit an error.
362 if (Tok.isObjCAtKeyword(tok::objc_end))
363 ConsumeToken(); // the "end" identifier
364 else
365 Diag(Tok, diag::err_objc_missing_end);
366
Chris Lattnera2449b22008-10-20 05:57:40 +0000367 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000368 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000369 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
370 allMethods.empty() ? 0 : &allMethods[0],
371 allMethods.size(),
372 allProperties.empty() ? 0 : &allProperties[0],
373 allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000374}
375
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000376/// Parse property attribute declarations.
377///
378/// property-attr-decl: '(' property-attrlist ')'
379/// property-attrlist:
380/// property-attribute
381/// property-attrlist ',' property-attribute
382/// property-attribute:
383/// getter '=' identifier
384/// setter '=' identifier ':'
385/// readonly
386/// readwrite
387/// assign
388/// retain
389/// copy
390/// nonatomic
391///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000392void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000393 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000394 SourceLocation LHSLoc = ConsumeParen(); // consume '('
395
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000396 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000397 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000398
399 // If this is not an identifier at all, bail out early.
400 if (II == 0) {
401 MatchRHSPunctuation(tok::r_paren, LHSLoc);
402 return;
403 }
404
Chris Lattner156b0612008-10-20 07:37:22 +0000405 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
406
Chris Lattner92e62b02008-11-20 04:42:34 +0000407 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000409 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000410 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000411 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000412 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000413 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000414 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000415 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000416 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000417 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000418 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000419 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000420 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000421 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
422 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000423 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000424
Chris Lattner8ca329c2008-10-20 07:24:39 +0000425 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000426 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000427 SkipUntil(tok::r_paren);
428 return;
429 }
430
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000431 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
433 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000434 ConsumeToken(); // consume method name
435
436 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
437 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000438 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000439 } else {
440 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
441 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000442 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000443 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000444 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000445 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000446 SkipUntil(tok::r_paren);
447 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000448 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000449
Chris Lattner156b0612008-10-20 07:37:22 +0000450 if (Tok.isNot(tok::comma))
451 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000452
Chris Lattner156b0612008-10-20 07:37:22 +0000453 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000454 }
Chris Lattner156b0612008-10-20 07:37:22 +0000455
456 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000457}
458
Steve Naroff3536b442007-09-06 21:24:23 +0000459/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000460/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000461/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000462///
463/// objc-instance-method: '-'
464/// objc-class-method: '+'
465///
Steve Naroff4985ace2007-08-22 18:35:33 +0000466/// objc-method-attributes: [OBJC2]
467/// __attribute__((deprecated))
468///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000469Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000470 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000471 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000472
473 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000474 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000475
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000476 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000477 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000478 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000479 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000480}
481
482/// objc-selector:
483/// identifier
484/// one of
485/// enum struct union if else while do for switch case default
486/// break continue return goto asm sizeof typeof __alignof
487/// unsigned long const short volatile signed restrict _Complex
488/// in out inout bycopy byref oneway int char float double void _Bool
489///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000490IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000491 switch (Tok.getKind()) {
492 default:
493 return 0;
494 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000495 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000496 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000497 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000498 case tok::kw_break:
499 case tok::kw_case:
500 case tok::kw_catch:
501 case tok::kw_char:
502 case tok::kw_class:
503 case tok::kw_const:
504 case tok::kw_const_cast:
505 case tok::kw_continue:
506 case tok::kw_default:
507 case tok::kw_delete:
508 case tok::kw_do:
509 case tok::kw_double:
510 case tok::kw_dynamic_cast:
511 case tok::kw_else:
512 case tok::kw_enum:
513 case tok::kw_explicit:
514 case tok::kw_export:
515 case tok::kw_extern:
516 case tok::kw_false:
517 case tok::kw_float:
518 case tok::kw_for:
519 case tok::kw_friend:
520 case tok::kw_goto:
521 case tok::kw_if:
522 case tok::kw_inline:
523 case tok::kw_int:
524 case tok::kw_long:
525 case tok::kw_mutable:
526 case tok::kw_namespace:
527 case tok::kw_new:
528 case tok::kw_operator:
529 case tok::kw_private:
530 case tok::kw_protected:
531 case tok::kw_public:
532 case tok::kw_register:
533 case tok::kw_reinterpret_cast:
534 case tok::kw_restrict:
535 case tok::kw_return:
536 case tok::kw_short:
537 case tok::kw_signed:
538 case tok::kw_sizeof:
539 case tok::kw_static:
540 case tok::kw_static_cast:
541 case tok::kw_struct:
542 case tok::kw_switch:
543 case tok::kw_template:
544 case tok::kw_this:
545 case tok::kw_throw:
546 case tok::kw_true:
547 case tok::kw_try:
548 case tok::kw_typedef:
549 case tok::kw_typeid:
550 case tok::kw_typename:
551 case tok::kw_typeof:
552 case tok::kw_union:
553 case tok::kw_unsigned:
554 case tok::kw_using:
555 case tok::kw_virtual:
556 case tok::kw_void:
557 case tok::kw_volatile:
558 case tok::kw_wchar_t:
559 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000560 case tok::kw__Bool:
561 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000562 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000563 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000564 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000565 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000566 }
Steve Naroff294494e2007-08-22 16:35:03 +0000567}
568
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000569/// objc-for-collection-in: 'in'
570///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000571bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000572 // FIXME: May have to do additional look-ahead to only allow for
573 // valid tokens following an 'in'; such as an identifier, unary operators,
574 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000575 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000576 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000577}
578
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000580/// qualifier list and builds their bitmask representation in the input
581/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000582///
583/// objc-type-qualifiers:
584/// objc-type-qualifier
585/// objc-type-qualifiers objc-type-qualifier
586///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000588 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000589 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000590 return;
591
592 const IdentifierInfo *II = Tok.getIdentifierInfo();
593 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000595 continue;
596
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000598 switch (i) {
599 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000600 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
601 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
602 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
603 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
604 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
605 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000606 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000608 ConsumeToken();
609 II = 0;
610 break;
611 }
612
613 // If this wasn't a recognized qualifier, bail out.
614 if (II) return;
615 }
616}
617
618/// objc-type-name:
619/// '(' objc-type-qualifiers[opt] type-name ')'
620/// '(' objc-type-qualifiers[opt] ')'
621///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000623 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000624
Chris Lattner4a76b292008-10-22 03:52:06 +0000625 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000626 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000627
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000628 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000629 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000630
Chris Lattner4a76b292008-10-22 03:52:06 +0000631 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000632 if (isTypeSpecifierQualifier()) {
633 TypeResult TypeSpec = ParseTypeName();
634 if (!TypeSpec.isInvalid())
635 Ty = TypeSpec.get();
636 }
Chris Lattnere8904e92008-08-23 01:48:03 +0000637
Steve Naroffd7333c22008-10-21 14:15:04 +0000638 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000639 ConsumeParen();
640 else if (Tok.getLocation() == TypeStartLoc) {
641 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000642 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000643 SkipUntil(tok::r_paren);
644 } else {
645 // Otherwise, we found *something*, but didn't get a ')' in the right
646 // place. Emit an error then return what we have as the type.
647 MatchRHSPunctuation(tok::r_paren, LParenLoc);
648 }
Steve Narofff28b2642007-09-05 23:30:30 +0000649 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000650}
651
652/// objc-method-decl:
653/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000654/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000655/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000656/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000657///
658/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000659/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000660/// objc-keyword-selector objc-keyword-decl
661///
662/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000663/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
664/// objc-selector ':' objc-keyword-attributes[opt] identifier
665/// ':' objc-type-name objc-keyword-attributes[opt] identifier
666/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000667///
Steve Naroff4985ace2007-08-22 18:35:33 +0000668/// objc-parmlist:
669/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000670///
Steve Naroff4985ace2007-08-22 18:35:33 +0000671/// objc-parms:
672/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000673///
Steve Naroff4985ace2007-08-22 18:35:33 +0000674/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000675/// , ...
676///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000677/// objc-keyword-attributes: [OBJC2]
678/// __attribute__((unused))
679///
Steve Naroffbef11852007-10-26 20:53:56 +0000680Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000681 tok::TokenKind mType,
682 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000683 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000684{
Chris Lattnere8904e92008-08-23 01:48:03 +0000685 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000686 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000688 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000689 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000690
Steve Naroffbef11852007-10-26 20:53:56 +0000691 SourceLocation selLoc;
692 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000693
Steve Naroff84c43102009-02-11 20:43:13 +0000694 // An unnamed colon is valid.
695 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000696 Diag(Tok, diag::err_expected_selector_for_method)
697 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000698 // Skip until we get a ; or {}.
699 SkipUntil(tok::r_brace);
700 return 0;
701 }
702
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000703 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000704 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000705 // If attributes exist after the method, parse them.
706 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000707 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000708 MethodAttrs = ParseAttributes();
709
710 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000711 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000712 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000713 0, 0, 0, CargNames,
714 MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000715 }
Steve Narofff28b2642007-09-05 23:30:30 +0000716
Steve Naroff68d331a2007-09-27 14:38:14 +0000717 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
718 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000719 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000720 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000721
722 Action::TypeTy *TypeInfo;
723 while (1) {
724 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000725
Chris Lattnerff384912007-10-07 02:00:24 +0000726 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000727 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000728 Diag(Tok, diag::err_expected_colon);
729 break;
730 }
731 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000732 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000733 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000734 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000735 else
736 TypeInfo = 0;
737 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000738 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000739
Chris Lattnerff384912007-10-07 02:00:24 +0000740 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000741 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000742 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000743
Chris Lattnerdf195262007-10-09 17:51:17 +0000744 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000745 Diag(Tok, diag::err_expected_ident); // missing argument name.
746 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000747 }
Chris Lattnerff384912007-10-07 02:00:24 +0000748 ArgNames.push_back(Tok.getIdentifierInfo());
749 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000750
Chris Lattnerff384912007-10-07 02:00:24 +0000751 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000752 SourceLocation Loc;
753 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000754 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000755 break;
756 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000757 }
Chris Lattnerff384912007-10-07 02:00:24 +0000758
Steve Naroff335eafa2007-11-15 12:35:21 +0000759 bool isVariadic = false;
760
Chris Lattnerff384912007-10-07 02:00:24 +0000761 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000762 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000763 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000764 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000765 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000766 ConsumeToken();
767 break;
768 }
Chris Lattnerff384912007-10-07 02:00:24 +0000769 DeclSpec DS;
770 ParseDeclarationSpecifiers(DS);
771 // Parse the declarator.
772 Declarator ParmDecl(DS, Declarator::PrototypeContext);
773 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000774 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000775 }
776
777 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000778 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000779 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000780 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000781 MethodAttrs = ParseAttributes();
782
783 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
784 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000785 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000786 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000787 &ArgTypeQuals[0], &KeyTypes[0],
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000788 &ArgNames[0], CargNames,
789 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000790 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000791}
792
Steve Naroffdac269b2007-08-20 21:31:48 +0000793/// objc-protocol-refs:
794/// '<' identifier-list '>'
795///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000796bool Parser::
Chris Lattnere13b9592008-07-26 04:03:38 +0000797ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
798 bool WarnOnDeclarations, SourceLocation &EndLoc) {
799 assert(Tok.is(tok::less) && "expected <");
800
801 ConsumeToken(); // the "<"
802
803 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
804
805 while (1) {
806 if (Tok.isNot(tok::identifier)) {
807 Diag(Tok, diag::err_expected_ident);
808 SkipUntil(tok::greater);
809 return true;
810 }
811 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
812 Tok.getLocation()));
813 ConsumeToken();
814
815 if (Tok.isNot(tok::comma))
816 break;
817 ConsumeToken();
818 }
819
820 // Consume the '>'.
821 if (Tok.isNot(tok::greater)) {
822 Diag(Tok, diag::err_expected_greater);
823 return true;
824 }
825
826 EndLoc = ConsumeAnyToken();
827
828 // Convert the list of protocols identifiers into a list of protocol decls.
829 Actions.FindProtocolDeclaration(WarnOnDeclarations,
830 &ProtocolIdents[0], ProtocolIdents.size(),
831 Protocols);
832 return false;
833}
834
Steve Naroffdac269b2007-08-20 21:31:48 +0000835/// objc-class-instance-variables:
836/// '{' objc-instance-variable-decl-list[opt] '}'
837///
838/// objc-instance-variable-decl-list:
839/// objc-visibility-spec
840/// objc-instance-variable-decl ';'
841/// ';'
842/// objc-instance-variable-decl-list objc-visibility-spec
843/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
844/// objc-instance-variable-decl-list ';'
845///
846/// objc-visibility-spec:
847/// @private
848/// @protected
849/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000850/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000851///
852/// objc-instance-variable-decl:
853/// struct-declaration
854///
Steve Naroff60fccee2007-10-29 21:38:07 +0000855void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
856 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000857 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000858 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000859 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
860
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000861 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000862
Steve Naroffddbff782007-08-21 21:17:12 +0000863 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000864
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000865 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000866 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000867 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000868 // Each iteration of this loop reads one objc-instance-variable-decl.
869
870 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000871 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000872 Diag(Tok, diag::ext_extra_struct_semi);
873 ConsumeToken();
874 continue;
875 }
Chris Lattnere1359422008-04-10 06:46:29 +0000876
Steve Naroffddbff782007-08-21 21:17:12 +0000877 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000878 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000879 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000880 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000881 case tok::objc_private:
882 case tok::objc_public:
883 case tok::objc_protected:
884 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000885 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000886 ConsumeToken();
887 continue;
888 default:
889 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000890 continue;
891 }
892 }
Chris Lattnere1359422008-04-10 06:46:29 +0000893
894 // Parse all the comma separated declarators.
895 DeclSpec DS;
896 FieldDeclarators.clear();
897 ParseStructDeclaration(DS, FieldDeclarators);
898
899 // Convert them all to fields.
900 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
901 FieldDeclarator &FD = FieldDeclarators[i];
902 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000903 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000904 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000905 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000906 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000907 }
Steve Naroff3536b442007-09-06 21:24:23 +0000908
Chris Lattnerdf195262007-10-09 17:51:17 +0000909 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000910 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000911 } else {
912 Diag(Tok, diag::err_expected_semi_decl_list);
913 // Skip to end of block or statement
914 SkipUntil(tok::r_brace, true, true);
915 }
916 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000917 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000918 // Call ActOnFields() even if we don't have any decls. This is useful
919 // for code rewriting tools that need to be aware of the empty list.
920 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
921 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000922 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000923 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000924}
Steve Naroffdac269b2007-08-20 21:31:48 +0000925
926/// objc-protocol-declaration:
927/// objc-protocol-definition
928/// objc-protocol-forward-reference
929///
930/// objc-protocol-definition:
931/// @protocol identifier
932/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000933/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000934/// @end
935///
936/// objc-protocol-forward-reference:
937/// @protocol identifier-list ';'
938///
939/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000940/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000941/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000942Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
943 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000944 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000945 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
946 ConsumeToken(); // the "protocol" identifier
947
Chris Lattnerdf195262007-10-09 17:51:17 +0000948 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000949 Diag(Tok, diag::err_expected_ident); // missing protocol name.
950 return 0;
951 }
952 // Save the protocol name, then consume it.
953 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
954 SourceLocation nameLoc = ConsumeToken();
955
Chris Lattnerdf195262007-10-09 17:51:17 +0000956 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000957 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000958 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000959 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
960 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000961 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000962
Chris Lattnerdf195262007-10-09 17:51:17 +0000963 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000964 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
965 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
966
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000967 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000968 while (1) {
969 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000970 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000971 Diag(Tok, diag::err_expected_ident);
972 SkipUntil(tok::semi);
973 return 0;
974 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000975 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
976 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000977 ConsumeToken(); // the identifier
978
Chris Lattnerdf195262007-10-09 17:51:17 +0000979 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000980 break;
981 }
982 // Consume the ';'.
983 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
984 return 0;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000985
Steve Naroffe440eb82007-10-10 17:32:04 +0000986 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000987 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000988 ProtocolRefs.size(),
989 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000990 }
991
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000992 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000993 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000994
Chris Lattnere13b9592008-07-26 04:03:38 +0000995 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000996 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000997 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner7caeabd2008-07-21 22:17:28 +0000998 return 0;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000999
Chris Lattnere13b9592008-07-26 04:03:38 +00001000 DeclTy *ProtoType =
1001 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1002 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001003 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001004 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001005 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001006}
Steve Naroffdac269b2007-08-20 21:31:48 +00001007
1008/// objc-implementation:
1009/// objc-class-implementation-prologue
1010/// objc-category-implementation-prologue
1011///
1012/// objc-class-implementation-prologue:
1013/// @implementation identifier objc-superclass[opt]
1014/// objc-class-instance-variables[opt]
1015///
1016/// objc-category-implementation-prologue:
1017/// @implementation identifier ( identifier )
1018
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001019Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1020 SourceLocation atLoc) {
1021 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1022 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1023 ConsumeToken(); // the "implementation" identifier
1024
Chris Lattnerdf195262007-10-09 17:51:17 +00001025 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001026 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1027 return 0;
1028 }
1029 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001030 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001031 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1032
Chris Lattnerdf195262007-10-09 17:51:17 +00001033 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001034 // we have a category implementation.
1035 SourceLocation lparenLoc = ConsumeParen();
1036 SourceLocation categoryLoc, rparenLoc;
1037 IdentifierInfo *categoryId = 0;
1038
Chris Lattnerdf195262007-10-09 17:51:17 +00001039 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001040 categoryId = Tok.getIdentifierInfo();
1041 categoryLoc = ConsumeToken();
1042 } else {
1043 Diag(Tok, diag::err_expected_ident); // missing category name.
1044 return 0;
1045 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001046 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001047 Diag(Tok, diag::err_expected_rparen);
1048 SkipUntil(tok::r_paren, false); // don't stop at ';'
1049 return 0;
1050 }
1051 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +00001052 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001053 atLoc, nameId, nameLoc, categoryId,
1054 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001055 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001056 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001057 }
1058 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001059 SourceLocation superClassLoc;
1060 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001061 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001062 // We have a super class
1063 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001064 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001065 Diag(Tok, diag::err_expected_ident); // missing super class name.
1066 return 0;
1067 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001068 superClassId = Tok.getIdentifierInfo();
1069 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001070 }
Steve Naroffe440eb82007-10-10 17:32:04 +00001071 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001072 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001073 superClassId, superClassLoc);
1074
Steve Naroff60fccee2007-10-29 21:38:07 +00001075 if (Tok.is(tok::l_brace)) // we have ivars
1076 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001077 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001078
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001079 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001080}
Steve Naroff60fccee2007-10-29 21:38:07 +00001081
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001082Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1083 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1084 "ParseObjCAtEndDeclaration(): Expected @end");
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001085 DeclTy *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001086 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001087 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001088 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001089 ObjCImpDecl = 0;
1090 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001091 else
1092 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001093 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001094}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001095
1096/// compatibility-alias-decl:
1097/// @compatibility_alias alias-name class-name ';'
1098///
1099Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1100 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1101 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1102 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001103 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001104 Diag(Tok, diag::err_expected_ident);
1105 return 0;
1106 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001107 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1108 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001109 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001110 Diag(Tok, diag::err_expected_ident);
1111 return 0;
1112 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001113 IdentifierInfo *classId = Tok.getIdentifierInfo();
1114 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1115 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001116 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001117 return 0;
1118 }
1119 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1120 aliasId, aliasLoc,
1121 classId, classLoc);
1122 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001123}
1124
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001125/// property-synthesis:
1126/// @synthesize property-ivar-list ';'
1127///
1128/// property-ivar-list:
1129/// property-ivar
1130/// property-ivar-list ',' property-ivar
1131///
1132/// property-ivar:
1133/// identifier
1134/// identifier '=' identifier
1135///
1136Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1137 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1138 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001139 SourceLocation loc = ConsumeToken(); // consume synthesize
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 return 0;
1143 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001144 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001145 IdentifierInfo *propertyIvar = 0;
1146 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1147 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001148 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001149 // property '=' ivar-name
1150 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001152 Diag(Tok, diag::err_expected_ident);
1153 break;
1154 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001155 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001156 ConsumeToken(); // consume ivar-name
1157 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001158 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1159 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001160 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001161 break;
1162 ConsumeToken(); // consume ','
1163 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001164 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001165 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001166 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001167}
1168
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001169/// property-dynamic:
1170/// @dynamic property-list
1171///
1172/// property-list:
1173/// identifier
1174/// property-list ',' identifier
1175///
1176Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1177 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1178 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1179 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001180 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001181 Diag(Tok, diag::err_expected_ident);
1182 return 0;
1183 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001184 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001185 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1186 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1187 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1188 propertyId, 0);
1189
Chris Lattnerdf195262007-10-09 17:51:17 +00001190 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001191 break;
1192 ConsumeToken(); // consume ','
1193 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001195 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001196 return 0;
1197}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001198
1199/// objc-throw-statement:
1200/// throw expression[opt];
1201///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001202Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001203 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001204 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001206 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001207 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001208 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001209 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001210 }
1211 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001212 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001213 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001214}
1215
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001216/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001217/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001218///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001219Parser::OwningStmtResult
1220Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001221 ConsumeToken(); // consume synchronized
1222 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001223 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001224 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001225 }
1226 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001227 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001228 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001229 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001230 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001231 }
1232 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001233 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001234 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001235 }
1236 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001237 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001238 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001239 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001240 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001241 // Enter a scope to hold everything within the compound stmt. Compound
1242 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001243 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001244
Sebastian Redl61364dd2008-12-11 19:30:53 +00001245 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001246
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001247 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001248 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001249 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001250 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001251}
1252
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001253/// objc-try-catch-statement:
1254/// @try compound-statement objc-catch-list[opt]
1255/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1256///
1257/// objc-catch-list:
1258/// @catch ( parameter-declaration ) compound-statement
1259/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1260/// catch-parameter-declaration:
1261/// parameter-declaration
1262/// '...' [OBJC2]
1263///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001264Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001265 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001266
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001267 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001268 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001269 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001270 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001271 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001272 OwningStmtResult CatchStmts(Actions);
1273 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001274 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001275 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001276 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001277 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001278 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001279
Chris Lattnerdf195262007-10-09 17:51:17 +00001280 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001281 // At this point, we need to lookahead to determine if this @ is the start
1282 // of an @catch or @finally. We don't want to consume the @ token if this
1283 // is an @try or @encode or something else.
1284 Token AfterAt = GetLookAheadToken(1);
1285 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1286 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1287 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001288
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001289 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001290 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00001291 DeclTy *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001292 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001293 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001294 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001295 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001296 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001297 DeclSpec DS;
1298 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001299 // For some odd reason, the name of the exception variable is
Steve Naroff7ba138a2009-03-03 19:52:17 +00001300 // optional. As a result, we need to use "PrototypeContext", because
1301 // we must accept either 'declarator' or 'abstract-declarator' here.
1302 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1303 ParseDeclarator(ParmDecl);
1304
1305 // Inform the actions module about the parameter declarator, so it
1306 // gets added to the current scope.
1307 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001308 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001309 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001310 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001311
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001312 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001313 if (Tok.is(tok::l_brace))
1314 CatchBody = ParseCompoundStatementBody();
1315 else
1316 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001317 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001318 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001319 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001320 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001321 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001322 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001323 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1324 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001325 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001326 }
1327 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001328 } else {
1329 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001330 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001331 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001332
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001333 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001334 if (Tok.is(tok::l_brace))
1335 FinallyBody = ParseCompoundStatementBody();
1336 else
1337 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001338 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001339 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001340 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001341 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001342 catch_or_finally_seen = true;
1343 break;
1344 }
1345 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001346 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001347 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001348 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001349 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001350 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1351 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001352}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001353
Steve Naroff3536b442007-09-06 21:24:23 +00001354/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001355///
Steve Naroff71c0a952007-11-13 23:01:27 +00001356Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001357 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner73e80f62009-03-05 02:03:49 +00001358
Chris Lattner49f28ca2009-03-05 08:00:35 +00001359 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1360 PP.getSourceManager(),
1361 "parsing Objective-C method");
Chris Lattner73e80f62009-03-05 02:03:49 +00001362
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001363 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001364 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001365 ConsumeToken();
1366
Steve Naroff409be832007-11-11 19:54:21 +00001367 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001368 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001369 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001370
1371 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1372 SkipUntil(tok::l_brace, true, true);
1373
1374 // If we didn't find the '{', bail out.
1375 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001376 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001377 }
Steve Naroff409be832007-11-11 19:54:21 +00001378 SourceLocation BraceLoc = Tok.getLocation();
1379
1380 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001381 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001382
1383 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001384 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001385 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001386
1387 OwningStmtResult FnBody(ParseCompoundStatementBody());
1388
Steve Naroff409be832007-11-11 19:54:21 +00001389 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001390 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001391 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1392 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001393
Steve Naroff32ce8372009-03-02 22:00:56 +00001394 // TODO: Pass argument information.
1395 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1396
Steve Naroff409be832007-11-11 19:54:21 +00001397 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001398 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001399
Steve Naroff71c0a952007-11-13 23:01:27 +00001400 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001401}
Anders Carlsson55085182007-08-21 17:43:55 +00001402
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001403Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001404 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001405 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001406 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1407 return ParseObjCThrowStmt(AtLoc);
1408 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1409 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001410 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001411 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001412 // If the expression is invalid, skip ahead to the next semicolon. Not
1413 // doing this opens us up to the possibility of infinite loops if
1414 // ParseExpression does not consume any tokens.
1415 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001416 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001417 }
1418 // Otherwise, eat the semicolon.
1419 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001420 return Actions.ActOnExprStmt(move(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001421}
1422
Sebastian Redl1d922962008-12-13 15:32:12 +00001423Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001424 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001425 case tok::string_literal: // primary-expression: string-literal
1426 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001427 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001428 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001429 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001430 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001431
Chris Lattner4fef81d2008-08-05 06:19:09 +00001432 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1433 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001434 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001435 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001436 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001437 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001438 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001439 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001440 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001441 }
Anders Carlsson55085182007-08-21 17:43:55 +00001442 }
Anders Carlsson55085182007-08-21 17:43:55 +00001443}
1444
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001445/// objc-message-expr:
1446/// '[' objc-receiver objc-message-args ']'
1447///
1448/// objc-receiver:
1449/// expression
1450/// class-name
1451/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001452Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001453 assert(Tok.is(tok::l_square) && "'[' expected");
1454 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1455
1456 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001457 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001458 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +00001459 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001460 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1461 ExprArg(Actions));
Chris Lattner699b6612008-01-25 18:59:06 +00001462 }
1463
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001464 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001465 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001466 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001467 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001468 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001469
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001470 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001471 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001472}
Sebastian Redl1d922962008-12-13 15:32:12 +00001473
Chris Lattner699b6612008-01-25 18:59:06 +00001474/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1475/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001476///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001477/// objc-message-args:
1478/// objc-selector
1479/// objc-keywordarg-list
1480///
1481/// objc-keywordarg-list:
1482/// objc-keywordarg
1483/// objc-keywordarg-list objc-keywordarg
1484///
1485/// objc-keywordarg:
1486/// selector-name[opt] ':' objc-keywordexpr
1487///
1488/// objc-keywordexpr:
1489/// nonempty-expr-list
1490///
1491/// nonempty-expr-list:
1492/// assignment-expression
1493/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001494///
1495Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001496Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001497 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001498 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001499 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001500 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001501 SourceLocation Loc;
1502 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001503
Anders Carlssonff975cf2009-02-14 18:21:46 +00001504 SourceLocation SelectorLoc = Loc;
1505
Steve Naroff68d331a2007-09-27 14:38:14 +00001506 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001507 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001508
Chris Lattnerdf195262007-10-09 17:51:17 +00001509 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001510 while (1) {
1511 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001512 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001513
Chris Lattnerdf195262007-10-09 17:51:17 +00001514 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001515 Diag(Tok, diag::err_expected_colon);
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 ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001521 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001522
Steve Naroff68d331a2007-09-27 14:38:14 +00001523 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001524 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001525 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001526 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001527 // We must manually skip to a ']', otherwise the expression skipper will
1528 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1529 // the enclosing expression.
1530 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001531 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001532 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001533
Steve Naroff37387c92007-09-17 20:25:27 +00001534 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001535 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001536
Steve Naroff37387c92007-09-17 20:25:27 +00001537 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001538 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001539 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001540 break;
1541 // We have a selector or a colon, continue parsing.
1542 }
1543 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001544 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001545 ConsumeToken(); // Eat the ','.
1546 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001547 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001548 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001549 // We must manually skip to a ']', otherwise the expression skipper will
1550 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1551 // the enclosing expression.
1552 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001553 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001554 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001555
Steve Naroff49f109c2007-11-15 13:05:42 +00001556 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001557 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001558 }
1559 } else if (!selIdent) {
1560 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001561
Chris Lattner4fef81d2008-08-05 06:19:09 +00001562 // We must manually skip to a ']', otherwise the expression skipper will
1563 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1564 // the enclosing expression.
1565 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001566 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001567 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001568
Chris Lattnerdf195262007-10-09 17:51:17 +00001569 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001570 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001571 // We must manually skip to a ']', otherwise the expression skipper will
1572 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1573 // the enclosing expression.
1574 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001575 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001576 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001577
Chris Lattner699b6612008-01-25 18:59:06 +00001578 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001579
Steve Naroff29238a02007-10-05 18:42:47 +00001580 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001581 if (nKeys == 0)
1582 KeyIdents.push_back(selIdent);
1583 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001584
Chris Lattnerff384912007-10-07 02:00:24 +00001585 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001586 if (ReceiverName)
1587 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001588 LBracLoc, NameLoc, SelectorLoc,
1589 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001590 KeyExprs.take(), KeyExprs.size()));
1591 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001592 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001593 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001594}
1595
Sebastian Redl1d922962008-12-13 15:32:12 +00001596Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001597 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001598 if (Res.isInvalid()) return move(Res);
1599
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001600 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1601 // expressions. At this point, we know that the only valid thing that starts
1602 // with '@' is an @"".
1603 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001604 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001605 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001606 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001607
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001608 while (Tok.is(tok::at)) {
1609 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001610
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001611 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001612 if (!isTokenStringLiteral())
1613 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001614
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001615 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001616 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001617 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001618
Sebastian Redleffa8d12008-12-10 00:02:53 +00001619 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001620 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001621
1622 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1623 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001624}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001625
1626/// objc-encode-expression:
1627/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001628Parser::OwningExprResult
1629Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001630 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001631
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001632 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001633
Chris Lattner4fef81d2008-08-05 06:19:09 +00001634 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001635 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1636
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001637 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001638
Douglas Gregor809070a2009-02-18 17:45:20 +00001639 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001640
Anders Carlsson4988ae32007-08-23 15:31:37 +00001641 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001642
Douglas Gregor809070a2009-02-18 17:45:20 +00001643 if (Ty.isInvalid())
1644 return ExprError();
1645
1646 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1647 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001648}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001649
1650/// objc-protocol-expression
1651/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001652Parser::OwningExprResult
1653Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001654 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001655
Chris Lattner4fef81d2008-08-05 06:19:09 +00001656 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001657 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1658
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001659 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001660
Chris Lattner4fef81d2008-08-05 06:19:09 +00001661 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001662 return ExprError(Diag(Tok, diag::err_expected_ident));
1663
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001664 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001665 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001666
Anders Carlsson4988ae32007-08-23 15:31:37 +00001667 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001668
Sebastian Redl1d922962008-12-13 15:32:12 +00001669 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1670 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001671}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001672
1673/// objc-selector-expression
1674/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001675Parser::OwningExprResult
1676Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001677 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001678
Chris Lattner4fef81d2008-08-05 06:19:09 +00001679 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001680 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1681
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001682 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001683 SourceLocation LParenLoc = ConsumeParen();
1684 SourceLocation sLoc;
1685 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001686 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1687 return ExprError(Diag(Tok, diag::err_expected_ident));
1688
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001689 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001690 unsigned nColons = 0;
1691 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001692 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001693 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001694 return ExprError(Diag(Tok, diag::err_expected_colon));
1695
Chris Lattnercb53b362007-12-27 19:57:00 +00001696 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001697 ConsumeToken(); // Eat the ':'.
1698 if (Tok.is(tok::r_paren))
1699 break;
1700 // Check for another keyword selector.
1701 SourceLocation Loc;
1702 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001703 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001704 if (!SelIdent && Tok.isNot(tok::colon))
1705 break;
1706 }
Steve Naroff887407e2007-12-05 22:21:29 +00001707 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001708 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001709 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001710 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1711 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001712 }