blob: 1ff5877d2cda3fd5cc21572c7557c6cf91d28797 [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 =
274 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName());
275 Selector SetterSel =
276 PP.getSelectorTable().getNullarySelector(OCDS.getSetterName());
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000277 DeclTy *Property = Actions.ActOnProperty(CurScope,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000278 DS.getSourceRange().getBegin(), FD, OCDS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000279 GetterSel, SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000280 MethodImplKind);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000281 allProperties.push_back(Property);
282 }
Steve Naroff294494e2007-08-22 16:35:03 +0000283 continue;
284 } else {
285 Diag(Tok, diag::err_objc_illegal_interface_qual);
286 ConsumeToken();
287 }
288 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000289 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
290 DeclTy *methodPrototype =
291 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000292 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000293 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
294 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000295 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000296 continue;
297 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000298 else if (Tok.is(tok::at))
299 continue;
300
Chris Lattnerdf195262007-10-09 17:51:17 +0000301 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000302 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000303 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000304 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000305 else {
Steve Naroff4985ace2007-08-22 18:35:33 +0000306 // FIXME: as the name implies, this rule allows function definitions.
307 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000308 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000309 }
Steve Naroff294494e2007-08-22 16:35:03 +0000310 }
Steve Naroff2feac5e2007-10-30 03:43:13 +0000311 /// Insert collected methods declarations into the @interface object.
Steve Naroff0416fb92007-11-11 17:19:15 +0000312 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
313 &allProperties[0], allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000314}
315
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000316/// Parse property attribute declarations.
317///
318/// property-attr-decl: '(' property-attrlist ')'
319/// property-attrlist:
320/// property-attribute
321/// property-attrlist ',' property-attribute
322/// property-attribute:
323/// getter '=' identifier
324/// setter '=' identifier ':'
325/// readonly
326/// readwrite
327/// assign
328/// retain
329/// copy
330/// nonatomic
331///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000332void Parser::ParseObjCPropertyAttribute (ObjCDeclSpec &DS) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000333 SourceLocation loc = ConsumeParen(); // consume '('
334 while (isObjCPropertyAttribute()) {
335 const IdentifierInfo *II = Tok.getIdentifierInfo();
336 // getter/setter require extra treatment.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000337 if (II == ObjCPropertyAttrs[objc_getter] ||
338 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000339 // skip getter/setter part.
340 SourceLocation loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000341 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000342 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000343 if (Tok.is(tok::identifier)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000344 if (II == ObjCPropertyAttrs[objc_setter]) {
345 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000346 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000347 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000348 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000349 Diag(loc, diag::err_expected_colon);
350 SkipUntil(tok::r_paren,true,true);
351 break;
352 }
353 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000354 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000355 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000356 DS.setGetterName(Tok.getIdentifierInfo());
357 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000358 }
359 else {
360 Diag(loc, diag::err_expected_ident);
Chris Lattnercb53b362007-12-27 19:57:00 +0000361 SkipUntil(tok::r_paren,true,true);
362 break;
363 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000364 }
365 else {
366 Diag(loc, diag::err_objc_expected_equal);
367 SkipUntil(tok::r_paren,true,true);
368 break;
369 }
370 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000371
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000372 else if (II == ObjCPropertyAttrs[objc_readonly])
373 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
374 else if (II == ObjCPropertyAttrs[objc_assign])
375 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
376 else if (II == ObjCPropertyAttrs[objc_readwrite])
377 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
378 else if (II == ObjCPropertyAttrs[objc_retain])
379 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
380 else if (II == ObjCPropertyAttrs[objc_copy])
381 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
382 else if (II == ObjCPropertyAttrs[objc_nonatomic])
383 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000384
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000385 ConsumeToken(); // consume last attribute token
Chris Lattnerdf195262007-10-09 17:51:17 +0000386 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000387 loc = ConsumeToken();
388 continue;
389 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000390 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000391 break;
392 Diag(loc, diag::err_expected_rparen);
393 SkipUntil(tok::semi);
394 return;
395 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000396 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000397 ConsumeParen();
398 else {
399 Diag(loc, diag::err_objc_expected_property_attr);
400 SkipUntil(tok::r_paren); // recover from error inside attribute list
401 }
402}
403
Steve Naroff3536b442007-09-06 21:24:23 +0000404/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000405/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000406/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000407///
408/// objc-instance-method: '-'
409/// objc-class-method: '+'
410///
Steve Naroff4985ace2007-08-22 18:35:33 +0000411/// objc-method-attributes: [OBJC2]
412/// __attribute__((deprecated))
413///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000414Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000415 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000416 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000417
418 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000419 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000420
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000421 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000422 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000423 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000424 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000425}
426
427/// objc-selector:
428/// identifier
429/// one of
430/// enum struct union if else while do for switch case default
431/// break continue return goto asm sizeof typeof __alignof
432/// unsigned long const short volatile signed restrict _Complex
433/// in out inout bycopy byref oneway int char float double void _Bool
434///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000435IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000436 switch (Tok.getKind()) {
437 default:
438 return 0;
439 case tok::identifier:
440 case tok::kw_typeof:
441 case tok::kw___alignof:
442 case tok::kw_auto:
443 case tok::kw_break:
444 case tok::kw_case:
445 case tok::kw_char:
446 case tok::kw_const:
447 case tok::kw_continue:
448 case tok::kw_default:
449 case tok::kw_do:
450 case tok::kw_double:
451 case tok::kw_else:
452 case tok::kw_enum:
453 case tok::kw_extern:
454 case tok::kw_float:
455 case tok::kw_for:
456 case tok::kw_goto:
457 case tok::kw_if:
458 case tok::kw_inline:
459 case tok::kw_int:
460 case tok::kw_long:
461 case tok::kw_register:
462 case tok::kw_restrict:
463 case tok::kw_return:
464 case tok::kw_short:
465 case tok::kw_signed:
466 case tok::kw_sizeof:
467 case tok::kw_static:
468 case tok::kw_struct:
469 case tok::kw_switch:
470 case tok::kw_typedef:
471 case tok::kw_union:
472 case tok::kw_unsigned:
473 case tok::kw_void:
474 case tok::kw_volatile:
475 case tok::kw_while:
Chris Lattner9298d962007-11-15 05:25:19 +0000476 case tok::kw_bool:
Chris Lattnerff384912007-10-07 02:00:24 +0000477 case tok::kw__Bool:
478 case tok::kw__Complex:
479 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000480 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000481 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000482 }
Steve Naroff294494e2007-08-22 16:35:03 +0000483}
484
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000485/// property-attrlist: one of
486/// readonly getter setter assign retain copy nonatomic
487///
488bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000489 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000490 const IdentifierInfo *II = Tok.getIdentifierInfo();
491 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000492 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000493 }
494 return false;
495}
496
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000497/// objc-for-collection-in: 'in'
498///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000499bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000500 // FIXME: May have to do additional look-ahead to only allow for
501 // valid tokens following an 'in'; such as an identifier, unary operators,
502 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000503 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
504 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000505}
506
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000507/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000508/// qualifier list and builds their bitmask representation in the input
509/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000510///
511/// objc-type-qualifiers:
512/// objc-type-qualifier
513/// objc-type-qualifiers objc-type-qualifier
514///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000515void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000516 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000517 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000518 return;
519
520 const IdentifierInfo *II = Tok.getIdentifierInfo();
521 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000523 continue;
524
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000525 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000526 switch (i) {
527 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000528 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
529 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
530 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
531 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
532 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
533 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000534 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000535 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000536 ConsumeToken();
537 II = 0;
538 break;
539 }
540
541 // If this wasn't a recognized qualifier, bail out.
542 if (II) return;
543 }
544}
545
546/// objc-type-name:
547/// '(' objc-type-qualifiers[opt] type-name ')'
548/// '(' objc-type-qualifiers[opt] ')'
549///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000550Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000551 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000552
553 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000554 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000555
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000556 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000557 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000558
Steve Naroff294494e2007-08-22 16:35:03 +0000559 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000560 Ty = ParseTypeName();
561 // FIXME: back when Sema support is in place...
562 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000563 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000564 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000565 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000566 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000567 }
568 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000569 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000570}
571
572/// objc-method-decl:
573/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000574/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000575/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000576/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000577///
578/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000579/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000580/// objc-keyword-selector objc-keyword-decl
581///
582/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000583/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
584/// objc-selector ':' objc-keyword-attributes[opt] identifier
585/// ':' objc-type-name objc-keyword-attributes[opt] identifier
586/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000587///
Steve Naroff4985ace2007-08-22 18:35:33 +0000588/// objc-parmlist:
589/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000590///
Steve Naroff4985ace2007-08-22 18:35:33 +0000591/// objc-parms:
592/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000593///
Steve Naroff4985ace2007-08-22 18:35:33 +0000594/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000595/// , ...
596///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000597/// objc-keyword-attributes: [OBJC2]
598/// __attribute__((unused))
599///
Steve Naroffbef11852007-10-26 20:53:56 +0000600Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000601 tok::TokenKind mType,
602 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000603 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000604{
Steve Naroff4985ace2007-08-22 18:35:33 +0000605 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000606 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000608 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000609 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroffbef11852007-10-26 20:53:56 +0000610 SourceLocation selLoc;
611 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000612 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000613 if (!SelIdent) {
614 Diag(Tok, diag::err_expected_ident); // missing selector name.
615 // FIXME: this creates a unary selector with a null identifier, is this
616 // ok?? Maybe we should skip to the next semicolon or something.
617 }
618
619 // If attributes exist after the method, parse them.
620 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000621 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000622 MethodAttrs = ParseAttributes();
623
624 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000625 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000626 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000627 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000628 }
Steve Narofff28b2642007-09-05 23:30:30 +0000629
Steve Naroff68d331a2007-09-27 14:38:14 +0000630 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
631 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000632 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000633 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000634
635 Action::TypeTy *TypeInfo;
636 while (1) {
637 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000638
Chris Lattnerff384912007-10-07 02:00:24 +0000639 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000640 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000641 Diag(Tok, diag::err_expected_colon);
642 break;
643 }
644 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000645 ObjCDeclSpec DSType;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000646 if (Tok.is(tok::l_paren)) { // Parse the argument type.
647 TypeInfo = ParseObjCTypeName(DSType);
648 }
Chris Lattnerff384912007-10-07 02:00:24 +0000649 else
650 TypeInfo = 0;
651 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000652 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000653
Chris Lattnerff384912007-10-07 02:00:24 +0000654 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000655 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000656 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000657
Chris Lattnerdf195262007-10-09 17:51:17 +0000658 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000659 Diag(Tok, diag::err_expected_ident); // missing argument name.
660 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000661 }
Chris Lattnerff384912007-10-07 02:00:24 +0000662 ArgNames.push_back(Tok.getIdentifierInfo());
663 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000664
Chris Lattnerff384912007-10-07 02:00:24 +0000665 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000666 SourceLocation Loc;
667 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000668 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000669 break;
670 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000671 }
Chris Lattnerff384912007-10-07 02:00:24 +0000672
Steve Naroff335eafa2007-11-15 12:35:21 +0000673 bool isVariadic = false;
674
Chris Lattnerff384912007-10-07 02:00:24 +0000675 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000676 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000677 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000678 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000679 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000680 ConsumeToken();
681 break;
682 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000683 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000684 // Parse the c-style argument declaration-specifier.
685 DeclSpec DS;
686 ParseDeclarationSpecifiers(DS);
687 // Parse the declarator.
688 Declarator ParmDecl(DS, Declarator::PrototypeContext);
689 ParseDeclarator(ParmDecl);
690 }
691
692 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000693 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000694 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000695 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000696 MethodAttrs = ParseAttributes();
697
698 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
699 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000700 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000701 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000702 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000703 &ArgNames[0], MethodAttrs,
704 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000705}
706
Steve Naroffdac269b2007-08-20 21:31:48 +0000707/// objc-protocol-refs:
708/// '<' identifier-list '>'
709///
Steve Narofff28b2642007-09-05 23:30:30 +0000710bool Parser::ParseObjCProtocolReferences(
Chris Lattner88cb27a2008-04-07 04:56:42 +0000711 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnerdf195262007-10-09 17:51:17 +0000712 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000713
714 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000715
716 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000717 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000718 Diag(Tok, diag::err_expected_ident);
719 SkipUntil(tok::greater);
720 return true;
721 }
722 ProtocolRefs.push_back(Tok.getIdentifierInfo());
723 ConsumeToken();
724
Chris Lattnerdf195262007-10-09 17:51:17 +0000725 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000726 break;
727 ConsumeToken();
728 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000729
Steve Naroffdac269b2007-08-20 21:31:48 +0000730 // Consume the '>'.
Steve Narofff908a872007-10-30 02:23:23 +0000731 if (Tok.is(tok::greater)) {
732 endLoc = ConsumeAnyToken();
733 return false;
734 }
735 Diag(Tok, diag::err_expected_greater);
736 return true;
Steve Naroffdac269b2007-08-20 21:31:48 +0000737}
738
739/// objc-class-instance-variables:
740/// '{' objc-instance-variable-decl-list[opt] '}'
741///
742/// objc-instance-variable-decl-list:
743/// objc-visibility-spec
744/// objc-instance-variable-decl ';'
745/// ';'
746/// objc-instance-variable-decl-list objc-visibility-spec
747/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
748/// objc-instance-variable-decl-list ';'
749///
750/// objc-visibility-spec:
751/// @private
752/// @protected
753/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000754/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000755///
756/// objc-instance-variable-decl:
757/// struct-declaration
758///
Steve Naroff60fccee2007-10-29 21:38:07 +0000759void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
760 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000761 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000762 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000763 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
764
Steve Naroffddbff782007-08-21 21:17:12 +0000765 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000766
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000767 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000768 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000769 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000770 // Each iteration of this loop reads one objc-instance-variable-decl.
771
772 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000773 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000774 Diag(Tok, diag::ext_extra_struct_semi);
775 ConsumeToken();
776 continue;
777 }
Chris Lattnere1359422008-04-10 06:46:29 +0000778
Steve Naroffddbff782007-08-21 21:17:12 +0000779 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000780 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000781 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000782 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000783 case tok::objc_private:
784 case tok::objc_public:
785 case tok::objc_protected:
786 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000787 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000788 ConsumeToken();
789 continue;
790 default:
791 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000792 continue;
793 }
794 }
Chris Lattnere1359422008-04-10 06:46:29 +0000795
796 // Parse all the comma separated declarators.
797 DeclSpec DS;
798 FieldDeclarators.clear();
799 ParseStructDeclaration(DS, FieldDeclarators);
800
801 // Convert them all to fields.
802 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
803 FieldDeclarator &FD = FieldDeclarators[i];
804 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000805 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000806 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000807 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000808 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000809 }
Steve Naroff3536b442007-09-06 21:24:23 +0000810
Chris Lattnerdf195262007-10-09 17:51:17 +0000811 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000812 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000813 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000814 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
815 break;
816 } else {
817 Diag(Tok, diag::err_expected_semi_decl_list);
818 // Skip to end of block or statement
819 SkipUntil(tok::r_brace, true, true);
820 }
821 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000822 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000823 // Call ActOnFields() even if we don't have any decls. This is useful
824 // for code rewriting tools that need to be aware of the empty list.
825 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
826 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000827 LBraceLoc, RBraceLoc);
Steve Naroffddbff782007-08-21 21:17:12 +0000828 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829}
Steve Naroffdac269b2007-08-20 21:31:48 +0000830
831/// objc-protocol-declaration:
832/// objc-protocol-definition
833/// objc-protocol-forward-reference
834///
835/// objc-protocol-definition:
836/// @protocol identifier
837/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000838/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000839/// @end
840///
841/// objc-protocol-forward-reference:
842/// @protocol identifier-list ';'
843///
844/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000845/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000846/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000847Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000848 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000849 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
850 ConsumeToken(); // the "protocol" identifier
851
Chris Lattnerdf195262007-10-09 17:51:17 +0000852 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000853 Diag(Tok, diag::err_expected_ident); // missing protocol name.
854 return 0;
855 }
856 // Save the protocol name, then consume it.
857 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
858 SourceLocation nameLoc = ConsumeToken();
859
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000860 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000861 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000862 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000863 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000864 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000865 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000866 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000867 ProtocolRefs.push_back(protocolName);
868
869 while (1) {
870 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000871 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000872 Diag(Tok, diag::err_expected_ident);
873 SkipUntil(tok::semi);
874 return 0;
875 }
876 ProtocolRefs.push_back(Tok.getIdentifierInfo());
877 ConsumeToken(); // the identifier
878
Chris Lattnerdf195262007-10-09 17:51:17 +0000879 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000880 break;
881 }
882 // Consume the ';'.
883 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
884 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000885 }
Chris Lattnerc8581052008-03-16 20:19:15 +0000886 if (!ProtocolRefs.empty())
Steve Naroffe440eb82007-10-10 17:32:04 +0000887 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000888 &ProtocolRefs[0],
889 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000890 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000891 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000892 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000893 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000894 return 0;
895 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000896
Steve Naroffe440eb82007-10-10 17:32:04 +0000897 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000898 protocolName, nameLoc,
899 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000900 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000901 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000902
903 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000904 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000905 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000906 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000907 }
908 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000909 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000910}
Steve Naroffdac269b2007-08-20 21:31:48 +0000911
912/// objc-implementation:
913/// objc-class-implementation-prologue
914/// objc-category-implementation-prologue
915///
916/// objc-class-implementation-prologue:
917/// @implementation identifier objc-superclass[opt]
918/// objc-class-instance-variables[opt]
919///
920/// objc-category-implementation-prologue:
921/// @implementation identifier ( identifier )
922
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000923Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
924 SourceLocation atLoc) {
925 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
926 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
927 ConsumeToken(); // the "implementation" identifier
928
Chris Lattnerdf195262007-10-09 17:51:17 +0000929 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000930 Diag(Tok, diag::err_expected_ident); // missing class or category name.
931 return 0;
932 }
933 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000934 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000935 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
936
Chris Lattnerdf195262007-10-09 17:51:17 +0000937 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000938 // we have a category implementation.
939 SourceLocation lparenLoc = ConsumeParen();
940 SourceLocation categoryLoc, rparenLoc;
941 IdentifierInfo *categoryId = 0;
942
Chris Lattnerdf195262007-10-09 17:51:17 +0000943 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000944 categoryId = Tok.getIdentifierInfo();
945 categoryLoc = ConsumeToken();
946 } else {
947 Diag(Tok, diag::err_expected_ident); // missing category name.
948 return 0;
949 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000950 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000951 Diag(Tok, diag::err_expected_rparen);
952 SkipUntil(tok::r_paren, false); // don't stop at ';'
953 return 0;
954 }
955 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000956 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000957 atLoc, nameId, nameLoc, categoryId,
958 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000959 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000960 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000961 }
962 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000963 SourceLocation superClassLoc;
964 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000965 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000966 // We have a super class
967 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000968 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000969 Diag(Tok, diag::err_expected_ident); // missing super class name.
970 return 0;
971 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000972 superClassId = Tok.getIdentifierInfo();
973 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000974 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000975 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +0000976 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000977 superClassId, superClassLoc);
978
Steve Naroff60fccee2007-10-29 21:38:07 +0000979 if (Tok.is(tok::l_brace)) // we have ivars
980 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000981 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000982
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000983 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000984}
Steve Naroff60fccee2007-10-29 21:38:07 +0000985
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000986Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
987 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
988 "ParseObjCAtEndDeclaration(): Expected @end");
989 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000990 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000991 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000992 else
993 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +0000995}
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000996
997/// compatibility-alias-decl:
998/// @compatibility_alias alias-name class-name ';'
999///
1000Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1001 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1002 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1003 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001004 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001005 Diag(Tok, diag::err_expected_ident);
1006 return 0;
1007 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001008 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1009 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001010 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001011 Diag(Tok, diag::err_expected_ident);
1012 return 0;
1013 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001014 IdentifierInfo *classId = Tok.getIdentifierInfo();
1015 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1016 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001017 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001018 return 0;
1019 }
1020 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1021 aliasId, aliasLoc,
1022 classId, classLoc);
1023 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001024}
1025
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001026/// property-synthesis:
1027/// @synthesize property-ivar-list ';'
1028///
1029/// property-ivar-list:
1030/// property-ivar
1031/// property-ivar-list ',' property-ivar
1032///
1033/// property-ivar:
1034/// identifier
1035/// identifier '=' identifier
1036///
1037Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1038 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1039 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001040 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001041 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001042 Diag(Tok, diag::err_expected_ident);
1043 return 0;
1044 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001045 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001046 IdentifierInfo *propertyIvar = 0;
1047 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1048 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001049 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001050 // property '=' ivar-name
1051 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001052 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001053 Diag(Tok, diag::err_expected_ident);
1054 break;
1055 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001056 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001057 ConsumeToken(); // consume ivar-name
1058 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001059 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1060 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001061 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001062 break;
1063 ConsumeToken(); // consume ','
1064 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001065 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001066 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1067 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001068}
1069
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001070/// property-dynamic:
1071/// @dynamic property-list
1072///
1073/// property-list:
1074/// identifier
1075/// property-list ',' identifier
1076///
1077Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1078 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1079 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1080 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001081 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001082 Diag(Tok, diag::err_expected_ident);
1083 return 0;
1084 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001085 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001086 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1087 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1088 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1089 propertyId, 0);
1090
Chris Lattnerdf195262007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001092 break;
1093 ConsumeToken(); // consume ','
1094 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001095 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001096 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1097 return 0;
1098}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001099
1100/// objc-throw-statement:
1101/// throw expression[opt];
1102///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001103Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1104 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001105 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001106 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001107 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001108 if (Res.isInvalid) {
1109 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001110 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001111 }
1112 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001113 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001115}
1116
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001117/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001118/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001119///
1120Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001121 ConsumeToken(); // consume synchronized
1122 if (Tok.isNot(tok::l_paren)) {
1123 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1124 return true;
1125 }
1126 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001127 ExprResult Res = ParseExpression();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001128 if (Res.isInvalid) {
1129 SkipUntil(tok::semi);
1130 return true;
1131 }
1132 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001133 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001134 return true;
1135 }
1136 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001137 if (Tok.isNot(tok::l_brace)) {
1138 Diag (Tok, diag::err_expected_lbrace);
1139 return true;
1140 }
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001141 StmtResult SynchBody = ParseCompoundStatementBody();
1142 if (SynchBody.isInvalid)
1143 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1144 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001145}
1146
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001147/// objc-try-catch-statement:
1148/// @try compound-statement objc-catch-list[opt]
1149/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1150///
1151/// objc-catch-list:
1152/// @catch ( parameter-declaration ) compound-statement
1153/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1154/// catch-parameter-declaration:
1155/// parameter-declaration
1156/// '...' [OBJC2]
1157///
Chris Lattner6b884502008-03-10 06:06:04 +00001158Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001159 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001160
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001161 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001162 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001163 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001164 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001165 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001166 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001167 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001168 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001169 if (TryBody.isInvalid)
1170 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner6b884502008-03-10 06:06:04 +00001171
Chris Lattnerdf195262007-10-09 17:51:17 +00001172 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001173 // At this point, we need to lookahead to determine if this @ is the start
1174 // of an @catch or @finally. We don't want to consume the @ token if this
1175 // is an @try or @encode or something else.
1176 Token AfterAt = GetLookAheadToken(1);
1177 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1178 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1179 break;
1180
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001181 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001182 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001183 StmtTy *FirstPart = 0;
1184 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001185 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001186 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001187 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001188 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001189 DeclSpec DS;
1190 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001191 // FIXME: Is BlockContext right?
1192 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1193 ParseDeclarator(DeclaratorInfo);
Chris Lattner81c018d2008-03-13 06:29:04 +00001194 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1195 DeclaratorInfo, 0);
1196 StmtResult stmtResult =
1197 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1198 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001199 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroff64515f32008-02-05 21:27:35 +00001200 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001201 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001202 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001203
1204 StmtResult CatchBody(true);
1205 if (Tok.is(tok::l_brace))
1206 CatchBody = ParseCompoundStatementBody();
1207 else
1208 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001209 if (CatchBody.isInvalid)
1210 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001211 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001212 FirstPart, CatchBody.Val, CatchStmts.Val);
1213 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001214 } else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001215 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1216 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001217 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001218 }
1219 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001220 } else {
1221 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001222 ConsumeToken(); // consume finally
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001223
1224 StmtResult FinallyBody(true);
1225 if (Tok.is(tok::l_brace))
1226 FinallyBody = ParseCompoundStatementBody();
1227 else
1228 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001229 if (FinallyBody.isInvalid)
1230 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001231 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001232 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001233 catch_or_finally_seen = true;
1234 break;
1235 }
1236 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001237 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001238 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001239 return true;
1240 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001241 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001242 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001243}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001244
Steve Naroff3536b442007-09-06 21:24:23 +00001245/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001246///
Steve Naroff71c0a952007-11-13 23:01:27 +00001247Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001248 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001249 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001250 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001251 ConsumeToken();
1252
Steve Naroff409be832007-11-11 19:54:21 +00001253 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001255 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001256
1257 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1258 SkipUntil(tok::l_brace, true, true);
1259
1260 // If we didn't find the '{', bail out.
1261 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001262 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001263 }
Steve Naroff409be832007-11-11 19:54:21 +00001264 SourceLocation BraceLoc = Tok.getLocation();
1265
1266 // Enter a scope for the method body.
1267 EnterScope(Scope::FnScope|Scope::DeclScope);
1268
1269 // Tell the actions module that we have entered a method definition with the
1270 // specified Declarator for the method.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001271 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001272
1273 StmtResult FnBody = ParseCompoundStatementBody();
1274
1275 // If the function body could not be parsed, make a bogus compoundstmt.
1276 if (FnBody.isInvalid)
1277 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1278
1279 // Leave the function body scope.
1280 ExitScope();
1281
1282 // TODO: Pass argument information.
Steve Naroffd6d054d2007-11-11 23:20:51 +00001283 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001284 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001285}
Anders Carlsson55085182007-08-21 17:43:55 +00001286
Steve Naroff64515f32008-02-05 21:27:35 +00001287Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1288 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001289 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001290 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1291 return ParseObjCThrowStmt(AtLoc);
1292 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1293 return ParseObjCSynchronizedStmt(AtLoc);
1294 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1295 if (Res.isInvalid) {
1296 // If the expression is invalid, skip ahead to the next semicolon. Not
1297 // doing this opens us up to the possibility of infinite loops if
1298 // ParseExpression does not consume any tokens.
1299 SkipUntil(tok::semi);
1300 return true;
1301 }
1302 // Otherwise, eat the semicolon.
1303 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1304 return Actions.ActOnExprStmt(Res.Val);
1305}
1306
Steve Naroffa642beb2007-10-15 20:55:58 +00001307Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001308
1309 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001310 case tok::string_literal: // primary-expression: string-literal
1311 case tok::wide_string_literal:
1312 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1313 default:
1314 break;
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001315 }
1316
1317 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001318 case tok::objc_encode:
1319 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1320 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001321 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001322 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001323 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001324 default:
1325 Diag(AtLoc, diag::err_unexpected_at);
1326 SkipUntil(tok::semi);
Chris Lattner9d493952008-01-30 21:20:25 +00001327 return true;
Anders Carlsson55085182007-08-21 17:43:55 +00001328 }
Anders Carlsson55085182007-08-21 17:43:55 +00001329}
1330
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001331/// objc-message-expr:
1332/// '[' objc-receiver objc-message-args ']'
1333///
1334/// objc-receiver:
1335/// expression
1336/// class-name
1337/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001338Parser::ExprResult Parser::ParseObjCMessageExpression() {
1339 assert(Tok.is(tok::l_square) && "'[' expected");
1340 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1341
1342 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001343 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001344 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1345 ConsumeToken();
1346 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1347 }
1348
1349 ExprResult Res = ParseAssignmentExpression();
1350 if (Res.isInvalid) {
1351 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattner5c749422008-01-25 19:43:26 +00001352 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001353 return Res;
1354 }
1355 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1356}
1357
1358/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1359/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001360///
1361/// objc-message-args:
1362/// objc-selector
1363/// objc-keywordarg-list
1364///
1365/// objc-keywordarg-list:
1366/// objc-keywordarg
1367/// objc-keywordarg-list objc-keywordarg
1368///
1369/// objc-keywordarg:
1370/// selector-name[opt] ':' objc-keywordexpr
1371///
1372/// objc-keywordexpr:
1373/// nonempty-expr-list
1374///
1375/// nonempty-expr-list:
1376/// assignment-expression
1377/// nonempty-expr-list , assignment-expression
1378///
Chris Lattner699b6612008-01-25 18:59:06 +00001379Parser::ExprResult
1380Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1381 IdentifierInfo *ReceiverName,
1382 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001383 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001384 SourceLocation Loc;
1385 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001386
1387 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1388 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1389
Chris Lattnerdf195262007-10-09 17:51:17 +00001390 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001391 while (1) {
1392 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001393 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001394
Chris Lattnerdf195262007-10-09 17:51:17 +00001395 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001396 Diag(Tok, diag::err_expected_colon);
1397 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001398 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001399 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001400 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001401 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001402 ExprResult Res = ParseAssignmentExpression();
1403 if (Res.isInvalid) {
1404 SkipUntil(tok::identifier);
1405 return Res;
1406 }
1407 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001408 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001409
1410 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001411 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001412 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001413 break;
1414 // We have a selector or a colon, continue parsing.
1415 }
1416 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001417 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001418 ConsumeToken(); // Eat the ','.
1419 /// Parse the expression after ','
1420 ExprResult Res = ParseAssignmentExpression();
1421 if (Res.isInvalid) {
1422 SkipUntil(tok::identifier);
1423 return Res;
1424 }
1425 // We have a valid expression.
1426 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001427 }
1428 } else if (!selIdent) {
1429 Diag(Tok, diag::err_expected_ident); // missing selector name.
1430 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001431 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001432 }
Chris Lattnerff384912007-10-07 02:00:24 +00001433
Chris Lattnerdf195262007-10-09 17:51:17 +00001434 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001435 Diag(Tok, diag::err_expected_rsquare);
1436 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001437 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001438 }
Chris Lattner699b6612008-01-25 18:59:06 +00001439 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001440
Steve Naroff29238a02007-10-05 18:42:47 +00001441 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001442 if (nKeys == 0)
1443 KeyIdents.push_back(selIdent);
1444 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1445
1446 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001447 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001448 return Actions.ActOnClassMessage(CurScope,
Chris Lattner699b6612008-01-25 18:59:06 +00001449 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001450 &KeyExprs[0], KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001451 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001452 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001453}
1454
Steve Naroffbeaf2992007-11-03 11:27:19 +00001455Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001456 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001457 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001458
1459 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1460 // expressions. At this point, we know that the only valid thing that starts
1461 // with '@' is an @"".
1462 llvm::SmallVector<SourceLocation, 4> AtLocs;
1463 llvm::SmallVector<ExprTy*, 4> AtStrings;
1464 AtLocs.push_back(AtLoc);
1465 AtStrings.push_back(Res.Val);
1466
1467 while (Tok.is(tok::at)) {
1468 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001469
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001470 ExprResult Res(true); // Invalid unless there is a string literal.
1471 if (isTokenStringLiteral())
1472 Res = ParseStringLiteralExpression();
1473 else
1474 Diag(Tok, diag::err_objc_concat_string);
1475
1476 if (Res.isInvalid) {
1477 while (!AtStrings.empty()) {
1478 Actions.DeleteExpr(AtStrings.back());
1479 AtStrings.pop_back();
1480 }
1481 return Res;
1482 }
1483
1484 AtStrings.push_back(Res.Val);
1485 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001486
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001487 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1488 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001489}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001490
1491/// objc-encode-expression:
1492/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001493Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001494 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001495
1496 SourceLocation EncLoc = ConsumeToken();
1497
Chris Lattnerdf195262007-10-09 17:51:17 +00001498 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001499 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1500 return true;
1501 }
1502
1503 SourceLocation LParenLoc = ConsumeParen();
1504
1505 TypeTy *Ty = ParseTypeName();
1506
Anders Carlsson4988ae32007-08-23 15:31:37 +00001507 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001508
Chris Lattner674af952007-10-16 22:51:17 +00001509 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001510 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001511}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001512
1513/// objc-protocol-expression
1514/// @protocol ( protocol-name )
1515
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001516Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001517{
1518 SourceLocation ProtoLoc = ConsumeToken();
1519
Chris Lattnerdf195262007-10-09 17:51:17 +00001520 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001521 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1522 return true;
1523 }
1524
1525 SourceLocation LParenLoc = ConsumeParen();
1526
Chris Lattnerdf195262007-10-09 17:51:17 +00001527 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001528 Diag(Tok, diag::err_expected_ident);
1529 return true;
1530 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001531 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001532 ConsumeToken();
1533
Anders Carlsson4988ae32007-08-23 15:31:37 +00001534 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001535
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001536 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1537 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001538}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001539
1540/// objc-selector-expression
1541/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001542Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001543{
1544 SourceLocation SelectorLoc = ConsumeToken();
1545
1546 if (Tok.isNot(tok::l_paren)) {
1547 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1548 return 0;
1549 }
1550
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001551 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001552 SourceLocation LParenLoc = ConsumeParen();
1553 SourceLocation sLoc;
1554 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1555 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001556 Diag(Tok, diag::err_expected_ident); // missing selector name.
1557 return 0;
1558 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001559 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001560 unsigned nColons = 0;
1561 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001562 while (1) {
1563 if (Tok.isNot(tok::colon)) {
1564 Diag(Tok, diag::err_expected_colon);
1565 break;
1566 }
Chris Lattnercb53b362007-12-27 19:57:00 +00001567 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001568 ConsumeToken(); // Eat the ':'.
1569 if (Tok.is(tok::r_paren))
1570 break;
1571 // Check for another keyword selector.
1572 SourceLocation Loc;
1573 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001574 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001575 if (!SelIdent && Tok.isNot(tok::colon))
1576 break;
1577 }
Steve Naroff887407e2007-12-05 22:21:29 +00001578 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001579 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001580 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001581 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001582 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001583 }