blob: d495e6f062bd676415acc5d78c60885a16a82d4d [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;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000134 llvm::SmallVector<IdentifierLocPair, 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.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000188 llvm::SmallVector<IdentifierLocPair, 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) {
Ted Kremenek8c945b12008-06-06 16:45:15 +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.
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000273 Selector GetterSel =
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000274 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
275 ? OCDS.getGetterName()
276 : FD.D.getIdentifier());
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000277 Selector SetterSel =
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000278 PP.getSelectorTable().getNullarySelector(OCDS.getSetterName()
279 ? OCDS.getSetterName()
280 // FIXME. This is not right!
281 : FD.D.getIdentifier());
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000282 DeclTy *Property = Actions.ActOnProperty(CurScope,
Steve Naroff638d6a42008-05-22 23:24:08 +0000283 AtLoc, FD, OCDS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000284 GetterSel, SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000285 MethodImplKind);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000286 allProperties.push_back(Property);
287 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000288 continue;
289 } else {
290 Diag(Tok, diag::err_objc_illegal_interface_qual);
291 ConsumeToken();
292 }
293 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000294 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
295 DeclTy *methodPrototype =
296 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000297 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000298 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
299 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000300 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000301 continue;
302 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000303 else if (Tok.is(tok::at))
304 continue;
305
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000306 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000307 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000308 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000309 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000310 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000311 // FIXME: as the name implies, this rule allows function definitions.
312 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000313 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000314 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000315 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000316 /// Insert collected methods declarations into the @interface object.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000317 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
318 allMethods.empty() ? 0 : &allMethods[0],
319 allMethods.size(),
320 allProperties.empty() ? 0 : &allProperties[0],
321 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000322}
323
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000324/// Parse property attribute declarations.
325///
326/// property-attr-decl: '(' property-attrlist ')'
327/// property-attrlist:
328/// property-attribute
329/// property-attrlist ',' property-attribute
330/// property-attribute:
331/// getter '=' identifier
332/// setter '=' identifier ':'
333/// readonly
334/// readwrite
335/// assign
336/// retain
337/// copy
338/// nonatomic
339///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000340void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000341 SourceLocation loc = ConsumeParen(); // consume '('
342 while (isObjCPropertyAttribute()) {
343 const IdentifierInfo *II = Tok.getIdentifierInfo();
344 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000345 if (II == ObjCPropertyAttrs[objc_getter] ||
346 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000347 // skip getter/setter part.
348 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000349 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000350 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000351 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000352 if (II == ObjCPropertyAttrs[objc_setter]) {
353 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000354 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000355 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000356 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000357 Diag(loc, diag::err_expected_colon);
358 SkipUntil(tok::r_paren,true,true);
359 break;
360 }
361 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000362 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000363 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000364 DS.setGetterName(Tok.getIdentifierInfo());
365 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000366 }
367 else {
368 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000369 SkipUntil(tok::r_paren,true,true);
370 break;
371 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000372 }
373 else {
374 Diag(loc, diag::err_objc_expected_equal);
375 SkipUntil(tok::r_paren,true,true);
376 break;
377 }
378 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000379
Ted Kremenek42730c52008-01-07 19:49:32 +0000380 else if (II == ObjCPropertyAttrs[objc_readonly])
381 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
382 else if (II == ObjCPropertyAttrs[objc_assign])
383 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
384 else if (II == ObjCPropertyAttrs[objc_readwrite])
385 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
386 else if (II == ObjCPropertyAttrs[objc_retain])
387 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
388 else if (II == ObjCPropertyAttrs[objc_copy])
389 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
390 else if (II == ObjCPropertyAttrs[objc_nonatomic])
391 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000392
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000393 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000394 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000395 loc = ConsumeToken();
396 continue;
397 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000398 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000399 break;
400 Diag(loc, diag::err_expected_rparen);
401 SkipUntil(tok::semi);
402 return;
403 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000404 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000405 ConsumeParen();
406 else {
407 Diag(loc, diag::err_objc_expected_property_attr);
408 SkipUntil(tok::r_paren); // recover from error inside attribute list
409 }
410}
411
Steve Naroff81f1bba2007-09-06 21:24:23 +0000412/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000413/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000414/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000415///
416/// objc-instance-method: '-'
417/// objc-class-method: '+'
418///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000419/// objc-method-attributes: [OBJC2]
420/// __attribute__((deprecated))
421///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000422Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000423 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000424 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000425
426 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000427 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000428
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000429 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000430 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000431 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000432 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000433}
434
435/// objc-selector:
436/// identifier
437/// one of
438/// enum struct union if else while do for switch case default
439/// break continue return goto asm sizeof typeof __alignof
440/// unsigned long const short volatile signed restrict _Complex
441/// in out inout bycopy byref oneway int char float double void _Bool
442///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000443IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000444 switch (Tok.getKind()) {
445 default:
446 return 0;
447 case tok::identifier:
448 case tok::kw_typeof:
449 case tok::kw___alignof:
450 case tok::kw_auto:
451 case tok::kw_break:
452 case tok::kw_case:
453 case tok::kw_char:
454 case tok::kw_const:
455 case tok::kw_continue:
456 case tok::kw_default:
457 case tok::kw_do:
458 case tok::kw_double:
459 case tok::kw_else:
460 case tok::kw_enum:
461 case tok::kw_extern:
462 case tok::kw_float:
463 case tok::kw_for:
464 case tok::kw_goto:
465 case tok::kw_if:
466 case tok::kw_inline:
467 case tok::kw_int:
468 case tok::kw_long:
469 case tok::kw_register:
470 case tok::kw_restrict:
471 case tok::kw_return:
472 case tok::kw_short:
473 case tok::kw_signed:
474 case tok::kw_sizeof:
475 case tok::kw_static:
476 case tok::kw_struct:
477 case tok::kw_switch:
478 case tok::kw_typedef:
479 case tok::kw_union:
480 case tok::kw_unsigned:
481 case tok::kw_void:
482 case tok::kw_volatile:
483 case tok::kw_while:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000484 case tok::kw_bool:
Chris Lattnerd031a452007-10-07 02:00:24 +0000485 case tok::kw__Bool:
486 case tok::kw__Complex:
487 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000488 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000489 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000490 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000491}
492
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000493/// property-attrlist: one of
494/// readonly getter setter assign retain copy nonatomic
495///
496bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000497 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000498 const IdentifierInfo *II = Tok.getIdentifierInfo();
499 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000500 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000501 }
502 return false;
503}
504
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000505/// objc-for-collection-in: 'in'
506///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000507bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000508 // FIXME: May have to do additional look-ahead to only allow for
509 // valid tokens following an 'in'; such as an identifier, unary operators,
510 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000511 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
512 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000513}
514
Ted Kremenek42730c52008-01-07 19:49:32 +0000515/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000516/// qualifier list and builds their bitmask representation in the input
517/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000518///
519/// objc-type-qualifiers:
520/// objc-type-qualifier
521/// objc-type-qualifiers objc-type-qualifier
522///
Ted Kremenek42730c52008-01-07 19:49:32 +0000523void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000524 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000525 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000526 return;
527
528 const IdentifierInfo *II = Tok.getIdentifierInfo();
529 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000530 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000531 continue;
532
Ted Kremenek42730c52008-01-07 19:49:32 +0000533 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000534 switch (i) {
535 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000536 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
537 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
538 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
539 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
540 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
541 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000542 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000543 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000544 ConsumeToken();
545 II = 0;
546 break;
547 }
548
549 // If this wasn't a recognized qualifier, bail out.
550 if (II) return;
551 }
552}
553
554/// objc-type-name:
555/// '(' objc-type-qualifiers[opt] type-name ')'
556/// '(' objc-type-qualifiers[opt] ')'
557///
Ted Kremenek42730c52008-01-07 19:49:32 +0000558Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000559 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000560
561 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000562 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000563
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000564 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000565 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000566
Steve Naroff0bbffd82007-08-22 16:35:03 +0000567 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000568 Ty = ParseTypeName();
569 // FIXME: back when Sema support is in place...
570 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000571 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000572 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000573 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000574 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000575 }
576 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000577 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000578}
579
580/// objc-method-decl:
581/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000582/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000583/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000584/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000585///
586/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000587/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000588/// objc-keyword-selector objc-keyword-decl
589///
590/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000591/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
592/// objc-selector ':' objc-keyword-attributes[opt] identifier
593/// ':' objc-type-name objc-keyword-attributes[opt] identifier
594/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000595///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000596/// objc-parmlist:
597/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000598///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000599/// objc-parms:
600/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000601///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000602/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000603/// , ...
604///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000605/// objc-keyword-attributes: [OBJC2]
606/// __attribute__((unused))
607///
Steve Naroff3774dd92007-10-26 20:53:56 +0000608Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000609 tok::TokenKind mType,
610 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000611 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000612{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000613 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000614 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000615 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000616 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000617 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000618 SourceLocation selLoc;
619 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000620 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000621 if (!SelIdent) {
622 Diag(Tok, diag::err_expected_ident); // missing selector name.
623 // FIXME: this creates a unary selector with a null identifier, is this
624 // ok?? Maybe we should skip to the next semicolon or something.
625 }
626
627 // If attributes exist after the method, parse them.
628 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000629 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000630 MethodAttrs = ParseAttributes();
631
632 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000633 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000634 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000635 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000636 }
Steve Naroff304ed392007-09-05 23:30:30 +0000637
Steve Naroff4ed9d662007-09-27 14:38:14 +0000638 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
639 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000640 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000641 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000642
643 Action::TypeTy *TypeInfo;
644 while (1) {
645 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000646
Chris Lattnerd031a452007-10-07 02:00:24 +0000647 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000648 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000649 Diag(Tok, diag::err_expected_colon);
650 break;
651 }
652 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000653 ObjCDeclSpec DSType;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000654 if (Tok.is(tok::l_paren)) { // Parse the argument type.
655 TypeInfo = ParseObjCTypeName(DSType);
656 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000657 else
658 TypeInfo = 0;
659 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000660 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000661
Chris Lattnerd031a452007-10-07 02:00:24 +0000662 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000663 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000664 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000665
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000666 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000667 Diag(Tok, diag::err_expected_ident); // missing argument name.
668 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000669 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000670 ArgNames.push_back(Tok.getIdentifierInfo());
671 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000672
Chris Lattnerd031a452007-10-07 02:00:24 +0000673 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000674 SourceLocation Loc;
675 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000676 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000677 break;
678 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000679 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000680
Steve Naroff29fe7462007-11-15 12:35:21 +0000681 bool isVariadic = false;
682
Chris Lattnerd031a452007-10-07 02:00:24 +0000683 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000684 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000685 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000686 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000687 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000688 ConsumeToken();
689 break;
690 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000691 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000692 // Parse the c-style argument declaration-specifier.
693 DeclSpec DS;
694 ParseDeclarationSpecifiers(DS);
695 // Parse the declarator.
696 Declarator ParmDecl(DS, Declarator::PrototypeContext);
697 ParseDeclarator(ParmDecl);
698 }
699
700 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000701 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000702 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000703 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000704 MethodAttrs = ParseAttributes();
705
706 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
707 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000708 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000709 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000710 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000711 &ArgNames[0], MethodAttrs,
712 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000713}
714
Steve Narofffb367882007-08-20 21:31:48 +0000715/// objc-protocol-refs:
716/// '<' identifier-list '>'
717///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000718bool Parser::
719ParseObjCProtocolReferences(llvm::SmallVectorImpl<IdentifierLocPair> &Protocols,
720 SourceLocation &endLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000721 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000722
723 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000724
725 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000726 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000727 Diag(Tok, diag::err_expected_ident);
728 SkipUntil(tok::greater);
729 return true;
730 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000731 Protocols.push_back(std::make_pair(Tok.getIdentifierInfo(),
732 Tok.getLocation()));
Steve Narofffb367882007-08-20 21:31:48 +0000733 ConsumeToken();
734
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000735 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000736 break;
737 ConsumeToken();
738 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000739
Steve Narofffb367882007-08-20 21:31:48 +0000740 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000741 if (Tok.is(tok::greater)) {
742 endLoc = ConsumeAnyToken();
743 return false;
744 }
745 Diag(Tok, diag::err_expected_greater);
746 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000747}
748
749/// objc-class-instance-variables:
750/// '{' objc-instance-variable-decl-list[opt] '}'
751///
752/// objc-instance-variable-decl-list:
753/// objc-visibility-spec
754/// objc-instance-variable-decl ';'
755/// ';'
756/// objc-instance-variable-decl-list objc-visibility-spec
757/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
758/// objc-instance-variable-decl-list ';'
759///
760/// objc-visibility-spec:
761/// @private
762/// @protected
763/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000764/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000765///
766/// objc-instance-variable-decl:
767/// struct-declaration
768///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000769void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
770 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000771 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000772 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000773 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
774
Steve Naroffc4474992007-08-21 21:17:12 +0000775 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000776
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000777 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000778 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000779 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000780 // Each iteration of this loop reads one objc-instance-variable-decl.
781
782 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000783 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000784 Diag(Tok, diag::ext_extra_struct_semi);
785 ConsumeToken();
786 continue;
787 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000788
Steve Naroffc4474992007-08-21 21:17:12 +0000789 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000790 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000791 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000792 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000793 case tok::objc_private:
794 case tok::objc_public:
795 case tok::objc_protected:
796 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000797 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000798 ConsumeToken();
799 continue;
800 default:
801 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000802 continue;
803 }
804 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000805
806 // Parse all the comma separated declarators.
807 DeclSpec DS;
808 FieldDeclarators.clear();
809 ParseStructDeclaration(DS, FieldDeclarators);
810
811 // Convert them all to fields.
812 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
813 FieldDeclarator &FD = FieldDeclarators[i];
814 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000815 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000816 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000817 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000818 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000819 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000820
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000821 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000822 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000823 } else {
824 Diag(Tok, diag::err_expected_semi_decl_list);
825 // Skip to end of block or statement
826 SkipUntil(tok::r_brace, true, true);
827 }
828 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000829 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000830 // Call ActOnFields() even if we don't have any decls. This is useful
831 // for code rewriting tools that need to be aware of the empty list.
832 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
833 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000834 LBraceLoc, RBraceLoc);
Steve Naroffc4474992007-08-21 21:17:12 +0000835 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000836}
Steve Narofffb367882007-08-20 21:31:48 +0000837
838/// objc-protocol-declaration:
839/// objc-protocol-definition
840/// objc-protocol-forward-reference
841///
842/// objc-protocol-definition:
843/// @protocol identifier
844/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000845/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000846/// @end
847///
848/// objc-protocol-forward-reference:
849/// @protocol identifier-list ';'
850///
851/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000852/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000853/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000854Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000855 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000856 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
857 ConsumeToken(); // the "protocol" identifier
858
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000859 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000860 Diag(Tok, diag::err_expected_ident); // missing protocol name.
861 return 0;
862 }
863 // Save the protocol name, then consume it.
864 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
865 SourceLocation nameLoc = ConsumeToken();
866
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000867 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000868 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000869 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000870 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000871 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000872
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000873 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000874 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
875 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
876
Steve Naroff72f17fb2007-08-22 22:17:26 +0000877 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000878 while (1) {
879 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000880 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000881 Diag(Tok, diag::err_expected_ident);
882 SkipUntil(tok::semi);
883 return 0;
884 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000885 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
886 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000887 ConsumeToken(); // the identifier
888
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000889 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000890 break;
891 }
892 // Consume the ';'.
893 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
894 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000895
Steve Naroff415c1832007-10-10 17:32:04 +0000896 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000897 &ProtocolRefs[0],
898 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000899 }
900
Steve Naroff72f17fb2007-08-22 22:17:26 +0000901 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000902 SourceLocation endProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000903 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
904
905 if (Tok.is(tok::less) &&
906 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
907 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000908
Steve Naroff415c1832007-10-10 17:32:04 +0000909 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000910 protocolName, nameLoc,
911 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000912 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000913 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000914
915 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000916 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000917 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000918 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000919 }
920 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000921 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000922}
Steve Narofffb367882007-08-20 21:31:48 +0000923
924/// objc-implementation:
925/// objc-class-implementation-prologue
926/// objc-category-implementation-prologue
927///
928/// objc-class-implementation-prologue:
929/// @implementation identifier objc-superclass[opt]
930/// objc-class-instance-variables[opt]
931///
932/// objc-category-implementation-prologue:
933/// @implementation identifier ( identifier )
934
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000935Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
936 SourceLocation atLoc) {
937 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
938 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
939 ConsumeToken(); // the "implementation" identifier
940
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000941 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000942 Diag(Tok, diag::err_expected_ident); // missing class or category name.
943 return 0;
944 }
945 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000946 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000947 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
948
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000949 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000950 // we have a category implementation.
951 SourceLocation lparenLoc = ConsumeParen();
952 SourceLocation categoryLoc, rparenLoc;
953 IdentifierInfo *categoryId = 0;
954
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000955 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000956 categoryId = Tok.getIdentifierInfo();
957 categoryLoc = ConsumeToken();
958 } else {
959 Diag(Tok, diag::err_expected_ident); // missing category name.
960 return 0;
961 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000963 Diag(Tok, diag::err_expected_rparen);
964 SkipUntil(tok::r_paren, false); // don't stop at ';'
965 return 0;
966 }
967 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000968 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000969 atLoc, nameId, nameLoc, categoryId,
970 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000971 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000972 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000973 }
974 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000975 SourceLocation superClassLoc;
976 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000977 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000978 // We have a super class
979 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000980 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000981 Diag(Tok, diag::err_expected_ident); // missing super class name.
982 return 0;
983 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000984 superClassId = Tok.getIdentifierInfo();
985 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000986 }
Steve Naroff415c1832007-10-10 17:32:04 +0000987 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +0000988 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000989 superClassId, superClassLoc);
990
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000991 if (Tok.is(tok::l_brace)) // we have ivars
992 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000993 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000994
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000995 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000996}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000997
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000998Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
999 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1000 "ParseObjCAtEndDeclaration(): Expected @end");
1001 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001002 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001003 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001004 else
1005 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001006 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001007}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001008
1009/// compatibility-alias-decl:
1010/// @compatibility_alias alias-name class-name ';'
1011///
1012Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1013 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1014 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1015 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001016 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001017 Diag(Tok, diag::err_expected_ident);
1018 return 0;
1019 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001020 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1021 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001022 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001023 Diag(Tok, diag::err_expected_ident);
1024 return 0;
1025 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001026 IdentifierInfo *classId = Tok.getIdentifierInfo();
1027 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1028 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001029 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001030 return 0;
1031 }
1032 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1033 aliasId, aliasLoc,
1034 classId, classLoc);
1035 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001036}
1037
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001038/// property-synthesis:
1039/// @synthesize property-ivar-list ';'
1040///
1041/// property-ivar-list:
1042/// property-ivar
1043/// property-ivar-list ',' property-ivar
1044///
1045/// property-ivar:
1046/// identifier
1047/// identifier '=' identifier
1048///
1049Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1050 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1051 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001052 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001053 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001054 Diag(Tok, diag::err_expected_ident);
1055 return 0;
1056 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001058 IdentifierInfo *propertyIvar = 0;
1059 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1060 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001061 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001062 // property '=' ivar-name
1063 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001064 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001065 Diag(Tok, diag::err_expected_ident);
1066 break;
1067 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001068 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001069 ConsumeToken(); // consume ivar-name
1070 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001071 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1072 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001073 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001074 break;
1075 ConsumeToken(); // consume ','
1076 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001077 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001078 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1079 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001080}
1081
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001082/// property-dynamic:
1083/// @dynamic property-list
1084///
1085/// property-list:
1086/// identifier
1087/// property-list ',' identifier
1088///
1089Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1090 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1091 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1092 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001093 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001094 Diag(Tok, diag::err_expected_ident);
1095 return 0;
1096 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001097 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001098 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1099 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1100 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1101 propertyId, 0);
1102
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001103 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001104 break;
1105 ConsumeToken(); // consume ','
1106 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001107 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001108 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1109 return 0;
1110}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001111
1112/// objc-throw-statement:
1113/// throw expression[opt];
1114///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001115Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1116 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001117 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001118 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001119 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001120 if (Res.isInvalid) {
1121 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001122 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001123 }
1124 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001125 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001126 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001127}
1128
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001129/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001130/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001131///
1132Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001133 ConsumeToken(); // consume synchronized
1134 if (Tok.isNot(tok::l_paren)) {
1135 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1136 return true;
1137 }
1138 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001139 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001140 if (Res.isInvalid) {
1141 SkipUntil(tok::semi);
1142 return true;
1143 }
1144 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001145 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001146 return true;
1147 }
1148 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001149 if (Tok.isNot(tok::l_brace)) {
1150 Diag (Tok, diag::err_expected_lbrace);
1151 return true;
1152 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001153 // Enter a scope to hold everything within the compound stmt. Compound
1154 // statements can always hold declarations.
1155 EnterScope(Scope::DeclScope);
1156
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001157 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001158
1159 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001160 if (SynchBody.isInvalid)
1161 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1162 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001163}
1164
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001165/// objc-try-catch-statement:
1166/// @try compound-statement objc-catch-list[opt]
1167/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1168///
1169/// objc-catch-list:
1170/// @catch ( parameter-declaration ) compound-statement
1171/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1172/// catch-parameter-declaration:
1173/// parameter-declaration
1174/// '...' [OBJC2]
1175///
Chris Lattner80712392008-03-10 06:06:04 +00001176Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001177 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001178
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001179 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001180 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001181 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001182 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001183 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001184 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001185 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001186 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001187 if (TryBody.isInvalid)
1188 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001189
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001190 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001191 // At this point, we need to lookahead to determine if this @ is the start
1192 // of an @catch or @finally. We don't want to consume the @ token if this
1193 // is an @try or @encode or something else.
1194 Token AfterAt = GetLookAheadToken(1);
1195 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1196 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1197 break;
1198
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001199 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001200 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001201 StmtTy *FirstPart = 0;
1202 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001203 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001204 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001205 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001206 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001207 DeclSpec DS;
1208 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001209 // For some odd reason, the name of the exception variable is
1210 // optional. As a result, we need to use PrototypeContext.
1211 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001212 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001213 if (DeclaratorInfo.getIdentifier()) {
1214 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Chris Lattnera4ff4272008-03-13 06:29:04 +00001215 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001216 StmtResult stmtResult =
1217 Actions.ActOnDeclStmt(aBlockVarDecl,
1218 DS.getSourceRange().getBegin(),
1219 DeclaratorInfo.getSourceRange().getEnd());
1220 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1221 }
Steve Naroffc949a462008-02-05 21:27:35 +00001222 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001223 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001224 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001225
1226 StmtResult CatchBody(true);
1227 if (Tok.is(tok::l_brace))
1228 CatchBody = ParseCompoundStatementBody();
1229 else
1230 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001231 if (CatchBody.isInvalid)
1232 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001233 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001234 FirstPart, CatchBody.Val, CatchStmts.Val);
1235 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001236 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001237 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1238 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001239 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001240 }
1241 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001242 } else {
1243 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001244 ConsumeToken(); // consume finally
Chris Lattner8027be62008-02-14 19:27:54 +00001245
1246 StmtResult FinallyBody(true);
1247 if (Tok.is(tok::l_brace))
1248 FinallyBody = ParseCompoundStatementBody();
1249 else
1250 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001251 if (FinallyBody.isInvalid)
1252 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001253 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001254 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001255 catch_or_finally_seen = true;
1256 break;
1257 }
1258 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001259 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001260 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001261 return true;
1262 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001263 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001264 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001265}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001266
Steve Naroff81f1bba2007-09-06 21:24:23 +00001267/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001268///
Steve Naroff18c83382007-11-13 23:01:27 +00001269Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001270 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001271 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001272 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001273 ConsumeToken();
1274
Steve Naroff9191a9e82007-11-11 19:54:21 +00001275 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001276 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001277 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001278
1279 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1280 SkipUntil(tok::l_brace, true, true);
1281
1282 // If we didn't find the '{', bail out.
1283 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001284 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001285 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001286 SourceLocation BraceLoc = Tok.getLocation();
1287
1288 // Enter a scope for the method body.
1289 EnterScope(Scope::FnScope|Scope::DeclScope);
1290
1291 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001292 // specified Declarator for the method.
1293 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001294
1295 StmtResult FnBody = ParseCompoundStatementBody();
1296
1297 // If the function body could not be parsed, make a bogus compoundstmt.
1298 if (FnBody.isInvalid)
1299 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1300
1301 // Leave the function body scope.
1302 ExitScope();
1303
1304 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001305 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001306 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001307}
Anders Carlssona66cad42007-08-21 17:43:55 +00001308
Steve Naroffc949a462008-02-05 21:27:35 +00001309Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1310 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001311 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001312 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1313 return ParseObjCThrowStmt(AtLoc);
1314 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1315 return ParseObjCSynchronizedStmt(AtLoc);
1316 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1317 if (Res.isInvalid) {
1318 // If the expression is invalid, skip ahead to the next semicolon. Not
1319 // doing this opens us up to the possibility of infinite loops if
1320 // ParseExpression does not consume any tokens.
1321 SkipUntil(tok::semi);
1322 return true;
1323 }
1324 // Otherwise, eat the semicolon.
1325 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1326 return Actions.ActOnExprStmt(Res.Val);
1327}
1328
Steve Narofffb9dd752007-10-15 20:55:58 +00001329Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001330
1331 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001332 case tok::string_literal: // primary-expression: string-literal
1333 case tok::wide_string_literal:
1334 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1335 default:
1336 break;
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001337 }
1338
1339 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001340 case tok::objc_encode:
1341 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1342 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001343 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001344 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001345 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001346 default:
1347 Diag(AtLoc, diag::err_unexpected_at);
1348 SkipUntil(tok::semi);
Chris Lattnerfd44db32008-01-30 21:20:25 +00001349 return true;
Anders Carlssona66cad42007-08-21 17:43:55 +00001350 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001351}
1352
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001353/// objc-message-expr:
1354/// '[' objc-receiver objc-message-args ']'
1355///
1356/// objc-receiver:
1357/// expression
1358/// class-name
1359/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001360Parser::ExprResult Parser::ParseObjCMessageExpression() {
1361 assert(Tok.is(tok::l_square) && "'[' expected");
1362 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1363
1364 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001365 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001366 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1367 ConsumeToken();
1368 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1369 }
1370
1371 ExprResult Res = ParseAssignmentExpression();
1372 if (Res.isInvalid) {
1373 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattnere69015d2008-01-25 19:43:26 +00001374 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001375 return Res;
1376 }
1377 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1378}
1379
1380/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1381/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001382///
1383/// objc-message-args:
1384/// objc-selector
1385/// objc-keywordarg-list
1386///
1387/// objc-keywordarg-list:
1388/// objc-keywordarg
1389/// objc-keywordarg-list objc-keywordarg
1390///
1391/// objc-keywordarg:
1392/// selector-name[opt] ':' objc-keywordexpr
1393///
1394/// objc-keywordexpr:
1395/// nonempty-expr-list
1396///
1397/// nonempty-expr-list:
1398/// assignment-expression
1399/// nonempty-expr-list , assignment-expression
1400///
Chris Lattnered27a532008-01-25 18:59:06 +00001401Parser::ExprResult
1402Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1403 IdentifierInfo *ReceiverName,
1404 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001405 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001406 SourceLocation Loc;
1407 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001408
1409 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1410 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1411
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001412 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001413 while (1) {
1414 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001415 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001416
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001417 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001418 Diag(Tok, diag::err_expected_colon);
1419 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001420 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001421 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001422 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001423 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001424 ExprResult Res = ParseAssignmentExpression();
1425 if (Res.isInvalid) {
1426 SkipUntil(tok::identifier);
1427 return Res;
1428 }
1429 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001430 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001431
1432 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001433 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001434 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001435 break;
1436 // We have a selector or a colon, continue parsing.
1437 }
1438 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001439 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001440 ConsumeToken(); // Eat the ','.
1441 /// Parse the expression after ','
1442 ExprResult Res = ParseAssignmentExpression();
1443 if (Res.isInvalid) {
1444 SkipUntil(tok::identifier);
1445 return Res;
1446 }
1447 // We have a valid expression.
1448 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001449 }
1450 } else if (!selIdent) {
1451 Diag(Tok, diag::err_expected_ident); // missing selector name.
1452 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001453 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001454 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001455
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001456 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001457 Diag(Tok, diag::err_expected_rsquare);
1458 SkipUntil(tok::semi);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001459 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001460 }
Chris Lattnered27a532008-01-25 18:59:06 +00001461 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001462
Steve Narofff9e80db2007-10-05 18:42:47 +00001463 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001464 if (nKeys == 0)
1465 KeyIdents.push_back(selIdent);
1466 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1467
1468 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001469 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001470 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001471 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001472 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001473 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001474 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001475}
1476
Steve Naroff0add5d22007-11-03 11:27:19 +00001477Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001478 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001479 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001480
1481 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1482 // expressions. At this point, we know that the only valid thing that starts
1483 // with '@' is an @"".
1484 llvm::SmallVector<SourceLocation, 4> AtLocs;
1485 llvm::SmallVector<ExprTy*, 4> AtStrings;
1486 AtLocs.push_back(AtLoc);
1487 AtStrings.push_back(Res.Val);
1488
1489 while (Tok.is(tok::at)) {
1490 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001491
Chris Lattnerddd3e632007-12-12 01:04:12 +00001492 ExprResult Res(true); // Invalid unless there is a string literal.
1493 if (isTokenStringLiteral())
1494 Res = ParseStringLiteralExpression();
1495 else
1496 Diag(Tok, diag::err_objc_concat_string);
1497
1498 if (Res.isInvalid) {
1499 while (!AtStrings.empty()) {
1500 Actions.DeleteExpr(AtStrings.back());
1501 AtStrings.pop_back();
1502 }
1503 return Res;
1504 }
1505
1506 AtStrings.push_back(Res.Val);
1507 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001508
Chris Lattnerddd3e632007-12-12 01:04:12 +00001509 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1510 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001511}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001512
1513/// objc-encode-expression:
1514/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001515Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001516 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001517
1518 SourceLocation EncLoc = ConsumeToken();
1519
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001520 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001521 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1522 return true;
1523 }
1524
1525 SourceLocation LParenLoc = ConsumeParen();
1526
1527 TypeTy *Ty = ParseTypeName();
1528
Anders Carlsson92faeb82007-08-23 15:31:37 +00001529 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001530
Chris Lattnercfd61c82007-10-16 22:51:17 +00001531 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001532 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001533}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001534
1535/// objc-protocol-expression
1536/// @protocol ( protocol-name )
1537
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001538Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001539{
1540 SourceLocation ProtoLoc = ConsumeToken();
1541
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001542 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001543 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1544 return true;
1545 }
1546
1547 SourceLocation LParenLoc = ConsumeParen();
1548
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001549 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001550 Diag(Tok, diag::err_expected_ident);
1551 return true;
1552 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001553 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001554 ConsumeToken();
1555
Anders Carlsson92faeb82007-08-23 15:31:37 +00001556 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001557
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001558 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1559 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001560}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001561
1562/// objc-selector-expression
1563/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001564Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001565{
1566 SourceLocation SelectorLoc = ConsumeToken();
1567
1568 if (Tok.isNot(tok::l_paren)) {
1569 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1570 return 0;
1571 }
1572
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001573 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001574 SourceLocation LParenLoc = ConsumeParen();
1575 SourceLocation sLoc;
1576 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1577 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001578 Diag(Tok, diag::err_expected_ident); // missing selector name.
1579 return 0;
1580 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001581 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001582 unsigned nColons = 0;
1583 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001584 while (1) {
1585 if (Tok.isNot(tok::colon)) {
1586 Diag(Tok, diag::err_expected_colon);
1587 break;
1588 }
Chris Lattner847f5c12007-12-27 19:57:00 +00001589 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001590 ConsumeToken(); // Eat the ':'.
1591 if (Tok.is(tok::r_paren))
1592 break;
1593 // Check for another keyword selector.
1594 SourceLocation Loc;
1595 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001596 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001597 if (!SelIdent && Tok.isNot(tok::colon))
1598 break;
1599 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001600 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001601 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001602 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001603 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001604 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001605 }