blob: 52d52bde7e3df381368ce7fc02e30f9b10982106 [file] [log] [blame]
Ted Kremenek42730c52008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Steve Narofffb367882007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff87c329f2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner818350c2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
53 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Narofffb367882007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000069 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070 }
Chris Lattner4b009652007-07-25 00:24:17 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnera1d2bb72007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000082 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000083
Steve Naroff415c1832007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Steve Narofffb367882007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
116Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
124 return 0;
125 }
126 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
Chris Lattner45142b92008-07-26 04:07:02 +0000149
Steve Narofffb367882007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattner45142b92008-07-26 04:07:02 +0000151 SourceLocation EndProtoLoc;
152 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
153 if (Tok.is(tok::less) &&
154 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
155 return 0;
156
Steve Narofffb367882007-08-20 21:31:48 +0000157 if (attrList) // categories don't support attributes.
158 Diag(Tok, diag::err_objc_no_attributes_on_category);
159
Steve Naroff415c1832007-10-10 17:32:04 +0000160 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000161 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000162 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner45142b92008-07-26 04:07:02 +0000163 EndProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000164
165 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000166
Steve Naroff0bbffd82007-08-22 16:35:03 +0000167 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000168 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000169 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000170 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000171 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000172 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000173 return 0;
174 }
175 // Parse a class interface.
176 IdentifierInfo *superClassId = 0;
177 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000178
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000179 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000180 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000181 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000182 Diag(Tok, diag::err_expected_ident); // missing super class name.
183 return 0;
184 }
185 superClassId = Tok.getIdentifierInfo();
186 superClassLoc = ConsumeToken();
187 }
188 // Next, we need to check for any protocol references.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000189 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
190 SourceLocation EndProtoLoc;
191 if (Tok.is(tok::less) &&
192 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
193 return 0;
194
195 DeclTy *ClsType =
196 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
197 superClassId, superClassLoc,
198 &ProtocolRefs[0], ProtocolRefs.size(),
199 EndProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000200
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000201 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000202 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000203
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000204 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205
206 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000207 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000208 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000209 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000210 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000211 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000212 return 0;
213}
214
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000215/// constructSetterName - Return the setter name for the given
216/// identifier, i.e. "set" + Name where the initial character of Name
217/// has been capitalized.
218static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
219 const IdentifierInfo *Name) {
220 unsigned N = Name->getLength();
221 char *SelectorName = new char[3 + N];
222 memcpy(SelectorName, "set", 3);
223 memcpy(&SelectorName[3], Name->getName(), N);
224 SelectorName[3] = toupper(SelectorName[3]);
225
226 IdentifierInfo *Setter =
227 &Idents.get(SelectorName, &SelectorName[3 + N]);
228 delete[] SelectorName;
229 return Setter;
230}
231
Steve Narofffb367882007-08-20 21:31:48 +0000232/// objc-interface-decl-list:
233/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000234/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000235/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000236/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000237/// objc-interface-decl-list declaration
238/// objc-interface-decl-list ';'
239///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000240/// objc-method-requirement: [OBJC2]
241/// @required
242/// @optional
243///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000244void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000245 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000246 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000247 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000248 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000249 SourceLocation AtEndLoc;
250
Steve Naroff0bbffd82007-08-22 16:35:03 +0000251 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000252 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000253 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
254 DeclTy *methodPrototype =
255 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000256 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000257 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
258 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000259 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000260 continue;
261 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000262
Chris Lattnere48b46b2008-10-20 05:46:22 +0000263 // Ignore excess semicolons.
264 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000265 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000266 continue;
267 }
268
269 // If we got to the end of the file, pretend that we saw an @end.
270 // FIXME: Should this be a warning?
271 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000272 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000273
274 // If we don't have an @ directive, parse it as a function definition.
275 if (Tok.isNot(tok::at)) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000276 // FIXME: as the name implies, this rule allows function definitions.
277 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000278 ParseDeclarationOrFunctionDefinition();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000279 continue;
280 }
281
282 // Otherwise, we have an @ directive, eat the @.
283 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000284 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000285
Chris Lattnercba730b2008-10-20 05:57:40 +0000286 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000287 AtEndLoc = AtLoc;
288 break;
289 }
290
Chris Lattnercba730b2008-10-20 05:57:40 +0000291 switch (DirectiveKind) {
292 default:
293 Diag(Tok, diag::err_objc_illegal_interface_qual);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000294 ConsumeToken();
Chris Lattnercba730b2008-10-20 05:57:40 +0000295 // Skip until we see an @ or } or ;
296 SkipUntil(tok::r_brace, tok::at);
297 break;
298
299 case tok::objc_required:
300 ConsumeToken();
301 // This is only valid on protocols.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000302 if (contextKey != tok::objc_protocol)
303 Diag(AtLoc, diag::err_objc_protocol_required);
Chris Lattnercba730b2008-10-20 05:57:40 +0000304 else
305 MethodImplKind = tok::objc_required;
306 break;
307
308 case tok::objc_optional:
Chris Lattnere48b46b2008-10-20 05:46:22 +0000309 ConsumeToken();
Chris Lattnercba730b2008-10-20 05:57:40 +0000310 // This is only valid on protocols.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000311 if (contextKey != tok::objc_protocol)
312 Diag(AtLoc, diag::err_objc_protocol_optional);
Chris Lattnercba730b2008-10-20 05:57:40 +0000313 else
314 MethodImplKind = tok::objc_optional;
315 break;
316
317 case tok::objc_property:
Chris Lattnere48b46b2008-10-20 05:46:22 +0000318 ObjCDeclSpec OCDS;
319 ConsumeToken(); // the "property" identifier
320 // Parse property attribute list, if any.
321 if (Tok.is(tok::l_paren)) {
322 // property has attribute list.
323 ParseObjCPropertyAttribute(OCDS);
324 }
325 // Parse all the comma separated declarators.
326 DeclSpec DS;
327 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
328 ParseStructDeclaration(DS, FieldDeclarators);
329
330 if (Tok.is(tok::semi))
331 ConsumeToken();
332 else {
333 Diag(Tok, diag::err_expected_semi_decl_list);
334 SkipUntil(tok::r_brace, true, true);
335 }
336 // Convert them all to property declarations.
337 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
338 FieldDeclarator &FD = FieldDeclarators[i];
339 // Install the property declarator into interfaceDecl.
340 Selector GetterSel =
341 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
342 ? OCDS.getGetterName()
343 : FD.D.getIdentifier());
344 IdentifierInfo *SetterName = OCDS.getSetterName();
345 if (!SetterName)
346 SetterName = constructSetterName(PP.getIdentifierTable(),
347 FD.D.getIdentifier());
348 Selector SetterSel =
349 PP.getSelectorTable().getUnarySelector(SetterName);
350 DeclTy *Property = Actions.ActOnProperty(CurScope,
351 AtLoc, FD, OCDS,
352 GetterSel, SetterSel,
353 MethodImplKind);
354 allProperties.push_back(Property);
355 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000356 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000357 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000358 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000359 // Insert collected methods declarations into the @interface object.
360 // FIXME: This passes in an invalid SourceLocation for AtEndLoc when EOF is
361 // hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000362 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
363 allMethods.empty() ? 0 : &allMethods[0],
364 allMethods.size(),
365 allProperties.empty() ? 0 : &allProperties[0],
366 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000367}
368
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000369/// Parse property attribute declarations.
370///
371/// property-attr-decl: '(' property-attrlist ')'
372/// property-attrlist:
373/// property-attribute
374/// property-attrlist ',' property-attribute
375/// property-attribute:
376/// getter '=' identifier
377/// setter '=' identifier ':'
378/// readonly
379/// readwrite
380/// assign
381/// retain
382/// copy
383/// nonatomic
384///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000385void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000386 SourceLocation loc = ConsumeParen(); // consume '('
387 while (isObjCPropertyAttribute()) {
388 const IdentifierInfo *II = Tok.getIdentifierInfo();
389 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000390 if (II == ObjCPropertyAttrs[objc_getter] ||
391 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000392 // skip getter/setter part.
393 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000394 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000395 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000396 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000397 if (II == ObjCPropertyAttrs[objc_setter]) {
398 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000399 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000400 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000401 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000402 Diag(loc, diag::err_expected_colon);
403 SkipUntil(tok::r_paren,true,true);
404 break;
405 }
406 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000407 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000409 DS.setGetterName(Tok.getIdentifierInfo());
410 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000411 }
412 else {
413 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000414 SkipUntil(tok::r_paren,true,true);
415 break;
416 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000417 }
418 else {
419 Diag(loc, diag::err_objc_expected_equal);
420 SkipUntil(tok::r_paren,true,true);
421 break;
422 }
423 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000424
Ted Kremenek42730c52008-01-07 19:49:32 +0000425 else if (II == ObjCPropertyAttrs[objc_readonly])
426 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
427 else if (II == ObjCPropertyAttrs[objc_assign])
428 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
429 else if (II == ObjCPropertyAttrs[objc_readwrite])
430 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
431 else if (II == ObjCPropertyAttrs[objc_retain])
432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
433 else if (II == ObjCPropertyAttrs[objc_copy])
434 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
435 else if (II == ObjCPropertyAttrs[objc_nonatomic])
436 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000437
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000438 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000439 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000440 loc = ConsumeToken();
441 continue;
442 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000443 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000444 break;
445 Diag(loc, diag::err_expected_rparen);
446 SkipUntil(tok::semi);
447 return;
448 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000449 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000450 ConsumeParen();
451 else {
452 Diag(loc, diag::err_objc_expected_property_attr);
453 SkipUntil(tok::r_paren); // recover from error inside attribute list
454 }
455}
456
Steve Naroff81f1bba2007-09-06 21:24:23 +0000457/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000458/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000459/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000460///
461/// objc-instance-method: '-'
462/// objc-class-method: '+'
463///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000464/// objc-method-attributes: [OBJC2]
465/// __attribute__((deprecated))
466///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000467Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000468 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000469 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470
471 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000472 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000473
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000474 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000475 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000476 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000477 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000478}
479
480/// objc-selector:
481/// identifier
482/// one of
483/// enum struct union if else while do for switch case default
484/// break continue return goto asm sizeof typeof __alignof
485/// unsigned long const short volatile signed restrict _Complex
486/// in out inout bycopy byref oneway int char float double void _Bool
487///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000488IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000489 switch (Tok.getKind()) {
490 default:
491 return 0;
492 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000493 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000494 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000495 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000496 case tok::kw_break:
497 case tok::kw_case:
498 case tok::kw_catch:
499 case tok::kw_char:
500 case tok::kw_class:
501 case tok::kw_const:
502 case tok::kw_const_cast:
503 case tok::kw_continue:
504 case tok::kw_default:
505 case tok::kw_delete:
506 case tok::kw_do:
507 case tok::kw_double:
508 case tok::kw_dynamic_cast:
509 case tok::kw_else:
510 case tok::kw_enum:
511 case tok::kw_explicit:
512 case tok::kw_export:
513 case tok::kw_extern:
514 case tok::kw_false:
515 case tok::kw_float:
516 case tok::kw_for:
517 case tok::kw_friend:
518 case tok::kw_goto:
519 case tok::kw_if:
520 case tok::kw_inline:
521 case tok::kw_int:
522 case tok::kw_long:
523 case tok::kw_mutable:
524 case tok::kw_namespace:
525 case tok::kw_new:
526 case tok::kw_operator:
527 case tok::kw_private:
528 case tok::kw_protected:
529 case tok::kw_public:
530 case tok::kw_register:
531 case tok::kw_reinterpret_cast:
532 case tok::kw_restrict:
533 case tok::kw_return:
534 case tok::kw_short:
535 case tok::kw_signed:
536 case tok::kw_sizeof:
537 case tok::kw_static:
538 case tok::kw_static_cast:
539 case tok::kw_struct:
540 case tok::kw_switch:
541 case tok::kw_template:
542 case tok::kw_this:
543 case tok::kw_throw:
544 case tok::kw_true:
545 case tok::kw_try:
546 case tok::kw_typedef:
547 case tok::kw_typeid:
548 case tok::kw_typename:
549 case tok::kw_typeof:
550 case tok::kw_union:
551 case tok::kw_unsigned:
552 case tok::kw_using:
553 case tok::kw_virtual:
554 case tok::kw_void:
555 case tok::kw_volatile:
556 case tok::kw_wchar_t:
557 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000558 case tok::kw__Bool:
559 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000560 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000561 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000562 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000563 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000564 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000565}
566
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000567/// property-attrlist: one of
568/// readonly getter setter assign retain copy nonatomic
569///
570bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000571 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000572 const IdentifierInfo *II = Tok.getIdentifierInfo();
573 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000574 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000575 }
576 return false;
577}
578
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000579/// objc-for-collection-in: 'in'
580///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000581bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000582 // FIXME: May have to do additional look-ahead to only allow for
583 // valid tokens following an 'in'; such as an identifier, unary operators,
584 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000585 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000586 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000587}
588
Ted Kremenek42730c52008-01-07 19:49:32 +0000589/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000590/// qualifier list and builds their bitmask representation in the input
591/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000592///
593/// objc-type-qualifiers:
594/// objc-type-qualifier
595/// objc-type-qualifiers objc-type-qualifier
596///
Ted Kremenek42730c52008-01-07 19:49:32 +0000597void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000598 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000599 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000600 return;
601
602 const IdentifierInfo *II = Tok.getIdentifierInfo();
603 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000604 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000605 continue;
606
Ted Kremenek42730c52008-01-07 19:49:32 +0000607 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000608 switch (i) {
609 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000610 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
611 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
612 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
613 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
614 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
615 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000616 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000618 ConsumeToken();
619 II = 0;
620 break;
621 }
622
623 // If this wasn't a recognized qualifier, bail out.
624 if (II) return;
625 }
626}
627
628/// objc-type-name:
629/// '(' objc-type-qualifiers[opt] type-name ')'
630/// '(' objc-type-qualifiers[opt] ')'
631///
Ted Kremenek42730c52008-01-07 19:49:32 +0000632Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000633 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000634
635 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnerb5769332008-08-23 01:48:03 +0000636 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner265c8172007-09-27 15:15:46 +0000637 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000638
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000639 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000640 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000641
Steve Naroff0bbffd82007-08-22 16:35:03 +0000642 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000643 Ty = ParseTypeName();
644 // FIXME: back when Sema support is in place...
645 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000646 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000647
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000648 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000649 // If we didn't eat any tokens, then this isn't a type.
650 if (Tok.getLocation() == TypeStartLoc) {
651 Diag(Tok.getLocation(), diag::err_expected_type);
652 SkipUntil(tok::r_brace);
653 } else {
654 // Otherwise, we found *something*, but didn't get a ')' in the right
655 // place. Emit an error then return what we have as the type.
656 MatchRHSPunctuation(tok::r_paren, LParenLoc);
657 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000658 }
659 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000660 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000661}
662
663/// objc-method-decl:
664/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000665/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000666/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000667/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000668///
669/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000670/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000671/// objc-keyword-selector objc-keyword-decl
672///
673/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000674/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
675/// objc-selector ':' objc-keyword-attributes[opt] identifier
676/// ':' objc-type-name objc-keyword-attributes[opt] identifier
677/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000678///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000679/// objc-parmlist:
680/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000681///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000682/// objc-parms:
683/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000684///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000685/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000686/// , ...
687///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000688/// objc-keyword-attributes: [OBJC2]
689/// __attribute__((unused))
690///
Steve Naroff3774dd92007-10-26 20:53:56 +0000691Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000692 tok::TokenKind mType,
693 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000694 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000695{
Chris Lattnerb5769332008-08-23 01:48:03 +0000696 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000697 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000699 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000700 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000701
Steve Naroff3774dd92007-10-26 20:53:56 +0000702 SourceLocation selLoc;
703 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000704
705 if (!SelIdent) { // missing selector name.
706 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
707 SourceRange(mLoc, Tok.getLocation()));
708 // Skip until we get a ; or {}.
709 SkipUntil(tok::r_brace);
710 return 0;
711 }
712
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000713 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000714 // If attributes exist after the method, parse them.
715 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000716 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000717 MethodAttrs = ParseAttributes();
718
719 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000720 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000721 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000722 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000723 }
Steve Naroff304ed392007-09-05 23:30:30 +0000724
Steve Naroff4ed9d662007-09-27 14:38:14 +0000725 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
726 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000727 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000728 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000729
730 Action::TypeTy *TypeInfo;
731 while (1) {
732 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000733
Chris Lattnerd031a452007-10-07 02:00:24 +0000734 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000735 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000736 Diag(Tok, diag::err_expected_colon);
737 break;
738 }
739 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000740 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000741 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000742 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000743 else
744 TypeInfo = 0;
745 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000746 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000747
Chris Lattnerd031a452007-10-07 02:00:24 +0000748 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000749 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000750 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000751
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000752 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000753 Diag(Tok, diag::err_expected_ident); // missing argument name.
754 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000755 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000756 ArgNames.push_back(Tok.getIdentifierInfo());
757 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000758
Chris Lattnerd031a452007-10-07 02:00:24 +0000759 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000760 SourceLocation Loc;
761 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000762 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000763 break;
764 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000765 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000766
Steve Naroff29fe7462007-11-15 12:35:21 +0000767 bool isVariadic = false;
768
Chris Lattnerd031a452007-10-07 02:00:24 +0000769 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000770 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000771 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000772 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000773 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000774 ConsumeToken();
775 break;
776 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000777 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000778 // Parse the c-style argument declaration-specifier.
779 DeclSpec DS;
780 ParseDeclarationSpecifiers(DS);
781 // Parse the declarator.
782 Declarator ParmDecl(DS, Declarator::PrototypeContext);
783 ParseDeclarator(ParmDecl);
784 }
785
786 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000787 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000788 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000789 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000790 MethodAttrs = ParseAttributes();
791
792 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
793 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000794 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000795 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000796 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000797 &ArgNames[0], MethodAttrs,
798 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000799}
800
Steve Narofffb367882007-08-20 21:31:48 +0000801/// objc-protocol-refs:
802/// '<' identifier-list '>'
803///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000804bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000805ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
806 bool WarnOnDeclarations, SourceLocation &EndLoc) {
807 assert(Tok.is(tok::less) && "expected <");
808
809 ConsumeToken(); // the "<"
810
811 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
812
813 while (1) {
814 if (Tok.isNot(tok::identifier)) {
815 Diag(Tok, diag::err_expected_ident);
816 SkipUntil(tok::greater);
817 return true;
818 }
819 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
820 Tok.getLocation()));
821 ConsumeToken();
822
823 if (Tok.isNot(tok::comma))
824 break;
825 ConsumeToken();
826 }
827
828 // Consume the '>'.
829 if (Tok.isNot(tok::greater)) {
830 Diag(Tok, diag::err_expected_greater);
831 return true;
832 }
833
834 EndLoc = ConsumeAnyToken();
835
836 // Convert the list of protocols identifiers into a list of protocol decls.
837 Actions.FindProtocolDeclaration(WarnOnDeclarations,
838 &ProtocolIdents[0], ProtocolIdents.size(),
839 Protocols);
840 return false;
841}
842
Steve Narofffb367882007-08-20 21:31:48 +0000843/// objc-class-instance-variables:
844/// '{' objc-instance-variable-decl-list[opt] '}'
845///
846/// objc-instance-variable-decl-list:
847/// objc-visibility-spec
848/// objc-instance-variable-decl ';'
849/// ';'
850/// objc-instance-variable-decl-list objc-visibility-spec
851/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
852/// objc-instance-variable-decl-list ';'
853///
854/// objc-visibility-spec:
855/// @private
856/// @protected
857/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000858/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000859///
860/// objc-instance-variable-decl:
861/// struct-declaration
862///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000863void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
864 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000865 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000866 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000867 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
868
Steve Naroffc4474992007-08-21 21:17:12 +0000869 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000870
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000871 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000872 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000873 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000874 // Each iteration of this loop reads one objc-instance-variable-decl.
875
876 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000877 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000878 Diag(Tok, diag::ext_extra_struct_semi);
879 ConsumeToken();
880 continue;
881 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000882
Steve Naroffc4474992007-08-21 21:17:12 +0000883 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000884 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000885 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000886 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000887 case tok::objc_private:
888 case tok::objc_public:
889 case tok::objc_protected:
890 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000891 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000892 ConsumeToken();
893 continue;
894 default:
895 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000896 continue;
897 }
898 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000899
900 // Parse all the comma separated declarators.
901 DeclSpec DS;
902 FieldDeclarators.clear();
903 ParseStructDeclaration(DS, FieldDeclarators);
904
905 // Convert them all to fields.
906 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
907 FieldDeclarator &FD = FieldDeclarators[i];
908 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000909 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000910 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000911 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000912 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000913 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000914
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000915 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000916 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000917 } else {
918 Diag(Tok, diag::err_expected_semi_decl_list);
919 // Skip to end of block or statement
920 SkipUntil(tok::r_brace, true, true);
921 }
922 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000923 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000924 // Call ActOnFields() even if we don't have any decls. This is useful
925 // for code rewriting tools that need to be aware of the empty list.
926 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
927 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000928 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000929 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000930}
Steve Narofffb367882007-08-20 21:31:48 +0000931
932/// objc-protocol-declaration:
933/// objc-protocol-definition
934/// objc-protocol-forward-reference
935///
936/// objc-protocol-definition:
937/// @protocol identifier
938/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000939/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000940/// @end
941///
942/// objc-protocol-forward-reference:
943/// @protocol identifier-list ';'
944///
945/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000946/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000947/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000948Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
949 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000950 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000951 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
952 ConsumeToken(); // the "protocol" identifier
953
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000954 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000955 Diag(Tok, diag::err_expected_ident); // missing protocol name.
956 return 0;
957 }
958 // Save the protocol name, then consume it.
959 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
960 SourceLocation nameLoc = ConsumeToken();
961
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000962 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000963 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000964 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000965 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000966 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000967
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000968 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000969 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
970 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
971
Steve Naroff72f17fb2007-08-22 22:17:26 +0000972 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000973 while (1) {
974 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000975 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000976 Diag(Tok, diag::err_expected_ident);
977 SkipUntil(tok::semi);
978 return 0;
979 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000980 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
981 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000982 ConsumeToken(); // the identifier
983
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000984 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000985 break;
986 }
987 // Consume the ';'.
988 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
989 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000990
Steve Naroff415c1832007-10-10 17:32:04 +0000991 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000992 &ProtocolRefs[0],
993 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000994 }
995
Steve Naroff72f17fb2007-08-22 22:17:26 +0000996 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000997 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000998
Chris Lattner2bdedd62008-07-26 04:03:38 +0000999 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +00001000 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +00001001 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +00001002 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001003
Chris Lattner2bdedd62008-07-26 04:03:38 +00001004 DeclTy *ProtoType =
1005 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1006 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +00001007 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001008 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +00001009
1010 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +00001011 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +00001012 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +00001013 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +00001014 }
1015 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +00001016 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001017}
Steve Narofffb367882007-08-20 21:31:48 +00001018
1019/// objc-implementation:
1020/// objc-class-implementation-prologue
1021/// objc-category-implementation-prologue
1022///
1023/// objc-class-implementation-prologue:
1024/// @implementation identifier objc-superclass[opt]
1025/// objc-class-instance-variables[opt]
1026///
1027/// objc-category-implementation-prologue:
1028/// @implementation identifier ( identifier )
1029
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001030Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1031 SourceLocation atLoc) {
1032 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1033 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1034 ConsumeToken(); // the "implementation" identifier
1035
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001036 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001037 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1038 return 0;
1039 }
1040 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001041 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001042 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1043
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001044 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001045 // we have a category implementation.
1046 SourceLocation lparenLoc = ConsumeParen();
1047 SourceLocation categoryLoc, rparenLoc;
1048 IdentifierInfo *categoryId = 0;
1049
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001050 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001051 categoryId = Tok.getIdentifierInfo();
1052 categoryLoc = ConsumeToken();
1053 } else {
1054 Diag(Tok, diag::err_expected_ident); // missing category name.
1055 return 0;
1056 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001058 Diag(Tok, diag::err_expected_rparen);
1059 SkipUntil(tok::r_paren, false); // don't stop at ';'
1060 return 0;
1061 }
1062 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001063 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001064 atLoc, nameId, nameLoc, categoryId,
1065 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001066 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001067 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001068 }
1069 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001070 SourceLocation superClassLoc;
1071 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001072 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001073 // We have a super class
1074 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001075 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001076 Diag(Tok, diag::err_expected_ident); // missing super class name.
1077 return 0;
1078 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001079 superClassId = Tok.getIdentifierInfo();
1080 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001081 }
Steve Naroff415c1832007-10-10 17:32:04 +00001082 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001083 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001084 superClassId, superClassLoc);
1085
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001086 if (Tok.is(tok::l_brace)) // we have ivars
1087 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001088 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001089
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001090 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001091}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001092
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001093Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1094 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1095 "ParseObjCAtEndDeclaration(): Expected @end");
1096 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001097 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001098 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001099 else
1100 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001101 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001102}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001103
1104/// compatibility-alias-decl:
1105/// @compatibility_alias alias-name class-name ';'
1106///
1107Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1108 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1109 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1110 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001111 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001112 Diag(Tok, diag::err_expected_ident);
1113 return 0;
1114 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001115 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1116 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001117 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001118 Diag(Tok, diag::err_expected_ident);
1119 return 0;
1120 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001121 IdentifierInfo *classId = Tok.getIdentifierInfo();
1122 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1123 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001124 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001125 return 0;
1126 }
1127 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1128 aliasId, aliasLoc,
1129 classId, classLoc);
1130 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001131}
1132
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001133/// property-synthesis:
1134/// @synthesize property-ivar-list ';'
1135///
1136/// property-ivar-list:
1137/// property-ivar
1138/// property-ivar-list ',' property-ivar
1139///
1140/// property-ivar:
1141/// identifier
1142/// identifier '=' identifier
1143///
1144Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1145 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1146 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001147 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001148 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001149 Diag(Tok, diag::err_expected_ident);
1150 return 0;
1151 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001152 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001153 IdentifierInfo *propertyIvar = 0;
1154 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1155 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001156 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001157 // property '=' ivar-name
1158 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001159 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001160 Diag(Tok, diag::err_expected_ident);
1161 break;
1162 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001163 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001164 ConsumeToken(); // consume ivar-name
1165 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001166 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1167 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001168 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001169 break;
1170 ConsumeToken(); // consume ','
1171 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001172 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001173 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1174 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001175}
1176
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001177/// property-dynamic:
1178/// @dynamic property-list
1179///
1180/// property-list:
1181/// identifier
1182/// property-list ',' identifier
1183///
1184Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1185 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1186 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1187 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001188 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001189 Diag(Tok, diag::err_expected_ident);
1190 return 0;
1191 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001192 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001193 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1194 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1195 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1196 propertyId, 0);
1197
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001198 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001199 break;
1200 ConsumeToken(); // consume ','
1201 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001202 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001203 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1204 return 0;
1205}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001206
1207/// objc-throw-statement:
1208/// throw expression[opt];
1209///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001210Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1211 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001212 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001213 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001214 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001215 if (Res.isInvalid) {
1216 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001217 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001218 }
1219 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001220 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001221 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001222}
1223
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001224/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001225/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001226///
1227Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001228 ConsumeToken(); // consume synchronized
1229 if (Tok.isNot(tok::l_paren)) {
1230 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1231 return true;
1232 }
1233 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001234 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001235 if (Res.isInvalid) {
1236 SkipUntil(tok::semi);
1237 return true;
1238 }
1239 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001240 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001241 return true;
1242 }
1243 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001244 if (Tok.isNot(tok::l_brace)) {
1245 Diag (Tok, diag::err_expected_lbrace);
1246 return true;
1247 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001248 // Enter a scope to hold everything within the compound stmt. Compound
1249 // statements can always hold declarations.
1250 EnterScope(Scope::DeclScope);
1251
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001252 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001253
1254 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001255 if (SynchBody.isInvalid)
1256 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1257 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001258}
1259
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001260/// objc-try-catch-statement:
1261/// @try compound-statement objc-catch-list[opt]
1262/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1263///
1264/// objc-catch-list:
1265/// @catch ( parameter-declaration ) compound-statement
1266/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1267/// catch-parameter-declaration:
1268/// parameter-declaration
1269/// '...' [OBJC2]
1270///
Chris Lattner80712392008-03-10 06:06:04 +00001271Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001272 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001273
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001274 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001275 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001276 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001277 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001278 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001279 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001280 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001281 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001282 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001283 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001284 if (TryBody.isInvalid)
1285 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001286
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001287 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001288 // At this point, we need to lookahead to determine if this @ is the start
1289 // of an @catch or @finally. We don't want to consume the @ token if this
1290 // is an @try or @encode or something else.
1291 Token AfterAt = GetLookAheadToken(1);
1292 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1293 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1294 break;
1295
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001296 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001297 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001298 StmtTy *FirstPart = 0;
1299 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001300 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001301 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001302 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001303 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001304 DeclSpec DS;
1305 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001306 // For some odd reason, the name of the exception variable is
1307 // optional. As a result, we need to use PrototypeContext.
1308 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001309 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001310 if (DeclaratorInfo.getIdentifier()) {
1311 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001312 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001313 StmtResult stmtResult =
1314 Actions.ActOnDeclStmt(aBlockVarDecl,
1315 DS.getSourceRange().getBegin(),
1316 DeclaratorInfo.getSourceRange().getEnd());
1317 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1318 }
Steve Naroffc949a462008-02-05 21:27:35 +00001319 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001320 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001321 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001322
1323 StmtResult CatchBody(true);
1324 if (Tok.is(tok::l_brace))
1325 CatchBody = ParseCompoundStatementBody();
1326 else
1327 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001328 if (CatchBody.isInvalid)
1329 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001330 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001331 FirstPart, CatchBody.Val, CatchStmts.Val);
1332 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001333 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001334 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1335 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001336 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001337 }
1338 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001339 } else {
1340 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001341 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001342 EnterScope(Scope::DeclScope);
1343
Chris Lattner8027be62008-02-14 19:27:54 +00001344
1345 StmtResult FinallyBody(true);
1346 if (Tok.is(tok::l_brace))
1347 FinallyBody = ParseCompoundStatementBody();
1348 else
1349 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001350 if (FinallyBody.isInvalid)
1351 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001352 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001353 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001354 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001355 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001356 break;
1357 }
1358 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001359 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001360 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001361 return true;
1362 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001363 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001364 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001365}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001366
Steve Naroff81f1bba2007-09-06 21:24:23 +00001367/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001368///
Steve Naroff18c83382007-11-13 23:01:27 +00001369Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001370 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001371 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001372 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001373 ConsumeToken();
1374
Steve Naroff9191a9e82007-11-11 19:54:21 +00001375 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001376 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001377 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001378
1379 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1380 SkipUntil(tok::l_brace, true, true);
1381
1382 // If we didn't find the '{', bail out.
1383 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001384 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001385 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001386 SourceLocation BraceLoc = Tok.getLocation();
1387
1388 // Enter a scope for the method body.
1389 EnterScope(Scope::FnScope|Scope::DeclScope);
1390
1391 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001392 // specified Declarator for the method.
1393 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001394
1395 StmtResult FnBody = ParseCompoundStatementBody();
1396
1397 // If the function body could not be parsed, make a bogus compoundstmt.
1398 if (FnBody.isInvalid)
1399 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1400
1401 // Leave the function body scope.
1402 ExitScope();
1403
1404 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001405 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001406 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001407}
Anders Carlssona66cad42007-08-21 17:43:55 +00001408
Steve Naroffc949a462008-02-05 21:27:35 +00001409Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1410 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001411 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001412 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1413 return ParseObjCThrowStmt(AtLoc);
1414 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1415 return ParseObjCSynchronizedStmt(AtLoc);
1416 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1417 if (Res.isInvalid) {
1418 // If the expression is invalid, skip ahead to the next semicolon. Not
1419 // doing this opens us up to the possibility of infinite loops if
1420 // ParseExpression does not consume any tokens.
1421 SkipUntil(tok::semi);
1422 return true;
1423 }
1424 // Otherwise, eat the semicolon.
1425 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1426 return Actions.ActOnExprStmt(Res.Val);
1427}
1428
Steve Narofffb9dd752007-10-15 20:55:58 +00001429Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001430 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001431 case tok::string_literal: // primary-expression: string-literal
1432 case tok::wide_string_literal:
1433 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1434 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001435 if (Tok.getIdentifierInfo() == 0)
1436 return Diag(AtLoc, diag::err_unexpected_at);
1437
1438 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1439 case tok::objc_encode:
1440 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1441 case tok::objc_protocol:
1442 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1443 case tok::objc_selector:
1444 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1445 default:
1446 return Diag(AtLoc, diag::err_unexpected_at);
1447 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001448 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001449}
1450
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001451/// objc-message-expr:
1452/// '[' objc-receiver objc-message-args ']'
1453///
1454/// objc-receiver:
1455/// expression
1456/// class-name
1457/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001458Parser::ExprResult Parser::ParseObjCMessageExpression() {
1459 assert(Tok.is(tok::l_square) && "'[' expected");
1460 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1461
1462 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001463 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001464 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1465 ConsumeToken();
1466 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1467 }
1468
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001469 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001470 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001471 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001472 return Res;
1473 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001474
Chris Lattnered27a532008-01-25 18:59:06 +00001475 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1476}
1477
1478/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1479/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001480///
1481/// objc-message-args:
1482/// objc-selector
1483/// objc-keywordarg-list
1484///
1485/// objc-keywordarg-list:
1486/// objc-keywordarg
1487/// objc-keywordarg-list objc-keywordarg
1488///
1489/// objc-keywordarg:
1490/// selector-name[opt] ':' objc-keywordexpr
1491///
1492/// objc-keywordexpr:
1493/// nonempty-expr-list
1494///
1495/// nonempty-expr-list:
1496/// assignment-expression
1497/// nonempty-expr-list , assignment-expression
1498///
Chris Lattnered27a532008-01-25 18:59:06 +00001499Parser::ExprResult
1500Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1501 IdentifierInfo *ReceiverName,
1502 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001503 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001504 SourceLocation Loc;
1505 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001506
1507 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1508 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1509
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001510 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001511 while (1) {
1512 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001513 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001514
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001515 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001516 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001517 // We must manually skip to a ']', otherwise the expression skipper will
1518 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1519 // the enclosing expression.
1520 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001521 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001522 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001523
Steve Naroff4ed9d662007-09-27 14:38:14 +00001524 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001525 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001526 ExprResult Res = ParseAssignmentExpression();
1527 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001528 // We must manually skip to a ']', otherwise the expression skipper will
1529 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1530 // the enclosing expression.
1531 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001532 return Res;
1533 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001534
Steve Naroff253118b2007-09-17 20:25:27 +00001535 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001536 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001537
1538 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001539 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001540 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001541 break;
1542 // We have a selector or a colon, continue parsing.
1543 }
1544 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001545 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001546 ConsumeToken(); // Eat the ','.
1547 /// Parse the expression after ','
1548 ExprResult Res = ParseAssignmentExpression();
1549 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001550 // We must manually skip to a ']', otherwise the expression skipper will
1551 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1552 // the enclosing expression.
1553 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001554 return Res;
1555 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001556
Steve Naroff9f176d12007-11-15 13:05:42 +00001557 // We have a valid expression.
1558 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001559 }
1560 } else if (!selIdent) {
1561 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001562
1563 // We must manually skip to a ']', otherwise the expression skipper will
1564 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1565 // the enclosing expression.
1566 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001567 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001568 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001569
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001570 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001571 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001572 // We must manually skip to a ']', otherwise the expression skipper will
1573 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1574 // the enclosing expression.
1575 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001576 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001577 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001578
Chris Lattnered27a532008-01-25 18:59:06 +00001579 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001580
Steve Narofff9e80db2007-10-05 18:42:47 +00001581 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001582 if (nKeys == 0)
1583 KeyIdents.push_back(selIdent);
1584 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1585
1586 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001587 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001588 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001589 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001590 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001591 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001592 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001593}
1594
Steve Naroff0add5d22007-11-03 11:27:19 +00001595Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001596 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001597 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001598
1599 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1600 // expressions. At this point, we know that the only valid thing that starts
1601 // with '@' is an @"".
1602 llvm::SmallVector<SourceLocation, 4> AtLocs;
1603 llvm::SmallVector<ExprTy*, 4> AtStrings;
1604 AtLocs.push_back(AtLoc);
1605 AtStrings.push_back(Res.Val);
1606
1607 while (Tok.is(tok::at)) {
1608 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001609
Chris Lattnerddd3e632007-12-12 01:04:12 +00001610 ExprResult Res(true); // Invalid unless there is a string literal.
1611 if (isTokenStringLiteral())
1612 Res = ParseStringLiteralExpression();
1613 else
1614 Diag(Tok, diag::err_objc_concat_string);
1615
1616 if (Res.isInvalid) {
1617 while (!AtStrings.empty()) {
1618 Actions.DeleteExpr(AtStrings.back());
1619 AtStrings.pop_back();
1620 }
1621 return Res;
1622 }
1623
1624 AtStrings.push_back(Res.Val);
1625 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001626
Chris Lattnerddd3e632007-12-12 01:04:12 +00001627 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1628 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001629}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001630
1631/// objc-encode-expression:
1632/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001633Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001634 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001635
1636 SourceLocation EncLoc = ConsumeToken();
1637
Chris Lattnerf9311a92008-08-05 06:19:09 +00001638 if (Tok.isNot(tok::l_paren))
1639 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001640
1641 SourceLocation LParenLoc = ConsumeParen();
1642
1643 TypeTy *Ty = ParseTypeName();
1644
Anders Carlsson92faeb82007-08-23 15:31:37 +00001645 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001646
Chris Lattnercfd61c82007-10-16 22:51:17 +00001647 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001648 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001649}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001650
1651/// objc-protocol-expression
1652/// @protocol ( protocol-name )
1653
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001654Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001655{
1656 SourceLocation ProtoLoc = ConsumeToken();
1657
Chris Lattnerf9311a92008-08-05 06:19:09 +00001658 if (Tok.isNot(tok::l_paren))
1659 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001660
1661 SourceLocation LParenLoc = ConsumeParen();
1662
Chris Lattnerf9311a92008-08-05 06:19:09 +00001663 if (Tok.isNot(tok::identifier))
1664 return Diag(Tok, diag::err_expected_ident);
1665
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001666 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001667 ConsumeToken();
1668
Anders Carlsson92faeb82007-08-23 15:31:37 +00001669 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001670
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001671 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1672 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001673}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001674
1675/// objc-selector-expression
1676/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001677Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001678{
1679 SourceLocation SelectorLoc = ConsumeToken();
1680
Chris Lattnerf9311a92008-08-05 06:19:09 +00001681 if (Tok.isNot(tok::l_paren))
1682 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001683
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001684 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001685 SourceLocation LParenLoc = ConsumeParen();
1686 SourceLocation sLoc;
1687 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001688 if (!SelIdent && Tok.isNot(tok::colon))
1689 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1690
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001691 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001692 unsigned nColons = 0;
1693 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001694 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001695 if (Tok.isNot(tok::colon))
1696 return Diag(Tok, diag::err_expected_colon);
1697
Chris Lattner847f5c12007-12-27 19:57:00 +00001698 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001699 ConsumeToken(); // Eat the ':'.
1700 if (Tok.is(tok::r_paren))
1701 break;
1702 // Check for another keyword selector.
1703 SourceLocation Loc;
1704 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001705 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001706 if (!SelIdent && Tok.isNot(tok::colon))
1707 break;
1708 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001709 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001710 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001711 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001712 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001713 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001714 }