blob: 4795a464f5490c588afae7d58b51626de46bcbf4 [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 Jahanian1de1e742008-04-14 23:36:35 +0000251 ObjCDeclSpec OCDS;
252 ConsumeToken(); // the "property" identifier
253 // Parse property attribute list, if any.
254 if (Tok.is(tok::l_paren)) {
255 // property has attribute list.
256 ParseObjCPropertyAttribute(OCDS);
257 }
258 // Parse all the comma separated declarators.
259 DeclSpec DS;
260 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
261 ParseStructDeclaration(DS, FieldDeclarators);
262
263 if (Tok.is(tok::semi))
264 ConsumeToken();
265 else {
266 Diag(Tok, diag::err_expected_semi_decl_list);
267 SkipUntil(tok::r_brace, true, true);
268 }
269 // Convert them all to property declarations.
270 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
271 FieldDeclarator &FD = FieldDeclarators[i];
272 // Install the property declarator into interfaceDecl.
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000273 Selector GetterSel =
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000274 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
275 ? OCDS.getGetterName()
276 : FD.D.getIdentifier());
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000277 Selector SetterSel =
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000278 PP.getSelectorTable().getNullarySelector(OCDS.getSetterName()
279 ? OCDS.getSetterName()
280 // FIXME. This is not right!
281 : FD.D.getIdentifier());
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000282 DeclTy *Property = Actions.ActOnProperty(CurScope,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000283 DS.getSourceRange().getBegin(), FD, OCDS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000284 GetterSel, SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000285 MethodImplKind);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000286 allProperties.push_back(Property);
287 }
Steve Naroff294494e2007-08-22 16:35:03 +0000288 continue;
289 } else {
290 Diag(Tok, diag::err_objc_illegal_interface_qual);
291 ConsumeToken();
292 }
293 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000294 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
295 DeclTy *methodPrototype =
296 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000297 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000298 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
299 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000300 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000301 continue;
302 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000303 else if (Tok.is(tok::at))
304 continue;
305
Chris Lattnerdf195262007-10-09 17:51:17 +0000306 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000307 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000308 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000309 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000310 else {
Steve Naroff4985ace2007-08-22 18:35:33 +0000311 // FIXME: as the name implies, this rule allows function definitions.
312 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000313 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000314 }
Steve Naroff294494e2007-08-22 16:35:03 +0000315 }
Steve Naroff2feac5e2007-10-30 03:43:13 +0000316 /// Insert collected methods declarations into the @interface object.
Steve Naroff0416fb92007-11-11 17:19:15 +0000317 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
318 &allProperties[0], allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000319}
320
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000321/// Parse property attribute declarations.
322///
323/// property-attr-decl: '(' property-attrlist ')'
324/// property-attrlist:
325/// property-attribute
326/// property-attrlist ',' property-attribute
327/// property-attribute:
328/// getter '=' identifier
329/// setter '=' identifier ':'
330/// readonly
331/// readwrite
332/// assign
333/// retain
334/// copy
335/// nonatomic
336///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000337void Parser::ParseObjCPropertyAttribute (ObjCDeclSpec &DS) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000338 SourceLocation loc = ConsumeParen(); // consume '('
339 while (isObjCPropertyAttribute()) {
340 const IdentifierInfo *II = Tok.getIdentifierInfo();
341 // getter/setter require extra treatment.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000342 if (II == ObjCPropertyAttrs[objc_getter] ||
343 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000344 // skip getter/setter part.
345 SourceLocation loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000346 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000347 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000348 if (Tok.is(tok::identifier)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000349 if (II == ObjCPropertyAttrs[objc_setter]) {
350 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000351 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000352 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000353 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000354 Diag(loc, diag::err_expected_colon);
355 SkipUntil(tok::r_paren,true,true);
356 break;
357 }
358 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000359 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000360 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000361 DS.setGetterName(Tok.getIdentifierInfo());
362 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000363 }
364 else {
365 Diag(loc, diag::err_expected_ident);
Chris Lattnercb53b362007-12-27 19:57:00 +0000366 SkipUntil(tok::r_paren,true,true);
367 break;
368 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000369 }
370 else {
371 Diag(loc, diag::err_objc_expected_equal);
372 SkipUntil(tok::r_paren,true,true);
373 break;
374 }
375 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000376
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000377 else if (II == ObjCPropertyAttrs[objc_readonly])
378 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
379 else if (II == ObjCPropertyAttrs[objc_assign])
380 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
381 else if (II == ObjCPropertyAttrs[objc_readwrite])
382 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
383 else if (II == ObjCPropertyAttrs[objc_retain])
384 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
385 else if (II == ObjCPropertyAttrs[objc_copy])
386 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
387 else if (II == ObjCPropertyAttrs[objc_nonatomic])
388 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000389
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000390 ConsumeToken(); // consume last attribute token
Chris Lattnerdf195262007-10-09 17:51:17 +0000391 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000392 loc = ConsumeToken();
393 continue;
394 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000395 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000396 break;
397 Diag(loc, diag::err_expected_rparen);
398 SkipUntil(tok::semi);
399 return;
400 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000401 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000402 ConsumeParen();
403 else {
404 Diag(loc, diag::err_objc_expected_property_attr);
405 SkipUntil(tok::r_paren); // recover from error inside attribute list
406 }
407}
408
Steve Naroff3536b442007-09-06 21:24:23 +0000409/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000410/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000411/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000412///
413/// objc-instance-method: '-'
414/// objc-class-method: '+'
415///
Steve Naroff4985ace2007-08-22 18:35:33 +0000416/// objc-method-attributes: [OBJC2]
417/// __attribute__((deprecated))
418///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000419Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000420 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000421 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000422
423 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000424 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000425
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000426 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000427 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000428 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000429 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000430}
431
432/// objc-selector:
433/// identifier
434/// one of
435/// enum struct union if else while do for switch case default
436/// break continue return goto asm sizeof typeof __alignof
437/// unsigned long const short volatile signed restrict _Complex
438/// in out inout bycopy byref oneway int char float double void _Bool
439///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000440IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000441 switch (Tok.getKind()) {
442 default:
443 return 0;
444 case tok::identifier:
445 case tok::kw_typeof:
446 case tok::kw___alignof:
447 case tok::kw_auto:
448 case tok::kw_break:
449 case tok::kw_case:
450 case tok::kw_char:
451 case tok::kw_const:
452 case tok::kw_continue:
453 case tok::kw_default:
454 case tok::kw_do:
455 case tok::kw_double:
456 case tok::kw_else:
457 case tok::kw_enum:
458 case tok::kw_extern:
459 case tok::kw_float:
460 case tok::kw_for:
461 case tok::kw_goto:
462 case tok::kw_if:
463 case tok::kw_inline:
464 case tok::kw_int:
465 case tok::kw_long:
466 case tok::kw_register:
467 case tok::kw_restrict:
468 case tok::kw_return:
469 case tok::kw_short:
470 case tok::kw_signed:
471 case tok::kw_sizeof:
472 case tok::kw_static:
473 case tok::kw_struct:
474 case tok::kw_switch:
475 case tok::kw_typedef:
476 case tok::kw_union:
477 case tok::kw_unsigned:
478 case tok::kw_void:
479 case tok::kw_volatile:
480 case tok::kw_while:
Chris Lattner9298d962007-11-15 05:25:19 +0000481 case tok::kw_bool:
Chris Lattnerff384912007-10-07 02:00:24 +0000482 case tok::kw__Bool:
483 case tok::kw__Complex:
484 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000485 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000486 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000487 }
Steve Naroff294494e2007-08-22 16:35:03 +0000488}
489
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000490/// property-attrlist: one of
491/// readonly getter setter assign retain copy nonatomic
492///
493bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000494 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000495 const IdentifierInfo *II = Tok.getIdentifierInfo();
496 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000497 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000498 }
499 return false;
500}
501
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000502/// objc-for-collection-in: 'in'
503///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000504bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000505 // FIXME: May have to do additional look-ahead to only allow for
506 // valid tokens following an 'in'; such as an identifier, unary operators,
507 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000508 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
509 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000510}
511
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000512/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000513/// qualifier list and builds their bitmask representation in the input
514/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000515///
516/// objc-type-qualifiers:
517/// objc-type-qualifier
518/// objc-type-qualifiers objc-type-qualifier
519///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000521 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000522 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000523 return;
524
525 const IdentifierInfo *II = Tok.getIdentifierInfo();
526 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000528 continue;
529
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000530 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000531 switch (i) {
532 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000533 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
534 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
535 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
536 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
537 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
538 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000539 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000540 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000541 ConsumeToken();
542 II = 0;
543 break;
544 }
545
546 // If this wasn't a recognized qualifier, bail out.
547 if (II) return;
548 }
549}
550
551/// objc-type-name:
552/// '(' objc-type-qualifiers[opt] type-name ')'
553/// '(' objc-type-qualifiers[opt] ')'
554///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000555Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000556 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000557
558 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000559 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000560
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000561 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000562 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000563
Steve Naroff294494e2007-08-22 16:35:03 +0000564 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000565 Ty = ParseTypeName();
566 // FIXME: back when Sema support is in place...
567 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000568 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000569 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000570 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000571 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000572 }
573 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000574 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000575}
576
577/// objc-method-decl:
578/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000579/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000580/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000581/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000582///
583/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000584/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000585/// objc-keyword-selector objc-keyword-decl
586///
587/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000588/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
589/// objc-selector ':' objc-keyword-attributes[opt] identifier
590/// ':' objc-type-name objc-keyword-attributes[opt] identifier
591/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000592///
Steve Naroff4985ace2007-08-22 18:35:33 +0000593/// objc-parmlist:
594/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000595///
Steve Naroff4985ace2007-08-22 18:35:33 +0000596/// objc-parms:
597/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000598///
Steve Naroff4985ace2007-08-22 18:35:33 +0000599/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000600/// , ...
601///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000602/// objc-keyword-attributes: [OBJC2]
603/// __attribute__((unused))
604///
Steve Naroffbef11852007-10-26 20:53:56 +0000605Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000606 tok::TokenKind mType,
607 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000608 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000609{
Steve Naroff4985ace2007-08-22 18:35:33 +0000610 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000611 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000613 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000614 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroffbef11852007-10-26 20:53:56 +0000615 SourceLocation selLoc;
616 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000617 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000618 if (!SelIdent) {
619 Diag(Tok, diag::err_expected_ident); // missing selector name.
620 // FIXME: this creates a unary selector with a null identifier, is this
621 // ok?? Maybe we should skip to the next semicolon or something.
622 }
623
624 // If attributes exist after the method, parse them.
625 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000626 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000627 MethodAttrs = ParseAttributes();
628
629 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000630 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000631 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000632 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000633 }
Steve Narofff28b2642007-09-05 23:30:30 +0000634
Steve Naroff68d331a2007-09-27 14:38:14 +0000635 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
636 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000637 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000638 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000639
640 Action::TypeTy *TypeInfo;
641 while (1) {
642 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000643
Chris Lattnerff384912007-10-07 02:00:24 +0000644 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000645 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000646 Diag(Tok, diag::err_expected_colon);
647 break;
648 }
649 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650 ObjCDeclSpec DSType;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000651 if (Tok.is(tok::l_paren)) { // Parse the argument type.
652 TypeInfo = ParseObjCTypeName(DSType);
653 }
Chris Lattnerff384912007-10-07 02:00:24 +0000654 else
655 TypeInfo = 0;
656 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000657 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000658
Chris Lattnerff384912007-10-07 02:00:24 +0000659 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000660 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000661 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000662
Chris Lattnerdf195262007-10-09 17:51:17 +0000663 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000664 Diag(Tok, diag::err_expected_ident); // missing argument name.
665 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000666 }
Chris Lattnerff384912007-10-07 02:00:24 +0000667 ArgNames.push_back(Tok.getIdentifierInfo());
668 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000669
Chris Lattnerff384912007-10-07 02:00:24 +0000670 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000671 SourceLocation Loc;
672 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000673 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000674 break;
675 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000676 }
Chris Lattnerff384912007-10-07 02:00:24 +0000677
Steve Naroff335eafa2007-11-15 12:35:21 +0000678 bool isVariadic = false;
679
Chris Lattnerff384912007-10-07 02:00:24 +0000680 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000681 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000682 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000683 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000684 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000685 ConsumeToken();
686 break;
687 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000688 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000689 // Parse the c-style argument declaration-specifier.
690 DeclSpec DS;
691 ParseDeclarationSpecifiers(DS);
692 // Parse the declarator.
693 Declarator ParmDecl(DS, Declarator::PrototypeContext);
694 ParseDeclarator(ParmDecl);
695 }
696
697 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000698 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000699 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000700 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000701 MethodAttrs = ParseAttributes();
702
703 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
704 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000705 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000706 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000707 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000708 &ArgNames[0], MethodAttrs,
709 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000710}
711
Steve Naroffdac269b2007-08-20 21:31:48 +0000712/// objc-protocol-refs:
713/// '<' identifier-list '>'
714///
Steve Narofff28b2642007-09-05 23:30:30 +0000715bool Parser::ParseObjCProtocolReferences(
Chris Lattner88cb27a2008-04-07 04:56:42 +0000716 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnerdf195262007-10-09 17:51:17 +0000717 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000718
719 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000720
721 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000723 Diag(Tok, diag::err_expected_ident);
724 SkipUntil(tok::greater);
725 return true;
726 }
727 ProtocolRefs.push_back(Tok.getIdentifierInfo());
728 ConsumeToken();
729
Chris Lattnerdf195262007-10-09 17:51:17 +0000730 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000731 break;
732 ConsumeToken();
733 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000734
Steve Naroffdac269b2007-08-20 21:31:48 +0000735 // Consume the '>'.
Steve Narofff908a872007-10-30 02:23:23 +0000736 if (Tok.is(tok::greater)) {
737 endLoc = ConsumeAnyToken();
738 return false;
739 }
740 Diag(Tok, diag::err_expected_greater);
741 return true;
Steve Naroffdac269b2007-08-20 21:31:48 +0000742}
743
744/// objc-class-instance-variables:
745/// '{' objc-instance-variable-decl-list[opt] '}'
746///
747/// objc-instance-variable-decl-list:
748/// objc-visibility-spec
749/// objc-instance-variable-decl ';'
750/// ';'
751/// objc-instance-variable-decl-list objc-visibility-spec
752/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
753/// objc-instance-variable-decl-list ';'
754///
755/// objc-visibility-spec:
756/// @private
757/// @protected
758/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000759/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000760///
761/// objc-instance-variable-decl:
762/// struct-declaration
763///
Steve Naroff60fccee2007-10-29 21:38:07 +0000764void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
765 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000766 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000767 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000768 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
769
Steve Naroffddbff782007-08-21 21:17:12 +0000770 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000771
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000772 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000773 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000774 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000775 // Each iteration of this loop reads one objc-instance-variable-decl.
776
777 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000778 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000779 Diag(Tok, diag::ext_extra_struct_semi);
780 ConsumeToken();
781 continue;
782 }
Chris Lattnere1359422008-04-10 06:46:29 +0000783
Steve Naroffddbff782007-08-21 21:17:12 +0000784 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000785 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000786 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000787 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000788 case tok::objc_private:
789 case tok::objc_public:
790 case tok::objc_protected:
791 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000792 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000793 ConsumeToken();
794 continue;
795 default:
796 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000797 continue;
798 }
799 }
Chris Lattnere1359422008-04-10 06:46:29 +0000800
801 // Parse all the comma separated declarators.
802 DeclSpec DS;
803 FieldDeclarators.clear();
804 ParseStructDeclaration(DS, FieldDeclarators);
805
806 // Convert them all to fields.
807 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
808 FieldDeclarator &FD = FieldDeclarators[i];
809 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000810 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000811 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000812 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000813 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000814 }
Steve Naroff3536b442007-09-06 21:24:23 +0000815
Chris Lattnerdf195262007-10-09 17:51:17 +0000816 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000817 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000818 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000819 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
820 break;
821 } else {
822 Diag(Tok, diag::err_expected_semi_decl_list);
823 // Skip to end of block or statement
824 SkipUntil(tok::r_brace, true, true);
825 }
826 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000827 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000828 // Call ActOnFields() even if we don't have any decls. This is useful
829 // for code rewriting tools that need to be aware of the empty list.
830 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
831 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000832 LBraceLoc, RBraceLoc);
Steve Naroffddbff782007-08-21 21:17:12 +0000833 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000834}
Steve Naroffdac269b2007-08-20 21:31:48 +0000835
836/// objc-protocol-declaration:
837/// objc-protocol-definition
838/// objc-protocol-forward-reference
839///
840/// objc-protocol-definition:
841/// @protocol identifier
842/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000843/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000844/// @end
845///
846/// objc-protocol-forward-reference:
847/// @protocol identifier-list ';'
848///
849/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000850/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000851/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000852Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000853 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000854 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
855 ConsumeToken(); // the "protocol" identifier
856
Chris Lattnerdf195262007-10-09 17:51:17 +0000857 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000858 Diag(Tok, diag::err_expected_ident); // missing protocol name.
859 return 0;
860 }
861 // Save the protocol name, then consume it.
862 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
863 SourceLocation nameLoc = ConsumeToken();
864
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000865 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000866 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000867 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000868 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000869 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000870 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000871 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000872 ProtocolRefs.push_back(protocolName);
873
874 while (1) {
875 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000876 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000877 Diag(Tok, diag::err_expected_ident);
878 SkipUntil(tok::semi);
879 return 0;
880 }
881 ProtocolRefs.push_back(Tok.getIdentifierInfo());
882 ConsumeToken(); // the identifier
883
Chris Lattnerdf195262007-10-09 17:51:17 +0000884 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000885 break;
886 }
887 // Consume the ';'.
888 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
889 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000890 }
Chris Lattnerc8581052008-03-16 20:19:15 +0000891 if (!ProtocolRefs.empty())
Steve Naroffe440eb82007-10-10 17:32:04 +0000892 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000893 &ProtocolRefs[0],
894 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000895 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000896 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000897 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000898 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000899 return 0;
900 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000901
Steve Naroffe440eb82007-10-10 17:32:04 +0000902 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000903 protocolName, nameLoc,
904 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000905 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000906 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000907
908 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000909 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000910 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000911 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000912 }
913 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000914 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000915}
Steve Naroffdac269b2007-08-20 21:31:48 +0000916
917/// objc-implementation:
918/// objc-class-implementation-prologue
919/// objc-category-implementation-prologue
920///
921/// objc-class-implementation-prologue:
922/// @implementation identifier objc-superclass[opt]
923/// objc-class-instance-variables[opt]
924///
925/// objc-category-implementation-prologue:
926/// @implementation identifier ( identifier )
927
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000928Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
929 SourceLocation atLoc) {
930 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
931 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
932 ConsumeToken(); // the "implementation" identifier
933
Chris Lattnerdf195262007-10-09 17:51:17 +0000934 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000935 Diag(Tok, diag::err_expected_ident); // missing class or category name.
936 return 0;
937 }
938 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000939 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000940 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
941
Chris Lattnerdf195262007-10-09 17:51:17 +0000942 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000943 // we have a category implementation.
944 SourceLocation lparenLoc = ConsumeParen();
945 SourceLocation categoryLoc, rparenLoc;
946 IdentifierInfo *categoryId = 0;
947
Chris Lattnerdf195262007-10-09 17:51:17 +0000948 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000949 categoryId = Tok.getIdentifierInfo();
950 categoryLoc = ConsumeToken();
951 } else {
952 Diag(Tok, diag::err_expected_ident); // missing category name.
953 return 0;
954 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000955 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000956 Diag(Tok, diag::err_expected_rparen);
957 SkipUntil(tok::r_paren, false); // don't stop at ';'
958 return 0;
959 }
960 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000961 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000962 atLoc, nameId, nameLoc, categoryId,
963 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000964 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000965 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000966 }
967 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000968 SourceLocation superClassLoc;
969 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000970 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000971 // We have a super class
972 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000973 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000974 Diag(Tok, diag::err_expected_ident); // missing super class name.
975 return 0;
976 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000977 superClassId = Tok.getIdentifierInfo();
978 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000979 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000980 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +0000981 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000982 superClassId, superClassLoc);
983
Steve Naroff60fccee2007-10-29 21:38:07 +0000984 if (Tok.is(tok::l_brace)) // we have ivars
985 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000987
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000988 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000989}
Steve Naroff60fccee2007-10-29 21:38:07 +0000990
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000991Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
992 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
993 "ParseObjCAtEndDeclaration(): Expected @end");
994 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000995 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000997 else
998 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000999 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +00001000}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001001
1002/// compatibility-alias-decl:
1003/// @compatibility_alias alias-name class-name ';'
1004///
1005Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1006 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1007 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1008 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001009 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001010 Diag(Tok, diag::err_expected_ident);
1011 return 0;
1012 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001013 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1014 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001015 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001016 Diag(Tok, diag::err_expected_ident);
1017 return 0;
1018 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001019 IdentifierInfo *classId = Tok.getIdentifierInfo();
1020 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1021 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001022 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001023 return 0;
1024 }
1025 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1026 aliasId, aliasLoc,
1027 classId, classLoc);
1028 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001029}
1030
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001031/// property-synthesis:
1032/// @synthesize property-ivar-list ';'
1033///
1034/// property-ivar-list:
1035/// property-ivar
1036/// property-ivar-list ',' property-ivar
1037///
1038/// property-ivar:
1039/// identifier
1040/// identifier '=' identifier
1041///
1042Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1043 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1044 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001045 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001046 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001047 Diag(Tok, diag::err_expected_ident);
1048 return 0;
1049 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001050 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001051 IdentifierInfo *propertyIvar = 0;
1052 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1053 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001054 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001055 // property '=' ivar-name
1056 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001058 Diag(Tok, diag::err_expected_ident);
1059 break;
1060 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001061 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001062 ConsumeToken(); // consume ivar-name
1063 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001064 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1065 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001066 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001067 break;
1068 ConsumeToken(); // consume ','
1069 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001070 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001071 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1072 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001073}
1074
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001075/// property-dynamic:
1076/// @dynamic property-list
1077///
1078/// property-list:
1079/// identifier
1080/// property-list ',' identifier
1081///
1082Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1083 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1084 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1085 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001086 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001087 Diag(Tok, diag::err_expected_ident);
1088 return 0;
1089 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001091 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1092 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1093 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1094 propertyId, 0);
1095
Chris Lattnerdf195262007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001097 break;
1098 ConsumeToken(); // consume ','
1099 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001100 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001101 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1102 return 0;
1103}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001104
1105/// objc-throw-statement:
1106/// throw expression[opt];
1107///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001108Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1109 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001110 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001111 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001112 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001113 if (Res.isInvalid) {
1114 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001115 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001116 }
1117 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001118 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001119 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001120}
1121
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001122/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001123/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001124///
1125Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001126 ConsumeToken(); // consume synchronized
1127 if (Tok.isNot(tok::l_paren)) {
1128 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1129 return true;
1130 }
1131 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001132 ExprResult Res = ParseExpression();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001133 if (Res.isInvalid) {
1134 SkipUntil(tok::semi);
1135 return true;
1136 }
1137 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001138 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001139 return true;
1140 }
1141 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001142 if (Tok.isNot(tok::l_brace)) {
1143 Diag (Tok, diag::err_expected_lbrace);
1144 return true;
1145 }
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001146 StmtResult SynchBody = ParseCompoundStatementBody();
1147 if (SynchBody.isInvalid)
1148 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1149 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001150}
1151
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001152/// objc-try-catch-statement:
1153/// @try compound-statement objc-catch-list[opt]
1154/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1155///
1156/// objc-catch-list:
1157/// @catch ( parameter-declaration ) compound-statement
1158/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1159/// catch-parameter-declaration:
1160/// parameter-declaration
1161/// '...' [OBJC2]
1162///
Chris Lattner6b884502008-03-10 06:06:04 +00001163Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001164 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001165
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001166 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001168 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001169 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001170 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001171 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001172 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001173 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001174 if (TryBody.isInvalid)
1175 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner6b884502008-03-10 06:06:04 +00001176
Chris Lattnerdf195262007-10-09 17:51:17 +00001177 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001178 // At this point, we need to lookahead to determine if this @ is the start
1179 // of an @catch or @finally. We don't want to consume the @ token if this
1180 // is an @try or @encode or something else.
1181 Token AfterAt = GetLookAheadToken(1);
1182 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1183 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1184 break;
1185
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001186 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001187 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001188 StmtTy *FirstPart = 0;
1189 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001190 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001191 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001192 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001193 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001194 DeclSpec DS;
1195 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001196 // FIXME: Is BlockContext right?
1197 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1198 ParseDeclarator(DeclaratorInfo);
Chris Lattner81c018d2008-03-13 06:29:04 +00001199 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1200 DeclaratorInfo, 0);
1201 StmtResult stmtResult =
1202 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1203 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001204 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroff64515f32008-02-05 21:27:35 +00001205 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001206 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001207 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001208
1209 StmtResult CatchBody(true);
1210 if (Tok.is(tok::l_brace))
1211 CatchBody = ParseCompoundStatementBody();
1212 else
1213 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001214 if (CatchBody.isInvalid)
1215 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001216 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001217 FirstPart, CatchBody.Val, CatchStmts.Val);
1218 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001219 } else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001220 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1221 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001222 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001223 }
1224 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001225 } else {
1226 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001227 ConsumeToken(); // consume finally
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001228
1229 StmtResult FinallyBody(true);
1230 if (Tok.is(tok::l_brace))
1231 FinallyBody = ParseCompoundStatementBody();
1232 else
1233 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001234 if (FinallyBody.isInvalid)
1235 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001236 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001237 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001238 catch_or_finally_seen = true;
1239 break;
1240 }
1241 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001242 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001243 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001244 return true;
1245 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001246 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001247 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001248}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001249
Steve Naroff3536b442007-09-06 21:24:23 +00001250/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001251///
Steve Naroff71c0a952007-11-13 23:01:27 +00001252Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001253 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001254 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001255 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001256 ConsumeToken();
1257
Steve Naroff409be832007-11-11 19:54:21 +00001258 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001259 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001260 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001261
1262 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1263 SkipUntil(tok::l_brace, true, true);
1264
1265 // If we didn't find the '{', bail out.
1266 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001267 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001268 }
Steve Naroff409be832007-11-11 19:54:21 +00001269 SourceLocation BraceLoc = Tok.getLocation();
1270
1271 // Enter a scope for the method body.
1272 EnterScope(Scope::FnScope|Scope::DeclScope);
1273
1274 // Tell the actions module that we have entered a method definition with the
1275 // specified Declarator for the method.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001276 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001277
1278 StmtResult FnBody = ParseCompoundStatementBody();
1279
1280 // If the function body could not be parsed, make a bogus compoundstmt.
1281 if (FnBody.isInvalid)
1282 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1283
1284 // Leave the function body scope.
1285 ExitScope();
1286
1287 // TODO: Pass argument information.
Steve Naroffd6d054d2007-11-11 23:20:51 +00001288 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001289 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001290}
Anders Carlsson55085182007-08-21 17:43:55 +00001291
Steve Naroff64515f32008-02-05 21:27:35 +00001292Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1293 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001294 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001295 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1296 return ParseObjCThrowStmt(AtLoc);
1297 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1298 return ParseObjCSynchronizedStmt(AtLoc);
1299 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1300 if (Res.isInvalid) {
1301 // If the expression is invalid, skip ahead to the next semicolon. Not
1302 // doing this opens us up to the possibility of infinite loops if
1303 // ParseExpression does not consume any tokens.
1304 SkipUntil(tok::semi);
1305 return true;
1306 }
1307 // Otherwise, eat the semicolon.
1308 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1309 return Actions.ActOnExprStmt(Res.Val);
1310}
1311
Steve Naroffa642beb2007-10-15 20:55:58 +00001312Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001313
1314 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001315 case tok::string_literal: // primary-expression: string-literal
1316 case tok::wide_string_literal:
1317 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1318 default:
1319 break;
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001320 }
1321
1322 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001323 case tok::objc_encode:
1324 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1325 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001326 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001327 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001328 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001329 default:
1330 Diag(AtLoc, diag::err_unexpected_at);
1331 SkipUntil(tok::semi);
Chris Lattner9d493952008-01-30 21:20:25 +00001332 return true;
Anders Carlsson55085182007-08-21 17:43:55 +00001333 }
Anders Carlsson55085182007-08-21 17:43:55 +00001334}
1335
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001336/// objc-message-expr:
1337/// '[' objc-receiver objc-message-args ']'
1338///
1339/// objc-receiver:
1340/// expression
1341/// class-name
1342/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001343Parser::ExprResult Parser::ParseObjCMessageExpression() {
1344 assert(Tok.is(tok::l_square) && "'[' expected");
1345 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1346
1347 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001348 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001349 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1350 ConsumeToken();
1351 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1352 }
1353
1354 ExprResult Res = ParseAssignmentExpression();
1355 if (Res.isInvalid) {
1356 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattner5c749422008-01-25 19:43:26 +00001357 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001358 return Res;
1359 }
1360 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1361}
1362
1363/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1364/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001365///
1366/// objc-message-args:
1367/// objc-selector
1368/// objc-keywordarg-list
1369///
1370/// objc-keywordarg-list:
1371/// objc-keywordarg
1372/// objc-keywordarg-list objc-keywordarg
1373///
1374/// objc-keywordarg:
1375/// selector-name[opt] ':' objc-keywordexpr
1376///
1377/// objc-keywordexpr:
1378/// nonempty-expr-list
1379///
1380/// nonempty-expr-list:
1381/// assignment-expression
1382/// nonempty-expr-list , assignment-expression
1383///
Chris Lattner699b6612008-01-25 18:59:06 +00001384Parser::ExprResult
1385Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1386 IdentifierInfo *ReceiverName,
1387 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001388 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001389 SourceLocation Loc;
1390 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001391
1392 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1393 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1394
Chris Lattnerdf195262007-10-09 17:51:17 +00001395 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001396 while (1) {
1397 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001398 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001399
Chris Lattnerdf195262007-10-09 17:51:17 +00001400 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001401 Diag(Tok, diag::err_expected_colon);
1402 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001403 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001404 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001405 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001406 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001407 ExprResult Res = ParseAssignmentExpression();
1408 if (Res.isInvalid) {
1409 SkipUntil(tok::identifier);
1410 return Res;
1411 }
1412 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001413 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001414
1415 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001416 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001417 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001418 break;
1419 // We have a selector or a colon, continue parsing.
1420 }
1421 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001422 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001423 ConsumeToken(); // Eat the ','.
1424 /// Parse the expression after ','
1425 ExprResult Res = ParseAssignmentExpression();
1426 if (Res.isInvalid) {
1427 SkipUntil(tok::identifier);
1428 return Res;
1429 }
1430 // We have a valid expression.
1431 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001432 }
1433 } else if (!selIdent) {
1434 Diag(Tok, diag::err_expected_ident); // missing selector name.
1435 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001436 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001437 }
Chris Lattnerff384912007-10-07 02:00:24 +00001438
Chris Lattnerdf195262007-10-09 17:51:17 +00001439 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001440 Diag(Tok, diag::err_expected_rsquare);
1441 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001442 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001443 }
Chris Lattner699b6612008-01-25 18:59:06 +00001444 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001445
Steve Naroff29238a02007-10-05 18:42:47 +00001446 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001447 if (nKeys == 0)
1448 KeyIdents.push_back(selIdent);
1449 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1450
1451 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001452 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001453 return Actions.ActOnClassMessage(CurScope,
Chris Lattner699b6612008-01-25 18:59:06 +00001454 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001455 &KeyExprs[0], KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001456 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001457 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001458}
1459
Steve Naroffbeaf2992007-11-03 11:27:19 +00001460Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001461 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001462 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001463
1464 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1465 // expressions. At this point, we know that the only valid thing that starts
1466 // with '@' is an @"".
1467 llvm::SmallVector<SourceLocation, 4> AtLocs;
1468 llvm::SmallVector<ExprTy*, 4> AtStrings;
1469 AtLocs.push_back(AtLoc);
1470 AtStrings.push_back(Res.Val);
1471
1472 while (Tok.is(tok::at)) {
1473 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001474
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001475 ExprResult Res(true); // Invalid unless there is a string literal.
1476 if (isTokenStringLiteral())
1477 Res = ParseStringLiteralExpression();
1478 else
1479 Diag(Tok, diag::err_objc_concat_string);
1480
1481 if (Res.isInvalid) {
1482 while (!AtStrings.empty()) {
1483 Actions.DeleteExpr(AtStrings.back());
1484 AtStrings.pop_back();
1485 }
1486 return Res;
1487 }
1488
1489 AtStrings.push_back(Res.Val);
1490 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001491
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001492 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1493 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001494}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001495
1496/// objc-encode-expression:
1497/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001498Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001499 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001500
1501 SourceLocation EncLoc = ConsumeToken();
1502
Chris Lattnerdf195262007-10-09 17:51:17 +00001503 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001504 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1505 return true;
1506 }
1507
1508 SourceLocation LParenLoc = ConsumeParen();
1509
1510 TypeTy *Ty = ParseTypeName();
1511
Anders Carlsson4988ae32007-08-23 15:31:37 +00001512 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001513
Chris Lattner674af952007-10-16 22:51:17 +00001514 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001515 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001516}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001517
1518/// objc-protocol-expression
1519/// @protocol ( protocol-name )
1520
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001521Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001522{
1523 SourceLocation ProtoLoc = ConsumeToken();
1524
Chris Lattnerdf195262007-10-09 17:51:17 +00001525 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001526 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1527 return true;
1528 }
1529
1530 SourceLocation LParenLoc = ConsumeParen();
1531
Chris Lattnerdf195262007-10-09 17:51:17 +00001532 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001533 Diag(Tok, diag::err_expected_ident);
1534 return true;
1535 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001536 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001537 ConsumeToken();
1538
Anders Carlsson4988ae32007-08-23 15:31:37 +00001539 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001540
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001541 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1542 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001543}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001544
1545/// objc-selector-expression
1546/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001547Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001548{
1549 SourceLocation SelectorLoc = ConsumeToken();
1550
1551 if (Tok.isNot(tok::l_paren)) {
1552 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1553 return 0;
1554 }
1555
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001556 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001557 SourceLocation LParenLoc = ConsumeParen();
1558 SourceLocation sLoc;
1559 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1560 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001561 Diag(Tok, diag::err_expected_ident); // missing selector name.
1562 return 0;
1563 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001564 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001565 unsigned nColons = 0;
1566 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001567 while (1) {
1568 if (Tok.isNot(tok::colon)) {
1569 Diag(Tok, diag::err_expected_colon);
1570 break;
1571 }
Chris Lattnercb53b362007-12-27 19:57:00 +00001572 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001573 ConsumeToken(); // Eat the ':'.
1574 if (Tok.is(tok::r_paren))
1575 break;
1576 // Check for another keyword selector.
1577 SourceLocation Loc;
1578 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001579 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001580 if (!SelIdent && Tok.isNot(tok::colon))
1581 break;
1582 }
Steve Naroff887407e2007-12-05 22:21:29 +00001583 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001584 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001585 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001586 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001587 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001588 }