blob: eb3c5df6fbe0fd2a940ec100f60748d00d58ee40 [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 Jahanian1d78cc42008-04-10 23:32:45 +0000402 FD.D, FD.BitfieldSize);
Chris Lattnere1359422008-04-10 06:46:29 +0000403 PropertyDecls.push_back(Field);
404 }
405
Chris Lattnerdf195262007-10-09 17:51:17 +0000406 if (Tok.is(tok::semi))
Fariborz Jahaniand0f97d12007-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 Lattnerf4af5152008-03-17 01:19:02 +0000412 return Actions.ActOnAddObjCProperties(AtLoc, &PropertyDecls[0],
Chris Lattnere1359422008-04-10 06:46:29 +0000413 PropertyDecls.size(), OCDS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414}
Steve Naroffdac269b2007-08-20 21:31:48 +0000415
Steve Naroff3536b442007-09-06 21:24:23 +0000416/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000417/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000418/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000419///
420/// objc-instance-method: '-'
421/// objc-class-method: '+'
422///
Steve Naroff4985ace2007-08-22 18:35:33 +0000423/// objc-method-attributes: [OBJC2]
424/// __attribute__((deprecated))
425///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000426Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000427 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000428 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000429
430 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000431 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000432
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000433 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000434 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000435 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000436 return MDecl;
Steve Naroff294494e2007-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 Jahanian4b6c9052007-10-11 00:55:41 +0000447IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-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 Lattner9298d962007-11-15 05:25:19 +0000488 case tok::kw_bool:
Chris Lattnerff384912007-10-07 02:00:24 +0000489 case tok::kw__Bool:
490 case tok::kw__Complex:
491 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000492 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000493 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000494 }
Steve Naroff294494e2007-08-22 16:35:03 +0000495}
496
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000497/// property-attrlist: one of
498/// readonly getter setter assign retain copy nonatomic
499///
500bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000501 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000502 const IdentifierInfo *II = Tok.getIdentifierInfo();
503 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000504 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000505 }
506 return false;
507}
508
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000509/// objc-for-collection-in: 'in'
510///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000511bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-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 Jahanian335a2d42008-01-04 23:04:08 +0000515 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
516 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000517}
518
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000519/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000520/// qualifier list and builds their bitmask representation in the input
521/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000522///
523/// objc-type-qualifiers:
524/// objc-type-qualifier
525/// objc-type-qualifiers objc-type-qualifier
526///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000528 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000529 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000530 return;
531
532 const IdentifierInfo *II = Tok.getIdentifierInfo();
533 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000534 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000535 continue;
536
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000537 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000538 switch (i) {
539 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-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 Lattnere8b724d2007-12-12 06:56:32 +0000546 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000547 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000562Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000563 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000564
565 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000566 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000567
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000568 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000569 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000570
Steve Naroff294494e2007-08-22 16:35:03 +0000571 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-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 Naroff294494e2007-08-22 16:35:03 +0000575 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000576 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000577 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000578 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000579 }
580 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000581 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000582}
583
584/// objc-method-decl:
585/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000586/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000587/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000588/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000589///
590/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000591/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000592/// objc-keyword-selector objc-keyword-decl
593///
594/// objc-keyword-decl:
Steve Naroff7ef58fd2007-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 Naroff294494e2007-08-22 16:35:03 +0000599///
Steve Naroff4985ace2007-08-22 18:35:33 +0000600/// objc-parmlist:
601/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000602///
Steve Naroff4985ace2007-08-22 18:35:33 +0000603/// objc-parms:
604/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000605///
Steve Naroff4985ace2007-08-22 18:35:33 +0000606/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000607/// , ...
608///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000609/// objc-keyword-attributes: [OBJC2]
610/// __attribute__((unused))
611///
Steve Naroffbef11852007-10-26 20:53:56 +0000612Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000613 tok::TokenKind mType,
614 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000615 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000616{
Steve Naroff4985ace2007-08-22 18:35:33 +0000617 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000618 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000620 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000621 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroffbef11852007-10-26 20:53:56 +0000622 SourceLocation selLoc;
623 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000624 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-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 Lattnerdf195262007-10-09 17:51:17 +0000633 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000634 MethodAttrs = ParseAttributes();
635
636 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000637 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000638 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000639 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000640 }
Steve Narofff28b2642007-09-05 23:30:30 +0000641
Steve Naroff68d331a2007-09-27 14:38:14 +0000642 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
643 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000644 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000645 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000646
647 Action::TypeTy *TypeInfo;
648 while (1) {
649 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000650
Chris Lattnerff384912007-10-07 02:00:24 +0000651 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000652 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000653 Diag(Tok, diag::err_expected_colon);
654 break;
655 }
656 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000657 ObjCDeclSpec DSType;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000658 if (Tok.is(tok::l_paren)) { // Parse the argument type.
659 TypeInfo = ParseObjCTypeName(DSType);
660 }
Chris Lattnerff384912007-10-07 02:00:24 +0000661 else
662 TypeInfo = 0;
663 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000664 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000665
Chris Lattnerff384912007-10-07 02:00:24 +0000666 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000667 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000668 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000669
Chris Lattnerdf195262007-10-09 17:51:17 +0000670 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000671 Diag(Tok, diag::err_expected_ident); // missing argument name.
672 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000673 }
Chris Lattnerff384912007-10-07 02:00:24 +0000674 ArgNames.push_back(Tok.getIdentifierInfo());
675 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000676
Chris Lattnerff384912007-10-07 02:00:24 +0000677 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000678 SourceLocation Loc;
679 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000680 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000681 break;
682 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000683 }
Chris Lattnerff384912007-10-07 02:00:24 +0000684
Steve Naroff335eafa2007-11-15 12:35:21 +0000685 bool isVariadic = false;
686
Chris Lattnerff384912007-10-07 02:00:24 +0000687 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000688 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000689 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000690 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000691 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000692 ConsumeToken();
693 break;
694 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000695 // FIXME: implement this...
Chris Lattnerff384912007-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 Jahaniane3a2ca72007-09-10 20:33:04 +0000705 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000706 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000707 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000708 MethodAttrs = ParseAttributes();
709
710 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
711 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000712 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000713 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000714 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000715 &ArgNames[0], MethodAttrs,
716 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000717}
718
Steve Naroffdac269b2007-08-20 21:31:48 +0000719/// objc-protocol-refs:
720/// '<' identifier-list '>'
721///
Steve Narofff28b2642007-09-05 23:30:30 +0000722bool Parser::ParseObjCProtocolReferences(
Chris Lattner88cb27a2008-04-07 04:56:42 +0000723 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnerdf195262007-10-09 17:51:17 +0000724 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000725
726 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000727
728 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000729 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-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 Lattnerdf195262007-10-09 17:51:17 +0000737 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000738 break;
739 ConsumeToken();
740 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000741
Steve Naroffdac269b2007-08-20 21:31:48 +0000742 // Consume the '>'.
Steve Narofff908a872007-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 Naroffdac269b2007-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 Naroffddbff782007-08-21 21:17:12 +0000766/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000767///
768/// objc-instance-variable-decl:
769/// struct-declaration
770///
Steve Naroff60fccee2007-10-29 21:38:07 +0000771void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
772 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000773 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000774 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000775 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
776
Steve Naroffddbff782007-08-21 21:17:12 +0000777 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000778
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000779 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffddbff782007-08-21 21:17:12 +0000780 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000781 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000782 // Each iteration of this loop reads one objc-instance-variable-decl.
783
784 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000785 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000786 Diag(Tok, diag::ext_extra_struct_semi);
787 ConsumeToken();
788 continue;
789 }
Chris Lattnere1359422008-04-10 06:46:29 +0000790
Steve Naroffddbff782007-08-21 21:17:12 +0000791 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000792 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000793 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000794 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000795 case tok::objc_private:
796 case tok::objc_public:
797 case tok::objc_protected:
798 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000799 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000800 ConsumeToken();
801 continue;
802 default:
803 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000804 continue;
805 }
806 }
Chris Lattnere1359422008-04-10 06:46:29 +0000807
808 // Parse all the comma separated declarators.
809 DeclSpec DS;
810 FieldDeclarators.clear();
811 ParseStructDeclaration(DS, FieldDeclarators);
812
813 // Convert them all to fields.
814 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
815 FieldDeclarator &FD = FieldDeclarators[i];
816 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000817 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000818 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000819 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000820 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000821 }
Steve Naroff3536b442007-09-06 21:24:23 +0000822
Chris Lattnerdf195262007-10-09 17:51:17 +0000823 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000824 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000825 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000826 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
827 break;
828 } else {
829 Diag(Tok, diag::err_expected_semi_decl_list);
830 // Skip to end of block or statement
831 SkipUntil(tok::r_brace, true, true);
832 }
833 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000834 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000835 // Call ActOnFields() even if we don't have any decls. This is useful
836 // for code rewriting tools that need to be aware of the empty list.
837 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
838 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000839 LBraceLoc, RBraceLoc);
Steve Naroffddbff782007-08-21 21:17:12 +0000840 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000841}
Steve Naroffdac269b2007-08-20 21:31:48 +0000842
843/// objc-protocol-declaration:
844/// objc-protocol-definition
845/// objc-protocol-forward-reference
846///
847/// objc-protocol-definition:
848/// @protocol identifier
849/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000850/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000851/// @end
852///
853/// objc-protocol-forward-reference:
854/// @protocol identifier-list ';'
855///
856/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000857/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000858/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000859Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000860 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000861 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
862 ConsumeToken(); // the "protocol" identifier
863
Chris Lattnerdf195262007-10-09 17:51:17 +0000864 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000865 Diag(Tok, diag::err_expected_ident); // missing protocol name.
866 return 0;
867 }
868 // Save the protocol name, then consume it.
869 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
870 SourceLocation nameLoc = ConsumeToken();
871
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000872 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000873 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000874 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000875 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000876 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000877 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000878 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000879 ProtocolRefs.push_back(protocolName);
880
881 while (1) {
882 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000883 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000884 Diag(Tok, diag::err_expected_ident);
885 SkipUntil(tok::semi);
886 return 0;
887 }
888 ProtocolRefs.push_back(Tok.getIdentifierInfo());
889 ConsumeToken(); // the identifier
890
Chris Lattnerdf195262007-10-09 17:51:17 +0000891 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000892 break;
893 }
894 // Consume the ';'.
895 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
896 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000897 }
Chris Lattnerc8581052008-03-16 20:19:15 +0000898 if (!ProtocolRefs.empty())
Steve Naroffe440eb82007-10-10 17:32:04 +0000899 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000900 &ProtocolRefs[0],
901 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000902 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000903 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000904 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000905 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000906 return 0;
907 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000908
Steve Naroffe440eb82007-10-10 17:32:04 +0000909 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000910 protocolName, nameLoc,
911 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000912 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000913 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000914
915 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000916 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000917 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000918 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000919 }
920 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000921 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000922}
Steve Naroffdac269b2007-08-20 21:31:48 +0000923
924/// objc-implementation:
925/// objc-class-implementation-prologue
926/// objc-category-implementation-prologue
927///
928/// objc-class-implementation-prologue:
929/// @implementation identifier objc-superclass[opt]
930/// objc-class-instance-variables[opt]
931///
932/// objc-category-implementation-prologue:
933/// @implementation identifier ( identifier )
934
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000935Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
936 SourceLocation atLoc) {
937 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
938 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
939 ConsumeToken(); // the "implementation" identifier
940
Chris Lattnerdf195262007-10-09 17:51:17 +0000941 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000942 Diag(Tok, diag::err_expected_ident); // missing class or category name.
943 return 0;
944 }
945 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000946 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000947 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
948
Chris Lattnerdf195262007-10-09 17:51:17 +0000949 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000950 // we have a category implementation.
951 SourceLocation lparenLoc = ConsumeParen();
952 SourceLocation categoryLoc, rparenLoc;
953 IdentifierInfo *categoryId = 0;
954
Chris Lattnerdf195262007-10-09 17:51:17 +0000955 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000956 categoryId = Tok.getIdentifierInfo();
957 categoryLoc = ConsumeToken();
958 } else {
959 Diag(Tok, diag::err_expected_ident); // missing category name.
960 return 0;
961 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000963 Diag(Tok, diag::err_expected_rparen);
964 SkipUntil(tok::r_paren, false); // don't stop at ';'
965 return 0;
966 }
967 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000968 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000969 atLoc, nameId, nameLoc, categoryId,
970 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000972 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000973 }
974 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000975 SourceLocation superClassLoc;
976 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000977 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000978 // We have a super class
979 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000980 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000981 Diag(Tok, diag::err_expected_ident); // missing super class name.
982 return 0;
983 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000984 superClassId = Tok.getIdentifierInfo();
985 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000986 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000987 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +0000988 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000989 superClassId, superClassLoc);
990
Steve Naroff60fccee2007-10-29 21:38:07 +0000991 if (Tok.is(tok::l_brace)) // we have ivars
992 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000993 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000994
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000995 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000996}
Steve Naroff60fccee2007-10-29 21:38:07 +0000997
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000998Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
999 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1000 "ParseObjCAtEndDeclaration(): Expected @end");
1001 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001002 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001003 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001004 else
1005 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +00001007}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001008
1009/// compatibility-alias-decl:
1010/// @compatibility_alias alias-name class-name ';'
1011///
1012Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1013 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1014 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1015 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001016 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001017 Diag(Tok, diag::err_expected_ident);
1018 return 0;
1019 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001020 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1021 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001022 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001023 Diag(Tok, diag::err_expected_ident);
1024 return 0;
1025 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001026 IdentifierInfo *classId = Tok.getIdentifierInfo();
1027 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1028 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001029 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001030 return 0;
1031 }
1032 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1033 aliasId, aliasLoc,
1034 classId, classLoc);
1035 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001036}
1037
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001038/// property-synthesis:
1039/// @synthesize property-ivar-list ';'
1040///
1041/// property-ivar-list:
1042/// property-ivar
1043/// property-ivar-list ',' property-ivar
1044///
1045/// property-ivar:
1046/// identifier
1047/// identifier '=' identifier
1048///
1049Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1050 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1051 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1052 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001053 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001054 Diag(Tok, diag::err_expected_ident);
1055 return 0;
1056 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001058 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001059 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001060 // property '=' ivar-name
1061 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001062 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001063 Diag(Tok, diag::err_expected_ident);
1064 break;
1065 }
1066 ConsumeToken(); // consume ivar-name
1067 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001068 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001069 break;
1070 ConsumeToken(); // consume ','
1071 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001072 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001073 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1074 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001075}
1076
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001077/// property-dynamic:
1078/// @dynamic property-list
1079///
1080/// property-list:
1081/// identifier
1082/// property-list ',' identifier
1083///
1084Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1085 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1086 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1087 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001088 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001089 Diag(Tok, diag::err_expected_ident);
1090 return 0;
1091 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001092 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001093 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001094 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001095 break;
1096 ConsumeToken(); // consume ','
1097 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001098 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001099 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1100 return 0;
1101}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001102
1103/// objc-throw-statement:
1104/// throw expression[opt];
1105///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001106Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1107 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001108 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001109 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001110 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001111 if (Res.isInvalid) {
1112 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001113 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001114 }
1115 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001116 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001118}
1119
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001120/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001121/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001122///
1123Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001124 ConsumeToken(); // consume synchronized
1125 if (Tok.isNot(tok::l_paren)) {
1126 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1127 return true;
1128 }
1129 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001130 ExprResult Res = ParseExpression();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001131 if (Res.isInvalid) {
1132 SkipUntil(tok::semi);
1133 return true;
1134 }
1135 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001136 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001137 return true;
1138 }
1139 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001140 if (Tok.isNot(tok::l_brace)) {
1141 Diag (Tok, diag::err_expected_lbrace);
1142 return true;
1143 }
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001144 StmtResult SynchBody = ParseCompoundStatementBody();
1145 if (SynchBody.isInvalid)
1146 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1147 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001148}
1149
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001150/// objc-try-catch-statement:
1151/// @try compound-statement objc-catch-list[opt]
1152/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1153///
1154/// objc-catch-list:
1155/// @catch ( parameter-declaration ) compound-statement
1156/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1157/// catch-parameter-declaration:
1158/// parameter-declaration
1159/// '...' [OBJC2]
1160///
Chris Lattner6b884502008-03-10 06:06:04 +00001161Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001162 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001163
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001164 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001165 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001166 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001167 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001168 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001169 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001170 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001171 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001172 if (TryBody.isInvalid)
1173 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner6b884502008-03-10 06:06:04 +00001174
Chris Lattnerdf195262007-10-09 17:51:17 +00001175 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001176 // At this point, we need to lookahead to determine if this @ is the start
1177 // of an @catch or @finally. We don't want to consume the @ token if this
1178 // is an @try or @encode or something else.
1179 Token AfterAt = GetLookAheadToken(1);
1180 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1181 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1182 break;
1183
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001184 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001185 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001186 StmtTy *FirstPart = 0;
1187 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001188 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001189 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001190 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001191 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001192 DeclSpec DS;
1193 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001194 // FIXME: Is BlockContext right?
1195 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1196 ParseDeclarator(DeclaratorInfo);
Chris Lattner81c018d2008-03-13 06:29:04 +00001197 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1198 DeclaratorInfo, 0);
1199 StmtResult stmtResult =
1200 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1201 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001202 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroff64515f32008-02-05 21:27:35 +00001203 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001204 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001205 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001206
1207 StmtResult CatchBody(true);
1208 if (Tok.is(tok::l_brace))
1209 CatchBody = ParseCompoundStatementBody();
1210 else
1211 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001212 if (CatchBody.isInvalid)
1213 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001214 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001215 FirstPart, CatchBody.Val, CatchStmts.Val);
1216 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001217 } else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001218 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1219 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001220 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001221 }
1222 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001223 } else {
1224 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001225 ConsumeToken(); // consume finally
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001226
1227 StmtResult FinallyBody(true);
1228 if (Tok.is(tok::l_brace))
1229 FinallyBody = ParseCompoundStatementBody();
1230 else
1231 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001232 if (FinallyBody.isInvalid)
1233 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001234 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001235 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001236 catch_or_finally_seen = true;
1237 break;
1238 }
1239 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001240 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001241 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001242 return true;
1243 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001244 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001245 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001246}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001247
Steve Naroff3536b442007-09-06 21:24:23 +00001248/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001249///
Steve Naroff71c0a952007-11-13 23:01:27 +00001250Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001251 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001252 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001253 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001254 ConsumeToken();
1255
Steve Naroff409be832007-11-11 19:54:21 +00001256 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001257 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001258 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001259
1260 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1261 SkipUntil(tok::l_brace, true, true);
1262
1263 // If we didn't find the '{', bail out.
1264 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001265 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001266 }
Steve Naroff409be832007-11-11 19:54:21 +00001267 SourceLocation BraceLoc = Tok.getLocation();
1268
1269 // Enter a scope for the method body.
1270 EnterScope(Scope::FnScope|Scope::DeclScope);
1271
1272 // Tell the actions module that we have entered a method definition with the
1273 // specified Declarator for the method.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001274 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001275
1276 StmtResult FnBody = ParseCompoundStatementBody();
1277
1278 // If the function body could not be parsed, make a bogus compoundstmt.
1279 if (FnBody.isInvalid)
1280 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1281
1282 // Leave the function body scope.
1283 ExitScope();
1284
1285 // TODO: Pass argument information.
Steve Naroffd6d054d2007-11-11 23:20:51 +00001286 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001287 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001288}
Anders Carlsson55085182007-08-21 17:43:55 +00001289
Steve Naroff64515f32008-02-05 21:27:35 +00001290Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1291 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001292 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001293 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1294 return ParseObjCThrowStmt(AtLoc);
1295 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1296 return ParseObjCSynchronizedStmt(AtLoc);
1297 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1298 if (Res.isInvalid) {
1299 // If the expression is invalid, skip ahead to the next semicolon. Not
1300 // doing this opens us up to the possibility of infinite loops if
1301 // ParseExpression does not consume any tokens.
1302 SkipUntil(tok::semi);
1303 return true;
1304 }
1305 // Otherwise, eat the semicolon.
1306 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1307 return Actions.ActOnExprStmt(Res.Val);
1308}
1309
Steve Naroffa642beb2007-10-15 20:55:58 +00001310Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001311
1312 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001313 case tok::string_literal: // primary-expression: string-literal
1314 case tok::wide_string_literal:
1315 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1316 default:
1317 break;
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001318 }
1319
1320 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001321 case tok::objc_encode:
1322 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1323 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001324 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001325 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001326 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001327 default:
1328 Diag(AtLoc, diag::err_unexpected_at);
1329 SkipUntil(tok::semi);
Chris Lattner9d493952008-01-30 21:20:25 +00001330 return true;
Anders Carlsson55085182007-08-21 17:43:55 +00001331 }
Anders Carlsson55085182007-08-21 17:43:55 +00001332}
1333
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001334/// objc-message-expr:
1335/// '[' objc-receiver objc-message-args ']'
1336///
1337/// objc-receiver:
1338/// expression
1339/// class-name
1340/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001341Parser::ExprResult Parser::ParseObjCMessageExpression() {
1342 assert(Tok.is(tok::l_square) && "'[' expected");
1343 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1344
1345 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001346 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001347 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1348 ConsumeToken();
1349 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1350 }
1351
1352 ExprResult Res = ParseAssignmentExpression();
1353 if (Res.isInvalid) {
1354 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattner5c749422008-01-25 19:43:26 +00001355 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001356 return Res;
1357 }
1358 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1359}
1360
1361/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1362/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001363///
1364/// objc-message-args:
1365/// objc-selector
1366/// objc-keywordarg-list
1367///
1368/// objc-keywordarg-list:
1369/// objc-keywordarg
1370/// objc-keywordarg-list objc-keywordarg
1371///
1372/// objc-keywordarg:
1373/// selector-name[opt] ':' objc-keywordexpr
1374///
1375/// objc-keywordexpr:
1376/// nonempty-expr-list
1377///
1378/// nonempty-expr-list:
1379/// assignment-expression
1380/// nonempty-expr-list , assignment-expression
1381///
Chris Lattner699b6612008-01-25 18:59:06 +00001382Parser::ExprResult
1383Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1384 IdentifierInfo *ReceiverName,
1385 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001386 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001387 SourceLocation Loc;
1388 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001389
1390 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1391 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1392
Chris Lattnerdf195262007-10-09 17:51:17 +00001393 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001394 while (1) {
1395 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001396 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001397
Chris Lattnerdf195262007-10-09 17:51:17 +00001398 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001399 Diag(Tok, diag::err_expected_colon);
1400 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001401 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001402 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001403 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001404 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001405 ExprResult Res = ParseAssignmentExpression();
1406 if (Res.isInvalid) {
1407 SkipUntil(tok::identifier);
1408 return Res;
1409 }
1410 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001411 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001412
1413 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001414 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001415 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001416 break;
1417 // We have a selector or a colon, continue parsing.
1418 }
1419 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001420 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001421 ConsumeToken(); // Eat the ','.
1422 /// Parse the expression after ','
1423 ExprResult Res = ParseAssignmentExpression();
1424 if (Res.isInvalid) {
1425 SkipUntil(tok::identifier);
1426 return Res;
1427 }
1428 // We have a valid expression.
1429 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001430 }
1431 } else if (!selIdent) {
1432 Diag(Tok, diag::err_expected_ident); // missing selector name.
1433 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001434 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001435 }
Chris Lattnerff384912007-10-07 02:00:24 +00001436
Chris Lattnerdf195262007-10-09 17:51:17 +00001437 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001438 Diag(Tok, diag::err_expected_rsquare);
1439 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001440 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001441 }
Chris Lattner699b6612008-01-25 18:59:06 +00001442 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001443
Steve Naroff29238a02007-10-05 18:42:47 +00001444 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001445 if (nKeys == 0)
1446 KeyIdents.push_back(selIdent);
1447 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1448
1449 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001450 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001451 return Actions.ActOnClassMessage(CurScope,
Chris Lattner699b6612008-01-25 18:59:06 +00001452 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001453 &KeyExprs[0], KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001454 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001455 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001456}
1457
Steve Naroffbeaf2992007-11-03 11:27:19 +00001458Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001459 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001460 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001461
1462 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1463 // expressions. At this point, we know that the only valid thing that starts
1464 // with '@' is an @"".
1465 llvm::SmallVector<SourceLocation, 4> AtLocs;
1466 llvm::SmallVector<ExprTy*, 4> AtStrings;
1467 AtLocs.push_back(AtLoc);
1468 AtStrings.push_back(Res.Val);
1469
1470 while (Tok.is(tok::at)) {
1471 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001472
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001473 ExprResult Res(true); // Invalid unless there is a string literal.
1474 if (isTokenStringLiteral())
1475 Res = ParseStringLiteralExpression();
1476 else
1477 Diag(Tok, diag::err_objc_concat_string);
1478
1479 if (Res.isInvalid) {
1480 while (!AtStrings.empty()) {
1481 Actions.DeleteExpr(AtStrings.back());
1482 AtStrings.pop_back();
1483 }
1484 return Res;
1485 }
1486
1487 AtStrings.push_back(Res.Val);
1488 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001489
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001490 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1491 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001492}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001493
1494/// objc-encode-expression:
1495/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001496Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001497 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001498
1499 SourceLocation EncLoc = ConsumeToken();
1500
Chris Lattnerdf195262007-10-09 17:51:17 +00001501 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001502 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1503 return true;
1504 }
1505
1506 SourceLocation LParenLoc = ConsumeParen();
1507
1508 TypeTy *Ty = ParseTypeName();
1509
Anders Carlsson4988ae32007-08-23 15:31:37 +00001510 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001511
Chris Lattner674af952007-10-16 22:51:17 +00001512 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001513 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001514}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001515
1516/// objc-protocol-expression
1517/// @protocol ( protocol-name )
1518
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001519Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001520{
1521 SourceLocation ProtoLoc = ConsumeToken();
1522
Chris Lattnerdf195262007-10-09 17:51:17 +00001523 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001524 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1525 return true;
1526 }
1527
1528 SourceLocation LParenLoc = ConsumeParen();
1529
Chris Lattnerdf195262007-10-09 17:51:17 +00001530 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001531 Diag(Tok, diag::err_expected_ident);
1532 return true;
1533 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001534 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001535 ConsumeToken();
1536
Anders Carlsson4988ae32007-08-23 15:31:37 +00001537 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001538
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001539 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1540 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001541}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001542
1543/// objc-selector-expression
1544/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001545Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001546{
1547 SourceLocation SelectorLoc = ConsumeToken();
1548
1549 if (Tok.isNot(tok::l_paren)) {
1550 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1551 return 0;
1552 }
1553
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001554 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001555 SourceLocation LParenLoc = ConsumeParen();
1556 SourceLocation sLoc;
1557 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1558 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001559 Diag(Tok, diag::err_expected_ident); // missing selector name.
1560 return 0;
1561 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001562 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001563 unsigned nColons = 0;
1564 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001565 while (1) {
1566 if (Tok.isNot(tok::colon)) {
1567 Diag(Tok, diag::err_expected_colon);
1568 break;
1569 }
Chris Lattnercb53b362007-12-27 19:57:00 +00001570 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001571 ConsumeToken(); // Eat the ':'.
1572 if (Tok.is(tok::r_paren))
1573 break;
1574 // Check for another keyword selector.
1575 SourceLocation Loc;
1576 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001577 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001578 if (!SelIdent && Tok.isNot(tok::colon))
1579 break;
1580 }
Steve Naroff887407e2007-12-05 22:21:29 +00001581 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001582 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001583 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001584 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001585 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001586 }