blob: 980105a02ce572c10ad4c895f3c3e1e8f206279b [file] [log] [blame]
Ted Kremenek42730c52008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Steve Narofffb367882007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff87c329f2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000037 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000038 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000039 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000040 case tok::objc_implementation:
Fariborz Jahanian83ddf822007-11-10 20:59:13 +000041 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000042 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000043 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000044 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000045 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000046 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000050 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000053 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Narofffb367882007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000069 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070 }
Chris Lattner4b009652007-07-25 00:24:17 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnera1d2bb72007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000082 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000083
Steve Naroff415c1832007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Steve Narofffb367882007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
116Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
124 return 0;
125 }
126 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
Chris Lattner45142b92008-07-26 04:07:02 +0000149
Steve Narofffb367882007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattner45142b92008-07-26 04:07:02 +0000151 SourceLocation EndProtoLoc;
152 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
153 if (Tok.is(tok::less) &&
154 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
155 return 0;
156
Steve Narofffb367882007-08-20 21:31:48 +0000157 if (attrList) // categories don't support attributes.
158 Diag(Tok, diag::err_objc_no_attributes_on_category);
159
Steve Naroff415c1832007-10-10 17:32:04 +0000160 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000161 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000162 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner45142b92008-07-26 04:07:02 +0000163 EndProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000164
165 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000166
Steve Naroff0bbffd82007-08-22 16:35:03 +0000167 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000168 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000169 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000170 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000171 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000172 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000173 return 0;
174 }
175 // Parse a class interface.
176 IdentifierInfo *superClassId = 0;
177 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000178
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000179 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000180 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000181 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000182 Diag(Tok, diag::err_expected_ident); // missing super class name.
183 return 0;
184 }
185 superClassId = Tok.getIdentifierInfo();
186 superClassLoc = ConsumeToken();
187 }
188 // Next, we need to check for any protocol references.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000189 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
190 SourceLocation EndProtoLoc;
191 if (Tok.is(tok::less) &&
192 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
193 return 0;
194
195 DeclTy *ClsType =
196 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
197 superClassId, superClassLoc,
198 &ProtocolRefs[0], ProtocolRefs.size(),
199 EndProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000200
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000201 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000202 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000203
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000204 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205
206 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000207 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000208 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000209 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000210 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000211 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000212 return 0;
213}
214
215/// objc-interface-decl-list:
216/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000217/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000218/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000219/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000220/// objc-interface-decl-list declaration
221/// objc-interface-decl-list ';'
222///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000223/// objc-method-requirement: [OBJC2]
224/// @required
225/// @optional
226///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000227void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000228 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000229 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000230 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000231 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000232 SourceLocation AtEndLoc;
233
Steve Naroff0bbffd82007-08-22 16:35:03 +0000234 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000235 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000236 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000237 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000238
239 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000240 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000241 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 } else if (ocKind == tok::objc_required) { // protocols only
243 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000244 MethodImplKind = ocKind;
245 if (contextKey != tok::objc_protocol)
246 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000247 } else if (ocKind == tok::objc_optional) { // protocols only
248 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000249 MethodImplKind = ocKind;
250 if (contextKey != tok::objc_protocol)
251 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000252 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000253 ObjCDeclSpec OCDS;
254 ConsumeToken(); // the "property" identifier
255 // Parse property attribute list, if any.
256 if (Tok.is(tok::l_paren)) {
257 // property has attribute list.
258 ParseObjCPropertyAttribute(OCDS);
259 }
260 // Parse all the comma separated declarators.
261 DeclSpec DS;
262 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
263 ParseStructDeclaration(DS, FieldDeclarators);
264
265 if (Tok.is(tok::semi))
266 ConsumeToken();
267 else {
268 Diag(Tok, diag::err_expected_semi_decl_list);
269 SkipUntil(tok::r_brace, true, true);
270 }
271 // Convert them all to property declarations.
272 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
273 FieldDeclarator &FD = FieldDeclarators[i];
274 // Install the property declarator into interfaceDecl.
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000275 Selector GetterSel =
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000276 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
277 ? OCDS.getGetterName()
278 : FD.D.getIdentifier());
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000279 Selector SetterSel =
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000280 PP.getSelectorTable().getNullarySelector(OCDS.getSetterName()
281 ? OCDS.getSetterName()
282 // FIXME. This is not right!
283 : FD.D.getIdentifier());
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000284 DeclTy *Property = Actions.ActOnProperty(CurScope,
Steve Naroff638d6a42008-05-22 23:24:08 +0000285 AtLoc, FD, OCDS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000286 GetterSel, SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000287 MethodImplKind);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000288 allProperties.push_back(Property);
289 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000290 continue;
291 } else {
292 Diag(Tok, diag::err_objc_illegal_interface_qual);
293 ConsumeToken();
294 }
295 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000296 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
297 DeclTy *methodPrototype =
298 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000299 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000300 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
301 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000302 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000303 continue;
304 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000305 else if (Tok.is(tok::at))
306 continue;
307
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000308 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000309 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000310 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000311 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000312 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000313 // FIXME: as the name implies, this rule allows function definitions.
314 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000315 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000316 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000317 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000318 /// Insert collected methods declarations into the @interface object.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000319 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
320 allMethods.empty() ? 0 : &allMethods[0],
321 allMethods.size(),
322 allProperties.empty() ? 0 : &allProperties[0],
323 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000324}
325
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000326/// Parse property attribute declarations.
327///
328/// property-attr-decl: '(' property-attrlist ')'
329/// property-attrlist:
330/// property-attribute
331/// property-attrlist ',' property-attribute
332/// property-attribute:
333/// getter '=' identifier
334/// setter '=' identifier ':'
335/// readonly
336/// readwrite
337/// assign
338/// retain
339/// copy
340/// nonatomic
341///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000342void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000343 SourceLocation loc = ConsumeParen(); // consume '('
344 while (isObjCPropertyAttribute()) {
345 const IdentifierInfo *II = Tok.getIdentifierInfo();
346 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000347 if (II == ObjCPropertyAttrs[objc_getter] ||
348 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000349 // skip getter/setter part.
350 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000351 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000352 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000353 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000354 if (II == ObjCPropertyAttrs[objc_setter]) {
355 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000356 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000357 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000358 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000359 Diag(loc, diag::err_expected_colon);
360 SkipUntil(tok::r_paren,true,true);
361 break;
362 }
363 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000364 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000365 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000366 DS.setGetterName(Tok.getIdentifierInfo());
367 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000368 }
369 else {
370 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000371 SkipUntil(tok::r_paren,true,true);
372 break;
373 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000374 }
375 else {
376 Diag(loc, diag::err_objc_expected_equal);
377 SkipUntil(tok::r_paren,true,true);
378 break;
379 }
380 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000381
Ted Kremenek42730c52008-01-07 19:49:32 +0000382 else if (II == ObjCPropertyAttrs[objc_readonly])
383 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
384 else if (II == ObjCPropertyAttrs[objc_assign])
385 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
386 else if (II == ObjCPropertyAttrs[objc_readwrite])
387 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
388 else if (II == ObjCPropertyAttrs[objc_retain])
389 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
390 else if (II == ObjCPropertyAttrs[objc_copy])
391 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
392 else if (II == ObjCPropertyAttrs[objc_nonatomic])
393 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000394
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000395 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000396 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000397 loc = ConsumeToken();
398 continue;
399 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000400 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000401 break;
402 Diag(loc, diag::err_expected_rparen);
403 SkipUntil(tok::semi);
404 return;
405 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000406 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000407 ConsumeParen();
408 else {
409 Diag(loc, diag::err_objc_expected_property_attr);
410 SkipUntil(tok::r_paren); // recover from error inside attribute list
411 }
412}
413
Steve Naroff81f1bba2007-09-06 21:24:23 +0000414/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000415/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000416/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000417///
418/// objc-instance-method: '-'
419/// objc-class-method: '+'
420///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000421/// objc-method-attributes: [OBJC2]
422/// __attribute__((deprecated))
423///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000424Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000425 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000426 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000427
428 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000429 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000430
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000431 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000432 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000433 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000434 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000435}
436
437/// objc-selector:
438/// identifier
439/// one of
440/// enum struct union if else while do for switch case default
441/// break continue return goto asm sizeof typeof __alignof
442/// unsigned long const short volatile signed restrict _Complex
443/// in out inout bycopy byref oneway int char float double void _Bool
444///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000445IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000446 switch (Tok.getKind()) {
447 default:
448 return 0;
449 case tok::identifier:
450 case tok::kw_typeof:
451 case tok::kw___alignof:
452 case tok::kw_auto:
453 case tok::kw_break:
454 case tok::kw_case:
455 case tok::kw_char:
456 case tok::kw_const:
457 case tok::kw_continue:
458 case tok::kw_default:
459 case tok::kw_do:
460 case tok::kw_double:
461 case tok::kw_else:
462 case tok::kw_enum:
463 case tok::kw_extern:
464 case tok::kw_float:
465 case tok::kw_for:
466 case tok::kw_goto:
467 case tok::kw_if:
468 case tok::kw_inline:
469 case tok::kw_int:
470 case tok::kw_long:
471 case tok::kw_register:
472 case tok::kw_restrict:
473 case tok::kw_return:
474 case tok::kw_short:
475 case tok::kw_signed:
476 case tok::kw_sizeof:
477 case tok::kw_static:
478 case tok::kw_struct:
479 case tok::kw_switch:
480 case tok::kw_typedef:
481 case tok::kw_union:
482 case tok::kw_unsigned:
483 case tok::kw_void:
484 case tok::kw_volatile:
485 case tok::kw_while:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000486 case tok::kw_bool:
Chris Lattnerd031a452007-10-07 02:00:24 +0000487 case tok::kw__Bool:
488 case tok::kw__Complex:
489 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000490 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000491 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000492 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000493}
494
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000495/// property-attrlist: one of
496/// readonly getter setter assign retain copy nonatomic
497///
498bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000499 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000500 const IdentifierInfo *II = Tok.getIdentifierInfo();
501 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000502 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000503 }
504 return false;
505}
506
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000507/// objc-for-collection-in: 'in'
508///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000509bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000510 // FIXME: May have to do additional look-ahead to only allow for
511 // valid tokens following an 'in'; such as an identifier, unary operators,
512 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000513 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
514 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000515}
516
Ted Kremenek42730c52008-01-07 19:49:32 +0000517/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000518/// qualifier list and builds their bitmask representation in the input
519/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000520///
521/// objc-type-qualifiers:
522/// objc-type-qualifier
523/// objc-type-qualifiers objc-type-qualifier
524///
Ted Kremenek42730c52008-01-07 19:49:32 +0000525void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000526 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000527 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000528 return;
529
530 const IdentifierInfo *II = Tok.getIdentifierInfo();
531 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000532 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000533 continue;
534
Ted Kremenek42730c52008-01-07 19:49:32 +0000535 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000536 switch (i) {
537 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000538 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
539 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
540 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
541 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
542 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
543 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000544 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000545 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000546 ConsumeToken();
547 II = 0;
548 break;
549 }
550
551 // If this wasn't a recognized qualifier, bail out.
552 if (II) return;
553 }
554}
555
556/// objc-type-name:
557/// '(' objc-type-qualifiers[opt] type-name ')'
558/// '(' objc-type-qualifiers[opt] ')'
559///
Ted Kremenek42730c52008-01-07 19:49:32 +0000560Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000561 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000562
563 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000564 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000565
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000566 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000567 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000568
Steve Naroff0bbffd82007-08-22 16:35:03 +0000569 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000570 Ty = ParseTypeName();
571 // FIXME: back when Sema support is in place...
572 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000573 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000574 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000575 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000576 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000577 }
578 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000579 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000580}
581
582/// objc-method-decl:
583/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000584/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000585/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000586/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000587///
588/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000589/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000590/// objc-keyword-selector objc-keyword-decl
591///
592/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000593/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
594/// objc-selector ':' objc-keyword-attributes[opt] identifier
595/// ':' objc-type-name objc-keyword-attributes[opt] identifier
596/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000597///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000598/// objc-parmlist:
599/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000600///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000601/// objc-parms:
602/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000603///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000604/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000605/// , ...
606///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000607/// objc-keyword-attributes: [OBJC2]
608/// __attribute__((unused))
609///
Steve Naroff3774dd92007-10-26 20:53:56 +0000610Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000611 tok::TokenKind mType,
612 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000613 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000614{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000615 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000616 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000618 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000619 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000620 SourceLocation selLoc;
621 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000622 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000623 if (!SelIdent) {
624 Diag(Tok, diag::err_expected_ident); // missing selector name.
625 // FIXME: this creates a unary selector with a null identifier, is this
626 // ok?? Maybe we should skip to the next semicolon or something.
627 }
628
629 // If attributes exist after the method, parse them.
630 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000631 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000632 MethodAttrs = ParseAttributes();
633
634 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000635 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000636 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000637 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000638 }
Steve Naroff304ed392007-09-05 23:30:30 +0000639
Steve Naroff4ed9d662007-09-27 14:38:14 +0000640 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
641 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000642 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000643 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000644
645 Action::TypeTy *TypeInfo;
646 while (1) {
647 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000648
Chris Lattnerd031a452007-10-07 02:00:24 +0000649 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000650 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000651 Diag(Tok, diag::err_expected_colon);
652 break;
653 }
654 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000655 ObjCDeclSpec DSType;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000656 if (Tok.is(tok::l_paren)) { // Parse the argument type.
657 TypeInfo = ParseObjCTypeName(DSType);
658 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000659 else
660 TypeInfo = 0;
661 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000662 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000663
Chris Lattnerd031a452007-10-07 02:00:24 +0000664 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000665 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000666 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000667
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000668 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000669 Diag(Tok, diag::err_expected_ident); // missing argument name.
670 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000671 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000672 ArgNames.push_back(Tok.getIdentifierInfo());
673 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000674
Chris Lattnerd031a452007-10-07 02:00:24 +0000675 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000676 SourceLocation Loc;
677 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000678 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000679 break;
680 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000681 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000682
Steve Naroff29fe7462007-11-15 12:35:21 +0000683 bool isVariadic = false;
684
Chris Lattnerd031a452007-10-07 02:00:24 +0000685 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000686 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000687 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000688 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000689 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000690 ConsumeToken();
691 break;
692 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000693 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000694 // Parse the c-style argument declaration-specifier.
695 DeclSpec DS;
696 ParseDeclarationSpecifiers(DS);
697 // Parse the declarator.
698 Declarator ParmDecl(DS, Declarator::PrototypeContext);
699 ParseDeclarator(ParmDecl);
700 }
701
702 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000703 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000704 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000705 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000706 MethodAttrs = ParseAttributes();
707
708 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
709 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000710 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000711 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000712 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000713 &ArgNames[0], MethodAttrs,
714 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000715}
716
Steve Narofffb367882007-08-20 21:31:48 +0000717/// objc-protocol-refs:
718/// '<' identifier-list '>'
719///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000720bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000721ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
722 bool WarnOnDeclarations, SourceLocation &EndLoc) {
723 assert(Tok.is(tok::less) && "expected <");
724
725 ConsumeToken(); // the "<"
726
727 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
728
729 while (1) {
730 if (Tok.isNot(tok::identifier)) {
731 Diag(Tok, diag::err_expected_ident);
732 SkipUntil(tok::greater);
733 return true;
734 }
735 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
736 Tok.getLocation()));
737 ConsumeToken();
738
739 if (Tok.isNot(tok::comma))
740 break;
741 ConsumeToken();
742 }
743
744 // Consume the '>'.
745 if (Tok.isNot(tok::greater)) {
746 Diag(Tok, diag::err_expected_greater);
747 return true;
748 }
749
750 EndLoc = ConsumeAnyToken();
751
752 // Convert the list of protocols identifiers into a list of protocol decls.
753 Actions.FindProtocolDeclaration(WarnOnDeclarations,
754 &ProtocolIdents[0], ProtocolIdents.size(),
755 Protocols);
756 return false;
757}
758
Steve Narofffb367882007-08-20 21:31:48 +0000759/// objc-class-instance-variables:
760/// '{' objc-instance-variable-decl-list[opt] '}'
761///
762/// objc-instance-variable-decl-list:
763/// objc-visibility-spec
764/// objc-instance-variable-decl ';'
765/// ';'
766/// objc-instance-variable-decl-list objc-visibility-spec
767/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
768/// objc-instance-variable-decl-list ';'
769///
770/// objc-visibility-spec:
771/// @private
772/// @protected
773/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000774/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000775///
776/// objc-instance-variable-decl:
777/// struct-declaration
778///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000779void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
780 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000781 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000782 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000783 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
784
Steve Naroffc4474992007-08-21 21:17:12 +0000785 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000786
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000787 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000788 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000789 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000790 // Each iteration of this loop reads one objc-instance-variable-decl.
791
792 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000793 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000794 Diag(Tok, diag::ext_extra_struct_semi);
795 ConsumeToken();
796 continue;
797 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000798
Steve Naroffc4474992007-08-21 21:17:12 +0000799 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000800 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000801 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000802 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000803 case tok::objc_private:
804 case tok::objc_public:
805 case tok::objc_protected:
806 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000807 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000808 ConsumeToken();
809 continue;
810 default:
811 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000812 continue;
813 }
814 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000815
816 // Parse all the comma separated declarators.
817 DeclSpec DS;
818 FieldDeclarators.clear();
819 ParseStructDeclaration(DS, FieldDeclarators);
820
821 // Convert them all to fields.
822 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
823 FieldDeclarator &FD = FieldDeclarators[i];
824 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000825 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000826 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000827 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000828 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000829 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000830
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000831 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000832 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000833 } else {
834 Diag(Tok, diag::err_expected_semi_decl_list);
835 // Skip to end of block or statement
836 SkipUntil(tok::r_brace, true, true);
837 }
838 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000839 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000840 // Call ActOnFields() even if we don't have any decls. This is useful
841 // for code rewriting tools that need to be aware of the empty list.
842 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
843 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000844 LBraceLoc, RBraceLoc);
Steve Naroffc4474992007-08-21 21:17:12 +0000845 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000846}
Steve Narofffb367882007-08-20 21:31:48 +0000847
848/// objc-protocol-declaration:
849/// objc-protocol-definition
850/// objc-protocol-forward-reference
851///
852/// objc-protocol-definition:
853/// @protocol identifier
854/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000855/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000856/// @end
857///
858/// objc-protocol-forward-reference:
859/// @protocol identifier-list ';'
860///
861/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000862/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000863/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000864Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000865 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000866 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
867 ConsumeToken(); // the "protocol" identifier
868
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000869 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000870 Diag(Tok, diag::err_expected_ident); // missing protocol name.
871 return 0;
872 }
873 // Save the protocol name, then consume it.
874 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
875 SourceLocation nameLoc = ConsumeToken();
876
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000877 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000878 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000879 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000880 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000881 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000882
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000883 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000884 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
885 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
886
Steve Naroff72f17fb2007-08-22 22:17:26 +0000887 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000888 while (1) {
889 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000890 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000891 Diag(Tok, diag::err_expected_ident);
892 SkipUntil(tok::semi);
893 return 0;
894 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000895 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
896 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000897 ConsumeToken(); // the identifier
898
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000899 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000900 break;
901 }
902 // Consume the ';'.
903 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
904 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000905
Steve Naroff415c1832007-10-10 17:32:04 +0000906 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000907 &ProtocolRefs[0],
908 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000909 }
910
Steve Naroff72f17fb2007-08-22 22:17:26 +0000911 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000912 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000913
Chris Lattner2bdedd62008-07-26 04:03:38 +0000914 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000915 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000916 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000917 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000918
Chris Lattner2bdedd62008-07-26 04:03:38 +0000919 DeclTy *ProtoType =
920 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
921 &ProtocolRefs[0], ProtocolRefs.size(),
922 EndProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000923 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000924
925 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000926 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000927 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000928 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000929 }
930 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000931 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000932}
Steve Narofffb367882007-08-20 21:31:48 +0000933
934/// objc-implementation:
935/// objc-class-implementation-prologue
936/// objc-category-implementation-prologue
937///
938/// objc-class-implementation-prologue:
939/// @implementation identifier objc-superclass[opt]
940/// objc-class-instance-variables[opt]
941///
942/// objc-category-implementation-prologue:
943/// @implementation identifier ( identifier )
944
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000945Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
946 SourceLocation atLoc) {
947 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
948 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
949 ConsumeToken(); // the "implementation" identifier
950
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000951 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000952 Diag(Tok, diag::err_expected_ident); // missing class or category name.
953 return 0;
954 }
955 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000956 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000957 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
958
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000959 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000960 // we have a category implementation.
961 SourceLocation lparenLoc = ConsumeParen();
962 SourceLocation categoryLoc, rparenLoc;
963 IdentifierInfo *categoryId = 0;
964
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000965 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000966 categoryId = Tok.getIdentifierInfo();
967 categoryLoc = ConsumeToken();
968 } else {
969 Diag(Tok, diag::err_expected_ident); // missing category name.
970 return 0;
971 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000972 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000973 Diag(Tok, diag::err_expected_rparen);
974 SkipUntil(tok::r_paren, false); // don't stop at ';'
975 return 0;
976 }
977 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000978 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000979 atLoc, nameId, nameLoc, categoryId,
980 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000981 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000982 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000983 }
984 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000985 SourceLocation superClassLoc;
986 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000987 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000988 // We have a super class
989 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000990 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000991 Diag(Tok, diag::err_expected_ident); // missing super class name.
992 return 0;
993 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000994 superClassId = Tok.getIdentifierInfo();
995 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000996 }
Steve Naroff415c1832007-10-10 17:32:04 +0000997 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +0000998 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000999 superClassId, superClassLoc);
1000
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001001 if (Tok.is(tok::l_brace)) // we have ivars
1002 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001003 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001004
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001005 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001006}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001007
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001008Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1009 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1010 "ParseObjCAtEndDeclaration(): Expected @end");
1011 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001012 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001013 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001014 else
1015 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001016 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001017}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001018
1019/// compatibility-alias-decl:
1020/// @compatibility_alias alias-name class-name ';'
1021///
1022Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1023 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1024 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1025 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001026 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001027 Diag(Tok, diag::err_expected_ident);
1028 return 0;
1029 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001030 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1031 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001032 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001033 Diag(Tok, diag::err_expected_ident);
1034 return 0;
1035 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001036 IdentifierInfo *classId = Tok.getIdentifierInfo();
1037 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1038 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001039 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001040 return 0;
1041 }
1042 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1043 aliasId, aliasLoc,
1044 classId, classLoc);
1045 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001046}
1047
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001048/// property-synthesis:
1049/// @synthesize property-ivar-list ';'
1050///
1051/// property-ivar-list:
1052/// property-ivar
1053/// property-ivar-list ',' property-ivar
1054///
1055/// property-ivar:
1056/// identifier
1057/// identifier '=' identifier
1058///
1059Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1060 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1061 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001062 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001063 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001064 Diag(Tok, diag::err_expected_ident);
1065 return 0;
1066 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001067 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001068 IdentifierInfo *propertyIvar = 0;
1069 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1070 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001071 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001072 // property '=' ivar-name
1073 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001075 Diag(Tok, diag::err_expected_ident);
1076 break;
1077 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001078 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001079 ConsumeToken(); // consume ivar-name
1080 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001081 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1082 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001083 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001084 break;
1085 ConsumeToken(); // consume ','
1086 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001087 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001088 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1089 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001090}
1091
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001092/// property-dynamic:
1093/// @dynamic property-list
1094///
1095/// property-list:
1096/// identifier
1097/// property-list ',' identifier
1098///
1099Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1100 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1101 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1102 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001103 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001104 Diag(Tok, diag::err_expected_ident);
1105 return 0;
1106 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001107 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001108 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1109 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1110 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1111 propertyId, 0);
1112
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001113 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001114 break;
1115 ConsumeToken(); // consume ','
1116 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001117 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001118 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1119 return 0;
1120}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001121
1122/// objc-throw-statement:
1123/// throw expression[opt];
1124///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001125Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1126 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001127 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001128 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001129 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001130 if (Res.isInvalid) {
1131 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001132 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001133 }
1134 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001135 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001136 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001137}
1138
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001139/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001140/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001141///
1142Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001143 ConsumeToken(); // consume synchronized
1144 if (Tok.isNot(tok::l_paren)) {
1145 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1146 return true;
1147 }
1148 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001149 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001150 if (Res.isInvalid) {
1151 SkipUntil(tok::semi);
1152 return true;
1153 }
1154 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001155 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001156 return true;
1157 }
1158 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001159 if (Tok.isNot(tok::l_brace)) {
1160 Diag (Tok, diag::err_expected_lbrace);
1161 return true;
1162 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001163 // Enter a scope to hold everything within the compound stmt. Compound
1164 // statements can always hold declarations.
1165 EnterScope(Scope::DeclScope);
1166
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001167 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001168
1169 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001170 if (SynchBody.isInvalid)
1171 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1172 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001173}
1174
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001175/// objc-try-catch-statement:
1176/// @try compound-statement objc-catch-list[opt]
1177/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1178///
1179/// objc-catch-list:
1180/// @catch ( parameter-declaration ) compound-statement
1181/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1182/// catch-parameter-declaration:
1183/// parameter-declaration
1184/// '...' [OBJC2]
1185///
Chris Lattner80712392008-03-10 06:06:04 +00001186Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001187 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001188
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001189 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001190 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001191 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001192 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001193 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001194 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001195 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001196 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001197 if (TryBody.isInvalid)
1198 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001199
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001200 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001201 // At this point, we need to lookahead to determine if this @ is the start
1202 // of an @catch or @finally. We don't want to consume the @ token if this
1203 // is an @try or @encode or something else.
1204 Token AfterAt = GetLookAheadToken(1);
1205 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1206 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1207 break;
1208
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001209 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001210 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001211 StmtTy *FirstPart = 0;
1212 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001213 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001214 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001215 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001216 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001217 DeclSpec DS;
1218 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001219 // For some odd reason, the name of the exception variable is
1220 // optional. As a result, we need to use PrototypeContext.
1221 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001222 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001223 if (DeclaratorInfo.getIdentifier()) {
1224 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Chris Lattnera4ff4272008-03-13 06:29:04 +00001225 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001226 StmtResult stmtResult =
1227 Actions.ActOnDeclStmt(aBlockVarDecl,
1228 DS.getSourceRange().getBegin(),
1229 DeclaratorInfo.getSourceRange().getEnd());
1230 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1231 }
Steve Naroffc949a462008-02-05 21:27:35 +00001232 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001233 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001234 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001235
1236 StmtResult CatchBody(true);
1237 if (Tok.is(tok::l_brace))
1238 CatchBody = ParseCompoundStatementBody();
1239 else
1240 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001241 if (CatchBody.isInvalid)
1242 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001243 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001244 FirstPart, CatchBody.Val, CatchStmts.Val);
1245 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001246 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001247 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1248 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001249 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001250 }
1251 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001252 } else {
1253 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001254 ConsumeToken(); // consume finally
Chris Lattner8027be62008-02-14 19:27:54 +00001255
1256 StmtResult FinallyBody(true);
1257 if (Tok.is(tok::l_brace))
1258 FinallyBody = ParseCompoundStatementBody();
1259 else
1260 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001261 if (FinallyBody.isInvalid)
1262 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001263 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001264 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001265 catch_or_finally_seen = true;
1266 break;
1267 }
1268 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001269 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001270 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001271 return true;
1272 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001273 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001274 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001275}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001276
Steve Naroff81f1bba2007-09-06 21:24:23 +00001277/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001278///
Steve Naroff18c83382007-11-13 23:01:27 +00001279Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001280 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001281 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001282 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001283 ConsumeToken();
1284
Steve Naroff9191a9e82007-11-11 19:54:21 +00001285 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001286 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001287 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001288
1289 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1290 SkipUntil(tok::l_brace, true, true);
1291
1292 // If we didn't find the '{', bail out.
1293 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001294 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001295 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001296 SourceLocation BraceLoc = Tok.getLocation();
1297
1298 // Enter a scope for the method body.
1299 EnterScope(Scope::FnScope|Scope::DeclScope);
1300
1301 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001302 // specified Declarator for the method.
1303 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001304
1305 StmtResult FnBody = ParseCompoundStatementBody();
1306
1307 // If the function body could not be parsed, make a bogus compoundstmt.
1308 if (FnBody.isInvalid)
1309 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1310
1311 // Leave the function body scope.
1312 ExitScope();
1313
1314 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001315 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001316 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001317}
Anders Carlssona66cad42007-08-21 17:43:55 +00001318
Steve Naroffc949a462008-02-05 21:27:35 +00001319Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1320 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001321 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001322 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1323 return ParseObjCThrowStmt(AtLoc);
1324 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1325 return ParseObjCSynchronizedStmt(AtLoc);
1326 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1327 if (Res.isInvalid) {
1328 // If the expression is invalid, skip ahead to the next semicolon. Not
1329 // doing this opens us up to the possibility of infinite loops if
1330 // ParseExpression does not consume any tokens.
1331 SkipUntil(tok::semi);
1332 return true;
1333 }
1334 // Otherwise, eat the semicolon.
1335 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1336 return Actions.ActOnExprStmt(Res.Val);
1337}
1338
Steve Narofffb9dd752007-10-15 20:55:58 +00001339Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001340
1341 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001342 case tok::string_literal: // primary-expression: string-literal
1343 case tok::wide_string_literal:
1344 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1345 default:
1346 break;
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001347 }
1348
1349 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001350 case tok::objc_encode:
1351 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1352 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001353 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001354 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001355 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001356 default:
1357 Diag(AtLoc, diag::err_unexpected_at);
1358 SkipUntil(tok::semi);
Chris Lattnerfd44db32008-01-30 21:20:25 +00001359 return true;
Anders Carlssona66cad42007-08-21 17:43:55 +00001360 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001361}
1362
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001363/// objc-message-expr:
1364/// '[' objc-receiver objc-message-args ']'
1365///
1366/// objc-receiver:
1367/// expression
1368/// class-name
1369/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001370Parser::ExprResult Parser::ParseObjCMessageExpression() {
1371 assert(Tok.is(tok::l_square) && "'[' expected");
1372 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1373
1374 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001375 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001376 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1377 ConsumeToken();
1378 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1379 }
1380
1381 ExprResult Res = ParseAssignmentExpression();
1382 if (Res.isInvalid) {
1383 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattnere69015d2008-01-25 19:43:26 +00001384 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001385 return Res;
1386 }
1387 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1388}
1389
1390/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1391/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001392///
1393/// objc-message-args:
1394/// objc-selector
1395/// objc-keywordarg-list
1396///
1397/// objc-keywordarg-list:
1398/// objc-keywordarg
1399/// objc-keywordarg-list objc-keywordarg
1400///
1401/// objc-keywordarg:
1402/// selector-name[opt] ':' objc-keywordexpr
1403///
1404/// objc-keywordexpr:
1405/// nonempty-expr-list
1406///
1407/// nonempty-expr-list:
1408/// assignment-expression
1409/// nonempty-expr-list , assignment-expression
1410///
Chris Lattnered27a532008-01-25 18:59:06 +00001411Parser::ExprResult
1412Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1413 IdentifierInfo *ReceiverName,
1414 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001415 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001416 SourceLocation Loc;
1417 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001418
1419 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1420 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1421
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001422 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001423 while (1) {
1424 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001425 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001426
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001427 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001428 Diag(Tok, diag::err_expected_colon);
1429 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001430 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001431 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001432 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001433 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001434 ExprResult Res = ParseAssignmentExpression();
1435 if (Res.isInvalid) {
1436 SkipUntil(tok::identifier);
1437 return Res;
1438 }
1439 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001440 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001441
1442 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001443 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001444 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001445 break;
1446 // We have a selector or a colon, continue parsing.
1447 }
1448 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001449 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001450 ConsumeToken(); // Eat the ','.
1451 /// Parse the expression after ','
1452 ExprResult Res = ParseAssignmentExpression();
1453 if (Res.isInvalid) {
1454 SkipUntil(tok::identifier);
1455 return Res;
1456 }
1457 // We have a valid expression.
1458 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001459 }
1460 } else if (!selIdent) {
1461 Diag(Tok, diag::err_expected_ident); // missing selector name.
1462 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001463 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001464 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001465
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001466 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001467 Diag(Tok, diag::err_expected_rsquare);
1468 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001469 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001470 }
Chris Lattnered27a532008-01-25 18:59:06 +00001471 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001472
Steve Narofff9e80db2007-10-05 18:42:47 +00001473 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001474 if (nKeys == 0)
1475 KeyIdents.push_back(selIdent);
1476 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1477
1478 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001479 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001480 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001481 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001482 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001483 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001484 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001485}
1486
Steve Naroff0add5d22007-11-03 11:27:19 +00001487Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001488 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001489 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001490
1491 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1492 // expressions. At this point, we know that the only valid thing that starts
1493 // with '@' is an @"".
1494 llvm::SmallVector<SourceLocation, 4> AtLocs;
1495 llvm::SmallVector<ExprTy*, 4> AtStrings;
1496 AtLocs.push_back(AtLoc);
1497 AtStrings.push_back(Res.Val);
1498
1499 while (Tok.is(tok::at)) {
1500 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001501
Chris Lattnerddd3e632007-12-12 01:04:12 +00001502 ExprResult Res(true); // Invalid unless there is a string literal.
1503 if (isTokenStringLiteral())
1504 Res = ParseStringLiteralExpression();
1505 else
1506 Diag(Tok, diag::err_objc_concat_string);
1507
1508 if (Res.isInvalid) {
1509 while (!AtStrings.empty()) {
1510 Actions.DeleteExpr(AtStrings.back());
1511 AtStrings.pop_back();
1512 }
1513 return Res;
1514 }
1515
1516 AtStrings.push_back(Res.Val);
1517 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001518
Chris Lattnerddd3e632007-12-12 01:04:12 +00001519 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1520 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001521}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001522
1523/// objc-encode-expression:
1524/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001525Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001526 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001527
1528 SourceLocation EncLoc = ConsumeToken();
1529
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001530 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001531 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1532 return true;
1533 }
1534
1535 SourceLocation LParenLoc = ConsumeParen();
1536
1537 TypeTy *Ty = ParseTypeName();
1538
Anders Carlsson92faeb82007-08-23 15:31:37 +00001539 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001540
Chris Lattnercfd61c82007-10-16 22:51:17 +00001541 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001542 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001543}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001544
1545/// objc-protocol-expression
1546/// @protocol ( protocol-name )
1547
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001548Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001549{
1550 SourceLocation ProtoLoc = ConsumeToken();
1551
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001552 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001553 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1554 return true;
1555 }
1556
1557 SourceLocation LParenLoc = ConsumeParen();
1558
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001559 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001560 Diag(Tok, diag::err_expected_ident);
1561 return true;
1562 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001563 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001564 ConsumeToken();
1565
Anders Carlsson92faeb82007-08-23 15:31:37 +00001566 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001567
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001568 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1569 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001570}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001571
1572/// objc-selector-expression
1573/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001574Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001575{
1576 SourceLocation SelectorLoc = ConsumeToken();
1577
1578 if (Tok.isNot(tok::l_paren)) {
1579 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1580 return 0;
1581 }
1582
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001583 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001584 SourceLocation LParenLoc = ConsumeParen();
1585 SourceLocation sLoc;
1586 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1587 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001588 Diag(Tok, diag::err_expected_ident); // missing selector name.
1589 return 0;
1590 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001591 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001592 unsigned nColons = 0;
1593 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001594 while (1) {
1595 if (Tok.isNot(tok::colon)) {
1596 Diag(Tok, diag::err_expected_colon);
1597 break;
1598 }
Chris Lattner847f5c12007-12-27 19:57:00 +00001599 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001600 ConsumeToken(); // Eat the ':'.
1601 if (Tok.is(tok::r_paren))
1602 break;
1603 // Check for another keyword selector.
1604 SourceLocation Loc;
1605 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001606 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001607 if (!SelIdent && Tok.isNot(tok::colon))
1608 break;
1609 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001610 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001611 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001612 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001613 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001614 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001615 }