blob: c17e35e6f4987eb9cfdfb096c6a309a36004faa2 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
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 Lattner4b009652007-07-25 00:24:17 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000037 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000038 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000039 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000040 case tok::objc_implementation:
Fariborz Jahanian83ddf822007-11-10 20:59:13 +000041 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000042 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000043 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000044 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000045 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000046 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000050 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000053 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;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000134 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000135
Steve Naroffa7f62782007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000143 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-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 Naroffef20ed32007-10-30 02:23:23 +0000150 SourceLocation endProtoLoc;
Steve Narofffb367882007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000152 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000153 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-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 Naroff415c1832007-10-10 17:32:04 +0000159 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000160 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000161 &ProtocolRefs[0], ProtocolRefs.size(),
162 endProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000163
164 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000165
Steve Naroff0bbffd82007-08-22 16:35:03 +0000166 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000167 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000168 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000169 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000170 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000171 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000172 return 0;
173 }
174 // Parse a class interface.
175 IdentifierInfo *superClassId = 0;
176 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000177
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000178 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000179 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-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 Naroff304ed392007-09-05 23:30:30 +0000188 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000189 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000190 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000191 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000192 return 0;
193 }
Steve Naroff415c1832007-10-10 17:32:04 +0000194 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Chris Lattner847f5c12007-12-27 19:57:00 +0000195 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000196 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000197 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000198
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000199 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000200 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000201
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000202 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000203
204 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000205 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000206 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000207 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000208 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000209 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000210 return 0;
211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000225void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000226 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000227 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000228 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000229 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000230 SourceLocation AtEndLoc;
231
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000233 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000234 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000235 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000236
237 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000238 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000239 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000240 } else if (ocKind == tok::objc_required) { // protocols only
241 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000242 MethodImplKind = ocKind;
243 if (contextKey != tok::objc_protocol)
244 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000245 } else if (ocKind == tok::objc_optional) { // protocols only
246 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000247 MethodImplKind = ocKind;
248 if (contextKey != tok::objc_protocol)
249 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000250 } else if (ocKind == tok::objc_property) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000251 allProperties.push_back(ParseObjCPropertyDecl(interfaceDecl, AtLoc));
Steve Naroff0bbffd82007-08-22 16:35:03 +0000252 continue;
253 } else {
254 Diag(Tok, diag::err_objc_illegal_interface_qual);
255 ConsumeToken();
256 }
257 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000258 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
259 DeclTy *methodPrototype =
260 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000261 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000262 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
263 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000264 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000265 continue;
266 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000267 else if (Tok.is(tok::at))
268 continue;
269
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000270 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000271 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000272 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000273 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000274 else {
Steve Naroff09a0c4c2007-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 Naroff81f1bba2007-09-06 21:24:23 +0000277 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000278 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000279 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000280 /// Insert collected methods declarations into the @interface object.
Steve Naroffb82c50f2007-11-11 17:19:15 +0000281 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
282 &allProperties[0], allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000283}
284
Fariborz Jahanian6668b8c2007-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 Jahaniand8df6d82007-11-06 22:01:00 +0000301void Parser::ParseObjCPropertyAttribute (ObjcDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-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 Lattnera1d2bb72007-10-09 17:51:17 +0000310 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000311 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000312 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000313 if (II == ObjcPropertyAttrs[objc_setter]) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000314 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_setter);
315 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000316 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000317 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000318 Diag(loc, diag::err_expected_colon);
319 SkipUntil(tok::r_paren,true,true);
320 break;
321 }
322 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000323 else {
324 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_getter);
325 DS.setGetterName(Tok.getIdentifierInfo());
326 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000327 }
328 else {
329 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000330 SkipUntil(tok::r_paren,true,true);
331 break;
332 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000333 }
334 else {
335 Diag(loc, diag::err_objc_expected_equal);
336 SkipUntil(tok::r_paren,true,true);
337 break;
338 }
339 }
Fariborz Jahaniand8df6d82007-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 Jahanian6668b8c2007-08-31 16:11:31 +0000354 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000355 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000356 loc = ConsumeToken();
357 continue;
358 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000359 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000360 break;
361 Diag(loc, diag::err_expected_rparen);
362 SkipUntil(tok::semi);
363 return;
364 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000365 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-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 Jahaniand8df6d82007-11-06 22:01:00 +0000377Parser::DeclTy *Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl,
378 SourceLocation AtLoc) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000379 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
380 "ParseObjCPropertyDecl(): Expected @property");
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000381 ObjcDeclSpec DS;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000382 ConsumeToken(); // the "property" identifier
383 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000384 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000385 // property has attribute list.
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000386 ParseObjCPropertyAttribute(DS);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000387 }
388 // Parse declaration portion of @property.
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000389 llvm::SmallVector<DeclTy*, 8> PropertyDecls;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000390 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000391 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-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 Jahaniand8df6d82007-11-06 22:01:00 +0000397 return Actions.ActOnAddObjcProperties(AtLoc,
398 &PropertyDecls[0], PropertyDecls.size(), DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000399}
Steve Narofffb367882007-08-20 21:31:48 +0000400
Steve Naroff81f1bba2007-09-06 21:24:23 +0000401/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000402/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000403/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000404///
405/// objc-instance-method: '-'
406/// objc-class-method: '+'
407///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000408/// objc-method-attributes: [OBJC2]
409/// __attribute__((deprecated))
410///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000411Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000412 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000413 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000414
415 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000416 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000417
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000418 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000419 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000420 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000421 return MDecl;
Steve Naroff0bbffd82007-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 Jahanian91193f62007-10-11 00:55:41 +0000432IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-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 Lattner2baef2e2007-11-15 05:25:19 +0000473 case tok::kw_bool:
Chris Lattnerd031a452007-10-07 02:00:24 +0000474 case tok::kw__Bool:
475 case tok::kw__Complex:
476 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000477 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000478 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000479 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480}
481
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000482/// property-attrlist: one of
483/// readonly getter setter assign retain copy nonatomic
484///
485bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000486 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-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
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000494/// objc-for-collection-in: 'in'
495///
496bool Parser::isObjCForCollectionInKW() {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000497 // FIXME: May have to do additional look-ahead to only allow for
498 // valid tokens following an 'in'; such as an identifier, unary operators,
499 // '[' etc.
500 if (getLang().ObjC2 && Tok.is(tok::identifier)) {
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000501 const IdentifierInfo *II = Tok.getIdentifierInfo();
502 return II == ObjCForCollectionInKW;
503 }
504 return false;
505}
506
Chris Lattner2b740db2007-12-12 06:56:32 +0000507/// ParseObjcTypeQualifierList - This routine parses the objective-c's type
508/// qualifier list and builds their bitmask representation in the input
509/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000510///
511/// objc-type-qualifiers:
512/// objc-type-qualifier
513/// objc-type-qualifiers objc-type-qualifier
514///
Chris Lattner2b740db2007-12-12 06:56:32 +0000515void Parser::ParseObjcTypeQualifierList(ObjcDeclSpec &DS) {
516 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000517 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000518 return;
519
520 const IdentifierInfo *II = Tok.getIdentifierInfo();
521 for (unsigned i = 0; i != objc_NumQuals; ++i) {
522 if (II != ObjcTypeQuals[i])
523 continue;
524
525 ObjcDeclSpec::ObjcDeclQualifier Qual;
526 switch (i) {
527 default: assert(0 && "Unknown decl qualifier");
528 case objc_in: Qual = ObjcDeclSpec::DQ_In; break;
529 case objc_out: Qual = ObjcDeclSpec::DQ_Out; break;
530 case objc_inout: Qual = ObjcDeclSpec::DQ_Inout; break;
531 case objc_oneway: Qual = ObjcDeclSpec::DQ_Oneway; break;
532 case objc_bycopy: Qual = ObjcDeclSpec::DQ_Bycopy; break;
533 case objc_byref: Qual = ObjcDeclSpec::DQ_Byref; break;
534 }
535 DS.setObjcDeclQualifier(Qual);
536 ConsumeToken();
537 II = 0;
538 break;
539 }
540
541 // If this wasn't a recognized qualifier, bail out.
542 if (II) return;
543 }
544}
545
546/// objc-type-name:
547/// '(' objc-type-qualifiers[opt] type-name ')'
548/// '(' objc-type-qualifiers[opt] ')'
549///
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000550Parser::TypeTy *Parser::ParseObjCTypeName(ObjcDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000551 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000552
553 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000554 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000555
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000556 // Parse type qualifiers, in, inout, etc.
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000557 ParseObjcTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000558
Steve Naroff0bbffd82007-08-22 16:35:03 +0000559 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000560 Ty = ParseTypeName();
561 // FIXME: back when Sema support is in place...
562 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000563 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000564 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000565 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000566 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000567 }
568 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000569 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000570}
571
572/// objc-method-decl:
573/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000574/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000575/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000576/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000577///
578/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000579/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000580/// objc-keyword-selector objc-keyword-decl
581///
582/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000583/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
584/// objc-selector ':' objc-keyword-attributes[opt] identifier
585/// ':' objc-type-name objc-keyword-attributes[opt] identifier
586/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000587///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000588/// objc-parmlist:
589/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000590///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000591/// objc-parms:
592/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000593///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000594/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000595/// , ...
596///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000597/// objc-keyword-attributes: [OBJC2]
598/// __attribute__((unused))
599///
Steve Naroff3774dd92007-10-26 20:53:56 +0000600Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000601 tok::TokenKind mType,
602 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000603 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000604{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000605 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000606 TypeTy *ReturnType = 0;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000607 ObjcDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000608 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000609 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000610 SourceLocation selLoc;
611 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000612 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000613 if (!SelIdent) {
614 Diag(Tok, diag::err_expected_ident); // missing selector name.
615 // FIXME: this creates a unary selector with a null identifier, is this
616 // ok?? Maybe we should skip to the next semicolon or something.
617 }
618
619 // If attributes exist after the method, parse them.
620 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000621 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000622 MethodAttrs = ParseAttributes();
623
624 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000625 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000626 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000627 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000628 }
Steve Naroff304ed392007-09-05 23:30:30 +0000629
Steve Naroff4ed9d662007-09-27 14:38:14 +0000630 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
631 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000632 llvm::SmallVector<ObjcDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000633 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000634
635 Action::TypeTy *TypeInfo;
636 while (1) {
637 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000638
Chris Lattnerd031a452007-10-07 02:00:24 +0000639 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000640 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000641 Diag(Tok, diag::err_expected_colon);
642 break;
643 }
644 ConsumeToken(); // Eat the ':'.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000645 ObjcDeclSpec DSType;
646 if (Tok.is(tok::l_paren)) { // Parse the argument type.
647 TypeInfo = ParseObjCTypeName(DSType);
648 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000649 else
650 TypeInfo = 0;
651 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000652 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000653
Chris Lattnerd031a452007-10-07 02:00:24 +0000654 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000655 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000656 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000657
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000658 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000659 Diag(Tok, diag::err_expected_ident); // missing argument name.
660 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000661 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000662 ArgNames.push_back(Tok.getIdentifierInfo());
663 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000664
Chris Lattnerd031a452007-10-07 02:00:24 +0000665 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000666 SourceLocation Loc;
667 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000668 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000669 break;
670 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000671 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000672
Steve Naroff29fe7462007-11-15 12:35:21 +0000673 bool isVariadic = false;
674
Chris Lattnerd031a452007-10-07 02:00:24 +0000675 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000676 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000677 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000678 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000679 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000680 ConsumeToken();
681 break;
682 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000683 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000684 // Parse the c-style argument declaration-specifier.
685 DeclSpec DS;
686 ParseDeclarationSpecifiers(DS);
687 // Parse the declarator.
688 Declarator ParmDecl(DS, Declarator::PrototypeContext);
689 ParseDeclarator(ParmDecl);
690 }
691
692 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000693 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000694 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000695 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000696 MethodAttrs = ParseAttributes();
697
698 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
699 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000700 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000701 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000702 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000703 &ArgNames[0], MethodAttrs,
704 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000705}
706
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000707/// CmpProtocolVals - Comparison predicate for sorting protocols.
708static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
709 const IdentifierInfo* const& rhs) {
710 return strcmp(lhs->getName(), rhs->getName()) < 0;
711}
712
Steve Narofffb367882007-08-20 21:31:48 +0000713/// objc-protocol-refs:
714/// '<' identifier-list '>'
715///
Steve Naroff304ed392007-09-05 23:30:30 +0000716bool Parser::ParseObjCProtocolReferences(
Steve Naroffef20ed32007-10-30 02:23:23 +0000717 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
718{
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000719 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000720
721 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000722
723 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000724 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000725 Diag(Tok, diag::err_expected_ident);
726 SkipUntil(tok::greater);
727 return true;
728 }
729 ProtocolRefs.push_back(Tok.getIdentifierInfo());
730 ConsumeToken();
731
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000732 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000733 break;
734 ConsumeToken();
735 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000736
737 // Sort protocols, keyed by name.
738 // Later on, we remove duplicates.
739 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
740
741 // Make protocol names unique.
742 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
743 ProtocolRefs.end());
Steve Narofffb367882007-08-20 21:31:48 +0000744 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000745 if (Tok.is(tok::greater)) {
746 endLoc = ConsumeAnyToken();
747 return false;
748 }
749 Diag(Tok, diag::err_expected_greater);
750 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000751}
752
753/// objc-class-instance-variables:
754/// '{' objc-instance-variable-decl-list[opt] '}'
755///
756/// objc-instance-variable-decl-list:
757/// objc-visibility-spec
758/// objc-instance-variable-decl ';'
759/// ';'
760/// objc-instance-variable-decl-list objc-visibility-spec
761/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
762/// objc-instance-variable-decl-list ';'
763///
764/// objc-visibility-spec:
765/// @private
766/// @protected
767/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000768/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000769///
770/// objc-instance-variable-decl:
771/// struct-declaration
772///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000773void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
774 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000775 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000776 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000777 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
778 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000779
780 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000781
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000782 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000783 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000784 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000785 // Each iteration of this loop reads one objc-instance-variable-decl.
786
787 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000788 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000789 Diag(Tok, diag::ext_extra_struct_semi);
790 ConsumeToken();
791 continue;
792 }
793 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000794 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000795 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000796 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000797 case tok::objc_private:
798 case tok::objc_public:
799 case tok::objc_protected:
800 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000801 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000802 ConsumeToken();
803 continue;
804 default:
805 Diag(Tok, diag::err_objc_illegal_visibility_spec);
806 ConsumeToken();
807 continue;
808 }
809 }
810 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000811 for (unsigned i = 0; i < IvarDecls.size(); i++) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000812 AllIvarDecls.push_back(IvarDecls[i]);
813 AllVisibilities.push_back(visibility);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000814 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000815 IvarDecls.clear();
816
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000817 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000818 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000819 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000820 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
821 break;
822 } else {
823 Diag(Tok, diag::err_expected_semi_decl_list);
824 // Skip to end of block or statement
825 SkipUntil(tok::r_brace, true, true);
826 }
827 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000828 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000829 // Call ActOnFields() even if we don't have any decls. This is useful
830 // for code rewriting tools that need to be aware of the empty list.
831 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
832 &AllIvarDecls[0], AllIvarDecls.size(),
833 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Steve Naroffc4474992007-08-21 21:17:12 +0000834 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000835}
Steve Narofffb367882007-08-20 21:31:48 +0000836
837/// objc-protocol-declaration:
838/// objc-protocol-definition
839/// objc-protocol-forward-reference
840///
841/// objc-protocol-definition:
842/// @protocol identifier
843/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000844/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000845/// @end
846///
847/// objc-protocol-forward-reference:
848/// @protocol identifier-list ';'
849///
850/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000851/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000852/// semicolon in the first alternative if objc-protocol-refs are omitted.
853
Steve Naroff72f17fb2007-08-22 22:17:26 +0000854Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000855 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000856 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
857 ConsumeToken(); // the "protocol" identifier
858
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000859 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000860 Diag(Tok, diag::err_expected_ident); // missing protocol name.
861 return 0;
862 }
863 // Save the protocol name, then consume it.
864 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
865 SourceLocation nameLoc = ConsumeToken();
866
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000867 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000868 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000869 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000870 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000871 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000872 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000873 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000874 ProtocolRefs.push_back(protocolName);
875
876 while (1) {
877 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000878 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000879 Diag(Tok, diag::err_expected_ident);
880 SkipUntil(tok::semi);
881 return 0;
882 }
883 ProtocolRefs.push_back(Tok.getIdentifierInfo());
884 ConsumeToken(); // the identifier
885
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000886 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000887 break;
888 }
889 // Consume the ';'.
890 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
891 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000892 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000893 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000894 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000895 &ProtocolRefs[0],
896 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000897 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000898 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000899 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000900 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000901 return 0;
902 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000903
Steve Naroff415c1832007-10-10 17:32:04 +0000904 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000905 protocolName, nameLoc,
906 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000907 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000908 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000909
910 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000911 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000912 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000913 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000914 }
915 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000916 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000917}
Steve Narofffb367882007-08-20 21:31:48 +0000918
919/// objc-implementation:
920/// objc-class-implementation-prologue
921/// objc-category-implementation-prologue
922///
923/// objc-class-implementation-prologue:
924/// @implementation identifier objc-superclass[opt]
925/// objc-class-instance-variables[opt]
926///
927/// objc-category-implementation-prologue:
928/// @implementation identifier ( identifier )
929
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000930Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
931 SourceLocation atLoc) {
932 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
933 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
934 ConsumeToken(); // the "implementation" identifier
935
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000936 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000937 Diag(Tok, diag::err_expected_ident); // missing class or category name.
938 return 0;
939 }
940 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000941 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000942 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
943
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000944 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000945 // we have a category implementation.
946 SourceLocation lparenLoc = ConsumeParen();
947 SourceLocation categoryLoc, rparenLoc;
948 IdentifierInfo *categoryId = 0;
949
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000950 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000951 categoryId = Tok.getIdentifierInfo();
952 categoryLoc = ConsumeToken();
953 } else {
954 Diag(Tok, diag::err_expected_ident); // missing category name.
955 return 0;
956 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000957 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000958 Diag(Tok, diag::err_expected_rparen);
959 SkipUntil(tok::r_paren, false); // don't stop at ';'
960 return 0;
961 }
962 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000963 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000964 atLoc, nameId, nameLoc, categoryId,
965 categoryLoc);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000966 ObjcImpDecl = ImplCatType;
967 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000968 }
969 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000970 SourceLocation superClassLoc;
971 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000972 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000973 // We have a super class
974 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000975 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000976 Diag(Tok, diag::err_expected_ident); // missing super class name.
977 return 0;
978 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000979 superClassId = Tok.getIdentifierInfo();
980 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000981 }
Steve Naroff415c1832007-10-10 17:32:04 +0000982 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +0000983 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000984 superClassId, superClassLoc);
985
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000986 if (Tok.is(tok::l_brace)) // we have ivars
987 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000988 ObjcImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000989
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000990 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000991}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000992
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000993Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
994 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
995 "ParseObjCAtEndDeclaration(): Expected @end");
996 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000997 if (ObjcImpDecl) {
998 // Checking is not necessary except that a parse error might have caused
999 // @implementation not to have been parsed to completion and ObjcImpDecl
1000 // could be 0.
Steve Naroffb82c50f2007-11-11 17:19:15 +00001001 Actions.ActOnAtEnd(atLoc, ObjcImpDecl);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001002 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001003
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001004 return ObjcImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001005}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001006
1007/// compatibility-alias-decl:
1008/// @compatibility_alias alias-name class-name ';'
1009///
1010Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1011 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1012 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1013 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001014 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001015 Diag(Tok, diag::err_expected_ident);
1016 return 0;
1017 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001018 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1019 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001021 Diag(Tok, diag::err_expected_ident);
1022 return 0;
1023 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001024 IdentifierInfo *classId = Tok.getIdentifierInfo();
1025 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1026 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001027 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001028 return 0;
1029 }
1030 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1031 aliasId, aliasLoc,
1032 classId, classLoc);
1033 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001034}
1035
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001036/// property-synthesis:
1037/// @synthesize property-ivar-list ';'
1038///
1039/// property-ivar-list:
1040/// property-ivar
1041/// property-ivar-list ',' property-ivar
1042///
1043/// property-ivar:
1044/// identifier
1045/// identifier '=' identifier
1046///
1047Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1048 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1049 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1050 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001051 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001052 Diag(Tok, diag::err_expected_ident);
1053 return 0;
1054 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001055 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001056 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001058 // property '=' ivar-name
1059 ConsumeToken(); // consume '='
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);
1062 break;
1063 }
1064 ConsumeToken(); // consume ivar-name
1065 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001066 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001067 break;
1068 ConsumeToken(); // consume ','
1069 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001070 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001071 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1072 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001073}
1074
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001075/// property-dynamic:
1076/// @dynamic property-list
1077///
1078/// property-list:
1079/// identifier
1080/// property-list ',' identifier
1081///
1082Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1083 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1084 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1085 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001086 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001087 Diag(Tok, diag::err_expected_ident);
1088 return 0;
1089 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001090 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001091 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001092 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001093 break;
1094 ConsumeToken(); // consume ','
1095 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001097 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1098 return 0;
1099}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001100
1101/// objc-throw-statement:
1102/// throw expression[opt];
1103///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001104Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1105 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001106 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001107 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001108 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001109 if (Res.isInvalid) {
1110 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001111 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001112 }
1113 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001114 ConsumeToken(); // consume ';'
1115 return Actions.ActOnObjcAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001116}
1117
1118/// objc-try-catch-statement:
1119/// @try compound-statement objc-catch-list[opt]
1120/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1121///
1122/// objc-catch-list:
1123/// @catch ( parameter-declaration ) compound-statement
1124/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1125/// catch-parameter-declaration:
1126/// parameter-declaration
1127/// '...' [OBJC2]
1128///
Fariborz Jahanian70952482007-11-01 21:12:44 +00001129Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001130 bool catch_or_finally_seen = false;
1131 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001132 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001133 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001134 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001135 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001136 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001137 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001138 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001139 if (TryBody.isInvalid)
1140 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001141 while (Tok.is(tok::at)) {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001142 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001143 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001144 StmtTy *FirstPart = 0;
1145 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001146 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001147 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001148 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001149 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001150 DeclSpec DS;
1151 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001152 // FIXME: Is BlockContext right?
1153 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1154 ParseDeclarator(DeclaratorInfo);
Fariborz Jahanian59e9e042007-11-02 18:16:07 +00001155 DeclTy * aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1156 DeclaratorInfo, 0);
1157 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001158 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001159 }
1160 else
1161 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001162 SourceLocation RParenLoc = ConsumeParen();
Fariborz Jahanian70952482007-11-01 21:12:44 +00001163 StmtResult CatchBody = ParseCompoundStatementBody();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001164 if (CatchBody.isInvalid)
1165 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001166 CatchStmts = Actions.ActOnObjcAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001167 FirstPart, CatchBody.Val, CatchStmts.Val);
1168 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001169 }
1170 else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001171 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1172 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001173 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001174 }
1175 catch_or_finally_seen = true;
1176 }
Chris Lattner847f5c12007-12-27 19:57:00 +00001177 else if (Tok.isObjCAtKeyword(tok::objc_finally)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001178 ConsumeToken(); // consume finally
1179 StmtResult FinallyBody = ParseCompoundStatementBody();
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001180 if (FinallyBody.isInvalid)
1181 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
1182 FinallyStmt = Actions.ActOnObjcAtFinallyStmt(AtCatchFinallyLoc,
1183 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001184 catch_or_finally_seen = true;
1185 break;
1186 }
1187 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001188 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001189 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001190 return true;
1191 }
1192 return Actions.ActOnObjcAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
1193 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001194}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001195
Steve Naroff81f1bba2007-09-06 21:24:23 +00001196/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001197///
Steve Naroff18c83382007-11-13 23:01:27 +00001198Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +00001199 DeclTy *MDecl = ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001200 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001201 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001202 ConsumeToken();
1203
Steve Naroff9191a9e82007-11-11 19:54:21 +00001204 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::l_brace)) {
Steve Naroff9191a9e82007-11-11 19:54:21 +00001206 Diag(Tok, diag::err_expected_fn_body);
1207
1208 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1209 SkipUntil(tok::l_brace, true, true);
1210
1211 // If we didn't find the '{', bail out.
1212 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001213 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001214 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001215 SourceLocation BraceLoc = Tok.getLocation();
1216
1217 // Enter a scope for the method body.
1218 EnterScope(Scope::FnScope|Scope::DeclScope);
1219
1220 // Tell the actions module that we have entered a method definition with the
1221 // specified Declarator for the method.
1222 Actions.ObjcActOnStartOfMethodDef(CurScope, MDecl);
1223
1224 StmtResult FnBody = ParseCompoundStatementBody();
1225
1226 // If the function body could not be parsed, make a bogus compoundstmt.
1227 if (FnBody.isInvalid)
1228 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1229
1230 // Leave the function body scope.
1231 ExitScope();
1232
1233 // TODO: Pass argument information.
Steve Naroff99ee4302007-11-11 23:20:51 +00001234 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001235 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001236}
Anders Carlssona66cad42007-08-21 17:43:55 +00001237
Steve Narofffb9dd752007-10-15 20:55:58 +00001238Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001239
1240 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001241 case tok::string_literal: // primary-expression: string-literal
1242 case tok::wide_string_literal:
1243 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1244 default:
1245 break;
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001246 }
1247
1248 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001249 case tok::objc_encode:
1250 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1251 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001252 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001253 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001254 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001255 default:
1256 Diag(AtLoc, diag::err_unexpected_at);
1257 SkipUntil(tok::semi);
1258 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001259 }
1260
1261 return 0;
1262}
1263
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001264/// objc-message-expr:
1265/// '[' objc-receiver objc-message-args ']'
1266///
1267/// objc-receiver:
1268/// expression
1269/// class-name
1270/// type-name
1271///
1272/// objc-message-args:
1273/// objc-selector
1274/// objc-keywordarg-list
1275///
1276/// objc-keywordarg-list:
1277/// objc-keywordarg
1278/// objc-keywordarg-list objc-keywordarg
1279///
1280/// objc-keywordarg:
1281/// selector-name[opt] ':' objc-keywordexpr
1282///
1283/// objc-keywordexpr:
1284/// nonempty-expr-list
1285///
1286/// nonempty-expr-list:
1287/// assignment-expression
1288/// nonempty-expr-list , assignment-expression
1289///
1290Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001291 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001292 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001293 IdentifierInfo *ReceiverName = 0;
1294 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001295 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001296 if (Tok.is(tok::identifier) &&
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001297 (Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)
Chris Lattner9b6d9332007-12-13 02:05:09 +00001298 || Tok.isNamedIdentifier("super"))) {
Steve Naroff253118b2007-09-17 20:25:27 +00001299 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001300 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001301 } else {
1302 ExprResult Res = ParseAssignmentExpression();
1303 if (Res.isInvalid) {
Steve Naroffe015e9c2007-11-12 19:18:37 +00001304 Diag(Tok, diag::err_invalid_receiver_to_message);
Steve Naroff253118b2007-09-17 20:25:27 +00001305 SkipUntil(tok::identifier);
1306 return Res;
1307 }
1308 ReceiverExpr = Res.Val;
1309 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001310 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001311 SourceLocation Loc;
1312 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001313
1314 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1315 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1316
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001317 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001318 while (1) {
1319 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001320 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001321
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001322 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001323 Diag(Tok, diag::err_expected_colon);
1324 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001325 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001326 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001327 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001328 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001329 ExprResult Res = ParseAssignmentExpression();
1330 if (Res.isInvalid) {
1331 SkipUntil(tok::identifier);
1332 return Res;
1333 }
1334 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001335 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001336
1337 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001338 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001339 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001340 break;
1341 // We have a selector or a colon, continue parsing.
1342 }
1343 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001344 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001345 ConsumeToken(); // Eat the ','.
1346 /// Parse the expression after ','
1347 ExprResult Res = ParseAssignmentExpression();
1348 if (Res.isInvalid) {
1349 SkipUntil(tok::identifier);
1350 return Res;
1351 }
1352 // We have a valid expression.
1353 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001354 }
1355 } else if (!selIdent) {
1356 Diag(Tok, diag::err_expected_ident); // missing selector name.
1357 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001358 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001359 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001360
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001361 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001362 Diag(Tok, diag::err_expected_rsquare);
1363 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001364 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001365 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001366 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001367
Steve Narofff9e80db2007-10-05 18:42:47 +00001368 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001369 if (nKeys == 0)
1370 KeyIdents.push_back(selIdent);
1371 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1372
1373 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001374 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001375 return Actions.ActOnClassMessage(CurScope,
1376 ReceiverName, Sel, LBracloc, RBracloc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001377 &KeyExprs[0], KeyExprs.size());
Chris Lattnerd031a452007-10-07 02:00:24 +00001378 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001379 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001380}
1381
Steve Naroff0add5d22007-11-03 11:27:19 +00001382Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001383 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001384 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001385
1386 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1387 // expressions. At this point, we know that the only valid thing that starts
1388 // with '@' is an @"".
1389 llvm::SmallVector<SourceLocation, 4> AtLocs;
1390 llvm::SmallVector<ExprTy*, 4> AtStrings;
1391 AtLocs.push_back(AtLoc);
1392 AtStrings.push_back(Res.Val);
1393
1394 while (Tok.is(tok::at)) {
1395 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001396
Chris Lattnerddd3e632007-12-12 01:04:12 +00001397 ExprResult Res(true); // Invalid unless there is a string literal.
1398 if (isTokenStringLiteral())
1399 Res = ParseStringLiteralExpression();
1400 else
1401 Diag(Tok, diag::err_objc_concat_string);
1402
1403 if (Res.isInvalid) {
1404 while (!AtStrings.empty()) {
1405 Actions.DeleteExpr(AtStrings.back());
1406 AtStrings.pop_back();
1407 }
1408 return Res;
1409 }
1410
1411 AtStrings.push_back(Res.Val);
1412 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001413
Chris Lattnerddd3e632007-12-12 01:04:12 +00001414 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1415 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001416}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001417
1418/// objc-encode-expression:
1419/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001420Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001421 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001422
1423 SourceLocation EncLoc = ConsumeToken();
1424
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001425 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001426 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1427 return true;
1428 }
1429
1430 SourceLocation LParenLoc = ConsumeParen();
1431
1432 TypeTy *Ty = ParseTypeName();
1433
Anders Carlsson92faeb82007-08-23 15:31:37 +00001434 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001435
Chris Lattnercfd61c82007-10-16 22:51:17 +00001436 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001437 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001438}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001439
1440/// objc-protocol-expression
1441/// @protocol ( protocol-name )
1442
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001443Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001444{
1445 SourceLocation ProtoLoc = ConsumeToken();
1446
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001447 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001448 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1449 return true;
1450 }
1451
1452 SourceLocation LParenLoc = ConsumeParen();
1453
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001454 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001455 Diag(Tok, diag::err_expected_ident);
1456 return true;
1457 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001458 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001459 ConsumeToken();
1460
Anders Carlsson92faeb82007-08-23 15:31:37 +00001461 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001462
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001463 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1464 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001465}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001466
1467/// objc-selector-expression
1468/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001469Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001470{
1471 SourceLocation SelectorLoc = ConsumeToken();
1472
1473 if (Tok.isNot(tok::l_paren)) {
1474 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1475 return 0;
1476 }
1477
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001478 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001479 SourceLocation LParenLoc = ConsumeParen();
1480 SourceLocation sLoc;
1481 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1482 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001483 Diag(Tok, diag::err_expected_ident); // missing selector name.
1484 return 0;
1485 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001486 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001487 unsigned nColons = 0;
1488 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001489 while (1) {
1490 if (Tok.isNot(tok::colon)) {
1491 Diag(Tok, diag::err_expected_colon);
1492 break;
1493 }
Chris Lattner847f5c12007-12-27 19:57:00 +00001494 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001495 ConsumeToken(); // Eat the ':'.
1496 if (Tok.is(tok::r_paren))
1497 break;
1498 // Check for another keyword selector.
1499 SourceLocation Loc;
1500 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001501 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001502 if (!SelIdent && Tok.isNot(tok::colon))
1503 break;
1504 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001505 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001506 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001507 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001508 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001509 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001510 }