blob: 5758460412111df4abb54a106eb0748e2f887914 [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 Lattner818350c2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
53 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:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000450 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000451 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000452 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000453 case tok::kw_break:
454 case tok::kw_case:
455 case tok::kw_catch:
456 case tok::kw_char:
457 case tok::kw_class:
458 case tok::kw_const:
459 case tok::kw_const_cast:
460 case tok::kw_continue:
461 case tok::kw_default:
462 case tok::kw_delete:
463 case tok::kw_do:
464 case tok::kw_double:
465 case tok::kw_dynamic_cast:
466 case tok::kw_else:
467 case tok::kw_enum:
468 case tok::kw_explicit:
469 case tok::kw_export:
470 case tok::kw_extern:
471 case tok::kw_false:
472 case tok::kw_float:
473 case tok::kw_for:
474 case tok::kw_friend:
475 case tok::kw_goto:
476 case tok::kw_if:
477 case tok::kw_inline:
478 case tok::kw_int:
479 case tok::kw_long:
480 case tok::kw_mutable:
481 case tok::kw_namespace:
482 case tok::kw_new:
483 case tok::kw_operator:
484 case tok::kw_private:
485 case tok::kw_protected:
486 case tok::kw_public:
487 case tok::kw_register:
488 case tok::kw_reinterpret_cast:
489 case tok::kw_restrict:
490 case tok::kw_return:
491 case tok::kw_short:
492 case tok::kw_signed:
493 case tok::kw_sizeof:
494 case tok::kw_static:
495 case tok::kw_static_cast:
496 case tok::kw_struct:
497 case tok::kw_switch:
498 case tok::kw_template:
499 case tok::kw_this:
500 case tok::kw_throw:
501 case tok::kw_true:
502 case tok::kw_try:
503 case tok::kw_typedef:
504 case tok::kw_typeid:
505 case tok::kw_typename:
506 case tok::kw_typeof:
507 case tok::kw_union:
508 case tok::kw_unsigned:
509 case tok::kw_using:
510 case tok::kw_virtual:
511 case tok::kw_void:
512 case tok::kw_volatile:
513 case tok::kw_wchar_t:
514 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000515 case tok::kw__Bool:
516 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000517 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000518 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000519 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000520 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000521 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000522}
523
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000524/// property-attrlist: one of
525/// readonly getter setter assign retain copy nonatomic
526///
527bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000528 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000529 const IdentifierInfo *II = Tok.getIdentifierInfo();
530 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000531 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000532 }
533 return false;
534}
535
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000536/// objc-for-collection-in: 'in'
537///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000538bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000539 // FIXME: May have to do additional look-ahead to only allow for
540 // valid tokens following an 'in'; such as an identifier, unary operators,
541 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000542 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000543 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000544}
545
Ted Kremenek42730c52008-01-07 19:49:32 +0000546/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000547/// qualifier list and builds their bitmask representation in the input
548/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000549///
550/// objc-type-qualifiers:
551/// objc-type-qualifier
552/// objc-type-qualifiers objc-type-qualifier
553///
Ted Kremenek42730c52008-01-07 19:49:32 +0000554void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000555 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000556 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000557 return;
558
559 const IdentifierInfo *II = Tok.getIdentifierInfo();
560 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000562 continue;
563
Ted Kremenek42730c52008-01-07 19:49:32 +0000564 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000565 switch (i) {
566 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000567 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
568 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
569 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
570 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
571 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
572 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000573 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000574 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000575 ConsumeToken();
576 II = 0;
577 break;
578 }
579
580 // If this wasn't a recognized qualifier, bail out.
581 if (II) return;
582 }
583}
584
585/// objc-type-name:
586/// '(' objc-type-qualifiers[opt] type-name ')'
587/// '(' objc-type-qualifiers[opt] ')'
588///
Ted Kremenek42730c52008-01-07 19:49:32 +0000589Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000590 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000591
592 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnerb5769332008-08-23 01:48:03 +0000593 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner265c8172007-09-27 15:15:46 +0000594 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000595
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000596 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000598
Steve Naroff0bbffd82007-08-22 16:35:03 +0000599 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000600 Ty = ParseTypeName();
601 // FIXME: back when Sema support is in place...
602 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000603 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000604
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000605 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000606 // If we didn't eat any tokens, then this isn't a type.
607 if (Tok.getLocation() == TypeStartLoc) {
608 Diag(Tok.getLocation(), diag::err_expected_type);
609 SkipUntil(tok::r_brace);
610 } else {
611 // Otherwise, we found *something*, but didn't get a ')' in the right
612 // place. Emit an error then return what we have as the type.
613 MatchRHSPunctuation(tok::r_paren, LParenLoc);
614 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000615 }
616 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000617 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000618}
619
620/// objc-method-decl:
621/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000622/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000623/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000624/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000625///
626/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000627/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000628/// objc-keyword-selector objc-keyword-decl
629///
630/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000631/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
632/// objc-selector ':' objc-keyword-attributes[opt] identifier
633/// ':' objc-type-name objc-keyword-attributes[opt] identifier
634/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000635///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000636/// objc-parmlist:
637/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000638///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000639/// objc-parms:
640/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000641///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000642/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000643/// , ...
644///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000645/// objc-keyword-attributes: [OBJC2]
646/// __attribute__((unused))
647///
Steve Naroff3774dd92007-10-26 20:53:56 +0000648Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000649 tok::TokenKind mType,
650 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000651 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000652{
Chris Lattnerb5769332008-08-23 01:48:03 +0000653 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000654 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000655 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000656 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000657 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000658
Steve Naroff3774dd92007-10-26 20:53:56 +0000659 SourceLocation selLoc;
660 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000661
662 if (!SelIdent) { // missing selector name.
663 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
664 SourceRange(mLoc, Tok.getLocation()));
665 // Skip until we get a ; or {}.
666 SkipUntil(tok::r_brace);
667 return 0;
668 }
669
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000670 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000671 // If attributes exist after the method, parse them.
672 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000673 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000674 MethodAttrs = ParseAttributes();
675
676 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000677 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000678 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000679 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000680 }
Steve Naroff304ed392007-09-05 23:30:30 +0000681
Steve Naroff4ed9d662007-09-27 14:38:14 +0000682 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
683 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000684 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000685 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000686
687 Action::TypeTy *TypeInfo;
688 while (1) {
689 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000690
Chris Lattnerd031a452007-10-07 02:00:24 +0000691 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000692 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000693 Diag(Tok, diag::err_expected_colon);
694 break;
695 }
696 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000697 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000698 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000699 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000700 else
701 TypeInfo = 0;
702 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000703 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000704
Chris Lattnerd031a452007-10-07 02:00:24 +0000705 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000706 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000707 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000708
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000709 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000710 Diag(Tok, diag::err_expected_ident); // missing argument name.
711 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000712 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000713 ArgNames.push_back(Tok.getIdentifierInfo());
714 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000715
Chris Lattnerd031a452007-10-07 02:00:24 +0000716 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000717 SourceLocation Loc;
718 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000719 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000720 break;
721 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000722 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000723
Steve Naroff29fe7462007-11-15 12:35:21 +0000724 bool isVariadic = false;
725
Chris Lattnerd031a452007-10-07 02:00:24 +0000726 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000727 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000728 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000729 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000730 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000731 ConsumeToken();
732 break;
733 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000734 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000735 // Parse the c-style argument declaration-specifier.
736 DeclSpec DS;
737 ParseDeclarationSpecifiers(DS);
738 // Parse the declarator.
739 Declarator ParmDecl(DS, Declarator::PrototypeContext);
740 ParseDeclarator(ParmDecl);
741 }
742
743 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000744 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000745 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000746 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000747 MethodAttrs = ParseAttributes();
748
749 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
750 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000751 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000752 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000753 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000754 &ArgNames[0], MethodAttrs,
755 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000756}
757
Steve Narofffb367882007-08-20 21:31:48 +0000758/// objc-protocol-refs:
759/// '<' identifier-list '>'
760///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000761bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000762ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
763 bool WarnOnDeclarations, SourceLocation &EndLoc) {
764 assert(Tok.is(tok::less) && "expected <");
765
766 ConsumeToken(); // the "<"
767
768 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
769
770 while (1) {
771 if (Tok.isNot(tok::identifier)) {
772 Diag(Tok, diag::err_expected_ident);
773 SkipUntil(tok::greater);
774 return true;
775 }
776 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
777 Tok.getLocation()));
778 ConsumeToken();
779
780 if (Tok.isNot(tok::comma))
781 break;
782 ConsumeToken();
783 }
784
785 // Consume the '>'.
786 if (Tok.isNot(tok::greater)) {
787 Diag(Tok, diag::err_expected_greater);
788 return true;
789 }
790
791 EndLoc = ConsumeAnyToken();
792
793 // Convert the list of protocols identifiers into a list of protocol decls.
794 Actions.FindProtocolDeclaration(WarnOnDeclarations,
795 &ProtocolIdents[0], ProtocolIdents.size(),
796 Protocols);
797 return false;
798}
799
Steve Narofffb367882007-08-20 21:31:48 +0000800/// objc-class-instance-variables:
801/// '{' objc-instance-variable-decl-list[opt] '}'
802///
803/// objc-instance-variable-decl-list:
804/// objc-visibility-spec
805/// objc-instance-variable-decl ';'
806/// ';'
807/// objc-instance-variable-decl-list objc-visibility-spec
808/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
809/// objc-instance-variable-decl-list ';'
810///
811/// objc-visibility-spec:
812/// @private
813/// @protected
814/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000815/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000816///
817/// objc-instance-variable-decl:
818/// struct-declaration
819///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000820void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
821 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000822 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000823 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000824 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
825
Steve Naroffc4474992007-08-21 21:17:12 +0000826 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000827
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000828 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000829 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000830 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000831 // Each iteration of this loop reads one objc-instance-variable-decl.
832
833 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000834 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000835 Diag(Tok, diag::ext_extra_struct_semi);
836 ConsumeToken();
837 continue;
838 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000839
Steve Naroffc4474992007-08-21 21:17:12 +0000840 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000841 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000842 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000843 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000844 case tok::objc_private:
845 case tok::objc_public:
846 case tok::objc_protected:
847 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000848 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000849 ConsumeToken();
850 continue;
851 default:
852 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000853 continue;
854 }
855 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000856
857 // Parse all the comma separated declarators.
858 DeclSpec DS;
859 FieldDeclarators.clear();
860 ParseStructDeclaration(DS, FieldDeclarators);
861
862 // Convert them all to fields.
863 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
864 FieldDeclarator &FD = FieldDeclarators[i];
865 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000866 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000867 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000868 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000869 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000870 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000871
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000872 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000873 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000874 } else {
875 Diag(Tok, diag::err_expected_semi_decl_list);
876 // Skip to end of block or statement
877 SkipUntil(tok::r_brace, true, true);
878 }
879 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000880 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000881 // Call ActOnFields() even if we don't have any decls. This is useful
882 // for code rewriting tools that need to be aware of the empty list.
883 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
884 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000885 LBraceLoc, RBraceLoc);
Steve Naroffc4474992007-08-21 21:17:12 +0000886 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000887}
Steve Narofffb367882007-08-20 21:31:48 +0000888
889/// objc-protocol-declaration:
890/// objc-protocol-definition
891/// objc-protocol-forward-reference
892///
893/// objc-protocol-definition:
894/// @protocol identifier
895/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000896/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000897/// @end
898///
899/// objc-protocol-forward-reference:
900/// @protocol identifier-list ';'
901///
902/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000903/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000904/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000905Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000906 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000907 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
908 ConsumeToken(); // the "protocol" identifier
909
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000910 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000911 Diag(Tok, diag::err_expected_ident); // missing protocol name.
912 return 0;
913 }
914 // Save the protocol name, then consume it.
915 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
916 SourceLocation nameLoc = ConsumeToken();
917
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000918 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000919 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000920 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000921 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000922 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000923
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000924 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000925 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
926 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
927
Steve Naroff72f17fb2007-08-22 22:17:26 +0000928 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000929 while (1) {
930 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000931 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000932 Diag(Tok, diag::err_expected_ident);
933 SkipUntil(tok::semi);
934 return 0;
935 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000936 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
937 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000938 ConsumeToken(); // the identifier
939
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000940 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000941 break;
942 }
943 // Consume the ';'.
944 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
945 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000946
Steve Naroff415c1832007-10-10 17:32:04 +0000947 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000948 &ProtocolRefs[0],
949 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000950 }
951
Steve Naroff72f17fb2007-08-22 22:17:26 +0000952 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000953 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000954
Chris Lattner2bdedd62008-07-26 04:03:38 +0000955 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000956 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000957 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000958 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000959
Chris Lattner2bdedd62008-07-26 04:03:38 +0000960 DeclTy *ProtoType =
961 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
962 &ProtocolRefs[0], ProtocolRefs.size(),
963 EndProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000964 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000965
966 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000967 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000968 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000969 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000970 }
971 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000972 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000973}
Steve Narofffb367882007-08-20 21:31:48 +0000974
975/// objc-implementation:
976/// objc-class-implementation-prologue
977/// objc-category-implementation-prologue
978///
979/// objc-class-implementation-prologue:
980/// @implementation identifier objc-superclass[opt]
981/// objc-class-instance-variables[opt]
982///
983/// objc-category-implementation-prologue:
984/// @implementation identifier ( identifier )
985
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000986Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
987 SourceLocation atLoc) {
988 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
989 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
990 ConsumeToken(); // the "implementation" identifier
991
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000992 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000993 Diag(Tok, diag::err_expected_ident); // missing class or category name.
994 return 0;
995 }
996 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000997 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000998 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
999
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001000 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001001 // we have a category implementation.
1002 SourceLocation lparenLoc = ConsumeParen();
1003 SourceLocation categoryLoc, rparenLoc;
1004 IdentifierInfo *categoryId = 0;
1005
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001006 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001007 categoryId = Tok.getIdentifierInfo();
1008 categoryLoc = ConsumeToken();
1009 } else {
1010 Diag(Tok, diag::err_expected_ident); // missing category name.
1011 return 0;
1012 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001013 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001014 Diag(Tok, diag::err_expected_rparen);
1015 SkipUntil(tok::r_paren, false); // don't stop at ';'
1016 return 0;
1017 }
1018 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001019 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001020 atLoc, nameId, nameLoc, categoryId,
1021 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001022 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001023 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001024 }
1025 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001026 SourceLocation superClassLoc;
1027 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001028 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001029 // We have a super class
1030 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001031 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001032 Diag(Tok, diag::err_expected_ident); // missing super class name.
1033 return 0;
1034 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001035 superClassId = Tok.getIdentifierInfo();
1036 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001037 }
Steve Naroff415c1832007-10-10 17:32:04 +00001038 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001039 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001040 superClassId, superClassLoc);
1041
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001042 if (Tok.is(tok::l_brace)) // we have ivars
1043 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001044 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001045
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001046 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001047}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001048
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001049Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1050 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1051 "ParseObjCAtEndDeclaration(): Expected @end");
1052 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001053 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001054 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001055 else
1056 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001057 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001058}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001059
1060/// compatibility-alias-decl:
1061/// @compatibility_alias alias-name class-name ';'
1062///
1063Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1064 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1065 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1066 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001067 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001068 Diag(Tok, diag::err_expected_ident);
1069 return 0;
1070 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001071 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1072 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001073 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001074 Diag(Tok, diag::err_expected_ident);
1075 return 0;
1076 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001077 IdentifierInfo *classId = Tok.getIdentifierInfo();
1078 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1079 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001080 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001081 return 0;
1082 }
1083 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1084 aliasId, aliasLoc,
1085 classId, classLoc);
1086 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001087}
1088
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001089/// property-synthesis:
1090/// @synthesize property-ivar-list ';'
1091///
1092/// property-ivar-list:
1093/// property-ivar
1094/// property-ivar-list ',' property-ivar
1095///
1096/// property-ivar:
1097/// identifier
1098/// identifier '=' identifier
1099///
1100Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1101 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1102 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001103 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001104 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001105 Diag(Tok, diag::err_expected_ident);
1106 return 0;
1107 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001108 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001109 IdentifierInfo *propertyIvar = 0;
1110 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1111 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001112 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001113 // property '=' ivar-name
1114 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001115 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001116 Diag(Tok, diag::err_expected_ident);
1117 break;
1118 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001119 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001120 ConsumeToken(); // consume ivar-name
1121 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001122 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1123 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001124 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001125 break;
1126 ConsumeToken(); // consume ','
1127 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001128 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001129 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1130 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001131}
1132
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001133/// property-dynamic:
1134/// @dynamic property-list
1135///
1136/// property-list:
1137/// identifier
1138/// property-list ',' identifier
1139///
1140Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1141 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1142 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1143 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001144 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001145 Diag(Tok, diag::err_expected_ident);
1146 return 0;
1147 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001148 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001149 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1150 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1151 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1152 propertyId, 0);
1153
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001154 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001155 break;
1156 ConsumeToken(); // consume ','
1157 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001158 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001159 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1160 return 0;
1161}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001162
1163/// objc-throw-statement:
1164/// throw expression[opt];
1165///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001166Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1167 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001168 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001169 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001170 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001171 if (Res.isInvalid) {
1172 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001173 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001174 }
1175 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001176 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001177 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001178}
1179
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001180/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001181/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001182///
1183Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001184 ConsumeToken(); // consume synchronized
1185 if (Tok.isNot(tok::l_paren)) {
1186 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1187 return true;
1188 }
1189 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001190 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001191 if (Res.isInvalid) {
1192 SkipUntil(tok::semi);
1193 return true;
1194 }
1195 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001196 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001197 return true;
1198 }
1199 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001200 if (Tok.isNot(tok::l_brace)) {
1201 Diag (Tok, diag::err_expected_lbrace);
1202 return true;
1203 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001204 // Enter a scope to hold everything within the compound stmt. Compound
1205 // statements can always hold declarations.
1206 EnterScope(Scope::DeclScope);
1207
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001208 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001209
1210 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001211 if (SynchBody.isInvalid)
1212 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1213 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001214}
1215
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001216/// objc-try-catch-statement:
1217/// @try compound-statement objc-catch-list[opt]
1218/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1219///
1220/// objc-catch-list:
1221/// @catch ( parameter-declaration ) compound-statement
1222/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1223/// catch-parameter-declaration:
1224/// parameter-declaration
1225/// '...' [OBJC2]
1226///
Chris Lattner80712392008-03-10 06:06:04 +00001227Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001228 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001229
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001230 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001231 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001232 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001233 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001234 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001235 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001236 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001237 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001238 if (TryBody.isInvalid)
1239 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001240
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001241 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001242 // At this point, we need to lookahead to determine if this @ is the start
1243 // of an @catch or @finally. We don't want to consume the @ token if this
1244 // is an @try or @encode or something else.
1245 Token AfterAt = GetLookAheadToken(1);
1246 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1247 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1248 break;
1249
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001250 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001251 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001252 StmtTy *FirstPart = 0;
1253 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001254 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001255 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001256 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001257 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001258 DeclSpec DS;
1259 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001260 // For some odd reason, the name of the exception variable is
1261 // optional. As a result, we need to use PrototypeContext.
1262 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001263 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001264 if (DeclaratorInfo.getIdentifier()) {
1265 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001266 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001267 StmtResult stmtResult =
1268 Actions.ActOnDeclStmt(aBlockVarDecl,
1269 DS.getSourceRange().getBegin(),
1270 DeclaratorInfo.getSourceRange().getEnd());
1271 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1272 }
Steve Naroffc949a462008-02-05 21:27:35 +00001273 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001274 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001275 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001276
1277 StmtResult CatchBody(true);
1278 if (Tok.is(tok::l_brace))
1279 CatchBody = ParseCompoundStatementBody();
1280 else
1281 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001282 if (CatchBody.isInvalid)
1283 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001284 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001285 FirstPart, CatchBody.Val, CatchStmts.Val);
1286 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001287 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001288 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1289 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001290 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001291 }
1292 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001293 } else {
1294 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001295 ConsumeToken(); // consume finally
Chris Lattner8027be62008-02-14 19:27:54 +00001296
1297 StmtResult FinallyBody(true);
1298 if (Tok.is(tok::l_brace))
1299 FinallyBody = ParseCompoundStatementBody();
1300 else
1301 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001302 if (FinallyBody.isInvalid)
1303 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001304 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001305 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001306 catch_or_finally_seen = true;
1307 break;
1308 }
1309 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001310 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001311 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001312 return true;
1313 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001314 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001315 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001316}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001317
Steve Naroff81f1bba2007-09-06 21:24:23 +00001318/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001319///
Steve Naroff18c83382007-11-13 23:01:27 +00001320Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001321 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001322 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001323 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001324 ConsumeToken();
1325
Steve Naroff9191a9e82007-11-11 19:54:21 +00001326 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001327 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001328 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001329
1330 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1331 SkipUntil(tok::l_brace, true, true);
1332
1333 // If we didn't find the '{', bail out.
1334 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001335 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001336 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001337 SourceLocation BraceLoc = Tok.getLocation();
1338
1339 // Enter a scope for the method body.
1340 EnterScope(Scope::FnScope|Scope::DeclScope);
1341
1342 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001343 // specified Declarator for the method.
1344 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001345
1346 StmtResult FnBody = ParseCompoundStatementBody();
1347
1348 // If the function body could not be parsed, make a bogus compoundstmt.
1349 if (FnBody.isInvalid)
1350 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1351
1352 // Leave the function body scope.
1353 ExitScope();
1354
1355 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001356 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001357 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001358}
Anders Carlssona66cad42007-08-21 17:43:55 +00001359
Steve Naroffc949a462008-02-05 21:27:35 +00001360Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1361 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001362 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001363 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1364 return ParseObjCThrowStmt(AtLoc);
1365 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1366 return ParseObjCSynchronizedStmt(AtLoc);
1367 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1368 if (Res.isInvalid) {
1369 // If the expression is invalid, skip ahead to the next semicolon. Not
1370 // doing this opens us up to the possibility of infinite loops if
1371 // ParseExpression does not consume any tokens.
1372 SkipUntil(tok::semi);
1373 return true;
1374 }
1375 // Otherwise, eat the semicolon.
1376 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1377 return Actions.ActOnExprStmt(Res.Val);
1378}
1379
Steve Narofffb9dd752007-10-15 20:55:58 +00001380Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001381 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001382 case tok::string_literal: // primary-expression: string-literal
1383 case tok::wide_string_literal:
1384 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1385 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001386 if (Tok.getIdentifierInfo() == 0)
1387 return Diag(AtLoc, diag::err_unexpected_at);
1388
1389 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1390 case tok::objc_encode:
1391 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1392 case tok::objc_protocol:
1393 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1394 case tok::objc_selector:
1395 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1396 default:
1397 return Diag(AtLoc, diag::err_unexpected_at);
1398 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001399 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001400}
1401
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001402/// objc-message-expr:
1403/// '[' objc-receiver objc-message-args ']'
1404///
1405/// objc-receiver:
1406/// expression
1407/// class-name
1408/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001409Parser::ExprResult Parser::ParseObjCMessageExpression() {
1410 assert(Tok.is(tok::l_square) && "'[' expected");
1411 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1412
1413 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001414 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001415 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1416 ConsumeToken();
1417 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1418 }
1419
1420 ExprResult Res = ParseAssignmentExpression();
1421 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001422 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001423 return Res;
1424 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001425
Chris Lattnered27a532008-01-25 18:59:06 +00001426 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1427}
1428
1429/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1430/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001431///
1432/// objc-message-args:
1433/// objc-selector
1434/// objc-keywordarg-list
1435///
1436/// objc-keywordarg-list:
1437/// objc-keywordarg
1438/// objc-keywordarg-list objc-keywordarg
1439///
1440/// objc-keywordarg:
1441/// selector-name[opt] ':' objc-keywordexpr
1442///
1443/// objc-keywordexpr:
1444/// nonempty-expr-list
1445///
1446/// nonempty-expr-list:
1447/// assignment-expression
1448/// nonempty-expr-list , assignment-expression
1449///
Chris Lattnered27a532008-01-25 18:59:06 +00001450Parser::ExprResult
1451Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1452 IdentifierInfo *ReceiverName,
1453 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001454 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001455 SourceLocation Loc;
1456 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001457
1458 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1459 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1460
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001461 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001462 while (1) {
1463 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001464 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001465
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001466 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001467 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001468 // We must manually skip to a ']', otherwise the expression skipper will
1469 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1470 // the enclosing expression.
1471 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001472 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001473 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001474
Steve Naroff4ed9d662007-09-27 14:38:14 +00001475 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001476 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001477 ExprResult Res = ParseAssignmentExpression();
1478 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001479 // We must manually skip to a ']', otherwise the expression skipper will
1480 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1481 // the enclosing expression.
1482 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001483 return Res;
1484 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001485
Steve Naroff253118b2007-09-17 20:25:27 +00001486 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001487 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001488
1489 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001490 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001491 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001492 break;
1493 // We have a selector or a colon, continue parsing.
1494 }
1495 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001496 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001497 ConsumeToken(); // Eat the ','.
1498 /// Parse the expression after ','
1499 ExprResult Res = ParseAssignmentExpression();
1500 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001501 // We must manually skip to a ']', otherwise the expression skipper will
1502 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1503 // the enclosing expression.
1504 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001505 return Res;
1506 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001507
Steve Naroff9f176d12007-11-15 13:05:42 +00001508 // We have a valid expression.
1509 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001510 }
1511 } else if (!selIdent) {
1512 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001513
1514 // We must manually skip to a ']', otherwise the expression skipper will
1515 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1516 // the enclosing expression.
1517 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001518 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001519 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001520
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001521 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001522 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001523 // We must manually skip to a ']', otherwise the expression skipper will
1524 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1525 // the enclosing expression.
1526 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001527 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001528 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001529
Chris Lattnered27a532008-01-25 18:59:06 +00001530 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001531
Steve Narofff9e80db2007-10-05 18:42:47 +00001532 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001533 if (nKeys == 0)
1534 KeyIdents.push_back(selIdent);
1535 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1536
1537 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001538 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001539 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001540 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001541 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001542 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001543 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001544}
1545
Steve Naroff0add5d22007-11-03 11:27:19 +00001546Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001547 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001548 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001549
1550 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1551 // expressions. At this point, we know that the only valid thing that starts
1552 // with '@' is an @"".
1553 llvm::SmallVector<SourceLocation, 4> AtLocs;
1554 llvm::SmallVector<ExprTy*, 4> AtStrings;
1555 AtLocs.push_back(AtLoc);
1556 AtStrings.push_back(Res.Val);
1557
1558 while (Tok.is(tok::at)) {
1559 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001560
Chris Lattnerddd3e632007-12-12 01:04:12 +00001561 ExprResult Res(true); // Invalid unless there is a string literal.
1562 if (isTokenStringLiteral())
1563 Res = ParseStringLiteralExpression();
1564 else
1565 Diag(Tok, diag::err_objc_concat_string);
1566
1567 if (Res.isInvalid) {
1568 while (!AtStrings.empty()) {
1569 Actions.DeleteExpr(AtStrings.back());
1570 AtStrings.pop_back();
1571 }
1572 return Res;
1573 }
1574
1575 AtStrings.push_back(Res.Val);
1576 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001577
Chris Lattnerddd3e632007-12-12 01:04:12 +00001578 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1579 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001580}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001581
1582/// objc-encode-expression:
1583/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001584Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001585 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001586
1587 SourceLocation EncLoc = ConsumeToken();
1588
Chris Lattnerf9311a92008-08-05 06:19:09 +00001589 if (Tok.isNot(tok::l_paren))
1590 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001591
1592 SourceLocation LParenLoc = ConsumeParen();
1593
1594 TypeTy *Ty = ParseTypeName();
1595
Anders Carlsson92faeb82007-08-23 15:31:37 +00001596 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001597
Chris Lattnercfd61c82007-10-16 22:51:17 +00001598 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001599 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001600}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001601
1602/// objc-protocol-expression
1603/// @protocol ( protocol-name )
1604
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001605Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001606{
1607 SourceLocation ProtoLoc = ConsumeToken();
1608
Chris Lattnerf9311a92008-08-05 06:19:09 +00001609 if (Tok.isNot(tok::l_paren))
1610 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001611
1612 SourceLocation LParenLoc = ConsumeParen();
1613
Chris Lattnerf9311a92008-08-05 06:19:09 +00001614 if (Tok.isNot(tok::identifier))
1615 return Diag(Tok, diag::err_expected_ident);
1616
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001617 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001618 ConsumeToken();
1619
Anders Carlsson92faeb82007-08-23 15:31:37 +00001620 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001621
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001622 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1623 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001624}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001625
1626/// objc-selector-expression
1627/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001628Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001629{
1630 SourceLocation SelectorLoc = ConsumeToken();
1631
Chris Lattnerf9311a92008-08-05 06:19:09 +00001632 if (Tok.isNot(tok::l_paren))
1633 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001634
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001635 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001636 SourceLocation LParenLoc = ConsumeParen();
1637 SourceLocation sLoc;
1638 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001639 if (!SelIdent && Tok.isNot(tok::colon))
1640 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1641
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001642 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001643 unsigned nColons = 0;
1644 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001645 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001646 if (Tok.isNot(tok::colon))
1647 return Diag(Tok, diag::err_expected_colon);
1648
Chris Lattner847f5c12007-12-27 19:57:00 +00001649 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001650 ConsumeToken(); // Eat the ':'.
1651 if (Tok.is(tok::r_paren))
1652 break;
1653 // Check for another keyword selector.
1654 SourceLocation Loc;
1655 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001656 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001657 if (!SelIdent && Tok.isNot(tok::colon))
1658 break;
1659 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001660 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001661 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001662 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001663 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001664 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001665 }