blob: 4e8790b40fff12dae7e067816eb6974b2196fb21 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Steve Naroffdac269b2007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff861cf3e2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
Steve Naroffdac269b2007-08-20 21:31:48 +000037 return ParseObjCAtInterfaceDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000038 case tok::objc_protocol:
Steve Naroff7ef58fd2007-08-22 22:17:26 +000039 return ParseObjCAtProtocolDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000040 case tok::objc_implementation:
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +000041 return ParseObjCAtImplementationDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000042 case tok::objc_end:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000043 return ParseObjCAtEndDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000044 case tok::objc_compatibility_alias:
Fariborz Jahaniane992af02007-09-04 19:26:51 +000045 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000046 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000053 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Naroffdac269b2007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000069 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
Reid Spencer5f016e22007-07-11 17:01:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnerdf195262007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000082 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000083
Steve Naroffe440eb82007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Steve Naroffdac269b2007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
116Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnerdf195262007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
124 return 0;
125 }
126 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnerdf195262007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000134 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffdac269b2007-08-20 21:31:48 +0000135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Steve Narofff908a872007-10-30 02:23:23 +0000150 SourceLocation endProtoLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattnerdf195262007-10-09 17:51:17 +0000152 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000153 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-08-20 21:31:48 +0000154 return 0;
155 }
156 if (attrList) // categories don't support attributes.
157 Diag(Tok, diag::err_objc_no_attributes_on_category);
158
Steve Naroffe440eb82007-10-10 17:32:04 +0000159 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000160 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000161 &ProtocolRefs[0], ProtocolRefs.size(),
162 endProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000163
164 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Naroffdac269b2007-08-20 21:31:48 +0000165
Steve Naroff294494e2007-08-22 16:35:03 +0000166 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000167 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000168 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000169 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 }
Steve Naroff294494e2007-08-22 16:35:03 +0000171 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000172 return 0;
173 }
174 // Parse a class interface.
175 IdentifierInfo *superClassId = 0;
176 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000177
Chris Lattnerdf195262007-10-09 17:51:17 +0000178 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000179 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing super class name.
182 return 0;
183 }
184 superClassId = Tok.getIdentifierInfo();
185 superClassLoc = ConsumeToken();
186 }
187 // Next, we need to check for any protocol references.
Steve Narofff28b2642007-09-05 23:30:30 +0000188 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000189 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000190 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000191 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-08-20 21:31:48 +0000192 return 0;
193 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000194 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Chris Lattnercb53b362007-12-27 19:57:00 +0000195 atLoc, nameId, nameLoc,
Steve Narofff28b2642007-09-05 23:30:30 +0000196 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000197 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000198
Chris Lattnerdf195262007-10-09 17:51:17 +0000199 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000200 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000201
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000202 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff294494e2007-08-22 16:35:03 +0000203
204 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000205 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000206 ConsumeToken(); // the "end" identifier
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000207 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000208 }
Steve Naroff294494e2007-08-22 16:35:03 +0000209 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000210 return 0;
211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff294494e2007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000225void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000226 tok::ObjCKeywordKind contextKey) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000227 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000228 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000229 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000230 SourceLocation AtEndLoc;
231
Steve Naroff294494e2007-08-22 16:35:03 +0000232 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 if (Tok.is(tok::at)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000234 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff861cf3e2007-08-23 18:16:40 +0000235 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff294494e2007-08-22 16:35:03 +0000236
237 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff60fccee2007-10-29 21:38:07 +0000238 AtEndLoc = AtLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000239 break;
Steve Naroff294494e2007-08-22 16:35:03 +0000240 } else if (ocKind == tok::objc_required) { // protocols only
241 ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +0000242 MethodImplKind = ocKind;
243 if (contextKey != tok::objc_protocol)
244 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff294494e2007-08-22 16:35:03 +0000245 } else if (ocKind == tok::objc_optional) { // protocols only
246 ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +0000247 MethodImplKind = ocKind;
248 if (contextKey != tok::objc_protocol)
249 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff294494e2007-08-22 16:35:03 +0000250 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000251 allProperties.push_back(ParseObjCPropertyDecl(interfaceDecl, AtLoc));
Steve Naroff294494e2007-08-22 16:35:03 +0000252 continue;
253 } else {
254 Diag(Tok, diag::err_objc_illegal_interface_qual);
255 ConsumeToken();
256 }
257 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000258 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
259 DeclTy *methodPrototype =
260 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000261 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000262 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
263 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000264 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000265 continue;
266 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000267 else if (Tok.is(tok::at))
268 continue;
269
Chris Lattnerdf195262007-10-09 17:51:17 +0000270 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000271 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000272 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000273 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000274 else {
Steve Naroff4985ace2007-08-22 18:35:33 +0000275 // FIXME: as the name implies, this rule allows function definitions.
276 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000277 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000278 }
Steve Naroff294494e2007-08-22 16:35:03 +0000279 }
Steve Naroff2feac5e2007-10-30 03:43:13 +0000280 /// Insert collected methods declarations into the @interface object.
Steve Naroff0416fb92007-11-11 17:19:15 +0000281 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
282 &allProperties[0], allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000283}
284
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000285/// Parse property attribute declarations.
286///
287/// property-attr-decl: '(' property-attrlist ')'
288/// property-attrlist:
289/// property-attribute
290/// property-attrlist ',' property-attribute
291/// property-attribute:
292/// getter '=' identifier
293/// setter '=' identifier ':'
294/// readonly
295/// readwrite
296/// assign
297/// retain
298/// copy
299/// nonatomic
300///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000301void Parser::ParseObjCPropertyAttribute (ObjCDeclSpec &DS) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000302 SourceLocation loc = ConsumeParen(); // consume '('
303 while (isObjCPropertyAttribute()) {
304 const IdentifierInfo *II = Tok.getIdentifierInfo();
305 // getter/setter require extra treatment.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000306 if (II == ObjCPropertyAttrs[objc_getter] ||
307 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000308 // skip getter/setter part.
309 SourceLocation loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000310 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000311 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000312 if (Tok.is(tok::identifier)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000313 if (II == ObjCPropertyAttrs[objc_setter]) {
314 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000315 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000316 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000317 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000318 Diag(loc, diag::err_expected_colon);
319 SkipUntil(tok::r_paren,true,true);
320 break;
321 }
322 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000323 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000324 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000325 DS.setGetterName(Tok.getIdentifierInfo());
326 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000327 }
328 else {
329 Diag(loc, diag::err_expected_ident);
Chris Lattnercb53b362007-12-27 19:57:00 +0000330 SkipUntil(tok::r_paren,true,true);
331 break;
332 }
Fariborz Jahaniand0f97d12007-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 Jahanian82a5fe32007-11-06 22:01:00 +0000340
Ted Kremeneka526c5c2008-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 Jahanian82a5fe32007-11-06 22:01:00 +0000353
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000354 ConsumeToken(); // consume last attribute token
Chris Lattnerdf195262007-10-09 17:51:17 +0000355 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000356 loc = ConsumeToken();
357 continue;
358 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000359 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000360 break;
361 Diag(loc, diag::err_expected_rparen);
362 SkipUntil(tok::semi);
363 return;
364 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000365 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000366 ConsumeParen();
367 else {
368 Diag(loc, diag::err_objc_expected_property_attr);
369 SkipUntil(tok::r_paren); // recover from error inside attribute list
370 }
371}
372
373/// Main routine to parse property declaration.
374///
375/// @property property-attr-decl[opt] property-component-decl ';'
376///
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000377Parser::DeclTy *Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl,
378 SourceLocation AtLoc) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000379 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
380 "ParseObjCPropertyDecl(): Expected @property");
Chris Lattnere1359422008-04-10 06:46:29 +0000381 ObjCDeclSpec OCDS;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000382 ConsumeToken(); // the "property" identifier
383 // Parse property attribute list, if any.
Chris Lattnerdf195262007-10-09 17:51:17 +0000384 if (Tok.is(tok::l_paren)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000385 // property has attribute list.
Chris Lattnere1359422008-04-10 06:46:29 +0000386 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000387 }
388 // Parse declaration portion of @property.
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000389 llvm::SmallVector<DeclTy*, 8> PropertyDecls;
Chris Lattnere1359422008-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.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000400 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000401 DS.getSourceRange().getBegin(),
Fariborz Jahanian45bc03f2008-04-11 16:55:42 +0000402 FD.D, FD.BitfieldSize,
403 tok::objc_not_keyword);
Chris Lattnere1359422008-04-10 06:46:29 +0000404 PropertyDecls.push_back(Field);
405 }
406
Chris Lattnerdf195262007-10-09 17:51:17 +0000407 if (Tok.is(tok::semi))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000408 ConsumeToken();
409 else {
410 Diag(Tok, diag::err_expected_semi_decl_list);
411 SkipUntil(tok::r_brace, true, true);
412 }
Chris Lattnerf4af5152008-03-17 01:19:02 +0000413 return Actions.ActOnAddObjCProperties(AtLoc, &PropertyDecls[0],
Chris Lattnere1359422008-04-10 06:46:29 +0000414 PropertyDecls.size(), OCDS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000415}
Steve Naroffdac269b2007-08-20 21:31:48 +0000416
Steve Naroff3536b442007-09-06 21:24:23 +0000417/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000418/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000419/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000420///
421/// objc-instance-method: '-'
422/// objc-class-method: '+'
423///
Steve Naroff4985ace2007-08-22 18:35:33 +0000424/// objc-method-attributes: [OBJC2]
425/// __attribute__((deprecated))
426///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000427Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000428 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000429 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000430
431 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000432 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000433
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000434 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000435 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000436 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000437 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000438}
439
440/// objc-selector:
441/// identifier
442/// one of
443/// enum struct union if else while do for switch case default
444/// break continue return goto asm sizeof typeof __alignof
445/// unsigned long const short volatile signed restrict _Complex
446/// in out inout bycopy byref oneway int char float double void _Bool
447///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000448IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000449 switch (Tok.getKind()) {
450 default:
451 return 0;
452 case tok::identifier:
453 case tok::kw_typeof:
454 case tok::kw___alignof:
455 case tok::kw_auto:
456 case tok::kw_break:
457 case tok::kw_case:
458 case tok::kw_char:
459 case tok::kw_const:
460 case tok::kw_continue:
461 case tok::kw_default:
462 case tok::kw_do:
463 case tok::kw_double:
464 case tok::kw_else:
465 case tok::kw_enum:
466 case tok::kw_extern:
467 case tok::kw_float:
468 case tok::kw_for:
469 case tok::kw_goto:
470 case tok::kw_if:
471 case tok::kw_inline:
472 case tok::kw_int:
473 case tok::kw_long:
474 case tok::kw_register:
475 case tok::kw_restrict:
476 case tok::kw_return:
477 case tok::kw_short:
478 case tok::kw_signed:
479 case tok::kw_sizeof:
480 case tok::kw_static:
481 case tok::kw_struct:
482 case tok::kw_switch:
483 case tok::kw_typedef:
484 case tok::kw_union:
485 case tok::kw_unsigned:
486 case tok::kw_void:
487 case tok::kw_volatile:
488 case tok::kw_while:
Chris Lattner9298d962007-11-15 05:25:19 +0000489 case tok::kw_bool:
Chris Lattnerff384912007-10-07 02:00:24 +0000490 case tok::kw__Bool:
491 case tok::kw__Complex:
492 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000493 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000494 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000495 }
Steve Naroff294494e2007-08-22 16:35:03 +0000496}
497
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000498/// property-attrlist: one of
499/// readonly getter setter assign retain copy nonatomic
500///
501bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000502 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000503 const IdentifierInfo *II = Tok.getIdentifierInfo();
504 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000505 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000506 }
507 return false;
508}
509
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000510/// objc-for-collection-in: 'in'
511///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000512bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000513 // FIXME: May have to do additional look-ahead to only allow for
514 // valid tokens following an 'in'; such as an identifier, unary operators,
515 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000516 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
517 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000518}
519
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000521/// qualifier list and builds their bitmask representation in the input
522/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000523///
524/// objc-type-qualifiers:
525/// objc-type-qualifier
526/// objc-type-qualifiers objc-type-qualifier
527///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000528void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000529 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000530 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000531 return;
532
533 const IdentifierInfo *II = Tok.getIdentifierInfo();
534 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000535 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000536 continue;
537
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000538 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000539 switch (i) {
540 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000541 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
542 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
543 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
544 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
545 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
546 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000547 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000548 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000549 ConsumeToken();
550 II = 0;
551 break;
552 }
553
554 // If this wasn't a recognized qualifier, bail out.
555 if (II) return;
556 }
557}
558
559/// objc-type-name:
560/// '(' objc-type-qualifiers[opt] type-name ')'
561/// '(' objc-type-qualifiers[opt] ')'
562///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000563Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000564 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000565
566 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000567 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000568
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000569 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000571
Steve Naroff294494e2007-08-22 16:35:03 +0000572 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000573 Ty = ParseTypeName();
574 // FIXME: back when Sema support is in place...
575 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000576 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000577 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000578 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000579 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000580 }
581 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000582 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000583}
584
585/// objc-method-decl:
586/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000587/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000588/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000589/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000590///
591/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000592/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000593/// objc-keyword-selector objc-keyword-decl
594///
595/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000596/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
597/// objc-selector ':' objc-keyword-attributes[opt] identifier
598/// ':' objc-type-name objc-keyword-attributes[opt] identifier
599/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000600///
Steve Naroff4985ace2007-08-22 18:35:33 +0000601/// objc-parmlist:
602/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000603///
Steve Naroff4985ace2007-08-22 18:35:33 +0000604/// objc-parms:
605/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000606///
Steve Naroff4985ace2007-08-22 18:35:33 +0000607/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000608/// , ...
609///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000610/// objc-keyword-attributes: [OBJC2]
611/// __attribute__((unused))
612///
Steve Naroffbef11852007-10-26 20:53:56 +0000613Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000614 tok::TokenKind mType,
615 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000616 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000617{
Steve Naroff4985ace2007-08-22 18:35:33 +0000618 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000619 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000621 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000622 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroffbef11852007-10-26 20:53:56 +0000623 SourceLocation selLoc;
624 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000625 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000626 if (!SelIdent) {
627 Diag(Tok, diag::err_expected_ident); // missing selector name.
628 // FIXME: this creates a unary selector with a null identifier, is this
629 // ok?? Maybe we should skip to the next semicolon or something.
630 }
631
632 // If attributes exist after the method, parse them.
633 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000634 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000635 MethodAttrs = ParseAttributes();
636
637 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000638 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000639 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000640 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000641 }
Steve Narofff28b2642007-09-05 23:30:30 +0000642
Steve Naroff68d331a2007-09-27 14:38:14 +0000643 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
644 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000645 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000646 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000647
648 Action::TypeTy *TypeInfo;
649 while (1) {
650 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000651
Chris Lattnerff384912007-10-07 02:00:24 +0000652 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000653 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000654 Diag(Tok, diag::err_expected_colon);
655 break;
656 }
657 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000658 ObjCDeclSpec DSType;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000659 if (Tok.is(tok::l_paren)) { // Parse the argument type.
660 TypeInfo = ParseObjCTypeName(DSType);
661 }
Chris Lattnerff384912007-10-07 02:00:24 +0000662 else
663 TypeInfo = 0;
664 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000665 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000666
Chris Lattnerff384912007-10-07 02:00:24 +0000667 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000668 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000669 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000670
Chris Lattnerdf195262007-10-09 17:51:17 +0000671 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000672 Diag(Tok, diag::err_expected_ident); // missing argument name.
673 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000674 }
Chris Lattnerff384912007-10-07 02:00:24 +0000675 ArgNames.push_back(Tok.getIdentifierInfo());
676 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000677
Chris Lattnerff384912007-10-07 02:00:24 +0000678 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000679 SourceLocation Loc;
680 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000681 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000682 break;
683 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000684 }
Chris Lattnerff384912007-10-07 02:00:24 +0000685
Steve Naroff335eafa2007-11-15 12:35:21 +0000686 bool isVariadic = false;
687
Chris Lattnerff384912007-10-07 02:00:24 +0000688 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000689 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000690 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000691 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000692 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000693 ConsumeToken();
694 break;
695 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000696 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000697 // Parse the c-style argument declaration-specifier.
698 DeclSpec DS;
699 ParseDeclarationSpecifiers(DS);
700 // Parse the declarator.
701 Declarator ParmDecl(DS, Declarator::PrototypeContext);
702 ParseDeclarator(ParmDecl);
703 }
704
705 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000706 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000707 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000708 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000709 MethodAttrs = ParseAttributes();
710
711 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
712 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000713 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000714 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000715 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000716 &ArgNames[0], MethodAttrs,
717 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000718}
719
Steve Naroffdac269b2007-08-20 21:31:48 +0000720/// objc-protocol-refs:
721/// '<' identifier-list '>'
722///
Steve Narofff28b2642007-09-05 23:30:30 +0000723bool Parser::ParseObjCProtocolReferences(
Chris Lattner88cb27a2008-04-07 04:56:42 +0000724 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnerdf195262007-10-09 17:51:17 +0000725 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000726
727 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000728
729 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000730 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000731 Diag(Tok, diag::err_expected_ident);
732 SkipUntil(tok::greater);
733 return true;
734 }
735 ProtocolRefs.push_back(Tok.getIdentifierInfo());
736 ConsumeToken();
737
Chris Lattnerdf195262007-10-09 17:51:17 +0000738 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000739 break;
740 ConsumeToken();
741 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000742
Steve Naroffdac269b2007-08-20 21:31:48 +0000743 // Consume the '>'.
Steve Narofff908a872007-10-30 02:23:23 +0000744 if (Tok.is(tok::greater)) {
745 endLoc = ConsumeAnyToken();
746 return false;
747 }
748 Diag(Tok, diag::err_expected_greater);
749 return true;
Steve Naroffdac269b2007-08-20 21:31:48 +0000750}
751
752/// objc-class-instance-variables:
753/// '{' objc-instance-variable-decl-list[opt] '}'
754///
755/// objc-instance-variable-decl-list:
756/// objc-visibility-spec
757/// objc-instance-variable-decl ';'
758/// ';'
759/// objc-instance-variable-decl-list objc-visibility-spec
760/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
761/// objc-instance-variable-decl-list ';'
762///
763/// objc-visibility-spec:
764/// @private
765/// @protected
766/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000767/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000768///
769/// objc-instance-variable-decl:
770/// struct-declaration
771///
Steve Naroff60fccee2007-10-29 21:38:07 +0000772void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
773 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000774 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000775 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000776 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
777
Steve Naroffddbff782007-08-21 21:17:12 +0000778 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000779
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000780 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffddbff782007-08-21 21:17:12 +0000781 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000782 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-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 Lattnerdf195262007-10-09 17:51:17 +0000786 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000787 Diag(Tok, diag::ext_extra_struct_semi);
788 ConsumeToken();
789 continue;
790 }
Chris Lattnere1359422008-04-10 06:46:29 +0000791
Steve Naroffddbff782007-08-21 21:17:12 +0000792 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000793 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000794 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000795 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-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 Naroff861cf3e2007-08-23 18:16:40 +0000800 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000801 ConsumeToken();
802 continue;
803 default:
804 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000805 continue;
806 }
807 }
Chris Lattnere1359422008-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.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000818 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000819 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000820 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000821 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000822 }
Steve Naroff3536b442007-09-06 21:24:23 +0000823
Chris Lattnerdf195262007-10-09 17:51:17 +0000824 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000825 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000826 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000827 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
828 break;
829 } else {
830 Diag(Tok, diag::err_expected_semi_decl_list);
831 // Skip to end of block or statement
832 SkipUntil(tok::r_brace, true, true);
833 }
834 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000835 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000836 // Call ActOnFields() even if we don't have any decls. This is useful
837 // for code rewriting tools that need to be aware of the empty list.
838 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
839 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000840 LBraceLoc, RBraceLoc);
Steve Naroffddbff782007-08-21 21:17:12 +0000841 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842}
Steve Naroffdac269b2007-08-20 21:31:48 +0000843
844/// objc-protocol-declaration:
845/// objc-protocol-definition
846/// objc-protocol-forward-reference
847///
848/// objc-protocol-definition:
849/// @protocol identifier
850/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000851/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000852/// @end
853///
854/// objc-protocol-forward-reference:
855/// @protocol identifier-list ';'
856///
857/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000858/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000859/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000860Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000861 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000862 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
863 ConsumeToken(); // the "protocol" identifier
864
Chris Lattnerdf195262007-10-09 17:51:17 +0000865 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000866 Diag(Tok, diag::err_expected_ident); // missing protocol name.
867 return 0;
868 }
869 // Save the protocol name, then consume it.
870 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
871 SourceLocation nameLoc = ConsumeToken();
872
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000873 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000874 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000875 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000876 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000877 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000878 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000879 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000880 ProtocolRefs.push_back(protocolName);
881
882 while (1) {
883 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000884 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000885 Diag(Tok, diag::err_expected_ident);
886 SkipUntil(tok::semi);
887 return 0;
888 }
889 ProtocolRefs.push_back(Tok.getIdentifierInfo());
890 ConsumeToken(); // the identifier
891
Chris Lattnerdf195262007-10-09 17:51:17 +0000892 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000893 break;
894 }
895 // Consume the ';'.
896 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
897 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000898 }
Chris Lattnerc8581052008-03-16 20:19:15 +0000899 if (!ProtocolRefs.empty())
Steve Naroffe440eb82007-10-10 17:32:04 +0000900 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000901 &ProtocolRefs[0],
902 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000903 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000904 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000905 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000906 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000907 return 0;
908 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000909
Steve Naroffe440eb82007-10-10 17:32:04 +0000910 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000911 protocolName, nameLoc,
912 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000913 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000914 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000915
916 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000917 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000918 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000919 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000920 }
921 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000922 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000923}
Steve Naroffdac269b2007-08-20 21:31:48 +0000924
925/// objc-implementation:
926/// objc-class-implementation-prologue
927/// objc-category-implementation-prologue
928///
929/// objc-class-implementation-prologue:
930/// @implementation identifier objc-superclass[opt]
931/// objc-class-instance-variables[opt]
932///
933/// objc-category-implementation-prologue:
934/// @implementation identifier ( identifier )
935
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000936Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
937 SourceLocation atLoc) {
938 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
939 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
940 ConsumeToken(); // the "implementation" identifier
941
Chris Lattnerdf195262007-10-09 17:51:17 +0000942 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000943 Diag(Tok, diag::err_expected_ident); // missing class or category name.
944 return 0;
945 }
946 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000947 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000948 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
949
Chris Lattnerdf195262007-10-09 17:51:17 +0000950 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000951 // we have a category implementation.
952 SourceLocation lparenLoc = ConsumeParen();
953 SourceLocation categoryLoc, rparenLoc;
954 IdentifierInfo *categoryId = 0;
955
Chris Lattnerdf195262007-10-09 17:51:17 +0000956 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000957 categoryId = Tok.getIdentifierInfo();
958 categoryLoc = ConsumeToken();
959 } else {
960 Diag(Tok, diag::err_expected_ident); // missing category name.
961 return 0;
962 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000963 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000964 Diag(Tok, diag::err_expected_rparen);
965 SkipUntil(tok::r_paren, false); // don't stop at ';'
966 return 0;
967 }
968 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000969 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000970 atLoc, nameId, nameLoc, categoryId,
971 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000972 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000973 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000974 }
975 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000976 SourceLocation superClassLoc;
977 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000978 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000979 // We have a super class
980 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000981 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000982 Diag(Tok, diag::err_expected_ident); // missing super class name.
983 return 0;
984 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000985 superClassId = Tok.getIdentifierInfo();
986 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000987 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000988 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +0000989 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000990 superClassId, superClassLoc);
991
Steve Naroff60fccee2007-10-29 21:38:07 +0000992 if (Tok.is(tok::l_brace)) // we have ivars
993 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000995
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000996 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000997}
Steve Naroff60fccee2007-10-29 21:38:07 +0000998
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000999Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1000 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1001 "ParseObjCAtEndDeclaration(): Expected @end");
1002 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001003 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001005 else
1006 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001007 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +00001008}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001009
1010/// compatibility-alias-decl:
1011/// @compatibility_alias alias-name class-name ';'
1012///
1013Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1014 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1015 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1016 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001017 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001018 Diag(Tok, diag::err_expected_ident);
1019 return 0;
1020 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001021 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1022 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001023 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001024 Diag(Tok, diag::err_expected_ident);
1025 return 0;
1026 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001027 IdentifierInfo *classId = Tok.getIdentifierInfo();
1028 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1029 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001030 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001031 return 0;
1032 }
1033 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1034 aliasId, aliasLoc,
1035 classId, classLoc);
1036 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001037}
1038
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001039/// property-synthesis:
1040/// @synthesize property-ivar-list ';'
1041///
1042/// property-ivar-list:
1043/// property-ivar
1044/// property-ivar-list ',' property-ivar
1045///
1046/// property-ivar:
1047/// identifier
1048/// identifier '=' identifier
1049///
1050Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1051 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1052 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1053 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001054 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001055 Diag(Tok, diag::err_expected_ident);
1056 return 0;
1057 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001058 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001059 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001060 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001061 // property '=' ivar-name
1062 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001063 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001064 Diag(Tok, diag::err_expected_ident);
1065 break;
1066 }
1067 ConsumeToken(); // consume ivar-name
1068 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001069 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001070 break;
1071 ConsumeToken(); // consume ','
1072 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001073 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001074 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1075 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001076}
1077
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001078/// property-dynamic:
1079/// @dynamic property-list
1080///
1081/// property-list:
1082/// identifier
1083/// property-list ',' identifier
1084///
1085Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1086 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1087 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1088 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001089 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001090 Diag(Tok, diag::err_expected_ident);
1091 return 0;
1092 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001093 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001094 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001095 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001096 break;
1097 ConsumeToken(); // consume ','
1098 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001099 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001100 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1101 return 0;
1102}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001103
1104/// objc-throw-statement:
1105/// throw expression[opt];
1106///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001107Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1108 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001109 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001110 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001111 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001112 if (Res.isInvalid) {
1113 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001114 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001115 }
1116 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001117 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001118 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001119}
1120
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001121/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001122/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001123///
1124Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001125 ConsumeToken(); // consume synchronized
1126 if (Tok.isNot(tok::l_paren)) {
1127 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1128 return true;
1129 }
1130 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001131 ExprResult Res = ParseExpression();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001132 if (Res.isInvalid) {
1133 SkipUntil(tok::semi);
1134 return true;
1135 }
1136 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001137 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001138 return true;
1139 }
1140 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001141 if (Tok.isNot(tok::l_brace)) {
1142 Diag (Tok, diag::err_expected_lbrace);
1143 return true;
1144 }
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001145 StmtResult SynchBody = ParseCompoundStatementBody();
1146 if (SynchBody.isInvalid)
1147 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1148 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001149}
1150
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001151/// objc-try-catch-statement:
1152/// @try compound-statement objc-catch-list[opt]
1153/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1154///
1155/// objc-catch-list:
1156/// @catch ( parameter-declaration ) compound-statement
1157/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1158/// catch-parameter-declaration:
1159/// parameter-declaration
1160/// '...' [OBJC2]
1161///
Chris Lattner6b884502008-03-10 06:06:04 +00001162Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001163 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001164
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001165 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001166 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001167 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001168 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001169 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001170 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001171 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001172 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001173 if (TryBody.isInvalid)
1174 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner6b884502008-03-10 06:06:04 +00001175
Chris Lattnerdf195262007-10-09 17:51:17 +00001176 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001177 // At this point, we need to lookahead to determine if this @ is the start
1178 // of an @catch or @finally. We don't want to consume the @ token if this
1179 // is an @try or @encode or something else.
1180 Token AfterAt = GetLookAheadToken(1);
1181 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1182 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1183 break;
1184
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001185 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001186 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001187 StmtTy *FirstPart = 0;
1188 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001189 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001190 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001191 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001192 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001193 DeclSpec DS;
1194 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001195 // FIXME: Is BlockContext right?
1196 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1197 ParseDeclarator(DeclaratorInfo);
Chris Lattner81c018d2008-03-13 06:29:04 +00001198 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1199 DeclaratorInfo, 0);
1200 StmtResult stmtResult =
1201 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1202 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001203 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroff64515f32008-02-05 21:27:35 +00001204 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001205 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001206 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001207
1208 StmtResult CatchBody(true);
1209 if (Tok.is(tok::l_brace))
1210 CatchBody = ParseCompoundStatementBody();
1211 else
1212 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001213 if (CatchBody.isInvalid)
1214 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001215 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001216 FirstPart, CatchBody.Val, CatchStmts.Val);
1217 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001218 } else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001219 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1220 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001221 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001222 }
1223 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001224 } else {
1225 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001226 ConsumeToken(); // consume finally
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001227
1228 StmtResult FinallyBody(true);
1229 if (Tok.is(tok::l_brace))
1230 FinallyBody = ParseCompoundStatementBody();
1231 else
1232 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001233 if (FinallyBody.isInvalid)
1234 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001235 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001236 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001237 catch_or_finally_seen = true;
1238 break;
1239 }
1240 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001241 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001242 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001243 return true;
1244 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001245 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001246 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001247}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001248
Steve Naroff3536b442007-09-06 21:24:23 +00001249/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001250///
Steve Naroff71c0a952007-11-13 23:01:27 +00001251Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001252 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001253 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001255 ConsumeToken();
1256
Steve Naroff409be832007-11-11 19:54:21 +00001257 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001258 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001259 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001260
1261 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1262 SkipUntil(tok::l_brace, true, true);
1263
1264 // If we didn't find the '{', bail out.
1265 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001266 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001267 }
Steve Naroff409be832007-11-11 19:54:21 +00001268 SourceLocation BraceLoc = Tok.getLocation();
1269
1270 // Enter a scope for the method body.
1271 EnterScope(Scope::FnScope|Scope::DeclScope);
1272
1273 // Tell the actions module that we have entered a method definition with the
1274 // specified Declarator for the method.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001275 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001276
1277 StmtResult FnBody = ParseCompoundStatementBody();
1278
1279 // If the function body could not be parsed, make a bogus compoundstmt.
1280 if (FnBody.isInvalid)
1281 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1282
1283 // Leave the function body scope.
1284 ExitScope();
1285
1286 // TODO: Pass argument information.
Steve Naroffd6d054d2007-11-11 23:20:51 +00001287 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001288 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001289}
Anders Carlsson55085182007-08-21 17:43:55 +00001290
Steve Naroff64515f32008-02-05 21:27:35 +00001291Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1292 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001293 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001294 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1295 return ParseObjCThrowStmt(AtLoc);
1296 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1297 return ParseObjCSynchronizedStmt(AtLoc);
1298 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1299 if (Res.isInvalid) {
1300 // If the expression is invalid, skip ahead to the next semicolon. Not
1301 // doing this opens us up to the possibility of infinite loops if
1302 // ParseExpression does not consume any tokens.
1303 SkipUntil(tok::semi);
1304 return true;
1305 }
1306 // Otherwise, eat the semicolon.
1307 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1308 return Actions.ActOnExprStmt(Res.Val);
1309}
1310
Steve Naroffa642beb2007-10-15 20:55:58 +00001311Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001312
1313 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001314 case tok::string_literal: // primary-expression: string-literal
1315 case tok::wide_string_literal:
1316 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1317 default:
1318 break;
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001319 }
1320
1321 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001322 case tok::objc_encode:
1323 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1324 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001325 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001326 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001327 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001328 default:
1329 Diag(AtLoc, diag::err_unexpected_at);
1330 SkipUntil(tok::semi);
Chris Lattner9d493952008-01-30 21:20:25 +00001331 return true;
Anders Carlsson55085182007-08-21 17:43:55 +00001332 }
Anders Carlsson55085182007-08-21 17:43:55 +00001333}
1334
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001335/// objc-message-expr:
1336/// '[' objc-receiver objc-message-args ']'
1337///
1338/// objc-receiver:
1339/// expression
1340/// class-name
1341/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001342Parser::ExprResult Parser::ParseObjCMessageExpression() {
1343 assert(Tok.is(tok::l_square) && "'[' expected");
1344 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1345
1346 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001347 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001348 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1349 ConsumeToken();
1350 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1351 }
1352
1353 ExprResult Res = ParseAssignmentExpression();
1354 if (Res.isInvalid) {
1355 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattner5c749422008-01-25 19:43:26 +00001356 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001357 return Res;
1358 }
1359 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1360}
1361
1362/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1363/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001364///
1365/// objc-message-args:
1366/// objc-selector
1367/// objc-keywordarg-list
1368///
1369/// objc-keywordarg-list:
1370/// objc-keywordarg
1371/// objc-keywordarg-list objc-keywordarg
1372///
1373/// objc-keywordarg:
1374/// selector-name[opt] ':' objc-keywordexpr
1375///
1376/// objc-keywordexpr:
1377/// nonempty-expr-list
1378///
1379/// nonempty-expr-list:
1380/// assignment-expression
1381/// nonempty-expr-list , assignment-expression
1382///
Chris Lattner699b6612008-01-25 18:59:06 +00001383Parser::ExprResult
1384Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1385 IdentifierInfo *ReceiverName,
1386 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001387 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001388 SourceLocation Loc;
1389 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001390
1391 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1392 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1393
Chris Lattnerdf195262007-10-09 17:51:17 +00001394 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001395 while (1) {
1396 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001397 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001398
Chris Lattnerdf195262007-10-09 17:51:17 +00001399 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001400 Diag(Tok, diag::err_expected_colon);
1401 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001402 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001403 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001404 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001405 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001406 ExprResult Res = ParseAssignmentExpression();
1407 if (Res.isInvalid) {
1408 SkipUntil(tok::identifier);
1409 return Res;
1410 }
1411 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001412 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001413
1414 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001415 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001416 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001417 break;
1418 // We have a selector or a colon, continue parsing.
1419 }
1420 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001421 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001422 ConsumeToken(); // Eat the ','.
1423 /// Parse the expression after ','
1424 ExprResult Res = ParseAssignmentExpression();
1425 if (Res.isInvalid) {
1426 SkipUntil(tok::identifier);
1427 return Res;
1428 }
1429 // We have a valid expression.
1430 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001431 }
1432 } else if (!selIdent) {
1433 Diag(Tok, diag::err_expected_ident); // missing selector name.
1434 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001435 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001436 }
Chris Lattnerff384912007-10-07 02:00:24 +00001437
Chris Lattnerdf195262007-10-09 17:51:17 +00001438 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001439 Diag(Tok, diag::err_expected_rsquare);
1440 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001441 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001442 }
Chris Lattner699b6612008-01-25 18:59:06 +00001443 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001444
Steve Naroff29238a02007-10-05 18:42:47 +00001445 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001446 if (nKeys == 0)
1447 KeyIdents.push_back(selIdent);
1448 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1449
1450 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001451 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001452 return Actions.ActOnClassMessage(CurScope,
Chris Lattner699b6612008-01-25 18:59:06 +00001453 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001454 &KeyExprs[0], KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001455 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001456 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001457}
1458
Steve Naroffbeaf2992007-11-03 11:27:19 +00001459Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001460 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001461 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001462
1463 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1464 // expressions. At this point, we know that the only valid thing that starts
1465 // with '@' is an @"".
1466 llvm::SmallVector<SourceLocation, 4> AtLocs;
1467 llvm::SmallVector<ExprTy*, 4> AtStrings;
1468 AtLocs.push_back(AtLoc);
1469 AtStrings.push_back(Res.Val);
1470
1471 while (Tok.is(tok::at)) {
1472 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001473
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001474 ExprResult Res(true); // Invalid unless there is a string literal.
1475 if (isTokenStringLiteral())
1476 Res = ParseStringLiteralExpression();
1477 else
1478 Diag(Tok, diag::err_objc_concat_string);
1479
1480 if (Res.isInvalid) {
1481 while (!AtStrings.empty()) {
1482 Actions.DeleteExpr(AtStrings.back());
1483 AtStrings.pop_back();
1484 }
1485 return Res;
1486 }
1487
1488 AtStrings.push_back(Res.Val);
1489 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001490
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001491 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1492 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001493}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001494
1495/// objc-encode-expression:
1496/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001497Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001498 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001499
1500 SourceLocation EncLoc = ConsumeToken();
1501
Chris Lattnerdf195262007-10-09 17:51:17 +00001502 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001503 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1504 return true;
1505 }
1506
1507 SourceLocation LParenLoc = ConsumeParen();
1508
1509 TypeTy *Ty = ParseTypeName();
1510
Anders Carlsson4988ae32007-08-23 15:31:37 +00001511 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001512
Chris Lattner674af952007-10-16 22:51:17 +00001513 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001514 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001515}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001516
1517/// objc-protocol-expression
1518/// @protocol ( protocol-name )
1519
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001520Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001521{
1522 SourceLocation ProtoLoc = ConsumeToken();
1523
Chris Lattnerdf195262007-10-09 17:51:17 +00001524 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001525 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1526 return true;
1527 }
1528
1529 SourceLocation LParenLoc = ConsumeParen();
1530
Chris Lattnerdf195262007-10-09 17:51:17 +00001531 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001532 Diag(Tok, diag::err_expected_ident);
1533 return true;
1534 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001535 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001536 ConsumeToken();
1537
Anders Carlsson4988ae32007-08-23 15:31:37 +00001538 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001539
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001540 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1541 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001542}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001543
1544/// objc-selector-expression
1545/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001546Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001547{
1548 SourceLocation SelectorLoc = ConsumeToken();
1549
1550 if (Tok.isNot(tok::l_paren)) {
1551 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1552 return 0;
1553 }
1554
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001555 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001556 SourceLocation LParenLoc = ConsumeParen();
1557 SourceLocation sLoc;
1558 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1559 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001560 Diag(Tok, diag::err_expected_ident); // missing selector name.
1561 return 0;
1562 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001563 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001564 unsigned nColons = 0;
1565 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001566 while (1) {
1567 if (Tok.isNot(tok::colon)) {
1568 Diag(Tok, diag::err_expected_colon);
1569 break;
1570 }
Chris Lattnercb53b362007-12-27 19:57:00 +00001571 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001572 ConsumeToken(); // Eat the ':'.
1573 if (Tok.is(tok::r_paren))
1574 break;
1575 // Check for another keyword selector.
1576 SourceLocation Loc;
1577 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001578 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001579 if (!SelIdent && Tok.isNot(tok::colon))
1580 break;
1581 }
Steve Naroff887407e2007-12-05 22:21:29 +00001582 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001583 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001584 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001585 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001586 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001587 }