blob: 5a9b980b6dd436d34c9fc3e10c5a6de89bf37019 [file] [log] [blame]
Ted Kremenek42730c52008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Steve Narofffb367882007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff87c329f2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris 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///
Ted Kremenek42730c52008-01-07 19:49:32 +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.
Ted Kremenek42730c52008-01-07 19:49:32 +0000306 if (II == ObjCPropertyAttrs[objc_getter] ||
307 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000308 // 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)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000313 if (II == ObjCPropertyAttrs[objc_setter]) {
314 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000315 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 {
Ted Kremenek42730c52008-01-07 19:49:32 +0000324 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000325 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
Ted Kremenek42730c52008-01-07 19:49:32 +0000341 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);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000353
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");
Chris Lattner3dd8d392008-04-10 06:46:29 +0000381 ObjCDeclSpec OCDS;
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.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000386 ParseObjCPropertyAttribute(OCDS);
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;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000390
391 // Parse all the comma separated declarators.
392 DeclSpec DS;
393 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
394 ParseStructDeclaration(DS, FieldDeclarators);
395
396 // Convert them all to fields.
397 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
398 FieldDeclarator &FD = FieldDeclarators[i];
399 // Install the declarator into interfaceDecl.
400 DeclTy *Field = Actions.ActOnField(CurScope, interfaceDecl,
401 DS.getSourceRange().getBegin(),
402 FD.D, FD.BitfieldSize);
403 PropertyDecls.push_back(Field);
404 }
405
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000406 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000407 ConsumeToken();
408 else {
409 Diag(Tok, diag::err_expected_semi_decl_list);
410 SkipUntil(tok::r_brace, true, true);
411 }
Chris Lattner44859612008-03-17 01:19:02 +0000412 return Actions.ActOnAddObjCProperties(AtLoc, &PropertyDecls[0],
Chris Lattner3dd8d392008-04-10 06:46:29 +0000413 PropertyDecls.size(), OCDS);
Chris Lattner4b009652007-07-25 00:24:17 +0000414}
Steve Narofffb367882007-08-20 21:31:48 +0000415
Steve Naroff81f1bba2007-09-06 21:24:23 +0000416/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000417/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000418/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000419///
420/// objc-instance-method: '-'
421/// objc-class-method: '+'
422///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000423/// objc-method-attributes: [OBJC2]
424/// __attribute__((deprecated))
425///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000426Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000427 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000428 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000429
430 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000431 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000432
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000433 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000434 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000435 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000436 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000437}
438
439/// objc-selector:
440/// identifier
441/// one of
442/// enum struct union if else while do for switch case default
443/// break continue return goto asm sizeof typeof __alignof
444/// unsigned long const short volatile signed restrict _Complex
445/// in out inout bycopy byref oneway int char float double void _Bool
446///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000447IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000448 switch (Tok.getKind()) {
449 default:
450 return 0;
451 case tok::identifier:
452 case tok::kw_typeof:
453 case tok::kw___alignof:
454 case tok::kw_auto:
455 case tok::kw_break:
456 case tok::kw_case:
457 case tok::kw_char:
458 case tok::kw_const:
459 case tok::kw_continue:
460 case tok::kw_default:
461 case tok::kw_do:
462 case tok::kw_double:
463 case tok::kw_else:
464 case tok::kw_enum:
465 case tok::kw_extern:
466 case tok::kw_float:
467 case tok::kw_for:
468 case tok::kw_goto:
469 case tok::kw_if:
470 case tok::kw_inline:
471 case tok::kw_int:
472 case tok::kw_long:
473 case tok::kw_register:
474 case tok::kw_restrict:
475 case tok::kw_return:
476 case tok::kw_short:
477 case tok::kw_signed:
478 case tok::kw_sizeof:
479 case tok::kw_static:
480 case tok::kw_struct:
481 case tok::kw_switch:
482 case tok::kw_typedef:
483 case tok::kw_union:
484 case tok::kw_unsigned:
485 case tok::kw_void:
486 case tok::kw_volatile:
487 case tok::kw_while:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000488 case tok::kw_bool:
Chris Lattnerd031a452007-10-07 02:00:24 +0000489 case tok::kw__Bool:
490 case tok::kw__Complex:
491 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000492 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000493 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000494 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000495}
496
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000497/// property-attrlist: one of
498/// readonly getter setter assign retain copy nonatomic
499///
500bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000501 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000502 const IdentifierInfo *II = Tok.getIdentifierInfo();
503 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000504 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000505 }
506 return false;
507}
508
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000509/// objc-for-collection-in: 'in'
510///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000511bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000512 // FIXME: May have to do additional look-ahead to only allow for
513 // valid tokens following an 'in'; such as an identifier, unary operators,
514 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000515 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
516 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000517}
518
Ted Kremenek42730c52008-01-07 19:49:32 +0000519/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000520/// qualifier list and builds their bitmask representation in the input
521/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000522///
523/// objc-type-qualifiers:
524/// objc-type-qualifier
525/// objc-type-qualifiers objc-type-qualifier
526///
Ted Kremenek42730c52008-01-07 19:49:32 +0000527void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000528 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000529 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000530 return;
531
532 const IdentifierInfo *II = Tok.getIdentifierInfo();
533 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000534 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000535 continue;
536
Ted Kremenek42730c52008-01-07 19:49:32 +0000537 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000538 switch (i) {
539 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000540 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
541 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
542 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
543 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
544 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
545 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000546 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000547 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000548 ConsumeToken();
549 II = 0;
550 break;
551 }
552
553 // If this wasn't a recognized qualifier, bail out.
554 if (II) return;
555 }
556}
557
558/// objc-type-name:
559/// '(' objc-type-qualifiers[opt] type-name ')'
560/// '(' objc-type-qualifiers[opt] ')'
561///
Ted Kremenek42730c52008-01-07 19:49:32 +0000562Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000563 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000564
565 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000566 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000567
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000568 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000569 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000570
Steve Naroff0bbffd82007-08-22 16:35:03 +0000571 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000572 Ty = ParseTypeName();
573 // FIXME: back when Sema support is in place...
574 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000575 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000576 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000577 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000578 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000579 }
580 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000581 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000582}
583
584/// objc-method-decl:
585/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000586/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000587/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000588/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000589///
590/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000591/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000592/// objc-keyword-selector objc-keyword-decl
593///
594/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000595/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
596/// objc-selector ':' objc-keyword-attributes[opt] identifier
597/// ':' objc-type-name objc-keyword-attributes[opt] identifier
598/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000599///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000600/// objc-parmlist:
601/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000602///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000603/// objc-parms:
604/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000605///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000606/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000607/// , ...
608///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000609/// objc-keyword-attributes: [OBJC2]
610/// __attribute__((unused))
611///
Steve Naroff3774dd92007-10-26 20:53:56 +0000612Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000613 tok::TokenKind mType,
614 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000615 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000616{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000617 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000618 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000620 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000621 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000622 SourceLocation selLoc;
623 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000624 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000625 if (!SelIdent) {
626 Diag(Tok, diag::err_expected_ident); // missing selector name.
627 // FIXME: this creates a unary selector with a null identifier, is this
628 // ok?? Maybe we should skip to the next semicolon or something.
629 }
630
631 // If attributes exist after the method, parse them.
632 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000633 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000634 MethodAttrs = ParseAttributes();
635
636 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000637 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000638 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000639 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000640 }
Steve Naroff304ed392007-09-05 23:30:30 +0000641
Steve Naroff4ed9d662007-09-27 14:38:14 +0000642 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
643 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000644 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000645 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000646
647 Action::TypeTy *TypeInfo;
648 while (1) {
649 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000650
Chris Lattnerd031a452007-10-07 02:00:24 +0000651 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000652 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000653 Diag(Tok, diag::err_expected_colon);
654 break;
655 }
656 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000657 ObjCDeclSpec DSType;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000658 if (Tok.is(tok::l_paren)) { // Parse the argument type.
659 TypeInfo = ParseObjCTypeName(DSType);
660 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000661 else
662 TypeInfo = 0;
663 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000664 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000665
Chris Lattnerd031a452007-10-07 02:00:24 +0000666 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000667 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000668 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000669
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000670 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000671 Diag(Tok, diag::err_expected_ident); // missing argument name.
672 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000673 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000674 ArgNames.push_back(Tok.getIdentifierInfo());
675 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000676
Chris Lattnerd031a452007-10-07 02:00:24 +0000677 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000678 SourceLocation Loc;
679 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000680 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000681 break;
682 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000683 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000684
Steve Naroff29fe7462007-11-15 12:35:21 +0000685 bool isVariadic = false;
686
Chris Lattnerd031a452007-10-07 02:00:24 +0000687 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000688 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000689 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000690 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000691 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000692 ConsumeToken();
693 break;
694 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000695 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000696 // Parse the c-style argument declaration-specifier.
697 DeclSpec DS;
698 ParseDeclarationSpecifiers(DS);
699 // Parse the declarator.
700 Declarator ParmDecl(DS, Declarator::PrototypeContext);
701 ParseDeclarator(ParmDecl);
702 }
703
704 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000705 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000706 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000707 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000708 MethodAttrs = ParseAttributes();
709
710 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
711 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000712 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000713 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000714 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000715 &ArgNames[0], MethodAttrs,
716 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000717}
718
Steve Narofffb367882007-08-20 21:31:48 +0000719/// objc-protocol-refs:
720/// '<' identifier-list '>'
721///
Steve Naroff304ed392007-09-05 23:30:30 +0000722bool Parser::ParseObjCProtocolReferences(
Chris Lattnere1352302008-04-07 04:56:42 +0000723 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000724 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000725
726 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000727
728 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000729 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000730 Diag(Tok, diag::err_expected_ident);
731 SkipUntil(tok::greater);
732 return true;
733 }
734 ProtocolRefs.push_back(Tok.getIdentifierInfo());
735 ConsumeToken();
736
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000737 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000738 break;
739 ConsumeToken();
740 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000741
Steve Narofffb367882007-08-20 21:31:48 +0000742 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000743 if (Tok.is(tok::greater)) {
744 endLoc = ConsumeAnyToken();
745 return false;
746 }
747 Diag(Tok, diag::err_expected_greater);
748 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000749}
750
751/// objc-class-instance-variables:
752/// '{' objc-instance-variable-decl-list[opt] '}'
753///
754/// objc-instance-variable-decl-list:
755/// objc-visibility-spec
756/// objc-instance-variable-decl ';'
757/// ';'
758/// objc-instance-variable-decl-list objc-visibility-spec
759/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
760/// objc-instance-variable-decl-list ';'
761///
762/// objc-visibility-spec:
763/// @private
764/// @protected
765/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000766/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000767///
768/// objc-instance-variable-decl:
769/// struct-declaration
770///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000771void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
772 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000773 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000774 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
775 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000776 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
777
Steve Naroffc4474992007-08-21 21:17:12 +0000778 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000779
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000780 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000781 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000782 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000783 // Each iteration of this loop reads one objc-instance-variable-decl.
784
785 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000786 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000787 Diag(Tok, diag::ext_extra_struct_semi);
788 ConsumeToken();
789 continue;
790 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000791
Steve Naroffc4474992007-08-21 21:17:12 +0000792 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000793 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000794 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000795 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000796 case tok::objc_private:
797 case tok::objc_public:
798 case tok::objc_protected:
799 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000800 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000801 ConsumeToken();
802 continue;
803 default:
804 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000805 continue;
806 }
807 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000808
809 // Parse all the comma separated declarators.
810 DeclSpec DS;
811 FieldDeclarators.clear();
812 ParseStructDeclaration(DS, FieldDeclarators);
813
814 // Convert them all to fields.
815 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
816 FieldDeclarator &FD = FieldDeclarators[i];
817 // Install the declarator into interfaceDecl.
818 DeclTy *Field = Actions.ActOnField(CurScope, interfaceDecl,
819 DS.getSourceRange().getBegin(),
820 FD.D, FD.BitfieldSize);
821 AllIvarDecls.push_back(Field);
Chris Lattner847f5c12007-12-27 19:57:00 +0000822 AllVisibilities.push_back(visibility);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000823 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000824
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000825 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000826 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000827 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000828 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
829 break;
830 } else {
831 Diag(Tok, diag::err_expected_semi_decl_list);
832 // Skip to end of block or statement
833 SkipUntil(tok::r_brace, true, true);
834 }
835 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000836 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000837 // Call ActOnFields() even if we don't have any decls. This is useful
838 // for code rewriting tools that need to be aware of the empty list.
839 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
840 &AllIvarDecls[0], AllIvarDecls.size(),
841 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Steve Naroffc4474992007-08-21 21:17:12 +0000842 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000843}
Steve Narofffb367882007-08-20 21:31:48 +0000844
845/// objc-protocol-declaration:
846/// objc-protocol-definition
847/// objc-protocol-forward-reference
848///
849/// objc-protocol-definition:
850/// @protocol identifier
851/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000852/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000853/// @end
854///
855/// objc-protocol-forward-reference:
856/// @protocol identifier-list ';'
857///
858/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000859/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000860/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000861Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000862 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000863 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
864 ConsumeToken(); // the "protocol" identifier
865
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000866 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000867 Diag(Tok, diag::err_expected_ident); // missing protocol name.
868 return 0;
869 }
870 // Save the protocol name, then consume it.
871 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
872 SourceLocation nameLoc = ConsumeToken();
873
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000874 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000875 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000876 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000877 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000878 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000879 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000880 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000881 ProtocolRefs.push_back(protocolName);
882
883 while (1) {
884 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000885 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000886 Diag(Tok, diag::err_expected_ident);
887 SkipUntil(tok::semi);
888 return 0;
889 }
890 ProtocolRefs.push_back(Tok.getIdentifierInfo());
891 ConsumeToken(); // the identifier
892
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000893 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000894 break;
895 }
896 // Consume the ';'.
897 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
898 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000899 }
Chris Lattner7afba9c2008-03-16 20:19:15 +0000900 if (!ProtocolRefs.empty())
Steve Naroff415c1832007-10-10 17:32:04 +0000901 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000902 &ProtocolRefs[0],
903 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000904 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000905 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000906 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000907 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000908 return 0;
909 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000910
Steve Naroff415c1832007-10-10 17:32:04 +0000911 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000912 protocolName, nameLoc,
913 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000914 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000915 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000916
917 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000918 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000919 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000920 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000921 }
922 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000923 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000924}
Steve Narofffb367882007-08-20 21:31:48 +0000925
926/// objc-implementation:
927/// objc-class-implementation-prologue
928/// objc-category-implementation-prologue
929///
930/// objc-class-implementation-prologue:
931/// @implementation identifier objc-superclass[opt]
932/// objc-class-instance-variables[opt]
933///
934/// objc-category-implementation-prologue:
935/// @implementation identifier ( identifier )
936
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000937Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
938 SourceLocation atLoc) {
939 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
940 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
941 ConsumeToken(); // the "implementation" identifier
942
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000943 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000944 Diag(Tok, diag::err_expected_ident); // missing class or category name.
945 return 0;
946 }
947 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000948 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000949 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
950
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000951 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000952 // we have a category implementation.
953 SourceLocation lparenLoc = ConsumeParen();
954 SourceLocation categoryLoc, rparenLoc;
955 IdentifierInfo *categoryId = 0;
956
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000957 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000958 categoryId = Tok.getIdentifierInfo();
959 categoryLoc = ConsumeToken();
960 } else {
961 Diag(Tok, diag::err_expected_ident); // missing category name.
962 return 0;
963 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000964 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000965 Diag(Tok, diag::err_expected_rparen);
966 SkipUntil(tok::r_paren, false); // don't stop at ';'
967 return 0;
968 }
969 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000970 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000971 atLoc, nameId, nameLoc, categoryId,
972 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000973 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000974 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000975 }
976 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000977 SourceLocation superClassLoc;
978 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000979 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000980 // We have a super class
981 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000982 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000983 Diag(Tok, diag::err_expected_ident); // missing super class name.
984 return 0;
985 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000986 superClassId = Tok.getIdentifierInfo();
987 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000988 }
Steve Naroff415c1832007-10-10 17:32:04 +0000989 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +0000990 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000991 superClassId, superClassLoc);
992
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000993 if (Tok.is(tok::l_brace)) // we have ivars
994 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000995 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000996
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000997 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000998}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000999
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001000Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1001 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1002 "ParseObjCAtEndDeclaration(): Expected @end");
1003 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001004 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001005 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001006 else
1007 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001008 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001009}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001010
1011/// compatibility-alias-decl:
1012/// @compatibility_alias alias-name class-name ';'
1013///
1014Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1015 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1016 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1017 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001018 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001019 Diag(Tok, diag::err_expected_ident);
1020 return 0;
1021 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001022 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1023 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001024 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001025 Diag(Tok, diag::err_expected_ident);
1026 return 0;
1027 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001028 IdentifierInfo *classId = Tok.getIdentifierInfo();
1029 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1030 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001031 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001032 return 0;
1033 }
1034 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1035 aliasId, aliasLoc,
1036 classId, classLoc);
1037 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001038}
1039
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001040/// property-synthesis:
1041/// @synthesize property-ivar-list ';'
1042///
1043/// property-ivar-list:
1044/// property-ivar
1045/// property-ivar-list ',' property-ivar
1046///
1047/// property-ivar:
1048/// identifier
1049/// identifier '=' identifier
1050///
1051Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1052 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1053 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1054 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001055 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001056 Diag(Tok, diag::err_expected_ident);
1057 return 0;
1058 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001059 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001060 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001061 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001062 // property '=' ivar-name
1063 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001064 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001065 Diag(Tok, diag::err_expected_ident);
1066 break;
1067 }
1068 ConsumeToken(); // consume ivar-name
1069 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001070 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001071 break;
1072 ConsumeToken(); // consume ','
1073 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001075 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1076 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001077}
1078
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001079/// property-dynamic:
1080/// @dynamic property-list
1081///
1082/// property-list:
1083/// identifier
1084/// property-list ',' identifier
1085///
1086Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1087 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1088 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1089 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001090 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001091 Diag(Tok, diag::err_expected_ident);
1092 return 0;
1093 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001094 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001095 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001097 break;
1098 ConsumeToken(); // consume ','
1099 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001100 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001101 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1102 return 0;
1103}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001104
1105/// objc-throw-statement:
1106/// throw expression[opt];
1107///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001108Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1109 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001110 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001111 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001112 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001113 if (Res.isInvalid) {
1114 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001115 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001116 }
1117 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001118 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001119 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001120}
1121
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001122/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001123/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001124///
1125Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001126 ConsumeToken(); // consume synchronized
1127 if (Tok.isNot(tok::l_paren)) {
1128 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1129 return true;
1130 }
1131 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001132 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001133 if (Res.isInvalid) {
1134 SkipUntil(tok::semi);
1135 return true;
1136 }
1137 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001138 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001139 return true;
1140 }
1141 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001142 if (Tok.isNot(tok::l_brace)) {
1143 Diag (Tok, diag::err_expected_lbrace);
1144 return true;
1145 }
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001146 StmtResult SynchBody = ParseCompoundStatementBody();
1147 if (SynchBody.isInvalid)
1148 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1149 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001150}
1151
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001152/// objc-try-catch-statement:
1153/// @try compound-statement objc-catch-list[opt]
1154/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1155///
1156/// objc-catch-list:
1157/// @catch ( parameter-declaration ) compound-statement
1158/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1159/// catch-parameter-declaration:
1160/// parameter-declaration
1161/// '...' [OBJC2]
1162///
Chris Lattner80712392008-03-10 06:06:04 +00001163Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001164 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001165
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001166 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001168 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001169 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001170 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001171 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001172 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001173 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001174 if (TryBody.isInvalid)
1175 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001176
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001177 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001178 // At this point, we need to lookahead to determine if this @ is the start
1179 // of an @catch or @finally. We don't want to consume the @ token if this
1180 // is an @try or @encode or something else.
1181 Token AfterAt = GetLookAheadToken(1);
1182 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1183 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1184 break;
1185
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001186 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001187 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001188 StmtTy *FirstPart = 0;
1189 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001190 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001191 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001192 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001193 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001194 DeclSpec DS;
1195 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001196 // FIXME: Is BlockContext right?
1197 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1198 ParseDeclarator(DeclaratorInfo);
Chris Lattnera4ff4272008-03-13 06:29:04 +00001199 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1200 DeclaratorInfo, 0);
1201 StmtResult stmtResult =
1202 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1203 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian06798362007-11-01 23:59:59 +00001204 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroffc949a462008-02-05 21:27:35 +00001205 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001206 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001207 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001208
1209 StmtResult CatchBody(true);
1210 if (Tok.is(tok::l_brace))
1211 CatchBody = ParseCompoundStatementBody();
1212 else
1213 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001214 if (CatchBody.isInvalid)
1215 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001216 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001217 FirstPart, CatchBody.Val, CatchStmts.Val);
1218 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001219 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001220 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1221 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001222 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001223 }
1224 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001225 } else {
1226 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001227 ConsumeToken(); // consume finally
Chris Lattner8027be62008-02-14 19:27:54 +00001228
1229 StmtResult FinallyBody(true);
1230 if (Tok.is(tok::l_brace))
1231 FinallyBody = ParseCompoundStatementBody();
1232 else
1233 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001234 if (FinallyBody.isInvalid)
1235 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001236 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001237 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001238 catch_or_finally_seen = true;
1239 break;
1240 }
1241 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001242 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001243 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001244 return true;
1245 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001246 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001247 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001248}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001249
Steve Naroff81f1bba2007-09-06 21:24:23 +00001250/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001251///
Steve Naroff18c83382007-11-13 23:01:27 +00001252Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001253 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001254 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001255 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001256 ConsumeToken();
1257
Steve Naroff9191a9e82007-11-11 19:54:21 +00001258 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001259 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001260 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001261
1262 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1263 SkipUntil(tok::l_brace, true, true);
1264
1265 // If we didn't find the '{', bail out.
1266 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001267 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001268 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001269 SourceLocation BraceLoc = Tok.getLocation();
1270
1271 // Enter a scope for the method body.
1272 EnterScope(Scope::FnScope|Scope::DeclScope);
1273
1274 // Tell the actions module that we have entered a method definition with the
1275 // specified Declarator for the method.
Ted Kremenek42730c52008-01-07 19:49:32 +00001276 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001277
1278 StmtResult FnBody = ParseCompoundStatementBody();
1279
1280 // If the function body could not be parsed, make a bogus compoundstmt.
1281 if (FnBody.isInvalid)
1282 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1283
1284 // Leave the function body scope.
1285 ExitScope();
1286
1287 // TODO: Pass argument information.
Steve Naroff99ee4302007-11-11 23:20:51 +00001288 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001289 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001290}
Anders Carlssona66cad42007-08-21 17:43:55 +00001291
Steve Naroffc949a462008-02-05 21:27:35 +00001292Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1293 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001294 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001295 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1296 return ParseObjCThrowStmt(AtLoc);
1297 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1298 return ParseObjCSynchronizedStmt(AtLoc);
1299 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1300 if (Res.isInvalid) {
1301 // If the expression is invalid, skip ahead to the next semicolon. Not
1302 // doing this opens us up to the possibility of infinite loops if
1303 // ParseExpression does not consume any tokens.
1304 SkipUntil(tok::semi);
1305 return true;
1306 }
1307 // Otherwise, eat the semicolon.
1308 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1309 return Actions.ActOnExprStmt(Res.Val);
1310}
1311
Steve Narofffb9dd752007-10-15 20:55:58 +00001312Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001313
1314 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001315 case tok::string_literal: // primary-expression: string-literal
1316 case tok::wide_string_literal:
1317 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1318 default:
1319 break;
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001320 }
1321
1322 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001323 case tok::objc_encode:
1324 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1325 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001326 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001327 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001328 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001329 default:
1330 Diag(AtLoc, diag::err_unexpected_at);
1331 SkipUntil(tok::semi);
Chris Lattnerfd44db32008-01-30 21:20:25 +00001332 return true;
Anders Carlssona66cad42007-08-21 17:43:55 +00001333 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001334}
1335
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001336/// objc-message-expr:
1337/// '[' objc-receiver objc-message-args ']'
1338///
1339/// objc-receiver:
1340/// expression
1341/// class-name
1342/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001343Parser::ExprResult Parser::ParseObjCMessageExpression() {
1344 assert(Tok.is(tok::l_square) && "'[' expected");
1345 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1346
1347 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001348 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001349 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1350 ConsumeToken();
1351 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1352 }
1353
1354 ExprResult Res = ParseAssignmentExpression();
1355 if (Res.isInvalid) {
1356 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattnere69015d2008-01-25 19:43:26 +00001357 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001358 return Res;
1359 }
1360 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1361}
1362
1363/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1364/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001365///
1366/// objc-message-args:
1367/// objc-selector
1368/// objc-keywordarg-list
1369///
1370/// objc-keywordarg-list:
1371/// objc-keywordarg
1372/// objc-keywordarg-list objc-keywordarg
1373///
1374/// objc-keywordarg:
1375/// selector-name[opt] ':' objc-keywordexpr
1376///
1377/// objc-keywordexpr:
1378/// nonempty-expr-list
1379///
1380/// nonempty-expr-list:
1381/// assignment-expression
1382/// nonempty-expr-list , assignment-expression
1383///
Chris Lattnered27a532008-01-25 18:59:06 +00001384Parser::ExprResult
1385Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1386 IdentifierInfo *ReceiverName,
1387 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001388 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001389 SourceLocation Loc;
1390 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001391
1392 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1393 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1394
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001395 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001396 while (1) {
1397 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001398 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001399
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001400 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001401 Diag(Tok, diag::err_expected_colon);
1402 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001403 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001404 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001405 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001406 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001407 ExprResult Res = ParseAssignmentExpression();
1408 if (Res.isInvalid) {
1409 SkipUntil(tok::identifier);
1410 return Res;
1411 }
1412 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001413 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001414
1415 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001416 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001417 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001418 break;
1419 // We have a selector or a colon, continue parsing.
1420 }
1421 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001422 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001423 ConsumeToken(); // Eat the ','.
1424 /// Parse the expression after ','
1425 ExprResult Res = ParseAssignmentExpression();
1426 if (Res.isInvalid) {
1427 SkipUntil(tok::identifier);
1428 return Res;
1429 }
1430 // We have a valid expression.
1431 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001432 }
1433 } else if (!selIdent) {
1434 Diag(Tok, diag::err_expected_ident); // missing selector name.
1435 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001436 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001437 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001438
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001439 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001440 Diag(Tok, diag::err_expected_rsquare);
1441 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001442 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001443 }
Chris Lattnered27a532008-01-25 18:59:06 +00001444 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001445
Steve Narofff9e80db2007-10-05 18:42:47 +00001446 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001447 if (nKeys == 0)
1448 KeyIdents.push_back(selIdent);
1449 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1450
1451 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001452 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001453 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001454 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001455 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001456 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001457 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001458}
1459
Steve Naroff0add5d22007-11-03 11:27:19 +00001460Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001461 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001462 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001463
1464 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1465 // expressions. At this point, we know that the only valid thing that starts
1466 // with '@' is an @"".
1467 llvm::SmallVector<SourceLocation, 4> AtLocs;
1468 llvm::SmallVector<ExprTy*, 4> AtStrings;
1469 AtLocs.push_back(AtLoc);
1470 AtStrings.push_back(Res.Val);
1471
1472 while (Tok.is(tok::at)) {
1473 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001474
Chris Lattnerddd3e632007-12-12 01:04:12 +00001475 ExprResult Res(true); // Invalid unless there is a string literal.
1476 if (isTokenStringLiteral())
1477 Res = ParseStringLiteralExpression();
1478 else
1479 Diag(Tok, diag::err_objc_concat_string);
1480
1481 if (Res.isInvalid) {
1482 while (!AtStrings.empty()) {
1483 Actions.DeleteExpr(AtStrings.back());
1484 AtStrings.pop_back();
1485 }
1486 return Res;
1487 }
1488
1489 AtStrings.push_back(Res.Val);
1490 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001491
Chris Lattnerddd3e632007-12-12 01:04:12 +00001492 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1493 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001494}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001495
1496/// objc-encode-expression:
1497/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001498Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001499 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001500
1501 SourceLocation EncLoc = ConsumeToken();
1502
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001503 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001504 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1505 return true;
1506 }
1507
1508 SourceLocation LParenLoc = ConsumeParen();
1509
1510 TypeTy *Ty = ParseTypeName();
1511
Anders Carlsson92faeb82007-08-23 15:31:37 +00001512 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001513
Chris Lattnercfd61c82007-10-16 22:51:17 +00001514 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001515 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001516}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001517
1518/// objc-protocol-expression
1519/// @protocol ( protocol-name )
1520
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001521Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001522{
1523 SourceLocation ProtoLoc = ConsumeToken();
1524
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001525 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001526 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1527 return true;
1528 }
1529
1530 SourceLocation LParenLoc = ConsumeParen();
1531
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001532 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001533 Diag(Tok, diag::err_expected_ident);
1534 return true;
1535 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001536 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001537 ConsumeToken();
1538
Anders Carlsson92faeb82007-08-23 15:31:37 +00001539 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001540
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001541 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1542 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001543}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001544
1545/// objc-selector-expression
1546/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001547Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001548{
1549 SourceLocation SelectorLoc = ConsumeToken();
1550
1551 if (Tok.isNot(tok::l_paren)) {
1552 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1553 return 0;
1554 }
1555
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001556 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001557 SourceLocation LParenLoc = ConsumeParen();
1558 SourceLocation sLoc;
1559 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1560 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001561 Diag(Tok, diag::err_expected_ident); // missing selector name.
1562 return 0;
1563 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001564 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001565 unsigned nColons = 0;
1566 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001567 while (1) {
1568 if (Tok.isNot(tok::colon)) {
1569 Diag(Tok, diag::err_expected_colon);
1570 break;
1571 }
Chris Lattner847f5c12007-12-27 19:57:00 +00001572 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001573 ConsumeToken(); // Eat the ':'.
1574 if (Tok.is(tok::r_paren))
1575 break;
1576 // Check for another keyword selector.
1577 SourceLocation Loc;
1578 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001579 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001580 if (!SelIdent && Tok.isNot(tok::colon))
1581 break;
1582 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001583 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001584 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001585 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001586 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001587 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001588 }