blob: ec40e364795d6b26112e0f0bca9157aedc3abacc [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;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000134 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000135
Steve Naroffa7f62782007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000143 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Steve Naroffef20ed32007-10-30 02:23:23 +0000150 SourceLocation endProtoLoc;
Steve Narofffb367882007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000152 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000153 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000154 return 0;
155 }
156 if (attrList) // categories don't support attributes.
157 Diag(Tok, diag::err_objc_no_attributes_on_category);
158
Steve Naroff415c1832007-10-10 17:32:04 +0000159 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000160 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000161 &ProtocolRefs[0], ProtocolRefs.size(),
162 endProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000163
164 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000165
Steve Naroff0bbffd82007-08-22 16:35:03 +0000166 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000167 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000168 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000169 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000170 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000171 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000172 return 0;
173 }
174 // Parse a class interface.
175 IdentifierInfo *superClassId = 0;
176 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000177
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000178 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000179 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing super class name.
182 return 0;
183 }
184 superClassId = Tok.getIdentifierInfo();
185 superClassLoc = ConsumeToken();
186 }
187 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000188 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000189 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000190 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000191 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000192 return 0;
193 }
Steve Naroff415c1832007-10-10 17:32:04 +0000194 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Chris Lattner847f5c12007-12-27 19:57:00 +0000195 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000196 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000197 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000198
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000199 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000200 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000201
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000202 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000203
204 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000205 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000206 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000207 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000208 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000209 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000210 return 0;
211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000225void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000226 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000227 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000228 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000229 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000230 SourceLocation AtEndLoc;
231
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000233 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000234 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000235 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000236
237 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000238 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000239 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000240 } else if (ocKind == tok::objc_required) { // protocols only
241 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000242 MethodImplKind = ocKind;
243 if (contextKey != tok::objc_protocol)
244 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000245 } else if (ocKind == tok::objc_optional) { // protocols only
246 ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +0000247 MethodImplKind = ocKind;
248 if (contextKey != tok::objc_protocol)
249 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000250 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000251 ObjCDeclSpec OCDS;
252 ConsumeToken(); // the "property" identifier
253 // Parse property attribute list, if any.
254 if (Tok.is(tok::l_paren)) {
255 // property has attribute list.
256 ParseObjCPropertyAttribute(OCDS);
257 }
258 // Parse all the comma separated declarators.
259 DeclSpec DS;
260 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
261 ParseStructDeclaration(DS, FieldDeclarators);
262
263 if (Tok.is(tok::semi))
264 ConsumeToken();
265 else {
266 Diag(Tok, diag::err_expected_semi_decl_list);
267 SkipUntil(tok::r_brace, true, true);
268 }
269 // Convert them all to property declarations.
270 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
271 FieldDeclarator &FD = FieldDeclarators[i];
272 // Install the property declarator into interfaceDecl.
273 DeclTy *Property = Actions.ActOnProperty(CurScope,
274 DS.getSourceRange().getBegin(), FD, OCDS);
275 allProperties.push_back(Property);
276 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000277 continue;
278 } else {
279 Diag(Tok, diag::err_objc_illegal_interface_qual);
280 ConsumeToken();
281 }
282 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000283 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
284 DeclTy *methodPrototype =
285 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000286 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000287 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
288 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000289 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000290 continue;
291 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000292 else if (Tok.is(tok::at))
293 continue;
294
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000295 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000296 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000297 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000298 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000299 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000300 // FIXME: as the name implies, this rule allows function definitions.
301 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000302 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000303 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000304 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000305 /// Insert collected methods declarations into the @interface object.
Steve Naroffb82c50f2007-11-11 17:19:15 +0000306 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
307 &allProperties[0], allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000308}
309
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000310/// Parse property attribute declarations.
311///
312/// property-attr-decl: '(' property-attrlist ')'
313/// property-attrlist:
314/// property-attribute
315/// property-attrlist ',' property-attribute
316/// property-attribute:
317/// getter '=' identifier
318/// setter '=' identifier ':'
319/// readonly
320/// readwrite
321/// assign
322/// retain
323/// copy
324/// nonatomic
325///
Ted Kremenek42730c52008-01-07 19:49:32 +0000326void Parser::ParseObjCPropertyAttribute (ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000327 SourceLocation loc = ConsumeParen(); // consume '('
328 while (isObjCPropertyAttribute()) {
329 const IdentifierInfo *II = Tok.getIdentifierInfo();
330 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000331 if (II == ObjCPropertyAttrs[objc_getter] ||
332 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000333 // skip getter/setter part.
334 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000335 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000336 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000337 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000338 if (II == ObjCPropertyAttrs[objc_setter]) {
339 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000340 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000341 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000342 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000343 Diag(loc, diag::err_expected_colon);
344 SkipUntil(tok::r_paren,true,true);
345 break;
346 }
347 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000348 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000349 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000350 DS.setGetterName(Tok.getIdentifierInfo());
351 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000352 }
353 else {
354 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000355 SkipUntil(tok::r_paren,true,true);
356 break;
357 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000358 }
359 else {
360 Diag(loc, diag::err_objc_expected_equal);
361 SkipUntil(tok::r_paren,true,true);
362 break;
363 }
364 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000365
Ted Kremenek42730c52008-01-07 19:49:32 +0000366 else if (II == ObjCPropertyAttrs[objc_readonly])
367 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
368 else if (II == ObjCPropertyAttrs[objc_assign])
369 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
370 else if (II == ObjCPropertyAttrs[objc_readwrite])
371 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
372 else if (II == ObjCPropertyAttrs[objc_retain])
373 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
374 else if (II == ObjCPropertyAttrs[objc_copy])
375 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
376 else if (II == ObjCPropertyAttrs[objc_nonatomic])
377 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000378
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000379 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000380 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000381 loc = ConsumeToken();
382 continue;
383 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000384 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000385 break;
386 Diag(loc, diag::err_expected_rparen);
387 SkipUntil(tok::semi);
388 return;
389 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000390 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000391 ConsumeParen();
392 else {
393 Diag(loc, diag::err_objc_expected_property_attr);
394 SkipUntil(tok::r_paren); // recover from error inside attribute list
395 }
396}
397
Steve Naroff81f1bba2007-09-06 21:24:23 +0000398/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000399/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000400/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000401///
402/// objc-instance-method: '-'
403/// objc-class-method: '+'
404///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000405/// objc-method-attributes: [OBJC2]
406/// __attribute__((deprecated))
407///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000408Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000409 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000410 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000411
412 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000413 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000414
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000415 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000416 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000417 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000418 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000419}
420
421/// objc-selector:
422/// identifier
423/// one of
424/// enum struct union if else while do for switch case default
425/// break continue return goto asm sizeof typeof __alignof
426/// unsigned long const short volatile signed restrict _Complex
427/// in out inout bycopy byref oneway int char float double void _Bool
428///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000429IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000430 switch (Tok.getKind()) {
431 default:
432 return 0;
433 case tok::identifier:
434 case tok::kw_typeof:
435 case tok::kw___alignof:
436 case tok::kw_auto:
437 case tok::kw_break:
438 case tok::kw_case:
439 case tok::kw_char:
440 case tok::kw_const:
441 case tok::kw_continue:
442 case tok::kw_default:
443 case tok::kw_do:
444 case tok::kw_double:
445 case tok::kw_else:
446 case tok::kw_enum:
447 case tok::kw_extern:
448 case tok::kw_float:
449 case tok::kw_for:
450 case tok::kw_goto:
451 case tok::kw_if:
452 case tok::kw_inline:
453 case tok::kw_int:
454 case tok::kw_long:
455 case tok::kw_register:
456 case tok::kw_restrict:
457 case tok::kw_return:
458 case tok::kw_short:
459 case tok::kw_signed:
460 case tok::kw_sizeof:
461 case tok::kw_static:
462 case tok::kw_struct:
463 case tok::kw_switch:
464 case tok::kw_typedef:
465 case tok::kw_union:
466 case tok::kw_unsigned:
467 case tok::kw_void:
468 case tok::kw_volatile:
469 case tok::kw_while:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000470 case tok::kw_bool:
Chris Lattnerd031a452007-10-07 02:00:24 +0000471 case tok::kw__Bool:
472 case tok::kw__Complex:
473 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000474 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000475 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000476 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000477}
478
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000479/// property-attrlist: one of
480/// readonly getter setter assign retain copy nonatomic
481///
482bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000483 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000484 const IdentifierInfo *II = Tok.getIdentifierInfo();
485 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000486 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000487 }
488 return false;
489}
490
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000491/// objc-for-collection-in: 'in'
492///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000493bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000494 // FIXME: May have to do additional look-ahead to only allow for
495 // valid tokens following an 'in'; such as an identifier, unary operators,
496 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000497 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
498 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000499}
500
Ted Kremenek42730c52008-01-07 19:49:32 +0000501/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000502/// qualifier list and builds their bitmask representation in the input
503/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000504///
505/// objc-type-qualifiers:
506/// objc-type-qualifier
507/// objc-type-qualifiers objc-type-qualifier
508///
Ted Kremenek42730c52008-01-07 19:49:32 +0000509void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000510 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000511 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000512 return;
513
514 const IdentifierInfo *II = Tok.getIdentifierInfo();
515 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000516 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000517 continue;
518
Ted Kremenek42730c52008-01-07 19:49:32 +0000519 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000520 switch (i) {
521 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000522 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
523 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
524 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
525 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
526 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
527 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000528 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000529 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000530 ConsumeToken();
531 II = 0;
532 break;
533 }
534
535 // If this wasn't a recognized qualifier, bail out.
536 if (II) return;
537 }
538}
539
540/// objc-type-name:
541/// '(' objc-type-qualifiers[opt] type-name ')'
542/// '(' objc-type-qualifiers[opt] ')'
543///
Ted Kremenek42730c52008-01-07 19:49:32 +0000544Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000545 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000546
547 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000548 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000549
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000550 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000551 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000552
Steve Naroff0bbffd82007-08-22 16:35:03 +0000553 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000554 Ty = ParseTypeName();
555 // FIXME: back when Sema support is in place...
556 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000557 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000558 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000559 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000560 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000561 }
562 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000563 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000564}
565
566/// objc-method-decl:
567/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000568/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000569/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000570/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000571///
572/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000573/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000574/// objc-keyword-selector objc-keyword-decl
575///
576/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000577/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
578/// objc-selector ':' objc-keyword-attributes[opt] identifier
579/// ':' objc-type-name objc-keyword-attributes[opt] identifier
580/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000581///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000582/// objc-parmlist:
583/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000584///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000585/// objc-parms:
586/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000587///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000588/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000589/// , ...
590///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000591/// objc-keyword-attributes: [OBJC2]
592/// __attribute__((unused))
593///
Steve Naroff3774dd92007-10-26 20:53:56 +0000594Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000595 tok::TokenKind mType,
596 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000597 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000598{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000599 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000600 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000601 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000602 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000603 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000604 SourceLocation selLoc;
605 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000606 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000607 if (!SelIdent) {
608 Diag(Tok, diag::err_expected_ident); // missing selector name.
609 // FIXME: this creates a unary selector with a null identifier, is this
610 // ok?? Maybe we should skip to the next semicolon or something.
611 }
612
613 // If attributes exist after the method, parse them.
614 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000615 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000616 MethodAttrs = ParseAttributes();
617
618 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000619 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000620 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000621 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000622 }
Steve Naroff304ed392007-09-05 23:30:30 +0000623
Steve Naroff4ed9d662007-09-27 14:38:14 +0000624 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
625 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000626 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000627 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000628
629 Action::TypeTy *TypeInfo;
630 while (1) {
631 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000632
Chris Lattnerd031a452007-10-07 02:00:24 +0000633 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000634 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000635 Diag(Tok, diag::err_expected_colon);
636 break;
637 }
638 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 ObjCDeclSpec DSType;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000640 if (Tok.is(tok::l_paren)) { // Parse the argument type.
641 TypeInfo = ParseObjCTypeName(DSType);
642 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000643 else
644 TypeInfo = 0;
645 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000646 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000647
Chris Lattnerd031a452007-10-07 02:00:24 +0000648 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000649 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000650 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000651
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000652 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000653 Diag(Tok, diag::err_expected_ident); // missing argument name.
654 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000655 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000656 ArgNames.push_back(Tok.getIdentifierInfo());
657 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000658
Chris Lattnerd031a452007-10-07 02:00:24 +0000659 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000660 SourceLocation Loc;
661 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000662 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000663 break;
664 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000665 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000666
Steve Naroff29fe7462007-11-15 12:35:21 +0000667 bool isVariadic = false;
668
Chris Lattnerd031a452007-10-07 02:00:24 +0000669 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000670 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000671 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000672 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000673 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000674 ConsumeToken();
675 break;
676 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000677 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000678 // Parse the c-style argument declaration-specifier.
679 DeclSpec DS;
680 ParseDeclarationSpecifiers(DS);
681 // Parse the declarator.
682 Declarator ParmDecl(DS, Declarator::PrototypeContext);
683 ParseDeclarator(ParmDecl);
684 }
685
686 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000687 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000688 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000689 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000690 MethodAttrs = ParseAttributes();
691
692 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
693 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000694 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000695 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000696 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000697 &ArgNames[0], MethodAttrs,
698 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000699}
700
Steve Narofffb367882007-08-20 21:31:48 +0000701/// objc-protocol-refs:
702/// '<' identifier-list '>'
703///
Steve Naroff304ed392007-09-05 23:30:30 +0000704bool Parser::ParseObjCProtocolReferences(
Chris Lattnere1352302008-04-07 04:56:42 +0000705 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000706 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000707
708 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000709
710 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000711 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000712 Diag(Tok, diag::err_expected_ident);
713 SkipUntil(tok::greater);
714 return true;
715 }
716 ProtocolRefs.push_back(Tok.getIdentifierInfo());
717 ConsumeToken();
718
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000719 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000720 break;
721 ConsumeToken();
722 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000723
Steve Narofffb367882007-08-20 21:31:48 +0000724 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000725 if (Tok.is(tok::greater)) {
726 endLoc = ConsumeAnyToken();
727 return false;
728 }
729 Diag(Tok, diag::err_expected_greater);
730 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000731}
732
733/// objc-class-instance-variables:
734/// '{' objc-instance-variable-decl-list[opt] '}'
735///
736/// objc-instance-variable-decl-list:
737/// objc-visibility-spec
738/// objc-instance-variable-decl ';'
739/// ';'
740/// objc-instance-variable-decl-list objc-visibility-spec
741/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
742/// objc-instance-variable-decl-list ';'
743///
744/// objc-visibility-spec:
745/// @private
746/// @protected
747/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000748/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000749///
750/// objc-instance-variable-decl:
751/// struct-declaration
752///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000753void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
754 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000755 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000756 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000757 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
758
Steve Naroffc4474992007-08-21 21:17:12 +0000759 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000760
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000761 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000762 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000763 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000764 // Each iteration of this loop reads one objc-instance-variable-decl.
765
766 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000767 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000768 Diag(Tok, diag::ext_extra_struct_semi);
769 ConsumeToken();
770 continue;
771 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000772
Steve Naroffc4474992007-08-21 21:17:12 +0000773 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000774 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000775 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000776 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000777 case tok::objc_private:
778 case tok::objc_public:
779 case tok::objc_protected:
780 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000781 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000782 ConsumeToken();
783 continue;
784 default:
785 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000786 continue;
787 }
788 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000789
790 // Parse all the comma separated declarators.
791 DeclSpec DS;
792 FieldDeclarators.clear();
793 ParseStructDeclaration(DS, FieldDeclarators);
794
795 // Convert them all to fields.
796 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
797 FieldDeclarator &FD = FieldDeclarators[i];
798 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000799 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000800 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000801 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000802 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000803 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000804
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000805 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000806 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000807 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000808 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
809 break;
810 } else {
811 Diag(Tok, diag::err_expected_semi_decl_list);
812 // Skip to end of block or statement
813 SkipUntil(tok::r_brace, true, true);
814 }
815 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000816 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000817 // Call ActOnFields() even if we don't have any decls. This is useful
818 // for code rewriting tools that need to be aware of the empty list.
819 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
820 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000821 LBraceLoc, RBraceLoc);
Steve Naroffc4474992007-08-21 21:17:12 +0000822 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000823}
Steve Narofffb367882007-08-20 21:31:48 +0000824
825/// objc-protocol-declaration:
826/// objc-protocol-definition
827/// objc-protocol-forward-reference
828///
829/// objc-protocol-definition:
830/// @protocol identifier
831/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000832/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000833/// @end
834///
835/// objc-protocol-forward-reference:
836/// @protocol identifier-list ';'
837///
838/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000839/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000840/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000841Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000842 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000843 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
844 ConsumeToken(); // the "protocol" identifier
845
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000846 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000847 Diag(Tok, diag::err_expected_ident); // missing protocol name.
848 return 0;
849 }
850 // Save the protocol name, then consume it.
851 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
852 SourceLocation nameLoc = ConsumeToken();
853
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000854 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000855 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000856 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000857 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000858 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000859 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000860 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000861 ProtocolRefs.push_back(protocolName);
862
863 while (1) {
864 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000865 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000866 Diag(Tok, diag::err_expected_ident);
867 SkipUntil(tok::semi);
868 return 0;
869 }
870 ProtocolRefs.push_back(Tok.getIdentifierInfo());
871 ConsumeToken(); // the identifier
872
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000873 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000874 break;
875 }
876 // Consume the ';'.
877 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
878 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000879 }
Chris Lattner7afba9c2008-03-16 20:19:15 +0000880 if (!ProtocolRefs.empty())
Steve Naroff415c1832007-10-10 17:32:04 +0000881 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000882 &ProtocolRefs[0],
883 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000884 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000885 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000886 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000887 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000888 return 0;
889 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000890
Steve Naroff415c1832007-10-10 17:32:04 +0000891 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000892 protocolName, nameLoc,
893 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000894 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000895 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000896
897 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000898 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000899 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000900 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000901 }
902 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000903 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000904}
Steve Narofffb367882007-08-20 21:31:48 +0000905
906/// objc-implementation:
907/// objc-class-implementation-prologue
908/// objc-category-implementation-prologue
909///
910/// objc-class-implementation-prologue:
911/// @implementation identifier objc-superclass[opt]
912/// objc-class-instance-variables[opt]
913///
914/// objc-category-implementation-prologue:
915/// @implementation identifier ( identifier )
916
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000917Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
918 SourceLocation atLoc) {
919 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
920 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
921 ConsumeToken(); // the "implementation" identifier
922
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000923 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000924 Diag(Tok, diag::err_expected_ident); // missing class or category name.
925 return 0;
926 }
927 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000928 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000929 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
930
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000931 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000932 // we have a category implementation.
933 SourceLocation lparenLoc = ConsumeParen();
934 SourceLocation categoryLoc, rparenLoc;
935 IdentifierInfo *categoryId = 0;
936
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000937 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000938 categoryId = Tok.getIdentifierInfo();
939 categoryLoc = ConsumeToken();
940 } else {
941 Diag(Tok, diag::err_expected_ident); // missing category name.
942 return 0;
943 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000944 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000945 Diag(Tok, diag::err_expected_rparen);
946 SkipUntil(tok::r_paren, false); // don't stop at ';'
947 return 0;
948 }
949 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000950 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000951 atLoc, nameId, nameLoc, categoryId,
952 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000953 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000954 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000955 }
956 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000957 SourceLocation superClassLoc;
958 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000959 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000960 // We have a super class
961 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000963 Diag(Tok, diag::err_expected_ident); // missing super class name.
964 return 0;
965 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000966 superClassId = Tok.getIdentifierInfo();
967 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000968 }
Steve Naroff415c1832007-10-10 17:32:04 +0000969 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +0000970 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000971 superClassId, superClassLoc);
972
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000973 if (Tok.is(tok::l_brace)) // we have ivars
974 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000975 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000976
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000977 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000978}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000979
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000980Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
981 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
982 "ParseObjCAtEndDeclaration(): Expected @end");
983 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +0000984 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +0000985 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +0000986 else
987 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +0000988 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +0000989}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000990
991/// compatibility-alias-decl:
992/// @compatibility_alias alias-name class-name ';'
993///
994Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
995 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
996 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
997 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000998 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000999 Diag(Tok, diag::err_expected_ident);
1000 return 0;
1001 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001002 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1003 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001004 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001005 Diag(Tok, diag::err_expected_ident);
1006 return 0;
1007 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001008 IdentifierInfo *classId = Tok.getIdentifierInfo();
1009 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1010 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001011 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001012 return 0;
1013 }
1014 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1015 aliasId, aliasLoc,
1016 classId, classLoc);
1017 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001018}
1019
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001020/// property-synthesis:
1021/// @synthesize property-ivar-list ';'
1022///
1023/// property-ivar-list:
1024/// property-ivar
1025/// property-ivar-list ',' property-ivar
1026///
1027/// property-ivar:
1028/// identifier
1029/// identifier '=' identifier
1030///
1031Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1032 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1033 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1034 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001035 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001036 Diag(Tok, diag::err_expected_ident);
1037 return 0;
1038 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001039 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001040 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001041 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001042 // property '=' ivar-name
1043 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001044 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001045 Diag(Tok, diag::err_expected_ident);
1046 break;
1047 }
1048 ConsumeToken(); // consume ivar-name
1049 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001050 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001051 break;
1052 ConsumeToken(); // consume ','
1053 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001054 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001055 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1056 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001057}
1058
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001059/// property-dynamic:
1060/// @dynamic property-list
1061///
1062/// property-list:
1063/// identifier
1064/// property-list ',' identifier
1065///
1066Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1067 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1068 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1069 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001070 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001071 Diag(Tok, diag::err_expected_ident);
1072 return 0;
1073 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001074 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001075 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001076 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001077 break;
1078 ConsumeToken(); // consume ','
1079 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001080 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001081 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1082 return 0;
1083}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001084
1085/// objc-throw-statement:
1086/// throw expression[opt];
1087///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001088Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1089 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001090 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001092 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001093 if (Res.isInvalid) {
1094 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001095 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001096 }
1097 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001098 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001099 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001100}
1101
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001102/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001103/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001104///
1105Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001106 ConsumeToken(); // consume synchronized
1107 if (Tok.isNot(tok::l_paren)) {
1108 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1109 return true;
1110 }
1111 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001112 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001113 if (Res.isInvalid) {
1114 SkipUntil(tok::semi);
1115 return true;
1116 }
1117 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001118 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001119 return true;
1120 }
1121 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001122 if (Tok.isNot(tok::l_brace)) {
1123 Diag (Tok, diag::err_expected_lbrace);
1124 return true;
1125 }
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001126 StmtResult SynchBody = ParseCompoundStatementBody();
1127 if (SynchBody.isInvalid)
1128 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1129 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001130}
1131
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001132/// objc-try-catch-statement:
1133/// @try compound-statement objc-catch-list[opt]
1134/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1135///
1136/// objc-catch-list:
1137/// @catch ( parameter-declaration ) compound-statement
1138/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1139/// catch-parameter-declaration:
1140/// parameter-declaration
1141/// '...' [OBJC2]
1142///
Chris Lattner80712392008-03-10 06:06:04 +00001143Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001144 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001145
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001146 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001147 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001148 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001149 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001150 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001151 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001152 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001153 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001154 if (TryBody.isInvalid)
1155 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001156
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001157 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001158 // At this point, we need to lookahead to determine if this @ is the start
1159 // of an @catch or @finally. We don't want to consume the @ token if this
1160 // is an @try or @encode or something else.
1161 Token AfterAt = GetLookAheadToken(1);
1162 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1163 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1164 break;
1165
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001166 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001167 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001168 StmtTy *FirstPart = 0;
1169 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001170 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001171 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001172 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001173 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001174 DeclSpec DS;
1175 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001176 // FIXME: Is BlockContext right?
1177 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1178 ParseDeclarator(DeclaratorInfo);
Chris Lattnera4ff4272008-03-13 06:29:04 +00001179 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1180 DeclaratorInfo, 0);
1181 StmtResult stmtResult =
1182 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1183 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian06798362007-11-01 23:59:59 +00001184 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroffc949a462008-02-05 21:27:35 +00001185 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001186 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001187 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001188
1189 StmtResult CatchBody(true);
1190 if (Tok.is(tok::l_brace))
1191 CatchBody = ParseCompoundStatementBody();
1192 else
1193 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001194 if (CatchBody.isInvalid)
1195 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001196 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001197 FirstPart, CatchBody.Val, CatchStmts.Val);
1198 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001199 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001200 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1201 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001202 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001203 }
1204 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001205 } else {
1206 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001207 ConsumeToken(); // consume finally
Chris Lattner8027be62008-02-14 19:27:54 +00001208
1209 StmtResult FinallyBody(true);
1210 if (Tok.is(tok::l_brace))
1211 FinallyBody = ParseCompoundStatementBody();
1212 else
1213 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001214 if (FinallyBody.isInvalid)
1215 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001216 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001217 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001218 catch_or_finally_seen = true;
1219 break;
1220 }
1221 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001222 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001223 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001224 return true;
1225 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001226 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001227 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001228}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001229
Steve Naroff81f1bba2007-09-06 21:24:23 +00001230/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001231///
Steve Naroff18c83382007-11-13 23:01:27 +00001232Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001233 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001234 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001235 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001236 ConsumeToken();
1237
Steve Naroff9191a9e82007-11-11 19:54:21 +00001238 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001239 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001240 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001241
1242 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1243 SkipUntil(tok::l_brace, true, true);
1244
1245 // If we didn't find the '{', bail out.
1246 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001247 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001248 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001249 SourceLocation BraceLoc = Tok.getLocation();
1250
1251 // Enter a scope for the method body.
1252 EnterScope(Scope::FnScope|Scope::DeclScope);
1253
1254 // Tell the actions module that we have entered a method definition with the
1255 // specified Declarator for the method.
Ted Kremenek42730c52008-01-07 19:49:32 +00001256 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001257
1258 StmtResult FnBody = ParseCompoundStatementBody();
1259
1260 // If the function body could not be parsed, make a bogus compoundstmt.
1261 if (FnBody.isInvalid)
1262 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1263
1264 // Leave the function body scope.
1265 ExitScope();
1266
1267 // TODO: Pass argument information.
Steve Naroff99ee4302007-11-11 23:20:51 +00001268 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001269 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001270}
Anders Carlssona66cad42007-08-21 17:43:55 +00001271
Steve Naroffc949a462008-02-05 21:27:35 +00001272Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1273 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001274 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001275 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1276 return ParseObjCThrowStmt(AtLoc);
1277 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1278 return ParseObjCSynchronizedStmt(AtLoc);
1279 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1280 if (Res.isInvalid) {
1281 // If the expression is invalid, skip ahead to the next semicolon. Not
1282 // doing this opens us up to the possibility of infinite loops if
1283 // ParseExpression does not consume any tokens.
1284 SkipUntil(tok::semi);
1285 return true;
1286 }
1287 // Otherwise, eat the semicolon.
1288 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1289 return Actions.ActOnExprStmt(Res.Val);
1290}
1291
Steve Narofffb9dd752007-10-15 20:55:58 +00001292Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001293
1294 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001295 case tok::string_literal: // primary-expression: string-literal
1296 case tok::wide_string_literal:
1297 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1298 default:
1299 break;
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001300 }
1301
1302 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001303 case tok::objc_encode:
1304 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1305 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001306 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001307 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001308 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001309 default:
1310 Diag(AtLoc, diag::err_unexpected_at);
1311 SkipUntil(tok::semi);
Chris Lattnerfd44db32008-01-30 21:20:25 +00001312 return true;
Anders Carlssona66cad42007-08-21 17:43:55 +00001313 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001314}
1315
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001316/// objc-message-expr:
1317/// '[' objc-receiver objc-message-args ']'
1318///
1319/// objc-receiver:
1320/// expression
1321/// class-name
1322/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001323Parser::ExprResult Parser::ParseObjCMessageExpression() {
1324 assert(Tok.is(tok::l_square) && "'[' expected");
1325 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1326
1327 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001328 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001329 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1330 ConsumeToken();
1331 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1332 }
1333
1334 ExprResult Res = ParseAssignmentExpression();
1335 if (Res.isInvalid) {
1336 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattnere69015d2008-01-25 19:43:26 +00001337 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001338 return Res;
1339 }
1340 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1341}
1342
1343/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1344/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001345///
1346/// objc-message-args:
1347/// objc-selector
1348/// objc-keywordarg-list
1349///
1350/// objc-keywordarg-list:
1351/// objc-keywordarg
1352/// objc-keywordarg-list objc-keywordarg
1353///
1354/// objc-keywordarg:
1355/// selector-name[opt] ':' objc-keywordexpr
1356///
1357/// objc-keywordexpr:
1358/// nonempty-expr-list
1359///
1360/// nonempty-expr-list:
1361/// assignment-expression
1362/// nonempty-expr-list , assignment-expression
1363///
Chris Lattnered27a532008-01-25 18:59:06 +00001364Parser::ExprResult
1365Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1366 IdentifierInfo *ReceiverName,
1367 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001368 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001369 SourceLocation Loc;
1370 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001371
1372 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1373 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1374
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001375 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001376 while (1) {
1377 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001378 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001379
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001380 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001381 Diag(Tok, diag::err_expected_colon);
1382 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001383 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001384 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001385 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001386 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001387 ExprResult Res = ParseAssignmentExpression();
1388 if (Res.isInvalid) {
1389 SkipUntil(tok::identifier);
1390 return Res;
1391 }
1392 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001393 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001394
1395 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001396 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001397 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001398 break;
1399 // We have a selector or a colon, continue parsing.
1400 }
1401 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001402 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001403 ConsumeToken(); // Eat the ','.
1404 /// Parse the expression after ','
1405 ExprResult Res = ParseAssignmentExpression();
1406 if (Res.isInvalid) {
1407 SkipUntil(tok::identifier);
1408 return Res;
1409 }
1410 // We have a valid expression.
1411 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001412 }
1413 } else if (!selIdent) {
1414 Diag(Tok, diag::err_expected_ident); // missing selector name.
1415 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001416 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001417 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001418
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001419 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001420 Diag(Tok, diag::err_expected_rsquare);
1421 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001422 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001423 }
Chris Lattnered27a532008-01-25 18:59:06 +00001424 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001425
Steve Narofff9e80db2007-10-05 18:42:47 +00001426 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001427 if (nKeys == 0)
1428 KeyIdents.push_back(selIdent);
1429 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1430
1431 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001432 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001433 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001434 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001435 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001436 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001437 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001438}
1439
Steve Naroff0add5d22007-11-03 11:27:19 +00001440Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001441 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001442 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001443
1444 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1445 // expressions. At this point, we know that the only valid thing that starts
1446 // with '@' is an @"".
1447 llvm::SmallVector<SourceLocation, 4> AtLocs;
1448 llvm::SmallVector<ExprTy*, 4> AtStrings;
1449 AtLocs.push_back(AtLoc);
1450 AtStrings.push_back(Res.Val);
1451
1452 while (Tok.is(tok::at)) {
1453 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001454
Chris Lattnerddd3e632007-12-12 01:04:12 +00001455 ExprResult Res(true); // Invalid unless there is a string literal.
1456 if (isTokenStringLiteral())
1457 Res = ParseStringLiteralExpression();
1458 else
1459 Diag(Tok, diag::err_objc_concat_string);
1460
1461 if (Res.isInvalid) {
1462 while (!AtStrings.empty()) {
1463 Actions.DeleteExpr(AtStrings.back());
1464 AtStrings.pop_back();
1465 }
1466 return Res;
1467 }
1468
1469 AtStrings.push_back(Res.Val);
1470 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001471
Chris Lattnerddd3e632007-12-12 01:04:12 +00001472 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1473 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001474}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001475
1476/// objc-encode-expression:
1477/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001478Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001479 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001480
1481 SourceLocation EncLoc = ConsumeToken();
1482
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001483 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001484 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1485 return true;
1486 }
1487
1488 SourceLocation LParenLoc = ConsumeParen();
1489
1490 TypeTy *Ty = ParseTypeName();
1491
Anders Carlsson92faeb82007-08-23 15:31:37 +00001492 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001493
Chris Lattnercfd61c82007-10-16 22:51:17 +00001494 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001495 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001496}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001497
1498/// objc-protocol-expression
1499/// @protocol ( protocol-name )
1500
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001501Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001502{
1503 SourceLocation ProtoLoc = ConsumeToken();
1504
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001505 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001506 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1507 return true;
1508 }
1509
1510 SourceLocation LParenLoc = ConsumeParen();
1511
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001512 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001513 Diag(Tok, diag::err_expected_ident);
1514 return true;
1515 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001516 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001517 ConsumeToken();
1518
Anders Carlsson92faeb82007-08-23 15:31:37 +00001519 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001520
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001521 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1522 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001523}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001524
1525/// objc-selector-expression
1526/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001527Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001528{
1529 SourceLocation SelectorLoc = ConsumeToken();
1530
1531 if (Tok.isNot(tok::l_paren)) {
1532 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1533 return 0;
1534 }
1535
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001536 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001537 SourceLocation LParenLoc = ConsumeParen();
1538 SourceLocation sLoc;
1539 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1540 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001541 Diag(Tok, diag::err_expected_ident); // missing selector name.
1542 return 0;
1543 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001544 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001545 unsigned nColons = 0;
1546 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001547 while (1) {
1548 if (Tok.isNot(tok::colon)) {
1549 Diag(Tok, diag::err_expected_colon);
1550 break;
1551 }
Chris Lattner847f5c12007-12-27 19:57:00 +00001552 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001553 ConsumeToken(); // Eat the ':'.
1554 if (Tok.is(tok::r_paren))
1555 break;
1556 // Check for another keyword selector.
1557 SourceLocation Loc;
1558 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001559 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001560 if (!SelIdent && Tok.isNot(tok::colon))
1561 break;
1562 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001563 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001564 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001565 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001566 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001567 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001568 }