blob: 16fab37e4815e467b08676afaff2d63f059bad1a [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 "@"
284 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
285
286 if (ocKind == tok::objc_end) { // @end -> terminate list
287 AtEndLoc = AtLoc;
288 break;
289 }
290
291 if (ocKind == tok::objc_required) { // protocols only
292 ConsumeToken();
293 MethodImplKind = ocKind;
294 if (contextKey != tok::objc_protocol)
295 Diag(AtLoc, diag::err_objc_protocol_required);
296 } else if (ocKind == tok::objc_optional) { // protocols only
297 ConsumeToken();
298 MethodImplKind = ocKind;
299 if (contextKey != tok::objc_protocol)
300 Diag(AtLoc, diag::err_objc_protocol_optional);
301 } else if (ocKind == tok::objc_property) {
302 ObjCDeclSpec OCDS;
303 ConsumeToken(); // the "property" identifier
304 // Parse property attribute list, if any.
305 if (Tok.is(tok::l_paren)) {
306 // property has attribute list.
307 ParseObjCPropertyAttribute(OCDS);
308 }
309 // Parse all the comma separated declarators.
310 DeclSpec DS;
311 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
312 ParseStructDeclaration(DS, FieldDeclarators);
313
314 if (Tok.is(tok::semi))
315 ConsumeToken();
316 else {
317 Diag(Tok, diag::err_expected_semi_decl_list);
318 SkipUntil(tok::r_brace, true, true);
319 }
320 // Convert them all to property declarations.
321 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
322 FieldDeclarator &FD = FieldDeclarators[i];
323 // Install the property declarator into interfaceDecl.
324 Selector GetterSel =
325 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
326 ? OCDS.getGetterName()
327 : FD.D.getIdentifier());
328 IdentifierInfo *SetterName = OCDS.getSetterName();
329 if (!SetterName)
330 SetterName = constructSetterName(PP.getIdentifierTable(),
331 FD.D.getIdentifier());
332 Selector SetterSel =
333 PP.getSelectorTable().getUnarySelector(SetterName);
334 DeclTy *Property = Actions.ActOnProperty(CurScope,
335 AtLoc, FD, OCDS,
336 GetterSel, SetterSel,
337 MethodImplKind);
338 allProperties.push_back(Property);
339 }
340 continue;
341 } else {
342 Diag(Tok, diag::err_objc_illegal_interface_qual);
343 ConsumeToken();
Steve Naroff304ed392007-09-05 23:30:30 +0000344 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000345 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000346 /// Insert collected methods declarations into the @interface object.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000347 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
348 allMethods.empty() ? 0 : &allMethods[0],
349 allMethods.size(),
350 allProperties.empty() ? 0 : &allProperties[0],
351 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000352}
353
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000354/// Parse property attribute declarations.
355///
356/// property-attr-decl: '(' property-attrlist ')'
357/// property-attrlist:
358/// property-attribute
359/// property-attrlist ',' property-attribute
360/// property-attribute:
361/// getter '=' identifier
362/// setter '=' identifier ':'
363/// readonly
364/// readwrite
365/// assign
366/// retain
367/// copy
368/// nonatomic
369///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000370void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000371 SourceLocation loc = ConsumeParen(); // consume '('
372 while (isObjCPropertyAttribute()) {
373 const IdentifierInfo *II = Tok.getIdentifierInfo();
374 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000375 if (II == ObjCPropertyAttrs[objc_getter] ||
376 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000377 // skip getter/setter part.
378 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000379 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000380 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000381 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000382 if (II == ObjCPropertyAttrs[objc_setter]) {
383 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000384 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000385 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000386 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000387 Diag(loc, diag::err_expected_colon);
388 SkipUntil(tok::r_paren,true,true);
389 break;
390 }
391 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000392 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000393 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000394 DS.setGetterName(Tok.getIdentifierInfo());
395 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000396 }
397 else {
398 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000399 SkipUntil(tok::r_paren,true,true);
400 break;
401 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000402 }
403 else {
404 Diag(loc, diag::err_objc_expected_equal);
405 SkipUntil(tok::r_paren,true,true);
406 break;
407 }
408 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000409
Ted Kremenek42730c52008-01-07 19:49:32 +0000410 else if (II == ObjCPropertyAttrs[objc_readonly])
411 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
412 else if (II == ObjCPropertyAttrs[objc_assign])
413 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
414 else if (II == ObjCPropertyAttrs[objc_readwrite])
415 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
416 else if (II == ObjCPropertyAttrs[objc_retain])
417 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
418 else if (II == ObjCPropertyAttrs[objc_copy])
419 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
420 else if (II == ObjCPropertyAttrs[objc_nonatomic])
421 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000422
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000423 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000424 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000425 loc = ConsumeToken();
426 continue;
427 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000428 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000429 break;
430 Diag(loc, diag::err_expected_rparen);
431 SkipUntil(tok::semi);
432 return;
433 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000434 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000435 ConsumeParen();
436 else {
437 Diag(loc, diag::err_objc_expected_property_attr);
438 SkipUntil(tok::r_paren); // recover from error inside attribute list
439 }
440}
441
Steve Naroff81f1bba2007-09-06 21:24:23 +0000442/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000443/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000444/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000445///
446/// objc-instance-method: '-'
447/// objc-class-method: '+'
448///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000449/// objc-method-attributes: [OBJC2]
450/// __attribute__((deprecated))
451///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000452Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000453 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000454 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000455
456 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000457 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000458
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000459 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000460 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000461 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000462 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000463}
464
465/// objc-selector:
466/// identifier
467/// one of
468/// enum struct union if else while do for switch case default
469/// break continue return goto asm sizeof typeof __alignof
470/// unsigned long const short volatile signed restrict _Complex
471/// in out inout bycopy byref oneway int char float double void _Bool
472///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000473IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000474 switch (Tok.getKind()) {
475 default:
476 return 0;
477 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000478 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000479 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000480 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000481 case tok::kw_break:
482 case tok::kw_case:
483 case tok::kw_catch:
484 case tok::kw_char:
485 case tok::kw_class:
486 case tok::kw_const:
487 case tok::kw_const_cast:
488 case tok::kw_continue:
489 case tok::kw_default:
490 case tok::kw_delete:
491 case tok::kw_do:
492 case tok::kw_double:
493 case tok::kw_dynamic_cast:
494 case tok::kw_else:
495 case tok::kw_enum:
496 case tok::kw_explicit:
497 case tok::kw_export:
498 case tok::kw_extern:
499 case tok::kw_false:
500 case tok::kw_float:
501 case tok::kw_for:
502 case tok::kw_friend:
503 case tok::kw_goto:
504 case tok::kw_if:
505 case tok::kw_inline:
506 case tok::kw_int:
507 case tok::kw_long:
508 case tok::kw_mutable:
509 case tok::kw_namespace:
510 case tok::kw_new:
511 case tok::kw_operator:
512 case tok::kw_private:
513 case tok::kw_protected:
514 case tok::kw_public:
515 case tok::kw_register:
516 case tok::kw_reinterpret_cast:
517 case tok::kw_restrict:
518 case tok::kw_return:
519 case tok::kw_short:
520 case tok::kw_signed:
521 case tok::kw_sizeof:
522 case tok::kw_static:
523 case tok::kw_static_cast:
524 case tok::kw_struct:
525 case tok::kw_switch:
526 case tok::kw_template:
527 case tok::kw_this:
528 case tok::kw_throw:
529 case tok::kw_true:
530 case tok::kw_try:
531 case tok::kw_typedef:
532 case tok::kw_typeid:
533 case tok::kw_typename:
534 case tok::kw_typeof:
535 case tok::kw_union:
536 case tok::kw_unsigned:
537 case tok::kw_using:
538 case tok::kw_virtual:
539 case tok::kw_void:
540 case tok::kw_volatile:
541 case tok::kw_wchar_t:
542 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000543 case tok::kw__Bool:
544 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000545 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000546 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000547 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000548 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000549 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000550}
551
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000552/// property-attrlist: one of
553/// readonly getter setter assign retain copy nonatomic
554///
555bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000556 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000557 const IdentifierInfo *II = Tok.getIdentifierInfo();
558 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000559 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000560 }
561 return false;
562}
563
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000564/// objc-for-collection-in: 'in'
565///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000566bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000567 // FIXME: May have to do additional look-ahead to only allow for
568 // valid tokens following an 'in'; such as an identifier, unary operators,
569 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000570 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000571 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000572}
573
Ted Kremenek42730c52008-01-07 19:49:32 +0000574/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000575/// qualifier list and builds their bitmask representation in the input
576/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000577///
578/// objc-type-qualifiers:
579/// objc-type-qualifier
580/// objc-type-qualifiers objc-type-qualifier
581///
Ted Kremenek42730c52008-01-07 19:49:32 +0000582void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000583 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000584 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000585 return;
586
587 const IdentifierInfo *II = Tok.getIdentifierInfo();
588 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000590 continue;
591
Ted Kremenek42730c52008-01-07 19:49:32 +0000592 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000593 switch (i) {
594 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000595 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
596 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
597 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
598 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
599 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
600 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000601 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000602 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000603 ConsumeToken();
604 II = 0;
605 break;
606 }
607
608 // If this wasn't a recognized qualifier, bail out.
609 if (II) return;
610 }
611}
612
613/// objc-type-name:
614/// '(' objc-type-qualifiers[opt] type-name ')'
615/// '(' objc-type-qualifiers[opt] ')'
616///
Ted Kremenek42730c52008-01-07 19:49:32 +0000617Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000618 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000619
620 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnerb5769332008-08-23 01:48:03 +0000621 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner265c8172007-09-27 15:15:46 +0000622 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000623
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000624 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000625 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000626
Steve Naroff0bbffd82007-08-22 16:35:03 +0000627 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000628 Ty = ParseTypeName();
629 // FIXME: back when Sema support is in place...
630 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000631 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000632
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000633 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000634 // If we didn't eat any tokens, then this isn't a type.
635 if (Tok.getLocation() == TypeStartLoc) {
636 Diag(Tok.getLocation(), diag::err_expected_type);
637 SkipUntil(tok::r_brace);
638 } else {
639 // Otherwise, we found *something*, but didn't get a ')' in the right
640 // place. Emit an error then return what we have as the type.
641 MatchRHSPunctuation(tok::r_paren, LParenLoc);
642 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000643 }
644 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000645 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000646}
647
648/// objc-method-decl:
649/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000650/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000651/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000652/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000653///
654/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000655/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000656/// objc-keyword-selector objc-keyword-decl
657///
658/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000659/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
660/// objc-selector ':' objc-keyword-attributes[opt] identifier
661/// ':' objc-type-name objc-keyword-attributes[opt] identifier
662/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000663///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000664/// objc-parmlist:
665/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000666///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000667/// objc-parms:
668/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000669///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000670/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000671/// , ...
672///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000673/// objc-keyword-attributes: [OBJC2]
674/// __attribute__((unused))
675///
Steve Naroff3774dd92007-10-26 20:53:56 +0000676Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000677 tok::TokenKind mType,
678 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000679 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000680{
Chris Lattnerb5769332008-08-23 01:48:03 +0000681 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000682 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000683 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000684 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000685 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000686
Steve Naroff3774dd92007-10-26 20:53:56 +0000687 SourceLocation selLoc;
688 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000689
690 if (!SelIdent) { // missing selector name.
691 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
692 SourceRange(mLoc, Tok.getLocation()));
693 // Skip until we get a ; or {}.
694 SkipUntil(tok::r_brace);
695 return 0;
696 }
697
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000698 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000699 // If attributes exist after the method, parse them.
700 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000701 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000702 MethodAttrs = ParseAttributes();
703
704 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000705 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000706 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000707 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000708 }
Steve Naroff304ed392007-09-05 23:30:30 +0000709
Steve Naroff4ed9d662007-09-27 14:38:14 +0000710 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
711 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000712 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000713 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000714
715 Action::TypeTy *TypeInfo;
716 while (1) {
717 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000718
Chris Lattnerd031a452007-10-07 02:00:24 +0000719 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000720 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000721 Diag(Tok, diag::err_expected_colon);
722 break;
723 }
724 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000725 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000726 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000727 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000728 else
729 TypeInfo = 0;
730 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000731 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000732
Chris Lattnerd031a452007-10-07 02:00:24 +0000733 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000734 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000735 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000736
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000737 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000738 Diag(Tok, diag::err_expected_ident); // missing argument name.
739 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000740 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000741 ArgNames.push_back(Tok.getIdentifierInfo());
742 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000743
Chris Lattnerd031a452007-10-07 02:00:24 +0000744 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000745 SourceLocation Loc;
746 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000747 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000748 break;
749 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000750 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000751
Steve Naroff29fe7462007-11-15 12:35:21 +0000752 bool isVariadic = false;
753
Chris Lattnerd031a452007-10-07 02:00:24 +0000754 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000755 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000756 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000757 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000758 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000759 ConsumeToken();
760 break;
761 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000762 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000763 // Parse the c-style argument declaration-specifier.
764 DeclSpec DS;
765 ParseDeclarationSpecifiers(DS);
766 // Parse the declarator.
767 Declarator ParmDecl(DS, Declarator::PrototypeContext);
768 ParseDeclarator(ParmDecl);
769 }
770
771 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000772 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000773 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000774 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000775 MethodAttrs = ParseAttributes();
776
777 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
778 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000779 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000780 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000781 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000782 &ArgNames[0], MethodAttrs,
783 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000784}
785
Steve Narofffb367882007-08-20 21:31:48 +0000786/// objc-protocol-refs:
787/// '<' identifier-list '>'
788///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000789bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000790ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
791 bool WarnOnDeclarations, SourceLocation &EndLoc) {
792 assert(Tok.is(tok::less) && "expected <");
793
794 ConsumeToken(); // the "<"
795
796 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
797
798 while (1) {
799 if (Tok.isNot(tok::identifier)) {
800 Diag(Tok, diag::err_expected_ident);
801 SkipUntil(tok::greater);
802 return true;
803 }
804 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
805 Tok.getLocation()));
806 ConsumeToken();
807
808 if (Tok.isNot(tok::comma))
809 break;
810 ConsumeToken();
811 }
812
813 // Consume the '>'.
814 if (Tok.isNot(tok::greater)) {
815 Diag(Tok, diag::err_expected_greater);
816 return true;
817 }
818
819 EndLoc = ConsumeAnyToken();
820
821 // Convert the list of protocols identifiers into a list of protocol decls.
822 Actions.FindProtocolDeclaration(WarnOnDeclarations,
823 &ProtocolIdents[0], ProtocolIdents.size(),
824 Protocols);
825 return false;
826}
827
Steve Narofffb367882007-08-20 21:31:48 +0000828/// objc-class-instance-variables:
829/// '{' objc-instance-variable-decl-list[opt] '}'
830///
831/// objc-instance-variable-decl-list:
832/// objc-visibility-spec
833/// objc-instance-variable-decl ';'
834/// ';'
835/// objc-instance-variable-decl-list objc-visibility-spec
836/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
837/// objc-instance-variable-decl-list ';'
838///
839/// objc-visibility-spec:
840/// @private
841/// @protected
842/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000843/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000844///
845/// objc-instance-variable-decl:
846/// struct-declaration
847///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000848void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
849 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000850 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000851 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000852 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
853
Steve Naroffc4474992007-08-21 21:17:12 +0000854 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000855
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000856 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000857 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000858 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000859 // Each iteration of this loop reads one objc-instance-variable-decl.
860
861 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000862 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000863 Diag(Tok, diag::ext_extra_struct_semi);
864 ConsumeToken();
865 continue;
866 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000867
Steve Naroffc4474992007-08-21 21:17:12 +0000868 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000869 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000870 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000871 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000872 case tok::objc_private:
873 case tok::objc_public:
874 case tok::objc_protected:
875 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000876 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000877 ConsumeToken();
878 continue;
879 default:
880 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000881 continue;
882 }
883 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000884
885 // Parse all the comma separated declarators.
886 DeclSpec DS;
887 FieldDeclarators.clear();
888 ParseStructDeclaration(DS, FieldDeclarators);
889
890 // Convert them all to fields.
891 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
892 FieldDeclarator &FD = FieldDeclarators[i];
893 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000894 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000895 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000896 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000897 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000898 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000899
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000900 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000901 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000902 } else {
903 Diag(Tok, diag::err_expected_semi_decl_list);
904 // Skip to end of block or statement
905 SkipUntil(tok::r_brace, true, true);
906 }
907 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000908 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000909 // Call ActOnFields() even if we don't have any decls. This is useful
910 // for code rewriting tools that need to be aware of the empty list.
911 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
912 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000913 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000914 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000915}
Steve Narofffb367882007-08-20 21:31:48 +0000916
917/// objc-protocol-declaration:
918/// objc-protocol-definition
919/// objc-protocol-forward-reference
920///
921/// objc-protocol-definition:
922/// @protocol identifier
923/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000924/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000925/// @end
926///
927/// objc-protocol-forward-reference:
928/// @protocol identifier-list ';'
929///
930/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000931/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000932/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000933Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
934 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000935 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000936 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
937 ConsumeToken(); // the "protocol" identifier
938
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000939 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000940 Diag(Tok, diag::err_expected_ident); // missing protocol name.
941 return 0;
942 }
943 // Save the protocol name, then consume it.
944 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
945 SourceLocation nameLoc = ConsumeToken();
946
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000947 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000948 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000949 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000950 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000951 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000952
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000953 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000954 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
955 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
956
Steve Naroff72f17fb2007-08-22 22:17:26 +0000957 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000958 while (1) {
959 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000960 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000961 Diag(Tok, diag::err_expected_ident);
962 SkipUntil(tok::semi);
963 return 0;
964 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000965 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
966 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000967 ConsumeToken(); // the identifier
968
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000969 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000970 break;
971 }
972 // Consume the ';'.
973 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
974 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000975
Steve Naroff415c1832007-10-10 17:32:04 +0000976 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000977 &ProtocolRefs[0],
978 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000979 }
980
Steve Naroff72f17fb2007-08-22 22:17:26 +0000981 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000982 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000983
Chris Lattner2bdedd62008-07-26 04:03:38 +0000984 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000985 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000986 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000987 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000988
Chris Lattner2bdedd62008-07-26 04:03:38 +0000989 DeclTy *ProtoType =
990 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
991 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000992 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000993 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000994
995 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000996 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000997 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000998 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000999 }
1000 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +00001001 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001002}
Steve Narofffb367882007-08-20 21:31:48 +00001003
1004/// objc-implementation:
1005/// objc-class-implementation-prologue
1006/// objc-category-implementation-prologue
1007///
1008/// objc-class-implementation-prologue:
1009/// @implementation identifier objc-superclass[opt]
1010/// objc-class-instance-variables[opt]
1011///
1012/// objc-category-implementation-prologue:
1013/// @implementation identifier ( identifier )
1014
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001015Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1016 SourceLocation atLoc) {
1017 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1018 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1019 ConsumeToken(); // the "implementation" identifier
1020
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001021 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001022 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1023 return 0;
1024 }
1025 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001026 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001027 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1028
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001029 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001030 // we have a category implementation.
1031 SourceLocation lparenLoc = ConsumeParen();
1032 SourceLocation categoryLoc, rparenLoc;
1033 IdentifierInfo *categoryId = 0;
1034
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001035 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001036 categoryId = Tok.getIdentifierInfo();
1037 categoryLoc = ConsumeToken();
1038 } else {
1039 Diag(Tok, diag::err_expected_ident); // missing category name.
1040 return 0;
1041 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001042 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001043 Diag(Tok, diag::err_expected_rparen);
1044 SkipUntil(tok::r_paren, false); // don't stop at ';'
1045 return 0;
1046 }
1047 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001048 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001049 atLoc, nameId, nameLoc, categoryId,
1050 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001052 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001053 }
1054 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001055 SourceLocation superClassLoc;
1056 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001058 // We have a super class
1059 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001060 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001061 Diag(Tok, diag::err_expected_ident); // missing super class name.
1062 return 0;
1063 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001064 superClassId = Tok.getIdentifierInfo();
1065 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001066 }
Steve Naroff415c1832007-10-10 17:32:04 +00001067 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001068 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001069 superClassId, superClassLoc);
1070
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001071 if (Tok.is(tok::l_brace)) // we have ivars
1072 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001073 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001074
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001075 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001076}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001077
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001078Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1079 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1080 "ParseObjCAtEndDeclaration(): Expected @end");
1081 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001082 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001083 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001084 else
1085 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001086 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001087}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001088
1089/// compatibility-alias-decl:
1090/// @compatibility_alias alias-name class-name ';'
1091///
1092Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1093 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1094 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1095 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001097 Diag(Tok, diag::err_expected_ident);
1098 return 0;
1099 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001100 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1101 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001102 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001103 Diag(Tok, diag::err_expected_ident);
1104 return 0;
1105 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001106 IdentifierInfo *classId = Tok.getIdentifierInfo();
1107 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1108 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001109 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001110 return 0;
1111 }
1112 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1113 aliasId, aliasLoc,
1114 classId, classLoc);
1115 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001116}
1117
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001118/// property-synthesis:
1119/// @synthesize property-ivar-list ';'
1120///
1121/// property-ivar-list:
1122/// property-ivar
1123/// property-ivar-list ',' property-ivar
1124///
1125/// property-ivar:
1126/// identifier
1127/// identifier '=' identifier
1128///
1129Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1130 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1131 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001133 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001134 Diag(Tok, diag::err_expected_ident);
1135 return 0;
1136 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001137 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001138 IdentifierInfo *propertyIvar = 0;
1139 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1140 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001141 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001142 // property '=' ivar-name
1143 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001144 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001145 Diag(Tok, diag::err_expected_ident);
1146 break;
1147 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001148 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001149 ConsumeToken(); // consume ivar-name
1150 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001151 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1152 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001153 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001154 break;
1155 ConsumeToken(); // consume ','
1156 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001157 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001158 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1159 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001160}
1161
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001162/// property-dynamic:
1163/// @dynamic property-list
1164///
1165/// property-list:
1166/// identifier
1167/// property-list ',' identifier
1168///
1169Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1170 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1171 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1172 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001173 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001174 Diag(Tok, diag::err_expected_ident);
1175 return 0;
1176 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001177 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001178 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1179 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1180 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1181 propertyId, 0);
1182
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001183 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001184 break;
1185 ConsumeToken(); // consume ','
1186 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001187 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001188 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1189 return 0;
1190}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001191
1192/// objc-throw-statement:
1193/// throw expression[opt];
1194///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001195Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1196 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001197 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001198 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001199 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001200 if (Res.isInvalid) {
1201 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001202 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001203 }
1204 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001205 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001206 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001207}
1208
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001209/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001210/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001211///
1212Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001213 ConsumeToken(); // consume synchronized
1214 if (Tok.isNot(tok::l_paren)) {
1215 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1216 return true;
1217 }
1218 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001219 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001220 if (Res.isInvalid) {
1221 SkipUntil(tok::semi);
1222 return true;
1223 }
1224 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001225 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001226 return true;
1227 }
1228 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001229 if (Tok.isNot(tok::l_brace)) {
1230 Diag (Tok, diag::err_expected_lbrace);
1231 return true;
1232 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001233 // Enter a scope to hold everything within the compound stmt. Compound
1234 // statements can always hold declarations.
1235 EnterScope(Scope::DeclScope);
1236
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001237 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001238
1239 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001240 if (SynchBody.isInvalid)
1241 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1242 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001243}
1244
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001245/// objc-try-catch-statement:
1246/// @try compound-statement objc-catch-list[opt]
1247/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1248///
1249/// objc-catch-list:
1250/// @catch ( parameter-declaration ) compound-statement
1251/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1252/// catch-parameter-declaration:
1253/// parameter-declaration
1254/// '...' [OBJC2]
1255///
Chris Lattner80712392008-03-10 06:06:04 +00001256Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001257 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001258
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001259 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001260 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001261 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001262 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001263 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001264 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001265 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001266 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001267 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001268 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001269 if (TryBody.isInvalid)
1270 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001271
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001272 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001273 // At this point, we need to lookahead to determine if this @ is the start
1274 // of an @catch or @finally. We don't want to consume the @ token if this
1275 // is an @try or @encode or something else.
1276 Token AfterAt = GetLookAheadToken(1);
1277 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1278 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1279 break;
1280
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001281 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001282 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001283 StmtTy *FirstPart = 0;
1284 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001285 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001286 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001287 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001288 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001289 DeclSpec DS;
1290 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001291 // For some odd reason, the name of the exception variable is
1292 // optional. As a result, we need to use PrototypeContext.
1293 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001294 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001295 if (DeclaratorInfo.getIdentifier()) {
1296 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001297 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001298 StmtResult stmtResult =
1299 Actions.ActOnDeclStmt(aBlockVarDecl,
1300 DS.getSourceRange().getBegin(),
1301 DeclaratorInfo.getSourceRange().getEnd());
1302 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1303 }
Steve Naroffc949a462008-02-05 21:27:35 +00001304 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001305 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001306 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001307
1308 StmtResult CatchBody(true);
1309 if (Tok.is(tok::l_brace))
1310 CatchBody = ParseCompoundStatementBody();
1311 else
1312 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001313 if (CatchBody.isInvalid)
1314 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001315 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001316 FirstPart, CatchBody.Val, CatchStmts.Val);
1317 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001318 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001319 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1320 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001321 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001322 }
1323 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001324 } else {
1325 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001326 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001327 EnterScope(Scope::DeclScope);
1328
Chris Lattner8027be62008-02-14 19:27:54 +00001329
1330 StmtResult FinallyBody(true);
1331 if (Tok.is(tok::l_brace))
1332 FinallyBody = ParseCompoundStatementBody();
1333 else
1334 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001335 if (FinallyBody.isInvalid)
1336 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001337 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001338 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001339 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001340 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001341 break;
1342 }
1343 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001344 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001345 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001346 return true;
1347 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001348 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001349 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001350}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001351
Steve Naroff81f1bba2007-09-06 21:24:23 +00001352/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001353///
Steve Naroff18c83382007-11-13 23:01:27 +00001354Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001355 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001356 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001357 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001358 ConsumeToken();
1359
Steve Naroff9191a9e82007-11-11 19:54:21 +00001360 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001361 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001362 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001363
1364 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1365 SkipUntil(tok::l_brace, true, true);
1366
1367 // If we didn't find the '{', bail out.
1368 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001369 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001370 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001371 SourceLocation BraceLoc = Tok.getLocation();
1372
1373 // Enter a scope for the method body.
1374 EnterScope(Scope::FnScope|Scope::DeclScope);
1375
1376 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001377 // specified Declarator for the method.
1378 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001379
1380 StmtResult FnBody = ParseCompoundStatementBody();
1381
1382 // If the function body could not be parsed, make a bogus compoundstmt.
1383 if (FnBody.isInvalid)
1384 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1385
1386 // Leave the function body scope.
1387 ExitScope();
1388
1389 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001390 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001391 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001392}
Anders Carlssona66cad42007-08-21 17:43:55 +00001393
Steve Naroffc949a462008-02-05 21:27:35 +00001394Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1395 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001396 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001397 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1398 return ParseObjCThrowStmt(AtLoc);
1399 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1400 return ParseObjCSynchronizedStmt(AtLoc);
1401 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1402 if (Res.isInvalid) {
1403 // If the expression is invalid, skip ahead to the next semicolon. Not
1404 // doing this opens us up to the possibility of infinite loops if
1405 // ParseExpression does not consume any tokens.
1406 SkipUntil(tok::semi);
1407 return true;
1408 }
1409 // Otherwise, eat the semicolon.
1410 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1411 return Actions.ActOnExprStmt(Res.Val);
1412}
1413
Steve Narofffb9dd752007-10-15 20:55:58 +00001414Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001415 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001416 case tok::string_literal: // primary-expression: string-literal
1417 case tok::wide_string_literal:
1418 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1419 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001420 if (Tok.getIdentifierInfo() == 0)
1421 return Diag(AtLoc, diag::err_unexpected_at);
1422
1423 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1424 case tok::objc_encode:
1425 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1426 case tok::objc_protocol:
1427 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1428 case tok::objc_selector:
1429 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1430 default:
1431 return Diag(AtLoc, diag::err_unexpected_at);
1432 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001433 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001434}
1435
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001436/// objc-message-expr:
1437/// '[' objc-receiver objc-message-args ']'
1438///
1439/// objc-receiver:
1440/// expression
1441/// class-name
1442/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001443Parser::ExprResult Parser::ParseObjCMessageExpression() {
1444 assert(Tok.is(tok::l_square) && "'[' expected");
1445 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1446
1447 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001448 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001449 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1450 ConsumeToken();
1451 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1452 }
1453
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001454 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001455 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001456 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001457 return Res;
1458 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001459
Chris Lattnered27a532008-01-25 18:59:06 +00001460 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1461}
1462
1463/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1464/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001465///
1466/// objc-message-args:
1467/// objc-selector
1468/// objc-keywordarg-list
1469///
1470/// objc-keywordarg-list:
1471/// objc-keywordarg
1472/// objc-keywordarg-list objc-keywordarg
1473///
1474/// objc-keywordarg:
1475/// selector-name[opt] ':' objc-keywordexpr
1476///
1477/// objc-keywordexpr:
1478/// nonempty-expr-list
1479///
1480/// nonempty-expr-list:
1481/// assignment-expression
1482/// nonempty-expr-list , assignment-expression
1483///
Chris Lattnered27a532008-01-25 18:59:06 +00001484Parser::ExprResult
1485Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1486 IdentifierInfo *ReceiverName,
1487 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001488 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001489 SourceLocation Loc;
1490 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001491
1492 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1493 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1494
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001495 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001496 while (1) {
1497 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001498 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001499
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001500 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001501 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001502 // We must manually skip to a ']', otherwise the expression skipper will
1503 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1504 // the enclosing expression.
1505 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001506 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001507 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001508
Steve Naroff4ed9d662007-09-27 14:38:14 +00001509 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001510 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001511 ExprResult Res = ParseAssignmentExpression();
1512 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001513 // We must manually skip to a ']', otherwise the expression skipper will
1514 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1515 // the enclosing expression.
1516 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001517 return Res;
1518 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001519
Steve Naroff253118b2007-09-17 20:25:27 +00001520 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001521 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001522
1523 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001524 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001525 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001526 break;
1527 // We have a selector or a colon, continue parsing.
1528 }
1529 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001530 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001531 ConsumeToken(); // Eat the ','.
1532 /// Parse the expression after ','
1533 ExprResult Res = ParseAssignmentExpression();
1534 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001535 // We must manually skip to a ']', otherwise the expression skipper will
1536 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1537 // the enclosing expression.
1538 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001539 return Res;
1540 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001541
Steve Naroff9f176d12007-11-15 13:05:42 +00001542 // We have a valid expression.
1543 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001544 }
1545 } else if (!selIdent) {
1546 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001547
1548 // We must manually skip to a ']', otherwise the expression skipper will
1549 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1550 // the enclosing expression.
1551 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001552 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001553 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001554
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001555 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001556 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001557 // We must manually skip to a ']', otherwise the expression skipper will
1558 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1559 // the enclosing expression.
1560 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001561 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001562 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001563
Chris Lattnered27a532008-01-25 18:59:06 +00001564 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001565
Steve Narofff9e80db2007-10-05 18:42:47 +00001566 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001567 if (nKeys == 0)
1568 KeyIdents.push_back(selIdent);
1569 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1570
1571 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001572 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001573 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001574 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001575 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001576 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001577 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001578}
1579
Steve Naroff0add5d22007-11-03 11:27:19 +00001580Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001581 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001582 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001583
1584 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1585 // expressions. At this point, we know that the only valid thing that starts
1586 // with '@' is an @"".
1587 llvm::SmallVector<SourceLocation, 4> AtLocs;
1588 llvm::SmallVector<ExprTy*, 4> AtStrings;
1589 AtLocs.push_back(AtLoc);
1590 AtStrings.push_back(Res.Val);
1591
1592 while (Tok.is(tok::at)) {
1593 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001594
Chris Lattnerddd3e632007-12-12 01:04:12 +00001595 ExprResult Res(true); // Invalid unless there is a string literal.
1596 if (isTokenStringLiteral())
1597 Res = ParseStringLiteralExpression();
1598 else
1599 Diag(Tok, diag::err_objc_concat_string);
1600
1601 if (Res.isInvalid) {
1602 while (!AtStrings.empty()) {
1603 Actions.DeleteExpr(AtStrings.back());
1604 AtStrings.pop_back();
1605 }
1606 return Res;
1607 }
1608
1609 AtStrings.push_back(Res.Val);
1610 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001611
Chris Lattnerddd3e632007-12-12 01:04:12 +00001612 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1613 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001614}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001615
1616/// objc-encode-expression:
1617/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001618Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001619 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001620
1621 SourceLocation EncLoc = ConsumeToken();
1622
Chris Lattnerf9311a92008-08-05 06:19:09 +00001623 if (Tok.isNot(tok::l_paren))
1624 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001625
1626 SourceLocation LParenLoc = ConsumeParen();
1627
1628 TypeTy *Ty = ParseTypeName();
1629
Anders Carlsson92faeb82007-08-23 15:31:37 +00001630 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001631
Chris Lattnercfd61c82007-10-16 22:51:17 +00001632 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001633 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001634}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001635
1636/// objc-protocol-expression
1637/// @protocol ( protocol-name )
1638
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001639Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001640{
1641 SourceLocation ProtoLoc = ConsumeToken();
1642
Chris Lattnerf9311a92008-08-05 06:19:09 +00001643 if (Tok.isNot(tok::l_paren))
1644 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001645
1646 SourceLocation LParenLoc = ConsumeParen();
1647
Chris Lattnerf9311a92008-08-05 06:19:09 +00001648 if (Tok.isNot(tok::identifier))
1649 return Diag(Tok, diag::err_expected_ident);
1650
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001651 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001652 ConsumeToken();
1653
Anders Carlsson92faeb82007-08-23 15:31:37 +00001654 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001655
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001656 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1657 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001658}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001659
1660/// objc-selector-expression
1661/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001662Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001663{
1664 SourceLocation SelectorLoc = ConsumeToken();
1665
Chris Lattnerf9311a92008-08-05 06:19:09 +00001666 if (Tok.isNot(tok::l_paren))
1667 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001668
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001669 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001670 SourceLocation LParenLoc = ConsumeParen();
1671 SourceLocation sLoc;
1672 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001673 if (!SelIdent && Tok.isNot(tok::colon))
1674 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1675
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001676 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001677 unsigned nColons = 0;
1678 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001679 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001680 if (Tok.isNot(tok::colon))
1681 return Diag(Tok, diag::err_expected_colon);
1682
Chris Lattner847f5c12007-12-27 19:57:00 +00001683 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001684 ConsumeToken(); // Eat the ':'.
1685 if (Tok.is(tok::r_paren))
1686 break;
1687 // Check for another keyword selector.
1688 SourceLocation Loc;
1689 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001690 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001691 if (!SelIdent && Tok.isNot(tok::colon))
1692 break;
1693 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001694 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001695 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001696 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001697 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001698 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001699 }