blob: 759de7a0648dd28253bc5b73b9746ff98cba216c [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/Diagnostic.h"
19#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.
Steve Naroffd16245b2007-09-17 15:07:43 +0000242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000243 continue;
244 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000245
Chris Lattnere82a10f2008-10-20 05:46:22 +0000246 // Ignore excess semicolons.
247 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000248 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000249 continue;
250 }
251
Chris Lattnerbc662af2008-10-20 06:10:06 +0000252 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000253 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000254 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000255
256 // If we don't have an @ directive, parse it as a function definition.
257 if (Tok.isNot(tok::at)) {
Steve Naroff4985ace2007-08-22 18:35:33 +0000258 // FIXME: as the name implies, this rule allows function definitions.
259 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000260 ParseDeclarationOrFunctionDefinition();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000261 continue;
262 }
263
264 // Otherwise, we have an @ directive, eat the @.
265 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000266 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000267
Chris Lattnera2449b22008-10-20 05:57:40 +0000268 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000269 AtEndLoc = AtLoc;
270 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000271 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000272
Chris Lattnerbc662af2008-10-20 06:10:06 +0000273 // Eat the identifier.
274 ConsumeToken();
275
Chris Lattnera2449b22008-10-20 05:57:40 +0000276 switch (DirectiveKind) {
277 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000278 // FIXME: If someone forgets an @end on a protocol, this loop will
279 // continue to eat up tons of stuff and spew lots of nonsense errors. It
280 // would probably be better to bail out if we saw an @class or @interface
281 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000282 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000283 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000284 SkipUntil(tok::r_brace, tok::at);
285 break;
286
287 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000288 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000289 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000290 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000291 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000292 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000293 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000294 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000295 break;
296
297 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000298 if (!getLang().ObjC2)
299 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
300
Chris Lattnere82a10f2008-10-20 05:46:22 +0000301 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000302 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000303 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000304 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000305
Chris Lattnere82a10f2008-10-20 05:46:22 +0000306 // Parse all the comma separated declarators.
307 DeclSpec DS;
308 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
309 ParseStructDeclaration(DS, FieldDeclarators);
310
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000311 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
312 tok::at);
313
Chris Lattnere82a10f2008-10-20 05:46:22 +0000314 // Convert them all to property declarations.
315 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
316 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000317 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000318 Diag(AtLoc, diag::err_objc_property_requires_field_name)
319 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000320 continue;
321 }
322
Chris Lattnere82a10f2008-10-20 05:46:22 +0000323 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000324 IdentifierInfo *SelName =
325 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
326
Chris Lattnere82a10f2008-10-20 05:46:22 +0000327 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000328 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 IdentifierInfo *SetterName = OCDS.getSetterName();
330 if (!SetterName)
331 SetterName = constructSetterName(PP.getIdentifierTable(),
332 FD.D.getIdentifier());
333 Selector SetterSel =
334 PP.getSelectorTable().getUnarySelector(SetterName);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000335 bool isOverridingProperty = false;
Chris Lattnerda3253d2008-10-20 06:33:53 +0000336 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
337 GetterSel, SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000338 interfaceDecl,
339 &isOverridingProperty,
Chris Lattnerda3253d2008-10-20 06:33:53 +0000340 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000341 if (!isOverridingProperty)
342 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000343 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000344 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000345 }
Steve Naroff294494e2007-08-22 16:35:03 +0000346 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000347
348 // We break out of the big loop in two cases: when we see @end or when we see
349 // EOF. In the former case, eat the @end. In the later case, emit an error.
350 if (Tok.isObjCAtKeyword(tok::objc_end))
351 ConsumeToken(); // the "end" identifier
352 else
353 Diag(Tok, diag::err_objc_missing_end);
354
Chris Lattnera2449b22008-10-20 05:57:40 +0000355 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000356 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000357 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
358 allMethods.empty() ? 0 : &allMethods[0],
359 allMethods.size(),
360 allProperties.empty() ? 0 : &allProperties[0],
361 allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000362}
363
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000364/// Parse property attribute declarations.
365///
366/// property-attr-decl: '(' property-attrlist ')'
367/// property-attrlist:
368/// property-attribute
369/// property-attrlist ',' property-attribute
370/// property-attribute:
371/// getter '=' identifier
372/// setter '=' identifier ':'
373/// readonly
374/// readwrite
375/// assign
376/// retain
377/// copy
378/// nonatomic
379///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000380void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000381 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000382 SourceLocation LHSLoc = ConsumeParen(); // consume '('
383
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000384 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000385 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000386
387 // If this is not an identifier at all, bail out early.
388 if (II == 0) {
389 MatchRHSPunctuation(tok::r_paren, LHSLoc);
390 return;
391 }
392
Chris Lattner156b0612008-10-20 07:37:22 +0000393 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
394
Chris Lattner92e62b02008-11-20 04:42:34 +0000395 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000396 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000397 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000398 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000399 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000400 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000401 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000402 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000403 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000404 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000405 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000406 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000407 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000408 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000409 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
410 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000411 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000412
Chris Lattner8ca329c2008-10-20 07:24:39 +0000413 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000414 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000415 SkipUntil(tok::r_paren);
416 return;
417 }
418
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000419 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000420 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
421 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000422 ConsumeToken(); // consume method name
423
424 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
425 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000426 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000427 } else {
428 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
429 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000430 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000431 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000432 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000433 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000434 SkipUntil(tok::r_paren);
435 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000436 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000437
Chris Lattner156b0612008-10-20 07:37:22 +0000438 if (Tok.isNot(tok::comma))
439 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000440
Chris Lattner156b0612008-10-20 07:37:22 +0000441 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000442 }
Chris Lattner156b0612008-10-20 07:37:22 +0000443
444 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000445}
446
Steve Naroff3536b442007-09-06 21:24:23 +0000447/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000448/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000449/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000450///
451/// objc-instance-method: '-'
452/// objc-class-method: '+'
453///
Steve Naroff4985ace2007-08-22 18:35:33 +0000454/// objc-method-attributes: [OBJC2]
455/// __attribute__((deprecated))
456///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000457Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000458 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000459 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000460
461 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000462 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000463
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000464 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000465 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000466 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000467 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000468}
469
470/// objc-selector:
471/// identifier
472/// one of
473/// enum struct union if else while do for switch case default
474/// break continue return goto asm sizeof typeof __alignof
475/// unsigned long const short volatile signed restrict _Complex
476/// in out inout bycopy byref oneway int char float double void _Bool
477///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000478IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000479 switch (Tok.getKind()) {
480 default:
481 return 0;
482 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000483 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000484 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000485 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000486 case tok::kw_break:
487 case tok::kw_case:
488 case tok::kw_catch:
489 case tok::kw_char:
490 case tok::kw_class:
491 case tok::kw_const:
492 case tok::kw_const_cast:
493 case tok::kw_continue:
494 case tok::kw_default:
495 case tok::kw_delete:
496 case tok::kw_do:
497 case tok::kw_double:
498 case tok::kw_dynamic_cast:
499 case tok::kw_else:
500 case tok::kw_enum:
501 case tok::kw_explicit:
502 case tok::kw_export:
503 case tok::kw_extern:
504 case tok::kw_false:
505 case tok::kw_float:
506 case tok::kw_for:
507 case tok::kw_friend:
508 case tok::kw_goto:
509 case tok::kw_if:
510 case tok::kw_inline:
511 case tok::kw_int:
512 case tok::kw_long:
513 case tok::kw_mutable:
514 case tok::kw_namespace:
515 case tok::kw_new:
516 case tok::kw_operator:
517 case tok::kw_private:
518 case tok::kw_protected:
519 case tok::kw_public:
520 case tok::kw_register:
521 case tok::kw_reinterpret_cast:
522 case tok::kw_restrict:
523 case tok::kw_return:
524 case tok::kw_short:
525 case tok::kw_signed:
526 case tok::kw_sizeof:
527 case tok::kw_static:
528 case tok::kw_static_cast:
529 case tok::kw_struct:
530 case tok::kw_switch:
531 case tok::kw_template:
532 case tok::kw_this:
533 case tok::kw_throw:
534 case tok::kw_true:
535 case tok::kw_try:
536 case tok::kw_typedef:
537 case tok::kw_typeid:
538 case tok::kw_typename:
539 case tok::kw_typeof:
540 case tok::kw_union:
541 case tok::kw_unsigned:
542 case tok::kw_using:
543 case tok::kw_virtual:
544 case tok::kw_void:
545 case tok::kw_volatile:
546 case tok::kw_wchar_t:
547 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000548 case tok::kw__Bool:
549 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000550 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000551 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000552 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000553 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000554 }
Steve Naroff294494e2007-08-22 16:35:03 +0000555}
556
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000557/// objc-for-collection-in: 'in'
558///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000559bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000560 // FIXME: May have to do additional look-ahead to only allow for
561 // valid tokens following an 'in'; such as an identifier, unary operators,
562 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000563 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000564 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000565}
566
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000567/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000568/// qualifier list and builds their bitmask representation in the input
569/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000570///
571/// objc-type-qualifiers:
572/// objc-type-qualifier
573/// objc-type-qualifiers objc-type-qualifier
574///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000576 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000577 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000578 return;
579
580 const IdentifierInfo *II = Tok.getIdentifierInfo();
581 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000583 continue;
584
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000586 switch (i) {
587 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000588 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
589 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
590 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
591 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
592 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
593 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000594 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000596 ConsumeToken();
597 II = 0;
598 break;
599 }
600
601 // If this wasn't a recognized qualifier, bail out.
602 if (II) return;
603 }
604}
605
606/// objc-type-name:
607/// '(' objc-type-qualifiers[opt] type-name ')'
608/// '(' objc-type-qualifiers[opt] ')'
609///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000610Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000611 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000612
Chris Lattner4a76b292008-10-22 03:52:06 +0000613 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000614 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000615
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000616 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000618
Chris Lattner4a76b292008-10-22 03:52:06 +0000619 TypeTy *Ty = 0;
620 if (isTypeSpecifierQualifier())
Steve Narofff28b2642007-09-05 23:30:30 +0000621 Ty = ParseTypeName();
Chris Lattnere8904e92008-08-23 01:48:03 +0000622
Steve Naroffd7333c22008-10-21 14:15:04 +0000623 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000624 ConsumeParen();
625 else if (Tok.getLocation() == TypeStartLoc) {
626 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000627 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000628 SkipUntil(tok::r_paren);
629 } else {
630 // Otherwise, we found *something*, but didn't get a ')' in the right
631 // place. Emit an error then return what we have as the type.
632 MatchRHSPunctuation(tok::r_paren, LParenLoc);
633 }
Steve Narofff28b2642007-09-05 23:30:30 +0000634 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000635}
636
637/// objc-method-decl:
638/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000639/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000640/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000641/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000642///
643/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000644/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000645/// objc-keyword-selector objc-keyword-decl
646///
647/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000648/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
649/// objc-selector ':' objc-keyword-attributes[opt] identifier
650/// ':' objc-type-name objc-keyword-attributes[opt] identifier
651/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000652///
Steve Naroff4985ace2007-08-22 18:35:33 +0000653/// objc-parmlist:
654/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000655///
Steve Naroff4985ace2007-08-22 18:35:33 +0000656/// objc-parms:
657/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000658///
Steve Naroff4985ace2007-08-22 18:35:33 +0000659/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000660/// , ...
661///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000662/// objc-keyword-attributes: [OBJC2]
663/// __attribute__((unused))
664///
Steve Naroffbef11852007-10-26 20:53:56 +0000665Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000666 tok::TokenKind mType,
667 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000668 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000669{
Chris Lattnere8904e92008-08-23 01:48:03 +0000670 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000671 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000672 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000673 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000674 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000675
Steve Naroffbef11852007-10-26 20:53:56 +0000676 SourceLocation selLoc;
677 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000678
679 if (!SelIdent) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000680 Diag(Tok, diag::err_expected_selector_for_method)
681 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000682 // Skip until we get a ; or {}.
683 SkipUntil(tok::r_brace);
684 return 0;
685 }
686
Chris Lattnerdf195262007-10-09 17:51:17 +0000687 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000688 // If attributes exist after the method, parse them.
689 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000690 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000691 MethodAttrs = ParseAttributes();
692
693 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000694 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000695 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000696 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000697 }
Steve Narofff28b2642007-09-05 23:30:30 +0000698
Steve Naroff68d331a2007-09-27 14:38:14 +0000699 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
700 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000701 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000702 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000703
704 Action::TypeTy *TypeInfo;
705 while (1) {
706 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000707
Chris Lattnerff384912007-10-07 02:00:24 +0000708 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000709 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000710 Diag(Tok, diag::err_expected_colon);
711 break;
712 }
713 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000714 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000715 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000716 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000717 else
718 TypeInfo = 0;
719 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000720 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000721
Chris Lattnerff384912007-10-07 02:00:24 +0000722 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000723 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000724 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000725
Chris Lattnerdf195262007-10-09 17:51:17 +0000726 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000727 Diag(Tok, diag::err_expected_ident); // missing argument name.
728 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000729 }
Chris Lattnerff384912007-10-07 02:00:24 +0000730 ArgNames.push_back(Tok.getIdentifierInfo());
731 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000732
Chris Lattnerff384912007-10-07 02:00:24 +0000733 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000734 SourceLocation Loc;
735 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000736 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000737 break;
738 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000739 }
Chris Lattnerff384912007-10-07 02:00:24 +0000740
Steve Naroff335eafa2007-11-15 12:35:21 +0000741 bool isVariadic = false;
742
Chris Lattnerff384912007-10-07 02:00:24 +0000743 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000744 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000745 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000746 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000747 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000748 ConsumeToken();
749 break;
750 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000751 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000752 // Parse the c-style argument declaration-specifier.
753 DeclSpec DS;
754 ParseDeclarationSpecifiers(DS);
755 // Parse the declarator.
756 Declarator ParmDecl(DS, Declarator::PrototypeContext);
757 ParseDeclarator(ParmDecl);
758 }
759
760 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000761 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000762 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000763 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000764 MethodAttrs = ParseAttributes();
765
766 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
767 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000768 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000769 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000770 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000771 &ArgNames[0], MethodAttrs,
772 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000773}
774
Steve Naroffdac269b2007-08-20 21:31:48 +0000775/// objc-protocol-refs:
776/// '<' identifier-list '>'
777///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000778bool Parser::
Chris Lattnere13b9592008-07-26 04:03:38 +0000779ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
780 bool WarnOnDeclarations, SourceLocation &EndLoc) {
781 assert(Tok.is(tok::less) && "expected <");
782
783 ConsumeToken(); // the "<"
784
785 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
786
787 while (1) {
788 if (Tok.isNot(tok::identifier)) {
789 Diag(Tok, diag::err_expected_ident);
790 SkipUntil(tok::greater);
791 return true;
792 }
793 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
794 Tok.getLocation()));
795 ConsumeToken();
796
797 if (Tok.isNot(tok::comma))
798 break;
799 ConsumeToken();
800 }
801
802 // Consume the '>'.
803 if (Tok.isNot(tok::greater)) {
804 Diag(Tok, diag::err_expected_greater);
805 return true;
806 }
807
808 EndLoc = ConsumeAnyToken();
809
810 // Convert the list of protocols identifiers into a list of protocol decls.
811 Actions.FindProtocolDeclaration(WarnOnDeclarations,
812 &ProtocolIdents[0], ProtocolIdents.size(),
813 Protocols);
814 return false;
815}
816
Steve Naroffdac269b2007-08-20 21:31:48 +0000817/// objc-class-instance-variables:
818/// '{' objc-instance-variable-decl-list[opt] '}'
819///
820/// objc-instance-variable-decl-list:
821/// objc-visibility-spec
822/// objc-instance-variable-decl ';'
823/// ';'
824/// objc-instance-variable-decl-list objc-visibility-spec
825/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
826/// objc-instance-variable-decl-list ';'
827///
828/// objc-visibility-spec:
829/// @private
830/// @protected
831/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000832/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000833///
834/// objc-instance-variable-decl:
835/// struct-declaration
836///
Steve Naroff60fccee2007-10-29 21:38:07 +0000837void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
838 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000839 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000840 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000841 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
842
Steve Naroffddbff782007-08-21 21:17:12 +0000843 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000844
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000845 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000846 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000847 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000848 // Each iteration of this loop reads one objc-instance-variable-decl.
849
850 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000851 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000852 Diag(Tok, diag::ext_extra_struct_semi);
853 ConsumeToken();
854 continue;
855 }
Chris Lattnere1359422008-04-10 06:46:29 +0000856
Steve Naroffddbff782007-08-21 21:17:12 +0000857 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000858 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000859 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000860 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000861 case tok::objc_private:
862 case tok::objc_public:
863 case tok::objc_protected:
864 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000865 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000866 ConsumeToken();
867 continue;
868 default:
869 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000870 continue;
871 }
872 }
Chris Lattnere1359422008-04-10 06:46:29 +0000873
874 // Parse all the comma separated declarators.
875 DeclSpec DS;
876 FieldDeclarators.clear();
877 ParseStructDeclaration(DS, FieldDeclarators);
878
879 // Convert them all to fields.
880 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
881 FieldDeclarator &FD = FieldDeclarators[i];
882 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000883 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000884 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000885 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000886 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000887 }
Steve Naroff3536b442007-09-06 21:24:23 +0000888
Chris Lattnerdf195262007-10-09 17:51:17 +0000889 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000890 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000891 } else {
892 Diag(Tok, diag::err_expected_semi_decl_list);
893 // Skip to end of block or statement
894 SkipUntil(tok::r_brace, true, true);
895 }
896 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000897 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000898 // Call ActOnFields() even if we don't have any decls. This is useful
899 // for code rewriting tools that need to be aware of the empty list.
900 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
901 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000902 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000903 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000904}
Steve Naroffdac269b2007-08-20 21:31:48 +0000905
906/// objc-protocol-declaration:
907/// objc-protocol-definition
908/// objc-protocol-forward-reference
909///
910/// objc-protocol-definition:
911/// @protocol identifier
912/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000913/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000914/// @end
915///
916/// objc-protocol-forward-reference:
917/// @protocol identifier-list ';'
918///
919/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000920/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000921/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000922Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
923 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000924 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000925 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
926 ConsumeToken(); // the "protocol" identifier
927
Chris Lattnerdf195262007-10-09 17:51:17 +0000928 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000929 Diag(Tok, diag::err_expected_ident); // missing protocol name.
930 return 0;
931 }
932 // Save the protocol name, then consume it.
933 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
934 SourceLocation nameLoc = ConsumeToken();
935
Chris Lattnerdf195262007-10-09 17:51:17 +0000936 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000937 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000938 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000939 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
940 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000941 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000942
Chris Lattnerdf195262007-10-09 17:51:17 +0000943 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000944 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
945 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
946
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000947 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000948 while (1) {
949 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000950 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000951 Diag(Tok, diag::err_expected_ident);
952 SkipUntil(tok::semi);
953 return 0;
954 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000955 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
956 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000957 ConsumeToken(); // the identifier
958
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000960 break;
961 }
962 // Consume the ';'.
963 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
964 return 0;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000965
Steve Naroffe440eb82007-10-10 17:32:04 +0000966 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000967 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000968 ProtocolRefs.size(),
969 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000970 }
971
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000972 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000973 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000974
Chris Lattnere13b9592008-07-26 04:03:38 +0000975 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000976 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000977 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner7caeabd2008-07-21 22:17:28 +0000978 return 0;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000979
Chris Lattnere13b9592008-07-26 04:03:38 +0000980 DeclTy *ProtoType =
981 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
982 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000983 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000984 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000985 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000986}
Steve Naroffdac269b2007-08-20 21:31:48 +0000987
988/// objc-implementation:
989/// objc-class-implementation-prologue
990/// objc-category-implementation-prologue
991///
992/// objc-class-implementation-prologue:
993/// @implementation identifier objc-superclass[opt]
994/// objc-class-instance-variables[opt]
995///
996/// objc-category-implementation-prologue:
997/// @implementation identifier ( identifier )
998
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000999Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1000 SourceLocation atLoc) {
1001 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1002 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1003 ConsumeToken(); // the "implementation" identifier
1004
Chris Lattnerdf195262007-10-09 17:51:17 +00001005 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001006 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1007 return 0;
1008 }
1009 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001010 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001011 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1012
Chris Lattnerdf195262007-10-09 17:51:17 +00001013 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001014 // we have a category implementation.
1015 SourceLocation lparenLoc = ConsumeParen();
1016 SourceLocation categoryLoc, rparenLoc;
1017 IdentifierInfo *categoryId = 0;
1018
Chris Lattnerdf195262007-10-09 17:51:17 +00001019 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001020 categoryId = Tok.getIdentifierInfo();
1021 categoryLoc = ConsumeToken();
1022 } else {
1023 Diag(Tok, diag::err_expected_ident); // missing category name.
1024 return 0;
1025 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001026 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001027 Diag(Tok, diag::err_expected_rparen);
1028 SkipUntil(tok::r_paren, false); // don't stop at ';'
1029 return 0;
1030 }
1031 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +00001032 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001033 atLoc, nameId, nameLoc, categoryId,
1034 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001035 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001036 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001037 }
1038 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001039 SourceLocation superClassLoc;
1040 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001041 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001042 // We have a super class
1043 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001044 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001045 Diag(Tok, diag::err_expected_ident); // missing super class name.
1046 return 0;
1047 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001048 superClassId = Tok.getIdentifierInfo();
1049 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001050 }
Steve Naroffe440eb82007-10-10 17:32:04 +00001051 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001052 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001053 superClassId, superClassLoc);
1054
Steve Naroff60fccee2007-10-29 21:38:07 +00001055 if (Tok.is(tok::l_brace)) // we have ivars
1056 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001057 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001058
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001059 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001060}
Steve Naroff60fccee2007-10-29 21:38:07 +00001061
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001062Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1063 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1064 "ParseObjCAtEndDeclaration(): Expected @end");
1065 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001066 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001067 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001068 else
1069 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001070 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +00001071}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001072
1073/// compatibility-alias-decl:
1074/// @compatibility_alias alias-name class-name ';'
1075///
1076Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1077 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1078 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1079 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001080 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001081 Diag(Tok, diag::err_expected_ident);
1082 return 0;
1083 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001084 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1085 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001086 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001087 Diag(Tok, diag::err_expected_ident);
1088 return 0;
1089 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001090 IdentifierInfo *classId = Tok.getIdentifierInfo();
1091 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1092 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001093 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001094 return 0;
1095 }
1096 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1097 aliasId, aliasLoc,
1098 classId, classLoc);
1099 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100}
1101
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001102/// property-synthesis:
1103/// @synthesize property-ivar-list ';'
1104///
1105/// property-ivar-list:
1106/// property-ivar
1107/// property-ivar-list ',' property-ivar
1108///
1109/// property-ivar:
1110/// identifier
1111/// identifier '=' identifier
1112///
1113Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1114 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1115 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001116 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001117 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001118 Diag(Tok, diag::err_expected_ident);
1119 return 0;
1120 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001121 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001122 IdentifierInfo *propertyIvar = 0;
1123 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1124 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001125 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001126 // property '=' ivar-name
1127 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001128 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001129 Diag(Tok, diag::err_expected_ident);
1130 break;
1131 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001132 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001133 ConsumeToken(); // consume ivar-name
1134 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001135 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1136 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001137 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001138 break;
1139 ConsumeToken(); // consume ','
1140 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001141 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001142 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001143 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001144}
1145
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001146/// property-dynamic:
1147/// @dynamic property-list
1148///
1149/// property-list:
1150/// identifier
1151/// property-list ',' identifier
1152///
1153Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1154 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1155 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1156 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001157 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001158 Diag(Tok, diag::err_expected_ident);
1159 return 0;
1160 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001161 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001162 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1163 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1164 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1165 propertyId, 0);
1166
Chris Lattnerdf195262007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001168 break;
1169 ConsumeToken(); // consume ','
1170 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001171 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001172 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001173 return 0;
1174}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001175
1176/// objc-throw-statement:
1177/// throw expression[opt];
1178///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001179Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001180 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001181 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001183 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001184 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001185 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001186 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001187 }
1188 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001189 ConsumeToken(); // consume ';'
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001190 return Owned(Actions.ActOnObjCAtThrowStmt(atLoc, Res.release()));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001191}
1192
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001193/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001194/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001195///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001196Parser::OwningStmtResult
1197Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001198 ConsumeToken(); // consume synchronized
1199 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001200 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001201 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001202 }
1203 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001204 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001205 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001206 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001207 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001208 }
1209 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001210 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001211 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001212 }
1213 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001214 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001215 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001216 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001217 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001218 // Enter a scope to hold everything within the compound stmt. Compound
1219 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001220 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001221
Sebastian Redl61364dd2008-12-11 19:30:53 +00001222 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001223
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001224 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001225 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001226 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001227 return Owned(Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.release(),
1228 SynchBody.release()));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001229}
1230
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001231/// objc-try-catch-statement:
1232/// @try compound-statement objc-catch-list[opt]
1233/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1234///
1235/// objc-catch-list:
1236/// @catch ( parameter-declaration ) compound-statement
1237/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1238/// catch-parameter-declaration:
1239/// parameter-declaration
1240/// '...' [OBJC2]
1241///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001242Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001243 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001244
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001245 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001246 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001247 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001248 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001249 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001250 OwningStmtResult CatchStmts(Actions);
1251 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001252 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001253 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001254 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001255 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001256 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001257
Chris Lattnerdf195262007-10-09 17:51:17 +00001258 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001259 // At this point, we need to lookahead to determine if this @ is the start
1260 // of an @catch or @finally. We don't want to consume the @ token if this
1261 // is an @try or @encode or something else.
1262 Token AfterAt = GetLookAheadToken(1);
1263 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1264 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1265 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001266
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001267 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001268 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001269 OwningStmtResult FirstPart(Actions);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001270 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001271 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001272 ConsumeParen();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001273 ParseScope CatchScope(this, Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001274 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001275 DeclSpec DS;
1276 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001277 // For some odd reason, the name of the exception variable is
Steve Naroff459a1e22008-06-03 05:36:54 +00001278 // optional. As a result, we need to use PrototypeContext.
1279 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001280 ParseDeclarator(DeclaratorInfo);
Steve Naroff459a1e22008-06-03 05:36:54 +00001281 if (DeclaratorInfo.getIdentifier()) {
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001282 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar914701e2008-08-05 16:28:08 +00001283 DeclaratorInfo, 0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001284 FirstPart =
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001285 Actions.ActOnDeclStmt(aBlockVarDecl,
Steve Naroff459a1e22008-06-03 05:36:54 +00001286 DS.getSourceRange().getBegin(),
1287 DeclaratorInfo.getSourceRange().getEnd());
Steve Naroff459a1e22008-06-03 05:36:54 +00001288 }
Steve Naroff64515f32008-02-05 21:27:35 +00001289 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001290 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001291 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001292
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001293 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001294 if (Tok.is(tok::l_brace))
1295 CatchBody = ParseCompoundStatementBody();
1296 else
1297 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001298 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001299 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001300 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001301 RParenLoc, FirstPart.release(), CatchBody.release(),
1302 CatchStmts.release());
Steve Naroff64515f32008-02-05 21:27:35 +00001303 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001304 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1305 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001306 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001307 }
1308 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001309 } else {
1310 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001311 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001312 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001313
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001314 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001315 if (Tok.is(tok::l_brace))
1316 FinallyBody = ParseCompoundStatementBody();
1317 else
1318 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001319 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001320 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001321 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001322 FinallyBody.release());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001323 catch_or_finally_seen = true;
1324 break;
1325 }
1326 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001327 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001328 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001329 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001330 }
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001331 return Owned(Actions.ActOnObjCAtTryStmt(atLoc, TryBody.release(),
1332 CatchStmts.release(),
1333 FinallyStmt.release()));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001334}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001335
Steve Naroff3536b442007-09-06 21:24:23 +00001336/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001337///
Steve Naroff71c0a952007-11-13 23:01:27 +00001338Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001340 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001341 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001342 ConsumeToken();
1343
Steve Naroff409be832007-11-11 19:54:21 +00001344 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001345 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001346 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001347
1348 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1349 SkipUntil(tok::l_brace, true, true);
1350
1351 // If we didn't find the '{', bail out.
1352 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001353 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001354 }
Steve Naroff409be832007-11-11 19:54:21 +00001355 SourceLocation BraceLoc = Tok.getLocation();
1356
1357 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001358 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001359
1360 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001361 // specified Declarator for the method.
1362 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001363
1364 OwningStmtResult FnBody(ParseCompoundStatementBody());
1365
Steve Naroff409be832007-11-11 19:54:21 +00001366 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001367 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001368 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1369 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001370
Steve Naroff409be832007-11-11 19:54:21 +00001371 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001372 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001373
Steve Naroff409be832007-11-11 19:54:21 +00001374 // TODO: Pass argument information.
Sebastian Redl798d1192008-12-13 16:23:55 +00001375 Actions.ActOnFinishFunctionBody(MDecl, move_convert(FnBody));
Steve Naroff71c0a952007-11-13 23:01:27 +00001376 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001377}
Anders Carlsson55085182007-08-21 17:43:55 +00001378
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001379Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001380 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001381 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001382 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1383 return ParseObjCThrowStmt(AtLoc);
1384 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1385 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001386 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001387 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001388 // If the expression is invalid, skip ahead to the next semicolon. Not
1389 // doing this opens us up to the possibility of infinite loops if
1390 // ParseExpression does not consume any tokens.
1391 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001392 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001393 }
1394 // Otherwise, eat the semicolon.
1395 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redla60528c2008-12-21 12:04:03 +00001396 return Actions.ActOnExprStmt(move_convert(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001397}
1398
Sebastian Redl1d922962008-12-13 15:32:12 +00001399Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001400 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001401 case tok::string_literal: // primary-expression: string-literal
1402 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001403 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001404 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001405 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001406 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001407
Chris Lattner4fef81d2008-08-05 06:19:09 +00001408 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1409 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001410 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001411 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001412 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001413 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001414 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001415 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001416 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001417 }
Anders Carlsson55085182007-08-21 17:43:55 +00001418 }
Anders Carlsson55085182007-08-21 17:43:55 +00001419}
1420
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001421/// objc-message-expr:
1422/// '[' objc-receiver objc-message-args ']'
1423///
1424/// objc-receiver:
1425/// expression
1426/// class-name
1427/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001428Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001429 assert(Tok.is(tok::l_square) && "'[' expected");
1430 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1431
1432 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001433 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001434 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +00001435 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001436 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1437 ExprArg(Actions));
Chris Lattner699b6612008-01-25 18:59:06 +00001438 }
1439
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001440 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001441 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001442 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001443 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001444 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001445
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001446 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl1d922962008-12-13 15:32:12 +00001447 0, move_convert(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001448}
Sebastian Redl1d922962008-12-13 15:32:12 +00001449
Chris Lattner699b6612008-01-25 18:59:06 +00001450/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1451/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001452///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001453/// objc-message-args:
1454/// objc-selector
1455/// objc-keywordarg-list
1456///
1457/// objc-keywordarg-list:
1458/// objc-keywordarg
1459/// objc-keywordarg-list objc-keywordarg
1460///
1461/// objc-keywordarg:
1462/// selector-name[opt] ':' objc-keywordexpr
1463///
1464/// objc-keywordexpr:
1465/// nonempty-expr-list
1466///
1467/// nonempty-expr-list:
1468/// assignment-expression
1469/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001470///
1471Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001472Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001473 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001474 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001475 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001476 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001477 SourceLocation Loc;
1478 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001479
1480 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001481 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001482
Chris Lattnerdf195262007-10-09 17:51:17 +00001483 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001484 while (1) {
1485 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001486 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001487
Chris Lattnerdf195262007-10-09 17:51:17 +00001488 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001489 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001490 // We must manually skip to a ']', otherwise the expression skipper will
1491 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1492 // the enclosing expression.
1493 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001494 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001495 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001496
Steve Naroff68d331a2007-09-27 14:38:14 +00001497 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001498 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001499 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001500 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001501 // We must manually skip to a ']', otherwise the expression skipper will
1502 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1503 // the enclosing expression.
1504 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001505 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001506 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001507
Steve Naroff37387c92007-09-17 20:25:27 +00001508 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001509 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001510
Steve Naroff37387c92007-09-17 20:25:27 +00001511 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001512 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001513 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001514 break;
1515 // We have a selector or a colon, continue parsing.
1516 }
1517 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001518 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001519 ConsumeToken(); // Eat the ','.
1520 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001521 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001522 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001523 // We must manually skip to a ']', otherwise the expression skipper will
1524 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1525 // the enclosing expression.
1526 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001527 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001528 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001529
Steve Naroff49f109c2007-11-15 13:05:42 +00001530 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001531 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001532 }
1533 } else if (!selIdent) {
1534 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001535
Chris Lattner4fef81d2008-08-05 06:19:09 +00001536 // We must manually skip to a ']', otherwise the expression skipper will
1537 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1538 // the enclosing expression.
1539 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001540 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001541 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001542
Chris Lattnerdf195262007-10-09 17:51:17 +00001543 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001544 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001545 // We must manually skip to a ']', otherwise the expression skipper will
1546 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1547 // the enclosing expression.
1548 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001549 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001550 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001551
Chris Lattner699b6612008-01-25 18:59:06 +00001552 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001553
Steve Naroff29238a02007-10-05 18:42:47 +00001554 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001555 if (nKeys == 0)
1556 KeyIdents.push_back(selIdent);
1557 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001558
Chris Lattnerff384912007-10-07 02:00:24 +00001559 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001560 if (ReceiverName)
1561 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
1562 LBracLoc, NameLoc, RBracLoc,
1563 KeyExprs.take(), KeyExprs.size()));
1564 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
1565 LBracLoc, RBracLoc,
1566 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001567}
1568
Sebastian Redl1d922962008-12-13 15:32:12 +00001569Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001570 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001571 if (Res.isInvalid()) return move(Res);
1572
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001573 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1574 // expressions. At this point, we know that the only valid thing that starts
1575 // with '@' is an @"".
1576 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001577 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001578 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001579 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001580
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001581 while (Tok.is(tok::at)) {
1582 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001583
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001584 // Invalid unless there is a string literal.
1585 OwningExprResult Lit(Actions, true);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001586 if (isTokenStringLiteral())
Sebastian Redla55e52c2008-11-25 22:21:31 +00001587 Lit = ParseStringLiteralExpression();
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001588 else
1589 Diag(Tok, diag::err_objc_concat_string);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001590
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001591 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001592 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001593
Sebastian Redleffa8d12008-12-10 00:02:53 +00001594 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001595 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001596
1597 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1598 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001599}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001600
1601/// objc-encode-expression:
1602/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001603Parser::OwningExprResult
1604Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001605 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001606
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001607 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001608
Chris Lattner4fef81d2008-08-05 06:19:09 +00001609 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001610 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1611
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001612 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001613
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001614 TypeTy *Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001615
Anders Carlsson4988ae32007-08-23 15:31:37 +00001616 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001617
1618 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
1619 RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001620}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001621
1622/// objc-protocol-expression
1623/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001624Parser::OwningExprResult
1625Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001626 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001627
Chris Lattner4fef81d2008-08-05 06:19:09 +00001628 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001629 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1630
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001631 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001632
Chris Lattner4fef81d2008-08-05 06:19:09 +00001633 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001634 return ExprError(Diag(Tok, diag::err_expected_ident));
1635
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001636 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001637 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001638
Anders Carlsson4988ae32007-08-23 15:31:37 +00001639 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001640
Sebastian Redl1d922962008-12-13 15:32:12 +00001641 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1642 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001643}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001644
1645/// objc-selector-expression
1646/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001647Parser::OwningExprResult
1648Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001649 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001650
Chris Lattner4fef81d2008-08-05 06:19:09 +00001651 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001652 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1653
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001654 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001655 SourceLocation LParenLoc = ConsumeParen();
1656 SourceLocation sLoc;
1657 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001658 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1659 return ExprError(Diag(Tok, diag::err_expected_ident));
1660
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001661 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001662 unsigned nColons = 0;
1663 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001664 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001665 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001666 return ExprError(Diag(Tok, diag::err_expected_colon));
1667
Chris Lattnercb53b362007-12-27 19:57:00 +00001668 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001669 ConsumeToken(); // Eat the ':'.
1670 if (Tok.is(tok::r_paren))
1671 break;
1672 // Check for another keyword selector.
1673 SourceLocation Loc;
1674 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001675 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001676 if (!SelIdent && Tok.isNot(tok::colon))
1677 break;
1678 }
Steve Naroff887407e2007-12-05 22:21:29 +00001679 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001680 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001681 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001682 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1683 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001684 }