blob: e3230c48924286801a355e3c38afbd80639ddfe8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff91fa0b72007-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 Naroffdac269b2007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff861cf3e2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
Steve Naroffdac269b2007-08-20 21:31:48 +000037 return ParseObjCAtInterfaceDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000038 case tok::objc_protocol:
Steve Naroff7ef58fd2007-08-22 22:17:26 +000039 return ParseObjCAtProtocolDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000040 case tok::objc_implementation:
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +000041 return ParseObjCAtImplementationDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000042 case tok::objc_end:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000043 return ParseObjCAtEndDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000044 case tok::objc_compatibility_alias:
Fariborz Jahaniane992af02007-09-04 19:26:51 +000045 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000046 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000053 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Naroffdac269b2007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000069 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
Reid Spencer5f016e22007-07-11 17:01:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnerdf195262007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000082 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000083
Steve Naroffe440eb82007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Steve Naroffdac269b2007-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 Naroff861cf3e2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnerdf195262007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-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 Naroff7ef58fd2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnerdf195262007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000134 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffdac269b2007-08-20 21:31:48 +0000135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Steve Narofff908a872007-10-30 02:23:23 +0000150 SourceLocation endProtoLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattnerdf195262007-10-09 17:51:17 +0000152 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000153 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-08-20 21:31:48 +0000154 return 0;
155 }
156 if (attrList) // categories don't support attributes.
157 Diag(Tok, diag::err_objc_no_attributes_on_category);
158
Steve Naroffe440eb82007-10-10 17:32:04 +0000159 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000160 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000161 &ProtocolRefs[0], ProtocolRefs.size(),
162 endProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000163
164 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Naroffdac269b2007-08-20 21:31:48 +0000165
Steve Naroff294494e2007-08-22 16:35:03 +0000166 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000167 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000168 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000169 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 }
Steve Naroff294494e2007-08-22 16:35:03 +0000171 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000172 return 0;
173 }
174 // Parse a class interface.
175 IdentifierInfo *superClassId = 0;
176 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000177
Chris Lattnerdf195262007-10-09 17:51:17 +0000178 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000179 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing super class name.
182 return 0;
183 }
184 superClassId = Tok.getIdentifierInfo();
185 superClassLoc = ConsumeToken();
186 }
187 // Next, we need to check for any protocol references.
Steve Narofff28b2642007-09-05 23:30:30 +0000188 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000189 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000190 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000191 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-08-20 21:31:48 +0000192 return 0;
193 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000194 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000195 atLoc, nameId, nameLoc,
Steve Narofff28b2642007-09-05 23:30:30 +0000196 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000197 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000198
Chris Lattnerdf195262007-10-09 17:51:17 +0000199 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000200 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000201
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000202 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff294494e2007-08-22 16:35:03 +0000203
204 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000205 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000206 ConsumeToken(); // the "end" identifier
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000207 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000208 }
Steve Naroff294494e2007-08-22 16:35:03 +0000209 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000210 return 0;
211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff294494e2007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000225void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
226 tok::ObjCKeywordKind contextKey) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000227 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000228 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000229 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000230 SourceLocation AtEndLoc;
231
Steve Naroff294494e2007-08-22 16:35:03 +0000232 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 if (Tok.is(tok::at)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000234 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff861cf3e2007-08-23 18:16:40 +0000235 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff294494e2007-08-22 16:35:03 +0000236
237 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff60fccee2007-10-29 21:38:07 +0000238 AtEndLoc = AtLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000239 break;
Steve Naroff294494e2007-08-22 16:35:03 +0000240 } else if (ocKind == tok::objc_required) { // protocols only
241 ConsumeToken();
Fariborz Jahanian00933592007-09-18 00:25:23 +0000242 MethodImplKind = ocKind;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000243 if (contextKey != tok::objc_protocol)
244 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff294494e2007-08-22 16:35:03 +0000245 } else if (ocKind == tok::objc_optional) { // protocols only
246 ConsumeToken();
Fariborz Jahanian00933592007-09-18 00:25:23 +0000247 MethodImplKind = ocKind;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000248 if (contextKey != tok::objc_protocol)
249 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff294494e2007-08-22 16:35:03 +0000250 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000251 allProperties.push_back(ParseObjCPropertyDecl(interfaceDecl, AtLoc));
Steve Naroff294494e2007-08-22 16:35:03 +0000252 continue;
253 } else {
254 Diag(Tok, diag::err_objc_illegal_interface_qual);
255 ConsumeToken();
256 }
257 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000258 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
259 DeclTy *methodPrototype =
260 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000261 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000262 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
263 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000264 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000265 continue;
266 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000267 else if (Tok.is(tok::at))
268 continue;
269
Chris Lattnerdf195262007-10-09 17:51:17 +0000270 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000271 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000272 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000273 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000274 else {
Steve Naroff4985ace2007-08-22 18:35:33 +0000275 // FIXME: as the name implies, this rule allows function definitions.
276 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000277 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000278 }
Steve Naroff294494e2007-08-22 16:35:03 +0000279 }
Steve Naroff2feac5e2007-10-30 03:43:13 +0000280 /// Insert collected methods declarations into the @interface object.
Steve Naroff0416fb92007-11-11 17:19:15 +0000281 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
282 &allProperties[0], allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000283}
284
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000285/// Parse property attribute declarations.
286///
287/// property-attr-decl: '(' property-attrlist ')'
288/// property-attrlist:
289/// property-attribute
290/// property-attrlist ',' property-attribute
291/// property-attribute:
292/// getter '=' identifier
293/// setter '=' identifier ':'
294/// readonly
295/// readwrite
296/// assign
297/// retain
298/// copy
299/// nonatomic
300///
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000301void Parser::ParseObjCPropertyAttribute (ObjcDeclSpec &DS) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000302 SourceLocation loc = ConsumeParen(); // consume '('
303 while (isObjCPropertyAttribute()) {
304 const IdentifierInfo *II = Tok.getIdentifierInfo();
305 // getter/setter require extra treatment.
306 if (II == ObjcPropertyAttrs[objc_getter] ||
307 II == ObjcPropertyAttrs[objc_setter]) {
308 // skip getter/setter part.
309 SourceLocation loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000310 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000311 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000312 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000313 if (II == ObjcPropertyAttrs[objc_setter]) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000314 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_setter);
315 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000316 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000317 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000318 Diag(loc, diag::err_expected_colon);
319 SkipUntil(tok::r_paren,true,true);
320 break;
321 }
322 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000323 else {
324 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_getter);
325 DS.setGetterName(Tok.getIdentifierInfo());
326 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000327 }
328 else {
329 Diag(loc, diag::err_expected_ident);
330 SkipUntil(tok::r_paren,true,true);
331 break;
332 }
333 }
334 else {
335 Diag(loc, diag::err_objc_expected_equal);
336 SkipUntil(tok::r_paren,true,true);
337 break;
338 }
339 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000340
341 else if (II == ObjcPropertyAttrs[objc_readonly])
342 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_readonly);
343 else if (II == ObjcPropertyAttrs[objc_assign])
344 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_assign);
345 else if (II == ObjcPropertyAttrs[objc_readwrite])
346 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_readwrite);
347 else if (II == ObjcPropertyAttrs[objc_retain])
348 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_retain);
349 else if (II == ObjcPropertyAttrs[objc_copy])
350 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_copy);
351 else if (II == ObjcPropertyAttrs[objc_nonatomic])
352 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_nonatomic);
353
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000354 ConsumeToken(); // consume last attribute token
Chris Lattnerdf195262007-10-09 17:51:17 +0000355 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000356 loc = ConsumeToken();
357 continue;
358 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000359 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000360 break;
361 Diag(loc, diag::err_expected_rparen);
362 SkipUntil(tok::semi);
363 return;
364 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000365 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000366 ConsumeParen();
367 else {
368 Diag(loc, diag::err_objc_expected_property_attr);
369 SkipUntil(tok::r_paren); // recover from error inside attribute list
370 }
371}
372
373/// Main routine to parse property declaration.
374///
375/// @property property-attr-decl[opt] property-component-decl ';'
376///
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000377Parser::DeclTy *Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl,
378 SourceLocation AtLoc) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000379 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
380 "ParseObjCPropertyDecl(): Expected @property");
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000381 ObjcDeclSpec DS;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000382 ConsumeToken(); // the "property" identifier
383 // Parse property attribute list, if any.
Chris Lattnerdf195262007-10-09 17:51:17 +0000384 if (Tok.is(tok::l_paren)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000385 // property has attribute list.
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000386 ParseObjCPropertyAttribute(DS);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000387 }
388 // Parse declaration portion of @property.
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000389 llvm::SmallVector<DeclTy*, 8> PropertyDecls;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000390 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnerdf195262007-10-09 17:51:17 +0000391 if (Tok.is(tok::semi))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000392 ConsumeToken();
393 else {
394 Diag(Tok, diag::err_expected_semi_decl_list);
395 SkipUntil(tok::r_brace, true, true);
396 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000397 return Actions.ActOnAddObjcProperties(AtLoc,
398 &PropertyDecls[0], PropertyDecls.size(), DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
Steve Naroffdac269b2007-08-20 21:31:48 +0000400
Steve Naroff3536b442007-09-06 21:24:23 +0000401/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000402/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000403/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000404///
405/// objc-instance-method: '-'
406/// objc-class-method: '+'
407///
Steve Naroff4985ace2007-08-22 18:35:33 +0000408/// objc-method-attributes: [OBJC2]
409/// __attribute__((deprecated))
410///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000411Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
412 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000413 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000414
415 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000416 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000417
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000418 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000419 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000420 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000421 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000422}
423
424/// objc-selector:
425/// identifier
426/// one of
427/// enum struct union if else while do for switch case default
428/// break continue return goto asm sizeof typeof __alignof
429/// unsigned long const short volatile signed restrict _Complex
430/// in out inout bycopy byref oneway int char float double void _Bool
431///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000432IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000433 switch (Tok.getKind()) {
434 default:
435 return 0;
436 case tok::identifier:
437 case tok::kw_typeof:
438 case tok::kw___alignof:
439 case tok::kw_auto:
440 case tok::kw_break:
441 case tok::kw_case:
442 case tok::kw_char:
443 case tok::kw_const:
444 case tok::kw_continue:
445 case tok::kw_default:
446 case tok::kw_do:
447 case tok::kw_double:
448 case tok::kw_else:
449 case tok::kw_enum:
450 case tok::kw_extern:
451 case tok::kw_float:
452 case tok::kw_for:
453 case tok::kw_goto:
454 case tok::kw_if:
455 case tok::kw_inline:
456 case tok::kw_int:
457 case tok::kw_long:
458 case tok::kw_register:
459 case tok::kw_restrict:
460 case tok::kw_return:
461 case tok::kw_short:
462 case tok::kw_signed:
463 case tok::kw_sizeof:
464 case tok::kw_static:
465 case tok::kw_struct:
466 case tok::kw_switch:
467 case tok::kw_typedef:
468 case tok::kw_union:
469 case tok::kw_unsigned:
470 case tok::kw_void:
471 case tok::kw_volatile:
472 case tok::kw_while:
Chris Lattner9298d962007-11-15 05:25:19 +0000473 case tok::kw_bool:
Chris Lattnerff384912007-10-07 02:00:24 +0000474 case tok::kw__Bool:
475 case tok::kw__Complex:
476 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000477 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000478 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000479 }
Steve Naroff294494e2007-08-22 16:35:03 +0000480}
481
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000482/// property-attrlist: one of
483/// readonly getter setter assign retain copy nonatomic
484///
485bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000486 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000487 const IdentifierInfo *II = Tok.getIdentifierInfo();
488 for (unsigned i = 0; i < objc_NumAttrs; ++i)
489 if (II == ObjcPropertyAttrs[i]) return true;
490 }
491 return false;
492}
493
Chris Lattnere8b724d2007-12-12 06:56:32 +0000494/// ParseObjcTypeQualifierList - This routine parses the objective-c's type
495/// qualifier list and builds their bitmask representation in the input
496/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000497///
498/// objc-type-qualifiers:
499/// objc-type-qualifier
500/// objc-type-qualifiers objc-type-qualifier
501///
Chris Lattnere8b724d2007-12-12 06:56:32 +0000502void Parser::ParseObjcTypeQualifierList(ObjcDeclSpec &DS) {
503 while (1) {
504 if (!Tok.is(tok::identifier))
505 return;
506
507 const IdentifierInfo *II = Tok.getIdentifierInfo();
508 for (unsigned i = 0; i != objc_NumQuals; ++i) {
509 if (II != ObjcTypeQuals[i])
510 continue;
511
512 ObjcDeclSpec::ObjcDeclQualifier Qual;
513 switch (i) {
514 default: assert(0 && "Unknown decl qualifier");
515 case objc_in: Qual = ObjcDeclSpec::DQ_In; break;
516 case objc_out: Qual = ObjcDeclSpec::DQ_Out; break;
517 case objc_inout: Qual = ObjcDeclSpec::DQ_Inout; break;
518 case objc_oneway: Qual = ObjcDeclSpec::DQ_Oneway; break;
519 case objc_bycopy: Qual = ObjcDeclSpec::DQ_Bycopy; break;
520 case objc_byref: Qual = ObjcDeclSpec::DQ_Byref; break;
521 }
522 DS.setObjcDeclQualifier(Qual);
523 ConsumeToken();
524 II = 0;
525 break;
526 }
527
528 // If this wasn't a recognized qualifier, bail out.
529 if (II) return;
530 }
531}
532
533/// objc-type-name:
534/// '(' objc-type-qualifiers[opt] type-name ')'
535/// '(' objc-type-qualifiers[opt] ')'
536///
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000537Parser::TypeTy *Parser::ParseObjCTypeName(ObjcDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000538 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000539
540 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000541 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000542
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000543 // Parse type qualifiers, in, inout, etc.
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000544 ParseObjcTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000545
Steve Naroff294494e2007-08-22 16:35:03 +0000546 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000547 Ty = ParseTypeName();
548 // FIXME: back when Sema support is in place...
549 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000550 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000551 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000552 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000553 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000554 }
555 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000556 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000557}
558
559/// objc-method-decl:
560/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000561/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000562/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000563/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000564///
565/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000566/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000567/// objc-keyword-selector objc-keyword-decl
568///
569/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000570/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
571/// objc-selector ':' objc-keyword-attributes[opt] identifier
572/// ':' objc-type-name objc-keyword-attributes[opt] identifier
573/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000574///
Steve Naroff4985ace2007-08-22 18:35:33 +0000575/// objc-parmlist:
576/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000577///
Steve Naroff4985ace2007-08-22 18:35:33 +0000578/// objc-parms:
579/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000580///
Steve Naroff4985ace2007-08-22 18:35:33 +0000581/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000582/// , ...
583///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000584/// objc-keyword-attributes: [OBJC2]
585/// __attribute__((unused))
586///
Steve Naroffbef11852007-10-26 20:53:56 +0000587Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000588 tok::TokenKind mType,
589 DeclTy *IDecl,
Steve Naroff68d331a2007-09-27 14:38:14 +0000590 tok::ObjCKeywordKind MethodImplKind)
591{
Steve Naroff4985ace2007-08-22 18:35:33 +0000592 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000593 TypeTy *ReturnType = 0;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000594 ObjcDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000595 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000596 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroffbef11852007-10-26 20:53:56 +0000597 SourceLocation selLoc;
598 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000599 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000600 if (!SelIdent) {
601 Diag(Tok, diag::err_expected_ident); // missing selector name.
602 // FIXME: this creates a unary selector with a null identifier, is this
603 // ok?? Maybe we should skip to the next semicolon or something.
604 }
605
606 // If attributes exist after the method, parse them.
607 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000608 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000609 MethodAttrs = ParseAttributes();
610
611 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000612 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000613 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000614 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000615 }
Steve Narofff28b2642007-09-05 23:30:30 +0000616
Steve Naroff68d331a2007-09-27 14:38:14 +0000617 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
618 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000619 llvm::SmallVector<ObjcDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000620 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000621
622 Action::TypeTy *TypeInfo;
623 while (1) {
624 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000625
Chris Lattnerff384912007-10-07 02:00:24 +0000626 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000627 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000628 Diag(Tok, diag::err_expected_colon);
629 break;
630 }
631 ConsumeToken(); // Eat the ':'.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000632 ObjcDeclSpec DSType;
633 if (Tok.is(tok::l_paren)) { // Parse the argument type.
634 TypeInfo = ParseObjCTypeName(DSType);
635 }
Chris Lattnerff384912007-10-07 02:00:24 +0000636 else
637 TypeInfo = 0;
638 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000639 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000640
Chris Lattnerff384912007-10-07 02:00:24 +0000641 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000642 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000643 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000644
Chris Lattnerdf195262007-10-09 17:51:17 +0000645 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000646 Diag(Tok, diag::err_expected_ident); // missing argument name.
647 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000648 }
Chris Lattnerff384912007-10-07 02:00:24 +0000649 ArgNames.push_back(Tok.getIdentifierInfo());
650 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000651
Chris Lattnerff384912007-10-07 02:00:24 +0000652 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000653 SourceLocation Loc;
654 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000655 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000656 break;
657 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000658 }
Chris Lattnerff384912007-10-07 02:00:24 +0000659
Steve Naroff335eafa2007-11-15 12:35:21 +0000660 bool isVariadic = false;
661
Chris Lattnerff384912007-10-07 02:00:24 +0000662 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000663 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000664 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000665 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000666 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000667 ConsumeToken();
668 break;
669 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000670 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000671 // Parse the c-style argument declaration-specifier.
672 DeclSpec DS;
673 ParseDeclarationSpecifiers(DS);
674 // Parse the declarator.
675 Declarator ParmDecl(DS, Declarator::PrototypeContext);
676 ParseDeclarator(ParmDecl);
677 }
678
679 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000680 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000681 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000682 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000683 MethodAttrs = ParseAttributes();
684
685 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
686 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000687 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000688 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000689 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000690 &ArgNames[0], MethodAttrs,
691 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000692}
693
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000694/// CmpProtocolVals - Comparison predicate for sorting protocols.
695static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
696 const IdentifierInfo* const& rhs) {
697 return strcmp(lhs->getName(), rhs->getName()) < 0;
698}
699
Steve Naroffdac269b2007-08-20 21:31:48 +0000700/// objc-protocol-refs:
701/// '<' identifier-list '>'
702///
Steve Narofff28b2642007-09-05 23:30:30 +0000703bool Parser::ParseObjCProtocolReferences(
Steve Narofff908a872007-10-30 02:23:23 +0000704 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
705{
Chris Lattnerdf195262007-10-09 17:51:17 +0000706 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000707
708 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000709
710 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000711 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000712 Diag(Tok, diag::err_expected_ident);
713 SkipUntil(tok::greater);
714 return true;
715 }
716 ProtocolRefs.push_back(Tok.getIdentifierInfo());
717 ConsumeToken();
718
Chris Lattnerdf195262007-10-09 17:51:17 +0000719 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000720 break;
721 ConsumeToken();
722 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000723
724 // Sort protocols, keyed by name.
725 // Later on, we remove duplicates.
726 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
727
728 // Make protocol names unique.
729 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
730 ProtocolRefs.end());
Steve Naroffdac269b2007-08-20 21:31:48 +0000731 // Consume the '>'.
Steve Narofff908a872007-10-30 02:23:23 +0000732 if (Tok.is(tok::greater)) {
733 endLoc = ConsumeAnyToken();
734 return false;
735 }
736 Diag(Tok, diag::err_expected_greater);
737 return true;
Steve Naroffdac269b2007-08-20 21:31:48 +0000738}
739
740/// objc-class-instance-variables:
741/// '{' objc-instance-variable-decl-list[opt] '}'
742///
743/// objc-instance-variable-decl-list:
744/// objc-visibility-spec
745/// objc-instance-variable-decl ';'
746/// ';'
747/// objc-instance-variable-decl-list objc-visibility-spec
748/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
749/// objc-instance-variable-decl-list ';'
750///
751/// objc-visibility-spec:
752/// @private
753/// @protected
754/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000755/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000756///
757/// objc-instance-variable-decl:
758/// struct-declaration
759///
Steve Naroff60fccee2007-10-29 21:38:07 +0000760void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
761 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000762 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff3536b442007-09-06 21:24:23 +0000763 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000764 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
765 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffddbff782007-08-21 21:17:12 +0000766
767 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000768
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000769 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffddbff782007-08-21 21:17:12 +0000770 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000771 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000772 // Each iteration of this loop reads one objc-instance-variable-decl.
773
774 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000775 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000776 Diag(Tok, diag::ext_extra_struct_semi);
777 ConsumeToken();
778 continue;
779 }
780 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000781 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000782 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000783 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000784 case tok::objc_private:
785 case tok::objc_public:
786 case tok::objc_protected:
787 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000788 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000789 ConsumeToken();
790 continue;
791 default:
792 Diag(Tok, diag::err_objc_illegal_visibility_spec);
793 ConsumeToken();
794 continue;
795 }
796 }
797 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000798 for (unsigned i = 0; i < IvarDecls.size(); i++) {
799 AllIvarDecls.push_back(IvarDecls[i]);
800 AllVisibilities.push_back(visibility);
801 }
Steve Naroff3536b442007-09-06 21:24:23 +0000802 IvarDecls.clear();
803
Chris Lattnerdf195262007-10-09 17:51:17 +0000804 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000805 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000806 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000807 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
808 break;
809 } else {
810 Diag(Tok, diag::err_expected_semi_decl_list);
811 // Skip to end of block or statement
812 SkipUntil(tok::r_brace, true, true);
813 }
814 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000815 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000816 // Call ActOnFields() even if we don't have any decls. This is useful
817 // for code rewriting tools that need to be aware of the empty list.
818 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
819 &AllIvarDecls[0], AllIvarDecls.size(),
820 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Steve Naroffddbff782007-08-21 21:17:12 +0000821 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000822}
Steve Naroffdac269b2007-08-20 21:31:48 +0000823
824/// objc-protocol-declaration:
825/// objc-protocol-definition
826/// objc-protocol-forward-reference
827///
828/// objc-protocol-definition:
829/// @protocol identifier
830/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000831/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000832/// @end
833///
834/// objc-protocol-forward-reference:
835/// @protocol identifier-list ';'
836///
837/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000838/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000839/// semicolon in the first alternative if objc-protocol-refs are omitted.
840
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000841Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000842 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000843 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
844 ConsumeToken(); // the "protocol" identifier
845
Chris Lattnerdf195262007-10-09 17:51:17 +0000846 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000847 Diag(Tok, diag::err_expected_ident); // missing protocol name.
848 return 0;
849 }
850 // Save the protocol name, then consume it.
851 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
852 SourceLocation nameLoc = ConsumeToken();
853
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000854 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000855 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000856 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000857 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000858 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000859 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000860 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000861 ProtocolRefs.push_back(protocolName);
862
863 while (1) {
864 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000865 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000866 Diag(Tok, diag::err_expected_ident);
867 SkipUntil(tok::semi);
868 return 0;
869 }
870 ProtocolRefs.push_back(Tok.getIdentifierInfo());
871 ConsumeToken(); // the identifier
872
Chris Lattnerdf195262007-10-09 17:51:17 +0000873 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000874 break;
875 }
876 // Consume the ';'.
877 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
878 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000879 }
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000880 if (ProtocolRefs.size() > 0)
Steve Naroffe440eb82007-10-10 17:32:04 +0000881 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000882 &ProtocolRefs[0],
883 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000884 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000885 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000886 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000887 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000888 return 0;
889 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000890
Steve Naroffe440eb82007-10-10 17:32:04 +0000891 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000892 protocolName, nameLoc,
893 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000894 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000895 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000896
897 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000898 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000899 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000900 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000901 }
902 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000903 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000904}
Steve Naroffdac269b2007-08-20 21:31:48 +0000905
906/// objc-implementation:
907/// objc-class-implementation-prologue
908/// objc-category-implementation-prologue
909///
910/// objc-class-implementation-prologue:
911/// @implementation identifier objc-superclass[opt]
912/// objc-class-instance-variables[opt]
913///
914/// objc-category-implementation-prologue:
915/// @implementation identifier ( identifier )
916
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000917Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
918 SourceLocation atLoc) {
919 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
920 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
921 ConsumeToken(); // the "implementation" identifier
922
Chris Lattnerdf195262007-10-09 17:51:17 +0000923 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000924 Diag(Tok, diag::err_expected_ident); // missing class or category name.
925 return 0;
926 }
927 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000928 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000929 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
930
Chris Lattnerdf195262007-10-09 17:51:17 +0000931 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000932 // we have a category implementation.
933 SourceLocation lparenLoc = ConsumeParen();
934 SourceLocation categoryLoc, rparenLoc;
935 IdentifierInfo *categoryId = 0;
936
Chris Lattnerdf195262007-10-09 17:51:17 +0000937 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000938 categoryId = Tok.getIdentifierInfo();
939 categoryLoc = ConsumeToken();
940 } else {
941 Diag(Tok, diag::err_expected_ident); // missing category name.
942 return 0;
943 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000944 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000945 Diag(Tok, diag::err_expected_rparen);
946 SkipUntil(tok::r_paren, false); // don't stop at ';'
947 return 0;
948 }
949 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000950 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000951 atLoc, nameId, nameLoc, categoryId,
952 categoryLoc);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000953 ObjcImpDecl = ImplCatType;
954 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000955 }
956 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000957 SourceLocation superClassLoc;
958 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000960 // We have a super class
961 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000963 Diag(Tok, diag::err_expected_ident); // missing super class name.
964 return 0;
965 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000966 superClassId = Tok.getIdentifierInfo();
967 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000968 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000969 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff3a165b02007-10-03 21:00:46 +0000970 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000971 superClassId, superClassLoc);
972
Steve Naroff60fccee2007-10-29 21:38:07 +0000973 if (Tok.is(tok::l_brace)) // we have ivars
974 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000975 ObjcImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000976
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000977 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000978}
Steve Naroff60fccee2007-10-29 21:38:07 +0000979
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000980Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
981 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
982 "ParseObjCAtEndDeclaration(): Expected @end");
983 ConsumeToken(); // the "end" identifier
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000984 if (ObjcImpDecl) {
985 // Checking is not necessary except that a parse error might have caused
986 // @implementation not to have been parsed to completion and ObjcImpDecl
987 // could be 0.
Steve Naroff0416fb92007-11-11 17:19:15 +0000988 Actions.ActOnAtEnd(atLoc, ObjcImpDecl);
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000989 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000990
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000991 return ObjcImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +0000992}
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000993
994/// compatibility-alias-decl:
995/// @compatibility_alias alias-name class-name ';'
996///
997Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
998 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
999 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1000 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001001 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001002 Diag(Tok, diag::err_expected_ident);
1003 return 0;
1004 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001005 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1006 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001007 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001008 Diag(Tok, diag::err_expected_ident);
1009 return 0;
1010 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001011 IdentifierInfo *classId = Tok.getIdentifierInfo();
1012 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1013 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001014 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001015 return 0;
1016 }
1017 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1018 aliasId, aliasLoc,
1019 classId, classLoc);
1020 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001021}
1022
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001023/// property-synthesis:
1024/// @synthesize property-ivar-list ';'
1025///
1026/// property-ivar-list:
1027/// property-ivar
1028/// property-ivar-list ',' property-ivar
1029///
1030/// property-ivar:
1031/// identifier
1032/// identifier '=' identifier
1033///
1034Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1035 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1036 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1037 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001038 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001039 Diag(Tok, diag::err_expected_ident);
1040 return 0;
1041 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001042 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001043 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001044 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001045 // property '=' ivar-name
1046 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001047 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001048 Diag(Tok, diag::err_expected_ident);
1049 break;
1050 }
1051 ConsumeToken(); // consume ivar-name
1052 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001053 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001054 break;
1055 ConsumeToken(); // consume ','
1056 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001058 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1059 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001060}
1061
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001062/// property-dynamic:
1063/// @dynamic property-list
1064///
1065/// property-list:
1066/// identifier
1067/// property-list ',' identifier
1068///
1069Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1070 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1071 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1072 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001073 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001074 Diag(Tok, diag::err_expected_ident);
1075 return 0;
1076 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001077 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001078 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001079 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001080 break;
1081 ConsumeToken(); // consume ','
1082 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001083 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001084 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1085 return 0;
1086}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001087
1088/// objc-throw-statement:
1089/// throw expression[opt];
1090///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001091Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1092 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001093 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001094 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001095 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001096 if (Res.isInvalid) {
1097 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001098 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001099 }
1100 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001101 ConsumeToken(); // consume ';'
1102 return Actions.ActOnObjcAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001103}
1104
1105/// objc-try-catch-statement:
1106/// @try compound-statement objc-catch-list[opt]
1107/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1108///
1109/// objc-catch-list:
1110/// @catch ( parameter-declaration ) compound-statement
1111/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1112/// catch-parameter-declaration:
1113/// parameter-declaration
1114/// '...' [OBJC2]
1115///
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001116Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001117 bool catch_or_finally_seen = false;
1118 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001119 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001120 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001121 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001122 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001123 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001124 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001125 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001126 if (TryBody.isInvalid)
1127 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattnerdf195262007-10-09 17:51:17 +00001128 while (Tok.is(tok::at)) {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001129 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001130 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001131 StmtTy *FirstPart = 0;
1132 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001133 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001134 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001135 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001136 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001137 DeclSpec DS;
1138 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001139 // FIXME: Is BlockContext right?
1140 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1141 ParseDeclarator(DeclaratorInfo);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +00001142 DeclTy * aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1143 DeclaratorInfo, 0);
1144 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001145 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001146 }
1147 else
1148 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001149 SourceLocation RParenLoc = ConsumeParen();
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001150 StmtResult CatchBody = ParseCompoundStatementBody();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001151 if (CatchBody.isInvalid)
1152 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001153 CatchStmts = Actions.ActOnObjcAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001154 FirstPart, CatchBody.Val, CatchStmts.Val);
1155 ExitScope();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001156 }
1157 else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001158 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1159 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001160 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001161 }
1162 catch_or_finally_seen = true;
1163 }
1164 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1165 ConsumeToken(); // consume finally
1166 StmtResult FinallyBody = ParseCompoundStatementBody();
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001167 if (FinallyBody.isInvalid)
1168 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
1169 FinallyStmt = Actions.ActOnObjcAtFinallyStmt(AtCatchFinallyLoc,
1170 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001171 catch_or_finally_seen = true;
1172 break;
1173 }
1174 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001175 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001176 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001177 return true;
1178 }
1179 return Actions.ActOnObjcAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
1180 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001181}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001182
Steve Naroff3536b442007-09-06 21:24:23 +00001183/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001184///
Steve Naroff71c0a952007-11-13 23:01:27 +00001185Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001186 DeclTy *MDecl = ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001187 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001188 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001189 ConsumeToken();
1190
Steve Naroff409be832007-11-11 19:54:21 +00001191 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001192 if (Tok.isNot(tok::l_brace)) {
Steve Naroff409be832007-11-11 19:54:21 +00001193 Diag(Tok, diag::err_expected_fn_body);
1194
1195 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1196 SkipUntil(tok::l_brace, true, true);
1197
1198 // If we didn't find the '{', bail out.
1199 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001200 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001201 }
Steve Naroff409be832007-11-11 19:54:21 +00001202 SourceLocation BraceLoc = Tok.getLocation();
1203
1204 // Enter a scope for the method body.
1205 EnterScope(Scope::FnScope|Scope::DeclScope);
1206
1207 // Tell the actions module that we have entered a method definition with the
1208 // specified Declarator for the method.
1209 Actions.ObjcActOnStartOfMethodDef(CurScope, MDecl);
1210
1211 StmtResult FnBody = ParseCompoundStatementBody();
1212
1213 // If the function body could not be parsed, make a bogus compoundstmt.
1214 if (FnBody.isInvalid)
1215 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1216
1217 // Leave the function body scope.
1218 ExitScope();
1219
1220 // TODO: Pass argument information.
Steve Naroffd6d054d2007-11-11 23:20:51 +00001221 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001222 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001223}
Anders Carlsson55085182007-08-21 17:43:55 +00001224
Steve Naroffa642beb2007-10-15 20:55:58 +00001225Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001226
1227 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001228 case tok::string_literal: // primary-expression: string-literal
1229 case tok::wide_string_literal:
1230 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1231 default:
1232 break;
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001233 }
1234
1235 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001236 case tok::objc_encode:
1237 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1238 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001239 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001240 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001241 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001242 default:
1243 Diag(AtLoc, diag::err_unexpected_at);
1244 SkipUntil(tok::semi);
1245 break;
Anders Carlsson55085182007-08-21 17:43:55 +00001246 }
1247
1248 return 0;
1249}
1250
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001251/// objc-message-expr:
1252/// '[' objc-receiver objc-message-args ']'
1253///
1254/// objc-receiver:
1255/// expression
1256/// class-name
1257/// type-name
1258///
1259/// objc-message-args:
1260/// objc-selector
1261/// objc-keywordarg-list
1262///
1263/// objc-keywordarg-list:
1264/// objc-keywordarg
1265/// objc-keywordarg-list objc-keywordarg
1266///
1267/// objc-keywordarg:
1268/// selector-name[opt] ':' objc-keywordexpr
1269///
1270/// objc-keywordexpr:
1271/// nonempty-expr-list
1272///
1273/// nonempty-expr-list:
1274/// assignment-expression
1275/// nonempty-expr-list , assignment-expression
1276///
1277Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnerdf195262007-10-09 17:51:17 +00001278 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroff563477d2007-09-18 23:55:05 +00001279 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff37387c92007-09-17 20:25:27 +00001280 IdentifierInfo *ReceiverName = 0;
1281 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001282 // Parse receiver
Chris Lattnerdf195262007-10-09 17:51:17 +00001283 if (Tok.is(tok::identifier) &&
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001284 (Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)
1285 || !strcmp(Tok.getIdentifierInfo()->getName(), "super"))) {
Steve Naroff37387c92007-09-17 20:25:27 +00001286 ReceiverName = Tok.getIdentifierInfo();
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001287 ConsumeToken();
Steve Naroff37387c92007-09-17 20:25:27 +00001288 } else {
1289 ExprResult Res = ParseAssignmentExpression();
1290 if (Res.isInvalid) {
Steve Naroff11508212007-11-12 19:18:37 +00001291 Diag(Tok, diag::err_invalid_receiver_to_message);
Steve Naroff37387c92007-09-17 20:25:27 +00001292 SkipUntil(tok::identifier);
1293 return Res;
1294 }
1295 ReceiverExpr = Res.Val;
1296 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001297 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001298 SourceLocation Loc;
1299 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001300
1301 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1302 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1303
Chris Lattnerdf195262007-10-09 17:51:17 +00001304 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001305 while (1) {
1306 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001307 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001308
Chris Lattnerdf195262007-10-09 17:51:17 +00001309 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001310 Diag(Tok, diag::err_expected_colon);
1311 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001312 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001313 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001314 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001315 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001316 ExprResult Res = ParseAssignmentExpression();
1317 if (Res.isInvalid) {
1318 SkipUntil(tok::identifier);
1319 return Res;
1320 }
1321 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001322 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001323
1324 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001325 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001326 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001327 break;
1328 // We have a selector or a colon, continue parsing.
1329 }
1330 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001331 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001332 ConsumeToken(); // Eat the ','.
1333 /// Parse the expression after ','
1334 ExprResult Res = ParseAssignmentExpression();
1335 if (Res.isInvalid) {
1336 SkipUntil(tok::identifier);
1337 return Res;
1338 }
1339 // We have a valid expression.
1340 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001341 }
1342 } else if (!selIdent) {
1343 Diag(Tok, diag::err_expected_ident); // missing selector name.
1344 SkipUntil(tok::semi);
1345 return 0;
1346 }
Chris Lattnerff384912007-10-07 02:00:24 +00001347
Chris Lattnerdf195262007-10-09 17:51:17 +00001348 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001349 Diag(Tok, diag::err_expected_rsquare);
1350 SkipUntil(tok::semi);
1351 return 0;
1352 }
Steve Naroff563477d2007-09-18 23:55:05 +00001353 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001354
Steve Naroff29238a02007-10-05 18:42:47 +00001355 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001356 if (nKeys == 0)
1357 KeyIdents.push_back(selIdent);
1358 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1359
1360 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001361 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001362 return Actions.ActOnClassMessage(CurScope,
1363 ReceiverName, Sel, LBracloc, RBracloc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001364 &KeyExprs[0], KeyExprs.size());
Chris Lattnerff384912007-10-07 02:00:24 +00001365 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001366 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001367}
1368
Steve Naroffbeaf2992007-11-03 11:27:19 +00001369Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001370 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001371 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001372
1373 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1374 // expressions. At this point, we know that the only valid thing that starts
1375 // with '@' is an @"".
1376 llvm::SmallVector<SourceLocation, 4> AtLocs;
1377 llvm::SmallVector<ExprTy*, 4> AtStrings;
1378 AtLocs.push_back(AtLoc);
1379 AtStrings.push_back(Res.Val);
1380
1381 while (Tok.is(tok::at)) {
1382 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001383
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001384 ExprResult Res(true); // Invalid unless there is a string literal.
1385 if (isTokenStringLiteral())
1386 Res = ParseStringLiteralExpression();
1387 else
1388 Diag(Tok, diag::err_objc_concat_string);
1389
1390 if (Res.isInvalid) {
1391 while (!AtStrings.empty()) {
1392 Actions.DeleteExpr(AtStrings.back());
1393 AtStrings.pop_back();
1394 }
1395 return Res;
1396 }
1397
1398 AtStrings.push_back(Res.Val);
1399 }
1400
1401 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1402 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001403}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001404
1405/// objc-encode-expression:
1406/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001407Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001408 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001409
1410 SourceLocation EncLoc = ConsumeToken();
1411
Chris Lattnerdf195262007-10-09 17:51:17 +00001412 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001413 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1414 return true;
1415 }
1416
1417 SourceLocation LParenLoc = ConsumeParen();
1418
1419 TypeTy *Ty = ParseTypeName();
1420
Anders Carlsson4988ae32007-08-23 15:31:37 +00001421 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001422
Chris Lattner674af952007-10-16 22:51:17 +00001423 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001424 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001425}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001426
1427/// objc-protocol-expression
1428/// @protocol ( protocol-name )
1429
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001430Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001431{
1432 SourceLocation ProtoLoc = ConsumeToken();
1433
Chris Lattnerdf195262007-10-09 17:51:17 +00001434 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001435 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1436 return true;
1437 }
1438
1439 SourceLocation LParenLoc = ConsumeParen();
1440
Chris Lattnerdf195262007-10-09 17:51:17 +00001441 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001442 Diag(Tok, diag::err_expected_ident);
1443 return true;
1444 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001445 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001446 ConsumeToken();
1447
Anders Carlsson4988ae32007-08-23 15:31:37 +00001448 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001449
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001450 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1451 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001452}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001453
1454/// objc-selector-expression
1455/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001456Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001457{
1458 SourceLocation SelectorLoc = ConsumeToken();
1459
1460 if (Tok.isNot(tok::l_paren)) {
1461 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1462 return 0;
1463 }
1464
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001465 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001466 SourceLocation LParenLoc = ConsumeParen();
1467 SourceLocation sLoc;
1468 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1469 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001470 Diag(Tok, diag::err_expected_ident); // missing selector name.
1471 return 0;
1472 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001473 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001474 unsigned nColons = 0;
1475 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001476 while (1) {
1477 if (Tok.isNot(tok::colon)) {
1478 Diag(Tok, diag::err_expected_colon);
1479 break;
1480 }
Steve Naroff887407e2007-12-05 22:21:29 +00001481 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001482 ConsumeToken(); // Eat the ':'.
1483 if (Tok.is(tok::r_paren))
1484 break;
1485 // Check for another keyword selector.
1486 SourceLocation Loc;
1487 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001488 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001489 if (!SelIdent && Tok.isNot(tok::colon))
1490 break;
1491 }
Steve Naroff887407e2007-12-05 22:21:29 +00001492 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001493 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001494 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001495 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001496 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001497 }